| 1 |
<?php |
| 2 |
|
| 3 |
namespace HT_Builder\Elementor; |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 5 |
|
| 6 |
/** |
| 7 |
* Scripts Manager |
| 8 |
*/ |
| 9 |
class HTBuilder_Scripts{ |
| 10 |
|
| 11 |
private static $instance = null; |
| 12 |
|
| 13 |
public static function instance() { |
| 14 |
if ( is_null( self::$instance ) ) { |
| 15 |
self::$instance = new self(); |
| 16 |
} |
| 17 |
return self::$instance; |
| 18 |
} |
| 19 |
|
| 20 |
function __construct(){ |
| 21 |
$this->init(); |
| 22 |
} |
| 23 |
|
| 24 |
public function init() { |
| 25 |
|
| 26 |
// Register Scripts |
| 27 |
add_action( 'init', [ $this, 'register_scripts' ] ); |
| 28 |
|
| 29 |
// Frontend Scripts |
| 30 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_frontend_scripts' ] ); |
| 31 |
|
| 32 |
// Editor Scripts |
| 33 |
add_action( 'elementor/editor/before_enqueue_scripts', [$this, 'enqueue_editor_scripts'] ); |
| 34 |
|
| 35 |
// Elementor only generates atomic-widget CSS for the main queried post; register |
| 36 |
// this page's header/footer/single/archive templates before that one-shot pass |
| 37 |
// so their atomic styles aren't skipped. |
| 38 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_atomic_styles_for_templates' ], 15 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register the Elementor template IDs used to render the current page's |
| 43 |
* header, footer, and single/archive override with Elementor's atomic-widget |
| 44 |
* CSS pipeline before its own single per-request enqueue pass runs |
| 45 |
* (Frontend::enqueue_styles(), hooked at wp_enqueue_scripts priority 20). |
| 46 |
* That pass only registers is_singular()'s queried post, so a template |
| 47 |
* rendered via HT Builder never gets its atomic CSS generated unless we |
| 48 |
* register it here first. |
| 49 |
*/ |
| 50 |
public function enqueue_atomic_styles_for_templates() { |
| 51 |
if ( is_admin() || ! class_exists( '\Elementor\Plugin' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$template_ids = $this->get_active_template_ids(); |
| 56 |
if ( empty( $template_ids ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $template_ids as $template_id ) { |
| 61 |
do_action( 'elementor/post/render', $template_id ); |
| 62 |
} |
| 63 |
|
| 64 |
\Elementor\Plugin::instance()->frontend->enqueue_styles(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Elementor template IDs HT Builder will render on the current request. |
| 69 |
*/ |
| 70 |
private function get_active_template_ids() { |
| 71 |
$template_ids = []; |
| 72 |
|
| 73 |
if ( class_exists( '\HT_Builder\Elementor\HeaderFooter\HTBuilder_Header_Footer' ) ) { |
| 74 |
$header_footer = \HT_Builder\Elementor\HeaderFooter\HTBuilder_Header_Footer::instance(); |
| 75 |
|
| 76 |
if ( ! empty( $header_footer->header_id ) ) { |
| 77 |
$template_ids[] = $header_footer->header_id; |
| 78 |
} |
| 79 |
if ( ! empty( $header_footer->footer_id ) ) { |
| 80 |
$template_ids[] = $header_footer->footer_id; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( class_exists( '\HT_Builder\Elementor\HTBuilder_Custom_Template_Layout' ) ) { |
| 85 |
$template_layout = \HT_Builder\Elementor\HTBuilder_Custom_Template_Layout::instance(); |
| 86 |
|
| 87 |
if ( is_singular( 'post' ) ) { |
| 88 |
$single_tm_id = $template_layout->custom_template_id( 'single_blog_page' ); |
| 89 |
if ( ! empty( $single_tm_id ) ) { |
| 90 |
$template_ids[] = $single_tm_id; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if ( is_post_type_archive( 'post' ) || htbuilder_is_blog_page() ) { |
| 95 |
$archive_tm_id = $template_layout->custom_template_id( 'archive_blog_page' ); |
| 96 |
if ( ! empty( $archive_tm_id ) ) { |
| 97 |
$template_ids[] = $archive_tm_id; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return array_unique( array_map( 'absint', $template_ids ) ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Register Scripts |
| 107 |
*/ |
| 108 |
|
| 109 |
public function register_scripts(){ |
| 110 |
wp_register_script( |
| 111 |
'goodshare', |
| 112 |
HTBUILDER_PL_URL . 'assets/js/goodshare.min.js', |
| 113 |
array('jquery'), |
| 114 |
HTBUILDER_VERSION, |
| 115 |
TRUE |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Enqueue frontend scripts |
| 121 |
*/ |
| 122 |
public function enqueue_frontend_scripts() { |
| 123 |
|
| 124 |
// CSS |
| 125 |
wp_enqueue_style( |
| 126 |
'htbuilder-main', |
| 127 |
HTBUILDER_PL_URL . 'assets/css/htbuilder.css', |
| 128 |
NULL, |
| 129 |
HTBUILDER_VERSION |
| 130 |
); |
| 131 |
|
| 132 |
// JS |
| 133 |
wp_enqueue_script( 'masonry' ); |
| 134 |
wp_enqueue_script( |
| 135 |
'htbuilder-main', |
| 136 |
HTBUILDER_PL_URL . 'assets/js/htbuilder.js', |
| 137 |
array('jquery'), |
| 138 |
HTBUILDER_VERSION, |
| 139 |
TRUE |
| 140 |
); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Enqueue Editor scripts |
| 146 |
*/ |
| 147 |
public function enqueue_editor_scripts(){} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
HTBuilder_Scripts::instance(); |