init_hooks(); } /** * Initialize hooks */ private function init_hooks() { add_action('init', array($this, 'register_post_type')); add_action('init', array($this, 'register_taxonomies')); add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts')); // AJAX handlers for template operations add_action('wp_ajax_hvac_get_template_preview', array($this, 'ajax_get_template_preview')); add_action('wp_ajax_hvac_search_templates', array($this, 'ajax_search_templates')); } /** * Register the communication templates custom post type */ public function register_post_type() { $labels = array( 'name' => _x('Communication Templates', 'Post type general name', 'hvac-community-events'), 'singular_name' => _x('Communication Template', 'Post type singular name', 'hvac-community-events'), 'menu_name' => _x('Communication Templates', 'Admin Menu text', 'hvac-community-events'), 'name_admin_bar' => _x('Communication Template', 'Add New on Toolbar', 'hvac-community-events'), 'add_new' => __('Add New', 'hvac-community-events'), 'add_new_item' => __('Add New Template', 'hvac-community-events'), 'new_item' => __('New Template', 'hvac-community-events'), 'edit_item' => __('Edit Template', 'hvac-community-events'), 'view_item' => __('View Template', 'hvac-community-events'), 'all_items' => __('All Templates', 'hvac-community-events'), 'search_items' => __('Search Templates', 'hvac-community-events'), 'parent_item_colon' => __('Parent Templates:', 'hvac-community-events'), 'not_found' => __('No templates found.', 'hvac-community-events'), 'not_found_in_trash' => __('No templates found in Trash.', 'hvac-community-events'), ); $args = array( 'labels' => $labels, 'public' => false, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_menu' => false, // We'll add to HVAC admin menu 'query_var' => false, 'rewrite' => false, 'capability_type' => array('hvac_comm_template', 'hvac_comm_templates'), 'map_meta_cap' => true, 'has_archive' => false, 'hierarchical' => false, 'menu_position' => null, 'menu_icon' => 'dashicons-email', 'supports' => array('title', 'editor', 'author', 'custom-fields'), 'show_in_rest' => true, 'rest_base' => 'hvac-comm-templates', ); register_post_type(self::POST_TYPE, $args); } /** * Register custom taxonomies */ public function register_taxonomies() { // Register Category taxonomy $category_labels = array( 'name' => _x('Template Categories', 'taxonomy general name', 'hvac-community-events'), 'singular_name' => _x('Template Category', 'taxonomy singular name', 'hvac-community-events'), 'search_items' => __('Search Categories', 'hvac-community-events'), 'all_items' => __('All Categories', 'hvac-community-events'), 'edit_item' => __('Edit Category', 'hvac-community-events'), 'update_item' => __('Update Category', 'hvac-community-events'), 'add_new_item' => __('Add New Category', 'hvac-community-events'), 'new_item_name' => __('New Category Name', 'hvac-community-events'), 'menu_name' => __('Categories', 'hvac-community-events'), ); register_taxonomy(self::TAXONOMY_CATEGORY, self::POST_TYPE, array( 'hierarchical' => true, 'labels' => $category_labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => false, 'rewrite' => false, 'show_in_rest' => true, )); // Register Channel taxonomy (email/sms) $channel_labels = array( 'name' => _x('Template Channels', 'taxonomy general name', 'hvac-community-events'), 'singular_name' => _x('Template Channel', 'taxonomy singular name', 'hvac-community-events'), 'search_items' => __('Search Channels', 'hvac-community-events'), 'all_items' => __('All Channels', 'hvac-community-events'), 'edit_item' => __('Edit Channel', 'hvac-community-events'), 'update_item' => __('Update Channel', 'hvac-community-events'), 'add_new_item' => __('Add New Channel', 'hvac-community-events'), 'new_item_name' => __('New Channel Name', 'hvac-community-events'), 'menu_name' => __('Channels', 'hvac-community-events'), ); register_taxonomy(self::TAXONOMY_CHANNEL, self::POST_TYPE, array( 'hierarchical' => false, 'labels' => $channel_labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => false, 'rewrite' => false, 'show_in_rest' => true, )); // Create default terms $this->create_default_terms(); } /** * Create default taxonomy terms */ private function create_default_terms() { // Default categories $categories = array( 'pre_event' => 'Pre-Event Communications', 'event_reminder' => 'Event Reminders', 'post_event' => 'Post-Event Follow-up', 'certificate' => 'Certificate Information', 'general' => 'General Communications' ); foreach ($categories as $slug => $name) { if (!term_exists($slug, self::TAXONOMY_CATEGORY)) { wp_insert_term($name, self::TAXONOMY_CATEGORY, array('slug' => $slug)); } } // Default channels $channels = array( 'email' => 'Email', 'sms' => 'SMS' ); foreach ($channels as $slug => $name) { if (!term_exists($slug, self::TAXONOMY_CHANNEL)) { wp_insert_term($name, self::TAXONOMY_CHANNEL, array('slug' => $slug)); } } } /** * Enqueue scripts and styles for communication templates page */ public function enqueue_scripts() { // Only enqueue on trainer communication templates page if (!$this->is_communication_templates_page()) { return; } // Enqueue CSS wp_enqueue_style( 'hvac-trainer-communication-templates', HVAC_PLUGIN_URL . 'assets/css/hvac-trainer-communication-templates.css', array(), HVAC_PLUGIN_VERSION ); // Enqueue JavaScript wp_enqueue_script( 'hvac-trainer-communication-templates', HVAC_PLUGIN_URL . 'assets/js/hvac-trainer-communication-templates.js', array('jquery'), HVAC_PLUGIN_VERSION, true ); // Localize script wp_localize_script('hvac-trainer-communication-templates', 'hvacTrainerTemplates', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('hvac_trainer_templates_nonce'), 'strings' => array( 'copy' => __('Copy to Clipboard', 'hvac-community-events'), 'copied' => __('Copied!', 'hvac-community-events'), 'copyError' => __('Copy failed. Please select and copy manually.', 'hvac-community-events'), 'loading' => __('Loading...', 'hvac-community-events'), 'noResults' => __('No templates found matching your criteria.', 'hvac-community-events'), 'showMore' => __('Show More', 'hvac-community-events'), 'showLess' => __('Show Less', 'hvac-community-events'), ) )); } /** * Check if current page is communication templates page */ private function is_communication_templates_page() { global $wp; $current_url = home_url(add_query_arg(array(), $wp->request)); return strpos($current_url, '/trainer/communication-templates') !== false || is_page('communication-templates') || is_page('trainer-communication-templates'); } /** * Get all communication templates with filtering */ public function get_templates($args = array()) { $defaults = array( 'post_type' => self::POST_TYPE, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'menu_order title', 'order' => 'ASC', 'meta_query' => array(), 'tax_query' => array(), ); $args = wp_parse_args($args, $defaults); $templates = get_posts($args); $formatted_templates = array(); foreach ($templates as $template) { $categories = wp_get_post_terms($template->ID, self::TAXONOMY_CATEGORY); $channels = wp_get_post_terms($template->ID, self::TAXONOMY_CHANNEL); $formatted_templates[] = array( 'id' => $template->ID, 'title' => $template->post_title, 'content' => $template->post_content, 'excerpt' => $template->post_excerpt, 'categories' => $categories, 'channels' => $channels, 'allowed_tokens' => get_post_meta($template->ID, '_hvac_allowed_tokens', true), 'created' => $template->post_date, 'modified' => $template->post_modified, ); } return $formatted_templates; } /** * Get templates filtered by category */ public function get_templates_by_category($category_slug = '') { $args = array(); if (!empty($category_slug)) { $args['tax_query'] = array( array( 'taxonomy' => self::TAXONOMY_CATEGORY, 'field' => 'slug', 'terms' => $category_slug, ), ); } return $this->get_templates($args); } /** * Get templates filtered by channel */ public function get_templates_by_channel($channel_slug = '') { $args = array(); if (!empty($channel_slug)) { $args['tax_query'] = array( array( 'taxonomy' => self::TAXONOMY_CHANNEL, 'field' => 'slug', 'terms' => $channel_slug, ), ); } return $this->get_templates($args); } /** * Search templates by title and content */ public function search_templates($search_term = '') { if (empty($search_term)) { return $this->get_templates(); } $args = array( 's' => sanitize_text_field($search_term), ); return $this->get_templates($args); } /** * Render the communication templates interface */ public function render_templates_interface() { // Check permissions if (!$this->user_can_view_templates()) { return '