Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Redo wp_enqueue_scripts logic so it triggers at the right time
Browse files Browse the repository at this point in the history
Re #154
  • Loading branch information
zedzedzed committed Feb 21, 2024
1 parent 69dde00 commit 22c36b4
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions includes/class-toc-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ public function shortcode_toc( $attributes ) {
$attributes
);

$re_enqueue_scripts = false;

if ( $atts['no_label'] ) {
$this->options['show_heading_text'] = false;
}
Expand All @@ -179,11 +177,9 @@ public function shortcode_toc( $attributes ) {
}
if ( $atts['label_show'] ) {
$this->options['visibility_show'] = wp_kses_post( html_entity_decode( $atts['label_show'] ) );
$re_enqueue_scripts = true;
}
if ( $atts['label_hide'] ) {
$this->options['visibility_hide'] = wp_kses_post( html_entity_decode( $atts['label_hide'] ) );
$re_enqueue_scripts = true;
}
if ( $atts['class'] ) {
$this->options['css_container_class'] = wp_kses_post( html_entity_decode( $atts['class'] ) );
Expand All @@ -208,7 +204,6 @@ public function shortcode_toc( $attributes ) {
}
if ( $atts['collapse'] ) {
$this->options['visibility_hide_by_default'] = true;
$re_enqueue_scripts = true;
}

if ( $atts['no_numbers'] ) {
Expand All @@ -219,12 +214,6 @@ public function shortcode_toc( $attributes ) {
$this->options['start'] = $atts['start'];
}

if ( $re_enqueue_scripts ) {
// the shortcode has provided potentially new labels and we need to
// supply these new values to js_vars by enqueing scripts again
do_action( 'wp_enqueue_scripts' );
}

// if $atts['heading_levels'] is an array, then it came from the global options
// and wasn't provided by per instance
if ( $atts['heading_levels'] && ! is_array( $atts['heading_levels'] ) ) {
Expand Down Expand Up @@ -449,19 +438,25 @@ public function shortcode_sitemap_posts( $attributes ) {


/**
* Register and load CSS and javascript files for frontend.
* Register CSS and javascript files for frontend.
*/
public function wp_enqueue_scripts() {
// Do not output CSS / JS if the TOC is not going to be displayed on the current page
if ( ! $this->is_eligible() ) {
return;
}

$js_vars = [];

// register our CSS and scripts
wp_register_style( 'toc-screen', TOC_PLUGIN_PATH . '/screen.min.css', [], TOC_VERSION );
wp_register_script( 'toc-front', TOC_PLUGIN_PATH . '/front.min.js', [ 'jquery' ], TOC_VERSION, true );
}


/**
* Output CSS and JS files for frontend.
*/
private function do_wp_enqueue_scripts() {
$js_vars = [];

// enqueue them!
if ( ! $this->options['exclude_css'] ) {
Expand Down Expand Up @@ -1599,6 +1594,8 @@ public function the_content( $content ) {

if ( $this->is_eligible() ) {

$this->do_wp_enqueue_scripts();

$items = $this->extract_headings( $find, $replace, $content );

if ( $items ) {
Expand Down

0 comments on commit 22c36b4

Please sign in to comment.