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