Easy Table of Contents - Version 2.0.14

Version Description

Requires WordPress >

Download this release

Release Info

Developer shazahm1@hotmail.com
Plugin Icon 128x128 Easy Table of Contents
Version 2.0.14
Comparing to
See all releases

Code changes from version 2.0.13 to 2.0.14

Files changed (3) hide show
  1. README.txt +9 -1
  2. easy-table-of-contents.php +49 -24
  3. includes/Debug.php +80 -11
README.txt CHANGED
@@ -5,7 +5,7 @@ Tags: table of contents, toc
5
  Requires at least: 5.3
6
  Tested up to: 5.6
7
  Requires PHP: 5.6.20
8
- Stable tag: 2.0.13
9
  License: GPLv2 or later
10
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -89,6 +89,11 @@ Easy Table Contents is a fork of the excellent [Table of Contents Plus](https://
89
 
90
  == Changelog ==
91
 
 
 
 
 
 
92
  = 2.0.13 01/25/2021 =
93
  * TWEAK: Restrict debug logging to when `WP_DEBUG` is enabled *and* current user capability of `manage_options`.
94
  * TWEAK: Add logging to aid in support.
@@ -438,3 +443,6 @@ Requires WordPress >= 5.3 and PHP version >= 5.6.20 (>= 7.4 is recommended).
438
 
439
  = 2.0.13 =
440
  Requires WordPress >= 5.3 and PHP version >= 5.6.20 (>= 7.4 is recommended).
 
 
 
5
  Requires at least: 5.3
6
  Tested up to: 5.6
7
  Requires PHP: 5.6.20
8
+ Stable tag: 2.0.14
9
  License: GPLv2 or later
10
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
11
 
89
 
90
  == Changelog ==
91
 
92
+ = 2.0.14 01/26/2021 =
93
+ * TWEAK: Refactor debug log as a Singleton.
94
+ * TWEAK: Add additional logging to aid in debugging.
95
+ * BUG: Correct logic for PHP where empty string no longer evaluates as integer `0`.
96
+
97
  = 2.0.13 01/25/2021 =
98
  * TWEAK: Restrict debug logging to when `WP_DEBUG` is enabled *and* current user capability of `manage_options`.
99
  * TWEAK: Add logging to aid in support.
443
 
444
  = 2.0.13 =
445
  Requires WordPress >= 5.3 and PHP version >= 5.6.20 (>= 7.4 is recommended).
446
+
447
+ = 2.0.14 =
448
+ Requires WordPress >= 5.3 and PHP version >= 5.6.20 (>= 7.4 is recommended).
easy-table-of-contents.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Easy Table of Contents
4
  * Plugin URI: http://connections-pro.com/
5
  * Description: Adds a user friendly and fully automatic way to create and display a table of contents generated from the page content.
6
- * Version: 2.0.13
7
  * Author: Steven A. Zahm
8
  * Author URI: http://connections-pro.com/
9
  * Text Domain: easy-table-of-contents
@@ -26,7 +26,7 @@
26
  * @package Easy Table of Contents
27
  * @category Plugin
28
  * @author Steven A. Zahm
29
- * @version 2.0.13
30
  */
31
 
32
  use Easy_Plugins\Table_Of_Contents\Debug;
@@ -48,7 +48,7 @@ if ( ! class_exists( 'ezTOC' ) ) {
48
  * @since 1.0
49
  * @var string
50
  */
51
- const VERSION = '2.0.13';
52
 
53
  /**
54
  * Stores the instance of this class.
@@ -378,6 +378,8 @@ if ( ! class_exists( 'ezTOC' ) ) {
378
  //global $wp_current_filter;
379
 
380
  if ( empty( $post ) || ! $post instanceof WP_Post ) {
 
 
381
  return false;
382
  }
383
 
@@ -390,18 +392,27 @@ if ( ! class_exists( 'ezTOC' ) ) {
390
 
391
  if ( has_shortcode( $post->post_content, apply_filters( 'ez_toc_shortcode', 'toc' ) ) ||
392
  has_shortcode( $post->post_content, 'ez-toc' ) ) {
 
 
393
  return true;
394
  }
395
 
396
  if ( is_front_page() && ! ezTOC_Option::get( 'include_homepage' ) ) {
 
 
397
  return false;
398
  }
399
 
400
  $type = get_post_type( $post->ID );
401
 
 
 
402
  $enabled = in_array( $type, ezTOC_Option::get( 'enabled_post_types', array() ), true );
403
  $insert = in_array( $type, ezTOC_Option::get( 'auto_insert_post_types', array() ), true );
404
 
 
 
 
405
  if ( $insert || $enabled ) {
406
 
407
  if ( ezTOC_Option::get( 'restrict_path' ) ) {
@@ -411,33 +422,45 @@ if ( ! class_exists( 'ezTOC' ) ) {
411
  */
412
  if ( false !== strpos( ezTOC_Option::get( 'restrict_path' ), $_SERVER['REQUEST_URI'] ) ) {
413
 
 
414
  return false;
415
 
416
  } else {
417
 
 
418
  return true;
419
  }
420
 
421
  } else {
422
 
423
- if ( $insert && 1 == get_post_meta( $post->ID, '_ez-toc-disabled', true ) ) {
424
 
 
425
  return false;
426
 
427
- } elseif ( $insert && 0 == get_post_meta( $post->ID, '_ez-toc-disabled', true ) ) {
428
 
 
429
  return true;
430
 
431
- } elseif ( $enabled && 1 == get_post_meta( $post->ID, '_ez-toc-insert', true ) ) {
432
 
 
 
 
 
 
 
433
  return true;
434
  }
435
 
 
436
  return false;
437
  }
438
 
439
  } else {
440
 
 
441
  return false;
442
  }
