PluginProbe
Parse.ly / 3.3.2
Parse.ly v3.3.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / UI / class-recommended-widget.php

class-recommended-widget.php in Parse.ly 3.3.2, at src/UI/class-recommended-widget.php

335 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UI: Recommended Widget class
4 *
5 * Provides a widget with Parse.ly recommended articles.
6 *
7 * @package Parsely
8 */
9
10 declare(strict_types=1);
11
12 namespace Parsely\UI;
13
14 use WP_Widget;
15
16 use const Parsely\PARSELY_FILE;
17
18 /**
19 * Provides a widget with Parse.ly recommended articles.
20 */
21 final class Recommended_Widget extends WP_Widget {
22 /**
23 * Constructor.
24 */
25 public function __construct() {
26 parent::__construct(
27 'Parsely_Recommended_Widget',
28 __( 'Parse.ly Recommended Widget', 'wp-parsely' ),
29 array(
30 'classname' => 'Recommended_Widget parsely-recommended-widget-hidden',
31 'description' => __( 'Display a list of post recommendations, personalized for a visitor or the current post.', 'wp-parsely' ),
32 )
33 );
34 }
35
36 /**
37 * Gets the URL for the Recommendations API (GET /related).
38 *
39 * @see https://www.parse.ly/help/api/recommendations#get-related
40 *
41 * @internal While this is a public method now, this should be moved to a new class.
42 *
43 * @since 2.5.0
44 *
45 * @param string $api_key Publisher Site ID (API key).
46 * @param int|null $published_within Publication filter start date; see https://www.parse.ly/help/api/time for
47 * formatting details. No restriction by default.
48 * @param string|null $sort What to sort the results by. There are currently 2 valid options: `score`,
49 * which will sort articles by overall relevance and `pub_date` which will sort
50 * results by their publication date. The default is `score`.
51 * @param string|null $boost Available for sort=score only. Sub-sort value to re-rank relevant posts that
52 * received high e.g. views; default is undefined.
53 * @param int $return_limit Number of records to retrieve; defaults to "10".
54 * @return string API URL.
55 */
56 private function get_api_url( string $api_key, ?int $published_within, ?string $sort, ?string $boost, int $return_limit ): string {
57 $related_api_endpoint = 'https://api.parsely.com/v2/related';
58
59 $query_args = array(
60 'apikey' => $api_key,
61 'sort' => $sort,
62 'limit' => $return_limit,
63 );
64
65 if ( 'score' === $sort && 'no-boost' !== $boost ) {
66 $query_args['boost'] = $boost;
67 }
68
69 if ( null !== $published_within && 0 !== $published_within ) {
70 $query_args['pub_date_start'] = $published_within . 'd';
71 }
72
73 return add_query_arg( $query_args, $related_api_endpoint );
74 }
75
76 /**
77 * This is the widget function.
78 *
79 * @param array $args Widget Arguments.
80 * @param array $instance Values saved to the db.
81 */
82 public function widget( $args, $instance ): void {
83 if ( ! $this->api_key_and_secret_are_populated() ) {
84 return;
85 }
86
87 $removed_title_esc = remove_filter( 'widget_title', 'esc_html' );
88
89 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
90 $title = apply_filters( 'widget_title', $instance['title'] );
91
92 if ( $removed_title_esc ) {
93 add_filter( 'widget_title', 'esc_html' );
94 }
95
96 $title_html = $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
97 echo wp_kses_post( $title_html );
98
99 // Set up the variables.
100 $options = get_option( 'parsely' );
101 $api_url = $this->get_api_url(
102 $options['apikey'],
103 $instance['published_within'],
104 $instance['sort'],
105 $instance['boost'],
106 (int) $instance['return_limit']
107 );
108
109 $recommended_widget_script_asset = require plugin_dir_path( PARSELY_FILE ) . 'build/recommended-widget.asset.php';
110
111 ?>
112
113 <div class="parsely-recommended-widget"
114 data-parsely-widget-display-author="<?php echo esc_attr( wp_json_encode( isset( $instance['display_author'] ) && $instance['display_author'] ) ); ?>"
115 data-parsely-widget-display-direction="<?php echo esc_attr( $instance['display_direction'] ?? '' ); ?>"
116 data-parsely-widget-api-url="<?php echo esc_url( $api_url ); ?>"
117 data-parsely-widget-img-display="<?php echo esc_attr( $instance['img_src'] ?? '' ); ?>"
118 data-parsely-widget-permalink="<?php echo esc_url( get_permalink() ); ?>"
119 data-parsely-widget-personalized="<?php echo esc_attr( wp_json_encode( isset( $instance['personalize_results'] ) && $instance['personalize_results'] ) ); ?>"
120 data-parsely-widget-id="<?php echo esc_attr( $this->id ); ?>"
121 ></div>
122
123 <?php
124
125 wp_register_script(
126 'wp-parsely-recommended-widget',
127 plugin_dir_url( PARSELY_FILE ) . 'build/recommended-widget.js',
128 $recommended_widget_script_asset['dependencies'],
129 $recommended_widget_script_asset['version'],
130 true
131 );
132
133 wp_register_style(
134 'wp-parsely-recommended-widget',
135 plugin_dir_url( PARSELY_FILE ) . 'build/recommended-widget.css',
136 array(),
137 $recommended_widget_script_asset['version']
138 );
139
140 wp_enqueue_script( 'wp-parsely-recommended-widget' );
141 wp_enqueue_style( 'wp-parsely-recommended-widget' );
142
143 echo wp_kses_post( $args['after_widget'] );
144 }
145
146 /**
147 * This is the form function.
148 *
149 * @param array $instance Values saved to the db.
150 */
151 public function form( $instance ): void {
152 if ( ! $this->api_key_and_secret_are_populated() ) {
153 $settings_page_url = add_query_arg( 'page', 'parsely', get_admin_url() . 'options-general.php' );
154
155 $message = sprintf(
156 /* translators: %s: Plugin settings page URL */
157 __( 'The <i>Parse.ly Site ID</i> and <i>Parse.ly API Secret</i> fields need to be populated on the <a href="%s">Parse.ly settings page</a> for this widget to work.', 'wp-parsely' ),
158 esc_url( $settings_page_url )
159 );
160
161 echo '<p>', wp_kses_post( $message ), '</p>';
162
163 return;
164 }
165
166 // editable fields: title.
167 $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
168 $return_limit = ! empty( $instance['return_limit'] ) ? (int) $instance['return_limit'] : 5;
169 $display_direction = ! empty( $instance['display_direction'] ) ? $instance['display_direction'] : 'vertical';
170 $published_within = ! empty( $instance['published_within'] ) ? $instance['published_within'] : 0;
171 $sort = ! empty( $instance['sort'] ) ? $instance['sort'] : 'score';
172 $boost = ! empty( $instance['boost'] ) ? $instance['boost'] : 'views';
173 $personalize_results = ! empty( $instance['personalize_results'] ) ? $instance['personalize_results'] : false;
174 $img_src = ! empty( $instance['img_src'] ) ? $instance['img_src'] : 'parsely_thumb';
175 $display_author = ! empty( $instance['display_author'] ) ? $instance['display_author'] : false;
176
177 $instance['return_limit'] = $return_limit;
178 $instance['display_direction'] = $display_direction;
179 $instance['published_within'] = $published_within;
180 $instance['sort'] = $sort;
181 $instance['boost'] = $boost;
182 $instance['personalize_results'] = $personalize_results;
183 $instance['img_src'] = $img_src;
184 $instance['display_author'] = $display_author;
185
186 $boost_params = $this->get_boost_params();
187 ?>
188 <p>
189 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'wp-parsely' ); ?></label>
190 <br>
191 <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( $title ); ?>" class="widefat" />
192 </p>
193 <p>
194 <label for="<?php echo esc_attr( $this->get_field_id( 'published_within' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'published_within_label' ) ); ?>"><?php esc_html_e( 'Published within', 'wp-parsely' ); ?></label>
195 <input type="number" id="<?php echo esc_attr( $this->get_field_id( 'published_within' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'published_within' ) ); ?>" value="<?php echo esc_attr( (string) $instance['published_within'] ); ?>" min="0" max="30"
196 class="tiny-text" aria-labelledby="<?php echo esc_attr( $this->get_field_id( 'published_within_label' ) ); ?> <?php echo esc_attr( $this->get_field_id( 'published_within' ) ); ?> <?php echo esc_attr( $this->get_field_id( 'published_within_unit' ) ); ?>" />
197 <span id="<?php echo esc_attr( $this->get_field_id( 'published_within_unit' ) ); ?>"> <?php esc_html_e( 'days (0 for no limit).', 'wp-parsely' ); ?></span>
198 </p>
199 <p>
200 <label for="<?php echo esc_attr( $this->get_field_id( 'return_limit' ) ); ?>"><?php esc_html_e( 'Number of posts to show (max 20):', 'wp-parsely' ); ?></label>
201 <input type="number" id="<?php echo esc_attr( $this->get_field_id( 'return_limit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'return_limit' ) ); ?>" value="<?php echo esc_attr( $instance['return_limit'] ); ?>" min="1" max="20" class="tiny-text" />
202 </p>
203 <p>
204 <fieldset>
205 <legend><?php esc_html_e( 'Display entries:', 'wp-parsely' ); ?></legend>
206 <p>
207 <input type="radio" id="<?php echo esc_attr( $this->get_field_id( 'display_direction_horizontal' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_direction' ) ); ?>"<?php checked( $instance['display_direction'], 'horizontal' ); ?> value="horizontal" />
208 <label for="<?php echo esc_attr( $this->get_field_id( 'display_direction_horizontal' ) ); ?>"><?php esc_html_e( 'Horizontally', 'wp-parsely' ); ?></label>
209 <br />
210 <input type="radio" id="<?php echo esc_attr( $this->get_field_id( 'display_direction_vertical' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_direction' ) ); ?>"<?php checked( $instance['display_direction'], 'vertical' ); ?> value="vertical" />
211 <label for="<?php echo esc_attr( $this->get_field_id( 'display_direction_vertical' ) ); ?>"><?php esc_html_e( 'Vertically', 'wp-parsely' ); ?></label>
212 </p>
213 </fieldset>
214 </p>
215 <p>
216 <label for="<?php echo esc_attr( $this->get_field_id( 'sort' ) ); ?>"><?php esc_html_e( 'Sort by:', 'wp-parsely' ); ?></label>
217 <br>
218 <select id="<?php echo esc_attr( $this->get_field_id( 'sort' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>" class="widefat">
219 <option<?php selected( $instance['sort'], 'score' ); ?> value="score"><?php esc_html_e( 'Score (relevancy, boostable)', 'wp-parsely' ); ?></option>
220 <option<?php selected( $instance['sort'], 'pub_date' ); ?> value="pub_date"><?php esc_html_e( 'Publish date (not boostable)', 'wp-parsely' ); ?></option>
221 </select>
222 </p>
223 <p>
224 <label for="<?php echo esc_attr( $this->get_field_id( 'boost' ) ); ?>"><?php esc_html_e( 'Boost by:', 'wp-parsely' ); ?></label>
225 <br>
226 <select id="<?php echo esc_attr( $this->get_field_id( 'boost' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'boost' ) ); ?>" class="widefat">
227 <?php foreach ( $boost_params as $boost_param => $description ) { ?>
228 <option<?php selected( $instance['boost'], $boost_param ); ?> value="<?php echo esc_attr( $boost_param ); ?>"><?php echo esc_html( $description ); ?></option>
229 <?php } ?>
230 </select>
231
232 </p>
233 <p>
234 <label for="<?php echo esc_attr( $this->get_field_id( 'img_src' ) ); ?>"><?php esc_html_e( 'Image source:', 'wp-parsely' ); ?></label>
235 <br>
236 <select id="<?php echo esc_attr( $this->get_field_id( 'img_src' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'img_src' ) ); ?>" class="widefat">
237 <option<?php selected( $instance['img_src'], 'parsely_thumb' ); ?> value="parsely_thumb"><?php esc_html_e( 'Parse.ly generated thumbnail (85x85px)', 'wp-parsely' ); ?></option>
238 <option<?php selected( $instance['img_src'], 'original' ); ?> value="original"><?php esc_html_e( 'Original image', 'wp-parsely' ); ?></option>
239 <option<?php selected( $instance['img_src'], 'none' ); ?> value="none"><?php esc_html_e( 'No image', 'wp-parsely' ); ?></option>
240 </select>
241 </p>
242 <p>
243 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'display_author' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_author' ) ); ?>" value="display_author"<?php checked( $instance['display_author'], 'display_author' ); ?> />
244 <label for="<?php echo esc_attr( $this->get_field_id( 'display_author' ) ); ?>"><?php esc_html_e( 'Display author', 'wp-parsely' ); ?></label>
245 <br />
246 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'personalize_results' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'personalize_results' ) ); ?>" value="personalize_results"<?php checked( $instance['personalize_results'], 'personalize_results' ); ?> />
247 <label for="<?php echo esc_attr( $this->get_field_id( 'personalize_results' ) ); ?>"><?php esc_html_e( 'Personalize recommended results', 'wp-parsely' ); ?></label>
248 </p>
249 <?php
250 }
251
252 /**
253 * This is the update function.
254 *
255 * @param array $new_instance The new values for the db.
256 * @param array $old_instance Values saved to the db.
257 * @return array
258 */
259 public function update( $new_instance, $old_instance ): array {
260 $instance = $old_instance;
261 $instance['title'] = trim( wp_kses_post( $new_instance['title'] ) );
262 $instance['published_within'] = is_int( $new_instance['published_within'] ) ? $new_instance['published_within'] : (int) trim( $new_instance['published_within'] );
263 $instance['return_limit'] = (int) $new_instance['return_limit'] <= 20 ? (int) $new_instance['return_limit'] : 20;
264 $instance['display_direction'] = trim( $new_instance['display_direction'] );
265 $instance['sort'] = trim( $new_instance['sort'] );
266 $instance['boost'] = trim( $new_instance['boost'] );
267 $instance['display_author'] = $new_instance['display_author'];
268 $instance['personalize_results'] = $new_instance['personalize_results'];
269 $instance['img_src'] = trim( $new_instance['img_src'] );
270 return $instance;
271 }
272
273 /**
274 * Returns the list of boost parameters, values and labels.
275 *
276 * @since 2.5.0
277 *
278 * @return array<string, string> Boost parameters values and labels.
279 */
280 private function get_boost_params(): array {
281 return array(
282 'no-boost' => __( 'No boost', 'wp-parsely' ),
283 'views' => __( 'Page views', 'wp-parsely' ),
284 'mobile_views' => __( 'Page views on mobile devices', 'wp-parsely' ),
285 'tablet_views' => __( 'Page views on tablet devices', 'wp-parsely' ),
286 'desktop_views' => __( 'Page views on desktop devices', 'wp-parsely' ),
287 'visitors' => __( 'Unique page visitors, total', 'wp-parsely' ),
288 'visitors_new' => __( 'New visitors', 'wp-parsely' ),
289 'visitors_returning' => __( 'Returning visitors', 'wp-parsely' ),
290 'engaged_minutes' => __( 'Total engagement time in minutes', 'wp-parsely' ),
291 'avg_engaged' => __( 'Engaged minutes spent by total visitors', 'wp-parsely' ),
292 'avg_engaged_new' => __( 'Average engaged minutes spent by new visitors', 'wp-parsely' ),
293 'avg_engaged_returning' => __( 'Average engaged minutes spent by returning visitors', 'wp-parsely' ),
294 'social_interactions' => __( 'Total social interactions', 'wp-parsely' ),
295 'fb_interactions' => __( 'Count of Facebook shares, likes, and comments', 'wp-parsely' ),
296 'tw_interactions' => __( 'Count of Twitter tweets and retweets', 'wp-parsely' ),
297 'pi_interactions' => __( 'Count of Pinterest pins', 'wp-parsely' ),
298 'social_referrals' => __( 'Page views where the referrer was any social network', 'wp-parsely' ),
299 'fb_referrals' => __( 'Page views where the referrer was facebook.com', 'wp-parsely' ),
300 'tw_referrals' => __( 'Page views where the referrer was twitter.com', 'wp-parsely' ),
301 'pi_referrals' => __( 'Page views where the referrer was pinterest.com', 'wp-parsely' ),
302 );
303 }
304
305 /**
306 * Checks if both the API key and API secret settings are populated with
307 * non-empty values.
308 *
309 * @since 2.5.0
310 *
311 * @return bool True if apikey and api_secret settings are not empty strings.
312 * False otherwise.
313 */
314 private function api_key_and_secret_are_populated(): bool {
315 $options = get_option( 'parsely' );
316
317 // No options are saved, so API key is not available.
318 if ( ! is_array( $options ) ) {
319 return false;
320 }
321
322 // Parse.ly Site ID settings field is not populated.
323 if ( ! array_key_exists( 'apikey', $options ) || '' === $options['apikey'] ) {
324 return false;
325 }
326
327 // Parse.ly API Secret settings field is not populated.
328 if ( ! array_key_exists( 'api_secret', $options ) || '' === $options['api_secret'] ) {
329 return false;
330 }
331
332 return true;
333 }
334 }
335