| @@ -15,12 +15,12 @@ | ||
| 15 | 15 | $file = MWCODE_PATH . '/classes/' . str_replace( 'meow_mwcode_', '', strtolower( $class ) ) . '.php'; |
| 16 | 16 | } |
| 17 | 17 | if ( strpos( $class, 'Meow_MWCODE_Modules' ) !== false ) { |
| 18 | 18 | $file = MWCODE_PATH . '/classes/modules/' . str_replace( 'meow_mwcode_modules_', '', strtolower( $class ) ) . '.php'; |
| 19 | - } else if ( strpos( $class, 'MeowKit_MWCODE_' ) !== false ) { | |
| 20 | - $file = MWCODE_PATH . '/common/' . str_replace( 'meowkit_mwcode_', '', strtolower( $class ) ) . '.php'; | |
| 21 | - } else if ( strpos( $class, 'MeowKitPro_MWCODE_' ) !== false ) { | |
| 22 | - $file = MWCODE_PATH . '/common/premium/' . str_replace( 'meowkitpro_mwcode_', '', strtolower( $class ) ) . '.php'; | |
| 19 | + } else if ( strpos( $class, 'MeowCommon_' ) !== false ) { | |
| 20 | + $file = MWCODE_PATH . '/common/' . str_replace( 'meowcommon_', '', strtolower( $class ) ) . '.php'; | |
| 21 | + } else if ( strpos( $class, 'MeowCommonPro_' ) !== false ) { | |
| 22 | + $file = MWCODE_PATH . '/common/premium/' . str_replace( 'meowcommonpro_', '', strtolower( $class ) ) . '.php'; | |
| 23 | 23 | } else if ( strpos( $class, 'MeowPro_MWCODE' ) !== false ) { |
| 24 | 24 | $file = MWCODE_PATH . '/premium/' . str_replace( 'meowpro_mwcode_', '', strtolower( $class ) ) . '.php'; |
| 25 | 25 | } |
| 26 | 26 | if ( $file && file_exists( $file ) ) { |
| @@ -27,8 +27,163 @@ | ||
| 27 | 27 | require( $file ); |
| 28 | 28 | } |
| 29 | 29 | } ); |
| 30 | 30 | |
| 31 | -require_once( MWCODE_PATH . '/classes/api.php' ); | |
| 31 | +//require_once( MWCODE_PATH . '/classes/api.php' ); | |
| 32 | 32 | |
| 33 | 33 | |
| 34 | +$file_path = MWCODE_PATH . '/classes/modules/helpers.php'; | |
| 35 | +if ( file_exists( $file_path ) ) { | |
| 36 | + require_once( $file_path ); | |
| 37 | +} | |
| 34 | 38 | |
| 39 | +$file_path_common = MWCODE_PATH . '/common/helpers.php'; | |
| 40 | +if ( file_exists( $file_path_common ) ) { | |
| 41 | + require_once( $file_path_common ); | |
| 42 | +} | |
| 43 | + | |
| 44 | + | |
| 45 | +// In admin or Rest API request ( REQUEST URI begins with '/wp-json/' ) | |
| 46 | +if ( is_admin( ) || MeowCommon_Helpers::is_rest( ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
| 47 | + global $mwcode_core; | |
| 48 | + $mwcode_core = new Meow_MWCODE_Core( ); | |
| 49 | +} | |
| 50 | + | |
| 51 | +// Define a global variable to store the currently executing snippet | |
| 52 | +global $current_mwcode_snippet; | |
| 53 | + | |
| 54 | +/** | |
| 55 | + * Custom error handler | |
| 56 | + */ | |
| 57 | +function mwcode_error_handler( $errno, $errstr, $errfile, $errline ) | |
| 58 | +{ | |
| 59 | + global $current_mwcode_snippet, $mwcode_core; | |
| 60 | + | |
| 61 | + if ( !isset( $current_mwcode_snippet ) ) { | |
| 62 | + return false; | |
| 63 | + } | |
| 64 | + | |
| 65 | + if ( !isset( $mwcode_core ) ) { | |
| 66 | + $mwcode_core = new Meow_MWCODE_Core( ); | |
| 67 | + } | |
| 68 | + | |
| 69 | + if ( !( error_reporting( ) & $errno ) ) { | |
| 70 | + // This error code is not included in error_reporting | |
| 71 | + return false; | |
| 72 | + } | |
| 73 | + | |
| 74 | + if ( !array_key_exists( 'name', $current_mwcode_snippet ) ) { | |
| 75 | + return false; | |
| 76 | + } | |
| 77 | + | |
| 78 | + $error = "$errstr in $errfile on line $errline"; | |
| 79 | + | |
| 80 | + $mwcode_core->log( "⚠️ Error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" ); | |
| 81 | + $mwcode_core->log( $error ); | |
| 82 | + | |
| 83 | + $mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - $errstr" ); | |
| 84 | + $mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet ); | |
| 85 | + | |
| 86 | + // Don't execute PHP internal error handler | |
| 87 | + return true; | |
| 88 | +} | |
| 89 | + | |
| 90 | +/** | |
| 91 | + * Shutdown function to catch fatal errors | |
| 92 | + */ | |
| 93 | +function mwcode_shutdown_function( ) | |
| 94 | +{ | |
| 95 | + global $current_mwcode_snippet, $mwcode_core; | |
| 96 | + | |
| 97 | + if ( !isset( $current_mwcode_snippet ) ) { | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + | |
| 101 | + if ( !isset( $mwcode_core ) ) { | |
| 102 | + $mwcode_core = new Meow_MWCODE_Core( ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + $error = error_get_last( ); | |
| 106 | + | |
| 107 | + $fatal_error = $error !== null && in_array( $error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR] ); | |
| 108 | + | |
| 109 | + if ( $fatal_error ) { | |
| 110 | + | |
| 111 | + $mwcode_core->log( "⚠️ Fatal error in snippet: " . $current_mwcode_snippet["name"] . " ( ID: " . $current_mwcode_snippet["id"] . " )" ); | |
| 112 | + $mwcode_core->log( $error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line'] ); | |
| 113 | + | |
| 114 | + $mwcode_core->update_option( 'fatal_error', "\"{$current_mwcode_snippet["name"]}\" - " . $error['message'] ); | |
| 115 | + $mwcode_core->update_option( 'thrown_snippet', $current_mwcode_snippet ); | |
| 116 | + | |
| 117 | + } | |
| 118 | +} | |
| 119 | + | |
| 120 | +// Set the custom error handler and shutdown function | |
| 121 | +set_error_handler( "mwcode_error_handler" ); | |
| 122 | +register_shutdown_function( "mwcode_shutdown_function" ); | |
| 123 | + | |
| 124 | +// Execute all active snippets | |
| 125 | +add_action( 'plugins_loaded', function ( ) { | |
| 126 | + global $mwcode_core, $current_mwcode_snippet; | |
| 127 | + if ( !isset( $mwcode_core ) ) { | |
| 128 | + $mwcode_core = new Meow_MWCODE_Core( ); | |
| 129 | + } | |
| 130 | + $snippets = $mwcode_core->execute_active_snippets( ); | |
| 131 | + $scheduled = new Meow_MWCODE_Modules_Cron( $mwcode_core ); | |
| 132 | + | |
| 133 | + if ( empty( $snippets ) ) { | |
| 134 | + return; | |
| 135 | + } | |
| 136 | + | |
| 137 | + foreach ( $snippets as $snippet ) { | |
| 138 | + | |
| 139 | + if ( $snippet['blocked'] ) { | |
| 140 | + $mwcode_core->log( "⚠️ Snippet is blocked: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " ) Because it's not a frontend request, on a non authorized page." ); | |
| 141 | + continue; | |
| 142 | + } | |
| 143 | + | |
| 144 | + $current_mwcode_snippet = $snippet; | |
| 145 | + | |
| 146 | + ob_start( ); | |
| 147 | + | |
| 148 | + try { | |
| 149 | + eval( $snippet['code'] ); | |
| 150 | + } catch ( Throwable $e ) { | |
| 151 | + $mwcode_core->log( "⚠️ Exception in snippet: " . $snippet['name'] . " ( ID: " . $snippet['id'] . " )" ); | |
| 152 | + $mwcode_core->log( $e->getMessage( ) ); | |
| 153 | + $mwcode_core->update_option( 'fatal_error', "\"{$snippet["name"]}\" - " . $e->getMessage( ) ); | |
| 154 | + $mwcode_core->update_option( 'thrown_snippet', $snippet ); | |
| 155 | + } | |
| 156 | + | |
| 157 | + ob_end_clean( ); | |
| 158 | + } | |
| 159 | + | |
| 160 | + $current_mwcode_snippet = null; | |
| 161 | +} ); | |
| 162 | + | |
| 163 | +// Load all the JS functions to the front & back | |
| 164 | +// TODO: Add some restrictions by pages | |
| 165 | +function enqueue_mwcode_scripts( ) | |
| 166 | +{ | |
| 167 | + global $mwcode_core; | |
| 168 | + | |
| 169 | + wp_register_script( 'mwcode-scripts-js', '' ); | |
| 170 | + wp_enqueue_script( 'mwcode-scripts-js' ); | |
| 171 | + | |
| 172 | + $js_code = ''; | |
| 173 | + | |
| 174 | + if ( is_admin( ) ) { | |
| 175 | + $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null; | |
| 176 | + if ( $page === 'mwai_settings' ) { | |
| 177 | + $js_code = $mwcode_core->get_js_functions_to_push( ); | |
| 178 | + } | |
| 179 | + } else { | |
| 180 | + $js_code = $mwcode_core->get_js_functions_to_push( ); | |
| 181 | + } | |
| 182 | + | |
| 183 | + if ( $js_code ) { | |
| 184 | + wp_add_inline_script( 'mwcode-scripts-js', $js_code, 'after' ); | |
| 185 | + } | |
| 186 | +} | |
| 187 | + | |
| 188 | +add_action( 'wp_enqueue_scripts', 'enqueue_mwcode_scripts' ); | |
| 189 | +add_action( 'admin_enqueue_scripts', 'enqueue_mwcode_scripts' ); | |