array( 'shortcode' => '[hvac_master_trainers]', 'class' => 'HVAC_Master_Trainers_Overview', 'method' => 'render_trainers_overview' ), 'events-overview' => array( 'shortcode' => '[hvac_master_events]', 'class' => 'HVAC_Master_Events_Overview', 'method' => 'render_events_overview' ), 'pending-approvals' => array( 'shortcode' => '[hvac_pending_approvals]', 'class' => 'HVAC_Master_Pending_Approvals', 'method' => 'render_pending_approvals' ), 'announcements' => array( 'shortcode' => '[hvac_announcements_timeline]', 'class' => 'HVAC_Announcements_Display', 'method' => 'render_timeline_shortcode' ) ); /** * Get instance */ public static function instance() { if (null === self::$instance) { self::$instance = new self(); } return self::$instance; } /** * Constructor */ private function __construct() { add_filter('the_content', array($this, 'inject_master_content'), 10); add_action('wp_head', array($this, 'inject_inline_content'), 1); } /** * Inject content into master trainer pages via the_content filter * * @param string $content The existing content * @return string Modified content */ public function inject_master_content($content) { // Only run on master trainer pages if (!$this->is_master_trainer_page()) { return $content; } // If content is already present and not just a shortcode, return it if (!empty(trim(strip_tags($content))) && !$this->contains_only_shortcode($content)) { return $content; } // Get the appropriate shortcode for this page $shortcode_content = $this->get_page_shortcode_content(); if (!empty($shortcode_content)) { return $shortcode_content; } return $content; } /** * Inject content via inline JavaScript as last resort */ public function inject_inline_content() { if (!$this->is_master_trainer_page()) { return; } ?> ID); if (strpos($template, 'page-master-') === 0) { return true; } // Check page slug $slug = $post->post_name; $master_slugs = array('all-trainers', 'events-overview', 'pending-approvals', 'announcements'); return in_array($slug, $master_slugs); } /** * Check if content contains only a shortcode * * @param string $content * @return bool */ private function contains_only_shortcode($content) { $content = trim($content); return preg_match('/^\[[\w_-]+[^\]]*\]$/', $content); } /** * Get appropriate shortcode content for current page * * @return string */ private function get_page_shortcode_content() { global $post; if (!$post) { return ''; } // Map page slug to shortcode data $page_slug = $post->post_name; $shortcode_data = null; foreach ($this->master_pages as $key => $data) { if (strpos($page_slug, str_replace('-', '-', $key)) !== false) { $shortcode_data = $data; break; } } if (!$shortcode_data) { return ''; } // Try direct method call first if (class_exists($shortcode_data['class'])) { $instance = call_user_func(array($shortcode_data['class'], 'instance')); if (!$instance && method_exists($shortcode_data['class'], 'get_instance')) { $instance = call_user_func(array($shortcode_data['class'], 'get_instance')); } if ($instance && method_exists($instance, $shortcode_data['method'])) { ob_start(); echo $instance->{$shortcode_data['method']}(); return ob_get_clean(); } } // Fall back to shortcode return do_shortcode($shortcode_data['shortcode']); } } // Initialize the content injector HVAC_Master_Content_Injector::instance();