| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 6 |
use RadiusTheme\SB\Helpers\Cache; |
| 7 |
use RadiusTheme\SB\Helpers\Fns; |
| 8 |
use RadiusTheme\SB\Models\TemplateSettings; |
| 9 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 10 |
|
| 11 |
// Do not allow directly accessing this file. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit( 'This script cannot be accessed directly.' ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Default Template Switch. |
| 18 |
*/ |
| 19 |
class DefaultTemplate { |
| 20 |
/** |
| 21 |
* Singleton Trait. |
| 22 |
*/ |
| 23 |
use SingletonTrait; |
| 24 |
|
| 25 |
/** |
| 26 |
* Construct function |
| 27 |
*/ |
| 28 |
private function __construct() { |
| 29 |
add_action( 'wp_ajax_rtsb_default_template', [ $this, 'response' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Set Default Template. |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function response() { |
| 38 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { |
| 39 |
wp_send_json_error(); |
| 40 |
} |
| 41 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 42 |
wp_send_json_error(); |
| 43 |
} |
| 44 |
$page_type = isset( $_POST['template_type'] ) ? sanitize_text_field( wp_unslash( $_POST['template_type'] ) ) : 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 |
} |
| 67 |
|
| 68 |
if ( $is_can_set_default ) { |
| 69 |
$singleton_types = apply_filters( |
| 70 |
'rtsb/builder/singleton_types', |
| 71 |
array_keys( BuilderFns::builder_page_types() ) |
| 72 |
); |
| 73 |
|
| 74 |
if ( in_array( $page_type, $singleton_types, true ) ) { |
| 75 |
// Singleton path: one enabled template per type — overwrite the type-wide option. |
| 76 |
TemplateSettings::instance()->set_option( $option_name, $default_page_id ); |
| 77 |
} elseif ( $page_id ) { |
| 78 |
// Pool path: multi-enabled type — flip per-post meta flag, don't touch the type-wide option. |
| 79 |
if ( ! empty( $default_page_id ) ) { |
| 80 |
update_post_meta( $page_id, '_rtsb_tb_enabled_in_pool', 'on' ); |
| 81 |
} else { |
| 82 |
delete_post_meta( $page_id, '_rtsb_tb_enabled_in_pool' ); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
do_action( 'rtsb/set/builder/default/template', $_POST, $page_id ); |
| 88 |
|
| 89 |
$return = [ |
| 90 |
'post_id' => $default_page_id , |
| 91 |
'page_type' => $page_type, |
| 92 |
]; |
| 93 |
wp_send_json_success( $return ); |
| 94 |
wp_die(); |
| 95 |
} |
| 96 |
} |
| 97 |
|