Easy Table of Contents - Version 2.0.13

Version Description

Requires WordPress >

Download this release

Release Info

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

Code changes from version 2.0.12 to 2.0.13

Files changed (3) hide show
  1. README.txt +10 -2
  2. easy-table-of-contents.php +61 -36
  3. includes/Debug.php +111 -0
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.12
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.12 01/22/2021 =
93
  * TWEAK: Allow `_` and `-` in anchors.
94
  * TWEAK: Minor CSS tweaks that prevent theme from breaking the layout.
@@ -428,5 +433,8 @@ Requires WordPress >= 5.0 and PHP version >= 5.6.20 (>= 7.1 is recommended).
428
  = 2.0.11 =
429
  Requires WordPress >= 5.0 and PHP version >= 5.6.20 (>= 7.1 is recommended).
430
 
431
- = 2.0.11 =
 
 
 
432
  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.13
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.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.
95
+ * DEV: phpDoc update.
96
+
97
  = 2.0.12 01/22/2021 =
98
  * TWEAK: Allow `_` and `-` in anchors.
99
  * TWEAK: Minor CSS tweaks that prevent theme from breaking the layout.
433
  = 2.0.11 =
434
  Requires WordPress >= 5.0 and PHP version >= 5.6.20 (>= 7.1 is recommended).
435
 
436
+ = 2.0.12 =
437
+ 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).
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.12
7
  * Author: Steven A. Zahm
8
  * Author URI: http://connections-pro.com/
9
  * Text Domain: easy-table-of-contents
@@ -26,9 +26,10 @@
26
  * @package Easy Table of Contents
27
  * @category Plugin
28
  * @author Steven A. Zahm
29
- * @version 2.0.12
30
  */
31
 
 
32
  use function Easy_Plugins\Table_Of_Contents\String\mb_find_replace;
33
 
34
  // Exit if accessed directly
