PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 2.1.7
ShopBuilder – WooCommerce Builder For Elementor v2.1.7
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.1.7, at app/Controllers/Admin/Ajax/CreateTemplate.php

208 lines 6.2 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\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 ( ! Fns::verify_nonce() || ! $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( 'pre_option_elementor_unfiltered_files_upload', function () {
99 return '1';
100 }, 99 );
101
102 Plugin::$instance->files_manager->clear_cache();
103 add_filter( 'rtsb_import_status', '__return_true' );
104
105 $status = $_REQUEST['status'] ?? '';
106 $post_args = [ 'timeout' => 120 ];
107 $post_args['body'] = [
108 'status' => $status,
109 'layout_id' => $import_layout,
110 'has_pro' => $hasPro
111 ];
112 $layoutRequest = wp_remote_post( rtsb()->BASE_API, $post_args );
113
114 $layoutJson = [];
115 if ( ! is_wp_error( $layoutRequest ) && ! empty( $layoutRequest['body'] ) ) {
116 $layoutJson = json_decode( $layoutRequest['body'], true );
117 }
118
119 $option_name = BuilderFns::option_name( $page_type );
120
121 $post_data = [
122 'ID' => $page_id,
123 'post_title' => $page_name,
124 'meta_input' => [
125 BuilderFns::template_type_meta_key() => $page_type,
126 ],
127 ];
128 if ( 'elementor' == $edit_with ) {
129 $post_data['meta_input']['_elementor_edit_mode'] = 'builder';
130 } elseif ( 'gutenberg' == $edit_with ) {
131 $post_data['meta_input']['_elementor_edit_mode'] = '';
132 }
133
134 if ( $page_id ) {
135 $page_id = wp_update_post( $post_data );
136 $new_page = false;
137 } else {
138 unset( $post_data['ID'] );
139 $post_data['post_type'] = BuilderFns::$post_type_tb;
140 $post_data['post_status'] = 'publish';
141 $page_id = wp_insert_post( $post_data );
142 $new_page = true;
143 if ( 'elementor' == $edit_with ) {
144 update_post_meta( $page_id, '_wp_page_template', 'elementor_header_footer' );
145 update_post_meta( $page_id, '_elementor_version', ELEMENTOR_VERSION );
146 }
147 }
148
149 $edit_by = '';
150 if ( $page_id ) {
151 if ( 'product' == $page_type ) {
152 update_post_meta( $page_id, BuilderFns::$product_template_meta, $product_id );
153 }
154 $edit_by = Fns::page_edit_with( $page_id );
155 $category_archive = array_unique( isset( $_POST['category_archive'] ) && is_array( $_POST['category_archive'] ) ? array_map( 'sanitize_text_field', $_POST['category_archive'] ) : [] );
156 $the_products = array_unique( isset( $_POST['the_products'] ) && is_array( $_POST['the_products'] ) ? array_map( 'absint', $_POST['the_products'] ) : [] );
157 if ( ! count( $the_products ) && ! count( $category_archive ) ) {
158 if ( 'default_template' === $default_template ) {
159 // update_option( $option_name, $page_id );
160 TemplateSettings::instance()->set_option( $option_name, $page_id );
161 } else {
162 // $has_default = get_option( $option_name );
163 $has_default = TemplateSettings::instance()->get_option( $option_name );
164 if ( ! $has_default ) {
165 // update_option( $option_name, '' );
166 TemplateSettings::instance()->set_option( $option_name, '' );
167 }
168 }
169 }
170
171 $action = 'edit';
172 if ( 'elementor' === $edit_by ) {
173 $action = 'elementor';
174 }
175 $url = add_query_arg(
176 [
177 'post' => $page_id,
178 'action' => $action,
179 ],
180 admin_url( 'post.php' )
181 );
182
183 if ( ! empty( $layoutJson['data'] ) ) {
184 $data = json_decode( $layoutJson['data'], true );
185 $content = $this->process_export_import_content( $data, 'on_import' );
186 $content = json_encode( $content );
187 update_post_meta( $page_id, '_elementor_data', $content );
188 update_post_meta( $page_id, '_rtsb_import_id', $import_layout );
189 }
190 }
191
192 do_action( 'rtsb/after/create/and/edit/builder/template', $_POST, $page_id );
193
194 $return = [
195 'success' => true,
196 'post_id' => $page_id,
197 'post_edit_url' => $url,
198 'new_page' => $new_page,
199
200 ];
201 if ( $edit_by ) {
202 $return['edit_btn_text'] = esc_html__( sprintf( 'Edit with %s', $edit_by ), 'shopbuilder' );
203 }
204 wp_send_json( $return );
205 wp_die();
206 }
207 }
208