PluginProbe
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder / 2.0.4
Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder v2.0.4
3.1.4 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.0.7 3.0.5 3.0.4 3.0.3 3.0.2 trunk 1.5.0 1.5.1 1.5.2 1.6.1 1.6.2 1.6.3 1.6.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 All 93 releases
ultimate-store-kit / includes / builder / builder-cpt.php

builder-cpt.php in Ultimate Store Kit – Store Builder Addons for Elementor, WooCommerce Store Builder, EDD Store Builder 2.0.4, at includes/builder/builder-cpt.php

412 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimateStoreKit\Includes\Builder;
4
5 use UltimateStoreKit\Base\Singleton;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 class Builder_Cpt {
12
13 use Singleton;
14
15 public function init_hooks() {
16 $builderCpt = Meta::POST_TYPE;
17
18 add_action( 'init', [ $this, 'registered_post_type' ] );
19 add_action( 'admin_footer', [ $this, 'add_modal_html' ], 1 );
20 add_action( 'delete_post', [ $this, 'trashed_or_delete_post' ], 10, 2 );
21 add_action( 'trashed_post', [ $this, 'trashed_or_delete_post' ], 10, 1 );
22
23 add_action( 'wp_ajax_ultimate_store_kit_builder_create_template', [ $this, 'create_builder_template' ] );
24 add_action( 'wp_ajax_ultimate_store_kit_builder_get_edit_template', [ $this, 'get_builder_template_action' ] );
25 add_filter( "manage_{$builderCpt}_posts_columns", [ $this, 'set_post_columns' ] );
26 add_action( "manage_{$builderCpt}_posts_custom_column", [ $this, 'set_custom_column_value' ], 10, 2 );
27 add_filter( 'post_row_actions', [ $this, 'post_row_actions_filter' ], 20, 2 );
28
29 if ( is_admin() ) {
30 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 1 );
31 add_action( 'admin_menu', [ $this, 'add_admin_menu' ], 202 );
32 add_action( 'restrict_manage_posts', [ $this, 'add_filter' ] );
33 add_filter( 'parse_query', [ $this, 'parse_query_filter' ] );
34 }
35
36 // $this->resetTemplateCache();
37 }
38
39 public function resetTemplateCache() {
40 global $wpdb;
41
42 $postType = Meta::POST_TYPE;
43
44 $query = $wpdb->get_results( "SELECT {$wpdb->posts}.ID,{$wpdb->posts}.post_type, {$wpdb->posts}.post_status, {$wpdb->postmeta}.meta_value as template_type
45 FROM $wpdb->posts
46 LEFT JOIN $wpdb->postmeta
47 ON {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
48 WHERE 1=1
49 AND {$wpdb->posts}.post_type ='{$postType}'
50 AND {$wpdb->postmeta}.meta_key ='_ultimate_store_kit_template_type'
51 ORDER BY {$wpdb->posts}.post_date DESC" );
52
53 foreach ( $query as $q ) {
54 if ( ! $q->template_type ) {
55 continue;
56 }
57
58 $optionKey = Meta::TEMPLATE_ID . $q->template_type;
59
60 if ( $q->post_status == 'publish' ) {
61 update_option( $optionKey, $q->ID );
62 } else {
63 delete_option( $optionKey, $q->ID );
64 }
65 }
66 }
67
68 public function trashed_or_delete_post( $postId ) {
69 if ( get_post_type( $postId ) != Meta::POST_TYPE ) {
70 return;
71 }
72
73 if ( $template = get_post_meta( $postId, Meta::TEMPLATE_TYPE, true ) ) {
74 delete_option( Meta::TEMPLATE_ID . $template );
75 }
76 }
77
78 public function post_row_actions_filter( $actions, $post ) {
79
80 global $typenow;
81
82 if ( $typenow !== Meta::POST_TYPE ) {
83 return $actions;
84 }
85
86 if ( isset( $actions['edit_with_elementor'] ) ) {
87 unset( $actions['edit_with_elementor'] );
88 }
89
90 if ( get_post_meta( $post->ID, Meta::EDIT_WITH, true ) == 'gutenberg' ) {
91 $actions['usk_edit_with_gutenberg'] = sprintf(
92 '<a href="%1$s">%2$s</a>',
93 add_query_arg( [ 'post' => $post->ID, 'action' => 'edit' ], admin_url( 'post.php' ) ),
94 esc_html__( 'Edit with Gutenberg', 'ultimate-store-kit' )
95 );
96
97 }
98
99 if ( get_post_meta( $post->ID, Meta::EDIT_WITH, true ) == 'elementor' ) {
100 $actions['usk_edit_with_elementor'] = sprintf(
101 '<a href="%1$s">%2$s</a>',
102 add_query_arg( [ 'post' => $post->ID, 'action' => 'elementor', 'usk-template' => 1 ], admin_url( 'post.php' ) ),
103 esc_html__( 'Edit with Elementor', 'ultimate-store-kit' )
104 );
105
106 }
107
108 $editActionLink = sprintf(
109 '<a href="%1$s" data-id="%2$s" >%3$s</a>',
110 'javascript:void(0)',
111 $post->ID,
112 esc_html__( 'Edit', 'ultimate-store-kit' )
113 );
114
115 if ( isset( $actions['edit'] ) && apply_filters( 'ultimate_store_kit_remove_action_edit_link', __return_true() ) ) {
116 unset( $actions['edit'] );
117 }
118
119 return array_slice( $actions, 0, 1, true ) + [ 'usk-edit-action' => $editActionLink ] + array_slice( $actions, 1, null, true );
120 }
121
122 public function set_post_columns( $columns ) {
123 return array_slice( $columns, 0, 2, true ) + [ 'template_type' => 'Type', 'is_enabled' => 'Status' ] + array_slice( $columns, 2, null, true );
124 }
125
126 public function set_custom_column_value( $column, $post_id ) {
127 $templateType = get_post_meta(
128 $post_id,
129 Meta::TEMPLATE_TYPE,
130 true
131 );
132
133 switch ( $column ) {
134 case 'template_type':
135 $postType = Builder_Template_Helper::getTemplatePostTypeByIndex( $templateType );
136 $postTypeLabel = isset( $postType->name ) ? ' <strong>-- ' . ucwords( $postType->name ) . '</strong>' : '';
137 echo Builder_Template_Helper::getTemplateByIndex( $templateType ) . $postTypeLabel;
138 break;
139 case 'is_enabled':
140 echo ( Builder_Template_Helper::getTemplateId( $templateType ) == $post_id ? 'Active' : 'Inactive' );
141 break;
142 }
143 }
144
145 public function add_filter() {
146 global $typenow;
147
148 if ( $typenow !== Meta::POST_TYPE ) {
149 return;
150 }
151
152 $selected = isset( $_GET['type'] ) ? sanitize_key( $_GET['type'] ) : '';
153 ?>
154 <select name="type" id="type">
155 <option value="all" <?php
156 selected( 'all', $selected ); ?>><?php
157 esc_html_e(
158 'Template Type ',
159 'ultimate-store-kit'
160 ); ?></option>
161 <?php
162 $templates = Builder_Template_Helper::templateForSelectDropdown();
163 // It is single
164 if ( count( $templates ) == 1 ) {
165 $templateKey = array_key_last( $templates );
166 $template = $templates[ $templateKey ];
167 foreach ( $template as $key => $item ) :
168 $selectValue = "{$templateKey}_$key";
169 ?>
170 <option value="<?php
171 echo esc_attr( $selectValue ) ?>"><?php
172 echo wp_kses_post( $item ) ?></option>
173 <?php
174 endforeach;
175 }
176
177 if ( count( $templates ) > 1 ) {
178 foreach ( $templates as $keys => $items ) :
179 $label = ucwords( str_replace(
180 [ '-', '_' ],
181 [ ' ' ],
182 $keys
183 ) );
184 if ( is_array( $items ) ) {
185 ?>
186 <optgroup label="<?php
187 echo esc_attr( $label ) ?>"><?php
188 foreach ( $items as $key => $item ) :
189 $itemValue = "{$keys}_$key"
190 ?>
191 <option value="<?php
192 echo esc_attr( $itemValue ) ?>" <?php
193 selected( $key, $selected ); ?>><?php
194 echo wp_kses_post( $item ) ?></option>
195 <?php
196 endforeach;
197 ?>
198 </optgroup>
199 <?php
200 }
201 endforeach;
202 }
203 ?>
204 </select>
205 <?php
206 }
207
208 public function parse_query_filter( $query ) {
209 global $pagenow, $typenow;
210
211 if ( $typenow !== Meta::POST_TYPE ) {
212 return;
213 }
214
215 if (
216 'edit.php' == $pagenow
217 && isset( $_GET['type'] )
218 && $_GET['type'] != ''
219 && $_GET['type'] != 'all'
220 ) {
221 $query->query_vars['meta_key'] = Meta::TEMPLATE_TYPE;
222 $query->query_vars['meta_value'] = sanitize_key( $_GET['type'] );
223 $query->query_vars['meta_compare'] = '=';
224 }
225 }
226
227
228 public function create_builder_template() {
229 parse_str( $_POST['data'], $data );
230
231 $templateId = isset( $data['template_id'] ) ? trim( $data['template_id'] ) : '';
232 $name = isset( $data['template_name'] ) ? trim( $data['template_name'] ) : '';
233 $type = isset( $data['template_type'] ) ? trim( $data['template_type'] ) : '';
234 $editWith = isset( $data['edit_with'] ) ? trim( $data['edit_with'] ) : 'elementor'; //gutenberg
235 $isEnabled = ( isset( $data['template_status'] ) && $data['template_status'] ) == 1 ? 1 : 0;
236
237 $errors = [];
238
239 if ( empty( $name ) ) {
240 $errors['template_name'] = 'Field is required';
241 }
242
243 if ( empty( $type ) ) {
244 $errors['template_type'] = 'Field is required';
245 } else {
246 if ( ! Builder_Template_Helper::getTemplateByIndex( $type ) ) {
247 $errors['template_type'] = 'Invalid section';
248 }
249 }
250
251
252 if ( count( $errors ) > 0 ) {
253 wp_send_json_error( [ 'success' => false, 'errors_arr' => $errors ], 422 );
254 }
255
256 $page_data = [
257 'post_status' => 'publish',
258 'post_type' => Meta::POST_TYPE,
259 'post_author' => get_current_user_id(),
260 'post_title' => $name,
261 'comment_status' => 'closed',
262 'meta_input' => [
263 Meta::EDIT_WITH => $editWith,
264 Meta::TEMPLATE_TYPE => $type,
265 ],
266 ];
267
268 global $wp_rewrite;
269 $wp_rewrite->flush_rules();
270 $wp_rewrite->init();
271
272 if ( $templateId ) {
273 $page_data['ID'] = $templateId;
274 }
275
276 $post_id = wp_insert_post( $page_data );
277
278 $enabledTemplate = strtolower( Meta::TEMPLATE_ID . $type );
279 if ( $isEnabled == 1 ) {
280 update_option( $enabledTemplate, $post_id );
281 } else {
282 if ( get_option( $enabledTemplate ) == $post_id ) {
283 delete_option( $enabledTemplate );
284 }
285 }
286
287
288 if ( $editWith == 'elementor' ) {
289 if ( ! get_post_meta( $post_id, '_elementor_data' ) ) {
290 update_post_meta( $post_id, '_elementor_data', [] );
291 }
292
293 if ( ! $templateId ) {
294 update_post_meta( $post_id, '_elementor_data', [] );
295 }
296 update_post_meta( $post_id, '_wp_page_template', 'elementor_header_footer' );
297 update_post_meta( $post_id, '_elementor_edit_mode', 'builder' );
298 update_post_meta( $post_id, '_elementor_version', '3.7.1' );
299 }
300
301 if ( $templateId ) {
302 $url = add_query_arg(
303 [ 'post_type' => Meta::POST_TYPE ],
304 admin_url( 'edit.php' )
305 );
306 } else {
307 $url = add_query_arg( [
308 'post' => $post_id,
309 'action' => $editWith,
310 ], admin_url( 'post.php' ) );
311 }
312
313 wp_send_json_success( [ 'success' => true, 'redirect' => $url ] );
314 }
315
316 public function get_builder_template_action() {
317 if ( isset( $_REQUEST['template_id'] ) && ! empty( $_REQUEST['template_id'] ) ) {
318 $templateId = $_REQUEST['template_id'];
319 $templateData = get_post( $templateId );
320
321 if ( $templateData ) {
322 $meta = get_post_meta( $templateData->ID );
323
324
325 $templateType = isset( $meta[ Meta::TEMPLATE_TYPE ][0] ) ? $meta[ Meta::TEMPLATE_TYPE ][0] : '';
326 $enabledTemplate = strtolower( Meta::TEMPLATE_ID . $templateType );
327 $enabledTemplate = get_option( $enabledTemplate );
328
329 wp_send_json_success( [
330 'id' => $templateData->ID,
331 'name' => $templateData->post_title,
332 'type' => $templateType,
333 'status' => is_numeric( $enabledTemplate ) ? 1 : 0,
334 ] );
335 }
336 }
337 }
338
339 public function add_modal_html( $hook_suffix ) {
340 require_once 'modal/modal.php';
341 }
342
343 public function enqueue_scripts( $hook_suffix ) {
344 if ( in_array( $hook_suffix, [ 'edit.php', 'post-new.php' ] ) ) {
345 $screen = get_current_screen();
346
347 if ( is_object( $screen ) && Meta::POST_TYPE == $screen->post_type ) {
348 wp_enqueue_style( 'ultimate-store-kit-builder', BDTUSK_ADM_ASSETS_URL . 'css/usk-ultimate-builder.css', [], BDTUSK_VER );
349 wp_enqueue_script( 'ultimate-store-kit-builder', BDTUSK_ADM_ASSETS_URL . 'js/ultimate-builder.min.js', [ 'jquery' ], BDTUSK_VER );
350 }
351 }
352 }
353
354
355 public function add_admin_menu() {
356 add_submenu_page(
357 'ultimate_store_kit_options',
358 esc_html__( 'Template Builder', 'ultimate-store-kit' ),
359 esc_html__( 'Template Builder', 'ultimate-store-kit' ),
360 'edit_pages',
361 'edit.php?post_type=' . Meta::POST_TYPE
362 );
363 }
364
365 public function registered_post_type() {
366 $labels = [
367 'name' => _x( 'Template Items', 'post type general name', 'ultimate-store-kit' ),
368 'singular_name' => _x( 'Template Item', 'post type singular name', 'ultimate-store-kit' ),
369 'menu_name' => _x( 'Template Manager', 'admin menu', 'ultimate-store-kit' ),
370 'name_admin_bar' => _x( 'Template Manager', 'add new on admin bar', 'ultimate-store-kit' ),
371 'add_new' => _x( 'Add New', 'template_manager', 'ultimate-store-kit' ),
372 'add_new_item' => __( 'Add New Template', 'ultimate-store-kit' ),
373 'new_item' => __( 'New Template', 'ultimate-store-kit' ),
374 'edit_item' => __( 'Edit Template', 'ultimate-store-kit' ),
375 'view_item' => __( 'View Template', 'ultimate-store-kit' ),
376 'all_items' => __( 'All Templates', 'ultimate-store-kit' ),
377 'search_items' => __( 'Search Templates', 'ultimate-store-kit' ),
378 'parent_item_colon' => __( 'Parent Template:', 'ultimate-store-kit' ),
379 'not_found' => __( 'No Template found.', 'ultimate-store-kit' ),
380 'not_found_in_trash' => __( 'No Template found in Trash.', 'ultimate-store-kit' ),
381 ];
382
383 $args = [
384 'labels' => $labels,
385 'description' => __( 'Description.', 'ultimate-store-kit' ),
386 'taxonomies' => [],
387 'hierarchical' => false,
388 'public' => true,
389 'show_in_menu' => false,
390 'show_ui' => true,
391 'show_in_admin_bar' => true,
392 'menu_position' => null,
393 'menu_icon' => null,
394 'publicly_queryable' => true,
395 'supports' => [ 'title', 'editor', 'elementor' ],
396 'exclude_from_search' => true,
397 'has_archive' => false,
398 'query_var' => true,
399 'can_export' => true,
400 'rewrite' => false,
401 'show_in_nav_menus' => false,
402 'capability_type' => 'post',
403 // 'rest_base' => $this->getPostType(),
404 // 'register_meta_box_cb' => [ $this, 'register_meta_box_cb' ],
405 ];
406
407 register_post_type( Meta::POST_TYPE, $args );
408 }
409 }
410
411 Builder_Cpt::instance()->init_hooks();
412