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