@@ -47,7 +48,7 @@ if ( ! class_exists( 'ezTOC' ) ) {
47
  * @since 1.0
48
  * @var string
49
  */
50
- const VERSION = '2.0.12';
51
 
52
  /**
53
  * Stores the instance of this class.
@@ -131,6 +132,7 @@ if ( ! class_exists( 'ezTOC' ) ) {
131
 
132
  require_once( EZ_TOC_PATH . '/includes/class.post.php' );
133
  require_once( EZ_TOC_PATH . '/includes/class.widget-toc.php' );
 
134
  require_once( EZ_TOC_PATH . '/includes/inc.functions.php' );
135
  require_once( EZ_TOC_PATH . '/includes/inc.string-functions.php' );
136
 
@@ -546,9 +548,7 @@ if ( ! class_exists( 'ezTOC' ) ) {
546
  * This will add the inline table of contents page anchors to the post content. It will also insert the
547
  * table of contents inline with the post content as defined by the user defined preference.
548
  *
549
- * @access private
550
- * @since 1.0
551
- * @static
552
  *
553
  * @param string $content
554
  *
@@ -556,70 +556,91 @@ if ( ! class_exists( 'ezTOC' ) ) {
556
  */
557
  public static function the_content( $content ) {
558
 
559
- if ( ! self::maybeApplyTheContentFilter() ) {
560
 
561
- return $content;
 
 
 
 
 
 
562
  }
563
 
564
- // bail if post not eligible and widget is not active
565
- $is_eligible = self::is_eligible( get_post() );
566
 
567
- if ( ! $is_eligible && ! is_active_widget( false, false, 'ezw_tco' ) ) {
568
 
569
- return $content;
 
 
570
  }
571
 
572
- if ( is_null( $post = self::get( get_the_ID() ) ) ) {
573
 
574
- return $content;
 
 
 
 
575
  }
576
 
577
- // bail if no headings found
578
  if ( ! $post->hasTOCItems() ) {
579
 
580
  return $content;
581
  }
582
 
583
- $debug = '';
584
  $find = $post->getHeadings();
585
  $replace = $post->getHeadingsWithAnchors();
586
- $html = $post->getTOC();
587
 
588
- if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
 
589
 
590
- $headings = implode( PHP_EOL, $find );
591
- $anchors = implode( PHP_EOL, $replace );
592
 
593
- $headingRows = count( $find ) + 1;
594
- $anchorRows = count( $replace ) + 1;
595
 
596
- $displayDebug = defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ? 'block' : 'none';
 
 
 
 
597
 
598
- $style = "background-image: linear-gradient(#F1F1F1 50%, #F9F9F9 50%); background-size: 100% 4em; border: 1px solid #CCC; display: {$displayDebug}; font-family: monospace; font-size: 1em; line-height: 2em; margin: 0 auto; overflow: auto; padding: 0 8px 4px; white-space: nowrap; width: 100%;";
 
 
 
 
599
 
600
- $debug .= "<textarea rows='{$headingRows}' style='{$style}' wrap='soft'>{$headings}</textarea>";
601
- $debug .= "<textarea rows='{$anchorRows}' style='{$style}' wrap='soft'>{$anchors}</textarea>";
602
- }
603
 
604
- // if shortcode used or post not eligible, return content with anchored headings
605
- if ( strpos( $content, 'ez-toc-container' ) || ! $is_eligible ) {
606
 
607
  return mb_find_replace( $find, $replace, $content );
608
  }
609
 
 
 
 
 
610
  // else also add toc to content
611
- switch ( ezTOC_Option::get( 'position' ) ) {
612
 
613
  case 'top':
614
- $content = $html . mb_find_replace( $find, $replace, $content );
615
  break;
616
 
617
  case 'bottom':
618
- $content = mb_find_replace( $find, $replace, $content ) . $html;
619
  break;
620
 
621
  case 'after':
622
- $replace[0] = $replace[0] . $html;
623
  $content = mb_find_replace( $find, $replace, $content );
624
  break;
625
 
@@ -644,21 +665,25 @@ if ( ! class_exists( 'ezTOC' ) ) {
644
  */
645
  if ( 1 === $result ) {
646
 
 
 
647
  $start = strpos( $content, $matches[0] );
648
- $content = substr_replace( $content, $html, $start, 0 );
649
 
650
  } else {
651
 
 
 
652
  // Somehow, there are scenarios where the processing get this far and
653
  // the TOC is being added to pages where it should not. Disable for now.
654
  //$content = $html . $content;
655
  }
656
  }
657
 
658
- return $content . $debug;
659
  }
660
 
661
- } // end class
662
 
663
  /**
664
  * The main function responsible for returning the Easy Table of Contents instance to functions everywhere.
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
  * @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;
33
  use function Easy_Plugins\Table_Of_Contents\String\mb_find_replace;
34
 
35
  // Exit if accessed directly
48
  * @since 1.0
49
  * @var string
50
  */
51
+ const VERSION = '2.0.13';
52
 
