.
*
* @package Easy Table of Contents
* @category Plugin
* @author Magazine3
* @version 2.0.62
*/
use Easy_Plugins\Table_Of_Contents\Debug;
use function Easy_Plugins\Table_Of_Contents\Cord\insertElementByPTag;
use function Easy_Plugins\Table_Of_Contents\Cord\insertElementByImgTag;
use function Easy_Plugins\Table_Of_Contents\Cord\mb_find_replace;
// 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.62';
/**
* Stores the instance of this class.
*
* @access private
* @since 1.0
* @static
*
* @var ezTOC
*/
private static $instance;
/**
* @since 2.0
* @var array
*/
private static $store = 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 self ) ) {
self::$instance = new self;
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', dirname( __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/helper-function.php" );
require_once( EZ_TOC_PATH . '/includes/newsletter.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/class.widget-toc-sticky.php' );
require_once( EZ_TOC_PATH . '/includes/Debug.php' );
require_once( EZ_TOC_PATH . '/includes/inc.functions.php' );
require_once( EZ_TOC_PATH . '/includes/inc.cord-functions.php' );
require_once( EZ_TOC_PATH . '/includes/inc.plugin-compatibility.php' );
}
/**
* Add the core action filter hook.
*
* @access private
* @since 1.0
* @static
*/
private static function hooks() {
add_action('admin_head', array( __CLASS__, 'addEditorButton' ));
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueueScripts' ) );
add_action( 'wp_head', array( __CLASS__, 'ez_toc_inline_styles' ) );
add_action( 'wp_head', array( __CLASS__, 'ez_toc_schema_sitenav_creator' ) );
if ( in_array( 'divi-machine/divi-machine.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'Fortunato Pro' == apply_filters( 'current_theme', get_option( 'current_theme' ) ) ) {
add_option( 'ez-toc-post-content-core-level', false );
}
if ( ezTOC_Option::get( 'exclude_css' ) && 'css' == ezTOC_Option::get( 'toc_loading' ) ) {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueueScriptsforExcludeCSS' ) );
}
if( !self::checkBeaverBuilderPluginActive() ) {
add_filter( 'the_content', array( __CLASS__, 'the_content' ), 100 );
if( defined('EASY_TOC_AMP_VERSION') ){
add_filter( 'ampforwp_modify_the_content', array( __CLASS__, 'the_content' ) );
}
add_filter( 'term_description', array( __CLASS__, 'toc_term_content_filter' ), 99,2);
add_filter( 'woocommerce_taxonomy_archive_description_raw', array( __CLASS__, 'toc_category_content_filter_woocommerce' ), 99,2);
add_shortcode( 'ez-toc', array( __CLASS__, 'shortcode' ) );
add_shortcode( apply_filters( 'ez_toc_shortcode', 'toc' ), array( __CLASS__, 'shortcode' ) );
add_shortcode( 'ez-toc-widget-sticky', array( __CLASS__, 'ez_toc_widget_sticky_shortcode' ) );
add_action('wp_footer', array(__CLASS__, 'stickyToggleContent'));
}
}
/**
* is_sidebar_hastoc function
* @since 2.0.51
* @static
* @return bool
*/
public static function is_sidebar_hastoc() {
$status = false;
$generate_toc_link_ids = ezTOC_Option::get('generate_toc_link_ids');
if($generate_toc_link_ids){
return true;
}
$widget_blocks = get_option( 'widget_block' );
foreach( (array) $widget_blocks as $widget_block ) {
if ( ! empty( $widget_block['content'] ) && ( has_shortcode( $widget_block['content'] , 'toc' ) || has_shortcode( $widget_block['content'] , 'ez-toc' ) || has_shortcode( $widget_block['content'] , 'ez-toc-widget-sticky' ) ) ) {
$status = true;
break;
}
}
if(!$status){
$widget_texts = get_option( 'widget_text' );
foreach( (array) $widget_texts as $widget_text ) {
if ( ! empty( $widget_text['text'] ) && ( has_shortcode( $widget_text['text'] , 'toc' ) || has_shortcode( $widget_text['text'] , 'ez-toc' ) || has_shortcode( $widget_text['text'] , 'ez-toc-widget-sticky' ) ) ) {
$status = true;
break;
}
}
}
if(!$status){
$widget_cust_htmls = get_option( 'widget_custom_html' );
foreach( (array) $widget_cust_htmls as $widget_cust_html ) {
if ( ! empty( $widget_cust_html['content'] ) && ( has_shortcode( $widget_cust_html['content'] , 'toc' ) || has_shortcode( $widget_cust_html['content'] , 'ez-toc' ) || has_shortcode( $widget_cust_html['content'] , 'ez-toc-widget-sticky' ) ) ) {
$status = true;
break;
}
}
}
return apply_filters('ez_toc_sidebar_has_toc_filter', $status);
}
/**
* enqueueScriptsforExcludeCSS Method
* for adding toggle css on loading as CSS
* @access public
* @since 2.0.40
* @static
*/
public static function enqueueScriptsforExcludeCSS()
{
$cssChecked = '#ez-toc-container input[type="checkbox"]:checked + nav, #ez-toc-widget-container input[type="checkbox"]:checked + nav {opacity: 0;max-height: 0;border: none;display: none;}';
wp_register_style( 'ez-toc-exclude-toggle-css', '', array(), ezTOC::VERSION );
wp_enqueue_style( 'ez-toc-exclude-toggle-css', '', array(), ezTOC::VERSION );
wp_add_inline_style( 'ez-toc-exclude-toggle-css', $cssChecked );
}
/**
* checkBeaverBuilderPluginActive Method
* @since 2.0.34
* @return bool
*/
private static function checkBeaverBuilderPluginActive() {
if( has_action( 'the_content' ) && isset($_REQUEST['fl_builder'])) {
return true;
}
return false;
}
/**
* 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 );
}
}
public static function ez_toc_inline_styles(){
if (ezTOC_Option::get( 'inline_css' )) {
$screen_css = file_get_contents( EZ_TOC_PATH . '/assets/css/screen.min.css' );
$screen_css .= self::InlineCountingCSS( ezTOC_Option::get( 'heading-text-direction', 'ltr' ) );
$screen_css .= self::InlineCountingCSS( ezTOC_Option::get( 'heading-text-direction', 'ltr' ),'ez-toc-widget-direction','ez-toc-widget-container', 'counter', 'ez-toc-widget-container' );
$screen_css .= self::inlineCSS();
echo '';
}
}
public static function ez_toc_schema_sitenav_creator(){
if(ezTOC_Option::get( 'schema_sitenav_checkbox' ) == true){
$post = ezTOC::get( get_the_ID() );
if($post){
$items = $post->getTocTitleId();
if(!empty($items)){
$output_array = array();
foreach($items as $item){
$output_array[] = array(
"@context" => "https://schema.org",
"@type" => "SiteNavigationElement",
'@id' => '#ez-toc',
"name" => wp_strip_all_tags($item['title']),
"url" => get_permalink() ."#". $item['id'],
);
}
if(!empty($output_array)){
$schema_opt = array();
$schema_opt['@context'] = "https://schema.org";
$schema_opt['@graph'] = $output_array;
echo '';
}
}
}
}
}
/**
* Call back for the `wp_enqueue_scripts` action.
*
* Register and enqueue CSS and javascript files for frontend.
*
* @access private
* @since 1.0
* @static
*/
public static function enqueueScripts() {
$eztoc_post_id = get_the_ID();
// 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';
if ( in_array( 'js_composer_salient/js_composer.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$postMetaContent = get_post_meta( $eztoc_post_id, '_nectar_portfolio_extra_content',true );
if( !empty( $postMetaContent ) ){
update_option( 'ez-toc-post-meta-content', array( $eztoc_post_id => do_shortcode( $postMetaContent ) ) );
}
}
// Register stylesheet which can be called later using wp_enqueue_style()
wp_register_style( 'ez-toc', EZ_TOC_URL . "assets/css/screen$min.css",array( ), ezTOC::VERSION );
wp_register_style( 'ez-toc-sticky', EZ_TOC_URL . "assets/css/ez-toc-sticky{$min}.css", array(), self::VERSION );
// Register scripts which can be called later using wp_enqueue_script()
$in_footer = true;
if ( ezTOC_Option::get( 'load_js_in' ) == 'header' ) {
$in_footer = false;
}
wp_register_script( 'ez-toc-sticky', '', array(), '', $in_footer );
wp_register_script( 'ez-toc-js-cookie', EZ_TOC_URL . "vendor/js-cookie/js.cookie$min.js", array(), '2.2.1', $in_footer );
wp_register_script( 'ez-toc-jquery-sticky-kit', EZ_TOC_URL . "vendor/sticky-kit/jquery.sticky-kit$min.js", array( 'jquery' ), '1.9.2', $in_footer );
wp_register_script( 'ez-toc-js', EZ_TOC_URL . "assets/js/front{$min}.js", array( 'jquery', 'ez-toc-js-cookie', 'ez-toc-jquery-sticky-kit' ), ezTOC::VERSION . '-' . filemtime( EZ_TOC_PATH . "/assets/js/front{$min}.js" ), $in_footer );
wp_register_script( 'ez-toc-scroll-scriptjs', apply_filters('ez_toc_smscroll_jsfile_filter',EZ_TOC_URL . "assets/js/smooth_scroll{$min}.js"), array( 'jquery' ), ezTOC::VERSION, $in_footer );
self::localize_scripts();
if ( self::is_enqueue_scripts_eligible() ) {
self::enqueue_registered_script();
self::enqueue_registered_style();
self::inlineMainCountingCSS();
if ( in_array( 'js_composer/js_composer.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
self::inlineWPBakeryJS();
}
}
if ( ezTOC_Option::get( 'sticky-toggle' ) ) {
wp_enqueue_script( 'ez-toc-sticky', '', '', '', $in_footer );
self::inlineStickyToggleJS();
}
if ( ezTOC_Option::get( 'sticky-toggle' ) ) {
wp_enqueue_style( 'ez-toc-sticky' );
self::inlineStickyToggleCSS();
}
}
/**
* localize_scripts Method
* Localize scripts
*
* @since 2.0.52
* @static
* @uses wp_localize_script()
* @return void
*
*/
public static function localize_scripts(){
$eztoc_post_id = get_the_ID();
$js_vars = array();
if ( ezTOC_Option::get( 'smooth_scroll' ) ) {
$js_vars['smooth_scroll'] = true;
}else{
$js_vars['smooth_scroll'] = false;
}
if ( ezTOC_Option::get( 'show_heading_text' ) && ezTOC_Option::get( 'visibility' ) ) {
$width = ezTOC_Option::get( 'width' ) !== 'custom' ? ezTOC_Option::get( 'width' ) : (wp_is_mobile() ? 'auto' : 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;
if( true == get_post_meta( $eztoc_post_id, '_ez-toc-visibility_hide_by_default', true ) ){
$js_vars['visibility_hide_by_default'] = true;
$js_vars['width'] = esc_js( $width );
}
}else{
if(ezTOC_Option::get( 'visibility' )){
$js_vars['visibility_hide_by_default'] = ezTOC_Option::get( 'visibility_hide_by_default' ) ? true : false;
if( true == get_post_meta( $eztoc_post_id, '_ez-toc-visibility_hide_by_default', true ) ){
$js_vars['visibility_hide_by_default'] = true;
}
}
}
$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 (ezTOC_Option::get( 'toc_loading' ) != 'css') {
$icon = ezTOC::getTOCToggleIcon();
if( function_exists( 'ez_toc_pro_activation_link' ) ) {
$icon = apply_filters('ez_toc_modify_icon',$icon);
}
$js_vars['fallbackIcon'] = $icon;
}
if(ezTOC_Option::get( 'collapsable_sub_hd' )){
$js_vars['collapseSubHd'] = true;
}
if(ezTOC_Option::get( 'ajax_load_more' )){
$js_vars['ajax_toggle'] = true;
}
if ( 0 < count( $js_vars ) ) {
wp_localize_script( 'ez-toc-js', 'ezTOC', $js_vars );
// smooth scroll js localization
$js_scroll = array();
$js_scroll['scroll_offset'] = esc_js( $offset );
$js_scroll['add_request_uri'] = ezTOC_Option::get( 'add_request_uri' ) ? true : false;
if(ezTOC_Option::get( 'smooth_scroll' ) && ezTOC_Option::get( 'avoid_anch_jump' )){
$js_scroll['JumpJsLinks'] = true;
}
wp_localize_script( 'ez-toc-scroll-scriptjs', 'eztoc_smooth_local', $js_scroll );
}
}
/**
* enqueue_registered_style_and_script Method
* Enqueue styles and scripts later after registered
*
* @since 2.0.52
* @static
* @uses wp_enqueue_style() & wp_enqueue_script()
* @return void
*
*/
public static function enqueue_registered_style(){
if(!ezTOC_Option::get( 'exclude_css' )){
if ( ! ezTOC_Option::get( 'inline_css' ) ) {
wp_enqueue_style( 'ez-toc' );
$css = self::inlineCSS();
wp_add_inline_style( 'ez-toc', $css );
}
}
}
/**
* enqueue_registered_style_and_script Method
* Enqueue styles and scripts later after registered
*
* @since 2.0.52
* @static
* @uses wp_enqueue_style() & wp_enqueue_script()
* @return void
*
*/
public static function enqueue_registered_script(){
if (ezTOC_Option::get( 'toc_loading' ) == 'js') {
if ( ezTOC_Option::get( 'smooth_scroll' ) ) {
wp_enqueue_script( 'ez-toc-scroll-scriptjs' );
}
wp_enqueue_script( 'ez-toc-js' );
}
}
/**
* inlineWPBakeryJS Method
* Javascript code for WP Bakery Plugin issue for mobile screen
*
* @since 2.0.35
* @static
* @uses \wp_add_inline_script()
* @return void
*
* ez-toc-list ez-toc-link
* ez-toc-section
*/
private static function inlineWPBakeryJS()
{
$stickyJS = '';
if( wp_is_mobile() )
{
$stickyJS = << 0) {
let ezTocStickyContainerUL = ezTocStickyContainer.querySelectorAll('.ez-toc-link');
for(let i = 0; i < ezTocStickyContainerUL.length; i++) {
let anchorHREF = ezTocStickyContainerUL[i].getAttribute('href');
ezTocStickyContainerUL[i].setAttribute('href', anchorHREF + '-' + uniqID);
}
}
INLINESTICKJSFORMOBILE;
}
$inlineWPBakeryJS = << 0) {
let ezTocContainerUL = mobileContainer.querySelectorAll('.ez-toc-link');
let uniqID = 'xs-sm-' + Math.random().toString(16).slice(2);
for(let i = 0; i < ezTocContainerUL.length; i++) {
let anchorHREF = ezTocContainerUL[i].getAttribute('href');
mobileContainer.querySelector("span.ez-toc-section"+ anchorHREF).setAttribute('id', anchorHREF.replace
('#','') + '-' +
uniqID);
ezTocContainerUL[i].setAttribute('href', anchorHREF + '-' + uniqID);
}
$stickyJS
}
INLINEWPBAKERYJS;
wp_add_inline_script( 'ez-toc-js', $inlineWPBakeryJS );
}
/**
* 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('Chamomile' == apply_filters( 'current_theme', get_option( 'current_theme' ) )){
$css .= '@media screen and (max-width: 1000px) {
#ez-toc-container nav{
display: block;
}
}';
}
if ( ! ezTOC_Option::get( 'exclude_css' ) ) {
$css .= 'div#ez-toc-container .ez-toc-title {font-size: ' . esc_attr( ezTOC_Option::get( 'title_font_size', 120 ) ) . esc_attr( ezTOC_Option::get( 'title_font_size_units', '%' ) ) . ';}';
$css .= 'div#ez-toc-container .ez-toc-title {font-weight: ' . esc_attr( ezTOC_Option::get( 'title_font_weight', 500 ) ) . ';}';
$css .= 'div#ez-toc-container ul li {font-size: ' . esc_attr(ezTOC_Option::get( 'font_size' )) . esc_attr(ezTOC_Option::get( 'font_size_units' )) . ';}';
$css .= 'div#ez-toc-container ul li {font-weight: ' . esc_attr( ezTOC_Option::get( 'font_weight', 500 ) ) . ';}';
$css .= 'div#ez-toc-container nav ul ul li {font-size: ' . esc_attr( ezTOC_Option::get( 'child_font_size' ) . esc_attr(ezTOC_Option::get( 'child_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: ' . esc_attr( ezTOC_Option::get( 'custom_background_colour','#f9f9f9' ) ) . ';border: '.esc_attr( ezTOC_Option::get( 'custom_border_size' ,1) ).'px solid ' . esc_attr( ezTOC_Option::get( 'custom_border_colour' ,'#aaa') ) . ';';
}
if ( 'auto' !== ezTOC_Option::get( 'width' ) ) {
$css .= 'width: ';
if ( 'custom' !== ezTOC_Option::get( 'width' ) ) {
$css .= ezTOC_Option::get( 'width' );
} else {
$css .= wp_is_mobile() ? 'auto' : 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 , #ez-toc-container .ez_toc_custom_title_icon , #ez-toc-container .ez_toc_custom_toc_icon {color: ' . esc_attr( ezTOC_Option::get( 'custom_title_colour' ) ) . ';}';
$css .= 'div#ez-toc-container ul.ez-toc-list a {color: ' . esc_attr( ezTOC_Option::get( 'custom_link_colour' ) ) . ';}';
$css .= 'div#ez-toc-container ul.ez-toc-list a:hover {color: ' . esc_attr( ezTOC_Option::get( 'custom_link_hover_colour' ) ) . ';}';
$css .= 'div#ez-toc-container ul.ez-toc-list a:visited {color: ' . esc_attr( ezTOC_Option::get( 'custom_link_visited_colour' ) ) . ';}';
}
if(ezTOC_Option::get( 'headings-padding' )){
$css .= self::inlineHeadingsPaddingCSS();
}
}
return apply_filters('ez_toc_pro_inline_css',$css);
}
/**
* inlineMainCountingCSS Method
* for adding inlineCounting CSS
* in wp_head in last
* @since 2.0.37
* @return void
*/
public static function inlineMainCountingCSS() {
$css = '';
/**
* RTL Direction
* @since 2.0.33
*/
$css .= self::InlineCountingCSS( ezTOC_Option::get( 'heading-text-direction', 'ltr' ) );
$css .= self::InlineCountingCSS( ezTOC_Option::get( 'heading-text-direction', 'ltr' ),'ez-toc-widget-direction','ez-toc-widget-container', 'counter', 'ez-toc-widget-container' );
if( ezTOC_Option::get( 'sticky-toggle' ) ) {
$cssSticky = self::InlineCountingCSS( ezTOC_Option::get( 'heading-text-direction', 'ltr' ), 'ez-toc-sticky-toggle-direction', 'ez-toc-sticky-toggle-counter', 'counter', 'ez-toc-sticky-container' );
wp_add_inline_style( 'ez-toc-sticky', $cssSticky );
}
/* End rtl direction */
if ( ! ezTOC_Option::get( 'exclude_css' ) ) {
wp_add_inline_style( 'ez-toc', $css );
}
}
/**
* InlineCountingCSS Method
* @since 2.0.33
* @scope private
* @static
* @param string $direction
* @param string $directionClass
* @param string $class
* @param string $counter
* @param string $containerId
* @return string
*/
public static function InlineCountingCSS( $direction = 'ltr', $directionClass = 'ez-toc-container-direction', $class = 'ez-toc-counter', $counter = 'counter', $containerId = 'ez-toc-container' )
{
$list_type = ezTOC_Option::get( $counter, 'decimal' );
if( $list_type != 'none' ) {
$inlineCSS = '';
$counterListAll = array_merge( ezTOC_Option::getCounterListDecimal(), ezTOC_Option::getCounterList_i18n() );
$listTypesForCounting = array_keys( $counterListAll );
$inlineCSS .= << 20 ) {
$topMarginStickyContainer = '70px';
}
if( strlen($toc_title) > 40 ) {
$topMarginStickyContainer = '80px';
}
if( strlen($toc_title) > 60 ) {
$topMarginStickyContainer = '90px';
}
}
$stickyToggleAlignTop="8%";
$stickyToggleAlignChk = ezTOC_Option::get( 'sticky-toggle-alignment' );
if ( !empty($stickyToggleAlignChk) ) {
if($stickyToggleAlignChk == 'middle'){
$stickyToggleAlignTop = '45%';
}
if($stickyToggleAlignChk == 'bottom'){
$stickyToggleAlignTop = '75%';
}
}
$stickyBgColor="#fff";
$stickyHeadTxtColor="#111";
$stickyHeadBgColor="#fff";
$stickyAddlCss="";
$stickyHeadTxtWeight =600;
$stickyHeadTxtSize =18;
$stickyAddlCss = apply_filters('ez_toc_sticky_pro_css', $stickyAddlCss );
$inlineStickyToggleCSS = <<', '', $all_urls);
$urls_arr = explode(PHP_EOL, $all_urls);
if(is_array($urls_arr)){
foreach ($urls_arr as $url_arr) {
if ( isset($_SERVER['REQUEST_URI']) && false !== strpos( $_SERVER['REQUEST_URI'], $url_arr ) ) {
Debug::log( 'is_restricted_path', 'In restricted path, post not eligible.', ezTOC_Option::get( 'restrict_path' ) );
return false;
}
}
}
}
if ( has_shortcode( $post->post_content, apply_filters( 'ez_toc_shortcode', 'toc' ) ) || has_shortcode( $post->post_content, 'ez-toc' ) ) {
Debug::log( 'has_ez_toc_shortcode', 'Has instance of shortcode.', true );
return true;
}
if ( is_front_page() && ! ezTOC_Option::get( 'include_homepage' ) ) {
Debug::log( 'is_front_page', 'Is frontpage, TOC is not enabled.', false );
return false;
}
$type = get_post_type( $post->ID );
Debug::log( 'current_post_type', 'Post type is.', $type );
$enabled = in_array( $type, ezTOC_Option::get( 'enabled_post_types', array() ), true );
$insert = in_array( $type, ezTOC_Option::get( 'auto_insert_post_types', array() ), true );
Debug::log( 'is_supported_post_type', 'Is supported post type?', $enabled );
Debug::log( 'is_auto_insert_post_type', 'Is auto insert for post types?', $insert );
if ( $insert || $enabled ) {
if ( ezTOC_Option::get( 'restrict_path' ) ) {
/**
* @link https://wordpress.org/support/topic/restrict-path-logic-does-not-work-correctly?
*/
if ( isset($_SERVER['REQUEST_URI']) && false !== strpos( ezTOC_Option::get( 'restrict_path' ), $_SERVER['REQUEST_URI'] ) ) {
Debug::log( 'is_restricted_path', 'In restricted path, post not eligible.', ezTOC_Option::get( 'restrict_path' ) );
return false;
} else {
Debug::log( 'is_not_restricted_path', 'Not in restricted path, post is eligible.', ezTOC_Option::get( 'restrict_path' ) );
return true;
}
} else {
if ( $insert && 1 === (int) get_post_meta( $post->ID, '_ez-toc-disabled', true ) ) {
Debug::log( 'is_auto_insert_disable_post_meta', 'Auto insert enabled and disable TOC is enabled in post meta.', false );
return false;
} elseif ( $insert && 0 === (int) get_post_meta( $post->ID, '_ez-toc-disabled', true ) ) {
Debug::log( 'is_auto_insert_enabled_post_meta', 'Auto insert enabled and disable TOC is not enabled in post meta.', true );
return true;
} elseif ( $enabled && 1 === (int) get_post_meta( $post->ID, '_ez-toc-insert', true ) ) {
Debug::log( 'is_supported_post_type_disable_insert_post_meta', 'Supported post type and insert TOC is enabled in post meta.', true );
return true;
} elseif ( $enabled && $insert ) {
Debug::log( 'supported_post_type_and_auto_insert', 'Supported post type and auto insert TOC is enabled.', true );
return true;
}
Debug::log( 'not_auto_insert_or_not_supported_post_type', 'Not supported post type or insert TOC is disabled.', false );
return false;
}
} else {
Debug::log( 'not_auto_insert_and_not_supported post_type', 'Not supported post type and do not auto insert TOC.', false );
return false;
}
}
/**
* Get TOC from store and if not in store process post and add it to the store.
*
* @since 2.0
*
* @param int $id
*
* @return ezTOC_Post|null
*/
public static function get( $id ) {
$post = null;
if ( isset( self::$store[ $id ] ) && self::$store[ $id ] instanceof ezTOC_Post && !in_array( 'js_composer_salient/js_composer.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$post = self::$store[ $id ];
} else {
$post_id = ! empty( $id ) ? $id : get_the_ID();
$post = ezTOC_Post::get( $post_id );
if ( $post instanceof ezTOC_Post ) {
self::$store[ $id ] = $post;
}
}
return $post;
}
/**
* Callback for the registered shortcode `[ez-toc-widget-sticky]`
*
* NOTE: Shortcode is run before the callback @see ezTOC::the_content() for the `the_content` filter
*
* @access private
* @since 2.0.41
*
* @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 string
*/
public static function ez_toc_widget_sticky_shortcode( $atts, $content, $tag ) { global $wp_widget_factory;
if ( 'ez-toc-widget-sticky' == $tag ) {
extract( shortcode_atts( array(
'highlight_color' => '#ededed',
'title' => 'Table of Contents',
'advanced_options' => '',
'scroll_fixed_position' => 30,
'sidebar_width' => 'auto',
'sidebar_width_size_unit' => 'none',
'fixed_top_position' => 30,
'fixed_top_position_size_unit' => 'px',
'navigation_scroll_bar' => 'on',
'scroll_max_height' => 'auto',
'scroll_max_height_size_unit' => 'none',
'ez_toc_widget_sticky_before_widget_container' => '',
'ez_toc_widget_sticky_before_widget' => '',
'ez_toc_widget_sticky_before' => '',
'ez_toc_widget_sticky_after' => '',
'ez_toc_widget_sticky_after_widget' => '',
'ez_toc_widget_sticky_after_widget_container' => '',
), $atts ) );
$widget_name = wp_specialchars( 'ezTOC_WidgetSticky' );
$instance = array(
'title' => ( ! empty ( $title ) ) ? $title : '',
'highlight_color' => ( ! empty ( $highlight_color ) ) ? $highlight_color : '#ededed',
'advanced_options' => ( ! empty ( $advanced_options ) ) ? $advanced_options : '',
'scroll_fixed_position' => ( ! empty ( $scroll_fixed_position ) ) ? ( int ) $scroll_fixed_position : 30,
'sidebar_width' => ( ! empty ( $sidebar_width ) ) ? ( 'auto' == $sidebar_width ) ? $sidebar_width : ( int ) strip_tags ( $sidebar_width ) : 'auto',
'sidebar_width_size_unit' => ( ! empty ( $sidebar_width_size_unit ) ) ? $sidebar_width_size_unit : 'none',
'fixed_top_position' => ( ! empty ( $fixed_top_position ) ) ? ( 'auto' == $fixed_top_position ) ? $fixed_top_position : ( int ) strip_tags ( $fixed_top_position ) : 30,
'fixed_top_position_size_unit' => ( ! empty ( $fixed_top_position_size_unit ) ) ? $fixed_top_position_size_unit : 'px',
'navigation_scroll_bar' => ( ! empty ( $navigation_scroll_bar ) ) ? $navigation_scroll_bar : 'on',
'scroll_max_height' => ( ! empty ( $scroll_max_height ) ) ? ( 'auto' == $scroll_max_height ) ? $scroll_max_height : ( int ) strip_tags ( $scroll_max_height ) : 'auto',
'scroll_max_height_size_unit' => ( ! empty ( $scroll_max_height_size_unit ) ) ? $scroll_max_height_size_unit : 'none',
);
if ( !is_a( $wp_widget_factory->widgets[ $widget_name ], 'WP_Widget' ) ):
$wp_class = 'WP_Widget_' . ucwords(strtolower($class));
if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
return ''.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),''.$class.'').'
';
else:
$class = $wp_class;
endif;
endif;
$id = uniqid( time() );
ob_start();
the_widget( $widget_name, $instance, array(
'widget_id' => 'ez-toc-widget-sticky-' . $id,
'ez_toc_widget_sticky_before_widget_container' => $ez_toc_widget_sticky_before_widget_container,
'ez_toc_widget_sticky_before_widget' => $ez_toc_widget_sticky_before_widget,
'ez_toc_widget_sticky_before' => $ez_toc_widget_sticky_before,
'ez_toc_widget_sticky_after' => $ez_toc_widget_sticky_after,
'ez_toc_widget_sticky_after_widget' => $ez_toc_widget_sticky_after_widget,
'ez_toc_widget_sticky_after_widget_container' => $ez_toc_widget_sticky_after_widget_container,
)
);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
/**
* 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
*
* @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 string
*/
public static function shortcode( $atts, $content, $tag ) {
//Enqueue css and styles if that has not been added by wp_enqueue_scripts
$html = '';
if(!ez_toc_shortcode_enable_support_status($atts)){
return $html;
}
if( ( ezTOC_Option::get( 'toc-run-on-amp-pages', 1 ) !== false && 0 == ezTOC_Option::get( 'toc-run-on-amp-pages', 1 ) || '0' == ezTOC_Option::get( 'toc-run-on-amp-pages', 1 ) || false == ezTOC_Option::get( 'toc-run-on-amp-pages', 1 ) ) && !ez_toc_non_amp() ){
return $html;
}
self::enqueue_registered_script();
self::enqueue_registered_style();
self::inlineMainCountingCSS();
$post_id = isset( $atts['post_id'] ) ? (int) $atts['post_id'] : get_the_ID();
$post = self::get( $post_id );
if ( ! $post instanceof ezTOC_Post ) {
Debug::log( 'not_instance_of_post', 'Not an instance if `WP_Post`.', get_the_ID() );
return Debug::log()->appendTo( $content );
}
$options = array();
if (isset($atts["header_label"])) {
$options['header_label'] = $atts["header_label"];
}
if (isset($atts["display_header_label"]) && $atts["display_header_label"] == "no") {
$options['no_label'] = true;
}
if (isset($atts["toggle_view"]) && $atts["toggle_view"] == "no") {
$options['no_toggle'] = true;
}
if (isset($atts["initial_view"]) && $atts["initial_view"] == 'hide') {
$options['visibility_hide_by_default'] = true;
}
if (isset($atts["display_counter"]) && $atts["display_counter"] == "no") {
$options['no_counter'] = true;
}
if (isset($atts["view_more"]) && $atts["view_more"] > 0) {
$options['view_more'] = $atts["view_more"];
}
$html = count($options) > 0 ? $post->getTOC($options) : $post->getTOC();
return $html;
}
/**
* Whether or not apply `the_content` filter.
*
* @since 2.0
*
* @return bool
*/
private static function maybeApplyTheContentFilter() {
$apply = true;
global $wp_current_filter;
// Do not execute if root current filter is one of those in the array.
if ( in_array( $wp_current_filter[0], array( 'get_the_excerpt', 'init', 'wp_head' ), true ) ) {
$apply = false;
}
// bail if feed, search or archive
if ( is_feed() || is_search() || is_archive() ) {
if( (true == ezTOC_Option::get( 'include_category', false) && is_category()) || (true == ezTOC_Option::get( 'include_tag', false) && is_tag()) || (true == ezTOC_Option::get( 'include_product_category', false) && (function_exists('is_product_category') && is_product_category()) ) || (true == ezTOC_Option::get( 'include_custom_tax', false) && is_tax())) {
$apply = true;
} else {
$apply = false;
}
}
if( function_exists('get_current_screen') ) {
$my_current_screen = get_current_screen();
if ( isset( $my_current_screen->id ) ) {
if( $my_current_screen->id == 'edit-post' ) {
$apply = false;
}
}
}
if ( ! empty( array_intersect( $wp_current_filter, array( 'get_the_excerpt', 'init', 'wp_head' ) ) ) ) {
$apply = false;
}
/**
* Whether or not to apply `the_content` filter callback.
*
* @see ezTOC::the_content()
*
* @since 2.0
*
* @param bool $apply
*/
return apply_filters( 'ez_toc_maybe_apply_the_content_filter', $apply );
}
/**
* 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.
*
* @since 1.0
*
* @param string $content
*
* @return string
*/
public static function the_content( $content ) {
$content = apply_filters('eztoc_modify_the_content',$content);
if( function_exists( 'post_password_required' ) ) {
if( post_password_required() ) return Debug::log()->appendTo( $content );
}
$maybeApplyFilter = self::maybeApplyTheContentFilter();
if ( in_array( 'divi-machine/divi-machine.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'Fortunato Pro' == apply_filters( 'current_theme', get_option( 'current_theme' ) ) ) {
update_option( 'ez-toc-post-content-core-level', $content );
}
Debug::log( 'the_content_filter', 'The `the_content` filter applied.', $maybeApplyFilter );
if ( ! $maybeApplyFilter ) {
return Debug::log()->appendTo( $content );
}
// Bail if post not eligible and widget is not active.
$isEligible = self::is_eligible( get_post() );
//More button
$options = array();
if (ezTOC_Option::get( 'ctrl_headings' ) == true) {
$options['view_more'] = ezTOC_Option::get( 'limit_headings_num' );
}
$isEligible = apply_filters('eztoc_do_shortcode',$isEligible);
if($isEligible){
if(!ez_toc_auto_device_target_status()){
$isEligible = false;
}
}
Debug::log( 'post_eligible', 'Post eligible.', $isEligible );
$return_only_an = false;
if(!$isEligible && (self::is_sidebar_hastoc() || is_active_widget( false, false, 'ezw_tco' ) || is_active_widget( false, false, 'ez_toc_widget_sticky' ) || ezTOC_Option::get('sticky-toggle') )){
$isEligible = true;
$return_only_an = true;
}
if ( ! $isEligible ) {
return Debug::log()->appendTo( $content );
}
$post = self::get( get_the_ID() );
if ( ! $post instanceof ezTOC_Post ) {
Debug::log( 'not_instance_of_post', 'Not an instance if `WP_Post`.', get_the_ID() );
return Debug::log()->appendTo( $content );
}
// Bail if no headings found.
if ( ! $post->hasTOCItems() ) {
return Debug::log()->appendTo( $content );
}
$find = $post->getHeadings();
$replace = $post->getHeadingsWithAnchors();
$toc = count($options) > 0 ? $post->getTOC($options) : $post->getTOC();
$headings = implode( PHP_EOL, $find );
$anchors = implode( PHP_EOL, $replace );
$headingRows = count( $find ) + 1;
$anchorRows = count( $replace ) + 1;
$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%;";
Debug::log(
'found_post_headings',
'Found headings:',
""
);
Debug::log(
'replace_post_headings',
'Replace found headings with:',
""
);
if ( $return_only_an ) {
Debug::log( 'side_bar_has shortcode', 'Shortcode found, add links to content.', true );
return mb_find_replace( $find, $replace, $content );
}
// If shortcode used or post not eligible, return content with anchored headings.
if ( strpos( $content, 'ez-toc-container' ) || ! $isEligible ) {
Debug::log( 'shortcode_found', 'Shortcode found, add links to content.', true );
return mb_find_replace( $find, $replace, $content );
}
$position = ezTOC_Option::get( 'position' );
Debug::log( 'toc_insert_position', 'Insert TOC at position', $position );
switch ( $position ) {
case 'top':
$content = $toc . mb_find_replace( $find, $replace, $content );
break;
case 'bottom':
$content = mb_find_replace( $find, $replace, $content ) . $toc;
break;
case 'after':
$replace[0] = $replace[0] . $toc;
$content = mb_find_replace( $find, $replace, $content );
break;
case 'afterpara':
$exc_blkqt = ezTOC_Option::get( 'blockqoute_checkbox' );
//blockqoute
$blockquotes = array();
if($exc_blkqt == true){
preg_match_all("/(.*?)<\/blockquote>/s", $content, $blockquotes);
if(!empty($blockquotes)){
$content = ez_toc_para_blockquote_replace($blockquotes, $content, 1);
}
}
$content = insertElementByPTag( mb_find_replace( $find, $replace, $content ), $toc );
//add blockqoute back
if($exc_blkqt == true && !empty($blockquotes)){
$content = ez_toc_para_blockquote_replace($blockquotes, $content, 2);
}
break;
case 'aftercustompara':
$exc_blkqt = ezTOC_Option::get( 'blockqoute_checkbox' );
//blockqoute
$blockquotes = array();
if($exc_blkqt == true){
preg_match_all("/(.*?)<\/blockquote>/s", $content, $blockquotes);
if(!empty($blockquotes)){
$content = ez_toc_para_blockquote_replace($blockquotes, $content, 1);
}
}
$paragraph_index = ezTOC_Option::get( 'custom_para_number' );
if($paragraph_index == 1){
$content = insertElementByPTag( mb_find_replace( $find, $replace, $content ), $toc );
}else if($paragraph_index > 1){
$closing_p = '
';
$paragraphs = explode( $closing_p, $content );
if(!empty($paragraphs) && is_array($paragraphs) && $paragraph_index <= count($paragraphs)){
$paragraph_id = $paragraph_index;
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
$pos = strpos($paragraph, ' 1){
$closing_img = '';
$imgs = explode( $closing_img, $content );
if(!empty($imgs) && is_array($imgs) && $img_index <= count($imgs)){
$img_id = $img_index;
foreach ($imgs as $index => $img) {
if ( trim( $img ) ) {
$imgs[$index] .= $closing_img;
}
$pos = strpos($img, '