PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.4
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.4
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / blocks / includes / class-toc-manager.php

class-toc-manager.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.4, at blocks/includes/class-toc-manager.php

128 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Table of Contents block for Smart Post Show.
4 *
5 * @package Smart_Post_Show_Pro
6 * @subpackage Smart_Post_Show_Pro/blocks/includes
7 */
8
9 namespace SmartPostShow\Blocks;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * TOC class.
17 */
18 class SPS_Table_Of_Contents {
19
20 /**
21 * Table of content block constructor function
22 */
23 public function __construct() {
24 add_filter( 'the_content', array( $this, 'add_ids_to_post_headings' ), 9 );
25 }
26
27 /**
28 * Add id to heading fallback function
29 *
30 * @param string $content Post content.
31 * @param string $permalink Post permalink.
32 * @return string $content HTML markup.
33 */
34 private function add_ids_to_headings_fallback( $content, $permalink ) {
35 $used_ids = array();
36
37 return preg_replace_callback(
38 '/<(h[1-6])(.*?)>(.*?)<\/\1>/is',
39 function ( $matches ) use ( &$used_ids, $permalink ) {
40 $tag = $matches[1];
41 $attrs = $matches[2];
42 $text = wp_strip_all_tags( $matches[3] );
43
44 // Skip if class contains sp-smart-post-title.
45 if ( preg_match( '/class=["\'][^"\']*sp-smart-post-title[^"\']*["\']/', $attrs ) ) {
46 return $matches[0];
47 }
48
49 // Skip if ID already exists.
50 if ( preg_match( '/id=["\'].*?["\']/', $attrs ) ) {
51 return $matches[0];
52 }
53
54 $id = sanitize_title( $text );
55
56 while ( in_array( $id, $used_ids, true ) ) {
57 $id .= '-' . wp_rand( 10, 99 );
58 }
59
60 $used_ids[] = $id;
61
62 return sprintf(
63 '<%1$s%2$s><span id="%3$s"></span>%4$s<a href="#%3$s" class="sps-toc-heading-copy-link" data-url="%5$s#%3$s" title="Copy link to this heading">#</a></%1$s>',
64 $tag,
65 $attrs,
66 esc_attr( $id ),
67 $matches[3],
68 esc_url( $permalink )
69 );
70 },
71 $content
72 );
73 }
74
75 /**
76 * Add id to heading function
77 *
78 * @param string $content Post content.
79 * @param string $permalink Post permalink.
80 * @return string $content HTML markup.
81 */
82 private function add_ids_to_headings( $content, $permalink ) {
83 if ( empty( $content ) ) {
84 return $content;
85 }
86
87 return $this->add_ids_to_headings_fallback( $content, $permalink );
88 }
89
90 /**
91 * Check table of content function
92 *
93 * @param string $content Post content.
94 * @return boolean
95 */
96 private function has_toc_block( $content ) {
97 return preg_match( '/<!--\s*wp:sp-smart-post-show\/table-of-content/', $content )
98 || strpos( $content, 'sps-toc' ) !== false
99 || preg_match( '/"blockName":"sp-smart-post-show\/table-of-content"/', $content );
100 }
101
102 /**
103 * Add_ids_to_post_headings function
104 *
105 * @param string $content Post content.
106 * @return string
107 */
108 public function add_ids_to_post_headings( $content ) {
109 if ( ! is_singular() || is_admin() || wp_doing_ajax() ) {
110 return $content;
111 }
112
113 // Check if we're in the main query and it's the final output.
114 if ( ! in_the_loop() || ! is_main_query() ) {
115 return $content;
116 }
117
118 $permalink = get_permalink();
119 if ( is_single() && $this->has_toc_block( $content ) ) {
120 $content = $this->add_ids_to_headings( $content, $permalink );
121 } elseif ( is_page() && $this->has_toc_block( $content ) ) {
122 $content = $this->add_ids_to_headings( $content, $permalink );
123 }
124
125 return $content;
126 }
127 }
128