| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 7 |
use RadiusTheme\SB\Helpers\Cache; |
| 8 |
use RadiusTheme\SB\Helpers\Fns; |
| 9 |
use RadiusTheme\SB\Models\TemplateSettings; |
| 10 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 11 |
|
| 12 |
// Do not allow directly accessing this file. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit( 'This script cannot be accessed directly.' ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Default Template Switch. |
| 19 |
*/ |
| 20 |
class CreateTemplate { |
| 21 |
/** |
| 22 |
* Singleton Trait. |
| 23 |
*/ |
| 24 |
use SingletonTrait; |
| 25 |
|
| 26 |
/** |
| 27 |
* Construct function |
| 28 |
*/ |
| 29 |
private function __construct() { |
| 30 |
add_action( 'wp_ajax_rtsb_builder_create_template', [ $this, 'response' ] ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Create template |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function response() { |
| 39 |
Cache::clear_transient_cache(); |
| 40 |
$page_type = isset( $_POST['page_type'] ) ? sanitize_text_field( wp_unslash( $_POST['page_type'] ) ) : null; // rtsb_tb_template_type - it represent to template type. |
| 41 |
$page_id = isset( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null; |
| 42 |
$page_name = isset( $_POST['page_name'] ) ? sanitize_text_field( wp_unslash( $_POST['page_name'] ) ) : null; |
| 43 |
$hasPro = isset( $_POST['hasPro'] ) ? sanitize_text_field( wp_unslash( $_POST['hasPro'] ) ) : null; |
| 44 |
$edit_with = isset( $_POST['template_edit_with'] ) ? sanitize_text_field( wp_unslash( $_POST['template_edit_with'] ) ) : null; |
| 45 |
$default_template = isset( $_POST['default_template'] ) ? sanitize_text_field( wp_unslash( $_POST['default_template'] ) ) : null; |
| 46 |
$import_layout = isset( $_POST['import_default_layout'] ) ? sanitize_text_field( $_POST['import_default_layout'] ) : null; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 47 |
$layoutJson = isset( $_POST['demoData'] ) ? $_POST['demoData'] : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 48 |
$product_id = isset( $_POST['preview_product_id'] ) ? absint( $_POST['preview_product_id'] ) : null; |
| 49 |
$url = '#'; |
| 50 |
|
| 51 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) || ! $page_type ) { |
| 52 |
$return = [ |
| 53 |
'success' => false, |
| 54 |
'post_id' => $page_id, |
| 55 |
]; |
| 56 |
wp_send_json( $return ); |
| 57 |
|
| 58 |
} |
| 59 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 60 |
$return = [ |
| 61 |
'success' => false, |
| 62 |
'post_id' => $page_id, |
| 63 |
]; |
| 64 |
wp_send_json( $return ); |
| 65 |
} |
| 66 |
|
| 67 |
add_filter( |
| 68 |
'pre_option_elementor_unfiltered_files_upload', |
| 69 |
function () { |
| 70 |
return '1'; |
| 71 |
}, |
| 72 |
99 |
| 73 |
); |
| 74 |
|
| 75 |
Plugin::$instance->files_manager->clear_cache(); |
| 76 |
add_filter( 'rtsb_import_status', '__return_true' ); |
| 77 |
|
| 78 |
$option_name = BuilderFns::option_name( $page_type ); |
| 79 |
|
| 80 |
$post_data = [ |
| 81 |
'ID' => $page_id, |
| 82 |
'post_title' => $page_name, |
| 83 |
'meta_input' => [ |
| 84 |
BuilderFns::template_type_meta_key() => $page_type, |
| 85 |
], |
| 86 |
]; |
| 87 |
if ( 'elementor' == $edit_with ) { |
| 88 |
$post_data['meta_input']['_elementor_edit_mode'] = 'builder'; |
| 89 |
} elseif ( 'gutenberg' == $edit_with ) { |
| 90 |
$post_data['meta_input']['_elementor_edit_mode'] = ''; |
| 91 |
} |
| 92 |
|
| 93 |
if ( $page_id ) { |
| 94 |
$page_id = wp_update_post( $post_data ); |
| 95 |
$new_page = false; |
| 96 |
} else { |
| 97 |
unset( $post_data['ID'] ); |
| 98 |
$post_data['post_type'] = BuilderFns::$post_type_tb; |
| 99 |
$post_data['post_status'] = 'publish'; |
| 100 |
$page_id = wp_insert_post( $post_data ); |
| 101 |
$new_page = true; |
| 102 |
if ( 'elementor' == $edit_with ) { |
| 103 |
update_post_meta( $page_id, '_wp_page_template', 'elementor_header_footer' ); |
| 104 |
update_post_meta( $page_id, '_elementor_version', ELEMENTOR_VERSION ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
$edit_by = ''; |
| 109 |
if ( $page_id ) { |
| 110 |
$edit_by = Fns::page_edit_with( $page_id ); |
| 111 |
$template_for = sanitize_text_field( wp_unslash( $_POST['product_page_for'] ?? '' ) ); |
| 112 |
$selected_category = array_unique( isset( $_POST['selected_category'] ) && is_array( $_POST['selected_category'] ) ? array_map( 'sanitize_text_field', $_POST['selected_category'] ) : [] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 113 |
if ( 'product' == $page_type ) { |
| 114 |
update_post_meta( $page_id, BuilderFns::$product_template_meta, $product_id ); |
| 115 |
update_post_meta( $page_id, '_is_product_page_template_for', $template_for ); |
| 116 |
} |
| 117 |
|
| 118 |
$has_default = TemplateSettings::instance()->get_option( $option_name ); |
| 119 |
if ( ( 'product' === $page_type && 'all_products' !== $template_for ) || ( 'archive' === $page_type && count( $selected_category ) ) ) { |
| 120 |
if ( ! $has_default ) { |
| 121 |
TemplateSettings::instance()->set_option( $option_name, '' ); |
| 122 |
} |
| 123 |
} else { |
| 124 |
if ( 'default_template' === $default_template ) { |
| 125 |
TemplateSettings::instance()->set_option( $option_name, $page_id ); |
| 126 |
} else { |
| 127 |
TemplateSettings::instance()->set_option( $option_name, '' ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$action = 'edit'; |
| 132 |
if ( 'elementor' === $edit_by ) { |
| 133 |
$action = 'elementor'; |
| 134 |
} |
| 135 |
$url = add_query_arg( |
| 136 |
[ |
| 137 |
'post' => $page_id, |
| 138 |
'action' => $action, |
| 139 |
], |
| 140 |
admin_url( 'post.php' ) |
| 141 |
); |
| 142 |
|
| 143 |
if ( ! empty( $layoutJson ) && is_array( $layoutJson ) ) { |
| 144 |
$content = wp_slash( wp_json_encode( $layoutJson ) ); |
| 145 |
update_post_meta( $page_id, '_elementor_data', $content ); |
| 146 |
update_post_meta( $page_id, '_rtsb_import_id', $import_layout ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
do_action( 'rtsb/after/create/and/edit/builder/template', $_POST, $page_id ); |
| 151 |
|
| 152 |
$return = [ |
| 153 |
'success' => true, |
| 154 |
'post_id' => $page_id, |
| 155 |
'post_edit_url' => $url, |
| 156 |
'new_page' => $new_page, |
| 157 |
|
| 158 |
]; |
| 159 |
if ( $edit_by ) { |
| 160 |
$return['edit_btn_text'] = sprintf( |
| 161 |
/* translators: %s: Edit */ |
| 162 |
esc_html__( 'Edit with %s', 'shopbuilder' ), |
| 163 |
esc_html( $edit_by ) |
| 164 |
); |
| 165 |
} |
| 166 |
wp_send_json( $return ); |
| 167 |
wp_die(); |
| 168 |
} |
| 169 |
} |
| 170 |
|