| 1 |
<?php |
| 2 |
/** |
| 3 |
* TOC Compatibility Module |
| 4 |
* |
| 5 |
* Handles compatibility with third-party builders, themes, and plugins (Divi, Elementor, Flatsome, WooCommerce, Rank Math). |
| 6 |
* |
| 7 |
* @package WPEXtra\Modules\Common\TOC |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEXtra\Modules\Common\TOC; |
| 11 |
|
| 12 |
use WPEXtra\Helper; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
class Compatibility { |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize compatibility hooks. |
| 22 |
*/ |
| 23 |
public static function init() { |
| 24 |
// Category & Archive descriptions |
| 25 |
add_filter( 'term_description', array( __CLASS__, 'filter_term_description' ), 99, 2 ); |
| 26 |
add_filter( 'woocommerce_taxonomy_archive_description_raw', array( __CLASS__, 'filter_woo_category_description' ), 99, 2 ); |
| 27 |
add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'allow_svg_in_category_description' ), 10, 2 ); |
| 28 |
|
| 29 |
// Rank Math SEO |
| 30 |
add_filter( 'rank_math/researches/toc_plugins', array( __CLASS__, 'rank_math_compat' ) ); |
| 31 |
|
| 32 |
// Elementor |
| 33 |
add_action( 'elementor/init', array( __CLASS__, 'init_elementor' ) ); |
| 34 |
|
| 35 |
// Divi |
| 36 |
add_action( 'et_pb_admin_excluded_shortcodes', array( __CLASS__, 'divi_excluded_shortcodes' ) ); |
| 37 |
|
| 38 |
// Gutenberg Reusable Blocks |
| 39 |
add_filter( 'wp_toc_modify_process_page_content', array( __CLASS__, 'gutenberg_reusable_blocks' ), 10, 1 ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Filter term description for category/tag TOC. |
| 44 |
* |
| 45 |
* @param string $description |
| 46 |
* @param int $term_id |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public static function filter_term_description( $description, $term_id ) { |
| 50 |
if ( ! is_admin() && ! empty( $description ) && Helper::get_option( 'include_category', false ) ) { |
| 51 |
if ( is_category() || is_tag() || is_tax() ) { |
| 52 |
$module = \WPEXtra\Modules\Common\TOC::instance(); |
| 53 |
if ( $module ) { |
| 54 |
return $module->process_content( $description ); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
return $description; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Filter WooCommerce product category description. |
| 63 |
* |
| 64 |
* @param string $description |
| 65 |
* @param object $term |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
public static function filter_woo_category_description( $description, $term ) { |
| 69 |
if ( ! is_admin() && ! empty( $description ) && Helper::get_option( 'include_product_category', false ) ) { |
| 70 |
$module = \WPEXtra\Modules\Common\TOC::instance(); |
| 71 |
if ( $module ) { |
| 72 |
return $module->process_content( $description ); |
| 73 |
} |
| 74 |
} |
| 75 |
return $description; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* WooCommerce: Allow SVG tags in category descriptions. |
| 80 |
* |
| 81 |
* @param array $allowed_tags |
| 82 |
* @param string $context |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public static function allow_svg_in_category_description( $allowed_tags, $context = '' ) { |
| 86 |
if ( function_exists( 'is_product_category' ) && is_product_category() && Helper::get_option( 'include_product_category', false ) ) { |
| 87 |
$allowed_tags['svg'] = array( |
| 88 |
'width' => true, |
| 89 |
'height' => true, |
| 90 |
'viewbox' => true, |
| 91 |
'xmlns' => true, |
| 92 |
'fill' => true, |
| 93 |
'stroke' => true, |
| 94 |
'style' => true, |
| 95 |
'class' => true, |
| 96 |
); |
| 97 |
$allowed_tags['path'] = array( |
| 98 |
'd' => true, |
| 99 |
'fill' => true, |
| 100 |
'stroke' => true, |
| 101 |
); |
| 102 |
$allowed_tags['span']['style'] = true; |
| 103 |
} |
| 104 |
return $allowed_tags; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Rank Math SEO TOC plugin declaration. |
| 109 |
* |
| 110 |
* @param array $toc_plugins |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public static function rank_math_compat( $toc_plugins ) { |
| 114 |
$plugin_basename = defined( 'WPEX_BASENAME' ) ? WPEX_BASENAME : 'wp-extra-pro/wp-extra.php'; |
| 115 |
$toc_plugins[ $plugin_basename ] = 'WP Extra TOC'; |
| 116 |
return $toc_plugins; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Elementor compatibility. |
| 121 |
*/ |
| 122 |
public static function init_elementor() { |
| 123 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 124 |
add_action( 'elementor/frontend/after_enqueue_scripts', function() { |
| 125 |
if ( Helper::get_option( 'smooth_scroll', true ) ) { |
| 126 |
wp_enqueue_script( 'wptoc-scroll-scriptjs' ); |
| 127 |
} |
| 128 |
} ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Divi builder excluded shortcodes. |
| 134 |
* |
| 135 |
* @param array $shortcodes |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
public static function divi_excluded_shortcodes( $shortcodes ) { |
| 139 |
$shortcodes[] = 'toc'; |
| 140 |
$shortcodes[] = 'wp-toc'; |
| 141 |
return $shortcodes; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Gutenberg Reusable Blocks compatibility. |
| 146 |
* |
| 147 |
* @param string $content |
| 148 |
* @return string |
| 149 |
*/ |
| 150 |
public static function gutenberg_reusable_blocks( $content ) { |
| 151 |
if ( function_exists( 'do_blocks' ) ) { |
| 152 |
if ( has_block( 'easytoc/toc' ) ) { |
| 153 |
$content = str_replace( '<!-- wp:easytoc/toc /-->', 'wptoctempblock', $content ); |
| 154 |
$content = do_blocks( $content ); |
| 155 |
$content = str_replace( 'wptoctempblock', '<!-- wp:easytoc/toc /-->', $content ); |
| 156 |
} else { |
| 157 |
$content = do_blocks( $content ); |
| 158 |
} |
| 159 |
} |
| 160 |
return $content; |
| 161 |
} |
| 162 |
} |
| 163 |
|