53
  /**
54
  * Stores the instance of this class.
132
 
133
  require_once( EZ_TOC_PATH . '/includes/class.post.php' );
134
  require_once( EZ_TOC_PATH . '/includes/class.widget-toc.php' );
135
+ require_once( EZ_TOC_PATH . '/includes/Debug.php' );
136
  require_once( EZ_TOC_PATH . '/includes/inc.functions.php' );
137
  require_once( EZ_TOC_PATH . '/includes/inc.string-functions.php' );
138
 
548
  * This will add the inline table of contents page anchors to the post content. It will also insert the
549
  * table of contents inline with the post content as defined by the user defined preference.
550
  *
551
+ * @since 1.0
 
 
552
  *
553
  * @param string $content
554
  *
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();
596
  $replace = $post->getHeadingsWithAnchors();
597
+ $toc = $post->getTOC();
598
 
599
+ $headings = implode( PHP_EOL, $find );
600
+ $anchors = implode( PHP_EOL, $replace );
601
 
602
+ $headingRows = count( $find ) + 1;
603
+ $anchorRows = count( $replace ) + 1;
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>"
617
+ );
618
 
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 ) {
633
 
634
  case 'top':
635
+ $content = $toc . mb_find_replace( $find, $replace, $content );
636
  break;
637
 
638
  case 'bottom':
639
+ $content = mb_find_replace( $find, $replace, $content ) . $toc;
640
  break;
641
 
642
  case 'after':
643
+ $replace[0] = $replace[0] . $toc;
644
  $content = mb_find_replace( $find, $replace, $content );
645
  break;
646
 
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.
679
  //$content = $html . $content;
680
  }
681
  }
682
 
683
+ return $log->appendTo( $content );
684
  }
685
 
686
+ }
687
 
688
  /**
689
  * The main function responsible for returning the Easy Table of Contents instance to functions everywhere.
includes/Debug.php ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace Easy_Plugins\Table_Of_Contents;
4
+
5
+ use WP_Error;
6
+
7
+ /**
8
+ * Class Debug
9
+ *
10
+ * @package Easy_Plugins\Table_Of_Contents
11
+ */
12
+ final class Debug extends WP_Error {
13
+
14
+ /**
15
+ * @since 2.0.13
16
+ * @var bool
17
+ */
18
+ protected $display = false;
19
+
20
+ /**
21
+ * @since 2.0.13
22
+ * @var bool
23
+ */
24
+ protected $enabled = false;
25
+
26
+ /**
27
+ * Debug constructor.
28
+ *
29
+ * @since 2.0.13
30
+ *
31
+ * @param string $code
32
+ * @param string $message
33
+ * @param string $data
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
+ *
49
+ * @param string $content
50
+ *
51
+ * @return string
52
+ */
53
+ public function appendTo( $content = '' ) {
54
+
55
+ return $content . $this;
56
+ }
57
+
58
+ /**
59
+ * @since 2.0.13
60
+ *
61
+ * @return string
62
+ */
63
+ public function dump() {
64
+
65
+ $dump = array();
66
+
67
+ foreach ( (array) $this->errors as $code => $messages ) {
68
+
69
+ $data = $this->get_error_data( $code );
70
+ $data = is_string( $data ) ? $data : '<code>' . var_export( $data, true ) . '</code>';
71
+ $data = "\t\t<li class=\"ez-toc-debug-message-data\">{$data}</li>" . PHP_EOL;
72
+
73
+ array_push(
74
+ $dump,
75
+ PHP_EOL . "\t<ul class=\"ez-toc-debug-message-{$code}\">" . PHP_EOL . "\t\t<li class=\"ez-toc-debug-message\">" . implode( '</li>' . PHP_EOL . '<li>' . PHP_EOL, $messages ) . '</li>' . PHP_EOL . "{$data}\t</ul>" . PHP_EOL
76
+ );
77
+ }
78
+
79
+ return '<div class="ez-toc-debug-message">' . implode( '</div>' . PHP_EOL . '<div class="ez-toc-debug-message">', $dump ) . '</div>' . PHP_EOL;
80
+ }
81
+
82
+ /**
83
+ * @since 2.0.13
84
+ *
85
+ * @return string
86
+ */
87
+ public function __toString() {
88
+
89
+ if ( ! $this->enabled ) {
90
+
91
+ return '';
92
+ }
93
+
94
+ if ( ! $this->has_errors() ) {
95
+
96
+ return '';
97
+ }
98
+
99
+ $intro = sprintf(
100
+ 'You see the following because <a href="%1$s"><code>WP_DEBUG</code></a> and <a href="%1$s"><code>WP_DEBUG_DISPLAY</code></a> are enabled on this site. Please disabled these to prevent the display of these developers\' debug messages.',
101
+ 'https://codex.wordpress.org/WP_DEBUG'
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
+ }