PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 1.0.1
ShopBuilder – WooCommerce Builder For Elementor v1.0.1
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 / Builder / BuilderCpt.php

BuilderCpt.php in ShopBuilder – WooCommerce Builder For Elementor 1.0.1, at app/Controllers/Builder/BuilderCpt.php

374 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Builder Custom post type
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Controllers\Builder;
9
10 defined( 'ABSPATH' ) || exit();
11
12 use RadiusTheme\SB\Traits\SingletonTrait;
13 use RadiusTheme\SB\Helpers\Fns;
14 use RadiusTheme\SB\Helpers\BuilderFns;
15
16 /**
17 * Builder Custom post type
18 */
19 class BuilderCpt {
20 /**
21 * Singleton
22 */
23 use SingletonTrait;
24
25 /**
26 * Final constructor.
27 */
28 private function __construct() {
29 add_action( 'init', [ $this, 'template_builder_post_type' ] );
30 add_filter( 'views_edit-' . BuilderFns::$post_type_tb, [ $this, 'template_builder_tabs' ] );
31 add_filter( 'post_row_actions', [ $this, 'filter_post_row_actions' ], 11, 1 );
32 // Admin column.
33 add_filter( 'manage_edit-' . BuilderFns::$post_type_tb . '_columns', [ $this, 'add_new_columns' ] );
34 add_action(
35 'manage_' . BuilderFns::$post_type_tb . '_posts_custom_column',
36 [
37 $this,
38 'custom_columns',
39 ],
40 10,
41 2
42 );
43 add_filter(
44 'manage_edit-' . BuilderFns::$post_type_tb . '_sortable_columns',
45 [
46 $this,
47 'register_sortable_columns',
48 ]
49 );
50 add_action( 'pre_get_posts', [ $this, 'sortable_columns_query' ] );
51 add_filter( 'parse_query', [ $this, 'query_filter' ] );
52 add_filter( 'template_include', [ $this, 'builder_template' ], 99 );
53
54 $this->remove_all_notices();
55 }
56
57 /**
58 * Post type function
59 *
60 * @return void
61 */
62 public function template_builder_post_type() {
63 /**
64 * Template Builder Post type
65 */
66 $tb_labels = [
67 'name' => esc_html_x( 'WooCommerce Page Builder', 'Post Type General Name', 'shopbuilder' ),
68 'singular_name' => esc_html_x( 'Templates Builder', 'Post Type Singular Name', 'shopbuilder' ),
69 'menu_name' => esc_html__( 'Templates Builder', 'shopbuilder' ),
70 'name_admin_bar' => esc_html__( 'Templates Builder', 'shopbuilder' ),
71 'archives' => esc_html__( 'WC Page Archives', 'shopbuilder' ),
72 'attributes' => esc_html__( 'Page Attributes', 'shopbuilder' ),
73 'parent_item_colon' => esc_html__( 'Parent Item:', 'shopbuilder' ),
74 'all_items' => esc_html__( 'Templates Builder', 'shopbuilder' ),
75 'add_new_item' => esc_html__( 'Add New Page', 'shopbuilder' ),
76 'add_new' => esc_html__( 'Add New', 'shopbuilder' ),
77 'new_item' => esc_html__( 'New Page', 'shopbuilder' ),
78 'edit_item' => esc_html__( 'Edit Page', 'shopbuilder' ),
79 'update_item' => esc_html__( 'Update Page', 'shopbuilder' ),
80 'view_item' => esc_html__( 'View Page', 'shopbuilder' ),
81 'view_items' => esc_html__( 'View Pages', 'shopbuilder' ),
82 'search_items' => esc_html__( 'Search Pages', 'shopbuilder' ),
83 'not_found' => esc_html__( 'Not found', 'shopbuilder' ),
84 'not_found_in_trash' => esc_html__( 'Not found in Trash', 'shopbuilder' ),
85 'featured_image' => esc_html__( 'Featured Image', 'shopbuilder' ),
86 'set_featured_image' => esc_html__( 'Set featured image', 'shopbuilder' ),
87 'remove_featured_image' => esc_html__( 'Remove featured image', 'shopbuilder' ),
88 'use_featured_image' => esc_html__( 'Use as featured image', 'shopbuilder' ),
89 'insert_into_item' => esc_html__( 'Insert into page', 'shopbuilder' ),
90 'uploaded_to_this_item' => esc_html__( 'Uploaded to this page', 'shopbuilder' ),
91 'items_list' => esc_html__( 'Pages list', 'shopbuilder' ),
92 'items_list_navigation' => esc_html__( 'Pages list navigation', 'shopbuilder' ),
93 'filter_items_list' => esc_html__( 'Filter from list', 'shopbuilder' ),
94 ];
95
96 $tb_args = [
97 'label' => esc_html__( 'Templates Builder', 'shopbuilder' ),
98 'description' => esc_html__( 'Shopbuilder Template', 'shopbuilder' ),
99 'labels' => $tb_labels,
100 'supports' => [ 'title', 'editor', 'elementor', 'author', 'permalink', 'comments' ],
101 'hierarchical' => false,
102 'public' => true,
103 'show_ui' => true,
104 'show_in_menu' => 'rtsb',
105 'show_in_admin_bar' => false,
106 'show_in_nav_menus' => true,
107 'can_export' => true,
108 'has_archive' => false,
109 'rewrite' => [
110 'slug' => 'rtsb-template',
111 'pages' => false,
112 'with_front' => true,
113 'feeds' => false,
114 ],
115 'query_var' => true,
116 'publicly_queryable' => true,
117 'capability_type' => 'product',
118 'show_in_rest' => true,
119 'rest_base' => BuilderFns::$post_type_tb,
120 ];
121
122 $tb_args = apply_filters( 'rtsb_register_template_builder_args', $tb_args );
123
124 register_post_type( BuilderFns::$post_type_tb, $tb_args );
125
126 // Flash rewrite rules.
127 $this->flush_rewrite_rules();
128
129 }
130
131 /**
132 * Flush rewrite
133 *
134 * @return void
135 */
136 public function flush_rewrite_rules() {
137 if ( get_option( 'shopbuilder_permalinks_flushed', 'no' ) !== 'yes' ) {
138 flush_rewrite_rules();
139 update_option( 'shopbuilder_permalinks_flushed', 'yes' );
140 }
141 }
142
143 /**
144 * Print the tab for Template builder.
145 *
146 * @param object $views tabs.
147 *
148 * @return string
149 */
150 public function template_builder_tabs( $views ) {
151 $template_type = isset( $_GET['template_type'] ) ? sanitize_key( wp_unslash( $_GET['template_type'] ) ) : '';
152 ?>
153 <div id="rtcl-template-tabs-wrapper" class="nav-tab-wrapper" style="margin: 15px 0;">
154 <a class="nav-tab <?php echo empty( $template_type ) ? 'nav-tab-active' : ''; ?>"
155 href="edit.php?post_type=<?php echo esc_attr( BuilderFns::$post_type_tb ); ?>"><?php esc_html_e( 'All', 'shopbuilder' ); ?></a>
156 <?php
157 $builder_page_types = BuilderFns::builder_page_types();
158 foreach ( $builder_page_types as $key => $value ) {
159 ?>
160 <a class="nav-tab <?php echo $key === $template_type ? 'nav-tab-active' : ''; ?>"
161 href="edit.php?post_type=<?php echo esc_attr( BuilderFns::$post_type_tb ); ?>&amp;template_type=<?php echo esc_attr( $key ); ?>"> <?php echo esc_html( ucwords( $value ) ); ?></a>
162 <?php } ?>
163 </div>
164 <?php
165 return $views;
166 }
167
168 /**
169 * Add new columns to the post table
170 *
171 * @param array $columns - Current columns on the list post.
172 */
173 public function add_new_columns( $columns ) {
174 $column_meta = [
175 'type' => esc_html__( 'Template Type', 'shopbuilder' ),
176 'edit_with' => esc_html__( 'Editor Type', 'shopbuilder' ),
177 'set_default' => esc_html__( 'Set as Active', 'shopbuilder' ),
178 ];
179
180 unset( $columns['comments'] );
181
182 $columns = array_merge(
183 array_slice( $columns, 0, 2 ),
184 $column_meta,
185 array_slice( $columns, 2 )
186 );
187
188 return $columns;
189 }
190
191 /**
192 * Display data in new columns
193 *
194 * @param string $column table column.
195 * @param string $post_id Post id.
196 *
197 * @return void
198 */
199 public function custom_columns( $column, $post_id ) {
200
201 $types = BuilderFns::builder_page_types();
202 $type = BuilderFns::builder_type( $post_id ) ? BuilderFns::builder_type( $post_id ) : array_key_first( $types );
203
204 switch ( $column ) {
205 case 'type':
206 if ( ! empty( $types[ $type ] ) ) {
207 echo esc_html( ucwords( $types[ $type ] ) );
208 }
209 break;
210 case 'edit_with':
211 $edit_by = Fns::page_edit_with( $post_id );
212 echo esc_html( ucfirst( $edit_by ) );
213 break;
214 case 'set_default':
215 $is_set_default = absint( BuilderFns::builder_page_id_by_type( $type ) );
216 $is_published = 'publish' == get_post_status ( $post_id );
217
218 ?>
219 <span class="rtsb-switch-wrapper page-type-<?php echo esc_attr( $type ); ?>" <?php echo ! $is_published ? 'style="pointer-events: none;"' : null; ?> title="<?php esc_html_e( 'Only publish post can set default ', 'shopbuilder' ); ?>">
220 <label class="rtsb-switch">
221 <input type="hidden" class="rtsb_template_type" name="template_type"
222 value="<?php echo esc_attr( $type ); ?>">
223 <input value="<?php echo absint( $post_id ); ?>" class="rtsb_set_default" name="set_default"
224 type="checkbox" <?php echo esc_attr( $post_id === $is_set_default ? 'checked' : '' ); ?> >
225 <span class="rtsb-slider rtsb-round ">
226 <span class="rtsb-loader"></span>
227 </span>
228 </label>
229 </span>
230 <?php
231 break;
232 }
233 }
234
235 /**
236 * Register sortable columns
237 *
238 * @param [type] $columns column list.
239 *
240 * @return array
241 */
242 public function register_sortable_columns( $columns ) {
243 $columns['type'] = 'type';
244
245 return $columns;
246 }
247
248 /**
249 * Meta sortable function.
250 *
251 * @param object $query query object.
252 *
253 * @return void
254 */
255 public function sortable_columns_query( $query ) {
256 if ( ! is_admin() || ! $this->is_current_screen() ) {
257 return;
258 }
259 $orderby = $query->get( 'orderby' );
260 if ( 'type' === $orderby ) {
261 $query->set( 'meta_key', BuilderFns::template_type_meta_key() );
262 $query->set( 'orderby', 'meta_value' );
263 }
264 }
265
266 /**
267 * Check template screen
268 *
269 * @return boolean
270 */
271 public function is_current_screen() {
272 global $pagenow, $typenow;
273
274 return 'edit.php' === $pagenow && BuilderFns::$post_type_tb === $typenow;
275 }
276
277 /**
278 * Manage Template filter by template type
279 *
280 * @param \WP_Query $query WordPress main query.
281 *
282 * @return void
283 */
284 public function query_filter( \WP_Query $query ) {
285
286 if ( ! is_admin() || ! $this->is_current_screen() || ! empty( $query->query_vars['meta_key'] ) ) {
287 return;
288 }
289 if ( isset( $_GET['template_type'] ) && ( '' !== $_GET['template_type'] || 'all' !== $_GET['template_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
290 $type = isset( $_GET['template_type'] ) ? sanitize_key( $_GET['template_type'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
291 if ( 'defaults' === $type ) {
292 $defaults = [];
293 $builder_page_types = BuilderFns::builder_page_types();
294 foreach ( $builder_page_types as $key => $value ) {
295 $defaults[] = absint( BuilderFns::builder_page_id_by_type( $key ) );
296 }
297 if ( ! empty( $defaults ) ) {
298 $query->query_vars['post__in'] = $defaults;
299 }
300 } else {
301 $query->query_vars['meta_key'] = BuilderFns::template_type_meta_key(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
302 $query->query_vars['meta_value'] = $type;
303 $query->query_vars['meta_compare'] = '=';
304 }
305 }
306
307 }
308
309 /**
310 * Load Template.
311 *
312 * @param string $template Template File.
313 *
314 * @return string
315 */
316 public function builder_template( $template ) {
317 $builder_page_id = BuilderFns::is_builder_preview() ? get_the_ID() : BuilderFns::builder_page_id_by_page();
318 if ( $builder_page_id ) {
319 $page_template = get_post_meta( $builder_page_id, '_wp_page_template', true );
320 $new_template = RTSB_ABSPATH . 'page-templates/builder-template.php';
321 if ( 'elementor_canvas' === $page_template ) {
322 $new_template = RTSB_ABSPATH . 'page-templates/elementor-canvas.php';
323 } elseif ( 'elementor_header_footer' === $page_template ) {
324 $new_template = RTSB_ABSPATH . 'page-templates/builder-template.php';
325 }
326 if ( file_exists( $new_template ) ) {
327 $template = $new_template;
328 }
329 }
330
331 return $template;
332 }
333
334 /**
335 * Add/Remove edit link in dashboard.
336 *
337 * Add or remove an edit link to the post/page action links on the post/pages list table.
338 *
339 * Fired by `post_row_actions` and `page_row_actions` filters.
340 *
341 * @access public
342 *
343 * @param array $actions An array of row action links.
344 *
345 * @return array An updated array of row action links.
346 */
347 public function filter_post_row_actions( $actions ) {
348 if ( $this->is_current_screen() ) {
349 // unset( $actions['view'] );
350 unset( $actions['inline hide-if-no-js'] );
351 }
352
353 return $actions;
354 }
355
356 /**
357 * Remove admin notices
358 */
359 public function remove_all_notices() {
360 add_action(
361 'in_admin_header',
362 function () {
363 $screen = get_current_screen();
364
365 if ( 'edit-rtsb_builder' === $screen->id ) {
366 remove_all_actions( 'admin_notices' );
367 remove_all_actions( 'all_admin_notices' );
368 }
369 },
370 1000
371 );
372 }
373 }
374