'ez-toc-widget-sticky', 'description' => __ ( 'Display the table of contents.', 'easy-table-of-contents' ) ); parent::__construct ( 'ez_toc_widget_sticky', __ ( 'Sticky Sidebar Table of Contents', 'easy-table-of-contents' ), $options ); add_action ( 'admin_enqueue_scripts', array( $this, 'enqueueScripts' ) ); add_action ( 'admin_footer-widgets.php', array( $this, 'printScripts' ), 9999 ); } /** * Callback which registers the widget with the Widget API. * * @access public * @since 2.0.41 * @static * * @return void */ public static function register () { register_widget ( __CLASS__ ); } /** * Callback to enqueue scripts on the Widgets admin page. * * @access private * @since 1 .0 * * @param string $hook_suffix */ public function enqueueScripts ( $hook_suffix ) { $min = defined ( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; if ( 'widgets.php' !== $hook_suffix ) { return; } wp_enqueue_style ( 'wp-color-picker' ); wp_enqueue_script ( 'wp-color-picker' ); wp_enqueue_script ( 'underscore' ); $widgetStickyAdminCSSVersion = ezTOC::VERSION . '-' . filemtime ( EZ_TOC_PATH . DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "css" . DIRECTORY_SEPARATOR . "ez-toc-widget-sticky-admin$min.css" ); wp_register_style ( 'ez-toc-widget-sticky-admin', EZ_TOC_URL . "assets/css/ez-toc-widget-sticky-admin$min.css", array(), $widgetStickyAdminCSSVersion ); wp_enqueue_style ( 'ez-toc-widget-sticky-admin', EZ_TOC_URL . "assets/css/ez-toc-widget-sticky-admin$min.css", array(), $widgetStickyAdminCSSVersion ); } /** * Callback to print the scripts to the Widgets admin page footer. * * @access private * @since 2.0.41 */ public function printScripts () { ?> hasTOCItems () ) { /** * @var string $before_widget * @var string $after_widget * @var string $before_title * @var string $after_title */ extract ( $args ); $js_vars = array(); $js_vars[ 'appearance_options' ] = ''; $js_vars[ 'advanced_options' ] = ''; $js_vars[ 'scroll_fixed_position' ] = '30'; $js_vars[ 'sidebar_sticky_title' ] = 120; $js_vars[ 'sidebar_sticky_title_size_unit' ] = '%'; $js_vars[ 'sidebar_sticky_title_weight' ] = '500'; $js_vars[ 'sidebar_sticky_title_color' ] = '#000'; $js_vars[ 'sidebar_width' ] = 'auto'; $js_vars[ 'sidebar_width_size_unit' ] = 'none'; $js_vars[ 'fixed_top_position' ] = '30'; $js_vars[ 'fixed_top_position_size_unit' ] = 'px'; $js_vars[ 'navigation_scroll_bar' ] = 'on'; $js_vars[ 'scroll_max_height' ] = 'auto'; $js_vars[ 'scroll_max_height_size_unit' ] = 'none'; if ( 'on' == $instance[ 'appearance_options' ] || 'on' == $instance[ 'advanced_options' ] || $js_vars[ 'scroll_fixed_position' ] != $instance[ 'scroll_fixed_position' ] || $js_vars[ 'scroll_fixed_position' ] != $instance[ 'scroll_fixed_position' ] || $js_vars[ 'sidebar_sticky_title' ] != $instance[ 'sidebar_sticky_title' ] || $js_vars[ 'sidebar_sticky_title_size_unit' ] != $instance[ 'sidebar_sticky_title_size_unit' ] || $js_vars[ 'sidebar_sticky_title_weight' ] != $instance[ 'sidebar_sticky_title_weight' ] || $js_vars[ 'sidebar_sticky_title_color' ] != $instance[ 'sidebar_sticky_title_color' ] || $js_vars[ 'sidebar_width' ] != $instance[ 'sidebar_width' ] || $js_vars[ 'sidebar_width_size_unit' ] != $instance[ 'sidebar_width_size_unit' ] || $js_vars[ 'fixed_top_position' ] != $instance[ 'fixed_top_position' ] || $js_vars[ 'fixed_top_position_size_unit' ] != $instance[ 'fixed_top_position_size_unit' ] || $js_vars[ 'navigation_scroll_bar' ] != $instance[ 'navigation_scroll_bar' ] || $js_vars[ 'scroll_max_height' ] != $instance[ 'scroll_max_height' ] || $js_vars[ 'scroll_max_height_size_unit' ] != $instance[ 'scroll_max_height_size_unit' ] ) { $js_vars[ 'appearance_options' ] = $instance[ 'appearance_options' ]; $js_vars[ 'advanced_options' ] = $instance[ 'advanced_options' ]; if ( empty ( $instance[ 'scroll_fixed_position' ] ) || ( ! empty ( $instance[ 'scroll_fixed_position' ] ) && ! is_int ( $instance[ 'scroll_fixed_position' ] ) && 'auto' != $instance[ 'scroll_fixed_position' ] ) ) $js_vars[ 'scroll_fixed_position' ] = '30'; else $js_vars[ 'scroll_fixed_position' ] = $instance[ 'scroll_fixed_position' ]; if ( empty ( $instance[ 'sidebar_sticky_title' ] ) || ( ! empty ( $instance[ 'sidebar_sticky_title' ] ) && ! is_int ( $instance[ 'sidebar_sticky_title' ] ) ) ) $js_vars[ 'sidebar_sticky_title' ] = 120; else $js_vars[ 'sidebar_sticky_title' ] = $instance[ 'sidebar_sticky_title' ]; if ( empty ( $instance[ 'sidebar_sticky_title_size_unit' ] ) || ( ! empty ( $instance[ 'sidebar_sticky_title_size_unit' ] ) ) ) $js_vars[ 'sidebar_sticky_title_size_unit' ] = '%'; else $js_vars[ 'sidebar_sticky_title_size_unit' ] = $instance[ 'sidebar_sticky_title_size_unit' ]; if ( empty ( $instance[ 'sidebar_sticky_title_weight' ] ) || ( ! empty ( $instance[ 'sidebar_sticky_title_weight' ] ) ) ) $js_vars[ 'sidebar_sticky_title_weight' ] = '500'; else $js_vars[ 'sidebar_sticky_title_weight' ] = $instance[ 'sidebar_sticky_title_weight' ]; if ( empty ( $instance[ 'sidebar_sticky_title_color' ] ) || ( ! empty ( $instance[ 'sidebar_sticky_title_color' ] ) ) ) $js_vars[ 'sidebar_sticky_title_color' ] = '#000'; else $js_vars[ 'sidebar_sticky_title_color' ] = $instance[ 'sidebar_sticky_title_color' ]; if ( empty ( $instance[ 'sidebar_width' ] ) || ( ! empty ( $instance[ 'sidebar_width' ] ) && ! is_int ( $instance[ 'sidebar_width' ] ) && 'auto' != $instance[ 'sidebar_width' ] ) ) $js_vars[ 'sidebar_width' ] = 'auto'; else $js_vars[ 'sidebar_width' ] = $instance[ 'sidebar_width' ]; $js_vars[ 'sidebar_width_size_unit' ] = $instance[ 'sidebar_width_size_unit' ]; if ( empty ( $instance[ 'fixed_top_position' ] ) || ( ! empty ( $instance[ 'fixed_top_position' ] ) && ! is_int ( $instance[ 'fixed_top_position' ] ) && '30' != $instance[ 'fixed_top_position' ] ) ) $js_vars[ 'fixed_top_position' ] = '30'; else $js_vars[ 'fixed_top_position' ] = $instance[ 'fixed_top_position' ]; $js_vars[ 'fixed_top_position_size_unit' ] = $instance[ 'fixed_top_position_size_unit' ]; $js_vars[ 'navigation_scroll_bar' ] = $instance[ 'navigation_scroll_bar' ]; if ( empty ( $instance[ 'scroll_max_height' ] ) || ( ! empty ( $instance[ 'scroll_max_height' ] ) && ! is_int ( $instance[ 'scroll_max_height' ] ) && 'auto' != $instance[ 'scroll_max_height' ] ) ) $js_vars[ 'scroll_max_height' ] = 'auto'; else $js_vars[ 'scroll_max_height' ] = $instance[ 'scroll_max_height' ]; $js_vars[ 'scroll_max_height_size_unit' ] = $instance[ 'scroll_max_height_size_unit' ]; } $class = array( 'ez-toc-widget-sticky-v' . str_replace ( '.', '_', ezTOC::VERSION ), 'ez-toc-widget-sticky', ); $title = apply_filters ( 'widget_title', $instance[ 'title' ], $instance, $this -> id_base ); if ( false !== strpos ( $title, '%PAGE_TITLE%' ) || false !== strpos ( $title, '%PAGE_NAME%' ) ) { $title = str_replace ( '%PAGE_TITLE%', get_the_title (), $title ); } if ( ezTOC_Option::get ( 'show_hierarchy' ) ) { $class[] = 'counter-hierarchy'; } else { $class[] = 'counter-flat'; } if ( ezTOC_Option::get ( 'heading-text-direction', 'ltr' ) == 'ltr' ) { $class[] = 'ez-toc-widget-sticky-container'; } if ( ezTOC_Option::get ( 'heading-text-direction', 'ltr' ) == 'rtl' ) { $class[] = 'ez-toc-widget-sticky-container-rtl'; } $class[] = 'ez-toc-widget-sticky-direction'; $custom_classes = ezTOC_Option::get ( 'css_container_class', '' ); if ( 0 < strlen ( $custom_classes ) ) { $custom_classes = explode ( ' ', $custom_classes ); $custom_classes = apply_filters ( 'ez_toc_widget_sticky_container_class', $custom_classes, $this ); if ( is_array ( $custom_classes ) ) { $class = array_merge ( $class, $custom_classes ); } } $class = array_filter ( $class ); $class = array_map ( 'trim', $class ); $class = array_map ( 'sanitize_html_class', $class ); echo $before_widget; do_action ( 'ez_toc_widget_sticky_before_widget_container' ); echo '
' . PHP_EOL; do_action ( 'ez_toc_widget_sticky_after_widget_container' ); echo $after_widget; // Enqueue the script. $widgetCSSVersion = ezTOC::VERSION . '-' . filemtime ( EZ_TOC_PATH . DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "css" . DIRECTORY_SEPARATOR . "ez-toc-widget-sticky$min.css" ); wp_register_style ( 'ez-toc-widget-sticky', EZ_TOC_URL . "assets/css/ez-toc-widget-sticky$min.css", array(), $widgetCSSVersion ); wp_enqueue_style ( 'ez-toc-widget-sticky', EZ_TOC_URL . "assets/css/ez-toc-widget-sticky$min.css", array(), $widgetCSSVersion ); wp_add_inline_style ( 'ez-toc-widget-sticky', ezTOC::InlineCountingCSS ( ezTOC_Option::get ( 'heading-text-direction', 'ltr' ), 'ez-toc-widget-sticky-direction', 'ez-toc-widget-sticky-container', 'counter', 'ez-toc-widget-sticky-container' ) ); $widgetJSVersion = ezTOC::VERSION . '-' . filemtime ( EZ_TOC_PATH . DIRECTORY_SEPARATOR . "assets" . DIRECTORY_SEPARATOR . "js" . DIRECTORY_SEPARATOR . "ez-toc-widget-sticky$min.js" ); wp_register_script ( 'ez-toc-widget-stickyjs', EZ_TOC_URL . "assets/js/ez-toc-widget-sticky$min.js", array( 'jquery' ), $widgetJSVersion ); wp_enqueue_script ( 'ez-toc-widget-stickyjs', EZ_TOC_URL . "assets/js/ez-toc-widget-sticky$min.js", array( 'jquery' ), $widgetJSVersion ); if ( 0 < count ( $js_vars ) ) { wp_localize_script ( 'ez-toc-widget-stickyjs', 'ezTocWidgetSticky', $js_vars ); } } } /** * Update the widget settings. * * @access private * @since 2.0.41 * * @param array $new_instance * @param array $old_instance * * @return array */ public function update ( $new_instance, $old_instance ) { $instance = $old_instance; $instance[ 'title' ] = strip_tags ( $new_instance[ 'title' ] ); $instance[ 'highlight_color' ] = strip_tags ( $new_instance[ 'highlight_color' ] ); $instance[ 'hide_inline' ] = array_key_exists ( 'hide_inline', $new_instance ) ? $new_instance[ 'hide_inline' ] : '0'; if ( isset ( $new_instance[ 'appearance_options' ] ) && $new_instance[ 'appearance_options' ] == 'on' ) { $instance[ 'sidebar_sticky_title' ] = ( int ) strip_tags ( $new_instance[ 'sidebar_sticky_title' ] ); $instance[ 'sidebar_sticky_title_size_unit' ] = strip_tags ( $new_instance[ 'sidebar_sticky_title_size_unit' ] ); $instance[ 'sidebar_sticky_title_weight' ] = strip_tags ( $new_instance[ 'sidebar_sticky_title_weight' ] ); $instance[ 'sidebar_sticky_title_color' ] = strip_tags ( $new_instance[ 'sidebar_sticky_title_color' ] ); } else { $instance[ 'sidebar_sticky_title' ] = 120; $instance[ 'sidebar_sticky_title_size_unit' ] = '%'; $instance[ 'sidebar_sticky_title_weight' ] = '500'; $instance[ 'sidebar_sticky_title_color' ] = '#000'; } if ( isset ( $new_instance[ 'advanced_options' ] ) && $new_instance[ 'advanced_options' ] == 'on' ) { $instance[ 'advanced_options' ] = 'on'; $instance[ 'scroll_fixed_position' ] = ( int ) strip_tags ( $new_instance[ 'scroll_fixed_position' ] ); $instance[ 'sidebar_width' ] = ( 'auto' == $new_instance[ 'sidebar_width' ] ) ? $new_instance[ 'sidebar_width' ] : ( int ) strip_tags ( $new_instance[ 'sidebar_width' ] ); $instance[ 'sidebar_width_size_unit' ] = strip_tags ( $new_instance[ 'sidebar_width_size_unit' ] ); $instance[ 'fixed_top_position' ] = ( 'auto' == $new_instance[ 'fixed_top_position' ] ) ? $new_instance[ 'fixed_top_position' ] : ( int ) strip_tags ( $new_instance[ 'fixed_top_position' ] ); $instance[ 'fixed_top_position_size_unit' ] = strip_tags ( $new_instance[ 'fixed_top_position_size_unit' ] ); $instance[ 'navigation_scroll_bar' ] = strip_tags ( $new_instance[ 'navigation_scroll_bar' ] ); $instance[ 'scroll_max_height' ] = ( 'auto' == $new_instance[ 'scroll_max_height' ] ) ? $new_instance[ 'scroll_max_height' ] : ( int ) strip_tags ( $new_instance[ 'scroll_max_height' ] ); $instance[ 'scroll_max_height_size_unit' ] = strip_tags ( $new_instance[ 'scroll_max_height_size_unit' ] ); } else { $instance[ 'advanced_options' ] = ''; $instance[ 'scroll_fixed_position' ] = 30; $instance[ 'sidebar_width' ] = 'auto'; $instance[ 'sidebar_width_size_unit' ] = 'none'; $instance[ 'fixed_top_position' ] = 30; $instance[ 'fixed_top_position_size_unit' ] = 'px'; $instance[ 'navigation_scroll_bar' ] = 'on'; $instance[ 'scroll_max_height' ] = 'auto'; $instance[ 'scroll_max_height_size_unit' ] = 'none'; } return $instance; } /** * Displays the widget settings on the Widgets admin page. * * @access private * @since 2.0.41 * * @param array $instance * * @return string|void */ public function form ( $instance ) { $defaults = array( 'highlight_color' => '#ededed', 'title' => 'Table of Contents', 'appearance_options' => '', 'advanced_options' => '', 'scroll_fixed_position' => 30, 'sidebar_sticky_title' => 120, 'sidebar_sticky_title_size_unit' => '%', 'sidebar_sticky_title_weight' => '500', 'sidebar_sticky_title_color' => '#000', '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', ); $instance = wp_parse_args ( ( array ) $instance, $defaults ); $highlight_color = esc_attr ( $instance[ 'highlight_color' ] ); $title_color = esc_attr ( $instance[ 'sidebar_sticky_title_color' ] ); ?>