PluginProbe
ElasticPress / 4.2.2
ElasticPress v4.2.2
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / Feature / Facets / Widget.php

Widget.php in ElasticPress 4.2.2, at includes/classes/Feature/Facets/Widget.php

190 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Facets widget
4 *
5 * @package elasticpress
6 */
7
8 namespace ElasticPress\Feature\Facets;
9
10 use \WP_Widget as WP_Widget;
11 use ElasticPress\Features as Features;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * Facets widget class
19 */
20 class Widget extends WP_Widget {
21 /**
22 * The renderer instance.
23 *
24 * @var Renderer
25 */
26 public $renderer;
27
28 /**
29 * Create widget
30 */
31 public function __construct() {
32 $options = array( 'description' => esc_html__( 'Add a facet to an archive or search results page.', 'elasticpress' ) );
33 parent::__construct( 'ep-facet', esc_html__( 'ElasticPress - Facet', 'elasticpress' ), $options );
34
35 $this->renderer = new Renderer();
36 }
37
38 /**
39 * Output widget
40 *
41 * @param array $args Widget args
42 * @param array $instance Instance settings
43 * @since 2.5, 4.2.0 made a wrapper for the renderer call.
44 */
45 public function widget( $args, $instance ) {
46 $this->renderer->render( $args, $instance );
47 }
48
49 /**
50 * Get the markup for an individual facet item.
51 *
52 * @param WP_Term $term Term object.
53 * @param string $url Filter URL.
54 * @param boolean $selected Whether the term is currently selected.
55 * @since 3.6.3, 4.2.0 deprecated in favor of a method in the renderer.
56 * @return string HTML for an individual facet term.
57 */
58 protected function get_facet_term_html( $term, $url, $selected = false ) {
59 _deprecated_function( __FUNCTION__, '4.2.0', '$this->renderer->get_facet_term_html()' );
60
61 return $this->renderer->get_facet_term_html( $term, $url, $selected );
62 }
63
64 /**
65 * Output widget form
66 *
67 * @param array $instance Instance settings
68 * @since 2.5
69 */
70 public function form( $instance ) {
71 $dashboard_url = admin_url( 'admin.php?page=elasticpress' );
72
73 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
74 $dashboard_url = network_admin_url( 'admin.php?page=elasticpress' );
75 }
76
77 $feature = Features::factory()->get_registered_feature( 'facets' );
78 $settings = [];
79
80 if ( $feature ) {
81 $settings = $feature->get_settings();
82 }
83
84 $settings = wp_parse_args(
85 $settings,
86 array(
87 'match_type' => 'all',
88 )
89 );
90
91 $set = esc_html__( 'all', 'elasticpress' );
92 $not_set = esc_html__( 'any', 'elasticpress' );
93
94 if ( 'any' === $settings['match_type'] ) {
95 $set = esc_html__( 'any', 'elasticpress' );
96 $not_set = esc_html__( 'all', 'elasticpress' );
97 }
98
99 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
100 $facet = ( ! empty( $instance['facet'] ) ) ? $instance['facet'] : '';
101 $orderby = ( ! empty( $instance['orderby'] ) ) ? $instance['orderby'] : '';
102 $order = ( ! empty( $instance['order'] ) ) ? $instance['order'] : '';
103
104 $taxonomies = $feature->get_facetable_taxonomies();
105
106 $orderby_options = [
107 'count' => __( 'Count', 'elasticpress' ),
108 'name' => __( 'Term Name', 'elasticpress' ),
109 ];
110
111 $order_options = [
112 'desc' => __( 'Descending', 'elasticpress' ),
113 'asc' => __( 'Ascending', 'elasticpress' ),
114 ];
115
116 ?>
117 <div class="widget-ep-facet">
118 <p>
119 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
120 <?php esc_html_e( 'Title:', 'elasticpress' ); ?>
121 </label>
122 <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 ); ?>" />
123 </p>
124
125 <p>
126 <label for="<?php echo esc_attr( $this->get_field_id( 'facet' ) ); ?>">
127 <?php esc_html_e( 'Taxonomy:', 'elasticpress' ); ?>
128 </label><br>
129
130 <select id="<?php echo esc_attr( $this->get_field_id( 'facet' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'facet' ) ); ?>">
131 <?php foreach ( $taxonomies as $slug => $taxonomy_object ) : ?>
132 <option <?php selected( $facet, $taxonomy_object->name ); ?> value="<?php echo esc_attr( $taxonomy_object->name ); ?>"><?php echo esc_html( $taxonomy_object->labels->name ); ?></option>
133 <?php endforeach; ?>
134 </select>
135 </p>
136
137 <p>
138 <label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>">
139 <?php esc_html_e( 'Order Terms By:', 'elasticpress' ); ?>
140 </label><br>
141
142 <select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"
143 name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
144 <?php foreach ( $orderby_options as $name => $title ) : ?>
145 <option <?php selected( $orderby, $name ); ?>
146 value="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $title ); ?></option>
147 <?php endforeach; ?>
148 </select>
149 </p>
150
151 <p>
152 <label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>">
153 <?php esc_html_e( 'Term Order:', 'elasticpress' ); ?>
154 </label><br>
155
156 <select id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"
157 name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
158 <?php foreach ( $order_options as $name => $title ) : ?>
159 <option <?php selected( $order, $name ); ?>
160 value="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $title ); ?></option>
161 <?php endforeach; ?>
162 </select>
163 </p>
164
165 <?php // translators: "all" or "any", depending on configuration values, 3: URL ?>
166 <p><?php echo wp_kses_post( sprintf( __( 'Faceting will filter out any content that is not tagged to all selected terms; change this to show <strong>%1$s</strong> content tagged to <strong>%2$s</strong> selected term in <a href="%3$s">ElasticPress settings</a>.', 'elasticpress' ), $set, $not_set, esc_url( $dashboard_url ) ) ); ?></p>
167 </div>
168
169 <?php
170 }
171
172 /**
173 * Sanitize fields
174 *
175 * @param array $new_instance New instance settings
176 * @param array $old_instance Old instance settings
177 * @since 2.5
178 */
179 public function update( $new_instance, $old_instance ) {
180 $instance = [];
181
182 $instance['title'] = sanitize_text_field( $new_instance['title'] );
183 $instance['facet'] = sanitize_text_field( $new_instance['facet'] );
184 $instance['orderby'] = sanitize_text_field( $new_instance['orderby'] );
185 $instance['order'] = sanitize_text_field( $new_instance['order'] );
186
187 return $instance;
188 }
189 }
190