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

Commit

Permalink
Do not output JS/CSS when not eligible
Browse files Browse the repository at this point in the history
re #161
  • Loading branch information
zedzedzed committed Feb 21, 2024
1 parent d79fd14 commit b6b0de0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Collaborate, participate, fork this plugin on [Github](https://github.com/zedzed
## Links

* [WordPress plugin page](https://wordpress.org/plugins/table-of-contents-plus/)
* [Plugin homepage](http://dublue.com/plugins/toc/)
15 changes: 12 additions & 3 deletions includes/class-toc-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public function shortcode_toc( $attributes ) {
}

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' );
}

Expand Down Expand Up @@ -450,6 +452,11 @@ public function shortcode_sitemap_posts( $attributes ) {
* Register and load 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
Expand Down Expand Up @@ -1531,9 +1538,11 @@ public function extract_headings( &$find, &$replace, $content = '' ) {
/**
* Returns true if the table of contents is eligible to be printed, false otherwise.
*/
public function is_eligible( $shortcode_used = false ) {
public function is_eligible() {
global $post;

$custom_toc_position = strpos( $post->content, '[TOC]' );

// Do not trigger the TOC on REST Requests unless explicitly enabled.
// This ensures that the TOC is not included in REST API responses by default.
// If the TOC inclusion in REST API responses is desired,
Expand All @@ -1550,7 +1559,7 @@ public function is_eligible( $shortcode_used = false ) {
}

// if the shortcode was used, this bypasses many of the global options
if ( false !== $shortcode_used ) {
if ( false !== $custom_toc_position ) {
// shortcode is used, make sure it adheres to the exclude from
// homepage option if we're on the homepage
if ( ! $this->options['include_homepage'] && is_front_page() ) {
Expand Down Expand Up @@ -1588,7 +1597,7 @@ public function the_content( $content ) {
$replace = [];
$custom_toc_position = strpos( $content, '<!--TOC-->' );

if ( $this->is_eligible( $custom_toc_position ) ) {
if ( $this->is_eligible() ) {

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

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ I will never ask for any form of reward or compensation. Helping others achieve
* Released: February 2024
* Added option to generate TOC in REST requests, disabled by default (props ballpumpe)
* Composer improvements (props mohjak)
* Do not output CSS/JS on pages not eligible
* Update POT translation file
* Update links to help

Expand Down

0 comments on commit b6b0de0

Please sign in to comment.