> * @formatter :on * * @implements Widget * @implements WidgetWithId * @extends \WP_Widget * * @phpstan-ignore class.implementsDeprecatedInterface (Remove when minimum PRO version requires the basic version 9.8.0) */ class Category extends \WP_Widget implements Widget, WidgetWithId { /** * @use Checkbox */ use Checkbox; /** * @use Instance */ use Instance; use WidgetIdAccess; public const NAME = 'advanced_sidebar_menu_category'; public const TITLE = CategoryMenu::TITLE; public const INCLUDE_PARENT = CategoryMenu::INCLUDE_PARENT; public const INCLUDE_CHILDLESS_PARENT = CategoryMenu::INCLUDE_CHILDLESS_PARENT; public const ORDER_BY = CategoryMenu::ORDER_BY; public const EXCLUDE = CategoryMenu::EXCLUDE; public const DISPLAY_ALL = CategoryMenu::DISPLAY_ALL; public const LEVELS = CategoryMenu::LEVELS; public const DISPLAY_ON_SINGLE = CategoryMenu::DISPLAY_ON_SINGLE; public const POST_CATEGORY_LAYOUT = CategoryMenu::POST_CATEGORY_LAYOUT; /** * Default widget values. * * @var DEFAULTS */ protected static $defaults = [ self::TITLE => '', self::INCLUDE_PARENT => '', self::INCLUDE_CHILDLESS_PARENT => '', self::DISPLAY_ON_SINGLE => '', self::POST_CATEGORY_LAYOUT => 'widget', self::EXCLUDE => '', self::DISPLAY_ALL => '', self::LEVELS => '1', ]; /** * Register the widget. */ public function __construct() { $widget_ops = [ 'classname' => 'advanced-sidebar-menu advanced-sidebar-category', 'description' => __( 'Creates a menu of all the categories using the parent/child relationship', 'advanced-sidebar-menu' ), 'show_instance_in_rest' => true, ]; $control_ops = []; if ( ! wp_is_mobile() ) { $control_ops['width'] = 620; } parent::__construct( self::NAME, __( 'Advanced Sidebar - Categories', 'advanced-sidebar-menu' ), $widget_ops, $control_ops ); $this->hook(); } /** * Add the sections to the widget via actions. * * @notice Anything using the column actions must use the $widget class passed * via do_action instead of $this * * @return void */ protected function hook() { static $hooked; if ( null !== $hooked ) { return; } $hooked = true; add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_display' ], 5, 2 ); add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_display_on_single_posts', ], 15, 2 ); add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_exclude' ], 20, 2 ); } /** * Get the label for set taxonomy. * * For adjusting widget option labels. * * @since 8.2.0 * * @phpstan-param CATEGORY_SETTINGS $instance * * @param array $instance - Widget settings. * @param bool $single - Singular label or plural. * * @return string */ public function get_taxonomy_label( array $instance, $single = true ): string { $taxonomy = get_taxonomy( apply_filters( 'advanced-sidebar-menu/widget/category/taxonomy-for-label', 'category', $this->control_options, $instance ) ); if ( false === $taxonomy ) { $taxonomy = get_taxonomy( 'category' );// Sensible fallback. if ( false === $taxonomy ) { return $single ? __( 'Category', 'advanced-sidebar-menu' ) : __( 'Categories', 'advanced-sidebar-menu' ); } } return $single ? $taxonomy->labels->singular_name : $taxonomy->labels->name; } /** * Get the list of display each single post's category options. * * @return array{widget: string, list: string} */ public static function get_display_each_options() { return [ CategoryMenu::EACH_WIDGET => __( 'In a new widget', 'advanced-sidebar-menu' ), CategoryMenu::EACH_LIST => __( 'In another list in the same widget', 'advanced-sidebar-menu' ), ]; } /** * Display options. * * @phpstan-param CATEGORY_SETTINGS $instance * * @param array $instance - Widget settings. * @param Category $widget - Registered widget arguments. * * @return void */ public function box_display( array $instance, Category $widget ) { ?>

checkbox( self::INCLUDE_PARENT ); ?>

checkbox( self::INCLUDE_CHILDLESS_PARENT ); ?>

checkbox( self::DISPLAY_ALL, self::LEVELS ); ?>

hide_element( self::DISPLAY_ALL, self::LEVELS ); ?>>

checkbox( self::DISPLAY_ON_SINGLE, self::POST_CATEGORY_LAYOUT ); ?>

hide_element( self::DISPLAY_ON_SINGLE, self::POST_CATEGORY_LAYOUT ); ?>>

set_instance( $instance, static::$defaults ); do_action( 'advanced-sidebar-menu/widget/category/before-form', $settings, $this ); ?>

set_instance( $instance, static::$defaults ); $menu = CategoryMenu::factory( $settings, $args ); do_action( 'advanced-sidebar-menu/widget/before-render', $menu, $this ); $menu->render(); do_action( 'advanced-sidebar-menu/widget/after-render', $menu, $this ); } }