| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\TemplateLibrary\Source_Base; |
| 7 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 8 |
use RadiusTheme\SB\Helpers\Cache; |
| 9 |
use RadiusTheme\SB\Helpers\Fns; |
| 10 |
use RadiusTheme\SB\Models\TemplateSettings; |
| 11 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 12 |
|
| 13 |
// Do not allow directly accessing this file. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit( 'This script cannot be accessed directly.' ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Default Template Switch. |
| 20 |
*/ |
| 21 |
class CreateTemplate extends Source_Base { |
| 22 |
/** |
| 23 |
* Singleton Trait. |
| 24 |
*/ |
| 25 |
use SingletonTrait; |
| 26 |
|
| 27 |
/** |
| 28 |
* Construct function |
| 29 |
*/ |
| 30 |
private function __construct() { |
| 31 |
add_action( 'wp_ajax_rtsb_builder_create_template', [ $this, 'response' ] ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public function get_id() { |
| 36 |
} |
| 37 |
|
| 38 |
public function get_title() { |
| 39 |
} |
| 40 |
|
| 41 |
public function register_data() { |
| 42 |
} |
| 43 |
|
| 44 |
public function get_items( $args = [] ) { |
| 45 |
} |
| 46 |
|
| 47 |
public function get_item( $template_id ) { |
| 48 |
} |
| 49 |
|
| 50 |
public function get_data( array $args ) { |
| 51 |
} |
| 52 |
|
| 53 |
public function delete_template( $template_id ) { |
| 54 |
} |
| 55 |
|
| 56 |
public function save_item( $template_data ) { |
| 57 |
} |
| 58 |
|
| 59 |
public function update_item( $new_data ) { |
| 60 |
} |
| 61 |
|
| 62 |
public function export_template( $template_id ) { |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Create template |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function response() { |
| 71 |
Cache::clear_transient_cache(); |
| 72 |
$page_type = isset( $_POST['page_type'] ) ? sanitize_text_field( wp_unslash( $_POST['page_type'] ) ) : null; // rtsb_tb_template_type - it represent to template type |
| 73 |
$page_id = isset( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null; |
| 74 |
$page_name = isset( $_POST['page_name'] ) ? sanitize_text_field( wp_unslash( $_POST['page_name'] ) ) : null; |
| 75 |
$hasPro = isset( $_POST['hasPro'] ) ? sanitize_text_field( wp_unslash( $_POST['hasPro'] ) ) : null; |
| 76 |
$edit_with = isset( $_POST['template_edit_with'] ) ? sanitize_text_field( wp_unslash( $_POST['template_edit_with'] ) ) : null; |
| 77 |
$default_template = isset( $_POST['default_template'] ) ? sanitize_text_field( wp_unslash( $_POST['default_template'] ) ) : null; |
| 78 |
$import_layout = isset( $_POST['import_default_layout'] ) ? sanitize_text_field( $_POST['import_default_layout'] ) : null; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 79 |
$product_id = isset( $_POST['preview_product_id'] ) ? absint( $_POST['preview_product_id'] ) : null; |
| 80 |
|
| 81 |
$url = '#'; |
| 82 |
|
| 83 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) || ! $page_type ) { |
| 84 |
$return = [ |
| 85 |
'success' => false, |
| 86 |
'post_id' => $page_id, |
| 87 |
]; |
| 88 |
wp_send_json( $return ); |
| 89 |
|
| 90 |
} |
| 91 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 92 |
$return = [ |
| 93 |
'success' => false, |
| 94 |
'post_id' => $page_id, |
| 95 |
]; |
| 96 |
wp_send_json( $return ); |
| 97 |
} |
| 98 |
|
| 99 |
add_filter( |
| 100 |
'pre_option_elementor_unfiltered_files_upload', |
| 101 |
function () { |
| 102 |
return '1'; |
| 103 |
}, |
| 104 |
99 |
| 105 |
); |
| 106 |
|
| 107 |
Plugin::$instance->files_manager->clear_cache(); |
| 108 |
add_filter( 'rtsb_import_status', '__return_true' ); |
| 109 |
|
| 110 |
$status = sanitize_text_field( wp_unslash( $_REQUEST['status'] ?? '' ) ); |
| 111 |
$post_args = [ 'timeout' => 120 ]; |
| 112 |
$post_args['body'] = [ |
| 113 |
'status' => $status, |
| 114 |
'layout_id' => $import_layout, |
| 115 |
'has_pro' => $hasPro, |
| 116 |
]; |
| 117 |
$layoutRequest = wp_remote_post( rtsb()->BASE_API, $post_args ); |
| 118 |
|
| 119 |
$layoutJson = []; |
| 120 |
if ( ! is_wp_error( $layoutRequest ) && ! empty( $layoutRequest['body'] ) ) { |
| 121 |
$layoutJson = json_decode( $layoutRequest['body'], true ); |
| 122 |
} |
| 123 |
|
| 124 |
$option_name = BuilderFns::option_name( $page_type ); |
| 125 |
|
| 126 |
$post_data = [ |
| 127 |
'ID' => $page_id, |
| 128 |
'post_title' => $page_name, |
| 129 |
'meta_input' => [ |
| 130 |
BuilderFns::template_type_meta_key() => $page_type, |
| 131 |
], |
| 132 |
]; |
| 133 |
if ( 'elementor' == $edit_with ) { |
| 134 |
$post_data['meta_input']['_elementor_edit_mode'] = 'builder'; |
| 135 |
} elseif ( 'gutenberg' == $edit_with ) { |
| 136 |
$post_data['meta_input']['_elementor_edit_mode'] = ''; |
| 137 |
} |
| 138 |
|
| 139 |
if ( $page_id ) { |
| 140 |
$page_id = wp_update_post( $post_data ); |
| 141 |
$new_page = false; |
| 142 |
} else { |
| 143 |
unset( $post_data['ID'] ); |
| 144 |
$post_data['post_type'] = BuilderFns::$post_type_tb; |
| 145 |
$post_data['post_status'] = 'publish'; |
| 146 |
$page_id = wp_insert_post( $post_data ); |
| 147 |
$new_page = true; |
| 148 |
if ( 'elementor' == $edit_with ) { |
| 149 |
update_post_meta( $page_id, '_wp_page_template', 'elementor_header_footer' ); |
| 150 |
update_post_meta( $page_id, '_elementor_version', ELEMENTOR_VERSION ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$edit_by = ''; |
| 155 |
if ( $page_id ) { |
| 156 |
$edit_by = Fns::page_edit_with( $page_id ); |
| 157 |
$template_for = sanitize_text_field( wp_unslash( $_POST['product_page_for'] ?? '' ) ); |
| 158 |
$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 |
| 159 |
if ( 'product' == $page_type ) { |
| 160 |
update_post_meta( $page_id, BuilderFns::$product_template_meta, $product_id ); |
| 161 |
update_post_meta( $page_id, '_is_product_page_template_for', $template_for ); |
| 162 |
} |
| 163 |
|
| 164 |
$has_default = TemplateSettings::instance()->get_option( $option_name ); |
| 165 |
if ( ( 'product' === $page_type && 'all_products' !== $template_for ) || ( 'archive' === $page_type && count( $selected_category ) ) ) { |
| 166 |
if ( ! $has_default ) { |
| 167 |
TemplateSettings::instance()->set_option( $option_name, '' ); |
| 168 |
} |
| 169 |
} else { |
| 170 |
if ( 'default_template' === $default_template ) { |
| 171 |
TemplateSettings::instance()->set_option( $option_name, $page_id ); |
| 172 |
} else { |
| 173 |
if ( ! $has_default ) { |
| 174 |
TemplateSettings::instance()->set_option( $option_name, '' ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
$action = 'edit'; |
| 180 |
if ( 'elementor' === $edit_by ) { |
| 181 |
$action = 'elementor'; |
| 182 |
} |
| 183 |
$url = add_query_arg( |
| 184 |
[ |
| 185 |
'post' => $page_id, |
| 186 |
'action' => $action, |
| 187 |
], |
| 188 |
admin_url( 'post.php' ) |
| 189 |
); |
| 190 |
|
| 191 |
if ( ! empty( $layoutJson['data'] ) ) { |
| 192 |
$data = json_decode( $layoutJson['data'], true ); |
| 193 |
$content = $this->process_export_import_content( $data, 'on_import' ); |
| 194 |
$content = wp_json_encode( $content ); |
| 195 |
update_post_meta( $page_id, '_elementor_data', $content ); |
| 196 |
update_post_meta( $page_id, '_rtsb_import_id', $import_layout ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
do_action( 'rtsb/after/create/and/edit/builder/template', $_POST, $page_id ); |
| 201 |
|
| 202 |
$return = [ |
| 203 |
'success' => true, |
| 204 |
'post_id' => $page_id, |
| 205 |
'post_edit_url' => $url, |
| 206 |
'new_page' => $new_page, |
| 207 |
|
| 208 |
]; |
| 209 |
if ( $edit_by ) { |
| 210 |
$return['edit_btn_text'] = sprintf( |
| 211 |
/* translators: %s: Edit */ |
| 212 |
esc_html__( 'Edit with %s', 'shopbuilder' ), |
| 213 |
esc_html( $edit_by ) |
| 214 |
); |
| 215 |
} |
| 216 |
wp_send_json( $return ); |
| 217 |
wp_die(); |
| 218 |
} |
| 219 |
} |
| 220 |
|