| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 6 |
use RadiusTheme\SB\Helpers\Fns; |
| 7 |
use RadiusTheme\SB\Models\TemplateSettings; |
| 8 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 9 |
|
| 10 |
// Do not allow directly accessing this file. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit( 'This script cannot be accessed directly.' ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Default Template Switch. |
| 17 |
*/ |
| 18 |
class DefaultTemplate { |
| 19 |
/** |
| 20 |
* Singleton Trait. |
| 21 |
*/ |
| 22 |
use SingletonTrait; |
| 23 |
|
| 24 |
/** |
| 25 |
* Construct function |
| 26 |
*/ |
| 27 |
private function __construct() { |
| 28 |
add_action( 'wp_ajax_rtsb_default_template', [ $this, 'response' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Set Default Template. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function response() { |
| 37 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { |
| 38 |
wp_send_json_error(); |
| 39 |
} |
| 40 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 41 |
wp_send_json_error(); |
| 42 |
} |
| 43 |
$page_type = isset( $_POST['template_type'] ) ? sanitize_text_field( wp_unslash( $_POST['template_type'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 44 |
$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 |
| 45 |
$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 |
| 46 |
|
| 47 |
// For Specific Product |
| 48 |
$the_option = BuilderFns::option_name_by_template_id( $page_id ); |
| 49 |
$the_cat_option = BuilderFns::archive_option_name_by_template_id( $page_id ); |
| 50 |
|
| 51 |
// $product_ids = get_option( $the_option ); |
| 52 |
// $the_cats = get_option( $the_cat_option ); |
| 53 |
$product_ids = TemplateSettings::instance()->get_option( $the_option ); |
| 54 |
$the_cats = TemplateSettings::instance()->get_option( $the_cat_option ); |
| 55 |
if ( empty( $product_ids ) && empty( $the_cats ) ) { |
| 56 |
$option_name = BuilderFns::option_name( $page_type ); |
| 57 |
//update_option( $option_name, $default_page_id ); |
| 58 |
TemplateSettings::instance()->set_option( $option_name, $default_page_id ); |
| 59 |
} |
| 60 |
|
| 61 |
do_action( 'rtsb/set/builder/default/template', $_POST, $page_id ); |
| 62 |
|
| 63 |
$return = [ |
| 64 |
'post_id' => $default_page_id, |
| 65 |
'page_type' => $page_type, |
| 66 |
]; |
| 67 |
wp_send_json_success( $return ); |
| 68 |
wp_die(); |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|