PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.0
Post Grid Gutenberg Blocks – PostX v5.1.0
5.1.1 5.1.0 5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 All 240 releases
ultimate-post / addons / builder / Builder.php

Builder.php in Post Grid Gutenberg Blocks – PostX 5.1.0, at addons/builder/Builder.php

432 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ULTP;
4
5
6 defined( 'ABSPATH' ) || exit;
7
8 class Builder {
9 private $header_id = '';
10 private $footer_id = '';
11 private $theme_name = '';
12 private $is_block_theme = '';
13
14 public function __construct() {
15 $this->builder_post_type_callback(); // Post Type Register
16 add_action( 'wp', array( $this, 'checkfor_header_footer' ), 999 );
17 add_filter( 'template_include', array( $this, 'include_builder_files' ), 100 );
18 add_action( 'add_meta_boxes', array( $this, 'init_metabox_callback' ) );
19 add_action( 'save_post', array( $this, 'metabox_save_data' ) );
20 add_action( 'save_post', array( $this, 'metabox_save_video_data' ) );
21 add_action( 'delete_post', array( $this, 'delete_option_meta_action' ) );
22 }
23
24 public function checkfor_header_footer() {
25 $this->theme_name = get_template();
26 $this->is_block_theme = wp_is_block_theme();
27 $header_id = ultimate_post()->builder_check_conditions( 'header' );
28 $footer_id = ultimate_post()->builder_check_conditions( 'footer' );
29 global $ULTP_HEADER_ID;
30 global $ULTP_FOOTER_ID;
31
32 if ( $header_id ) {
33 $ULTP_HEADER_ID = $header_id;
34 do_action(
35 'ultp_enqueue_postx_block_css',
36 array(
37 'post_id' => $header_id,
38 'css' => '',
39 )
40 );
41 $this->header_id = $header_id;
42 if ( $this->is_block_theme ) {
43 add_action( 'wp_head', array( $this, 'ultp_header_builder_template' ) );
44 } else {
45 switch ( $this->theme_name ) {
46 case 'astra':
47 remove_all_actions( 'astra_header' );
48 add_action( 'astra_header', array( $this, 'ultp_header_builder_template' ) );
49 break;
50 default:
51 add_action( 'get_header', array( $this, 'ultp_header_builder_template' ) );
52 }
53 }
54 }
55 if ( $footer_id ) {
56 $ULTP_FOOTER_ID = $footer_id;
57 $this->footer_id = $footer_id;
58 do_action(
59 'ultp_enqueue_postx_block_css',
60 array(
61 'post_id' => $footer_id,
62 'css' => '',
63 )
64 );
65 if ( $this->is_block_theme ) {
66 add_action( 'wp_footer', array( $this, 'ultp_footer_builder_template' ) );
67 } else {
68 switch ( $this->theme_name ) {
69 case 'astra':
70 remove_all_actions( 'astra_footer' );
71 add_action( 'astra_footer', array( $this, 'ultp_footer_builder_template' ) );
72 break;
73 case 'generatepress':
74 remove_action( 'generate_footer', 'generate_construct_footer_widgets' );
75 remove_action( 'generate_footer', 'generate_construct_footer' );
76 add_action( 'generate_footer', array( $this, 'ultp_footer_builder_template' ) );
77 break;
78 default:
79 add_action( 'get_footer', array( $this, 'ultp_footer_builder_template' ) );
80 }
81 }
82 }
83 }
84
85 public function ultp_header_builder_template() {
86 if ( $this->header_id ) {
87 if ( $this->is_block_theme ) {
88 remove_action( 'wp_head', array( $this, 'ultp_header_builder_template' ) );
89 } elseif ( $this->theme_name == 'generatepress' ) {
90 require_once ULTP_PATH . 'addons/builder/templates/header.php';
91 } else {
92 if ( $this->theme_name != 'astra' ) { // Astra theme issue
93 require_once ULTP_PATH . 'addons/builder/templates/header.php';
94 }
95 $templates = array();
96 $templates[] = 'header.php';
97 remove_all_actions( 'wp_head' );
98 if ( $this->theme_name != 'bricks' ) { // Conflict with Bricks Builder Backend
99 ob_start();
100 }
101 locate_template( $templates, true );
102 if ( $this->theme_name != 'bricks' ) { // Conflict with Bricks Builder Backend
103 ob_get_clean();
104 } else {
105 wp_enqueue_style( 'wp-block-library' ); // Gutenberg CSS issue Bricks Builder frontend
106 }
107 }
108 ?>
109 <header id="ultp-header-template" class="<?php echo esc_attr( 'ultp-builderid-' . $this->header_id ); ?>">
110 <?php ultimate_post()->get_post_content( $this->header_id ); ?>
111 </header>
112 <?php
113 }
114 }
115 public function ultp_footer_builder_template() {
116 if ( $this->footer_id ) {
117
118 ?>
119 <footer id="ultp-footer-template" class="<?php echo esc_attr( 'ultp-builderid-' . $this->footer_id ); ?>" role="contentinfo">
120 <?php ultimate_post()->get_post_content( $this->footer_id ); ?>
121 </footer>
122 <?php
123 if ( ! $this->is_block_theme ) {
124 if ( $this->theme_name == 'astra' || $this->theme_name == 'generatepress' ) { // Astra theme issue
125 wp_footer();
126 } else {
127 require_once ULTP_PATH . 'addons/builder/templates/footer.php';
128 }
129 $templates = array();
130 $templates[] = 'footer.php';
131 remove_all_actions( 'wp_footer' );
132 if ( $this->theme_name != 'bricks' ) { // Conflict with Bricks Builder Backend
133 ob_start();
134 }
135 locate_template( $templates, true );
136 if ( $this->theme_name != 'bricks' ) { // Conflict with Bricks Builder Backend
137 ob_get_clean();
138 }
139 }
140 }
141 }
142
143 public function delete_option_meta_action( $post_id ) {
144 if ( get_post_type( $post_id ) != 'ultp_builder' ) {
145 return;
146 }
147
148 $conditions = get_option( 'ultp_builder_conditions', array() );
149 if ( $conditions ) {
150 $has_change = false;
151 if ( isset( $conditions['archive'][ $post_id ] ) ) {
152 $has_change = true;
153 unset( $conditions['archive'][ $post_id ] );
154 }
155 if ( isset( $conditions['singular'][ $post_id ] ) ) {
156 $has_change = true;
157 unset( $conditions['singular'][ $post_id ] );
158 }
159 if ( $has_change ) {
160 update_option( 'ultp_builder_conditions', $conditions );
161 }
162 }
163 delete_post_meta( $post_id, '_ultp_active' );
164 }
165
166
167 public function include_builder_files( $template ) {
168 $includes = ultimate_post()->builder_check_conditions( 'return' );
169
170 global $ultp_page_builder_id;
171 if ( $includes ) {
172 $ultp_page_builder_id = $includes;
173 return ULTP_PATH . 'addons/builder/templates/page.php';
174 }
175 return $template;
176 }
177
178 function init_metabox_callback() {
179 $title = '<div class="ultp-add-media-image-head"><img src="' . ULTP_URL . 'assets/img/logo-sm.svg" /><span>PostX Settings</span></div>';
180
181 add_meta_box(
182 'postx-builder-id',
183 __( 'PostX Builder Settings', 'ultimate-post' ),
184 array( $this, 'container_width_callback' ),
185 'ultp_builder',
186 'side'
187 );
188 add_meta_box(
189 'ultp-feature-video',
190 $title,
191 array( $this, 'video_source_callback' ),
192 'post',
193 'side',
194 'high'
195 );
196 }
197
198 function video_source_callback( $post ) {
199 wp_nonce_field( 'video_meta_box', 'video_meta_box_nonce' );
200 $video = get_post_meta( $post->ID, '__builder_feature_video', true );
201 $caption = get_post_meta( $post->ID, '__builder_feature_caption', true );
202 ?>
203 <div class="ultp-meta-video">
204 <?php if ( current_user_can( 'administrator' ) ) { ?>
205 <a class="ultp-dash-builder-btn" target="_blank" href="<?php echo esc_url( admin_url( 'admin.php?page=ultp-settings#builder' ) ); ?>"><?php echo esc_html__( 'Enable PostX Single Builder', 'ultimate-post' ); ?> </a>
206 <?php } ?>
207 <label><?php echo esc_html__( 'Featured Video', 'ultimate-post' ); ?></label>
208 <div class="ultp-video-input">
209 <input id="ultp-add-input" type="text" name="feature-video" value="<?php echo esc_attr( $video ); ?>" autocomplete="off" />
210 <button class="ultp-add-media"><span class="dashicons dashicons-cloud-upload"></span></button>
211 </div>
212 <span><strong><?php echo esc_html__( 'Note: ', 'ultimate-post' ); ?></strong><?php echo esc_html__( 'Enter Youtube/ Vimeo/ Self Hosted URL', 'ultimate-post' ); ?></span>
213 <label><?php echo esc_html__( 'Featured Video Caption', 'ultimate-post' ); ?></label>
214 <input id="ultp-add-caption" type="text" name="video-caption" value="<?php echo esc_attr( $caption ); ?>" autocomplete="off" />
215 </div>
216 <?php
217 }
218
219
220 function metabox_save_video_data( $post_id ) {
221 if ( ! isset( $_POST['video_meta_box_nonce'] ) ) {
222 return;
223 }
224 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['video_meta_box_nonce'] ) ), 'video_meta_box' ) ) {
225 return;
226 }
227 if ( ! isset( $_POST['feature-video'] ) ) {
228 return;
229 }
230 update_post_meta( $post_id, '__builder_feature_video', sanitize_text_field( $_POST['feature-video'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
231 if ( ! isset( $_POST['video-caption'] ) ) {
232 return;
233 }
234 update_post_meta( $post_id, '__builder_feature_caption', sanitize_text_field( $_POST['video-caption'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
235 }
236
237
238 function container_width_callback( $post ) {
239 wp_nonce_field( 'container_meta_box', 'container_meta_box_nonce' );
240 $width = get_post_meta( $post->ID, '__container_width', true );
241 $sidebar = get_post_meta( $post->ID, '__builder_sidebar', true );
242 $widget = get_post_meta( $post->ID, '__builder_widget_area', true );
243 $p_type = get_post_meta( $post->ID, '__ultp_builder_type', true );
244 $p_type = $p_type ? $p_type : 'archive';
245
246 $widget_area = wp_get_sidebars_widgets(); // phpcs:ignore Generic.PHP.ForbiddenFunctions.Found -- Needs every sidebar ID incl. the sidebars_widgets filter; no public equivalent.
247 if ( isset( $widget_area['wp_inactive_widgets'] ) ) {
248 unset( $widget_area['wp_inactive_widgets'] );
249 }
250 if ( isset( $widget_area['array_version'] ) ) {
251 unset( $widget_area['array_version'] );
252 }
253 ?>
254
255 <input type="hidden" name="postx-type" value="<?php echo esc_attr( isset( $_GET['postx_type'] ) ? sanitize_text_field( wp_unslash( $_GET['postx_type'] ) ) : $p_type ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only: pre-fills the builder type on the new-template screen; the save handler checks its own nonce. ?>" />
256 <p>
257 <label><?php echo esc_html__( 'Container Width', 'ultimate-post' ); ?></label>
258 <input type="number" style="width:100%" name="container-width" value="<?php echo esc_attr( $width ? $width : 1140 ); ?>" />
259 </p>
260 <p class="postx-meta-sidebar-position">
261 <label><?php echo esc_html__( 'Sidebar', 'ultimate-post' ); ?></label>
262 <select name="builder-sidebar" style="width:88%">
263 <option <?php selected( $sidebar, '' ); ?> value=""><?php echo esc_html__( '- None -', 'ultimate-post' ); ?></option>
264 <option <?php selected( $sidebar, 'left' ); ?> value="left"><?php echo esc_html__( 'Left Sidebar', 'ultimate-post' ); ?></option>
265 <option <?php selected( $sidebar, 'right' ); ?> value="right"><?php echo esc_html__( 'Right Sidebar', 'ultimate-post' ); ?></option>
266 </select>
267 </p>
268 <p class="postx-meta-sidebar-widget">
269 <label><?php echo esc_html__( 'Select Sidebar(Widget Area)', 'ultimate-post' ); ?></label>
270 <select name="builder-widget-area" style="width:88%">
271 <option <?php selected( $sidebar, '' ); ?> value=""><?php echo esc_html__( '- None -', 'ultimate-post' ); ?></option>
272 <?php foreach ( $widget_area as $key => $val ) { ?>
273 <option <?php selected( $widget, $key ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( ucwords( str_replace( '-', ' ', $key ) ) ); ?></option>
274 <?php } ?>
275 </select>
276 </p>
277 <?php
278 }
279
280 function metabox_save_data( $post_id ) {
281 // For Featured Video
282 if ( isset( $_POST['video_meta_box_nonce'] ) ) {
283 if ( wp_verify_nonce( sanitize_key( wp_unslash( $_POST['video_meta_box_nonce'] ) ), 'video_meta_box' ) ) {
284 if ( isset( $_POST['feature-video'] ) ) {
285 update_post_meta( $post_id, '__builder_feature_video', sanitize_text_field( $_POST['feature-video'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
286 }
287 if ( ! isset( $_POST['video-caption'] ) ) {
288 update_post_meta( $post_id, '__builder_feature_caption', sanitize_text_field( $_POST['video-caption'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
289 }
290 }
291 }
292
293 // For Container and Sidebar Information
294 if ( ! isset( $_POST['container_meta_box_nonce'] ) ) {
295 return;
296 }
297 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['container_meta_box_nonce'] ) ), 'container_meta_box' ) ) {
298 return;
299 }
300 if ( isset( $_POST['container-width'] ) ) {
301 update_post_meta( $post_id, '__container_width', sanitize_text_field( $_POST['container-width'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
302 }
303 if ( isset( $_POST['builder-sidebar'] ) ) {
304 update_post_meta( $post_id, '__builder_sidebar', sanitize_text_field( $_POST['builder-sidebar'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
305 }
306 if ( isset( $_POST['builder-widget-area'] ) ) {
307 update_post_meta( $post_id, '__builder_widget_area', sanitize_text_field( $_POST['builder-widget-area'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- update_post_meta() unslashes; unslashing here would strip backslashes twice.
308 }
309
310 // Save Conditions Data
311 if ( get_post_type( $post_id ) == 'ultp_builder' ) {
312 if ( get_post_type( $post_id ) == 'ultp_builder' ) {
313 $settings = get_option( 'ultp_builder_conditions', array() );
314 $p_type = isset( $_POST['postx-type'] ) ? sanitize_text_field( wp_unslash( $_POST['postx-type'] ) ) : 'singular';
315 $up_meta = '';
316 switch ( $p_type ) {
317 case 'singular':
318 if ( isset( $settings['singular'] ) ) {
319 if ( ! isset( $settings['singular'][ $post_id ] ) ) {
320 $up_meta = 'singular';
321 $settings['singular'][ $post_id ] = array( 'include/singular/post' );
322 }
323 } else {
324 $up_meta = 'singular';
325 $settings['singular'][ $post_id ] = array( 'include/singular/post' );
326 }
327 break;
328 case 'front_page':
329 if ( isset( $settings['front_page'] ) ) {
330 if ( ! isset( $settings['front_page'][ $post_id ] ) ) {
331 $up_meta = 'front_page';
332 $settings['front_page'][ $post_id ] = array( 'include/front_page' );
333 }
334 } else {
335 $up_meta = 'front_page';
336 $settings['front_page'][ $post_id ] = array( 'include/front_page' );
337 }
338 break;
339 case 'post_tag':
340 case 'date':
341 case 'search':
342 case 'author':
343 case 'archive':
344 case 'category':
345 $extra = $p_type != 'archive' ? '/' . $p_type : '';
346 if ( isset( $settings['archive'] ) ) {
347 if ( ! isset( $settings['archive'][ $post_id ] ) ) {
348 $up_meta = 'archive';
349 $settings['archive'][ $post_id ] = array( 'include/archive' . $extra );
350 }
351 } else {
352 $up_meta = 'archive';
353 $settings['archive'][ $post_id ] = array( 'include/archive' + $extra );
354 }
355 break;
356 case 'header':
357 if ( isset( $settings['header'] ) ) {
358 if ( ! isset( $settings['header'][ $post_id ] ) ) {
359 $up_meta = 'header';
360 $settings['header'][ $post_id ] = array( 'include/header/entire_site' );
361 }
362 } else {
363 $up_meta = 'header';
364 $settings['header'][ $post_id ] = array( 'include/header/entire_site' );
365 }
366 break;
367 case 'footer':
368 if ( isset( $settings['footer'] ) ) {
369 if ( ! isset( $settings['footer'][ $post_id ] ) ) {
370 $up_meta = 'footer';
371 $settings['footer'][ $post_id ] = array( 'include/footer/entire_site' );
372 }
373 } else {
374 $up_meta = 'footer';
375 $settings['footer'][ $post_id ] = array( 'include/footer/entire_site' );
376 }
377 break;
378 case '404':
379 if ( isset( $settings['404'] ) ) {
380 if ( ! isset( $settings['404'][ $post_id ] ) ) {
381 $up_meta = '404';
382 $settings['404'][ $post_id ] = array( 'include/404' );
383 }
384 } else {
385 $up_meta = '404';
386 $settings['404'][ $post_id ] = array( 'include/404' );
387 }
388 default:
389 break;
390 }
391 if ( $up_meta ) {
392 update_post_meta( $post_id, '__ultp_builder_type', $up_meta );
393 }
394 update_option( 'ultp_builder_conditions', $settings );
395 }
396 }
397 }
398
399 // Builder Post Type Register
400 public function builder_post_type_callback() {
401 $labels = array(
402 'name' => _x( 'Builder', 'Builder', 'ultimate-post' ),
403 'singular_name' => _x( 'Builder', 'Builder', 'ultimate-post' ),
404 'menu_name' => __( 'Builder', 'ultimate-post' ),
405 'parent_item_colon' => __( 'Parent Builder', 'ultimate-post' ),
406 'all_items' => __( 'Builder', 'ultimate-post' ),
407 'view_item' => __( 'View Builder', 'ultimate-post' ),
408 'add_new_item' => __( 'Add New', 'ultimate-post' ),
409 'add_new' => __( 'Add New', 'ultimate-post' ),
410 'edit_item' => __( 'Edit Builder', 'ultimate-post' ),
411 'update_item' => __( 'Update Builder', 'ultimate-post' ),
412 'search_items' => __( 'Search Builder', 'ultimate-post' ),
413 'not_found' => __( 'No Builder Found', 'ultimate-post' ),
414 'not_found_in_trash' => __( 'Not Builder found in Trash', 'ultimate-post' ),
415 );
416 $args = array(
417 'labels' => $labels,
418 'show_in_rest' => true,
419 'supports' => array( 'title', 'editor', 'comments' ),
420 'hierarchical' => false,
421 'public' => false,
422 'rewrite' => false,
423 'show_ui' => true,
424 'show_in_menu' => false,
425 'show_in_nav_menus' => false,
426 'exclude_from_search' => true,
427 'capability_type' => 'page',
428 );
429 register_post_type( 'ultp_builder', $args );
430 }
431 }
432