<?php
defined('ABSPATH') or die('HEY, WHAT ARE YOU DOING HERE? YOU SILLY HUMAN!');
if (!class_exists('PAC_DA_Snippets')) {
    class PAC_DA_Snippets
    {
        private static $_instance;

        /**
         * Get Class Instance
         *
         * @return PAC_DA_Snippets
         */
        public static function instance()
        {
            if (self::$_instance == null) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        /**
         * Initializer Of The Class
         *
         * Add/Remove Necessary Actions/Filters
         */
        public function init()
        {
            $codeData = json_decode(get_option('pacDaCodeData', '{"css":false,"jquery":false,"php":false,"runPhp":false}'));
            $runPhp = isset($codeData->runPhp) ? $codeData->runPhp : false;
            $enableAddons = json_decode(get_option('pacDaAddons', '{"startUp":true,"maintenance":true,"utility":true,"media":true,"learning":true,"support":true,"fonts":true,"styles":true,"code":true,"frontend":true,"accessibility":true}'));
            $codeSettings = isset($enableAddons->code) ? $enableAddons->code : true;
            if(boolval($runPhp) && boolval($codeSettings)){
                add_action('plugins_loaded', [$this, 'maybe_execute_snippets']);
            }
        }


        public function run_php_code_with_eval($code) {
            ob_start();
            try {
                $result = eval($code); // phpcs:ignore
                if ($result === false) {
                    throw new Exception('Execution failed.');
                }
                return $result;
            } catch (ParseError $parseError) { // phpcs:ignore
                $codeData = json_decode(get_option('pacDaCodeData', '{"css":false,"jquery":false,"php":false,"runPhp":false}'));
                $codeData->runPhp = false;
                update_option( 'pacDaCodeData',wp_json_encode($codeData));
                return ['error' => $parseError->getMessage(), 'line' => $parseError->getLine()];
            } catch (Throwable $throwable) {
                $codeData = json_decode(get_option('pacDaCodeData', '{"css":false,"jquery":false,"php":false,"runPhp":false}'));
                $codeData->runPhp = false;
                update_option( 'pacDaCodeData',wp_json_encode($codeData));
                return ['error' => $throwable->getMessage()];
            } finally {
                ob_end_clean();
            }
        }
        

        /**
         * Execute Snippets
         *
         * @return void
         */
        public function maybe_execute_snippets()
        {
            $snippets = get_option('pacDaCustomSnippetPhp');
            $snippets = preg_replace('/^<\?php(.*)(\?>)?$/s', '$1', $snippets);
            $snippets = trim($snippets);
            if (!empty($snippets)) {
                $result = $this->run_php_code_with_eval($snippets);
                if (is_array($result) && isset($result['error'])) {
                    echo "Error: " . esc_attr($result['error']);
                }
            }
        }
    }
    (new PAC_DA_Snippets())->instance()->init();
}