Theme_Builder.php
363 lines
| 1 | <?php |
| 2 | namespace SPEL\includes\classes; |
| 3 | if (!defined('ABSPATH')) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | use SPEL\includes\Admin\Admin_Settings; |
| 8 | |
| 9 | class Theme_Builder { |
| 10 | |
| 11 | public string $dir; |
| 12 | |
| 13 | public string $url; |
| 14 | |
| 15 | /** |
| 16 | * Theme_Builder constructor. |
| 17 | * Initializes the class, sets directory and URL, and registers actions and filters. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | |
| 21 | // Get the Current directory path |
| 22 | $this->dir = dirname( __FILE__ ); |
| 23 | |
| 24 | // Get the current URL |
| 25 | $this->url = plugin_dir_url( __FILE__ ); |
| 26 | |
| 27 | // Register actions and filters |
| 28 | add_action('init', [$this, 'cpt']); |
| 29 | add_action('admin_menu', [$this, 'theme_builder_menu'], 99); |
| 30 | |
| 31 | // Set default page template for new posts |
| 32 | add_action('add_meta_boxes', [$this, 'default_page_template'], 10, 2); |
| 33 | |
| 34 | // Customize admin columns |
| 35 | add_filter('manage_spel_template_posts_columns', [$this, 'set_columns']); |
| 36 | add_action('manage_spel_template_posts_custom_column', [$this, 'set_column_content'], 10, 2); |
| 37 | add_action('admin_init', [$this, 'add_author_support_to_column']); |
| 38 | |
| 39 | // Add modal popup view |
| 40 | add_action('admin_footer', [$this, 'modal_popup_view']); |
| 41 | |
| 42 | |
| 43 | // Enqueue scripts |
| 44 | add_action( 'admin_enqueue_scripts',[$this, 'enqueue_scripts'] ); |
| 45 | |
| 46 | // Register AJAX actions |
| 47 | add_action('wp_ajax_spel_create_template_post', [$this, 'ajax_create_template_post']); |
| 48 | add_action('wp_ajax_nopriv_spel_create_template_post', [$this, 'ajax_create_template_post']); |
| 49 | |
| 50 | add_action('wp_ajax_spel_edit_template_post', [$this, 'ajax_edit_template_post']); |
| 51 | add_action('wp_ajax_nopriv_spel_edit_template_post', [$this, 'ajax_edit_template_post']); |
| 52 | |
| 53 | |
| 54 | // Override the Header templates |
| 55 | add_action('get_header', [$this, 'override_get_header'], 99); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | public function override_get_header(): void |
| 60 | { |
| 61 | |
| 62 | $active_header_template_id = get_option('active_spel_header_template_id'); |
| 63 | |
| 64 | if ($active_header_template_id) { |
| 65 | |
| 66 | $condition = get_post_meta($active_header_template_id, 'spel_template_condition', true); |
| 67 | $status = get_post_meta($active_header_template_id, 'spel_template_status', true); |
| 68 | |
| 69 | if ($status == 'yes' && $condition == 'entire_site' ) { |
| 70 | include_once $this->dir . '/theme-builder/header.php'; |
| 71 | |
| 72 | add_action('after_setup_theme', 'override_header_template', 100); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |
| 80 | public function override_header_template(): void |
| 81 | { |
| 82 | // Remove WordPress version number |
| 83 | remove_action('wp_head', 'wp_generator'); |
| 84 | |
| 85 | // Remove wlwmanifest link |
| 86 | remove_action('wp_head', 'wlwmanifest_link'); |
| 87 | |
| 88 | // Remove RSD link |
| 89 | remove_action('wp_head', 'rsd_link'); |
| 90 | |
| 91 | // Remove shortlink |
| 92 | remove_action('wp_head', 'wp_shortlink_wp_head'); |
| 93 | |
| 94 | // Remove oEmbed discovery links |
| 95 | remove_action('wp_head', 'rest_output_link_wp_head'); |
| 96 | remove_action('wp_head', 'wp_oembed_add_discovery_links'); |
| 97 | |
| 98 | // Remove emojis |
| 99 | remove_action('wp_head', 'print_emoji_detection_script', 7); |
| 100 | remove_action('wp_print_styles', 'print_emoji_styles'); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public function cpt(): void { |
| 105 | $labels = array( |
| 106 | 'name' => esc_html__('Theme Builders', 'spider-elements'), |
| 107 | 'singular_name' => esc_html__('Theme Builder', 'spider-elements'), |
| 108 | 'menu_name' => esc_html__('Theme Builder', 'spider-elements'), |
| 109 | 'name_admin_bar' => esc_html__('Theme Builder', 'spider-elements'), |
| 110 | 'add_new' => esc_html__('Add New', 'spider-elements'), |
| 111 | 'add_new_item' => esc_html__('Add New Template', 'spider-elements'), |
| 112 | 'new_item' => esc_html__('New Template', 'spider-elements'), |
| 113 | 'edit_item' => esc_html__('Edit Template', 'spider-elements'), |
| 114 | 'view_item' => esc_html__('View Template', 'spider-elements'), |
| 115 | 'all_items' => esc_html__('All Templates', 'spider-elements'), |
| 116 | 'search_items' => esc_html__('Search Templates', 'spider-elements'), |
| 117 | 'parent_item_colon' => esc_html__('Parent Templates:', 'spider-elements'), |
| 118 | 'not_found' => esc_html__('No Templates found.', 'spider-elements'), |
| 119 | 'not_found_in_trash' => esc_html__('No Templates found in Trash.', 'spider-elements'), |
| 120 | ); |
| 121 | |
| 122 | $args = array( |
| 123 | 'labels' => $labels, |
| 124 | 'public' => true, |
| 125 | 'publicly_queryable' => true, |
| 126 | 'show_in_rest' => true, // Enable REST API support |
| 127 | 'rest_base' => 'spel_template', // Custom REST API base |
| 128 | 'show_ui' => true, |
| 129 | 'show_in_menu' => false, |
| 130 | 'query_var' => true, |
| 131 | 'rewrite' => array('slug' => 'spel_template'), |
| 132 | 'capability_type' => 'post', |
| 133 | 'has_archive' => true, |
| 134 | 'hierarchical' => true, |
| 135 | 'map_meta_cap' => true, |
| 136 | 'supports' => ['title', 'elementor'], |
| 137 | 'yarpp_support' => true, |
| 138 | 'show_admin_column' => true, |
| 139 | ); |
| 140 | |
| 141 | register_post_type('spel_template', $args); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Adds the theme builder menu to the admin menu. |
| 147 | */ |
| 148 | public function theme_builder_menu(): void |
| 149 | { |
| 150 | add_submenu_page( |
| 151 | Admin_Settings::PAGE_ID, // Parent slug |
| 152 | esc_html__('Theme Builder', 'spider-elements'), // Page title |
| 153 | esc_html__('Theme Builder', 'spider-elements'), // Menu title |
| 154 | 'manage_options', // Capability |
| 155 | 'edit.php?post_type=spel_template', // Slug |
| 156 | false // Function |
| 157 | ); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sets the default page template for new posts of type 'elementor_canvas'. |
| 163 | * |
| 164 | * @param string $post_type The post type. |
| 165 | * @param /WP_Post $post The post object. |
| 166 | */ |
| 167 | public function default_page_template(string $post_type, $post): void |
| 168 | { |
| 169 | if ($post_type == 'spel_template' && $post->post_status == 'auto-draft') { |
| 170 | $default_template = 'elementor_canvas'; |
| 171 | update_post_meta($post->ID, '_wp_page_template', $default_template); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Modifies the columns displayed in the admin list table for 'spel_template'. |
| 178 | * |
| 179 | * @param array $columns The existing columns. |
| 180 | * @return array The modified columns. |
| 181 | */ |
| 182 | public function set_columns(array $columns): array |
| 183 | { |
| 184 | $date = $columns['date'] ?? ''; |
| 185 | $author = $columns['author'] ?? ''; |
| 186 | |
| 187 | unset($columns['date']); |
| 188 | unset($columns['author']); |
| 189 | |
| 190 | $columns['type'] = esc_html__('Type', 'spider-elements'); |
| 191 | $columns['condition'] = esc_html__('Conditions', 'spider-elements'); |
| 192 | $columns['status'] = esc_html__('Status', 'spider-elements'); |
| 193 | |
| 194 | if (!empty($author)) { |
| 195 | $columns['author'] = $author; |
| 196 | } |
| 197 | |
| 198 | if (!empty($date)) { |
| 199 | $columns['date'] = $date; |
| 200 | } |
| 201 | |
| 202 | return $columns; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sets the content for custom columns in the admin list table for 'spel_template'. |
| 207 | * |
| 208 | * @param string $column The name of the column. |
| 209 | * @param int $post_id The post ID. |
| 210 | */ |
| 211 | public function set_column_content(string $column, int $post_id): void |
| 212 | { |
| 213 | switch ($column) { |
| 214 | case 'type': |
| 215 | $type = get_post_meta($post_id, 'spel_template_type', true); |
| 216 | echo esc_html($type); |
| 217 | break; |
| 218 | |
| 219 | case 'condition': |
| 220 | $conditions = get_post_meta($post_id, 'spel_template_condition', true); |
| 221 | echo esc_html($conditions); |
| 222 | break; |
| 223 | |
| 224 | case 'status': |
| 225 | $status = get_post_meta($post_id, 'spel_template_status', true); |
| 226 | echo esc_html($status === 'yes' ? 'Active' : 'Inactive'); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Adds author support to the 'spel_template' post type. |
| 234 | */ |
| 235 | public function add_author_support_to_column(): void |
| 236 | { |
| 237 | add_post_type_support('spel_template', 'author'); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * Includes the modal popup view template. |
| 243 | */ |
| 244 | public function modal_popup_view(): void |
| 245 | { |
| 246 | $screen = get_current_screen(); |
| 247 | if ( $screen->id == 'edit-spel_template' ) { |
| 248 | include_once $this->dir . '/theme-builder/modal-editor.php'; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * Enqueues scripts for the admin area. |
| 255 | */ |
| 256 | public function enqueue_scripts(): void |
| 257 | { |
| 258 | $screen = get_current_screen(); |
| 259 | if ( $screen->id == 'edit-spel_template' ) { |
| 260 | wp_enqueue_script('spel-tb-admin-script', $this->url . 'theme-builder/assets/js/admin-script.js', ['jquery'], SPEL_VERSION, true); |
| 261 | wp_localize_script('spel-tb-admin-script', 'spel_template_object', array( |
| 262 | 'ajaxurl' => admin_url('admin-ajax.php'), |
| 263 | 'nonce' => wp_create_nonce('spel_create_template_post_nonce'), |
| 264 | 'editor_url' => admin_url('post.php') |
| 265 | )); |
| 266 | |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /** |
| 272 | * Handles the AJAX request to create or update a template post. |
| 273 | */ |
| 274 | public function ajax_create_template_post(): void |
| 275 | { |
| 276 | // Check the nonce for security |
| 277 | if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'spel_create_template_post_nonce' ) ) { |
| 278 | wp_send_json_error('Invalid nonce'); |
| 279 | } |
| 280 | |
| 281 | // Parse the form data |
| 282 | parse_str($_POST['form_data'], $form_data); |
| 283 | |
| 284 | $post_id = intval($_POST['post_id']); |
| 285 | $post_title = !empty($form_data['title']) ? sanitize_text_field($form_data['title']) : ''; |
| 286 | $post_type = !empty($form_data['spel_template_type']) ? sanitize_text_field($form_data['spel_template_type']) : ''; |
| 287 | $post_conditions = !empty($form_data['spel_template_condition']) ? sanitize_text_field($form_data['spel_template_condition']) : ''; |
| 288 | $post_status = !empty($form_data['spel_template_status']) ? sanitize_text_field($form_data['spel_template_status']) : ''; |
| 289 | |
| 290 | |
| 291 | // Prepare the post data |
| 292 | $post_data = array( |
| 293 | 'post_title' => $post_title, |
| 294 | 'post_type' => 'spel_template', |
| 295 | 'post_status' => 'publish' |
| 296 | ); |
| 297 | |
| 298 | if ($post_id) { |
| 299 | // Update existing post |
| 300 | $post_data['ID'] = $post_id; |
| 301 | wp_update_post($post_data); |
| 302 | } else { |
| 303 | // Create a new post |
| 304 | $post_id = wp_insert_post($post_data); |
| 305 | } |
| 306 | |
| 307 | if ($post_id) { |
| 308 | |
| 309 | // Update post meta |
| 310 | update_post_meta($post_id, '_wp_page_template', 'elementor_canvas'); |
| 311 | update_post_meta($post_id, 'spel_template_type', $post_type); |
| 312 | update_post_meta($post_id, 'spel_template_condition', $post_conditions); |
| 313 | update_post_meta($post_id, 'spel_template_status', $post_status); |
| 314 | |
| 315 | // Set active header/footer template ID if the status is active |
| 316 | if ($post_type === 'header' && $post_status === 'yes') { |
| 317 | update_option('active_spel_header_template_id', $post_id); |
| 318 | } elseif ($post_type === 'footer' && $post_status === 'yes') { |
| 319 | update_option('active_spel_footer_template_id', $post_id); |
| 320 | } |
| 321 | |
| 322 | wp_send_json_success(array('post_id' => $post_id)); |
| 323 | |
| 324 | } else { |
| 325 | wp_send_json_error(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /** |
| 331 | * Handles the AJAX request to fetch the details of a template post. |
| 332 | */ |
| 333 | public function ajax_edit_template_post(): void |
| 334 | { |
| 335 | // Check the nonce for security |
| 336 | if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'spel_create_template_post_nonce' ) ) { |
| 337 | wp_send_json_error('Invalid nonce'); |
| 338 | } |
| 339 | |
| 340 | $post_id = intval($_POST['post_id']); |
| 341 | |
| 342 | if (!$post_id) { |
| 343 | wp_send_json_error('Invalid post ID'); |
| 344 | } |
| 345 | |
| 346 | $post = get_post($post_id); |
| 347 | |
| 348 | if ($post && $post->post_type === 'spel_template') { |
| 349 | wp_send_json_success(array( |
| 350 | 'post_title' => $post->post_title, |
| 351 | 'type' => get_post_meta($post_id, 'spel_template_type', true), |
| 352 | 'condition' => get_post_meta($post_id, 'spel_template_condition', true), |
| 353 | 'status' => get_post_meta($post_id, 'spel_template_status', true) |
| 354 | )); |
| 355 | } else { |
| 356 | wp_send_json_error('Post not found or invalid post type'); |
| 357 | } |
| 358 | |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | } |