Yet Another Related Posts Plugin (YARPP) - Version 3.4b3

Version Description

Download this release

Release Info

Developer mitchoyoshitaka
Plugin Icon 128x128 Yet Another Related Posts Plugin (YARPP)
Version 3.4b3
Comparing to
See all releases

Code changes from version 3.4b2 to 3.4b3

Files changed (7) hide show
  1. includes.php +6 -4
  2. keywords.php +32 -31
  3. magic.php +14 -8
  4. options.php +3 -2
  5. readme.txt +4 -1
  6. template-metabox.php +3 -2
  7. yarpp.php +4 -3
includes.php CHANGED
@@ -387,9 +387,11 @@ function yarpp_get_option($option = null) {
387
  // ensure defaults if not set:
388
  $options = array_merge( $yarpp_value_options, $yarpp_binary_options, $options );
389
 
390
- if ( !is_null( $option ) )
 
 
391
  return $options[$option];
392
- return $options;
393
  }
394
 
395
  function yarpp_microtime_float() {
@@ -414,12 +416,12 @@ function yarpp_version_info($enforce_cache = false) {
414
 
415
  function yarpp_add_metabox() {
416
  if (function_exists('add_meta_box')) {
417
- add_meta_box( 'yarpp_relatedposts', __( 'Related Posts' , 'yarpp'), 'yarpp_metabox', 'post', 'normal' );
418
  }
419
  }
420
  function yarpp_metabox() {
421
  global $post;
422
- echo '<div id="yarpp-related-posts">';
423
  if ($post->ID)
424
  yarpp_related(array('post'),array('limit'=>1000),true,false,'metabox');
425
  else
387
  // ensure defaults if not set:
388
  $options = array_merge( $yarpp_value_options, $yarpp_binary_options, $options );
389
 
390
+ if ( is_null( $option ) )
391
+ return $options;
392
+ if ( isset($options[$option]) )
393
  return $options[$option];
394
+ return null;
395
  }
396
 
397
  function yarpp_microtime_float() {
416
 
417
  function yarpp_add_metabox() {
418
  if (function_exists('add_meta_box')) {
419
+ add_meta_box( 'yarpp_relatedposts', __( 'Related Posts' , 'yarpp') . ' <span class="postbox-title-action"><a href="' . esc_url( admin_url('options-general.php?page=yarpp') ) . '" class="edit-box open-box">' . __( 'Configure' ) . '</a></span>', 'yarpp_metabox', 'post', 'normal' );
420
  }
421
  }
422
  function yarpp_metabox() {
423
  global $post;
424
+ echo '<style>#yarpp_relatedposts h3 .postbox-title-action { right: 30px; top: 5px; position: absolute; padding: 0 }</style><div id="yarpp-related-posts">';
425
  if ($post->ID)
426
  yarpp_related(array('post'),array('limit'=>1000),true,false,'metabox');
427
  else
keywords.php CHANGED
@@ -1,19 +1,22 @@
1
  <?php
2
 
3
- function yarpp_extract_keywords($source,$max = 20) {
4
  global $overusedwords;
5
 
 
 
 
6
  // 3.2.2: ignore soft hyphens
7
  // Requires PHP 5: http://bugs.php.net/bug.php?id=25670
8
  $softhyphen = html_entity_decode('&#173;',ENT_NOQUOTES,'UTF-8');
9
- $source = str_replace($softhyphen, '', $source);
10
 
11
  $charset = get_option('blog_charset');
12
  if ( function_exists('mb_split') && !empty($charset) ) {
13
  mb_regex_encoding($charset);
14
- $wordlist = mb_split('\s*\W+\s*', mb_strtolower($source, $charset));
15
  } else
16
- $wordlist = preg_split('%\s*\W+\s*%', strtolower($source));
17
 
18
  // Build an array of the unique words and number of times they occur.
19
  $tokens = array_count_values($wordlist);
@@ -23,8 +26,9 @@ function yarpp_extract_keywords($source,$max = 20) {
23
  unset($tokens[$word]);
24
  }
25
  // Remove words which are only a letter
 
26
  foreach (array_keys($tokens) as $word) {
27
- if (function_exists('mb_strlen'))
28
  if (mb_strlen($word) < 2) unset($tokens[$word]);
29
  else
30
  if (strlen($word) < 2) unset($tokens[$word]);
@@ -40,27 +44,20 @@ function yarpp_extract_keywords($source,$max = 20) {
40
  }
41
 
42
  function post_title_keywords($ID,$max = 20) {
43
- return yarpp_extract_keywords(yarpp_html_entity_strip(get_the_title($ID)),$max);
44
- }
45
-
46
- function yarpp_html_entity_strip( $html ) {
47
- $html = preg_replace('/&#x[0-9a-f]+;/','',$html);
48
- $html = preg_replace('/&#[0-9]+;/','',$html);
49
- $html = preg_replace('/&[a-zA-Z]+;/','',$html);
50
- return $html;
51
  }
52
 
53
  function post_body_keywords( $ID, $max = 20 ) {
54
  $post = get_post( $ID );
55
  if ( empty($post) )
56
  return '';
57
- $content = strip_tags( apply_filters_if_white( 'the_content', $post->post_content ) );
58
- $content = yarpp_html_entity_strip( $content );
59
  return yarpp_extract_keywords( $content, $max );
60
  }
61
 
62
- /* new in 2.0! apply_filters_if_white (previously apply_filters_without) now has a blacklist. It's defined here. */
63
-
 
64
  /* blacklisted so far:
65
  - diggZ-Et
66
  - reddZ-Et
@@ -72,20 +69,20 @@ function post_body_keywords( $ID, $max = 20 ) {
72
  - WP Greet Box
73
  //- Tweet This - could not reproduce problem.
74
  */
75
-
76
- $yarpp_blacklist = array(null,'yarpp_default','diggZEt_AddBut','reddZEt_AddBut','dzoneZEt_AddBut','wp_syntax_before_filter','wp_syntax_after_filter','wp_codebox_before_filter','wp_codebox_after_filter','do_shortcode');//,'insert_tweet_this'
77
- $yarpp_blackmethods = array(null,'addinlinejs','replacebbcode','filter_content');
78
-
79
- function yarpp_white($filter) {
80
- global $yarpp_blacklist;
81
- global $yarpp_blackmethods;
82
- if (is_array($filter)) {
83
- if (array_search($filter[1],$yarpp_blackmethods))
84
- return false;
85
  }
86
- if (array_search($filter,$yarpp_blacklist))
 
87
  return false;
88
- return true;
89
  }
90
 
91
  /* FYI, apply_filters_if_white was used here to avoid a loop in apply_filters('the_content') > yarpp_default() > yarpp_related() > current_post_keywords() > apply_filters('the_content').*/
@@ -93,19 +90,23 @@ function apply_filters_if_white($tag, $value) {
93
  global $wp_filter, $merged_filters, $wp_current_filter;
94
 
95
  $args = array();
96
- $wp_current_filter[] = $tag;
97
 
98
  // Do 'all' actions first
99
  if ( isset($wp_filter['all']) ) {
 
100
  $args = func_get_args();
101
  _wp_call_all_hook($args);
102
  }
103
 
104
  if ( !isset($wp_filter[$tag]) ) {
105
- array_pop($wp_current_filter);
 
106
  return $value;
107
  }
108
 
 
 
 
109
  // Sort
110
  if ( !isset( $merged_filters[ $tag ] ) ) {
111
  ksort($wp_filter[$tag]);
1
  <?php
2
 
3
+ function yarpp_extract_keywords($html, $max = 20) {
4
  global $overusedwords;
5
 
6
+ // strip tags and html entities
7
+ $text = preg_replace('/&(#x[0-9a-f]+|#[0-9]+|[a-zA-Z]+);/', '', strip_tags($html) );
8
+
9
  // 3.2.2: ignore soft hyphens
10
  // Requires PHP 5: http://bugs.php.net/bug.php?id=25670
11
  $softhyphen = html_entity_decode('&#173;',ENT_NOQUOTES,'UTF-8');
12
+ $text = str_replace($softhyphen, '', $text);
13
 
14
  $charset = get_option('blog_charset');
15
  if ( function_exists('mb_split') && !empty($charset) ) {
16
  mb_regex_encoding($charset);
17
+ $wordlist = mb_split('\s*\W+\s*', mb_strtolower($text, $charset));
18
  } else
19
+ $wordlist = preg_split('%\s*\W+\s*%', strtolower($text));
20
 
21
  // Build an array of the unique words and number of times they occur.
22
  $tokens = array_count_values($wordlist);
26
  unset($tokens[$word]);
27
  }
28
  // Remove words which are only a letter
29
+ $mb_strlen_exists = function_exists('mb_strlen');
30
  foreach (array_keys($tokens) as $word) {
31
+ if ($mb_strlen_exists)
32
  if (mb_strlen($word) < 2) unset($tokens[$word]);
33
  else
34
  if (strlen($word) < 2) unset($tokens[$word]);
44
  }
45
 
46
  function post_title_keywords($ID,$max = 20) {
47
+ return yarpp_extract_keywords(get_the_title($ID),$max);
 
 
 
 
 
 
 
48
  }
49
 
50
  function post_body_keywords( $ID, $max = 20 ) {
51
  $post = get_post( $ID );
52
  if ( empty($post) )
53
  return '';
54
+ $content = apply_filters_if_white( 'the_content', $post->post_content );
 
55
  return yarpp_extract_keywords( $content, $max );
56
  }
57
 
58
+ /* new in 2.0! apply_filters_if_white (previously apply_filters_without) now has a blacklist.
59
+ * It can be modified via the yarpp_blacklist and yarpp_blackmethods filters.
60
+ */
61
  /* blacklisted so far:
62
  - diggZ-Et
63
  - reddZ-Et
69
  - WP Greet Box
70
  //- Tweet This - could not reproduce problem.
71
  */
72
+ function yarpp_white( $filter ) {
73
+ static $blacklist, $blackmethods;
74
+
75
+ if ( is_null($blacklist) || is_null($blackmethods) ) {
76
+ $yarpp_blacklist = array('yarpp_default', 'diggZEt_AddBut', 'reddZEt_AddBut', 'dzoneZEt_AddBut', 'wp_syntax_before_filter', 'wp_syntax_after_filter', 'wp_codebox_before_filter', 'wp_codebox_after_filter', 'do_shortcode');//,'insert_tweet_this'
77
+ $yarpp_blackmethods = array('addinlinejs', 'replacebbcode', 'filter_content');
78
+
79
+ $blacklist = (array) apply_filters( 'yarpp_blacklist', $yarpp_blacklist );
80
+ $blackmethods = (array) apply_filters( 'yarpp_blackmethods', $yarpp_blackmethods );
 
81
  }
82
+
83
+ if ( is_array($filter) && in_array( $filter[1], $blackmethods ) )
84
  return false;
85
+ return !in_array( $filter, $blacklist );
86
  }
87
 
88
  /* FYI, apply_filters_if_white was used here to avoid a loop in apply_filters('the_content') > yarpp_default() > yarpp_related() > current_post_keywords() > apply_filters('the_content').*/
90
  global $wp_filter, $merged_filters, $wp_current_filter;
91
 
92
  $args = array();
 
93
 
94
  // Do 'all' actions first
95
  if ( isset($wp_filter['all']) ) {
96
+ $wp_current_filter[] = $tag;
97
  $args = func_get_args();
98
  _wp_call_all_hook($args);
99
  }
100
 
101
  if ( !isset($wp_filter[$tag]) ) {
102
+ if ( isset($wp_filter['all']) )
103
+ array_pop($wp_current_filter);
104
  return $value;
105
  }
106
 
107
+ if ( !isset($wp_filter['all']) )
108
+ $wp_current_filter[] = $tag;
109
+
110
  // Sort
111
  if ( !isset( $merged_filters[ $tag ] ) ) {
112
  ksort($wp_filter[$tag]);
magic.php CHANGED
@@ -153,9 +153,14 @@ function yarpp_related($type,$args,$echo = true,$reference_ID=false,$domain = 'w
153
  } else {
154
  if ($yarpp_cache->is_yarpp_time()) // if we're already in a YARPP loop, stop now.
155
  return false;
156
-
157
  if ( !$reference_ID )
158
  $reference_ID = get_the_ID();
 
 
 
 
 
 
159
  }
160
 
161
  get_currentuserinfo();
@@ -165,7 +170,6 @@ function yarpp_related($type,$args,$echo = true,$reference_ID=false,$domain = 'w
165
  $domainprefix = 'rss_';
166
  else
167
  $domainprefix = '';
168
-
169
  // get options
170
  // note the 2.1 change... the options array changed from what you might call a "list" to a "hash"... this changes the structure of the $args to something which is, in the long term, much more useful
171
  $options = array(
@@ -184,14 +188,10 @@ function yarpp_related($type,$args,$echo = true,$reference_ID=false,$domain = 'w
184
  }
185
  }
186
  extract($optvals);
187
-
188
  if ($cross_relate)
189
  $type = array('post','page');
190
 
191
- $cache_status = yarpp_cache_enforce($reference_ID);
192
-
193
- $output = '';
194
-
195
  if ($domain == 'demo_web' || $domain == 'demo_rss') {
196
  // It's DEMO TIME!
197
  $yarpp_cache->demo_time = true;
@@ -210,6 +210,7 @@ function yarpp_related($type,$args,$echo = true,$reference_ID=false,$domain = 'w
210
  $current_query = $wp_query;
211
  $current_pagenow = $pagenow;
212
 
 
213
  $wp_query = new WP_Query();
214
  $orders = explode(' ',$order);
215
  if ( 'demo_web' == $domain || 'demo_rss' == $domain ) {
@@ -340,7 +341,7 @@ function yarpp_status_transition($new_status, $old_status, $post) {
340
  }
341
 
342
  // Note: return value changed in 3.4
343
- // return YARPP_NO_RELATED | YARPP_RELATED | false if no good input
344
  function yarpp_cache_enforce($reference_ID, $force = false) {
345
  global $yarpp_debug, $yarpp_cache;
346
 
@@ -348,6 +349,11 @@ function yarpp_cache_enforce($reference_ID, $force = false) {
348
  return false;
349
 
350
  $status = $yarpp_cache->is_cached($reference_ID);
 
 
 
 
 
351
 
352
  // If not cached, process now:
353
  if ( YARPP_NOT_CACHED == $status || $force ) {
153
  } else {
154
  if ($yarpp_cache->is_yarpp_time()) // if we're already in a YARPP loop, stop now.
155
  return false;
 
156
  if ( !$reference_ID )
157
  $reference_ID = get_the_ID();
158
+
159
+ $cache_status = yarpp_cache_enforce($reference_ID);
160
+
161
+ // If cache status is YARPP_DONT_RUN, end here without returning or echoing anything.
162
+ if ( YARPP_DONT_RUN == $cache_status )
163
+ return;
164
  }
165
 
166
  get_currentuserinfo();
170
  $domainprefix = 'rss_';
171
  else
172
  $domainprefix = '';
 
173
  // get options
174
  // note the 2.1 change... the options array changed from what you might call a "list" to a "hash"... this changes the structure of the $args to something which is, in the long term, much more useful
175
  $options = array(
188
  }
189
  }
190
  extract($optvals);
191
+ // override $type for cross_relate:
192
  if ($cross_relate)
193
  $type = array('post','page');
194
 
 
 
 
 
195
  if ($domain == 'demo_web' || $domain == 'demo_rss') {
196
  // It's DEMO TIME!
197
  $yarpp_cache->demo_time = true;
210
  $current_query = $wp_query;
211
  $current_pagenow = $pagenow;
212
 
213
+ $output = '';
214
  $wp_query = new WP_Query();
215
  $orders = explode(' ',$order);
216
  if ( 'demo_web' == $domain || 'demo_rss' == $domain ) {
341
  }
342
 
343
  // Note: return value changed in 3.4
344
+ // return YARPP_NO_RELATED | YARPP_RELATED | YARPP_DONT_RUN | false if no good input
345
  function yarpp_cache_enforce($reference_ID, $force = false) {
346
  global $yarpp_debug, $yarpp_cache;
347
 
349
  return false;
350
 
351
  $status = $yarpp_cache->is_cached($reference_ID);
352
+ $status = apply_filters( 'yarpp_cache_enforce_status', $status, $reference_ID );
353
+
354
+ // There's a stop signal:
355
+ if ( YARPP_DONT_RUN === $status )
356
+ return YARPP_DONT_RUN;
357
 
358
  // If not cached, process now:
359
  if ( YARPP_NOT_CACHED == $status || $force ) {
options.php CHANGED
@@ -113,7 +113,8 @@ if (isset($_POST['update_yarpp'])) {
113
  foreach (array_keys($yarpp_binary_options) as $option) {
114
  $new_options[$option] = isset($_POST[$option]);
115
  }
116
-
 
117
  yarpp_set_option($new_options);
118
 
119
  echo '<div class="updated fade"><p>'.__('Options saved!','yarpp').'</p></div>';
@@ -175,7 +176,7 @@ function load_display_discats() {
175
  <div class="wrap">
176
  <h2>
177
  <?php _e('Yet Another Related Posts Plugin Options','yarpp');?> <small><?php
178
- echo esc_html(get_option('yarpp_version'));
179
  ?></small>
180
  </h2>
181
 
113
  foreach (array_keys($yarpp_binary_options) as $option) {
114
  $new_options[$option] = isset($_POST[$option]);
115
  }
116
+
117
+ $new_options = apply_filters( 'yarpp_settings_save', $new_options );
118
  yarpp_set_option($new_options);
119
 
120
  echo '<div class="updated fade"><p>'.__('Options saved!','yarpp').'</p></div>';
176
  <div class="wrap">
177
  <h2>
178
  <?php _e('Yet Another Related Posts Plugin Options','yarpp');?> <small><?php
179
+ echo apply_filters( 'yarpp_version_html', esc_html( get_option('yarpp_version') ) );
180
  ?></small>
181
  </h2>
182
 
readme.txt CHANGED
@@ -2,7 +2,7 @@
2
  Contributors: mitchoyoshitaka
3
  Author: mitcho (Michael Yoshitaka Erlewine)
4
  Author URI: http://mitcho.com/
5
- Plugin URI: http://mitcho.com/code/yarpp/
6
  Donate link: http://tinyurl.com/donatetomitcho
7
  Tags: related, posts, post, pages, page, RSS, feed, feeds
8
  Requires at least: 3.0
@@ -206,6 +206,9 @@ If you are a bilingual speaker of English and another language and an avid user
206
  * Performance improvements on pages with "no related posts"
207
  * Streamlined keyword storage in YARPP_Cache_Postmeta
208
  * Updated minor bug for computing how many results should be cached
 
 
 
209
  * Added Portuguese stopwords by Leandro Coelho ([Logística Descomplicada](http://www.logisticadescomplicada.com))
210
  = 3.3.3 =
211
  * [Bug fix](http://wordpress.org/support/topic/no-related-posts-1): a fix for keyword computation for pages; should improve results on pages. May require flushing of cache: see FAQ for instructions.
2
  Contributors: mitchoyoshitaka
3
  Author: mitcho (Michael Yoshitaka Erlewine)
4
  Author URI: http://mitcho.com/
5
+ Plugin URI: http://yarpp.org/
6
  Donate link: http://tinyurl.com/donatetomitcho
7
  Tags: related, posts, post, pages, page, RSS, feed, feeds
8
  Requires at least: 3.0
206
  * Performance improvements on pages with "no related posts"
207
  * Streamlined keyword storage in YARPP_Cache_Postmeta
208
  * Updated minor bug for computing how many results should be cached
209
+ * Adding some filters: yarpp_settings_save, yarpp_blacklist, yarpp_blackmethods
210
+ * New systematic use of YARPP_ constants to communicate cache status
211
+ * Support for [YARPP Experiments](http://wordpress.org/extend/plugins/yarpp-experiments/).
212
  * Added Portuguese stopwords by Leandro Coelho ([Logística Descomplicada](http://www.logisticadescomplicada.com))
213
  = 3.3.3 =
214
  * [Bug fix](http://wordpress.org/support/topic/no-related-posts-1): a fix for keyword computation for pages; should improve results on pages. May require flushing of cache: see FAQ for instructions.
template-metabox.php CHANGED
@@ -13,11 +13,12 @@ $output .= '<p>'.__( 'These are the related entries for this entry. Updating thi
13
  if ($yarpp_debug) $output .= "<p>last updated: ".$wpdb->get_var("select max(date) as updated from {$wpdb->prefix}yarpp_related_cache where reference_ID = '$reference_ID'")."</p>";
14
 
15
  if (have_posts()) {
 
16
  $output .= '<ol>';
17
  while (have_posts()) {
18
  the_post();
19
- $output .= "<li><a href='post.php?action=edit&post=$id'>".get_the_title()."</a>";
20
- $output .= ' ('.round(get_the_score(),3).')';
21
  $output .= '</li>';
22
  }
23
  $output .= '</ol>';
13
  if ($yarpp_debug) $output .= "<p>last updated: ".$wpdb->get_var("select max(date) as updated from {$wpdb->prefix}yarpp_related_cache where reference_ID = '$reference_ID'")."</p>";
14
 
15
  if (have_posts()) {
16
+ $output .= '<style>#yarpp-related-posts ol li { list-style-type: decimal; }</style>';
17
  $output .= '<ol>';
18
  while (have_posts()) {
19
  the_post();
20
+ $output .= "<li><a href='post.php?action=edit&post=" . get_the_ID() . "'>" . get_the_title() . "</a>";
21
+ $output .= ' (' . round(get_the_score(),3) . ')';
22
  $output .= '</li>';
23
  }
24
  $output .= '</ol>';
yarpp.php CHANGED
@@ -3,17 +3,18 @@
3
  Plugin Name: Yet Another Related Posts Plugin
4
  Plugin URI: http://yarpp.org/
5
  Description: Returns a list of related entries based on a unique algorithm for display on your blog and RSS feeds. A templating feature allows customization of the display.
6
- Version: 3.4b2
7
  Author: mitcho (Michael Yoshitaka Erlewine)
8
  Author URI: http://mitcho.com/
9
  Donate link: http://tinyurl.com/donatetomitcho
10
  */
11
 
12
- define('YARPP_VERSION', '3.4b2');
13
  define('YARPP_DIR', dirname(__FILE__));
14
  define('YARPP_NO_RELATED', ':(');
15
  define('YARPP_RELATED', ':)');
16
  define('YARPP_NOT_CACHED', ':/');
 
17
 
18
  require_once(YARPP_DIR.'/includes.php');
19
  require_once(YARPP_DIR.'/related-functions.php');
@@ -33,7 +34,7 @@ function yarpp_init() {
33
 
34
  // set $yarpp_debug
35
  if ( isset($_REQUEST['yarpp_debug']) )
36
- $yarpp_debug = true;
37
 
38
  register_activation_hook( __FILE__, 'yarpp_activate' );
39
 
3
  Plugin Name: Yet Another Related Posts Plugin
4
  Plugin URI: http://yarpp.org/
5
  Description: Returns a list of related entries based on a unique algorithm for display on your blog and RSS feeds. A templating feature allows customization of the display.
6
+ Version: 3.4b3
7
  Author: mitcho (Michael Yoshitaka Erlewine)
8
  Author URI: http://mitcho.com/
9
  Donate link: http://tinyurl.com/donatetomitcho
10
  */
11
 
12
+ define('YARPP_VERSION', '3.4b3');
13
  define('YARPP_DIR', dirname(__FILE__));
14
  define('YARPP_NO_RELATED', ':(');
15
  define('YARPP_RELATED', ':)');
16
  define('YARPP_NOT_CACHED', ':/');
17
+ define('YARPP_DONT_RUN', 'X(');
18
 
19
  require_once(YARPP_DIR.'/includes.php');
20
  require_once(YARPP_DIR.'/related-functions.php');
34
 
35
  // set $yarpp_debug
36
  if ( isset($_REQUEST['yarpp_debug']) )
37
+ $yarpp_debug = true;
38
 
39
  register_activation_hook( __FILE__, 'yarpp_activate' );
40