443
  }
@@ -493,9 +516,13 @@ if ( ! class_exists( 'ezTOC' ) ) {
493
 
494
  if ( $run ) {
495
 
496
- if ( is_null( $post = self::get( get_the_ID() ) ) ) {
 
 
497
 
498
- return $content;
 
 
499
  }
500
 
501
  $html = $post->getTOC();
@@ -556,40 +583,38 @@ if ( ! class_exists( 'ezTOC' ) ) {
556
  */
557
  public static function the_content( $content ) {
558
 
559
- $log = new Debug();
560
-
561
  $maybeApplyFilter = self::maybeApplyTheContentFilter();
562
 
563
- $log->add( 'the_content_filter', 'The `the_content` filter applied.', $maybeApplyFilter );
564
 
565
  if ( ! $maybeApplyFilter ) {
566
 
567
- return $log->appendTo( $content );
568
  }
569
 
570
  // Bail if post not eligible and widget is not active.
571
  $isEligible = self::is_eligible( get_post() );
572
 
573
- $log->add( 'post_eligible', 'Post eligible.', $isEligible );
574
 
575
  if ( ! $isEligible && ! is_active_widget( false, false, 'ezw_tco' ) ) {
576
 
577
- return $log->appendTo( $content );
578
  }
579
 
580
  $post = self::get( get_the_ID() );
581
 
582
  if ( ! $post instanceof ezTOC_Post ) {
583
 
584
- $log->add( 'not_instance_of_post', 'Not an instance if `WP_Post`.', get_the_ID() );
585
 
586
- return $log->appendTo( $content );
587
  }
588
 
589
  // Bail if no headings found.
590
  if ( ! $post->hasTOCItems() ) {
591
 
592
- return $content;
593
  }
594
 
595
  $find = $post->getHeadings();
@@ -604,13 +629,13 @@ if ( ! class_exists( 'ezTOC' ) ) {
604
 
605
  $style = "background-image: linear-gradient(#F1F1F1 50%, #F9F9F9 50%); background-size: 100% 4em; border: 1px solid #CCC; font-family: monospace; font-size: 1em; line-height: 2em; margin: 0 auto; overflow: auto; padding: 0 8px 4px; white-space: nowrap; width: 100%;";
606
 
607
- $log->add(
608
  'found_post_headings',
609
  'Found headings:',
610
  "<textarea rows='{$headingRows}' style='{$style}' wrap='soft'>{$headings}</textarea>"
611
  );
612
 
613
- $log->add(
614
  'replace_post_headings',
615
  'Replace found headings with:',
616
  "<textarea rows='{$anchorRows}' style='{$style}' wrap='soft'>{$anchors}</textarea>"
@@ -619,14 +644,14 @@ if ( ! class_exists( 'ezTOC' ) ) {
619
  // If shortcode used or post not eligible, return content with anchored headings.
620
  if ( strpos( $content, 'ez-toc-container' ) || ! $isEligible ) {
621
 
622
- $log->add( 'shortcode_found', 'Shortcode found, add links to content.', true );
623
 
624
  return mb_find_replace( $find, $replace, $content );
625
  }
626
 
627
  $position = ezTOC_Option::get( 'position' );
628
 
629
- $log->add( 'toc_insert_position', 'Insert TOC at position', $position );
630
 
631
  // else also add toc to content
632
  switch ( $position ) {
@@ -665,14 +690,14 @@ if ( ! class_exists( 'ezTOC' ) ) {
665
  */
666
  if ( 1 === $result ) {
667
 
668
- $log->add( 'toc_insert_position_found', 'Insert TOC before first eligible heading.', $result );
669
 
670
  $start = strpos( $content, $matches[0] );
671
  $content = substr_replace( $content, $toc, $start, 0 );
672
 
673
  } else {
674
 
675
- $log->add( 'toc_insert_position_not_found', 'Insert TOC before first eligible heading not found.', $result );
676
 
677
  // Somehow, there are scenarios where the processing get this far and
678
  // the TOC is being added to pages where it should not. Disable for now.
@@ -680,7 +705,7 @@ if ( ! class_exists( 'ezTOC' ) ) {
680
  }
681
  }
682
 
683
- return $log->appendTo( $content );
684
  }
685
 
686
  }
3
  * Plugin Name: Easy Table of Contents
4
  * Plugin URI: http://connections-pro.com/
5
  * Description: Adds a user friendly and fully automatic way to create and display a table of contents generated from the page content.
6
+ * Version: 2.0.14
7
  * Author: Steven A. Zahm
8
  * Author URI: http://connections-pro.com/
9
  * Text Domain: easy-table-of-contents
26
  * @package Easy Table of Contents
27
  * @category Plugin
28
  * @author Steven A. Zahm
29
+ * @version 2.0.14
30
  */
31
 
32
  use Easy_Plugins\Table_Of_Contents\Debug;
48
  * @since 1.0
49
  * @var string
50
  */
51
+ const VERSION = '2.0.14';
52
 
53
  /**
54
  * Stores the instance of this class.
378
  //global $wp_current_filter;
379
 
380
  if ( empty( $post ) || ! $post instanceof WP_Post ) {
381
+
382
+ Debug::log( 'not_instance_of_post', 'Not an instance if `WP_Post`.', $post );
383
  return false;
384
  }
385
 
392
 
393
  if ( has_shortcode( $post->post_content, apply_filters( 'ez_toc_shortcode', 'toc' ) ) ||
394
  has_shortcode( $post->post_content, 'ez-toc' ) ) {
395
+
396
+ Debug::log( 'has_ez_toc_shortcode', 'Has instance of shortcode.', true );
397
  return true;
398
  }
399
 
400
  if ( is_front_page() && ! ezTOC_Option::get( 'include_homepage' ) ) {
401
+
402
+ Debug::log( 'is_front_page', 'Is frontpage, TOC is not enabled.', false );
403
  return false;
404
  }
405
 
406
  $type = get_post_type( $post->ID );
407
 
408
+ Debug::log( 'current_post_type', 'Post type is.', $type );
409
+
410
  $enabled = in_array( $type, ezTOC_Option::get( 'enabled_post_types', array() ), true );
411
  $insert = in_array( $type, ezTOC_Option::get( 'auto_insert_post_types', array() ), true );
412
 
413
+ Debug::log( 'is_supported_post_type', 'Is supported post type?', $enabled );
414
+ Debug::log( 'is_auto_insert_post_type', 'Is auto insert for post types?', $insert );
415
+
416
  if ( $insert || $enabled ) {
417
 
418
  if ( ezTOC_Option::get( 'restrict_path' ) ) {
422
  */
423
  if ( false !== strpos( ezTOC_Option::get( 'restrict_path' ), $_SERVER['REQUEST_URI'] ) ) {
424
 
425
+ Debug::log( 'is_restricted_path', 'In restricted path, post not eligible.', ezTOC_Option::get( 'restrict_path' ) );
426
  return false;
427
 
428
  } else {
429
 
430
+ Debug::log( 'is_not_restricted_path', 'Not in restricted path, post is eligible.', ezTOC_Option::get( 'restrict_path' ) );
431
  return true;
432
  }
433
 
434
  } else {
435
 
436
+ if ( $insert && 1 === (int) get_post_meta( $post->ID, '_ez-toc-disabled', true ) ) {
437
 
438
+ Debug::log( 'is_auto_insert_disable_post_meta', 'Auto insert enabled and disable TOC is enabled in post meta.', false );
439
  return false;
440
 
441
+ } elseif ( $insert && 0 === (int) get_post_meta( $post->ID, '_ez-toc-disabled', true ) ) {
442
 
443
+ Debug::log( 'is_auto_insert_enabled_post_meta', 'Auto insert enabled and disable TOC is not enabled in post meta.', true );
444
  return true;
445
 
446
+ } elseif ( $enabled && 1 === (int) get_post_meta( $post->ID, '_ez-toc-insert', true ) ) {
447
 
448
+ Debug::log( 'is_supported_post_type_disable_insert_post_meta', 'Supported post type and insert TOC is enabled in post meta.', true );
449
+ return true;
450
+
451
+ } elseif ( $enabled && $insert ) {
452
+
453
+ Debug::log( 'supported_post_type_and_auto_insert', 'Supported post type and auto insert TOC is enabled.', true );
454
  return true;
455
  }
456
 
457
+ Debug::log( 'not_auto_insert_or_not_supported_post_type', 'Not supported post type or insert TOC is disabled.', false );
458
  return false;
459
  }
460
 
461
  } else {
462
 
463
+ Debug::log( 'not_auto_insert_and_not_supported post_type', 'Not supported post type and do not auto insert TOC.', false );
464
  return false;
465
  }
466
  }
516
 
517
  if ( $run ) {
518
 
519
+ $post = self::get( get_the_ID() );
520
+
521
+ if ( ! $post instanceof ezTOC_Post ) {
522
 
523
+ Debug::log( 'not_instance_of_post', 'Not an instance if `WP_Post`.', get_the_ID() );
524
+
525
+ return Debug::log()->appendTo( $content );
526
  }
527
 
528
  $html = $post->getTOC();
583
  */
584
  public static function the_content( $content ) {
585
 
 
 
586
  $maybeApplyFilter = self::maybeApplyTheContentFilter();
587
 
588
+ Debug::log( 'the_content_filter', 'The `the_content` filter applied.', $maybeApplyFilter );
589
 
590
  if ( ! $maybeApplyFilter ) {
591
 
592
+ return Debug::log()->appendTo( $content );
593
  }
594
 
595
  // Bail if post not eligible and widget is not active.
596
  $isEligible = self::is_eligible( get_post() );
597
 
598
+ Debug::log( 'post_eligible', 'Post eligible.', $isEligible );
599
 
600
  if ( ! $isEligible && ! is_active_widget( false, false, 'ezw_tco' ) ) {
601
 
602
+ return Debug::log()->appendTo( $content );
603
  }
604
 
605
  $post = self::get( get_the_ID() );
606
 
607
  if ( ! $post instanceof ezTOC_Post ) {
608
 
609
+ Debug::log( 'not_instance_of_post', 'Not an instance if `WP_Post`.', get_the_ID() );
610
 
611
+ return Debug::log()->appendTo( $content );
612
  }
613
 
614
  // Bail if no headings found.
615
  if ( ! $post->hasTOCItems() ) {
616
 
617
+ return Debug::log()->appendTo( $content );
618
  }
619
 
620
  $find = $post->getHeadings();
629
 
630
  $style = "background-image: linear-gradient(#F1F1F1 50%, #F9F9F9 50%); background-size: 100% 4em; border: 1px solid #CCC; font-family: monospace; font-size: 1em; line-height: 2em; margin: 0 auto; overflow: auto; padding: 0 8px 4px; white-space: nowrap; width: 100%;";
631
 
632
+ Debug::log(
633
  'found_post_headings',
634
  'Found headings:',
635
  "<textarea rows='{$headingRows}' style='{$style}' wrap='soft'>{$headings}</textarea>"
636
  );
637
 
638
+ Debug::log(
639
  'replace_post_headings',
640
  'Replace found headings with:',
641
  "<textarea rows='{$anchorRows}' style='{$style}' wrap='soft'>{$anchors}</textarea>"
644
  // If shortcode used or post not eligible, return content with anchored headings.
645
  if ( strpos( $content, 'ez-toc-container' ) || ! $isEligible ) {
646
 
647
+ Debug::log( 'shortcode_found', 'Shortcode found, add links to content.', true );
648
 
649
  return mb_find_replace( $find, $replace, $content );
650
  }
651
 
652
  $position = ezTOC_Option::get( 'position' );
653
 
654
+ Debug::log( 'toc_insert_position', 'Insert TOC at position', $position );
655
 
656
  // else also add toc to content
657
  switch ( $position ) {
690
  */
691
  if ( 1 === $result ) {
692
 
693
+ Debug::log( 'toc_insert_position_found', 'Insert TOC before first eligible heading.', $result );
694
 
695
  $start = strpos( $content, $matches[0] );
696
  $content = substr_replace( $content, $toc, $start, 0 );
697
 
698
  } else {
699
 
700
+ Debug::log( 'toc_insert_position_not_found', 'Insert TOC before first eligible heading not found.', $result );
701
 
702
  // Somehow, there are scenarios where the processing get this far and
703
  // the TOC is being added to pages where it should not. Disable for now.
705
  }
706
  }
707
 
708
+ return Debug::log()->appendTo( $content );
709
  }
710
 
711
  }
includes/Debug.php CHANGED
@@ -23,6 +23,11 @@ final class Debug extends WP_Error {
23
  */
24
  protected $enabled = false;
25
 
 
 
 
 
 
26
  /**
27
  * Debug constructor.
28
  *
@@ -34,15 +39,76 @@ final class Debug extends WP_Error {
34
  */
35
  public function __construct( $code = '', $message = '', $data = '' ) {
36
 
37
- $this->display = defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;
38
- $this->enabled = apply_filters(
39
- 'Easy_Plugins/Table_Of_Contents/Debug/Enabled',
40
- ( defined( 'WP_DEBUG' ) && WP_DEBUG ) && current_user_can( 'manage_options' )
41
- );
42
-
43
  parent::__construct( $code, $message, $data );
44
  }
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  /**
47
  * @since 2.0.13
48
  *
@@ -86,7 +152,12 @@ final class Debug extends WP_Error {
86
  */
87
  public function __toString() {
88
 
89
- if ( ! $this->enabled ) {
 
 
 
 
 
90
 
91
  return '';
92
  }
@@ -102,10 +173,8 @@ final class Debug extends WP_Error {
102
  );
103
 
104
  $intro = PHP_EOL . "<p>{$intro}</p>" .PHP_EOL;
 
105
 
106
- $display = $this->display ? 'block' : 'none';
107
- $dump = $this->dump();
108
-
109
- return PHP_EOL . "<div class='ez-toc-debug-messages' style='display: block;'>{$intro}{$dump}</div>" . PHP_EOL;
110
  }
111
  }
23
  */
24
  protected $enabled = false;
25
 
26
+ /**
27
+ * @var self
28
+ */
29
+ private static $instance;
30
+
31
  /**
32
  * Debug constructor.
33
  *
39
  */
40
  public function __construct( $code = '', $message = '', $data = '' ) {
41
 
 
 
 
 
 
 
42
  parent::__construct( $code, $message, $data );
43
  }
44
 
45
+ /**
46
+ * @since 2.0.14
47
+ *
48
+ * @param string $code
49
+ * @param string $message
50
+ * @param string $data
51
+ *
52
+ * @return Debug
53
+ */
54
+ public static function log( $code = '', $message = '', $data = '' ) {
55
+
56
+ if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
57
+
58
+ self::$instance = new self( $code, $message, $data );
59
+
60
+ self::$instance->display = apply_filters(
61
+ 'Easy_Plugins/Table_Of_Contents/Debug/Display',
62
+ defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY
63
+ );
64
+
65
+ self::$instance->enabled = apply_filters(
66
+ 'Easy_Plugins/Table_Of_Contents/Debug/Enabled',
67
+ ( defined( 'WP_DEBUG' ) && WP_DEBUG ) && current_user_can( 'manage_options' )
68
+ );
69
+
70
+ } else {
71
+
72
+ if ( ! empty( $code ) && ! empty( $message ) ) {
73
+
74
+ self::$instance->add( $code, $message, $data );
75
+ }
76
+ }
77
+
78
+ return self::$instance;
79
+ }
80
+
81
+ /**
82
+ * Adds an error or appends an additional message to an existing error.
83
+ *
84
+ * NOTE: Overrides WP_Error::add() to allow support of passing `false` as `$data`.
85
+ *
86
+ * @since 2.0.14
87
+ *
88
+ * @param string|int $code Error code.
89
+ * @param string $message Error message.
90
+ * @param mixed $data Optional. Error data.
91
+ */
92
+ public function add( $code, $message, $data = null ) {
93
+ $this->errors[ $code ][] = $message;
94
+
95
+ if ( ! is_null( $data ) ) {
96
+ $this->add_data( $data, $code );
97
+ }
98
+
99
+ /**
100
+ * Fires when an error is added to a WP_Error object.
101
+ *
102
+ * @since 5.6.0
103
+ *
104
+ * @param string|int $code Error code.
105
+ * @param string $message Error message.
106
+ * @param mixed $data Error data. Might be empty.
107
+ * @param WP_Error $wp_error The WP_Error object.
108
+ */
109
+ do_action( 'wp_error_added', $code, $message, $data, $this );
110
+ }
111
+
112
  /**
113
  * @since 2.0.13
114
  *
152
  */
153
  public function __toString() {
154
 
155
+ if ( false === $this->enabled ) {
156
+
157
+ return '';
158
+ }
159
+
160
+ if ( false === $this->display ) {
161
 
162
  return '';
163
  }
173
  );
174
 
175
  $intro = PHP_EOL . "<p>{$intro}</p>" .PHP_EOL;
176
+ $dump = $this->dump();
177
 
178
+ return PHP_EOL . "<div class='ez-toc-debug-messages'>{$intro}{$dump}</div>" . PHP_EOL;
 
 
 
179
  }
180
  }