Code_Manager.php
5 years ago
Code_Manager_Export.php
5 years ago
Code_Manager_Form.php
5 years ago
Code_Manager_Import.php
5 years ago
Code_Manager_Import_File.php
5 years ago
Code_Manager_List.php
5 years ago
Code_Manager_List_View.php
5 years ago
Code_Manager_Model.php
5 years ago
Code_Manager_Preview.php
5 years ago
Code_Manager_Settings.php
5 years ago
Code_Manager_Tabs.php
5 years ago
Message_Box.php
5 years ago
WP_List_Table.php
5 years ago
Code_Manager.php
231 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Code_Manager { |
| 4 | |
| 5 | /** |
| 6 | * Class Code_Manager |
| 7 | * |
| 8 | * Add plugin actions and runs the code saved in the code manager table. |
| 9 | * |
| 10 | * @author Peter Schulz |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | class Code_Manager { |
| 14 | |
| 15 | /** |
| 16 | * Add Code Manager specific actions |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | public function add_actions( $loader ) { |
| 21 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 22 | $code_manager_model = new $code_manager_model_class(); |
| 23 | |
| 24 | if ( is_admin() ) { |
| 25 | // Admin actions |
| 26 | $loader->add_action( 'admin_action_code_manager_export', Code_Manager_Export::class, 'export' ); |
| 27 | $loader->add_action( 'wp_ajax_code_manager_export', Code_Manager_Export::class, 'export_ajax' ); |
| 28 | $loader->add_action( 'wp_ajax_nopriv_code_manager_export', Code_Manager_Export::class, 'export_ajax' ); |
| 29 | |
| 30 | $loader->add_action( 'wp_ajax_code_manager_update_code', $code_manager_model, 'update_code' ); |
| 31 | $loader->add_action( 'wp_ajax_code_manager_activate_code', $code_manager_model, 'activate_code' ); |
| 32 | $loader->add_action( 'wp_ajax_code_manager_activate_code_preview', $code_manager_model, 'activate_code_preview' ); |
| 33 | $loader->add_action( 'wp_ajax_code_manager_deactivate_code_preview', $code_manager_model, 'deactivate_code_preview' ); |
| 34 | $loader->add_action( 'wp_ajax_code_manager_reset_preview', $code_manager_model, 'reset_preview' ); |
| 35 | $loader->add_action( 'wp_ajax_code_manager_get_code_list', $code_manager_model, 'get_code_list' ); |
| 36 | $loader->add_action( 'wp_ajax_code_manager_get_code', $code_manager_model, 'get_code' ); |
| 37 | $loader->add_action( 'wp_ajax_code_manager_code_name_exists', $code_manager_model, 'code_name_exists' ); |
| 38 | } else { |
| 39 | // Public actions |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Run shortcode |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | public function add_shortcode( $atts ) { |
| 51 | if ( self::code_manager_disabled() ) { |
| 52 | // Code manager disabled |
| 53 | return ''; |
| 54 | } |
| 55 | |
| 56 | global $pagenow; |
| 57 | if ( $pagenow === 'post.php' || $pagenow === 'edit.php' || $pagenow === 'post-new.php' ) { |
| 58 | // Prevent errors on execution if shortcode is shown in classic editor |
| 59 | return ''; |
| 60 | } |
| 61 | |
| 62 | if ( isset( $_SERVER["CONTENT_TYPE"] ) && 'application/json' === $_SERVER["CONTENT_TYPE"] ) { |
| 63 | // Prevent errors on execution if shortcode is shown in Gutenberg editor |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | $atts = array_change_key_case( (array) $atts, CASE_LOWER ); |
| 68 | $wp_atts = shortcode_atts( |
| 69 | [ |
| 70 | 'id' => '', |
| 71 | 'name' => '', |
| 72 | ], $atts |
| 73 | ); |
| 74 | |
| 75 | if ( '' === $wp_atts['id'] && '' === $wp_atts['name'] ) { |
| 76 | return ''; |
| 77 | } |
| 78 | |
| 79 | ob_start(); |
| 80 | |
| 81 | $ids = explode( ',', $wp_atts['id'] ); |
| 82 | foreach ( $ids as $id ) { |
| 83 | $this->run_shortcode_id( $id ); |
| 84 | } |
| 85 | |
| 86 | $names = explode( ',', $wp_atts['name'] ); |
| 87 | foreach ( $names as $name ) { |
| 88 | $this->run_shortcode_name( $name ); |
| 89 | } |
| 90 | |
| 91 | $content = ob_get_contents(); |
| 92 | ob_end_clean(); |
| 93 | |
| 94 | return $content; |
| 95 | } |
| 96 | |
| 97 | protected function run_shortcode_id( $id ) { |
| 98 | if ( '' !== $id ) { |
| 99 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 100 | $code_manager_model = new $code_manager_model_class(); |
| 101 | $code_row = $code_manager_model::dml_query( $id ); |
| 102 | if ( 1 === sizeof( $code_row ) ) { |
| 103 | if ( |
| 104 | 1 == $code_row[0]['code_enabled'] || |
| 105 | Code_Manager_Preview::is_code_id_preview_enabled( $id ) |
| 106 | ) { |
| 107 | $this->run_shortcode( $code_row[0]['code_type'], $code_row[0]['code'] ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | protected function run_shortcode_name( $name ) { |
| 114 | if ( '' !== $name ) { |
| 115 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 116 | $code_manager_model = new $code_manager_model_class(); |
| 117 | $code_row = $code_manager_model::dml_query_by_name( $name ); |
| 118 | if ( 1 === sizeof( $code_row ) ) { |
| 119 | if ( |
| 120 | 1 == $code_row[0]['code_enabled'] || |
| 121 | Code_Manager_Preview::is_code_id_preview_enabled( $code_row[0]['code_id'] ) |
| 122 | ) { |
| 123 | $this->run_shortcode( $code_row[0]['code_type'], $code_row[0]['code'] ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Adds code de pending on the code type |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | * |
| 134 | * @param string $code_type Code type (shortcodes only) |
| 135 | * @param string $code The code (PHP, JS, CSS or HTML) |
| 136 | */ |
| 137 | protected function run_shortcode( $code_type, $code ) { |
| 138 | if ( strpos( $code_type, 'html' ) !== false ) { |
| 139 | echo wp_unslash( $code ); |
| 140 | } elseif ( strpos( $code_type, 'css' ) !== false ) { |
| 141 | echo '<style type="text/css">' . wp_unslash( $code ) . '</style>'; |
| 142 | } elseif ( strpos( $code_type, 'javascript' ) !== false ) { |
| 143 | echo '<script type="text/javascript">' . wp_unslash( $code ) . '</script>'; |
| 144 | } elseif ( 'php shortcode' === $code_type) { |
| 145 | $this->add_php_code( $code, false ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | public function run_shortcode_id_from_anywhere( $id ) { |
| 150 | $this->run_shortcode_id( $id ); |
| 151 | } |
| 152 | |
| 153 | public function run_shortcode_name_from_anywhere( $name ) { |
| 154 | $this->run_shortcode_name( $name ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Adds PHP code |
| 159 | * |
| 160 | * @since 1.0.0 |
| 161 | * |
| 162 | * @param string $php_code PHP code to be added |
| 163 | * @param bool $php7_required Indicates whether PHP7 is required for this code type |
| 164 | */ |
| 165 | protected function add_php_code( $php_code, $php7_required = true ) { |
| 166 | if ( self::is_code_manager_page() ) { |
| 167 | // Do not execute any code on Code Manager pages!!! |
| 168 | // This is an admins rescue in case code fails. |
| 169 | } else { |
| 170 | eval( $this->strip_code( $php_code ) ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Remove PHP opening and closing tags (when found) from given code |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | * |
| 179 | * @param string $php_code PHP source code |
| 180 | * |
| 181 | * @return string PHP code without PHP opening and closing tags |
| 182 | */ |
| 183 | protected function strip_code( $php_code ) { |
| 184 | $php_code = rtrim( ltrim( $php_code ) ); |
| 185 | |
| 186 | if ( '<?php' === strtolower( substr( $php_code, 0, 5 ) ) ) { |
| 187 | $php_code = substr( $php_code, 5 ); |
| 188 | } |
| 189 | |
| 190 | if ( '?>' === substr( $php_code, strlen( $php_code ) - 2 ) ) { |
| 191 | $php_code = substr( $php_code, 0, strlen( $php_code ) - 2 ); |
| 192 | } |
| 193 | |
| 194 | return $php_code; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Checks if Code Manager is disabled |
| 199 | * |
| 200 | * (1) Disabled in settings page |
| 201 | * (2) Disabled in config file |
| 202 | * |
| 203 | * @since 1.0.0 |
| 204 | * |
| 205 | * @return bool TRUE - Code Manager is disabled |
| 206 | */ |
| 207 | public static function code_manager_disabled() { |
| 208 | $plugin_code_execution = get_option('code_manager_plugin_code_execution'); |
| 209 | if ( false === $plugin_code_execution ) { |
| 210 | $plugin_code_execution = 'on'; |
| 211 | } |
| 212 | |
| 213 | return 'on' !== $plugin_code_execution || ( defined( 'CODE_MANAGER_DISABLED' ) && CODE_MANAGER_DISABLED ); |
| 214 | } |
| 215 | |
| 216 | public static function is_code_manager_page() { |
| 217 | return ( |
| 218 | is_admin() && |
| 219 | isset( $_REQUEST['page'] ) && |
| 220 | ( |
| 221 | CODE_MANAGER_MENU_SLUG === $_REQUEST['page'] || |
| 222 | CODE_MANAGER_SETTINGS_MENU_SLUG === $_REQUEST['page'] || |
| 223 | 'code_manager_post' === $_REQUEST['page'] |
| 224 | ) |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | } |
| 231 |