| 1 |
<?php |
| 2 |
namespace ULTP; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Builder { |
| 7 |
public function __construct(){ |
| 8 |
$this->builder_post_type_callback(); // Post Type Register |
| 9 |
add_action('wp', array($this, 'checkfor_header_footer'), 999); |
| 10 |
add_filter('template_include', array($this, 'include_builder_files'), 100); |
| 11 |
add_action('add_meta_boxes', array($this, 'init_metabox_callback')); |
| 12 |
add_action('save_post', array($this, 'metabox_save_data')); |
| 13 |
add_action('save_post', array($this, 'metabox_save_video_data')); |
| 14 |
add_action('delete_post', array($this, 'delete_option_meta_action')); |
| 15 |
add_action('load-post-new.php', array($this, 'disable_new_post_templates')); |
| 16 |
} |
| 17 |
|
| 18 |
public function checkfor_header_footer() { |
| 19 |
$theme_name = get_template(); |
| 20 |
$header_id = ultimate_post()->conditions('header'); |
| 21 |
$footer_id = ultimate_post()->conditions('footer'); |
| 22 |
|
| 23 |
if ( $header_id ) { |
| 24 |
if ( wp_is_block_theme() ) { |
| 25 |
// add_action('wp_head', function() use ($header_id) { |
| 26 |
// $this->header_builder_template($header_id); |
| 27 |
// }); |
| 28 |
add_action('wp_head', array($this, 'header_builder_template')); |
| 29 |
} else { |
| 30 |
// add_action('get_header', function() use ($header_id) { |
| 31 |
// $this->header_builder_template($header_id); |
| 32 |
// }); |
| 33 |
add_action('get_header', array($this, 'header_builder_template')); |
| 34 |
} |
| 35 |
} |
| 36 |
if ( $footer_id ) { |
| 37 |
if ( wp_is_block_theme() ) { |
| 38 |
add_action('wp_footer', function() use ($footer_id, $theme_name) { |
| 39 |
$this->footer_builder_template($footer_id, $theme_name); |
| 40 |
}); |
| 41 |
} else { |
| 42 |
switch ($theme_name) { |
| 43 |
case 'astra': |
| 44 |
remove_all_actions( 'astra_footer' ); |
| 45 |
add_action( 'astra_footer', function() use ($footer_id, $theme_name) { |
| 46 |
$this->footer_builder_template($footer_id, $theme_name); |
| 47 |
}); |
| 48 |
break; |
| 49 |
case 'generatepress': |
| 50 |
remove_action( 'generate_footer', 'generate_construct_footer_widgets'); |
| 51 |
remove_action( 'generate_footer', 'generate_construct_footer' ); |
| 52 |
add_action( 'generate_footer', function() use ($footer_id, $theme_name) { |
| 53 |
$this->footer_builder_template($footer_id, $theme_name); |
| 54 |
}); |
| 55 |
break; |
| 56 |
default: |
| 57 |
add_action('get_footer', function() use ($footer_id, $theme_name) { |
| 58 |
$this->footer_builder_template($footer_id, $theme_name); |
| 59 |
}); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function header_builder_template() { |
| 66 |
$header_id = ultimate_post()->conditions('header'); |
| 67 |
$theme_name = get_template(); |
| 68 |
if ($header_id) { |
| 69 |
if ( !wp_is_block_theme() ) { |
| 70 |
require_once ULTP_PATH.'addons/builder/templates/header.php'; |
| 71 |
$templates = []; |
| 72 |
$templates[] = 'header.php'; |
| 73 |
remove_all_actions( 'wp_head' ); |
| 74 |
if ($theme_name != 'bricks') { // Conflict with Bricks Builder Backend |
| 75 |
ob_start(); |
| 76 |
} |
| 77 |
locate_template( $templates, true ); |
| 78 |
if ($theme_name != 'bricks') { // Conflict with Bricks Builder Backend |
| 79 |
ob_get_clean(); |
| 80 |
} else { |
| 81 |
wp_enqueue_style( 'wp-block-library' ); // Gutenberg CSS issue Bricks Builder frontend |
| 82 |
} |
| 83 |
} |
| 84 |
ultimate_post()->register_scripts_common(); |
| 85 |
?> |
| 86 |
<header id="ultp-header-template" class="<?php esc_html_e('ultp-builderid-'.$header_id); ?>"> |
| 87 |
<?php echo ultimate_post()->content($header_id); //phpcs:ignore ?> |
| 88 |
</header> |
| 89 |
<?php |
| 90 |
} |
| 91 |
} |
| 92 |
public function footer_builder_template($footer_id, $theme_name = '') { |
| 93 |
if ($footer_id) { |
| 94 |
ultimate_post()->register_scripts_common(); |
| 95 |
if ( !wp_is_block_theme() ) { |
| 96 |
require_once ULTP_PATH.'addons/builder/templates/footer.php'; |
| 97 |
$templates = []; |
| 98 |
$templates[] = 'footer.php'; |
| 99 |
remove_all_actions( 'wp_footer' ); |
| 100 |
if ($theme_name != 'bricks') { // Conflict with Bricks Builder Backend |
| 101 |
ob_start(); |
| 102 |
} |
| 103 |
locate_template( $templates, true ); |
| 104 |
if ($theme_name != 'bricks') { // Conflict with Bricks Builder Backend |
| 105 |
ob_get_clean(); |
| 106 |
} |
| 107 |
} |
| 108 |
?> |
| 109 |
<footer id="ultp-footer-template" class="<?php esc_html_e('ultp-builderid-'.$footer_id); ?>" role="contentinfo"> |
| 110 |
<?php echo ultimate_post()->content($footer_id) //phpcs:ignore ?> |
| 111 |
</footer> |
| 112 |
<?php |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public function disable_new_post_templates() { |
| 117 |
if ( get_current_screen()->post_type == 'ultp_builder' && (!defined('ULTP_PRO_VER')) ){ |
| 118 |
$post_count = wp_count_posts('ultp_builder'); |
| 119 |
$post_count = $post_count->publish + $post_count->draft; |
| 120 |
if ($post_count > 0) { |
| 121 |
wp_die( 'You are not allowed to do that! Only one template can be created in the free version. Please <a target="_blank" href="'.esc_url(ultimate_post()->get_premium_link()).'">Upgrade Pro.</a>' ); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public function delete_option_meta_action( $post_id ) { |
| 127 |
if (get_post_type( $post_id ) != 'ultp_builder') { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$conditions = get_option('ultp_builder_conditions', array()); |
| 132 |
if($conditions){ |
| 133 |
$has_change = false; |
| 134 |
if(isset($conditions['archive'][$post_id])) { |
| 135 |
$has_change = true; |
| 136 |
unset($conditions['archive'][$post_id]); |
| 137 |
} |
| 138 |
if(isset($conditions['singular'][$post_id])) { |
| 139 |
$has_change = true; |
| 140 |
unset($conditions['singular'][$post_id]); |
| 141 |
} |
| 142 |
if($has_change) { |
| 143 |
update_option('ultp_builder_conditions', $conditions); |
| 144 |
} |
| 145 |
} |
| 146 |
delete_post_meta($post_id, '_ultp_active'); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public function include_builder_files($template) { |
| 151 |
$includes = ultimate_post()->conditions('includes'); |
| 152 |
return $includes ? $includes : $template; |
| 153 |
} |
| 154 |
|
| 155 |
function init_metabox_callback() { |
| 156 |
$all_types = get_post_types( array( 'public' => true ), 'names' ); |
| 157 |
|
| 158 |
$post_type = array_diff_key($all_types, array('page' => 'page', 'attachment' => 'attachment' )); |
| 159 |
$title = '<div class="ultp-add-media-image-head"><img src="'.ULTP_URL.'assets/img/logo-sm.svg" /><span>PostX Settings</span></div>'; |
| 160 |
|
| 161 |
add_meta_box( |
| 162 |
'postx-builder-id', |
| 163 |
__('PostX Builder Settings', 'ultimate-post'), |
| 164 |
array($this, 'container_width_callback'), |
| 165 |
'ultp_builder', |
| 166 |
'side' |
| 167 |
); |
| 168 |
add_meta_box( |
| 169 |
'ultp-feature-video', |
| 170 |
$title, |
| 171 |
array($this, 'video_source_callback'), |
| 172 |
$post_type, |
| 173 |
'side' |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
function video_source_callback($post) { |
| 178 |
wp_nonce_field('video_meta_box', 'video_meta_box_nonce'); |
| 179 |
$video = get_post_meta($post->ID, '__builder_feature_video', true); |
| 180 |
$caption = get_post_meta($post->ID, '__builder_feature_caption', true); |
| 181 |
?> |
| 182 |
<div class="ultp-meta-video"> |
| 183 |
<?php if(current_user_can('administrator')) { ?> |
| 184 |
<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> |
| 185 |
<?php } ?> |
| 186 |
<label><?php echo esc_html__('Featured Video', 'ultimate-post'); ?></label> |
| 187 |
<div class="ultp-video-input"> |
| 188 |
<input id="ultp-add-input" type="text" name="feature-video" value="<?php echo esc_attr($video); ?>" autocomplete="off"/> |
| 189 |
<button class="ultp-add-media"><span class="dashicons dashicons-cloud-upload"></span></button> |
| 190 |
</div> |
| 191 |
<span><strong><?php echo esc_html__('Note: ', 'ultimate-post'); ?></strong><?php echo esc_html__('Enter Youtube/ Vimeo/ Self Hosted URL', 'ultimate-post'); ?></span> |
| 192 |
<label><?php echo esc_html__('Featured Video Caption', 'ultimate-post'); ?></label> |
| 193 |
<input id="ultp-add-caption" type="text" name="video-caption" value="<?php echo esc_attr($caption); ?>" autocomplete="off"/> |
| 194 |
</div> |
| 195 |
<?php } |
| 196 |
|
| 197 |
|
| 198 |
function metabox_save_video_data($post_id) { |
| 199 |
if (!isset($_POST['video_meta_box_nonce'])) { return; } |
| 200 |
if (!wp_verify_nonce(sanitize_key(wp_unslash($_POST['video_meta_box_nonce'])), 'video_meta_box')) { return; } |
| 201 |
if (!isset( $_POST['feature-video'])) { return; } |
| 202 |
update_post_meta($post_id, '__builder_feature_video', sanitize_text_field($_POST['feature-video'])); |
| 203 |
if (!isset( $_POST['video-caption'])) { return; } |
| 204 |
update_post_meta($post_id, '__builder_feature_caption', sanitize_text_field($_POST['video-caption'])); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
function container_width_callback($post) { |
| 209 |
wp_nonce_field('container_meta_box', 'container_meta_box_nonce'); |
| 210 |
$width = get_post_meta($post->ID, '__container_width', true); |
| 211 |
$sidebar = get_post_meta($post->ID, '__builder_sidebar', true); |
| 212 |
$widget = get_post_meta($post->ID, '__builder_widget_area', true); |
| 213 |
$p_type = get_post_meta($post->ID, '__ultp_builder_type', true); |
| 214 |
$p_type = $p_type ? $p_type : 'archive'; |
| 215 |
|
| 216 |
$widget_area = wp_get_sidebars_widgets(); |
| 217 |
if (isset($widget_area['wp_inactive_widgets'])) { unset($widget_area['wp_inactive_widgets']); } |
| 218 |
if (isset($widget_area['array_version'])) { unset($widget_area['array_version']); } |
| 219 |
?> |
| 220 |
|
| 221 |
<input type="hidden" name="postx-type" value="<?php echo esc_attr(isset($_GET['postx_type']) ? sanitize_text_field($_GET['postx_type']) : $p_type); // @codingStandardsIgnoreLine ?>"/> |
| 222 |
<p> |
| 223 |
<label><?php echo esc_html__('Container Width', 'ultimate-post'); ?></label> |
| 224 |
<input type="number" style="width:100%" name="container-width" value="<?php echo esc_attr($width ? $width : 1140); ?>"/> |
| 225 |
</p> |
| 226 |
<p class="postx-meta-sidebar-position"> |
| 227 |
<label><?php echo esc_html__('Sidebar', 'ultimate-post'); ?></label> |
| 228 |
<select name="builder-sidebar" style="width:88%"> |
| 229 |
<option <?php selected( $sidebar, '' ); ?> value=""><?php echo esc_html__('- None -', 'ultimate-post'); ?></option> |
| 230 |
<option <?php selected( $sidebar, 'left' ); ?> value="left"><?php echo esc_html__('Left Sidebar', 'ultimate-post'); ?></option> |
| 231 |
<option <?php selected( $sidebar, 'right' ); ?> value="right"><?php echo esc_html__('Right Sidebar', 'ultimate-post'); ?></option> |
| 232 |
</select> |
| 233 |
</p> |
| 234 |
<p class="postx-meta-sidebar-widget"> |
| 235 |
<label><?php echo esc_html__('Select Sidebar(Widget Area)', 'ultimate-post'); ?></label> |
| 236 |
<select name="builder-widget-area" style="width:88%"> |
| 237 |
<option <?php selected( $sidebar, '' ); ?> value=""><?php echo esc_html__('- None -', 'ultimate-post'); ?></option> |
| 238 |
<?php foreach ($widget_area as $key => $val) { ?> |
| 239 |
<option <?php selected( $widget, $key ); ?> value="<?php echo esc_attr($key); ?>"><?php echo esc_html(ucwords(str_replace('-', ' ', $key))); ?></option> |
| 240 |
<?php } ?> |
| 241 |
</select> |
| 242 |
</p> |
| 243 |
<?php } |
| 244 |
|
| 245 |
function metabox_save_data($post_id) { |
| 246 |
// For Featured Video |
| 247 |
if (isset($_POST['video_meta_box_nonce'])) { |
| 248 |
if (wp_verify_nonce(sanitize_key(wp_unslash($_POST['video_meta_box_nonce'])), 'video_meta_box')) { |
| 249 |
if (isset($_POST['feature-video'])) { |
| 250 |
update_post_meta($post_id, '__builder_feature_video', sanitize_text_field($_POST['feature-video'])); |
| 251 |
} |
| 252 |
if(!isset( $_POST['video-caption'])){ |
| 253 |
update_post_meta($post_id, '__builder_feature_caption', sanitize_text_field($_POST['video-caption'])); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
// For Container and Sidebar Information |
| 259 |
if (!isset($_POST['container_meta_box_nonce'])){ return; } |
| 260 |
if (!wp_verify_nonce( sanitize_key( wp_unslash($_POST['container_meta_box_nonce'])), 'container_meta_box')) { return; } |
| 261 |
if (isset($_POST['container-width'])) { |
| 262 |
update_post_meta($post_id, '__container_width', sanitize_text_field($_POST['container-width'])); |
| 263 |
} |
| 264 |
if (isset($_POST['builder-sidebar'])) { |
| 265 |
update_post_meta($post_id, '__builder_sidebar', sanitize_text_field($_POST['builder-sidebar'])); |
| 266 |
} |
| 267 |
if (isset($_POST['builder-widget-area'])) { |
| 268 |
update_post_meta($post_id, '__builder_widget_area', sanitize_text_field($_POST['builder-widget-area'])); |
| 269 |
} |
| 270 |
|
| 271 |
// Save Conditions Data |
| 272 |
if (get_post_type($post_id) == 'ultp_builder') { |
| 273 |
if (get_post_type($post_id) == 'ultp_builder') { |
| 274 |
$settings = get_option('ultp_builder_conditions', array()); |
| 275 |
$p_type = isset($_POST['postx-type']) ? sanitize_text_field($_POST['postx-type']) : 'singular'; |
| 276 |
switch ($p_type) { |
| 277 |
case 'singular': |
| 278 |
update_post_meta($post_id, '__ultp_builder_type', 'singular'); |
| 279 |
if (isset($settings['singular'])) { |
| 280 |
if (!isset($settings['singular'][$post_id])) { |
| 281 |
$settings['singular'][$post_id] = ['include/singular/post']; |
| 282 |
} |
| 283 |
} else { |
| 284 |
$settings['singular'][$post_id] = ['include/singular/post']; |
| 285 |
} |
| 286 |
break; |
| 287 |
case 'post_tag': |
| 288 |
case 'date': |
| 289 |
case 'search': |
| 290 |
case 'author': |
| 291 |
case 'archive': |
| 292 |
case 'category': |
| 293 |
update_post_meta($post_id, '__ultp_builder_type', 'archive'); |
| 294 |
$extra = $p_type != 'archive' ? '/'.$p_type : ''; |
| 295 |
if (isset($settings['archive'])) { |
| 296 |
if (!isset($settings['archive'][$post_id])) { |
| 297 |
$settings['archive'][$post_id] = ['include/archive'.$extra]; |
| 298 |
} |
| 299 |
} else { |
| 300 |
$settings['archive'][$post_id] = ['include/archive'+$extra]; |
| 301 |
} |
| 302 |
break; |
| 303 |
case 'header': |
| 304 |
if ( isset($settings['header']) ) { |
| 305 |
if ( !isset($settings['header'][$post_id]) ) { |
| 306 |
$settings['header'][$post_id] = ['include/header/entire_site']; |
| 307 |
} |
| 308 |
} else { |
| 309 |
$settings['header'][$post_id] = ['include/header/entire_site']; |
| 310 |
} |
| 311 |
break; |
| 312 |
case 'footer': |
| 313 |
if ( isset($settings['footer']) ) { |
| 314 |
if ( !isset($settings['footer'][$post_id]) ) { |
| 315 |
$settings['footer'][$post_id] = ['include/footer/entire_site']; |
| 316 |
} |
| 317 |
} else { |
| 318 |
$settings['footer'][$post_id] = ['include/footer/entire_site']; |
| 319 |
} |
| 320 |
break; |
| 321 |
case '404': |
| 322 |
if ( isset($settings['404']) ) { |
| 323 |
if ( !isset($settings['404'][$post_id]) ) { |
| 324 |
$settings['404'][$post_id] = ['include/404']; |
| 325 |
} |
| 326 |
} else { |
| 327 |
$settings['404'][$post_id] = ['include/404']; |
| 328 |
} |
| 329 |
default: |
| 330 |
break; |
| 331 |
} |
| 332 |
update_option('ultp_builder_conditions', $settings); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// Builder Post Type Register |
| 338 |
public function builder_post_type_callback() { |
| 339 |
$labels = array( |
| 340 |
'name' => _x( 'Builder', 'Builder', 'ultimate-post' ), |
| 341 |
'singular_name' => _x( 'Builder', 'Builder', 'ultimate-post' ), |
| 342 |
'menu_name' => __( 'Builder', 'ultimate-post' ), |
| 343 |
'parent_item_colon' => __( 'Parent Builder', 'ultimate-post' ), |
| 344 |
'all_items' => __( 'Builder', 'ultimate-post' ), |
| 345 |
'view_item' => __( 'View Builder', 'ultimate-post' ), |
| 346 |
'add_new_item' => __( 'Add New', 'ultimate-post' ), |
| 347 |
'add_new' => __( 'Add New', 'ultimate-post' ), |
| 348 |
'edit_item' => __( 'Edit Builder', 'ultimate-post' ), |
| 349 |
'update_item' => __( 'Update Builder', 'ultimate-post' ), |
| 350 |
'search_items' => __( 'Search Builder', 'ultimate-post' ), |
| 351 |
'not_found' => __( 'No Builder Found', 'ultimate-post' ), |
| 352 |
'not_found_in_trash' => __( 'Not Builder found in Trash', 'ultimate-post' ), |
| 353 |
); |
| 354 |
$args = array( |
| 355 |
'labels' => $labels, |
| 356 |
'show_in_rest' => true, |
| 357 |
'supports' => array( 'title', 'editor', 'comments' ), |
| 358 |
'hierarchical' => false, |
| 359 |
'public' => false, |
| 360 |
'rewrite' => false, |
| 361 |
'show_ui' => true, |
| 362 |
'show_in_menu' => false, |
| 363 |
'show_in_nav_menus' => false, |
| 364 |
'exclude_from_search' => true, |
| 365 |
'capability_type' => 'page', |
| 366 |
); |
| 367 |
register_post_type( 'ultp_builder', $args ); |
| 368 |
} |
| 369 |
} |