Yet Another Related Posts Plugin (YARPP) - Version 5.3.0

Version Description

(2020-07-29) = * New: REST API support (documentation) * Enhancement: WP Rest Cache Plugin support * Enhancement: Filters to change the priority for YARPP's filters on the_content, the_content_feed and the_excerpt_rss, example: add_filter('yarpp_content_priority', 1); * Bugfix: Fixes deactivation survey when Google Translate in Chrome auto translates the admin page

Download this release

Release Info

Developer jeffparker
Plugin Icon 128x128 Yet Another Related Posts Plugin (YARPP)
Version 5.3.0
Comparing to
See all releases

Code changes from version 5.2.2 to 5.3.0

classes/YARPP_Core.php CHANGED
@@ -60,10 +60,26 @@ class YARPP {
60
  */
61
  add_action('transition_post_status', array($this->cache, 'transition_post_status'), 10, 3);
62
 
 
 
 
 
 
 
 
 
 
63
  /* Automatic display hooks: */
64
- add_filter('the_content', array($this, 'the_content'), 1200);
65
- add_filter('the_content_feed', array($this, 'the_content_feed'), 600);
66
- add_filter('the_excerpt_rss', array($this, 'the_excerpt_rss' ), 600);
 
 
 
 
 
 
 
67
  add_action('wp_enqueue_scripts', array($this, 'maybe_enqueue_thumbnails'));
68
 
69
  /**
@@ -163,6 +179,7 @@ class YARPP {
163
  'auto_display_post_types' => array('post'),
164
  'pools' => array(),
165
  'manually_using_thumbnails' => false,
 
166
  );
167
  }
168
 
@@ -1601,4 +1618,4 @@ class YARPP {
1601
  $text = str_replace('</p>', '', $text);
1602
  return $text;
1603
  }
1604
- }
60
  */
61
  add_action('transition_post_status', array($this->cache, 'transition_post_status'), 10, 3);
62
 
63
+ /**
64
+ * Initializes yarpp rest routes
65
+ */
66
+ if(apply_filters('rest_enabled', true) && class_exists('WP_REST_Controller') && class_exists('WP_REST_Posts_Controller')){
67
+ include_once( YARPP_DIR . '/classes/YARPP_Rest_Api.php' );
68
+ new YARPP_Rest_Api();
69
+ }
70
+
71
+
72
  /* Automatic display hooks: */
73
+ /**
74
+ * Allow filtering the priority of YARPP's placement.
75
+ */
76
+ $content_priority = apply_filters('yarpp_content_priority', 1200);
77
+ $feed_priority = apply_filters('yarpp_feed_priority', 600);
78
+ $excerpt_rss_priority = apply_filters('yarpp_excerpt_rss_priority', 600);
79
+
80
+ add_filter('the_content', array($this, 'the_content'), $content_priority);
81
+ add_filter('the_content_feed', array($this, 'the_content_feed'), $feed_priority);
82
+ add_filter('the_excerpt_rss', array($this, 'the_excerpt_rss' ), $excerpt_rss_priority);
83
  add_action('wp_enqueue_scripts', array($this, 'maybe_enqueue_thumbnails'));
84
 
85
  /**
179
  'auto_display_post_types' => array('post'),
180
  'pools' => array(),
181
  'manually_using_thumbnails' => false,
182
+ 'rest_api_display' => true
183
  );
184
  }
185
 
1618
  $text = str_replace('</p>', '', $text);
1619
  return $text;
1620
  }
1621
+ }
classes/YARPP_Meta_Box_Display_Rest_Api.php ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class YARPP_Meta_Box_Display_Rest_Api extends YARPP_Meta_Box {
4
+ public function display() {
5
+ echo "<div>";
6
+ $this->checkbox( 'rest_api_display', __( "Display related posts in REST API?", 'yarpp' )." <span class='yarpp_help' data-help='" . esc_attr( __( "This option adds related posts to the REST API.", 'yarpp' ) ) . "'>&nbsp;</span>", '' );
7
+ echo '<a href="https://support.shareaholic.com/hc/en-us/articles/360046456752">';
8
+ esc_html_e('Read the REST API documentation.');
9
+ echo "</a></div>";
10
+ }
11
+ }
classes/YARPP_Rest_Api.php ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * YARPP rest api functionality
5
+ */
6
+ class YARPP_Rest_Api extends WP_REST_Controller{
7
+
8
+ public function __construct() {
9
+ add_action('rest_api_init', array($this, 'register_api_routes'));
10
+ add_filter('wp_rest_cache/allowed_endpoints', array($this,'cache_endpoints'),10,1);
11
+ }
12
+
13
+ /**
14
+ * @var \WP_REST_Posts_Controller|null
15
+ */
16
+ protected $posts_controller = null;
17
+
18
+ /**
19
+ * Initializes yarpp rest routes via rest_api_init
20
+ */
21
+ function register_api_routes() {
22
+ global $yarpp;
23
+
24
+ if ($yarpp->get_option('rest_api_display')) {
25
+ $NAMESPACE = 'yarpp/v1';
26
+
27
+ /* Register the yarpp rest route */
28
+ register_rest_route( $NAMESPACE, '/related/(?P<id>[\w-]+)', array(
29
+ array(
30
+ 'methods' => WP_REST_Server::READABLE,
31
+ 'callback' => array($this,'get_related_posts'),
32
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
33
+ 'args' => $this->get_related_posts_args()
34
+ ),
35
+ 'args' => array(
36
+ 'id' => array(
37
+ 'description' => __( 'Unique identifier for the object.' ),
38
+ 'type' => 'integer',
39
+ ),
40
+ ),
41
+ 'schema' => array( $this->get_posts_controller('post'), 'get_public_item_schema' ),
42
+ ));
43
+ }
44
+ }
45
+
46
+ /**
47
+ * @param WP_REST_Request $request
48
+ *
49
+ * @return WP_Error
50
+ */
51
+ public function get_item_permissions_check($request) {
52
+ $error_response = new WP_Error(
53
+ 'rest_forbidden_context',
54
+ __( 'Sorry, you are not allowed to read this post.', 'yarpp' ),
55
+ array( 'status' => rest_authorization_required_code() )
56
+ );
57
+ $post_obj = get_post($request->get_param('id'));
58
+ if(! $this->get_posts_controller($post_obj->post_type)->check_read_permission($post_obj)){
59
+ return $error_response;
60
+ }
61
+
62
+ $core_permissions_check = $this->get_posts_controller()->get_items_permissions_check($request);
63
+ if($core_permissions_check instanceof WP_Error){
64
+ return $core_permissions_check;
65
+ }
66
+ // Check for password-protected posts.
67
+ if ( !empty($post_obj->post_password) && ! $this->get_posts_controller()->can_access_password_content( $post_obj, $request ) ) {
68
+ return $error_response;
69
+ }
70
+ return true;
71
+ }
72
+
73
+ /**
74
+ * Gets available arguments for related-posts endpoint.
75
+ *
76
+ * @return array
77
+ */
78
+ public function get_related_posts_args() {
79
+ /**
80
+ * @var $yarpp YARPRP
81
+ */
82
+ global $yarpp;
83
+
84
+ return array(
85
+ 'limit' => array(
86
+ 'description' => esc_html('Number of posts to display', 'yarpp'),
87
+ 'type' => 'integer',
88
+ 'default' => $yarpp->get_option('limit'),
89
+ 'validate_callback' => 'rest_validate_request_arg',
90
+ 'sanitize_callback' => 'absint',
91
+ 'minimum' => 1,
92
+ 'maximum' => apply_filters(
93
+ 'yarpp_rest_api_get_related_posts_args_limit_maximum',
94
+ 20
95
+ )
96
+ ),
97
+ 'context' => array_replace_recursive(
98
+ $this->get_context_param(),
99
+ array(
100
+ 'default' => 'embed'
101
+ )
102
+ ),
103
+ 'password' => array(
104
+ 'description' => __( 'The password for the post if it is password protected.' ),
105
+ 'type' => 'string',
106
+ )
107
+ );
108
+ }
109
+
110
+ /**
111
+ * Gets related posts provided a id param exists.
112
+ *
113
+ * @param WP_REST_REQUEST $request Incoming HTTP request data.
114
+ * @return WP_Error|WP_HTTP_Response
115
+ */
116
+ public function get_related_posts($request) {
117
+ /**
118
+ * @var $yarpp YARPP
119
+ */
120
+ global $yarpp;
121
+
122
+ $query_params = $request->get_params();
123
+ $id = $query_params['id'];
124
+
125
+ $post_obj = get_post($id);
126
+ if(! $post_obj instanceof WP_Post){
127
+ return new WP_Error('rest_invalid_id', esc_html__( 'Invalid Id', 'yarpp' ), array('status' => 404));
128
+ }
129
+
130
+ if ($yarpp->get_option('cross_relate')) {
131
+ $post_types = $yarpp->get_post_types();
132
+ } else {
133
+ $post_types = array(get_post_type($post_obj));
134
+ }
135
+
136
+ $post_types = apply_filters('yarpp_map_post_types', $post_types, 'rest_api');
137
+ $allowed_args = array('limit');
138
+
139
+ $args = array_filter(
140
+ $query_params,
141
+ function($key) use ($allowed_args){
142
+ return in_array($key,$allowed_args);
143
+ },
144
+ ARRAY_FILTER_USE_KEY
145
+ );
146
+ $args['post_type'] = $post_types;
147
+ $related_posts = $yarpp->get_related(
148
+ $id,
149
+ $args
150
+ );
151
+
152
+ // Great, we have the posts we want. But they're formatted totally differently than the WP REST API endpoints
153
+ // So we use the core WP_RESTS_Posts_Controller to get the response in exactly the same format.
154
+ $ids = wp_list_pluck( $related_posts, 'ID' );
155
+ $read_controller = $this->get_posts_controller($post_obj->post_type);
156
+ $simulated_request = clone $request;
157
+ $simulated_request->set_route('wp/v1/posts');
158
+
159
+ $simulated_params = array(
160
+ 'include' => $ids,
161
+ 'per_page' =>$query_params['limit']
162
+ );
163
+ if(isset($query_params['context'])){
164
+ $simulated_params['context'] = $query_params['context'];
165
+ }
166
+
167
+ $simulated_request->set_query_params($simulated_params);
168
+
169
+ // Hack the WordPress Posts controller to return posts of all types, so long as they have the IDs we want.
170
+ add_action( 'rest_post_query', array($this, 'ignore_post_type_filter_callback'), 10, 2 );
171
+ $read_controller_response = $read_controller->get_items($simulated_request);
172
+ remove_action( 'rest_post_query', array($this, 'ignore_post_type_filter_callback'), 10, 2 );
173
+
174
+ $read_controller_posts = $read_controller_response->get_data();
175
+ $ordered_rest_results = array();
176
+ // Reorder the posts in the response according to what they were in the YARPP response.
177
+ foreach($related_posts as $related_post){
178
+ foreach($read_controller_posts as $read_controller_post){
179
+ if($related_post->ID === $read_controller_post['id']){
180
+ $ordered_rest_results[] = $read_controller_post;
181
+ break;
182
+ }
183
+ }
184
+ }
185
+ $read_controller_response->set_data($ordered_rest_results);
186
+ return $read_controller_response;
187
+ }
188
+
189
+ /**
190
+ * @param string $post_type
191
+ *
192
+ * @return WP_REST_Posts_Controller
193
+ */
194
+ protected function get_posts_controller($post_type = null){
195
+ if( ! $this->posts_controller instanceof WP_REST_Posts_Controller){
196
+ $this->posts_controller = new WP_REST_Posts_Controller($post_type);
197
+ }
198
+ return $this->posts_controller;
199
+ }
200
+
201
+ /**
202
+ * Register the /wp-json/yarpp/v1/related for caching with https://wordpress.org/plugins/wp-rest-cache/
203
+ */
204
+ function cache_endpoints( $allowed_endpoints ) {
205
+ if ( ! isset( $allowed_endpoints[ 'yarpp/v1' ] ) || ! in_array( 'related', $allowed_endpoints[ 'yarpp/v1' ] ) ) {
206
+ $allowed_endpoints[ 'yarpp/v1' ][] = 'related';
207
+ }
208
+ return $allowed_endpoints;
209
+ }
210
+
211
+ /**
212
+ * Filters what post types the WordPress Posts Controller uses when querying.
213
+ * This way we can ask the posts controller for all posts of any type (remember we're only fetching ones with
214
+ * IDs that match the results of YARPP's related query.) The results are all formatted like posts, which isn't
215
+ * stellar, but it's got the important info.
216
+ * @param $args
217
+ * @param $request
218
+ *
219
+ * @return mixed
220
+ */
221
+ public function ignore_post_type_filter_callback( $args, $request ){
222
+ global $yarpp;
223
+ $args['post_type'] = $yarpp->get_post_types();
224
+ return $args;
225
+ }
226
+ }
includes/yarpp_meta_boxes_hooks.php CHANGED
@@ -6,6 +6,7 @@ include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Display_Web.php');
6
  include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Optin.php');
7
  include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Pool.php');
8
  include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Relatedness.php');
 
9
 
10
  global $yarpp;
11
 
@@ -54,6 +55,26 @@ add_meta_box(
54
  'core'
55
  );
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  add_meta_box(
58
  'yarpp_display_contact',
59
  __('Contact YARPP', 'yarpp'),
6
  include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Optin.php');
7
  include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Pool.php');
8
  include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Relatedness.php');
9
+ include_once(YARPP_DIR.'/classes/YARPP_Meta_Box_Display_Rest_Api.php');
10
 
11
  global $yarpp;
12
 
55
  'core'
56
  );
57
 
58
+ if(
59
+ apply_filters('rest_enabled', true) &&
60
+ function_exists('register_rest_route') &&
61
+ class_exists('WP_REST_Controller') &&
62
+ class_exists('WP_REST_Posts_Controller')
63
+ ){
64
+ add_meta_box(
65
+ 'yarp_display_api',
66
+ __('Display options <small>for REST API</small>', 'yarpp'),
67
+ array(
68
+ new YARPP_Meta_Box_Display_Rest_Api,
69
+ 'display'
70
+ ),
71
+ 'settings_page_yarpp',
72
+ 'normal',
73
+ 'core'
74
+ );
75
+ }
76
+
77
+
78
  add_meta_box(
79
  'yarpp_display_contact',
80
  __('Contact YARPP', 'yarpp'),
lib/plugin-deactivation-survey/deactivate-feedback-form.js CHANGED
@@ -132,7 +132,7 @@
132
 
133
  shareaholic.DeactivateFeedbackForm.prototype.onDeactivateClicked = function(event)
134
  {
135
- this.deactivateURL = event.target.href;
136
 
137
  if ( ! this.dialog) {
138
  this.dialog = $( this.element ).remodal({hashTracking:false,closeOnOutsideClick:false});
132
 
133
  shareaholic.DeactivateFeedbackForm.prototype.onDeactivateClicked = function(event)
134
  {
135
+ this.deactivateURL = event.currentTarget.href;
136
 
137
  if ( ! this.dialog) {
138
  this.dialog = $( this.element ).remodal({hashTracking:false,closeOnOutsideClick:false});
readme.txt CHANGED
@@ -5,7 +5,7 @@ Requires at least: 3.7
5
  Requires PHP: 5.3
6
  License: GPLv2 or later
7
  Tested up to: 5.4
8
- Stable tag: 5.2.2
9
 
10
  Display a list of related posts on your site based on a powerful unique algorithm. Optionally, earn money by including sponsored content.
11
 
@@ -21,6 +21,7 @@ Yet Another Related Posts Plugin (YARPP) displays pages, posts, and custom post
21
  * **An advanced and versatile algorithm**: Using a customizable algorithm considering post titles, content, tags, categories, and custom taxonomies, YARPP finds related content from across your site.
22
  * **Related posts in feeds**: Display related posts in RSS feeds with custom display options.
23
  * **HTTPS support**
 
24
  * **Professionally maintained** and supported with regular updates
25
 
26
  **YARPP Algorithm Explained**
@@ -271,8 +272,14 @@ add_action(
271
  `
272
 
273
  == Changelog ==
 
 
 
 
 
 
274
  = 5.2.2 (2020-07-21) =
275
- * [Critical Bugfix](https://wordpress.org/support/topic/slow-website-after-update-to-5-2-1/): Fixes plugin upgrade process. This in turn fixes a page slow down issue introduced for some sites with v5.2.1.
276
 
277
  = 5.2.1 (2020-07-20) =
278
  * Enhancement: Don't recommend switching database engine unless necessary for fulltext indexes.
@@ -965,6 +972,6 @@ After a break of many years, the plugin is 100% supported now that the baton has
965
  * Initial upload
966
 
967
  == Upgrade Notice ==
968
- = 5.2.2 =
969
  We update this plugin regularly so we can make it better for you. Update to the latest version for all of the available features and improvements. Thank you for using YARPP!
970
 
5
  Requires PHP: 5.3
6
  License: GPLv2 or later
7
  Tested up to: 5.4
8
+ Stable tag: 5.3.0
9
 
10
  Display a list of related posts on your site based on a powerful unique algorithm. Optionally, earn money by including sponsored content.
11
 
21
  * **An advanced and versatile algorithm**: Using a customizable algorithm considering post titles, content, tags, categories, and custom taxonomies, YARPP finds related content from across your site.
22
  * **Related posts in feeds**: Display related posts in RSS feeds with custom display options.
23
  * **HTTPS support**
24
+ * **REST API** support
25
  * **Professionally maintained** and supported with regular updates
26
 
27
  **YARPP Algorithm Explained**
272
  `
273
 
274
  == Changelog ==
275
+ = 5.3.0 (2020-07-29) =
276
+ * New: REST API support ([documentation](https://support.shareaholic.com/hc/en-us/articles/360046456752))
277
+ * Enhancement: [WP Rest Cache Plugin](https://wordpress.org/plugins/wp-rest-cache/) support
278
+ * Enhancement: Filters to change the priority for YARPP's filters on `the_content`, `the_content_feed` and `the_excerpt_rss`, example: `add_filter('yarpp_content_priority', 1);`
279
+ * Bugfix: Fixes deactivation survey when Google Translate in Chrome auto translates the admin page
280
+
281
  = 5.2.2 (2020-07-21) =
282
+ * [Critical Bugfix](https://wordpress.org/support/topic/slow-website-after-update-to-5-2-1/): Fixes plugin upgrade process. This in turn fixes a page slow down issue introduced for some sites with v5.2.0.
283
 
284
  = 5.2.1 (2020-07-20) =
285
  * Enhancement: Don't recommend switching database engine unless necessary for fulltext indexes.
972
  * Initial upload
973
 
974
  == Upgrade Notice ==
975
+ = 5.3.0 =
976
  We update this plugin regularly so we can make it better for you. Update to the latest version for all of the available features and improvements. Thank you for using YARPP!
977
 
yarpp.php CHANGED
@@ -2,7 +2,7 @@
2
  /*
3
  Plugin Name: Yet Another Related Posts Plugin (YARPP)
4
  Description: Adds related posts to your site and in RSS feeds, based on a powerful, customizable algorithm.
5
- Version: 5.2.2
6
  Author: YARPP
7
  Author URI: https://yarpp.com/
8
  Plugin URI: https://yarpp.com/
@@ -23,7 +23,7 @@ if(!defined('WP_CONTENT_DIR')){
23
  define('WP_CONTENT_DIR', substr($tr,0,strrpos($tr,'/')));
24
  }
25
 
26
- define('YARPP_VERSION', '5.2.2');
27
 
28
  define('YARPP_DIR', dirname(__FILE__));
29
  define('YARPP_URL', plugins_url('',__FILE__));
2
  /*
3
  Plugin Name: Yet Another Related Posts Plugin (YARPP)
4
  Description: Adds related posts to your site and in RSS feeds, based on a powerful, customizable algorithm.
5
+ Version: 5.3.0
6
  Author: YARPP
7
  Author URI: https://yarpp.com/
8
  Plugin URI: https://yarpp.com/
23
  define('WP_CONTENT_DIR', substr($tr,0,strrpos($tr,'/')));
24
  }
25
 
26
+ define('YARPP_VERSION', '5.3.0');
27
 
28
  define('YARPP_DIR', dirname(__FILE__));
29
  define('YARPP_URL', plugins_url('',__FILE__));