forked from ahmedkaludi/easy-table-of-contents
-
Notifications
You must be signed in to change notification settings - Fork 1
/
easy-table-of-contents.php
612 lines (473 loc) · 16.3 KB
/
easy-table-of-contents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
<?php
/**
* Plugin Name: Easy Table of Contents
* Plugin URI: http://connections-pro.com/
* Description: Adds a user friendly and fully automatic way to create and display a table of contents generated from the page content.
* Version: 2.0-rc1
* Author: Steven A. Zahm
* Author URI: http://connections-pro.com/
* Text Domain: easy-table-of-contents
* Domain Path: /languages
*
* Copyright 2018 Steven A. Zahm ( email : helpdesk@connections-pro.com )
*
* Easy Table of Contents is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy Table of Contents; if not, see <http://www.gnu.org/licenses/>.
*
* @package Easy Table of Contents
* @category Plugin
* @author Steven A. Zahm
* @version 2.0-rc1
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'ezTOC' ) ) {
/**
* Class ezTOC
*/
final class ezTOC {
/**
* Current version.
*
* @since 1.0
* @var string
*/
const VERSION = '2.0-rc1';
/**
* Stores the instance of this class.
*
* @access private
* @since 1.0
* @static
*
* @var ezTOC
*/
private static $instance;
/**
* Keeps a track of used anchors for collision detecting.
*
* @access private
* @since 1.0
* @static
*
* @var array
*/
private static $collision_collector = array();
/**
* A dummy constructor to prevent the class from being loaded more than once.
*
* @access public
* @since 1.0
*/
public function __construct() { /* Do nothing here */ }
/**
* @access private
* @since 1.0
* @static
*
* @return ezTOC
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof ezTOC ) ) {
self::$instance = new ezTOC();
self::defineConstants();
self::includes();
self::hooks();
self::loadTextdomain();
}
return self::$instance;
}
/**
* Define the plugin constants.
*
* @access private
* @since 1.0
* @static
*/
private static function defineConstants() {
define( 'EZ_TOC_DIR_NAME', plugin_basename( dirname( __FILE__ ) ) );
define( 'EZ_TOC_BASE_NAME', plugin_basename( __FILE__ ) );
define( 'EZ_TOC_PATH', plugin_dir_path( __FILE__ ) );
define( 'EZ_TOC_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Includes the plugin dependency files.
*
* @access private
* @since 1.0
* @static
*/
private static function includes() {
require_once( EZ_TOC_PATH . 'includes/class.options.php' );
if ( is_admin() ) {
// This must be included after `class.options.php` because it depends on it methods.
require_once( EZ_TOC_PATH . 'includes/class.admin.php' );
}
require_once( EZ_TOC_PATH . 'includes/class.post.php' );
require_once( EZ_TOC_PATH . 'includes/class.widget-toc.php' );
require_once( EZ_TOC_PATH . 'includes/inc.functions.php' );
}
/**
* Add the core action filter hook.
*
* @access private
* @since 1.0
* @static
*/
private static function hooks() {
//add_action( 'plugins_loaded', array( __CLASS__, 'loadTextdomain' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueueScripts' ) );
// Run after shortcodes are interpreted (priority 10).
add_filter( 'the_content', array( __CLASS__, 'the_content' ), ( PHP_INT_MAX - 1000 ) );
add_shortcode( 'ez-toc', array( __CLASS__, 'shortcode' ) );
add_shortcode( apply_filters( 'ez_toc_shortcode', 'toc' ), array( __CLASS__, 'shortcode' ) );
}
/**
* Load the plugin translation.
*
* Credit: Adapted from Ninja Forms / Easy Digital Downloads.
*
* @access private
* @since 1.0
* @static
*
* @uses apply_filters()
* @uses get_locale()
* @uses load_textdomain()
* @uses load_plugin_textdomain()
*
* @return void
*/
public static function loadTextdomain() {
// Plugin textdomain. This should match the one set in the plugin header.
$domain = 'easy-table-of-contents';
// Set filter for plugin's languages directory
$languagesDirectory = apply_filters( "ez_{$domain}_languages_directory", EZ_TOC_DIR_NAME . '/languages/' );
// Traditional WordPress plugin locale filter
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
$fileName = sprintf( '%1$s-%2$s.mo', $domain, $locale );
// Setup paths to current locale file
$local = $languagesDirectory . $fileName;
$global = WP_LANG_DIR . "/{$domain}/" . $fileName;
if ( file_exists( $global ) ) {
// Look in global `../wp-content/languages/{$domain}/` folder.
load_textdomain( $domain, $global );
} elseif ( file_exists( $local ) ) {
// Look in local `../wp-content/plugins/{plugin-directory}/languages/` folder.
load_textdomain( $domain, $local );
} else {
// Load the default language files
load_plugin_textdomain( $domain, FALSE, $languagesDirectory );
}
}
/**
* Register and enqueue CSS and javascript files for frontend.
*
* @access private
* @since 1.0
* @static
*/
public static function enqueueScripts() {
// If SCRIPT_DEBUG is set and TRUE load the non-minified JS files, otherwise, load the minified files.
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
$js_vars = array();
wp_register_style( 'ez-icomoon', EZ_TOC_URL . "vendor/icomoon/style$min.css", array(), ezTOC::VERSION );
wp_register_style( 'ez-toc', EZ_TOC_URL . "assets/css/screen$min.css", array( 'ez-icomoon' ), ezTOC::VERSION );
wp_register_script( 'js-cookie', EZ_TOC_URL . "vendor/js-cookie/js.cookie$min.js", array(), '2.0.3', TRUE );
wp_register_script( 'jquery-smooth-scroll', EZ_TOC_URL . "vendor/smooth-scroll/jquery.smooth-scroll$min.js", array( 'jquery' ), '1.5.5', TRUE );
wp_register_script( 'jquery-sticky-kit', EZ_TOC_URL . "vendor/sticky-kit/jquery.sticky-kit$min.js", array( 'jquery' ), '1.9.2', TRUE );
wp_register_script( 'ez-toc-js', EZ_TOC_URL . "assets/js/front$min.js", array( 'jquery-smooth-scroll', 'js-cookie', 'jquery-sticky-kit'), ezTOC::VERSION, TRUE );
if ( ! ezTOC_Option::get( 'exclude_css' ) ) {
wp_enqueue_style( 'ez-toc' );
self::inlineCSS();
}
if ( ezTOC_Option::get( 'smooth_scroll' ) ) {
$js_vars['smooth_scroll'] = TRUE;
}
//wp_enqueue_script( 'ez-toc-js' );
if ( ezTOC_Option::get( 'show_heading_text' ) && ezTOC_Option::get( 'visibility' ) ) {
$width = ezTOC_Option::get( 'width' ) != 'custom' ? ezTOC_Option::get( 'width' ) : ezTOC_Option::get( 'width_custom' ) . ezTOC_Option::get( 'width_custom_units' );
$js_vars['visibility_hide_by_default'] = ezTOC_Option::get( 'visibility_hide_by_default' ) ? TRUE : FALSE;
$js_vars['width'] = esc_js( $width );
}
$offset = wp_is_mobile() ? ezTOC_Option::get( 'mobile_smooth_scroll_offset', 0 ) : ezTOC_Option::get( 'smooth_scroll_offset', 30 );
$js_vars['scroll_offset'] = esc_js( $offset );
if ( ezTOC_Option::get( 'widget_affix_selector' ) ) {
$js_vars['affixSelector'] = ezTOC_Option::get( 'widget_affix_selector' );
}
if ( 0 < count( $js_vars ) ) {
wp_localize_script( 'ez-toc-js', 'ezTOC', $js_vars );
}
}
/**
* Prints out inline CSS after the core CSS file to allow overriding core styles via options.
*
* @access private
* @since 1.0
* @static
*/
public static function inlineCSS() {
$css = '';
if ( ! ezTOC_Option::get( 'exclude_css' ) ) {
$css .= 'div#ez-toc-container p.ez-toc-title {font-size: ' . ezTOC_Option::get( 'title_font_size', 120 ) . ezTOC_Option::get( 'title_font_size_units', '%' ) . ';}';
$css .= 'div#ez-toc-container p.ez-toc-title {font-weight: ' . ezTOC_Option::get( 'title_font_weight', 500 ) . ';}';
$css .= 'div#ez-toc-container ul li {font-size: ' . ezTOC_Option::get( 'font_size' ) . ezTOC_Option::get( 'font_size_units' ) . ';}';
if ( ezTOC_Option::get( 'theme' ) == 'custom' || ezTOC_Option::get( 'width' ) != 'auto' ) {
$css .= 'div#ez-toc-container {';
if ( ezTOC_Option::get( 'theme' ) == 'custom' ) {
$css .= 'background: ' . ezTOC_Option::get( 'custom_background_colour' ) . ';border: 1px solid ' . ezTOC_Option::get( 'custom_border_colour' ) . ';';
}
if ( 'auto' != ezTOC_Option::get( 'width' ) ) {
$css .= 'width: ';
if ( 'custom' != ezTOC_Option::get( 'width' ) ) {
$css .= ezTOC_Option::get( 'width' );
} else {
$css .= ezTOC_Option::get( 'width_custom' ) . ezTOC_Option::get( 'width_custom_units' );
}
$css .= ';';
}
$css .= '}';
}
if ( 'custom' == ezTOC_Option::get( 'theme' ) ) {
$css .= 'div#ez-toc-container p.ez-toc-title {color: ' . ezTOC_Option::get( 'custom_title_colour' ) . ';}';
//$css .= 'div#ez-toc-container p.ez-toc-title a,div#ez-toc-container ul.ez-toc-list a {color: ' . ezTOC_Option::get( 'custom_link_colour' ) . ';}';
$css .= 'div#ez-toc-container ul.ez-toc-list a {color: ' . ezTOC_Option::get( 'custom_link_colour' ) . ';}';
$css .= 'div#ez-toc-container ul.ez-toc-list a:hover {color: ' . ezTOC_Option::get( 'custom_link_hover_colour' ) . ';}';
$css .= 'div#ez-toc-container ul.ez-toc-list a:visited {color: ' . ezTOC_Option::get( 'custom_link_visited_colour' ) . ';}';
}
}
if ( $css ) {
wp_add_inline_style( 'ez-toc', $css );
}
}
/**
* Returns a string with all items from the $find array replaced with their matching
* items in the $replace array. This does a one to one replacement (rather than globally).
*
* This function is multibyte safe.
*
* $find and $replace are arrays, $string is the haystack. All variables are passed by reference.
*
* @access private
* @since 1.0
* @static
*
* @param bool $find
* @param bool $replace
* @param string $string
*
* @return mixed|string
*/
private static function mb_find_replace( &$find = FALSE, &$replace = FALSE, &$string = '' ) {
if ( is_array( $find ) && is_array( $replace ) && $string ) {
// check if multibyte strings are supported
if ( function_exists( 'mb_strpos' ) ) {
for ( $i = 0; $i < count( $find ); $i ++ ) {
$string = mb_substr(
$string,
0,
mb_strpos( $string, $find[ $i ] )
) . // everything before $find
$replace[ $i ] . // its replacement
mb_substr(
$string,
mb_strpos( $string, $find[ $i ] ) + mb_strlen( $find[ $i ] )
) // everything after $find
;
}
} else {
for ( $i = 0; $i < count( $find ); $i ++ ) {
$string = substr_replace(
$string,
$replace[ $i ],
strpos( $string, $find[ $i ] ),
strlen( $find[ $i ] )
);
}
}
}
return $string;
}
/**
* Array search deep.
*
* Search an array recursively for a value.
*
* @link https://stackoverflow.com/a/5427665/5351316
*
* @param $search
* @param array $array
* @param string $mode
*
* @return bool
*/
public static function array_search_deep( $search, array $array, $mode = 'value' ) {
foreach ( new RecursiveIteratorIterator( new RecursiveArrayIterator( $array ) ) as $key => $value ) {
if ( $search === ${${"mode"}} ) {
return TRUE;
}
}
return FALSE;
}
/**
* Returns true if the table of contents is eligible to be printed, false otherwise.
*
* NOTE: Must bve use only within the loop.
*
* @access public
* @since 1.0
* @static
*
* @return bool
*/
public static function is_eligible() {
$post = get_post();
if ( empty( $post ) || ! $post instanceof WP_Post ) {
return FALSE;
}
$content = get_the_content();
if ( has_shortcode( $content, apply_filters( 'ez_toc_shortcode', 'toc' ) ) ||
has_shortcode( $content, 'ez-toc' ) ) {
return TRUE;
}
if ( is_front_page() && ! ezTOC_Option::get( 'include_homepage' ) ) {
return FALSE;
}
$type = get_post_type( $post->ID );
$enabled = in_array( $type, ezTOC_Option::get( 'enabled_post_types', array() ) );
$insert = in_array( $type, ezTOC_Option::get( 'auto_insert_post_types', array() ) );
if ( $insert || $enabled ) {
if ( ezTOC_Option::get( 'restrict_path' ) ) {
/**
* @link https://wordpress.org/support/topic/restrict-path-logic-does-not-work-correctly?
*/
if ( FALSE !== strpos( ezTOC_Option::get( 'restrict_path' ), $_SERVER['REQUEST_URI'] ) ) {
return FALSE;
} else {
return TRUE;
}
} else {
if ( $insert && 1 == get_post_meta( $post->ID, '_ez-toc-disabled', TRUE ) ) {
return FALSE;
} elseif ( $insert && 0 == get_post_meta( $post->ID, '_ez-toc-disabled', TRUE ) ) {
return TRUE;
} elseif ( $enabled && 1 == get_post_meta( $post->ID, '_ez-toc-insert', TRUE ) ) {
return TRUE;
}
return FALSE;
}
} else {
return FALSE;
}
}
/**
* Callback for the registered shortcode `[ez-toc]`
*
* NOTE: Shortcode is run before the callback @see ezTOC::the_content() for the `the_content` filter
*
* @access private
* @since 1.3
* @static
*
* @param array|string $atts Shortcode attributes array or empty string.
* @param string $content The enclosed content (if the shortcode is used in its enclosing form)
* @param string $tag Shortcode name.
*
* @return mixed
*/
public static function shortcode( $atts, $content, $tag ) {
static $run = TRUE;
$out = '';
if ( $run ) {
$post = ezTOC_Post::get( get_the_ID() )->applyContentFilter()->process();
$out = $post->getTOC();
$run = FALSE;
}
return $out;
}
/**
* Callback for the `the_content` filter.
*
* This will add the inline table of contents page anchors to the post content. It will also insert the
* table of contents inline with the post content as defined by the user defined preference.
*
* @access private
* @since 1.0
* @static
*
* @param string $content
*
* @return string
*/
public static function the_content( $content ) {
// bail if feed, search or archive
if ( is_feed() || is_search() || is_archive() ) {
return $content;
}
// bail if post not eligible and widget is not active
$is_eligible = self::is_eligible();
if ( ! $is_eligible && ! is_active_widget( false, false, 'ezw_tco' ) ) {
return $content;
}
if ( ! $post = ezTOC_Post::get( get_the_ID() ) ) {
return $content;
}
$post->applyContentFilter()->process();
$find = $post->getHeadings();
$replace = $post->getHeadingsWithAnchors();
$html = $post->getTOC();
// bail if no headings found
if ( ! $post->hasTOCItems() ) {
return $content;
}
// if shortcode used or post not eligible, return content with anchored headings
if ( strpos( $content, 'ez-toc-container' ) || ! $is_eligible ) {
return self::mb_find_replace( $find, $replace, $content );
}
// else also add toc to content
switch ( ezTOC_Option::get( 'position' ) ) {
case 'top':
$content = $html . self::mb_find_replace( $find, $replace, $content );
break;
case 'bottom':
$content = self::mb_find_replace( $find, $replace, $content ) . $html;
break;
case 'after':
$replace[0] = $replace[0] . $html;
$content = self::mb_find_replace( $find, $replace, $content );
break;
case 'before':
default:
$replace[0] = $html . $replace[0];
$content = self::mb_find_replace( $find, $replace, $content );
}
return $content;
}
} // end class
/**
* The main function responsible for returning the Easy Table of Contents instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing to declare the global.
*
* Example: <?php $instance = ezTOC(); ?>
*
* @access public
* @since 1.0
*
* @return ezTOC
*/
function ezTOC() {
return ezTOC::instance();
}
// Start Easy Table of Contents.
add_action( 'plugins_loaded', 'ezTOC' );
}