| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 | 4 | |
| 5 | 5 | use RadiusTheme\SB\Helpers\BuilderFns; |
| 6 | +use RadiusTheme\SB\Helpers\Cache; | |
| 6 | 7 | use RadiusTheme\SB\Helpers\Fns; |
| 7 | 8 | use RadiusTheme\SB\Models\TemplateSettings; |
| 8 | 9 | use RadiusTheme\SB\Traits\SingletonTrait; |
| 9 | 10 | |
| @@ -33,27 +34,39 @@ | ||
| 33 | 34 | * |
| 34 | 35 | * @return void |
| 35 | 36 | */ |
| 36 | 37 | public function response() { |
| 37 | - if ( ! Fns::verify_nonce() ) { | |
| 38 | + if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { | |
| 38 | 39 | wp_send_json_error(); |
| 39 | 40 | } |
| 40 | - | |
| 41 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 42 | + wp_send_json_error(); | |
| 43 | + } | |
| 41 | 44 | $page_type = isset( $_POST['template_type'] ) ? sanitize_text_field( wp_unslash( $_POST['template_type'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 42 | - $default_page_id = isset( $_POST['set_default_page_id'] ) && 'publish' === get_post_status( $_POST['set_default_page_id'] ) ? absint( wp_unslash( $_POST['set_default_page_id'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 43 | - $page_id = isset( $_POST['page_id'] ) && 'publish' === get_post_status( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 45 | + $default_page_id = isset( $_POST['set_default_page_id'] ) && 'publish' === get_post_status( $_POST['set_default_page_id'] ) ? absint( wp_unslash( $_POST['set_default_page_id'] ) ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 46 | + $page_id = isset( $_POST['page_id'] ) && 'publish' === get_post_status( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 47 | + Cache::clear_transient_cache(); | |
| 48 | + $the_cat_option = BuilderFns::archive_option_name_by_template_id( $page_id ); | |
| 49 | + $the_cats = TemplateSettings::instance()->get_option( $the_cat_option ); | |
| 50 | + $option_name = BuilderFns::option_name( $page_type ); | |
| 51 | + $product_page_for = get_post_meta( $page_id, '_is_product_page_template_for', true ); | |
| 52 | + $is_can_set_default = true; | |
| 53 | + switch ( $page_type ) { | |
| 54 | + case 'product': | |
| 55 | + $product_page_for = empty( $product_page_for ) ? 'all_products' : $product_page_for; | |
| 56 | + if ( 'all_products' !== $product_page_for ) { | |
| 57 | + $is_can_set_default = null; | |
| 58 | + } | |
| 59 | + break; | |
| 60 | + case 'archive': | |
| 61 | + if ( ! empty( $the_cats ) ) { | |
| 62 | + $is_can_set_default = null; | |
| 63 | + } | |
| 64 | + break; | |
| 65 | + default: | |
| 66 | + } | |
| 44 | 67 | |
| 45 | - // For Specific Product | |
| 46 | - $the_option = BuilderFns::option_name_by_template_id( $page_id ); | |
| 47 | - $the_cat_option = BuilderFns::archive_option_name_by_template_id( $page_id ); | |
| 48 | - | |
| 49 | - // $product_ids = get_option( $the_option ); | |
| 50 | - // $the_cats = get_option( $the_cat_option ); | |
| 51 | - $product_ids = TemplateSettings::instance()->get_option( $the_option ); | |
| 52 | - $the_cats = TemplateSettings::instance()->get_option( $the_cat_option ); | |
| 53 | - if ( empty( $product_ids ) && empty( $the_cats ) ) { | |
| 54 | - $option_name = BuilderFns::option_name( $page_type ); | |
| 55 | - //update_option( $option_name, $default_page_id ); | |
| 68 | + if ( $is_can_set_default ) { | |
| 56 | 69 | TemplateSettings::instance()->set_option( $option_name, $default_page_id ); |
| 57 | 70 | } |
| 58 | 71 | |
| 59 | 72 | do_action( 'rtsb/set/builder/default/template', $_POST, $page_id ); |
| @@ -58,12 +71,11 @@ | ||
| 58 | 71 | |
| 59 | 72 | do_action( 'rtsb/set/builder/default/template', $_POST, $page_id ); |
| 60 | 73 | |
| 61 | 74 | $return = [ |
| 62 | - 'post_id' => $default_page_id, | |
| 75 | + 'post_id' => $default_page_id , | |
| 63 | 76 | 'page_type' => $page_type, |
| 64 | 77 | ]; |
| 65 | 78 | wp_send_json_success( $return ); |
| 66 | 79 | wp_die(); |
| 67 | 80 | } |
| 68 | - | |
| 69 | 81 | } |