PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.0.2
ShopBuilder – WooCommerce Builder For Elementor v2.0.2
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Controllers / Admin / Ajax / CreateTemplate.php

CreateTemplate.php in ShopBuilder – WooCommerce Builder For Elementor 2.0.2, at app/Controllers/Admin/Ajax/CreateTemplate.php

196 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\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 extends Source_Base {
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 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
64 /**
65 * Create template
66 *
67 * @return void
68 */
69 public function response() {
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
71 $page_id = isset( $_POST['page_id'] ) ? absint( wp_unslash( $_POST['page_id'] ) ) : null;
72 $page_name = isset( $_POST['page_name'] ) ? sanitize_text_field( wp_unslash( $_POST['page_name'] ) ) : null;
73 $hasPro = isset( $_POST['hasPro'] ) ? sanitize_text_field( wp_unslash( $_POST['hasPro'] ) ) : null;
74 $edit_with = isset( $_POST['template_edit_with'] ) ? sanitize_text_field( wp_unslash( $_POST['template_edit_with'] ) ) : null;
75 $default_template = isset( $_POST['default_template'] ) ? sanitize_text_field( wp_unslash( $_POST['default_template'] ) ) : null;
76 $import_layout = isset( $_POST['import_default_layout'] ) ? sanitize_text_field( $_POST['import_default_layout'] ) : null;
77 $product_id = isset( $_POST['preview_product_id'] ) ? absint( $_POST['preview_product_id'] ) : null;
78
79 $url = '#';
80
81 if ( ! Fns::verify_nonce() || ! $page_type ) {
82 $return = [
83 'success' => false,
84 'post_id' => $page_id,
85 ];
86 wp_send_json( $return );
87
88 }
89
90 add_filter( 'pre_option_elementor_unfiltered_files_upload', function () {
91 return '1';
92 }, 99 );
93
94 Plugin::$instance->files_manager->clear_cache();
95 add_filter( 'rtsb_import_status', '__return_true' );
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 );
110 $option_name = BuilderFns::option_name( $page_type );
111
112 $post_data = [
113 'ID' => $page_id,
114 'post_title' => $page_name,
115 'meta_input' => [
116 BuilderFns::template_type_meta_key() => $page_type,
117 ],
118 ];
119 if ( 'elementor' == $edit_with ) {
120 $post_data['meta_input']['_elementor_edit_mode'] = 'builder';
121 } elseif ( 'gutenberg' == $edit_with ) {
122 $post_data['meta_input']['_elementor_edit_mode'] = '';
123 }
124
125 if ( $page_id ) {
126 $page_id = wp_update_post( $post_data );
127 $new_page = false;
128 } else {
129 unset( $post_data['ID'] );
130 $post_data['post_type'] = BuilderFns::$post_type_tb;
131 $post_data['post_status'] = 'publish';
132 $page_id = wp_insert_post( $post_data );
133 $new_page = true;
134 if ( 'elementor' == $edit_with ) {
135 update_post_meta( $page_id, '_wp_page_template', 'elementor_header_footer' );
136 update_post_meta( $page_id, '_elementor_version', ELEMENTOR_VERSION );
137 }
138 }
139
140 $edit_by = '';
141 if ( $page_id ) {
142 if ( 'product' == $page_type ) {
143 update_post_meta( $page_id, BuilderFns::$product_template_meta, $product_id );
144 }
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 ) ) {
149 if ( 'default_template' === $default_template ) {
150 update_option( $option_name, $page_id );
151 } else {
152 $has_default = get_option( $option_name );
153 if ( ! $has_default ) {
154 update_option( $option_name, '' );
155 }
156 }
157 }
158
159 $action = 'edit';
160 if ( 'elementor' === $edit_by ) {
161 $action = 'elementor';
162 }
163 $url = add_query_arg(
164 [
165 'post' => $page_id,
166 'action' => $action,
167 ],
168 admin_url( 'post.php' )
169 );
170
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 );
175 update_post_meta( $page_id, '_elementor_data', $content );
176 update_post_meta( $page_id, '_rtsb_import_id', $import_layout );
177 }
178 }
179
180 do_action( 'rtsb/after/create/and/edit/builder/template', $_POST, $page_id );
181
182 $return = [
183 'success' => true,
184 'post_id' => $page_id,
185 'post_edit_url' => $url,
186 'new_page' => $new_page,
187
188 ];
189 if ( $edit_by ) {
190 $return['edit_btn_text'] = esc_html__( sprintf( 'Edit with %s', $edit_by ), 'shopbuilder' );
191 }
192 wp_send_json( $return );
193 wp_die();
194 }
195 }
196