This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,149 @@
<?php
if ( ! class_exists( 'TOC_Widget' ) ) :
class TOC_Widget extends WP_Widget {
public function __construct() {
$widget_options = [
'classname' => 'toc_widget',
'description' => __( 'Display the table of contents in the sidebar with this widget', 'table-of-contents-plus' ),
];
$control_options = [
'width' => 250,
'height' => 350,
'id_base' => 'toc-widget',
];
parent::__construct( 'toc-widget', 'TOC+', $widget_options, $control_options );
}
/**
* Widget output to the public
*/
public function widget( $args, $instance ) {
global $toc_plus, $wp_query;
if ( is_null( $wp_query->post ) ) {
return;
}
$html = '';
$items = '';
$custom_toc_position = '';
$find = [];
$replace = [];
$toc_options = $toc_plus->get_options();
$post = get_post( $wp_query->post->ID );
$custom_toc_position = strpos( $post->post_content, '[toc]' ); // at this point, shortcodes haven't run yet so we can't search for <!--TOC-->
if ( $toc_plus->is_eligible( $custom_toc_position ) ) {
$items = $toc_plus->extract_headings( $find, $replace, wptexturize( do_shortcode( $post->post_content ) ) );
$title = ( array_key_exists( 'title', $instance ) ) ? apply_filters( 'widget_title', $instance['title'] ) : '';
if ( false !== strpos( $title, '%PAGE_TITLE%' ) ) {
$title = str_replace( '%PAGE_TITLE%', get_the_title(), $title );
}
if ( false !== strpos( $title, '%PAGE_NAME%' ) ) {
$title = str_replace( '%PAGE_NAME%', get_the_title(), $title );
}
$hide_inline = $toc_options['show_toc_in_widget_only'];
$css_classes = '';
// bullets?
if ( $toc_options['bullet_spacing'] ) {
$css_classes .= ' have_bullets';
} else {
$css_classes .= ' no_bullets';
}
if ( $items ) {
// before widget (defined by themes)
$html .= $args['before_widget'];
// display the widget title if one was input (before and after titles defined by themes)
if ( $title ) {
$html .= $args['before_title'] . $title . $args['after_title'];
}
// display the list
$html .= '<ul class="toc_widget_list' . $css_classes . '">' . $items . '</ul>';
// after widget (defined by themes)
$html .= $args['after_widget'];
echo wp_kses_post( $html );
}
}
}
/**
* Update the widget settings
*/
public function update( $new_instance, $old_instance ) {
global $toc_plus;
$instance = $old_instance;
// strip tags for title to remove HTML (important for text inputs)
$instance['title'] = wp_strip_all_tags( trim( $new_instance['title'] ) );
// no need to strip tags for the following
//$instance['hide_inline'] = $new_instance['hide_inline'];
$toc_plus->set_show_toc_in_widget_only( $new_instance['hide_inline'] );
$toc_plus->set_show_toc_in_widget_only_post_types( (array) $new_instance['show_toc_in_widget_only_post_types'] );
return $instance;
}
/**
* Displays the widget settings on the widget panel.
*/
public function form( $instance ) {
global $toc_plus;
$toc_options = $toc_plus->get_options();
$defaults = [ 'title' => $toc_options['heading_text'] ];
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'table-of-contents-plus' ); ?>:</label>
<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" style="width: 100%;">
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked( $toc_options['show_toc_in_widget_only'], 1 ); ?> id="<?php echo esc_attr( $this->get_field_id( 'hide_inline' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_inline' ) ); ?>" value="1">
<label for="<?php echo esc_attr( $this->get_field_id( 'hide_inline' ) ); ?>"> <?php esc_html_e( 'Show the table of contents only in the sidebar', 'table-of-contents-plus' ); ?></label>
</p>
<div class="show_toc_in_widget_only_post_types" style="margin: 0 0 25px 25px; display: <?php echo ( 1 === $toc_options['show_toc_in_widget_only'] ) ? 'block' : 'none'; ?>;">
<p><?php esc_html_e( 'For the following content types:', 'table-of-contents-plus' ); ?></p>
<?php
foreach ( get_post_types() as $post_type ) {
// make sure the post type isn't on the exclusion list
if ( ! in_array( $post_type, $toc_plus->get_exclude_post_types(), true ) ) {
echo '<input type="checkbox" value="' . esc_attr( $post_type ) . '" id="' . esc_attr( $this->get_field_id( 'show_toc_in_widget_only_post_types_' . $post_type ) ) . '" name="' . esc_attr( $this->get_field_name( 'show_toc_in_widget_only_post_types' ) ) . '[]"';
if ( in_array( $post_type, $toc_options['show_toc_in_widget_only_post_types'], true ) ) {
echo ' checked="checked"';
}
echo ' /><label for="' . esc_attr( $this->get_field_id( 'show_toc_in_widget_only_post_types_' . $post_type ) ) . '"> ' . esc_html( $post_type ) . '</label><br />';
}
}
?>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#<?php echo esc_js( $this->get_field_id( 'hide_inline' ) ); ?>').click(function() {
$(this).parent().siblings('div.show_toc_in_widget_only_post_types').toggle('fast');
});
});
</script>
<?php
}
} // end class
endif;

