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
247 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_code_name_exists', $code_manager_model, 'code_name_exists' ); |
| 37 | |
| 38 | $loader->add_action( 'wp_ajax_code_manager_get_code', $code_manager_model, 'get_code' ); |
| 39 | $loader->add_action( 'wp_ajax_nopriv_code_manager_get_code', $code_manager_model, 'get_code' ); |
| 40 | } else { |
| 41 | // Public actions |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Run shortcode |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | public function add_shortcode( $atts ) { |
| 53 | if ( self::code_manager_disabled() ) { |
| 54 | // Code manager disabled |
| 55 | return ''; |
| 56 | } |
| 57 | |
| 58 | global $pagenow; |
| 59 | if ( $pagenow === 'post.php' || $pagenow === 'edit.php' || $pagenow === 'post-new.php' ) { |
| 60 | // Prevent errors on execution if shortcode is shown in classic editor |
| 61 | return ''; |
| 62 | } |
| 63 | |
| 64 | if ( isset( $_SERVER["CONTENT_TYPE"] ) && 'application/json' === $_SERVER["CONTENT_TYPE"] ) { |
| 65 | // Prevent errors on execution if shortcode is shown in Gutenberg editor |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | $atts = array_change_key_case( (array) $atts, CASE_LOWER ); |
| 70 | $wp_atts = shortcode_atts( |
| 71 | [ |
| 72 | 'id' => '', |
| 73 | 'name' => '', |
| 74 | ], $atts |
| 75 | ); |
| 76 | |
| 77 | if ( '' === $wp_atts['id'] && '' === $wp_atts['name'] ) { |
| 78 | return ''; |
| 79 | } |
| 80 | |
| 81 | ob_start(); |
| 82 | |
| 83 | $ids = explode( ',', $wp_atts['id'] ); |
| 84 | foreach ( $ids as $id ) { |
| 85 | $this->run_shortcode_id( $id ); |
| 86 | } |
| 87 | |
| 88 | $names = explode( ',', $wp_atts['name'] ); |
| 89 | foreach ( $names as $name ) { |
| 90 | $this->run_shortcode_name( $name ); |
| 91 | } |
| 92 | |
| 93 | $content = ob_get_contents(); |
| 94 | ob_end_clean(); |
| 95 | |
| 96 | return $content; |
| 97 | } |
| 98 | |
| 99 | protected function run_shortcode_id( $id ) { |
| 100 | if ( '' !== $id ) { |
| 101 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 102 | $code_manager_model = new $code_manager_model_class(); |
| 103 | $code_row = $code_manager_model::dml_query( $id ); |
| 104 | if ( 1 === sizeof( $code_row ) ) { |
| 105 | if ( |
| 106 | 1 == $code_row[0]['code_enabled'] || |
| 107 | Code_Manager_Preview::is_code_id_preview_enabled( $id ) |
| 108 | ) { |
| 109 | $this->run_shortcode( $code_row[0]['code_type'], $code_row[0]['code'] ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | protected function run_shortcode_name( $name ) { |
| 116 | if ( '' !== $name ) { |
| 117 | $code_manager_model_class = CODE_MANAGER_MODEL_CLASS; |
| 118 | $code_manager_model = new $code_manager_model_class(); |
| 119 | $code_row = $code_manager_model::dml_query_by_name( $name ); |
| 120 | if ( 1 === sizeof( $code_row ) ) { |
| 121 | if ( |
| 122 | 1 == $code_row[0]['code_enabled'] || |
| 123 | Code_Manager_Preview::is_code_id_preview_enabled( $code_row[0]['code_id'] ) |
| 124 | ) { |
| 125 | $this->run_shortcode( $code_row[0]['code_type'], $code_row[0]['code'] ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Adds code de pending on the code type |
| 133 | * |
| 134 | * @since 1.0.0 |
| 135 | * |
| 136 | * @param string $code_type Code type (shortcodes only) |
| 137 | * @param string $code The code (PHP, JS, CSS or HTML) |
| 138 | */ |
| 139 | protected function run_shortcode( $code_type, $code ) { |
| 140 | if ( strpos( $code_type, 'html' ) !== false ) { |
| 141 | echo wp_unslash( $code ); |
| 142 | } elseif ( strpos( $code_type, 'css' ) !== false ) { |
| 143 | echo '<style type="text/css">' . wp_unslash( $code ) . '</style>'; |
| 144 | } elseif ( strpos( $code_type, 'javascript' ) !== false ) { |
| 145 | echo '<script type="text/javascript">' . wp_unslash( $code ) . '</script>'; |
| 146 | } elseif ( 'php shortcode' === $code_type) { |
| 147 | $this->add_php_code( $code, false ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public function run_shortcode_id_from_anywhere( $id ) { |
| 152 | $this->run_shortcode_id( $id ); |
| 153 | } |
| 154 | |
| 155 | public function run_shortcode_name_from_anywhere( $name ) { |
| 156 | $this->run_shortcode_name( $name ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Adds PHP code |
| 161 | * |
| 162 | * @since 1.0.0 |
| 163 | * |
| 164 | * @param string $php_code PHP code to be added |
| 165 | * @param bool $php7_required Indicates whether PHP7 is required for this code type |
| 166 | */ |
| 167 | protected function add_php_code( $php_code, $php7_required = true ) { |
| 168 | if ( self::is_code_manager_page() ) { |
| 169 | // Do not execute any code on Code Manager pages!!! |
| 170 | // This is an admins rescue in case code fails. |
| 171 | } else { |
| 172 | eval( $this->strip_code( $php_code ) ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Remove PHP opening and closing tags (when found) from given code |
| 178 | * |
| 179 | * @since 1.0.0 |
| 180 | * |
| 181 | * @param string $php_code PHP source code |
| 182 | * |
| 183 | * @return string PHP code without PHP opening and closing tags |
| 184 | */ |
| 185 | protected function strip_code( $php_code ) { |
| 186 | $php_code = rtrim( ltrim( $php_code ) ); |
| 187 | |
| 188 | if ( '<?php' === strtolower( substr( $php_code, 0, 5 ) ) ) { |
| 189 | $php_code = substr( $php_code, 5 ); |
| 190 | } |
| 191 | |
| 192 | if ( '?>' === substr( $php_code, strlen( $php_code ) - 2 ) ) { |
| 193 | $php_code = substr( $php_code, 0, strlen( $php_code ) - 2 ); |
| 194 | } |
| 195 | |
| 196 | return $php_code; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Checks if Code Manager is disabled |
| 201 | * |
| 202 | * (1) Disabled in settings page |
| 203 | * (2) Disabled in config file |
| 204 | * |
| 205 | * @since 1.0.0 |
| 206 | * |
| 207 | * @return bool TRUE - Code Manager is disabled |
| 208 | */ |
| 209 | public static function code_manager_disabled() { |
| 210 | $plugin_code_execution = get_option('code_manager_plugin_code_execution'); |
| 211 | if ( false === $plugin_code_execution ) { |
| 212 | $plugin_code_execution = 'on'; |
| 213 | } |
| 214 | |
| 215 | return 'on' !== $plugin_code_execution || ( defined( 'CODE_MANAGER_DISABLED' ) && CODE_MANAGER_DISABLED ); |
| 216 | } |
| 217 | |
| 218 | public static function is_code_manager_page() { |
| 219 | return ( |
| 220 | is_admin() && |
| 221 | isset( $_REQUEST['page'] ) && |
| 222 | ( |
| 223 | CODE_MANAGER_MENU_SLUG === $_REQUEST['page'] || |
| 224 | CODE_MANAGER_SETTINGS_MENU_SLUG === $_REQUEST['page'] || |
| 225 | 'code_manager_post' === $_REQUEST['page'] |
| 226 | ) |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | public static function get_current_user_login() { |
| 231 | global $current_user; |
| 232 | if ( isset( $current_user->user_login ) ) { |
| 233 | return $current_user->user_login; |
| 234 | } else { |
| 235 | $wp_user = wp_get_current_user(); |
| 236 | if ( isset( $wp_user->data->user_login ) ) { |
| 237 | return $wp_user->data->user_login; |
| 238 | } else { |
| 239 | return 'anonymous'; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | } |
| 247 |