| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace RadiusTheme\SB\Controllers\Admin\Ajax; |
| 4 | 4 | |
| 5 | +use Elementor\Core\Files\Uploads_Manager; | |
| 5 | 6 | use Elementor\Plugin; |
| 7 | +use Elementor\TemplateLibrary\Source_Base; | |
| 6 | 8 | use RadiusTheme\SB\Helpers\BuilderFns; |
| 7 | -use RadiusTheme\SB\Helpers\Cache; | |
| 8 | 9 | use RadiusTheme\SB\Helpers\Fns; |
| 9 | -use RadiusTheme\SB\Models\TemplateSettings; | |
| 10 | 10 | use RadiusTheme\SB\Traits\SingletonTrait; |
| 11 | 11 | |
| 12 | 12 | // Do not allow directly accessing this file. |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Default Template Switch. |
| 19 | 19 | */ |
| 20 | -class CreateTemplate { | |
| 20 | +class CreateTemplate extends Source_Base { | |
| 21 | 21 | /** |
| 22 | 22 | * Singleton Trait. |
| 23 | 23 | */ |
| 24 | 24 | use SingletonTrait; |
| @@ -29,8 +29,39 @@ | ||
| 29 | 29 | private function __construct() { |
| 30 | 30 | add_action( 'wp_ajax_rtsb_builder_create_template', [ $this, 'response' ] ); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | + | |
| 34 | + public function get_id() { | |
| 35 | + } | |
| 36 | + | |
| 37 | + public function get_title() { | |
| 38 | + } | |
| 39 | + | |
| 40 | + public function register_data() { | |
| 41 | + } | |
| 42 | + | |
| 43 | + public function get_items( $args = [] ) { | |
| 44 | + } | |
| 45 | + | |
| 46 | + public function get_item( $template_id ) { | |
| 47 | + } | |
| 48 | + | |
| 49 | + public function get_data( array $args ) { | |
| 50 | + } | |
| 51 | + | |
| 52 | + public function delete_template( $template_id ) { | |
| 53 | + } | |
| 54 | + | |
| 55 | + public function save_item( $template_data ) { | |
| 56 | + } | |
| 57 | + | |
| 58 | + public function update_item( $new_data ) { | |
| 59 | + } | |
| 60 | + | |
| 61 | + public function export_template( $template_id ) { | |
| 62 | + } | |
| 63 | + | |
| 33 | 64 | /** |
| 34 | 65 | * Create template |
| 35 | 66 | * |
| 36 | 67 | * @return void |
| @@ -35,60 +66,48 @@ | ||
| 35 | 66 | * |
| 36 | 67 | * @return void |
| 37 | 68 | */ |
| 38 | 69 | 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. | |
| 70 | + $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 | 71 | $page_id = isset( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null; |
| 42 | 72 | $page_name = isset( $_POST['page_name'] ) ? sanitize_text_field( wp_unslash( $_POST['page_name'] ) ) : null; |
| 43 | 73 | $hasPro = isset( $_POST['hasPro'] ) ? sanitize_text_field( wp_unslash( $_POST['hasPro'] ) ) : null; |
| 44 | 74 | $edit_with = isset( $_POST['template_edit_with'] ) ? sanitize_text_field( wp_unslash( $_POST['template_edit_with'] ) ) : null; |
| 45 | 75 | $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 | |
| 76 | + $import_layout = isset( $_POST['import_default_layout'] ) ? sanitize_text_field( $_POST['import_default_layout'] ) : null; | |
| 48 | 77 | $product_id = isset( $_POST['preview_product_id'] ) ? absint( $_POST['preview_product_id'] ) : null; |
| 49 | - $url = '#'; | |
| 50 | 78 | |
| 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 ); | |
| 79 | + $url = '#'; | |
| 57 | 80 | |
| 58 | - } | |
| 59 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 81 | + if ( ! Fns::verify_nonce() || ! $page_type ) { | |
| 60 | 82 | $return = [ |
| 61 | 83 | 'success' => false, |
| 62 | 84 | 'post_id' => $page_id, |
| 63 | 85 | ]; |
| 64 | 86 | wp_send_json( $return ); |
| 65 | - } | |
| 66 | 87 | |
| 67 | - // Nonce verified above. | |
| 68 | - $can_save = apply_filters( 'rtsb/builder/create_template/can_save', true, $page_type, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 69 | - | |
| 70 | - if ( true !== $can_save ) { | |
| 71 | - wp_send_json( | |
| 72 | - [ | |
| 73 | - 'success' => false, | |
| 74 | - 'post_id' => $page_id, | |
| 75 | - 'message' => is_string( $can_save ) ? $can_save : esc_html__( 'Template cannot be saved.', 'shopbuilder' ), | |
| 76 | - ] | |
| 77 | - ); | |
| 78 | 88 | } |
| 79 | 89 | |
| 80 | - add_filter( | |
| 81 | - 'pre_option_elementor_unfiltered_files_upload', | |
| 82 | - function () { | |
| 83 | - return '1'; | |
| 84 | - }, | |
| 85 | - 99 | |
| 86 | - ); | |
| 90 | + add_filter( 'pre_option_elementor_unfiltered_files_upload', function () { | |
| 91 | + return '1'; | |
| 92 | + }, 99 ); | |
| 87 | 93 | |
| 88 | 94 | Plugin::$instance->files_manager->clear_cache(); |
| 89 | 95 | add_filter( 'rtsb_import_status', '__return_true' ); |
| 90 | 96 | |
| 97 | + $status = $_REQUEST['status'] ?? ''; | |
| 98 | + $post_args = [ 'timeout' => 120 ]; | |
| 99 | + $post_args['body'] = [ | |
| 100 | + 'status' => $status, | |
| 101 | + 'layout_id' => $import_layout, | |
| 102 | + 'has_pro' => $hasPro | |
| 103 | + ]; | |
| 104 | + $layoutRequest = wp_remote_post( rtsb()->BASE_API, $post_args ); | |
| 105 | + | |
| 106 | + if ( is_wp_error( $layoutRequest ) ) { | |
| 107 | + wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] ); | |
| 108 | + } | |
| 109 | + $layoutJson = json_decode( $layoutRequest['body'], true ); | |
| 91 | 110 | $option_name = BuilderFns::option_name( $page_type ); |
| 92 | 111 | |
| 93 | 112 | $post_data = [ |
| 94 | 113 | 'ID' => $page_id, |
| @@ -119,40 +138,21 @@ | ||
| 119 | 138 | } |
| 120 | 139 | |
| 121 | 140 | $edit_by = ''; |
| 122 | 141 | if ( $page_id ) { |
| 123 | - $edit_by = Fns::page_edit_with( $page_id ); | |
| 124 | - $template_for = sanitize_text_field( wp_unslash( $_POST['product_page_for'] ?? '' ) ); | |
| 125 | - $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 | |
| 126 | 142 | if ( 'product' == $page_type ) { |
| 127 | 143 | update_post_meta( $page_id, BuilderFns::$product_template_meta, $product_id ); |
| 128 | - update_post_meta( $page_id, '_is_product_page_template_for', $template_for ); | |
| 129 | 144 | } |
| 130 | - | |
| 131 | - $has_default = TemplateSettings::instance()->get_option( $option_name ); | |
| 132 | - $singleton_types = apply_filters( | |
| 133 | - 'rtsb/builder/singleton_types', | |
| 134 | - array_keys( BuilderFns::builder_page_types() ) | |
| 135 | - ); | |
| 136 | - $is_pool_type = ! in_array( $page_type, $singleton_types, true ); | |
| 137 | - if ( ( 'product' === $page_type && 'all_products' !== $template_for ) || ( 'archive' === $page_type && count( $selected_category ) ) ) { | |
| 138 | - if ( ! $has_default ) { | |
| 139 | - TemplateSettings::instance()->set_option( $option_name, '' ); | |
| 140 | - } | |
| 141 | - } else { | |
| 145 | + $edit_by = Fns::page_edit_with( $page_id ); | |
| 146 | + $category_archive = array_unique( isset( $_POST['category_archive'] ) && is_array( $_POST['category_archive'] ) ? array_map( 'sanitize_text_field', $_POST['category_archive'] ) : [] ); | |
| 147 | + $the_products = array_unique( isset( $_POST['the_products'] ) && is_array( $_POST['the_products'] ) ? array_map( 'absint', $_POST['the_products'] ) : [] ); | |
| 148 | + if ( ! count( $the_products ) && ! count( $category_archive ) ) { | |
| 142 | 149 | if ( 'default_template' === $default_template ) { |
| 143 | - if ( $is_pool_type ) { | |
| 144 | - update_post_meta( $page_id, '_rtsb_tb_enabled_in_pool', 'on' ); | |
| 145 | - } else { | |
| 146 | - TemplateSettings::instance()->set_option( $option_name, $page_id ); | |
| 147 | - } | |
| 150 | + update_option( $option_name, $page_id ); | |
| 148 | 151 | } else { |
| 149 | - if ( $is_pool_type ) { | |
| 150 | - delete_post_meta( $page_id, '_rtsb_tb_enabled_in_pool' ); | |
| 151 | - } else { | |
| 152 | - if ( ! $has_default ) { | |
| 153 | - TemplateSettings::instance()->set_option( $option_name, '' ); | |
| 154 | - } | |
| 152 | + $has_default = get_option( $option_name ); | |
| 153 | + if ( ! $has_default ) { | |
| 154 | + update_option( $option_name, '' ); | |
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | |
| @@ -167,10 +167,12 @@ | ||
| 167 | 167 | ], |
| 168 | 168 | admin_url( 'post.php' ) |
| 169 | 169 | ); |
| 170 | 170 | |
| 171 | - if ( ! empty( $layoutJson ) && is_array( $layoutJson ) ) { | |
| 172 | - $content = wp_slash( wp_json_encode( $layoutJson ) ); | |
| 171 | + if ( ! empty( $layoutJson['data'] ) ) { | |
| 172 | + $data = json_decode( $layoutJson['data'], true ); | |
| 173 | + $content = $this->process_export_import_content( $data, 'on_import' ); | |
| 174 | + $content = json_encode( $content ); | |
| 173 | 175 | update_post_meta( $page_id, '_elementor_data', $content ); |
| 174 | 176 | update_post_meta( $page_id, '_rtsb_import_id', $import_layout ); |
| 175 | 177 | } |
| 176 | 178 | } |
| @@ -184,13 +186,9 @@ | ||
| 184 | 186 | 'new_page' => $new_page, |
| 185 | 187 | |
| 186 | 188 | ]; |
| 187 | 189 | if ( $edit_by ) { |
| 188 | - $return['edit_btn_text'] = sprintf( | |
| 189 | - /* translators: %s: Edit */ | |
| 190 | - esc_html__( 'Edit with %s', 'shopbuilder' ), | |
| 191 | - esc_html( $edit_by ) | |
| 192 | - ); | |
| 190 | + $return['edit_btn_text'] = esc_html__( sprintf( 'Edit with %s', $edit_by ), 'shopbuilder' ); | |
| 193 | 191 | } |
| 194 | 192 | wp_send_json( $return ); |
| 195 | 193 | wp_die(); |
| 196 | 194 | } |