View File

@ -0,0 +1,47 @@
<?php
/**
* Returns a HTML formatted string of the table of contents without the surrounding UL or OL
* tags to enable the theme editor to supply their own ID and/or classes to the outer list.
*
* There are three optional parameters you can feed this function with:
*
* - $content is the entire content with headings. If blank, will default to the current $post
*
* - $link is the URL to prefix the anchor with. If provided a string, will use it as the prefix.
* If set to true then will try to obtain the permalink from the $post object.
*
* - $apply_eligibility bool, defaults to false. When set to true, will apply the check to
* see if bit of content has the prerequisites needed for a TOC, eg minimum number of headings
* enabled post type, etc.
*/
function toc_get_index( $content = '', $prefix_url = '', $apply_eligibility = false ) {
global $wp_query, $toc_plus;
$return = '';
$find = [];
$replace = [];
$proceed = true;
if ( ! $content ) {
$post = get_post( $wp_query->post->ID );
$content = wptexturize( $post->post_content );
}
if ( $apply_eligibility ) {
if ( ! $toc_plus->is_eligible() ) {
$proceed = false;
}
} else {
$toc_plus->set_option( [ 'start' => 0 ] );
}
if ( $proceed ) {
$return = $toc_plus->extract_headings( $find, $replace, $content );
if ( $prefix_url ) {
$return = str_replace( 'href="#', 'href="' . $prefix_url . '#', $return );
}
}
return $return;
}

View File

@ -0,0 +1,26 @@
<?php
define( 'TOC_VERSION', '2309' );
define( 'TOC_POSITION_BEFORE_FIRST_HEADING', 1 );
define( 'TOC_POSITION_TOP', 2 );
define( 'TOC_POSITION_BOTTOM', 3 );
define( 'TOC_POSITION_AFTER_FIRST_HEADING', 4 );
define( 'TOC_MIN_START', 2 );
define( 'TOC_MAX_START', 10 );
define( 'TOC_SMOOTH_SCROLL_OFFSET', 30 );
define( 'TOC_WRAPPING_NONE', 0 );
define( 'TOC_WRAPPING_LEFT', 1 );
define( 'TOC_WRAPPING_RIGHT', 2 );
define( 'TOC_THEME_GREY', 1 );
define( 'TOC_THEME_LIGHT_BLUE', 2 );
define( 'TOC_THEME_WHITE', 3 );
define( 'TOC_THEME_BLACK', 4 );
define( 'TOC_THEME_TRANSPARENT', 99 );
define( 'TOC_THEME_CUSTOM', 100 );
define( 'TOC_DEFAULT_BACKGROUND_COLOUR', '#f9f9f9' );
define( 'TOC_DEFAULT_BORDER_COLOUR', '#aaaaaa' );
define( 'TOC_DEFAULT_TITLE_COLOUR', '#' );
define( 'TOC_DEFAULT_LINKS_COLOUR', '#' );
define( 'TOC_DEFAULT_LINKS_HOVER_COLOUR', '#' );
define( 'TOC_DEFAULT_LINKS_VISITED_COLOUR', '#' );
define( 'TOC_PLUGIN_PATH', plugins_url( '', __DIR__ ) );

View File

@ -0,0 +1,9 @@
<?php
require_once __DIR__ . '/globals.php';
require_once __DIR__ . '/class-toc-plus.php';
require_once __DIR__ . '/class-toc-widget.php';
require_once __DIR__ . '/functions.php';
// do the magic
$toc_plus = new TOC_Plus();