|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WZ Knowledge Base Products Widget class. |
| 4 | + * |
| 5 | + * @package WebberZone\Knowledge_Base |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WebberZone\Knowledge_Base\Widgets; |
| 9 | + |
| 10 | +use WebberZone\Knowledge_Base\Frontend\Display; |
| 11 | + |
| 12 | +if ( ! defined( 'WPINC' ) ) { |
| 13 | + die; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Create a WordPress Products Widget for WZ Knowledge Base. |
| 18 | + * |
| 19 | + * @since 2.3.0 |
| 20 | + */ |
| 21 | +class Products_Widget extends \WP_Widget { |
| 22 | + /** |
| 23 | + * Register widget with WordPress. |
| 24 | + */ |
| 25 | + public function __construct() { |
| 26 | + parent::__construct( |
| 27 | + 'widget_wzkb_products', |
| 28 | + __( 'WZKB Products', 'knowledgebase' ), |
| 29 | + array( |
| 30 | + 'description' => __( 'Display sections under a selected product for the Knowledge Base.', 'knowledgebase' ), |
| 31 | + 'customize_selective_refresh' => true, |
| 32 | + 'show_instance_in_rest' => true, |
| 33 | + ) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Back-end widget form. |
| 39 | + * |
| 40 | + * @see WP_Widget::form() |
| 41 | + * |
| 42 | + * @param array $instance Previously saved values from database. |
| 43 | + */ |
| 44 | + public function form( $instance ) { |
| 45 | + $title = isset( $instance['title'] ) ? $instance['title'] : ''; |
| 46 | + $product_id = isset( $instance['product_id'] ) ? (int) $instance['product_id'] : 0; |
| 47 | + $depth = isset( $instance['depth'] ) ? (int) $instance['depth'] : 0; |
| 48 | + |
| 49 | + $products = get_terms( |
| 50 | + array( |
| 51 | + 'taxonomy' => 'wzkb_product', |
| 52 | + 'hide_empty' => false, |
| 53 | + ) |
| 54 | + ); |
| 55 | + ?> |
| 56 | + <p> |
| 57 | + <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> |
| 58 | + <?php esc_html_e( 'Title:', 'knowledgebase' ); ?> |
| 59 | + <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 60 | + </label> |
| 61 | + </p> |
| 62 | + <p> |
| 63 | + <label for="<?php echo esc_attr( $this->get_field_id( 'product_id' ) ); ?>"> |
| 64 | + <?php esc_html_e( 'Product:', 'knowledgebase' ); ?> |
| 65 | + <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'product_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'product_id' ) ); ?>"> |
| 66 | + <option value="0"><?php esc_html_e( 'Select a product', 'knowledgebase' ); ?></option> |
| 67 | + <?php foreach ( $products as $product ) : ?> |
| 68 | + <option value="<?php echo esc_attr( (string) $product->term_id ); ?>" <?php selected( $product_id, $product->term_id ); ?>><?php echo esc_html( $product->name ); ?></option> |
| 69 | + <?php endforeach; ?> |
| 70 | + </select> |
| 71 | + </label> |
| 72 | + </p> |
| 73 | + <p> |
| 74 | + <label for="<?php echo esc_attr( $this->get_field_id( 'depth' ) ); ?>"> |
| 75 | + <?php esc_html_e( 'Max Depth (0 for unlimited):', 'knowledgebase' ); ?> |
| 76 | + <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'depth' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>" type="number" value="<?php echo esc_attr( (string) $depth ); ?>" min="0" /> |
| 77 | + </label> |
| 78 | + </p> |
| 79 | + <?php |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Sanitize widget form values as they are saved. |
| 84 | + * |
| 85 | + * @see WP_Widget::update() |
| 86 | + * |
| 87 | + * @param array $new_instance Values just sent to be saved. |
| 88 | + * @param array $old_instance Previously saved values from database. |
| 89 | + * @return array Updated safe values to be saved. |
| 90 | + */ |
| 91 | + public function update( $new_instance, $old_instance ) { |
| 92 | + $instance = array(); |
| 93 | + $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 94 | + $instance['product_id'] = isset( $new_instance['product_id'] ) ? (int) $new_instance['product_id'] : 0; |
| 95 | + $instance['depth'] = isset( $new_instance['depth'] ) ? (int) $new_instance['depth'] : 0; |
| 96 | + return $instance; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Front-end display of widget. |
| 101 | + * |
| 102 | + * @see WP_Widget::widget() |
| 103 | + * |
| 104 | + * @param array $args Widget arguments. |
| 105 | + * @param array $instance Saved values from database. |
| 106 | + */ |
| 107 | + public function widget( $args, $instance ) { |
| 108 | + if ( ! isset( $args['widget_id'] ) ) { |
| 109 | + $args['widget_id'] = $this->id; |
| 110 | + } |
| 111 | + |
| 112 | + // Return if not a WZKB post type archive or single page. |
| 113 | + if ( ! is_post_type_archive( 'wz_knowledgebase' ) && ! is_singular( 'wz_knowledgebase' ) && ! is_tax( 'wzkb_category' ) && ! is_tax( 'wzkb_tag' ) ) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : ''; |
| 118 | + $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 119 | + $product_id = ! empty( $instance['product_id'] ) ? (int) $instance['product_id'] : 0; |
| 120 | + $depth = ! empty( $instance['depth'] ) ? (int) $instance['depth'] : 0; |
| 121 | + |
| 122 | + if ( ! $product_id ) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + $output = ''; |
| 127 | + $output .= $args['before_widget']; |
| 128 | + if ( ! empty( $title ) ) { |
| 129 | + $output .= $args['before_title'] . esc_html( $title ) . $args['after_title']; |
| 130 | + } |
| 131 | + $output .= Display::get_product_sections_list( $product_id, array( 'depth' => $depth ) ); |
| 132 | + $output .= $args['after_widget']; |
| 133 | + |
| 134 | + echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 135 | + } |
| 136 | +} |
0 commit comments