PluginProbe
Parse.ly / 3.0.0
Parse.ly v3.0.0
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.0.0, at src/UI/class-recommended-widget.php

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