| @@ -1,11 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Ajax requests handler |
| 4 | 4 | * |
| 5 | - * @author Webcraftic <[email protected]> | |
| 6 | - * @copyright (c) 2018 Webraftic Ltd | |
| 7 | - * @version 1.0 | |
| 5 | + * @package Woody_Code_Snippets | |
| 8 | 6 | */ |
| 9 | 7 | |
| 10 | 8 | // Exit if accessed directly |
| 11 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -17,13 +15,13 @@ | ||
| 17 | 15 | */ |
| 18 | 16 | function wbcr_inp_ajax_get_user_roles() { |
| 19 | 17 | global $wp_roles; |
| 20 | 18 | |
| 21 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 19 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 22 | 20 | wp_die( - 1, 403 ); |
| 23 | 21 | } |
| 24 | 22 | |
| 25 | - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' ); | |
| 23 | + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' ); | |
| 26 | 24 | |
| 27 | 25 | check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' ); |
| 28 | 26 | |
| 29 | 27 | $roles = $wp_roles->roles; |
| @@ -55,13 +53,13 @@ | ||
| 55 | 53 | * Returns a list of public post types. |
| 56 | 54 | */ |
| 57 | 55 | function wbcr_inp_ajax_get_post_types() { |
| 58 | 56 | |
| 59 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 57 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 60 | 58 | wp_die( - 1, 403 ); |
| 61 | 59 | } |
| 62 | 60 | |
| 63 | - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' ); | |
| 61 | + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' ); | |
| 64 | 62 | |
| 65 | 63 | check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' ); |
| 66 | 64 | |
| 67 | 65 | $values = []; |
| @@ -89,13 +87,13 @@ | ||
| 89 | 87 | * Returns a list of public taxonomies. |
| 90 | 88 | */ |
| 91 | 89 | function wbcr_inp_ajax_get_taxonomies() { |
| 92 | 90 | |
| 93 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 91 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 94 | 92 | wp_die( - 1, 403 ); |
| 95 | 93 | } |
| 96 | 94 | |
| 97 | - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' ); | |
| 95 | + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' ); | |
| 98 | 96 | |
| 99 | 97 | check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' ); |
| 100 | 98 | |
| 101 | 99 | $values = []; |
| @@ -124,13 +122,13 @@ | ||
| 124 | 122 | * Returns a list of page list values |
| 125 | 123 | */ |
| 126 | 124 | function wbcr_inp_ajax_get_page_list() { |
| 127 | 125 | |
| 128 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 126 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 129 | 127 | wp_die( - 1, 403 ); |
| 130 | 128 | } |
| 131 | 129 | |
| 132 | - $snippet_id = WINP_Plugin::app()->request->post( 'snippet_id', 0, 'intval' ); | |
| 130 | + $snippet_id = WINP_HTTP::post( 'snippet_id', 0, 'intval' ); | |
| 133 | 131 | |
| 134 | 132 | check_admin_referer( 'wbcr_inp_snippet_' . $snippet_id . '_conditions_metabox' ); |
| 135 | 133 | |
| 136 | 134 | $is_woo = WINP_Helper::is_woo_active(); |
| @@ -144,13 +142,13 @@ | ||
| 144 | 142 | 'title' => __( 'Entire Website', 'insert-php' ), |
| 145 | 143 | ], |
| 146 | 144 | [ |
| 147 | 145 | 'value' => 'base_sing', |
| 148 | - 'title' => __( 'All Singulars', 'insert-php' ), | |
| 146 | + 'title' => __( 'All Single Posts & Pages', 'insert-php' ), | |
| 149 | 147 | ], |
| 150 | 148 | [ |
| 151 | 149 | 'value' => 'base_arch', |
| 152 | - 'title' => __( 'All Archives', 'insert-php' ), | |
| 150 | + 'title' => __( 'All Archive Pages', 'insert-php' ), | |
| 153 | 151 | ], |
| 154 | 152 | ], |
| 155 | 153 | __( 'Special Pages', 'insert-php' ) => [ |
| 156 | 154 | [ |
| @@ -162,9 +160,9 @@ | ||
| 162 | 160 | 'title' => __( 'Search Page', 'insert-php' ), |
| 163 | 161 | ], |
| 164 | 162 | [ |
| 165 | 163 | 'value' => 'spec_blog', |
| 166 | - 'title' => __( 'Blog / Posts Page', 'insert-php' ), | |
| 164 | + 'title' => __( 'Blog/Posts Page', 'insert-php' ), | |
| 167 | 165 | ], |
| 168 | 166 | [ |
| 169 | 167 | 'value' => 'spec_front', |
| 170 | 168 | 'title' => __( 'Front Page', 'insert-php' ), |
| @@ -205,9 +203,9 @@ | ||
| 205 | 203 | 'value' => 'page_arch', |
| 206 | 204 | 'title' => __( 'All Pages Archive', 'insert-php' ), |
| 207 | 205 | ], |
| 208 | 206 | ], |
| 209 | - __( 'Woocommerce', 'insert-php' ) . $woo_desc => [ | |
| 207 | + __( 'WooCommerce', 'insert-php' ) . $woo_desc => [ | |
| 210 | 208 | [ |
| 211 | 209 | 'value' => 'woo_product', |
| 212 | 210 | 'title' => __( 'Product', 'insert-php' ), |
| 213 | 211 | 'disabled' => ! $is_woo, |
| @@ -213,34 +211,34 @@ | ||
| 213 | 211 | 'disabled' => ! $is_woo, |
| 214 | 212 | ], |
| 215 | 213 | [ |
| 216 | 214 | 'value' => 'woo_cart', |
| 217 | - 'title' => __( 'Cart page', 'insert-php' ), | |
| 215 | + 'title' => __( 'Cart Page', 'insert-php' ), | |
| 218 | 216 | 'disabled' => ! $is_woo, |
| 219 | 217 | ], |
| 220 | 218 | [ |
| 221 | 219 | 'value' => 'woo_checkout', |
| 222 | - 'title' => __( 'Checkout page', 'insert-php' ), | |
| 220 | + 'title' => __( 'Checkout Page', 'insert-php' ), | |
| 223 | 221 | 'disabled' => ! $is_woo, |
| 224 | 222 | ], |
| 225 | 223 | [ |
| 226 | 224 | 'value' => 'woo_checkout_pay', |
| 227 | - 'title' => __( 'Checkout pay page', 'insert-php' ), | |
| 225 | + 'title' => __( 'Checkout Payment Page', 'insert-php' ), | |
| 228 | 226 | 'disabled' => ! $is_woo, |
| 229 | 227 | ], |
| 230 | 228 | [ |
| 231 | 229 | 'value' => 'woo_arch', |
| 232 | - 'title' => __( 'All Products page', 'insert-php' ), | |
| 230 | + 'title' => __( 'All Products Page', 'insert-php' ), | |
| 233 | 231 | 'disabled' => ! $is_woo, |
| 234 | 232 | ], |
| 235 | 233 | [ |
| 236 | 234 | 'value' => 'woo_cat', |
| 237 | - 'title' => __( 'Products Category page', 'insert-php' ), | |
| 235 | + 'title' => __( 'Product Category Page', 'insert-php' ), | |
| 238 | 236 | 'disabled' => ! $is_woo, |
| 239 | 237 | ], |
| 240 | 238 | [ |
| 241 | 239 | 'value' => 'woo_tag', |
| 242 | - 'title' => __( 'Products Tag page', 'insert-php' ), | |
| 240 | + 'title' => __( 'Product Tag Page', 'insert-php' ), | |
| 243 | 241 | 'disabled' => ! $is_woo, |
| 244 | 242 | ], |
| 245 | 243 | ], |
| 246 | 244 | ], |
| @@ -256,18 +254,18 @@ | ||
| 256 | 254 | * Save the Permalink slug |
| 257 | 255 | */ |
| 258 | 256 | function wbcr_inp_ajax_save_permalink() { |
| 259 | 257 | |
| 260 | - if ( ! WINP_Plugin::app()->currentUserCan() ) { | |
| 258 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 261 | 259 | wp_die( - 1, 403 ); |
| 262 | 260 | } |
| 263 | 261 | |
| 264 | 262 | check_ajax_referer( 'winp-permalink', 'winp_permalink_nonce' ); |
| 265 | 263 | |
| 266 | - $code_id = WINP_Plugin::app()->request->post( 'code_id', 0 ); | |
| 267 | - $permalink = WINP_Plugin::app()->request->post( 'permalink', null, true ); | |
| 268 | - $slug = WINP_Plugin::app()->request->post( 'new_slug', null, 'sanitize_file_name' ); | |
| 269 | - $filetype = WINP_Plugin::app()->request->post( 'filetype', 'css', true ); | |
| 264 | + $code_id = WINP_HTTP::post( 'code_id', 0 ); | |
| 265 | + $permalink = WINP_HTTP::post( 'permalink', null, true ); | |
| 266 | + $slug = WINP_HTTP::post( 'new_slug', null, 'sanitize_file_name' ); | |
| 267 | + $filetype = WINP_HTTP::post( 'filetype', 'css', true ); | |
| 270 | 268 | |
| 271 | 269 | WINP_Helper::updateMetaOption( $code_id, 'filetype', $filetype ); |
| 272 | 270 | |
| 273 | 271 | if ( empty( $slug ) ) { |
| @@ -275,10 +273,56 @@ | ||
| 275 | 273 | WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', '' ); |
| 276 | 274 | } else { |
| 277 | 275 | WINP_Helper::updateMetaOption( $code_id, 'css_js_slug', $slug ); |
| 278 | 276 | } |
| 279 | - WINP_Plugin::app()->get_common_object()->editFormBeforePermalink( $slug, $permalink, $filetype ); | |
| 277 | + WINP_Plugin::app()->get_common_object()->edit_form_before_permalink( $slug, $permalink, $filetype ); | |
| 280 | 278 | |
| 281 | 279 | wp_die(); |
| 282 | 280 | } |
| 283 | 281 | |
| 284 | 282 | add_action( 'wp_ajax_winp_permalink', 'wbcr_inp_ajax_save_permalink' ); |
| 283 | + | |
| 284 | +/** | |
| 285 | + * Validate snippet code before saving (AJAX). | |
| 286 | + * | |
| 287 | + * @return void | |
| 288 | + */ | |
| 289 | +function wbcr_inp_ajax_validate_snippet() { | |
| 290 | + if ( ! WINP_Plugin::app()->current_user_car() ) { | |
| 291 | + wp_send_json_error( [ 'message' => __( 'You don\'t have permission to perform this action. Contact your administrator.', 'insert-php' ) ], 403 ); | |
| 292 | + } | |
| 293 | + | |
| 294 | + $post_id = WINP_HTTP::post( 'post_id', 0, 'intval' ); | |
| 295 | + | |
| 296 | + check_ajax_referer( 'winp_validate_snippet_' . $post_id, 'nonce' ); | |
| 297 | + | |
| 298 | + $snippet_code = WINP_HTTP::post( 'snippet_code', '', false ); | |
| 299 | + $snippet_type = WINP_HTTP::post( 'snippet_type', WINP_SNIPPET_TYPE_PHP, true ); | |
| 300 | + | |
| 301 | + // Only validate executable PHP snippets (not text, ad, css, js, html). | |
| 302 | + if ( WINP_SNIPPET_TYPE_TEXT !== $snippet_type && | |
| 303 | + WINP_SNIPPET_TYPE_AD !== $snippet_type && | |
| 304 | + WINP_SNIPPET_TYPE_CSS !== $snippet_type && | |
| 305 | + WINP_SNIPPET_TYPE_JS !== $snippet_type && | |
| 306 | + WINP_SNIPPET_TYPE_HTML !== $snippet_type ) { | |
| 307 | + | |
| 308 | + $snippet_code = stripslashes( $snippet_code ); | |
| 309 | + | |
| 310 | + $validation = WINP_Code_Validator::validate_code( $snippet_code, $snippet_type ); | |
| 311 | + | |
| 312 | + if ( ! $validation['valid'] ) { | |
| 313 | + wp_send_json_error( | |
| 314 | + [ | |
| 315 | + 'valid' => false, | |
| 316 | + 'message' => $validation['message'], | |
| 317 | + ] | |
| 318 | + ); | |
| 319 | + } | |
| 320 | + | |
| 321 | + wp_send_json_success( [ 'valid' => true ] ); | |
| 322 | + } else { | |
| 323 | + // No validation needed for this type. | |
| 324 | + wp_send_json_success( [ 'valid' => true ] ); | |
| 325 | + } | |
| 326 | +} | |
| 327 | + | |
| 328 | +add_action( 'wp_ajax_wbcr_inp_ajax_validate_snippet', 'wbcr_inp_ajax_validate_snippet' ); | |