| 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_filter('template_include', array($this, 'include_builder_files'), 100); |
| 10 |
add_action('add_meta_boxes', array($this, 'init_metabox_callback')); |
| 11 |
add_action('save_post', array($this, 'metabox_save_data')); |
| 12 |
add_action('save_post', array($this, 'metabox_save_video_data')); |
| 13 |
add_action('delete_post', array($this, 'delete_option_meta_action')); |
| 14 |
add_action('admin_head', array($this, 'custom_head_templates')); |
| 15 |
add_action('load-post-new.php', array($this, 'disable_new_post_templates')); |
| 16 |
} |
| 17 |
|
| 18 |
public function custom_head_templates() { |
| 19 |
if( 'ultp_builder' == get_current_screen()->post_type && (!defined('ULTP_PRO_VER')) ) { |
| 20 |
$post_count = wp_count_posts('ultp_builder'); |
| 21 |
$post_count = $post_count->publish + $post_count->draft; |
| 22 |
if ( $post_count > 0 ) { ?> |
| 23 |
<span class="ultp-pro-needed" style="display: none;"></span> |
| 24 |
<?php ultimate_post()->pro_popup_html(); ?> |
| 25 |
<?php } |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function disable_new_post_templates() { |
| 30 |
if ( get_current_screen()->post_type == 'ultp_builder' && (!defined('ULTP_PRO_VER')) ){ |
| 31 |
$post_count = wp_count_posts('ultp_builder'); |
| 32 |
$post_count = $post_count->publish + $post_count->draft; |
| 33 |
if ($post_count > 0) { |
| 34 |
wp_die( 'You are not allowed to do that! Please <a target="_blank" href="'.esc_url(ultimate_post()->get_premium_link()).'">Upgrade Pro.</a>' ); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function delete_option_meta_action( $post_id ) { |
| 40 |
if (get_post_type( $post_id ) != 'ultp_builder') { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$conditions = get_option('ultp_builder_conditions', array()); |
| 45 |
if($conditions){ |
| 46 |
$has_change = false; |
| 47 |
if(isset($conditions['archive'][$post_id])) { |
| 48 |
$has_change = true; |
| 49 |
unset($conditions['archive'][$post_id]); |
| 50 |
} |
| 51 |
if(isset($conditions['singular'][$post_id])) { |
| 52 |
$has_change = true; |
| 53 |
unset($conditions['singular'][$post_id]); |
| 54 |
} |
| 55 |
if($has_change) { |
| 56 |
update_option('ultp_builder_conditions', $conditions); |
| 57 |
} |
| 58 |
} |
| 59 |
delete_post_meta($post_id, '_ultp_active'); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function include_builder_files($template) { |
| 64 |
$includes = ultimate_post()->conditions('includes'); |
| 65 |
return $includes ? $includes : $template; |
| 66 |
} |
| 67 |
|
| 68 |
function init_metabox_callback() { |
| 69 |
add_meta_box( |
| 70 |
'postx-builder-id', |
| 71 |
__('PostX Builder Settings', 'ultimate-post-pro'), |
| 72 |
array($this, 'container_width_callback'), |
| 73 |
'ultp_builder', |
| 74 |
'side' |
| 75 |
); |
| 76 |
add_meta_box( |
| 77 |
'ultp-feature-video', |
| 78 |
__('Feature Video', 'ultimate-post-pro'), |
| 79 |
array($this, 'video_source_callback'), |
| 80 |
'post', |
| 81 |
'side' |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
function video_source_callback($post) { |
| 86 |
wp_nonce_field('video_meta_box', 'video_meta_box_nonce'); |
| 87 |
$video = get_post_meta($post->ID, '__builder_feature_video', true); ?> |
| 88 |
<p> |
| 89 |
<label><?php _e('PostX Feature Video', 'ultimate-post-pro'); ?></label> |
| 90 |
<input type="text" name="feature-video" value="<?php echo $video; ?>"/> |
| 91 |
<button class="ultp-add-media"><?php _e('+ Media', 'ultimate-post-pro'); ?></button> |
| 92 |
</p> |
| 93 |
<?php } |
| 94 |
|
| 95 |
|
| 96 |
function metabox_save_video_data($post_id) { |
| 97 |
if (!isset($_POST['video_meta_box_nonce'])) { return; } |
| 98 |
if (!wp_verify_nonce(wp_unslash($_POST['video_meta_box_nonce']), 'video_meta_box')) { return; } |
| 99 |
if (!isset( $_POST['feature-video'])) { return; } |
| 100 |
|
| 101 |
update_post_meta($post_id, '__builder_feature_video', sanitize_text_field($_POST['feature-video'])); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
function container_width_callback($post) { |
| 106 |
wp_nonce_field('container_meta_box', 'container_meta_box_nonce'); |
| 107 |
$width = get_post_meta($post->ID, '__container_width', true); |
| 108 |
$sidebar = get_post_meta($post->ID, '__builder_sidebar', true); |
| 109 |
$widget = get_post_meta($post->ID, '__builder_widget_area', true); |
| 110 |
$p_type = get_post_meta($post->ID, '__ultp_builder_type', true); |
| 111 |
$p_type = $p_type ? $p_type : 'archive'; |
| 112 |
|
| 113 |
$widget_area = wp_get_sidebars_widgets(); |
| 114 |
if (isset($widget_area['wp_inactive_widgets'])) { unset($widget_area['wp_inactive_widgets']); } |
| 115 |
if (isset($widget_area['array_version'])) { unset($widget_area['array_version']); } |
| 116 |
?> |
| 117 |
|
| 118 |
<input type="hidden" name="postx-type" value="<?php echo (isset($_GET['postx_type']) ? $_GET['postx_type'] : $p_type); ?>"/> |
| 119 |
<p> |
| 120 |
<label><?php _e('Container Width', 'ultimate-post-pro'); ?></label> |
| 121 |
<input type="number" style="width:100%" name="container-width" value="<?php echo ($width ? $width : 1140); ?>"/> |
| 122 |
</p> |
| 123 |
<p class="postx-meta-sidebar-position"> |
| 124 |
<label><?php _e('Sidebar', 'ultimate-post-pro'); ?></label> |
| 125 |
<select name="builder-sidebar" style="width:88%"> |
| 126 |
<option <?php selected( $sidebar, '' ); ?> value=""><?php _e('- None -', 'ultimate-post-pro'); ?></option> |
| 127 |
<option <?php selected( $sidebar, 'left' ); ?> value="left"><?php _e('Left Sidebar', 'ultimate-post-pro'); ?></option> |
| 128 |
<option <?php selected( $sidebar, 'right' ); ?> value="right"><?php _e('Right Sidebar', 'ultimate-post-pro'); ?></option> |
| 129 |
</select> |
| 130 |
</p> |
| 131 |
<p class="postx-meta-sidebar-widget"> |
| 132 |
<label><?php _e('Select Sidebar(Widget Area)', 'ultimate-post-pro'); ?></label> |
| 133 |
<select name="builder-widget-area" style="width:88%"> |
| 134 |
<option <?php selected( $sidebar, '' ); ?> value=""><?php _e('- None -', 'ultimate-post-pro'); ?></option> |
| 135 |
<?php foreach ($widget_area as $key => $val) { ?> |
| 136 |
<option <?php selected( $widget, $key ); ?> value="<?php echo $key; ?>"><?php echo ucwords(str_replace('-', ' ', $key)); ?></option> |
| 137 |
<?php } ?> |
| 138 |
</select> |
| 139 |
</p> |
| 140 |
<?php } |
| 141 |
|
| 142 |
function metabox_save_data($post_id) { |
| 143 |
// For Feature Video |
| 144 |
if (isset($_POST['video_meta_box_nonce'])) { |
| 145 |
if (wp_verify_nonce(wp_unslash($_POST['video_meta_box_nonce']), 'video_meta_box')) { |
| 146 |
if (isset($_POST['feature-video'])) { |
| 147 |
update_post_meta($post_id, '__builder_feature_video', sanitize_text_field($_POST['feature-video'])); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// For Container and Sidebar Information |
| 153 |
if (!isset($_POST['container_meta_box_nonce'])){ return; } |
| 154 |
if (!wp_verify_nonce( wp_unslash($_POST['container_meta_box_nonce']), 'container_meta_box')) { return; } |
| 155 |
if (isset($_POST['container-width'])) { |
| 156 |
update_post_meta($post_id, '__container_width', sanitize_text_field($_POST['container-width'])); |
| 157 |
} |
| 158 |
if (isset($_POST['builder-sidebar'])) { |
| 159 |
update_post_meta($post_id, '__builder_sidebar', sanitize_text_field($_POST['builder-sidebar'])); |
| 160 |
} |
| 161 |
if (isset($_POST['builder-widget-area'])) { |
| 162 |
update_post_meta($post_id, '__builder_widget_area', sanitize_text_field($_POST['builder-widget-area'])); |
| 163 |
} |
| 164 |
|
| 165 |
// Save Conditions Data |
| 166 |
if (get_post_type($post_id) == 'ultp_builder') { |
| 167 |
if (get_post_type($post_id) == 'ultp_builder') { |
| 168 |
$settings = get_option('ultp_builder_conditions', array()); |
| 169 |
$p_type = isset($_POST['postx-type']) ? sanitize_text_field($_POST['postx-type']) : 'singular'; |
| 170 |
switch ($p_type) { |
| 171 |
case 'singular': |
| 172 |
update_post_meta($post_id, '__ultp_builder_type', 'singular'); |
| 173 |
if (isset($settings['singular'])) { |
| 174 |
if (!isset($settings['singular'][$post_id])) { |
| 175 |
$settings['singular'][$post_id] = ['include/singular/post']; |
| 176 |
} |
| 177 |
} else { |
| 178 |
$settings['singular'][$post_id] = ['include/singular/post']; |
| 179 |
} |
| 180 |
update_option('ultp_builder_conditions', $settings); |
| 181 |
break; |
| 182 |
case 'post_tag': |
| 183 |
case 'date': |
| 184 |
case 'search': |
| 185 |
case 'author': |
| 186 |
case 'archive': |
| 187 |
case 'category': |
| 188 |
update_post_meta($post_id, '__ultp_builder_type', 'archive'); |
| 189 |
$extra = $p_type != 'archive' ? '/'.$p_type : ''; |
| 190 |
if (isset($settings['archive'])) { |
| 191 |
if (!isset($settings['archive'][$post_id])) { |
| 192 |
$settings['archive'][$post_id] = ['include/archive'.$extra]; |
| 193 |
} |
| 194 |
} else { |
| 195 |
$settings['archive'][$post_id] = ['include/archive'+$extra]; |
| 196 |
} |
| 197 |
update_option('ultp_builder_conditions', $settings); |
| 198 |
break; |
| 199 |
default: |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// Builder Post Type Register |
| 207 |
public function builder_post_type_callback() { |
| 208 |
$labels = array( |
| 209 |
'name' => _x( 'Builder', 'Builder', 'ultimate-post-pro' ), |
| 210 |
'singular_name' => _x( 'Builder', 'Builder', 'ultimate-post-pro' ), |
| 211 |
'menu_name' => __( 'Builder', 'ultimate-post-pro' ), |
| 212 |
'parent_item_colon' => __( 'Parent Builder', 'ultimate-post-pro' ), |
| 213 |
'all_items' => __( 'Builder', 'ultimate-post-pro' ), |
| 214 |
'view_item' => __( 'View Builder', 'ultimate-post-pro' ), |
| 215 |
'add_new_item' => __( 'Add New', 'ultimate-post-pro' ), |
| 216 |
'add_new' => __( 'Add New', 'ultimate-post-pro' ), |
| 217 |
'edit_item' => __( 'Edit Builder', 'ultimate-post-pro' ), |
| 218 |
'update_item' => __( 'Update Builder', 'ultimate-post-pro' ), |
| 219 |
'search_items' => __( 'Search Builder', 'ultimate-post-pro' ), |
| 220 |
'not_found' => __( 'No Builder Found', 'ultimate-post-pro' ), |
| 221 |
'not_found_in_trash' => __( 'Not Builder found in Trash', 'ultimate-post-pro' ), |
| 222 |
); |
| 223 |
$args = array( |
| 224 |
'labels' => $labels, |
| 225 |
'show_in_rest' => true, |
| 226 |
'supports' => array( 'title', 'editor' ), |
| 227 |
'hierarchical' => false, |
| 228 |
'public' => false, |
| 229 |
'rewrite' => false, |
| 230 |
'show_ui' => true, |
| 231 |
'show_in_menu' => false, |
| 232 |
'show_in_nav_menus' => false, |
| 233 |
'exclude_from_search' => true, |
| 234 |
'capability_type' => 'page', |
| 235 |
); |
| 236 |
register_post_type( 'ultp_builder', $args ); |
| 237 |
} |
| 238 |
} |