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

Version Description

Download this release

Release Info

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

Code changes from version 3.4b8 to 3.4b9

cache-postmeta.php CHANGED
@@ -7,27 +7,19 @@ define('YARPP_POSTMETA_RELATED_KEY', '_yarpp_related');
7
8
class YARPP_Cache_Postmeta {
9
10
// variables used for lookup
11
private $related_postdata = array();
12
private $related_IDs = array();
13
14
- public $name = "postmeta";
15
-
16
private $yarpp_time = false;
17
- public $demo_time = false;
18
19
/**
20
* SETUP/STATUS
21
*/
22
function __construct( &$core ) {
23
parent::__construct( $core );
24
- add_filter('posts_where',array(&$this,'where_filter'));
25
- add_filter('posts_orderby',array(&$this,'orderby_filter'));
26
- add_filter('posts_fields',array(&$this,'fields_filter'));
27
- add_filter('posts_request',array(&$this,'demo_request_filter'));
28
- add_filter('post_limits',array(&$this,'limit_filter'));
29
- // sets the score override flag.
30
- add_action('parse_query',array(&$this,'set_score_override_flag'));
31
}
32
33
public function is_enabled() {
@@ -68,17 +60,15 @@ class YARPP_Cache_Postmeta {
68
*/
69
public function where_filter($arg) {
70
global $wpdb;
71
- if ($this->yarpp_time) {
72
- $threshold = yarpp_get_option('threshold');
73
- // modify the where clause to use the related ID list.
74
- if (!count($this->related_IDs))
75
- $this->related_IDs = array(0);
76
- $arg = preg_replace("!{$wpdb->posts}.ID = \d+!","{$wpdb->posts}.ID in (".join(',',$this->related_IDs).")",$arg);
77
-
78
- // if we have "recent only" set, add an additional condition
79
- if (yarpp_get_option("recent_only"))
80
- $arg .= " and post_date > date_sub(now(), interval ".yarpp_get_option("recent_number")." ".yarpp_get_option("recent_units").") ";
81
- }
82
return $arg;
83
}
84
@@ -86,7 +76,7 @@ class YARPP_Cache_Postmeta {
86
global $wpdb;
87
// only order by score if the score function is added in fields_filter, which only happens
88
// if there are related posts in the postdata
89
- if ($this->yarpp_time && $this->score_override &&
90
is_array($this->related_postdata) && count($this->related_postdata))
91
return str_replace("$wpdb->posts.post_date","score",$arg);
92
return $arg;
@@ -94,7 +84,7 @@ class YARPP_Cache_Postmeta {
94
95
public function fields_filter($arg) {
96
global $wpdb;
97
- if ($this->yarpp_time && is_array($this->related_postdata) && count($this->related_postdata)) {
98
$scores = array();
99
foreach ($this->related_postdata as $related_entry) {
100
$scores[] = " WHEN {$related_entry['ID']} THEN {$related_entry['score']}";
@@ -104,21 +94,9 @@ class YARPP_Cache_Postmeta {
104
return $arg;
105
}
106
107
- public function demo_request_filter($arg) {
108
- global $wpdb;
109
- if ($this->demo_time) {
110
- $wpdb->query("set @count = 0;");
111
- return "SELECT SQL_CALC_FOUND_ROWS ID + {$this->demo_limit} as ID, post_author, post_date, post_date_gmt, '" . LOREMIPSUM . "' as post_content,
112
- concat('".__('Example post ','yarpp')."',@count:=@count+1) as post_title, 0 as post_category, '' as post_excerpt, 'publish' as post_status, 'open' as comment_status, 'open' as ping_status, '' as post_password, concat('example-post-',@count) as post_name, '' as to_ping, '' as pinged, post_modified, post_modified_gmt, '' as post_content_filtered, 0 as post_parent, concat('PERMALINK',@count) as guid, 0 as menu_order, 'post' as post_type, '' as post_mime_type, 0 as comment_count, 'SCORE' as score
113
- FROM $wpdb->posts
114
- ORDER BY ID DESC LIMIT 0, {$this->demo_limit}";
115
- }
116
- return $arg;
117
- }
118
-
119
public function limit_filter($arg) {
120
global $wpdb;
121
- if ($this->yarpp_time and $this->online_limit)
122
return " limit {$this->online_limit} ";
123
return $arg;
124
}
@@ -132,16 +110,30 @@ class YARPP_Cache_Postmeta {
132
133
public function begin_yarpp_time($reference_ID) {
134
$this->yarpp_time = true;
135
- // get the related posts from postdata, and also construct the relate_IDs array
136
$this->related_postdata = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
137
if (is_array($this->related_postdata) && count($this->related_postdata))
138
$this->related_IDs = array_map(create_function('$x','return $x["ID"];'), $this->related_postdata);
139
}
140
141
public function end_yarpp_time() {
142
$this->yarpp_time = false;
143
$this->related_IDs = array();
144
$this->related_postdata = array();
145
}
146
147
// @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
@@ -208,8 +200,10 @@ class YARPP_Cache_Postmeta {
208
public function related($reference_ID = null, $related_ID = null) {
209
global $wpdb;
210
211
- if ( !is_int( $reference_ID ) && !is_int( $related_ID ) )
212
- return new WP_Error('yarpp_cache_error', "reference ID and/or related ID must be ints" );
213
214
if (!is_null($reference_ID) && !is_null($related_ID)) {
215
$results = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
7
8
class YARPP_Cache_Postmeta {
9
10
+ public $name = "postmeta";
11
+
12
// variables used for lookup
13
private $related_postdata = array();
14
private $related_IDs = array();
15
16
private $yarpp_time = false;
17
18
/**
19
* SETUP/STATUS
20
*/
21
function __construct( &$core ) {
22
parent::__construct( $core );
23
}
24
25
public function is_enabled() {
60
*/
61
public function where_filter($arg) {
62
global $wpdb;
63
+ $threshold = yarpp_get_option('threshold');
64
+ // modify the where clause to use the related ID list.
65
+ if (!count($this->related_IDs))
66
+ $this->related_IDs = array(0);
67
+ $arg = preg_replace("!{$wpdb->posts}.ID = \d+!","{$wpdb->posts}.ID in (".join(',',$this->related_IDs).")",$arg);
68
+
69
+ // if we have "recent only" set, add an additional condition
70
+ if (yarpp_get_option("recent_only"))
71
+ $arg .= " and post_date > date_sub(now(), interval ".yarpp_get_option("recent_number")." ".yarpp_get_option("recent_units").") ";
72
return $arg;
73
}
74
76
global $wpdb;
77
// only order by score if the score function is added in fields_filter, which only happens
78
// if there are related posts in the postdata
79
+ if ($this->score_override &&
80
is_array($this->related_postdata) && count($this->related_postdata))
81
return str_replace("$wpdb->posts.post_date","score",$arg);
82
return $arg;
84
85
public function fields_filter($arg) {
86
global $wpdb;
87
+ if (is_array($this->related_postdata) && count($this->related_postdata)) {
88
$scores = array();
89
foreach ($this->related_postdata as $related_entry) {
90
$scores[] = " WHEN {$related_entry['ID']} THEN {$related_entry['score']}";
94
return $arg;
95
}
96
97
public function limit_filter($arg) {
98
global $wpdb;
99
+ if ($this->online_limit)
100
return " limit {$this->online_limit} ";
101
return $arg;
102
}
110
111
public function begin_yarpp_time($reference_ID) {
112
$this->yarpp_time = true;
113
+ // get the related posts from postmeta, and also construct the relate_IDs array
114
$this->related_postdata = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
115
if (is_array($this->related_postdata) && count($this->related_postdata))
116
$this->related_IDs = array_map(create_function('$x','return $x["ID"];'), $this->related_postdata);
117
+ add_filter('posts_where',array(&$this,'where_filter'));
118
+ add_filter('posts_orderby',array(&$this,'orderby_filter'));
119
+ add_filter('posts_fields',array(&$this,'fields_filter'));
120
+ add_filter('post_limits',array(&$this,'limit_filter'));
121
+ add_action('pre_get_posts',array(&$this,'add_signature'));
122
+ // sets the score override flag.
123
+ add_action('parse_query',array(&$this,'set_score_override_flag'));
124
}
125
126
public function end_yarpp_time() {
127
$this->yarpp_time = false;
128
$this->related_IDs = array();
129
$this->related_postdata = array();
130
+ remove_filter('posts_where',array(&$this,'where_filter'));
131
+ remove_filter('posts_orderby',array(&$this,'orderby_filter'));
132
+ remove_filter('posts_fields',array(&$this,'fields_filter'));
133
+ remove_filter('post_limits',array(&$this,'limit_filter'));
134
+ remove_action('pre_get_posts',array(&$this,'add_signature'));
135
+ // sets the score override flag.
136
+ remove_action('parse_query',array(&$this,'set_score_override_flag'));
137
}
138
139
// @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
200
public function related($reference_ID = null, $related_ID = null) {
201
global $wpdb;
202
203
+ if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
204
+ _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
205
+ return;
206
+ }
207
208
if (!is_null($reference_ID) && !is_null($related_ID)) {
209
$results = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
cache-tables.php CHANGED
@@ -8,21 +8,12 @@ define('YARPP_TABLES_KEYWORDS_TABLE', 'yarpp_keyword_cache');
8
class YARPP_Cache_Tables extends YARPP_Cache {
9
public $name = "custom tables";
10
private $yarpp_time = false;
11
- public $demo_time = false;
12
13
/**
14
* SETUP/STATUS
15
*/
16
function __construct( &$core ) {
17
parent::__construct( $core );
18
- add_filter('posts_join',array(&$this,'join_filter'));
19
- add_filter('posts_where',array(&$this,'where_filter'));
20
- add_filter('posts_orderby',array(&$this,'orderby_filter'));
21
- add_filter('posts_fields',array(&$this,'fields_filter'));
22
- add_filter('posts_request',array(&$this,'demo_request_filter'));
23
- add_filter('post_limits',array(&$this,'limit_filter'));
24
- // sets the score override flag.
25
- add_action('parse_query',array(&$this,'set_score_override_flag'));
26
}
27
28
public function is_enabled() {
@@ -124,18 +115,6 @@ class YARPP_Cache_Tables extends YARPP_Cache {
124
return $arg;
125
}
126
127
- public function demo_request_filter($arg) {
128
- global $wpdb;
129
- if ($this->demo_time) {
130
- $wpdb->query("set @count = 0;");
131
- $arg = "SELECT SQL_CALC_FOUND_ROWS ID + {$this->demo_limit} as ID, post_author, post_date, post_date_gmt, '" . LOREMIPSUM . "' as post_content,
132
- concat('".__('Example post ','yarpp')."',@count:=@count+1) as post_title, 0 as post_category, '' as post_excerpt, 'publish' as post_status, 'open' as comment_status, 'open' as ping_status, '' as post_password, concat('example-post-',@count) as post_name, '' as to_ping, '' as pinged, post_modified, post_modified_gmt, '' as post_content_filtered, 0 as post_parent, concat('PERMALINK',@count) as guid, 0 as menu_order, 'post' as post_type, '' as post_mime_type, 0 as comment_count, 'SCORE' as score
133
- FROM $wpdb->posts
134
- ORDER BY ID DESC LIMIT 0, {$this->demo_limit}";
135
- }
136
- return $arg;
137
- }
138
-
139
public function limit_filter($arg) {
140
global $wpdb;
141
if ($this->yarpp_time and $this->online_limit) {
@@ -153,12 +132,27 @@ class YARPP_Cache_Tables extends YARPP_Cache {
153
154
public function begin_yarpp_time() {
155
$this->yarpp_time = true;
156
}
157
-
158
public function end_yarpp_time() {
159
$this->yarpp_time = false;
160
}
161
-
162
// @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
163
public function is_cached($reference_ID) {
164
global $wpdb;
@@ -241,8 +235,10 @@ class YARPP_Cache_Tables extends YARPP_Cache {
241
public function related($reference_ID = null, $related_ID = null) {
242
global $wpdb;
243
244
- if ( !is_int( $reference_ID ) && !is_int( $related_ID ) )
245
- return new WP_Error('yarpp_cache_error', "reference ID and/or related ID must be ints" );
246
247
if (!is_null($reference_ID) && !is_null($related_ID)) {
248
$results = $wpdb->get_col("select ID from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where reference_ID = $reference_ID and ID = $related_ID");
8
class YARPP_Cache_Tables extends YARPP_Cache {
9
public $name = "custom tables";
10
private $yarpp_time = false;
11
12
/**
13
* SETUP/STATUS
14
*/
15
function __construct( &$core ) {
16
parent::__construct( $core );
17
}
18
19
public function is_enabled() {
115
return $arg;
116
}
117
118
public function limit_filter($arg) {
119
global $wpdb;
120
if ($this->yarpp_time and $this->online_limit) {
132
133
public function begin_yarpp_time() {
134
$this->yarpp_time = true;
135
+ add_filter('posts_join',array(&$this,'join_filter'));
136
+ add_filter('posts_where',array(&$this,'where_filter'));
137
+ add_filter('posts_orderby',array(&$this,'orderby_filter'));
138
+ add_filter('posts_fields',array(&$this,'fields_filter'));
139
+ add_filter('post_limits',array(&$this,'limit_filter'));
140
+ add_action('pre_get_posts',array(&$this,'add_signature'));
141
+ // sets the score override flag.
142
+ add_action('parse_query',array(&$this,'set_score_override_flag'));
143
}
144
+
145
public function end_yarpp_time() {
146
$this->yarpp_time = false;
147
+ remove_filter('posts_join',array(&$this,'join_filter'));
148
+ remove_filter('posts_where',array(&$this,'where_filter'));
149
+ remove_filter('posts_orderby',array(&$this,'orderby_filter'));
150
+ remove_filter('posts_fields',array(&$this,'fields_filter'));
151
+ remove_filter('post_limits',array(&$this,'limit_filter'));
152
+ remove_action('pre_get_posts',array(&$this,'add_signature'));
153
+ remove_action('parse_query',array(&$this,'set_score_override_flag'));
154
}
155
+
156
// @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
157
public function is_cached($reference_ID) {
158
global $wpdb;
235
public function related($reference_ID = null, $related_ID = null) {
236
global $wpdb;
237
238
+ if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
239
+ _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
240
+ return;
241
+ }
242
243
if (!is_null($reference_ID) && !is_null($related_ID)) {
244
$results = $wpdb->get_col("select ID from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where reference_ID = $reference_ID and ID = $related_ID");
class-admin.php CHANGED
@@ -7,12 +7,21 @@ class YARPP_Admin {
7
function __construct( &$core ) {
8
$this->core = &$core;
9
10
- add_action( 'admin_menu', array( $this, 'register' ) );
11
// new in 3.3: set default meta boxes to show:
12
add_filter( 'default_hidden_meta_boxes', array( $this, 'default_hidden_meta_boxes' ), 10, 2 );
13
}
14
15
- function register() {
16
// setup admin
17
$this->hook = add_options_page(__('Related Posts (YARPP)','yarpp'),__('Related Posts (YARPP)','yarpp'), 'manage_options', 'yarpp', array( $this, 'options_page' ) );
18
// new in 3.3: load options page sections as metaboxes
@@ -32,9 +41,10 @@ class YARPP_Admin {
32
function enqueue() {
33
global $current_screen;
34
if (is_object($current_screen) && $current_screen->id == 'settings_page_yarpp') {
35
wp_enqueue_script( 'postbox' );
36
- wp_enqueue_style( 'yarpp_options', plugins_url( 'options.css', __FILE__ ), array(), YARPP_VERSION );
37
- wp_enqueue_script( 'yarpp_options', plugins_url( 'options.js', __FILE__ ), array('jquery'), YARPP_VERSION );
38
// wp_enqueue_script( 'thickbox' );
39
// wp_enqueue_style( 'thickbox' );
40
}
@@ -56,7 +66,10 @@ class YARPP_Admin {
56
function metabox() {
57
echo '<style>#yarpp_relatedposts h3 .postbox-title-action { right: 30px; top: 5px; position: absolute; padding: 0 }</style><div id="yarpp-related-posts">';
58
if ( get_the_ID() )
59
- yarpp_related(array('post'),array('limit'=>1000),true,false,'metabox');
60
else
61
echo "<p>".__("Related entries may be displayed once you save your entry",'yarpp').".</p>";
62
echo '</div>';
@@ -68,4 +81,59 @@ class YARPP_Admin {
68
$hidden = array( 'yarpp_pool', 'yarpp_relatedness' );
69
return $hidden;
70
}
71
}
7
function __construct( &$core ) {
8
$this->core = &$core;
9
10
+ add_action( 'admin_init', array($this, 'ajax_register') );
11
+ add_action( 'admin_menu', array( $this, 'ui_register' ) );
12
// new in 3.3: set default meta boxes to show:
13
add_filter( 'default_hidden_meta_boxes', array( $this, 'default_hidden_meta_boxes' ), 10, 2 );
14
}
15
16
+ function ajax_register() {
17
+ // Register AJAX services
18
+ if ( defined('DOING_AJAX') && DOING_AJAX ) {
19
+ add_action( 'wp_ajax_yarpp_display_exclude_terms', array( $this, 'ajax_display_exclude_terms' ) );
20
+ add_action( 'wp_ajax_yarpp_display_demo', array( $this, 'ajax_display_demo' ) );
21
+ }
22
+ }
23
+
24
+ function ui_register() {
25
// setup admin
26
$this->hook = add_options_page(__('Related Posts (YARPP)','yarpp'),__('Related Posts (YARPP)','yarpp'), 'manage_options', 'yarpp', array( $this, 'options_page' ) );
27
// new in 3.3: load options page sections as metaboxes
41
function enqueue() {
42
global $current_screen;
43
if (is_object($current_screen) && $current_screen->id == 'settings_page_yarpp') {
44
+ $version = defined('WP_DEBUG') && WP_DEBUG ? time() : YARPP_VERSION;
45
wp_enqueue_script( 'postbox' );
46
+ wp_enqueue_style( 'yarpp_options', plugins_url( 'options.css', __FILE__ ), array(), $version );
47
+ wp_enqueue_script( 'yarpp_options', plugins_url( 'options.js', __FILE__ ), array('jquery'), $version );
48
// wp_enqueue_script( 'thickbox' );
49
// wp_enqueue_style( 'thickbox' );
50
}
66
function metabox() {
67
echo '<style>#yarpp_relatedposts h3 .postbox-title-action { right: 30px; top: 5px; position: absolute; padding: 0 }</style><div id="yarpp-related-posts">';
68
if ( get_the_ID() )
69
+ $this->core->display_related(null, array(
70
+ 'post_type' => array('post'),
71
+ 'domain' => 'metabox'
72
+ ));
73
else
74
echo "<p>".__("Related entries may be displayed once you save your entry",'yarpp').".</p>";
75
echo '</div>';
81
$hidden = array( 'yarpp_pool', 'yarpp_relatedness' );
82
return $hidden;
83
}
84
+
85
+ /*
86
+ * AJAX SERVICES
87
+ */
88
+
89
+ function ajax_display_exclude_terms() {
90
+ if ( !isset($_REQUEST['taxonomy']) )
91
+ return;
92
+
93
+ $taxonomy = (string) $_REQUEST['taxonomy'];
94
+
95
+ header("HTTP/1.1 200");
96
+ header("Content-Type: text/html; charset=UTF-8");
97
+
98
+ $exclude = yarpp_get_option('exclude');
99
+ if ( isset($exclude[$taxonomy]) )
100
+ $exclude = $exclude[$taxonomy];
101
+ else
102
+ $exclude = array();
103
+ if ( 'category' == $taxonomy )
104
+ $exclude .= ',' . get_option( 'default_category' );
105
+
106
+ $terms = get_terms($taxonomy, array(
107
+ 'exclude' => $exclude,
108
+ 'hide_empty' => false,
109
+ 'hierarchical' => false,
110
+ 'number' => 100,
111
+ 'offset' => $_REQUEST['offset']
112
+ ));
113
+
114
+ if ( !count($terms) ) {
115
+ echo ':('; // no more :(
116
+ exit;
117
+ }
118
+
119
+ foreach ($terms as $term) {
120
+ echo "<input type='checkbox' name='exclude[$taxonomy][$term->term_id]' value='true' /> <label>" . esc_html($term->name) . "</label> ";
121
+ //for='exclude[$taxonomy][$cat->term_id]' it's not HTML. :(
122
+ }
123
+ exit;
124
+ }
125
+
126
+ function ajax_display_demo() {
127
+ header("HTTP/1.1 200");
128
+ header("Content-Type: text/html; charset=UTF-8");
129
+
130
+ $args = array(
131
+ 'post_type' => array('post'),
132
+ 'domain' => isset($_REQUEST['domain']) ? $_REQUEST['domain'] : 'website'
133
+ );
134
+
135
+ $return = $this->core->display_demo_related($args, false);
136
+ echo ereg_replace("[\n\r]",'',nl2br(htmlspecialchars($return)));
137
+ exit;
138
+ }
139
}
class-cache.php CHANGED
@@ -12,6 +12,10 @@ abstract class YARPP_Cache {
12
$this->name = __($this->name, 'yarpp');
13
}
14
15
// Note: return value changed in 3.4
16
// return YARPP_NO_RELATED | YARPP_RELATED | YARPP_DONT_RUN | false if no good input
17
function enforce( $reference_ID, $force = false ) {
@@ -52,8 +56,8 @@ abstract class YARPP_Cache {
52
if ( defined( 'WP_IMPORTING' ) )
53
return;
54
55
- $sql = "select post_parent from $wpdb->posts where ID='$post_ID'";
56
- $parent_ID = $wpdb->get_var($sql);
57
58
if ( $parent_ID != $post_ID && $parent_ID )
59
$post_ID = $parent_ID;
@@ -106,7 +110,7 @@ abstract class YARPP_Cache {
106
* SQL!
107
*/
108
109
- function sql( $reference_ID = false ) {
110
global $wpdb, $post;
111
112
if ( is_object($post) && !$reference_ID ) {
@@ -119,10 +123,10 @@ abstract class YARPP_Cache {
119
$reference_post = $post;
120
}
121
122
- $options = array( 'threshold', 'show_pass_post', 'past_only', 'weight', 'exclude', 'recent_only', 'recent_number', 'recent_units' );
123
- // mask it so we only get the ones specified in $options
124
- $optvals = array_intersect_key($this->core->get_option(), array_flip($options));
125
- extract($optvals);
126
127
// Fetch keywords
128
$keywords = $this->get_keywords($reference_ID);
@@ -192,8 +196,6 @@ abstract class YARPP_Cache {
192
$newsql .= ' and '.$tax_criteria[$tax].' >= 2';
193
}
194
195
- // The maximum number of items we'll ever want to cache
196
- $limit = max($this->core->get_option('limit'), $this->core->get_option('rss_limit'));
197
$newsql .= " order by score desc limit $limit";
198
199
// in caching, we cross-relate regardless of whether we're going to actually
@@ -327,7 +329,7 @@ abstract class YARPP_Cache {
327
return !in_array( $filter, $blacklist );
328
}
329
330
- /* 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').*/
331
function apply_filters_if_white($tag, $value) {
332
global $wp_filter, $merged_filters, $wp_current_filter;
333
@@ -375,3 +377,230 @@ abstract class YARPP_Cache {
375
return $value;
376
}
377
}
12
$this->name = __($this->name, 'yarpp');
13
}
14
15
+ function add_signature( $query ) {
16
+ $query->yarpp_cache_type = $this->name;
17
+ }
18
+
19
// Note: return value changed in 3.4
20
// return YARPP_NO_RELATED | YARPP_RELATED | YARPP_DONT_RUN | false if no good input
21
function enforce( $reference_ID, $force = false ) {
56
if ( defined( 'WP_IMPORTING' ) )
57
return;
58
59
+ $post = get_post( $post_ID );
60
+ $parent_ID = $post->post_parent;
61
62
if ( $parent_ID != $post_ID && $parent_ID )
63
$post_ID = $parent_ID;
110
* SQL!
111
*/
112
113
+ function sql( $reference_ID = false, $args ) {
114
global $wpdb, $post;
115
116
if ( is_object($post) && !$reference_ID ) {
123
$reference_post = $post;
124
}
125
126
+ $options = array( 'threshold', 'show_pass_post', 'past_only', 'weight', 'exclude', 'recent_only', 'recent_number', 'recent_units', 'limit' );
127
+ extract( $this->core->parse_args($args, $options) );
128
+ // The maximum number of items we'll ever want to cache
129
+ $limit = max($limit, $this->core->get_option('rss_limit'));
130
131
// Fetch keywords
132
$keywords = $this->get_keywords($reference_ID);
196
$newsql .= ' and '.$tax_criteria[$tax].' >= 2';
197
}
198
199
$newsql .= " order by score desc limit $limit";
200
201
// in caching, we cross-relate regardless of whether we're going to actually
329
return !in_array( $filter, $blacklist );
330
}
331
332
+ /* FYI, apply_filters_if_white was used here to avoid a loop in apply_filters('the_content') > YARPP::the_content() > YARPP::related() > YARPP_Cache::body_keywords() > apply_filters('the_content').*/
333
function apply_filters_if_white($tag, $value) {
334
global $wp_filter, $merged_filters, $wp_current_filter;
335
377
return $value;
378
}
379
}
380
+
381
+ class YARPP_Cache_Bypass extends YARPP_Cache {
382
+
383
+ public $name = "bypass";
384
+
385
+ // variables used for lookup
386
+ private $related_postdata = array();
387
+ private $related_IDs = array();
388
+
389
+ private $yarpp_time = false;
390
+ public $demo_time = false;
391
+ private $demo_limit = 0;
392
+
393
+ /**
394
+ * SETUP/STATUS
395
+ */
396
+ function __construct( &$core ) {
397
+ parent::__construct( $core );
398
+ }
399
+
400
+ public function is_enabled() {
401
+ return true; // always enabled.
402
+ }
403
+
404
+ public function setup() {
405
+ }
406
+
407
+ public function upgrade( $last_version ) {
408
+ }
409
+
410
+ public function cache_status() {
411
+ return 0; // always uncached
412
+ }
413
+
414
+ public function uncached($limit = 20, $offset = 0) {
415
+ return array(); // nothing to cache
416
+ }
417
+
418
+ /**
419
+ * MAGIC FILTERS
420
+ */
421
+ public function where_filter($arg) {
422
+ global $wpdb;
423
+ $threshold = yarpp_get_option('threshold');
424
+ // modify the where clause to use the related ID list.
425
+ if (!count($this->related_IDs))
426
+ $this->related_IDs = array(0);
427
+ $arg = preg_replace("!{$wpdb->posts}.ID = \d+!","{$wpdb->posts}.ID in (".join(',',$this->related_IDs).")",$arg);
428
+
429
+ // if we have "recent only" set, add an additional condition
430
+ if (yarpp_get_option("recent_only"))
431
+ $arg .= " and post_date > date_sub(now(), interval ".yarpp_get_option("recent_number")." ".yarpp_get_option("recent_units").") ";
432
+ return $arg;
433
+ }
434
+
435
+ public function orderby_filter($arg) {
436
+ global $wpdb;
437
+ // only order by score if the score function is added in fields_filter, which only happens
438
+ // if there are related posts in the postdata
439
+ if ($this->score_override &&
440
+ is_array($this->related_postdata) && count($this->related_postdata))
441
+ return str_replace("$wpdb->posts.post_date","score",$arg);
442
+ return $arg;
443
+ }
444
+
445
+ public function fields_filter($arg) {
446
+ global $wpdb;
447
+ if (is_array($this->related_postdata) && count($this->related_postdata)) {
448
+ $scores = array();
449
+ foreach ($this->related_postdata as $related_entry) {
450
+ $scores[] = " WHEN {$related_entry['ID']} THEN {$related_entry['score']}";
451
+ }
452
+ $arg .= ", CASE {$wpdb->posts}.ID" . join('',$scores) ." END as score";
453
+ }
454
+ return $arg;
455
+ }
456
+
457
+ public function demo_request_filter($arg) {
458
+ global $wpdb;
459
+ $wpdb->query("set @count = 0;");
460
+
461
+ $loremipsum = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras tincidunt justo a urna. Ut turpis. Phasellus convallis, odio sit amet cursus convallis, eros orci scelerisque velit, ut sodales neque nisl at ante. Suspendisse metus. Curabitur auctor pede quis mi. Pellentesque lorem justo, condimentum ac, dapibus sit amet, ornare et, erat. Quisque velit. Etiam sodales dui feugiat neque suscipit bibendum. Integer mattis. Nullam et ante non sem commodo malesuada. Pellentesque ultrices fermentum lectus. Maecenas hendrerit neque ac est. Fusce tortor mi, tristique sed, cursus at, pellentesque non, dui. Suspendisse potenti.';
462
+
463
+ return "SELECT SQL_CALC_FOUND_ROWS ID + {$this->demo_limit} as ID, post_author, post_date, post_date_gmt, '{$loremipsum}' as post_content,
464
+ concat('".__('Example post ','yarpp')."',@count:=@count+1) as post_title, 0 as post_category, '' as post_excerpt, 'publish' as post_status, 'open' as comment_status, 'open' as ping_status, '' as post_password, concat('example-post-',@count) as post_name, '' as to_ping, '' as pinged, post_modified, post_modified_gmt, '' as post_content_filtered, 0 as post_parent, concat('PERMALINK',@count) as guid, 0 as menu_order, 'post' as post_type, '' as post_mime_type, 0 as comment_count, 'SCORE' as score
465
+ FROM $wpdb->posts
466
+ ORDER BY ID DESC LIMIT 0, {$this->demo_limit}";
467
+ }
468
+
469
+ public function limit_filter($arg) {
470
+ global $wpdb;
471
+ if ($this->online_limit)
472
+ return " limit {$this->online_limit} ";
473
+ return $arg;
474
+ }
475
+
476
+ /**
477
+ * RELATEDNESS CACHE CONTROL
478
+ */
479
+ public function is_yarpp_time() {
480
+ return $this->yarpp_time;
481
+ }
482
+
483
+ public function begin_yarpp_time( $reference_ID, $args ) {
484
+ global $wpdb;
485
+
486
+ $this->yarpp_time = true;
487
+
488
+ $this->related_postdata = $wpdb->get_results($this->sql($reference_ID, $args), ARRAY_A);
489
+ $this->related_IDs = array_map(create_function('$x','return $x["ID"];'), $this->related_postdata);
490
+
491
+ add_filter('posts_join',array(&$this,'join_filter'));
492
+ add_filter('posts_where',array(&$this,'where_filter'));
493
+ add_filter('posts_orderby',array(&$this,'orderby_filter'));
494
+ add_filter('posts_fields',array(&$this,'fields_filter'));
495
+ add_filter('post_limits',array(&$this,'limit_filter'));
496
+ add_action('pre_get_posts',array(&$this,'add_signature'));
497
+ // sets the score override flag.
498
+ add_action('parse_query',array(&$this,'set_score_override_flag'));
499
+ }
500
+
501
+ public function begin_demo_time( $limit ) {
502
+ $this->demo_time = true;
503
+ $this->demo_limit = $limit;
504
+ add_action('pre_get_posts',array(&$this,'add_signature'));
505
+ add_filter('posts_request',array(&$this,'demo_request_filter'));
506
+ }
507
+
508
+ public function end_yarpp_time() {
509
+ $this->yarpp_time = false;
510
+ remove_filter('posts_join',array(&$this,'join_filter'));
511
+ remove_filter('posts_where',array(&$this,'where_filter'));
512
+ remove_filter('posts_orderby',array(&$this,'orderby_filter'));
513
+ remove_filter('posts_fields',array(&$this,'fields_filter'));
514
+ remove_filter('post_limits',array(&$this,'limit_filter'));
515
+ remove_action('pre_get_posts',array(&$this,'add_signature'));
516
+ remove_action('parse_query',array(&$this,'set_score_override_flag'));
517
+ }
518
+
519
+ public function end_demo_time() {
520
+ $this->demo_time = false;
521
+ remove_action('pre_get_posts',array(&$this,'add_signature'));
522
+ remove_filter('posts_request',array(&$this,'demo_request_filter'));
523
+ }
524
+
525
+ // @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
526
+ public function is_cached($reference_ID) {
527
+ return YARPP_NOT_CACHED;
528
+ }
529
+
530
+ public function clear($reference_ID) {
531
+ return;
532
+ }
533
+
534
+ // @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
535
+ public function update($reference_ID) {
536
+ global $wpdb;
537
+
538
+ // $reference_ID must be numeric
539
+ if ( !$reference_ID = absint($reference_ID) )
540
+ return YARPP_NOT_CACHED;
541
+
542
+ return YARPP_RELATED;
543
+ }
544
+
545
+ public function flush() {
546
+ }
547
+
548
+ public function related($reference_ID = null, $related_ID = null) {
549
+ global $wpdb;
550
+
551
+ if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
552
+ _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
553
+ return;
554
+ }
555
+
556
+ // reverse lookup
557
+ if ( is_int($related_ID) && is_null($reference_ID) ) {
558
+ _doing_it_wrong( __METHOD__, 'YARPP_Cache_Bypass::related cannot do a reverse lookup', '3.4' );
559
+ return;
560
+ }
561
+
562
+ $results = $wpdb->get_results($this->sql($reference_ID), ARRAY_A);
563
+ if ( !$results || !count($results) )
564
+ return false;
565
+
566
+ if ( is_null($related_ID) ) {
567
+ return array_map(create_function('$x','return $x["ID"];'), $results);
568
+ } else {
569
+ foreach($results as $result) {
570
+ if ($result['ID'] == $related_ID)
571
+ return true;
572
+ }
573
+ }
574
+
575
+ return false;
576
+ }
577
+
578
+ /**
579
+ * KEYWORDS CACHE CONTROL
580
+ */
581
+
582
+ // @return (array) with body and title keywords
583
+ private function cache_keywords($ID) {
584
+ return array(
585
+ 'body' => $this->body_keywords($ID),
586
+ 'title' => $this->title_keywords($ID)
587
+ );
588
+ }
589
+
590
+ // @param $ID (int)
591
+ // @param $type (string) body | title | all
592
+ // @return (string|array) depending on whether "all" were requested or not
593
+ public function get_keywords( $ID, $type = 'all' ) {
594
+ if ( !$ID = absint($ID) )
595
+ return false;
596
+
597
+ $keywords = $this->cache_keywords($ID);
598
+
599
+ if ( empty($keywords) )
600
+ return false;
601
+
602
+ if ( 'all' == $type )
603
+ return $keywords;
604
+ return $keywords[$type];
605
+ }
606
+ }
class-core.php CHANGED
@@ -6,6 +6,9 @@ class YARPP {
6
public $debug = false;
7
8
public $cache;
9
public $admin;
10
private $storage_class;
11
@@ -29,9 +32,9 @@ class YARPP {
29
require_once(YARPP_DIR . '/cache-' . YARPP_CACHE_TYPE . '.php');
30
$this->storage_class = $yarpp_storage_class;
31
$this->cache = new $this->storage_class( $this );
32
33
- register_activation_hook( __FILE__, array($this, 'activate') );
34
- add_action( 'admin_init', array($this, 'admin_init') );
35
36
// update cache on save
37
add_action( 'save_post', array($this->cache, 'save_post') );
@@ -46,7 +49,7 @@ class YARPP {
46
add_filter( 'the_excerpt_rss', array( $this, 'the_excerpt_rss' ), 600 );
47
48
if ( isset($_REQUEST['yarpp_debug']) )
49
- $yarpp->debug = true;
50
51
// new in 3.4: only load UI if we're in the admin
52
if ( is_admin() ) {
@@ -54,15 +57,7 @@ class YARPP {
54
$this->admin = new YARPP_Admin( $this );
55
}
56
}
57
-
58
- function admin_init() {
59
- // Register AJAX services
60
- if ( defined('DOING_AJAX') && DOING_AJAX ) {
61
- add_action( 'wp_ajax_yarpp_display_exclude_terms', array( $this, 'ajax_display_exclude_terms' ) );
62
- add_action( 'wp_ajax_yarpp_display_demo', array( $this, 'ajax_display_demo' ) );
63
- }
64
- }
65
-
66
/*
67
* OPTIONS
68
*/
@@ -328,7 +323,7 @@ class YARPP {
328
// add the options directly first, then call set_option which will ensure defaults,
329
// in case any new options have been added.
330
update_option( 'yarpp', $yarpp_options );
331
- yarpp_set_option( $yarpp_options );
332
333
$option_keys = array_keys( $yarpp_options );
334
// append some keys for options which are long deprecated:
@@ -342,7 +337,7 @@ class YARPP {
342
}
343
344
function upgrade_3_4b5() {
345
- $options = yarpp_get_option();
346
$options['exclude'] = array(
347
'post_tag' => $options['distags'],
348
'category' => $options['discats']
@@ -353,7 +348,7 @@ class YARPP {
353
}
354
355
function upgrade_3_4b8() {
356
- $options = yarpp_get_option();
357
$options['weight'] = array(
358
'title' => $options['title'],
359
'body' => $options['body'],
@@ -389,6 +384,263 @@ class YARPP {
389
return true;
390
}
391
392
/*
393
* DEFAULT CONTENT FILTERS
394
*/
@@ -400,11 +652,11 @@ class YARPP {
400
return $this->the_content_rss($content);
401
402
$type = ($post->post_type == 'page' ? array('page') : array('post'));
403
- if ( yarpp_get_option('cross_relate') )
404
$type = array('post','page');
405
406
- if ( yarpp_get_option('auto_display') && is_single() )
407
- return $content . yarpp_related($type,array(),false,false,'website');
408
else
409
return $content;
410
}
@@ -413,11 +665,11 @@ class YARPP {
413
global $post;
414
415
$type = ($post->post_type == 'page' ? array('page') : array('post'));
416
- if ( yarpp_get_option('cross_relate') )
417
$type = array('post','page');
418
419
- if ( yarpp_get_option('rss_display') )
420
- return $content . yarpp_related($type,array(),false,false,'rss');
421
else
422
return $content;
423
}
@@ -426,11 +678,11 @@ class YARPP {
426
global $post;
427
428
$type = ($post->post_type == 'page' ? array('page') : array('post'));
429
- if ( yarpp_get_option('cross_relate') )
430
$type = array('post','page');
431
432
- if ( yarpp_get_option('rss_excerpt_display') && yarpp_get_option('rss_display') )
433
- return $content . clean_pre(yarpp_related($type,array(),false,false,'rss'));
434
else
435
return $content;
436
}
@@ -453,51 +705,4 @@ class YARPP {
453
}
454
return $result;
455
}
456
-
457
- /*
458
- * AJAX SERVICES
459
- */
460
-
461
- function ajax_display_exclude_terms() {
462
- if ( !isset($_REQUEST['taxonomy']) )
463
- return;
464
-
465
- $taxonomy = (string) $_REQUEST['taxonomy'];
466
-
467
- header("HTTP/1.1 200");
468
- header("Content-Type: text/html; charset=UTF-8");
469
-
470
- $exclude = yarpp_get_option('exclude');
471
- if ( !isset($exclude[$taxonomy]) )
472
- $exclude[$taxonomy] = array();
473
- $terms = get_terms($taxonomy, array(
474
- 'exclude' => $exclude[$taxonomy],
475
- 'number' => 100,
476
- 'offset' => $_REQUEST['offset']
477
- ));
478
-
479
- if ( !count($terms) ) {
480
- echo ':('; // no more :(
481
- exit;
482
- }
483
-
484
- foreach ($terms as $term) {
485
- echo "<input type='checkbox' name='exclude[$taxonomy][$term->term_id]' value='true' /> <label>" . esc_html($term->name) . "</label> ";
486
- //for='exclude[$taxonomy][$cat->term_id]' it's not HTML. :(
487
- }
488
- exit;
489
- }
490
-
491
- function ajax_display_demo() {
492
- header("HTTP/1.1 200");
493
- header("Content-Type: text/html; charset=UTF-8");
494
-
495
- $domain = 'demo_web';
496
- if ( isset($_REQUEST['domain']) )
497
- $domain = $_REQUEST['domain'];
498
-
499
- $return = yarpp_related(array('post'), array(), false, false, $domain);
500
- echo ereg_replace("[\n\r]",'',nl2br(htmlspecialchars($return)));
501
- exit;
502
- }
503
- }
6
public $debug = false;
7
8
public $cache;
9
+ public $cache_bypass;
10
+ private $active_cache;
11
+
12
public $admin;
13
private $storage_class;
14
32
require_once(YARPP_DIR . '/cache-' . YARPP_CACHE_TYPE . '.php');
33
$this->storage_class = $yarpp_storage_class;
34
$this->cache = new $this->storage_class( $this );
35
+ $this->cache_bypass = new YARPP_Cache_Bypass( $this );
36
37
+ register_activation_hook( __FILE__, array($this, 'activate') );
38
39
// update cache on save
40
add_action( 'save_post', array($this->cache, 'save_post') );
49
add_filter( 'the_excerpt_rss', array( $this, 'the_excerpt_rss' ), 600 );
50
51
if ( isset($_REQUEST['yarpp_debug']) )
52
+ $this->debug = true;
53
54
// new in 3.4: only load UI if we're in the admin
55
if ( is_admin() ) {
57
$this->admin = new YARPP_Admin( $this );
58
}
59
}
60
+
61
/*
62
* OPTIONS
63
*/
323
// add the options directly first, then call set_option which will ensure defaults,
324
// in case any new options have been added.
325
update_option( 'yarpp', $yarpp_options );
326
+ $this->set_option( $yarpp_options );
327
328
$option_keys = array_keys( $yarpp_options );
329
// append some keys for options which are long deprecated:
337
}
338
339
function upgrade_3_4b5() {
340
+ $options = $this->get_option();
341
$options['exclude'] = array(
342
'post_tag' => $options['distags'],
343
'category' => $options['discats']
348
}
349
350
function upgrade_3_4b8() {
351
+ $options = $this->get_option();
352
$options['weight'] = array(
353
'title' => $options['title'],
354
'body' => $options['body'],
384
return true;
385
}
386
387
+ /*
388
+ * CORE LOOKUP + DISPLAY FUNCTIONS
389
+ */
390
+
391
+ /* new in 2.1! the domain argument refers to {website,widget,rss} */
392
+ /* new in 3.0! new query-based approach: EXTREMELY HACKY! */
393
+ /*
394
+ * @param (int) $reference_ID - obligatory
395
+ * @param (array) $args
396
+ * @param (bool) $echo
397
+ */
398
+ function display_related($reference_ID, $args = array(), $echo = true) {
399
+ global $wp_query, $pagenow;
400
+
401
+ $this->upgrade_check();
402
+
403
+ // if we're already in a YARPP loop, stop now.
404
+ if ( $this->cache->is_yarpp_time() || $this->cache_bypass->is_yarpp_time() )
405
+ return false;
406
+ if ( is_null($reference_ID) )
407
+ $reference_ID = get_the_ID();
408
+
409
+ $this->setup_active_cache( $args );
410
+
411
+ $options = array( 'domain', 'limit', 'use_template', 'order', 'template_file', 'promote_yarpp' );
412
+ extract( $this->parse_args( $args, $options ) );
413
+
414
+ $cache_status = $this->active_cache->enforce($reference_ID);
415
+ // If cache status is YARPP_DONT_RUN, end here without returning or echoing anything.
416
+ if ( YARPP_DONT_RUN == $cache_status )
417
+ return;
418
+
419
+ if ( YARPP_NO_RELATED == $cache_status ) {
420
+ // There are no results, so no yarpp time for us... :'(
421
+ } else {
422
+ // Get ready for YARPP TIME!
423
+ $this->active_cache->begin_yarpp_time($reference_ID, $args);
424
+ }
425
+
426
+ // so we can return to normal later
427
+ $current_query = $wp_query;
428
+ $current_pagenow = $pagenow;
429
+
430
+ $output = '';
431
+ $wp_query = new WP_Query();
432
+ if ( YARPP_NO_RELATED == $cache_status ) {
433
+ // If there are no related posts, get no query
434
+ } else {
435
+ $orders = explode(' ',$order);
436
+ $wp_query->query(array(
437
+ 'p' => $reference_ID,
438
+ 'orderby' => $orders[0],
439
+ 'order' => $orders[1],
440
+ 'showposts' => $limit,
441
+ 'post_type' => $args['post_type']
442
+ ));
443
+ }
444
+ $this->prep_query( $current_query->is_feed );
445
+ $related_query = $wp_query; // backwards compatibility
446
+
447
+ if ($domain == 'metabox') {
448
+ include(YARPP_DIR.'/template-metabox.php');
449
+ } elseif ($use_template and file_exists(STYLESHEETPATH . '/' . $template_file) and $template_file != '') {
450
+ ob_start();
451
+ include(STYLESHEETPATH . '/' . $template_file);
452
+ $output = ob_get_contents();
453
+ ob_end_clean();
454
+ } elseif ($domain == 'widget') {
455
+ include(YARPP_DIR.'/template-widget.php');
456
+ } else {
457
+ include(YARPP_DIR.'/template-builtin.php');
458
+ }
459
+
460
+ if ( YARPP_NO_RELATED == $cache_status ) {
461
+ // Uh, do nothing. Stay very still.
462
+ } else {
463
+ $this->active_cache->end_yarpp_time(); // YARPP time is over... :(
464
+ }
465
+
466
+ // restore the older wp_query.
467
+ $wp_query = $current_query; unset($current_query); unset($related_query);
468
+ wp_reset_postdata();
469
+ $pagenow = $current_pagenow; unset($current_pagenow);
470
+
471
+ if ($promote_yarpp && $domain != 'metabox')
472
+ $output .= "\n<p>".sprintf(__("Related posts brought to you by <a href='%s'>Yet Another Related Posts Plugin</a>.",'yarpp'), 'http://yarpp.org')."</p>";
473
+
474
+ if ($echo)
475
+ echo $output;
476
+ return $output;
477
+ }
478
+
479
+ /*
480
+ * @param (int) $reference_ID - obligatory
481
+ * @param (array) $args
482
+ */
483
+ function get_related($reference_ID, $args = array()) {
484
+ $this->upgrade_check();
485
+
486
+ // if we're already in a YARPP loop, stop now.
487
+ if ( $this->cache->is_yarpp_time() || $this->cache_bypass->is_yarpp_time() )
488
+ return false;
489
+ if ( is_null($reference_ID) )
490
+ $reference_ID = get_the_ID();
491
+
492
+ $this->setup_active_cache( $args );
493
+
494
+ $options = array( 'limit', 'order' );
495
+ extract( $this->parse_args( $args, $options ) );
496
+
497
+ $cache_status = $this->active_cache->enforce($reference_ID);
498
+ if ( YARPP_DONT_RUN == $cache_status || YARPP_NO_RELATED == $cache_status )
499
+ return array();
500
+
501
+ // Get ready for YARPP TIME!
502
+ $this->active_cache->begin_yarpp_time($reference_ID, $args);
503
+
504
+ $related_query = new WP_Query();
505
+ $orders = explode(' ',$order);
506
+ $related_query->query(array(
507
+ 'p' => $reference_ID,
508
+ 'orderby' => $orders[0],
509
+ 'order' => $orders[1],
510
+ 'showposts' => $limit,
511
+ 'post_type' => $args['post_type']
512
+ ));
513
+ $this->active_cache->end_yarpp_time(); // YARPP time is over... :(
514
+
515
+ return $related_query->posts;
516
+ }
517
+
518
+ /*
519
+ * @param (int) $reference_ID
520
+ * @param (array) $args
521
+ */
522
+ function related_exist($reference_ID, $args = array()) {
523
+ global $post;
524
+
525
+ $this->upgrade_check();
526
+
527
+ if ( is_object($post) && is_null($reference_ID) )
528
+ $reference_ID = $post->ID;
529
+
530
+ // if we're already in a YARPP loop, stop now.
531
+ if ( $this->cache->is_yarpp_time() || $this->cache_bypass->is_yarpp_time() )
532
+ return false;
533
+
534
+ $this->setup_active_cache( $args );
535
+
536
+ $cache_status = $this->active_cache->enforce($reference_ID);
537
+
538
+ if ( YARPP_NO_RELATED == $cache_status )
539
+ return false;
540
+
541
+ $this->active_cache->begin_yarpp_time($reference_ID); // get ready for YARPP TIME!
542
+ $related_query = new WP_Query();
543
+ $related_query->query(array('p'=>$reference_ID,'showposts'=>1,'post_type'=>$args['post_type']));
544
+ $return = $related_query->have_posts();
545
+ unset($related_query);
546
+ $this->active_cache->end_yarpp_time(); // YARPP time is over. :(
547
+
548
+ return $return;
549
+ }
550
+
551
+ /*
552
+ * @param (array) $args
553
+ * @param (bool) $echo
554
+ */
555
+ function display_demo_related($args = array(), $echo = true) {
556
+ global $wp_query;
557
+
558
+ if ( $this->cache_bypass->demo_time ) // if we're already in a demo YARPP loop, stop now.
559
+ return false;
560
+
561
+ $options = array( 'domain', 'limit', 'use_template', 'order', 'template_file', 'promote_yarpp' );
562
+ extract( $this->parse_args( $args, $options ) );
563
+
564
+ $this->cache_bypass->begin_demo_time( $limit );
565
+
566
+ $output = '';
567
+ $wp_query = new WP_Query();
568
+ $wp_query->query('');
569
+
570
+ $this->prep_query( $domain == 'rss' );
571
+ $related_query = $wp_query; // backwards compatibility
572
+
573
+ if ($use_template and file_exists(STYLESHEETPATH . '/' . $template_file) and $template_file != '') {
574
+ ob_start();
575
+ include(STYLESHEETPATH . '/' . $template_file);
576
+ $output = ob_get_contents();
577
+ ob_end_clean();
578
+ } else {
579
+ include(YARPP_DIR.'/template-builtin.php');
580
+ }
581
+
582
+ $this->cache_bypass->end_demo_time();
583
+
584
+ if ($promote_yarpp)
585
+ $output .= "\n<p>".sprintf(__("Related posts brought to you by <a href='%s'>Yet Another Related Posts Plugin</a>.",'yarpp'), 'http://yarpp.org')."</p>";
586
+
587
+ if ( $echo )
588
+ echo $output;
589
+ return $output;
590
+ }
591
+
592
+ public function parse_args( $args, $options ) {
593
+ $options_with_rss_variants = array( 'limit', 'template_file', 'excerpt_length', 'before_title', 'after_title', 'before_post', 'after_post', 'before_related', 'after_related', 'no_results', 'order' );
594
+
595
+ $r = array();
596
+ foreach ( $options as $option ) {
597
+ if ( isset($args['domain']) && 'rss' == $args['domain'] &&
598
+ in_array( $option, $options_with_rss_variants ) )
599
+ $default = $this->get_option( 'rss_' . $option );
600
+ else
601
+ $default = $this->get_option( $option );
602
+
603
+ if ( isset($args[$option]) && $args[$option] !== $default ) {
604
+ $r[$option] = $args[$option];
605
+ } else {
606
+ $r[$option] = $default;
607
+ }
608
+ }
609
+ return $r;
610
+ }
611
+
612
+ private function setup_active_cache( $args ) {
613
+ // the options which the main sql query cares about:
614
+ $magic_options = array( 'limit', 'threshold', 'show_pass_post', 'past_only', 'weight', 'exclude', 'recent_only', 'recent_number', 'recent_units' );
615
+
616
+ $defaults = $this->get_option();
617
+ foreach ( $magic_options as $option ) {
618
+ if ( !isset($args[$option]) )
619
+ continue;
620
+
621
+ // limit is a little different... if it's less than what we cache,
622
+ // let it go.
623
+ if ( 'limit' == $option &&
624
+ $args[$option] <= max($defaults['limit'], $defaults['rss_limit']) )
625
+ continue;
626
+
627
+ if ( $args[$option] !== $defaults[$option] ) {
628
+ $this->active_cache = $this->cache_bypass;
629
+ return;
630
+ }
631
+ }
632
+ $this->active_cache = $this->cache;
633
+ }
634
+
635
+ private function prep_query( $is_feed = false ) {
636
+ global $wp_query;
637
+ $wp_query->in_the_loop = true;
638
+ $wp_query->is_feed = $is_feed;
639
+ // make sure we get the right is_single value
640
+ // (see http://wordpress.org/support/topic/288230)
641
+ $wp_query->is_single = false;
642
+ }
643
+
644
/*
645
* DEFAULT CONTENT FILTERS
646
*/
652
return $this->the_content_rss($content);
653
654
$type = ($post->post_type == 'page' ? array('page') : array('post'));
655
+ if ( $this->get_option('cross_relate') )
656
$type = array('post','page');
657
658
+ if ( $this->get_option('auto_display') && is_single() )
659
+ return $content . $this->display_related(null, array('post_type' => $type, 'domain' => 'website'), false);
660
else
661
return $content;
662
}
665
global $post;
666
667
$type = ($post->post_type == 'page' ? array('page') : array('post'));
668
+ if ( $this->get_option('cross_relate') )
669
$type = array('post','page');
670
671
+ if ( $this->get_option('rss_display') )
672
+ return $content . $this->display_related(null, array('post_type' => $type, 'domain' => 'rss'), false);
673
else
674
return $content;
675
}
678
global $post;
679
680
$type = ($post->post_type == 'page' ? array('page') : array('post'));
681
+ if ( $this->get_option('cross_relate') )
682
$type = array('post','page');
683
684
+ if ( $this->get_option('rss_excerpt_display') && $this->get_option('rss_display') )
685
+ return $content . clean_pre($this->display_related(null, array('post_type' => $type, 'domain' => 'rss'), false));
686
else
687
return $content;
688
}
705
}
706
return $result;
707
}
708
+ }
class-widget.php CHANGED
@@ -9,14 +9,14 @@ class YARPP_Widget extends WP_Widget {
9
10
function widget($args, $instance) {
11
global $post;
12
- if (!is_singular())
13
return;
14
15
extract($args);
16
17
- $type = ($post->post_type == 'page' ? array('page') : array('post'));
18
- if (yarpp_get_option('cross_relate'))
19
- $type = array('post','page');
20
21
$title = apply_filters('widget_title', $instance['title']);
22
echo $before_widget;
@@ -28,7 +28,9 @@ class YARPP_Widget extends WP_Widget {
28
_e('Related Posts (YARPP)','yarpp');
29
echo $after_title;
30
}
31
- echo yarpp_related($type,$instance,false,false,'widget');
32
echo $after_widget;
33
}
34
9
10
function widget($args, $instance) {
11
global $post;
12
+ if ( !is_singular() )
13
return;
14
15
extract($args);
16
17
+ $instance['post_type'] = ($post->post_type == 'page' ? array('page') : array('post'));
18
+ if ( yarpp_get_option('cross_relate') )
19
+ $instance['post_type'] = array('post','page');
20
21
$title = apply_filters('widget_title', $instance['title']);
22
echo $before_widget;
28
_e('Related Posts (YARPP)','yarpp');
29
echo $after_title;
30
}
31
+
32
+ $instance['domain'] = 'widget';
33
+ echo yarpp_related(null, $instance, false);
34
echo $after_widget;
35
}
36
includes.php DELETED
@@ -1,32 +0,0 @@
1
- <?php
2
-
3
- if ( !defined('WP_CONTENT_URL') )
4
- define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
5
- if ( !defined('WP_CONTENT_DIR') )
6
- define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
7
-
8
- // Used only in demo mode
9
- if (!defined('LOREMIPSUM'))
10
- define('LOREMIPSUM','Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras tincidunt justo a urna. Ut turpis. Phasellus convallis, odio sit amet cursus convallis, eros orci scelerisque velit, ut sodales neque nisl at ante. Suspendisse metus. Curabitur auctor pede quis mi. Pellentesque lorem justo, condimentum ac, dapibus sit amet, ornare et, erat. Quisque velit. Etiam sodales dui feugiat neque suscipit bibendum. Integer mattis. Nullam et ante non sem commodo malesuada. Pellentesque ultrices fermentum lectus. Maecenas hendrerit neque ac est. Fusce tortor mi, tristique sed, cursus at, pellentesque non, dui. Suspendisse potenti.');
11
-
12
- function yarpp_set_option($options, $value = null) {
13
- global $yarpp;
14
- $yarpp->set_option($options, $value);
15
- }
16
-
17
- function yarpp_get_option($option = null) {
18
- global $yarpp;
19
- return $yarpp->get_option($option);
20
- }
21
-
22
- // since 3.3.2: fix for WP 3.0.x
23
- if ( !function_exists( 'self_admin_url' ) ) {
24
- function self_admin_url($path = '', $scheme = 'admin') {
25
- if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
26
- return network_admin_url($path, $scheme);
27
- elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
28
- return user_admin_url($path, $scheme);
29
- else
30
- return admin_url($path, $scheme);
31
- }
32
- }
magic.php DELETED
@@ -1,161 +0,0 @@
1
- <?php
2
-
3
- //=CACHING===========
4
-
5
- /* new in 2.1! the domain argument refers to {website,widget,rss}, though widget is not used yet. */
6
-
7
- /* new in 3.0! new query-based approach: EXTREMELY HACKY! */
8
-
9
- function yarpp_related($type,$args,$echo = true,$reference_ID=false,$domain = 'website') {
10
- global $yarpp, $wp_query, $pagenow, $yarpp;
11
-
12
- $yarpp->upgrade_check();
13
-
14
- if ($domain == 'demo_web' || $domain == 'demo_rss') {
15
- if ($yarpp->cache->demo_time) // if we're already in a YARPP loop, stop now.
16
- return false;
17
- } else {
18
- if ($yarpp->cache->is_yarpp_time()) // if we're already in a YARPP loop, stop now.
19
- return false;
20
- if ( !$reference_ID )
21
- $reference_ID = get_the_ID();
22
-
23
- $cache_status = $yarpp->cache->enforce($reference_ID);
24
-
25
- // If cache status is YARPP_DONT_RUN, end here without returning or echoing anything.
26
- if ( YARPP_DONT_RUN == $cache_status )
27
- return;
28
- }
29
-
30
- get_currentuserinfo();
31
-
32
- // set the "domain prefix", used for all the preferences.
33
- if ($domain == 'rss' || $domain == 'demo_rss')
34
- $domainprefix = 'rss_';
35
- else
36
- $domainprefix = '';
37
- // get options
38
- // 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
39
- $options = array(
40
- 'cross_relate'=>"cross_relate",
41
- 'limit'=>"${domainprefix}limit",
42
- 'use_template'=>"${domainprefix}use_template",
43
- 'order'=>"${domainprefix}order",
44
- 'template_file'=>"${domainprefix}template_file",
45
- 'promote_yarpp'=>"${domainprefix}promote_yarpp");
46
- $optvals = array();
47
- foreach (array_keys($options) as $option) {
48
- if (isset($args[$option])) {
49
- $optvals[$option] = $args[$option];
50
- } else {
51
- $optvals[$option] = yarpp_get_option($options[$option]);
52
- }
53
- }
54
- extract($optvals);
55
- // override $type for cross_relate:
56
- if ($cross_relate)
57
- $type = array('post','page');
58
-
59
- if ($domain == 'demo_web' || $domain == 'demo_rss') {
60
- // It's DEMO TIME!
61
- $yarpp->cache->demo_time = true;
62
- if ($domain == 'demo_web')
63
- $yarpp->cache->demo_limit = yarpp_get_option('limit');
64
- else
65
- $yarpp->cache->demo_limit = yarpp_get_option('rss_limit');
66
- } else if ( YARPP_NO_RELATED == $cache_status ) {
67
- // There are no results, so no yarpp time for us... :'(
68
- } else {
69
- // Get ready for YARPP TIME!
70
- $yarpp->cache->begin_yarpp_time($reference_ID);
71
- }
72
-
73
- // so we can return to normal later
74
- $current_query = $wp_query;
75
- $current_pagenow = $pagenow;
76
-
77
- $output = '';
78
- $wp_query = new WP_Query();
79
- $orders = explode(' ',$order);
80
- if ( 'demo_web' == $domain || 'demo_rss' == $domain ) {
81
- $wp_query->query('');
82
- } else if ( YARPP_NO_RELATED == $cache_status ) {
83
- // If there are no related posts, get no query
84
- } else {
85
- $wp_query->query(array(
86
- 'p' => $reference_ID,
87
- 'orderby' => $orders[0],
88
- 'order' => $orders[1],
89
- 'showposts' => $limit,
90
- 'post_type' => $type
91
- ));
92
- }
93
-
94
- $wp_query->in_the_loop = true;
95
- $wp_query->is_feed = $current_query->is_feed;
96
- // make sure we get the right is_single value
97
- // (see http://wordpress.org/support/topic/288230)
98
- $wp_query->is_single = false;
99
- $related_query = $wp_query; // backwards compatibility
100
-
101
- if ($domain == 'metabox') {
102
- include(YARPP_DIR.'/template-metabox.php');
103
- } elseif ($use_template and file_exists(STYLESHEETPATH . '/' . $template_file) and $template_file != '') {
104
- ob_start();
105
- include(STYLESHEETPATH . '/' . $template_file);
106
- $output = ob_get_contents();
107
- ob_end_clean();
108
- } elseif ($domain == 'widget') {
109
- include(YARPP_DIR.'/template-widget.php');
110
- } else {
111
- include(YARPP_DIR.'/template-builtin.php');
112
- }
113
-
114
- if ( 'demo_web' == $domain || 'demo_rss' == $domain ) {
115
- $yarpp->cache->demo_time = false;
116
- } else if ( YARPP_NO_RELATED == $cache_status ) {
117
- // Uh, do nothing. Stay very still.
118
- } else {
119
- $yarpp->cache->end_yarpp_time(); // YARPP time is over... :(
120
- }
121
-
122
- // restore the older wp_query.
123
- $wp_query = $current_query; unset($current_query); unset($related_query);
124
- wp_reset_postdata();
125
- $pagenow = $current_pagenow; unset($current_pagenow);
126
-
127
- if ($promote_yarpp and $domain != 'metabox')
128
- $output .= "\n<p>".sprintf(__("Related posts brought to you by <a href='%s'>Yet Another Related Posts Plugin</a>.",'yarpp'), 'http://yarpp.org')."</p>";
129
-
130
- if ($echo) echo $output; else return ((!empty($output))?"\n\n":'').$output;
131
- }
132
-
133
- function yarpp_related_exist($type,$args,$reference_ID=false) {
134
- global $yarpp, $post, $yarpp;
135
-
136
- $yarpp->upgrade_check();
137
-
138
- if (is_object($post) && !$reference_ID)
139
- $reference_ID = $post->ID;
140
-
141
- if ($yarpp->cache->is_yarpp_time()) // if we're already in a YARPP loop, stop now.
142
- return false;
143
-
144
- if (yarpp_get_option('cross_relate'))
145
- $type = array('post','page');
146
-
147
- $cache_status = $yarpp->cache->enforce($reference_ID);
148
-
149
- if ( YARPP_NO_RELATED == $cache_status )
150
- return false;
151
-
152
- $yarpp->cache->begin_yarpp_time($reference_ID); // get ready for YARPP TIME!
153
- $related_query = new WP_Query();
154
- $related_query->query(array('p'=>$reference_ID,'showposts'=>1,'post_type'=>$type));
155
- $return = $related_query->have_posts();
156
- unset($related_query);
157
- $yarpp->cache->end_yarpp_time(); // YARPP time is over. :(
158
-
159
- return $return;
160
- }
161
-
options-meta-boxes.php CHANGED
@@ -33,10 +33,10 @@ class YARPP_Meta_Box {
33
function tax_importance($taxonomy) {
34
$value = yarpp_get_option("weight[tax][{$taxonomy->name}]");
35
echo "<tr valign='top'><th scope='row'>{$taxonomy->labels->name}:</th><td><select name='weight[tax][{$taxonomy->name}]'>";
36
- echo "<option $inputplus value='1'". (($value == 1) ? ' selected="selected"': '' )." > " . __("do not consider",'yarpp') . "</option>";
37
- echo "<option $inputplus value='2'". (($value == 2) ? ' selected="selected"': '' )." >" . __("consider",'yarpp') . "</option>";
38
- echo "<option $inputplus value='3'". (($value == 3) ? ' selected="selected"': '' )." >" . sprintf(__("require at least one %s in common",'yarpp'),$taxonomy->labels->singular_name) . "</option>";
39
- echo "<option $inputplus value='4'". (($value == 4) ? ' selected="selected"': '' )." >" . sprintf(__("require more than one %s in common",'yarpp'),$taxonomy->labels->singular_name) . "</option>";
40
echo "</select></td></tr>";
41
}
42
33
function tax_importance($taxonomy) {
34
$value = yarpp_get_option("weight[tax][{$taxonomy->name}]");
35
echo "<tr valign='top'><th scope='row'>{$taxonomy->labels->name}:</th><td><select name='weight[tax][{$taxonomy->name}]'>";
36
+ echo "<option value='1'". (($value == 1) ? ' selected="selected"': '' )." > " . __("do not consider",'yarpp') . "</option>";
37
+ echo "<option value='2'". (($value == 2) ? ' selected="selected"': '' )." >" . __("consider",'yarpp') . "</option>";
38
+ echo "<option value='3'". (($value == 3) ? ' selected="selected"': '' )." >" . sprintf(__("require at least one %s in common",'yarpp'),$taxonomy->labels->singular_name) . "</option>";
39
+ echo "<option value='4'". (($value == 4) ? ' selected="selected"': '' )." >" . sprintf(__("require more than one %s in common",'yarpp'),$taxonomy->labels->singular_name) . "</option>";
40
echo "</select></td></tr>";
41
}
42
options.js CHANGED
@@ -32,7 +32,7 @@ jQuery(function($) {
32
var demo_web = $('#display_demo_web');
33
$.ajax({type:'POST',
34
url: ajaxurl,
35
- data:'action=yarpp_display_demo&domain=demo_web',
36
beforeSend:function(){demo_web.html(loading)},
37
success:function(html){demo_web.html('<pre>'+html+'</pre>')},
38
dataType:'html'});
@@ -52,7 +52,7 @@ jQuery(function($) {
52
var demo_rss = $('#display_demo_rss');
53
$.ajax({type:'POST',
54
url: ajaxurl,
55
- data:'action=yarpp_display_demo&domain=demo_rss',
56
beforeSend:function(){demo_rss.html(loading)},
57
success:function(html){demo_rss.html('<pre>'+html+'</pre>')},
58
dataType:'html'});
32
var demo_web = $('#display_demo_web');
33
$.ajax({type:'POST',
34
url: ajaxurl,
35
+ data:'action=yarpp_display_demo&domain=website',
36
beforeSend:function(){demo_web.html(loading)},
37
success:function(html){demo_web.html('<pre>'+html+'</pre>')},
38
dataType:'html'});
52
var demo_rss = $('#display_demo_rss');
53
$.ajax({type:'POST',
54
url: ajaxurl,
55
+ data:'action=yarpp_display_demo&domain=rss',
56
beforeSend:function(){demo_rss.html(loading)},
57
success:function(html){demo_rss.html('<pre>'+html+'</pre>')},
58
dataType:'html'});
options.php CHANGED
@@ -125,7 +125,7 @@ if (isset($_POST['update_yarpp'])) {
125
<form method="post">
126
127
<div id="yarpp_author_text">
128
- <small><?php printf(__('by <a href="%s" target="_blank">mitcho (Michael 芳貴 Erlewine)</a>','yarpp'), 'http://yarpp.org/');?></small>
129
</div>
130
131
<!-- <div style='border:1px solid #ddd;padding:8px;'>-->
125
<form method="post">
126
127
<div id="yarpp_author_text">
128
+ <small><?php printf(__('by <a href="%s" target="_blank">mitcho (Michael 芳貴 Erlewine)</a>','yarpp'), 'http://mitcho.com/');?></small>
129
</div>
130
131
<!-- <div style='border:1px solid #ddd;padding:8px;'>-->
readme.txt CHANGED
@@ -201,8 +201,11 @@ If you are a bilingual speaker of English and another language and an avid user
201
* Now can consider custom taxonomies (of posts and pages), in addition to tags and cateogories! Custom taxonomies can also be used to exclude certain content.
202
* [Bug fix](http://wordpress.org/support/topic/plugin-yet-another-related-posts-plugin-version-333-breaks-templates-in-widget): Custom templates could not be used in widget display
203
* Implement lazy/infinite scrolling for the "disallow tags" and "disallow categories," so the YARPP settings screen doesn't lock up the browser for sites which have a crazy number or tags or categories
204
* Significant code cleanup
205
* Move many internal functions into a global object `$yarpp` of class `YARPP`; references to the global `$yarpp_cache` should now be to global `$yarpp->cache`.
206
* Removed the many different options entries, replacing them with a single `yarpp` option (except `yarpp_version`)
207
* Fixed issues with display options field data escaping and slashing once and for all
208
* Performance improvements on pages with "no related posts"
201
* Now can consider custom taxonomies (of posts and pages), in addition to tags and cateogories! Custom taxonomies can also be used to exclude certain content.
202
* [Bug fix](http://wordpress.org/support/topic/plugin-yet-another-related-posts-plugin-version-333-breaks-templates-in-widget): Custom templates could not be used in widget display
203
* Implement lazy/infinite scrolling for the "disallow tags" and "disallow categories," so the YARPP settings screen doesn't lock up the browser for sites which have a crazy number or tags or categories
204
+ * Added `yarpp_get_related()` function can be used similar to `get_posts()`
205
* Significant code cleanup
206
* Move many internal functions into a global object `$yarpp` of class `YARPP`; references to the global `$yarpp_cache` should now be to global `$yarpp->cache`.
207
+ * Created the "bypass" cache engine which is used when custom arguments are specified.
208
+ * Switch to bypass cache for demos.
209
* Removed the many different options entries, replacing them with a single `yarpp` option (except `yarpp_version`)
210
* Fixed issues with display options field data escaping and slashing once and for all
211
* Performance improvements on pages with "no related posts"
related-functions.php CHANGED
@@ -1,29 +1,69 @@
1
<?php
2
3
- // Here are the related_WHATEVER functions, as introduced in 1.1, which actually just use the yarpp_related and yarpp_related_exist functions.
4
5
// Since YARPP 2.1, these functions receive (optionally) one array argument. See the documentation for instructions on how to customize their output.
6
7
- function related_posts($a = array(),$echo=true,$reference_ID=false) {
8
- return yarpp_related(array('post'),$a,$echo,$reference_ID);
9
}
10
11
- function related_pages($a = array(),$echo=true,$reference_ID=false) {
12
- return yarpp_related(array('page'),$a,$echo,$reference_ID);
13
}
14
15
- function related_entries($a = array(),$echo=true,$reference_ID=false) {
16
- return yarpp_related(array('page','post'),$a,$echo,$reference_ID);
17
}
18
19
- function related_posts_exist($a = array(),$reference_ID=false) {
20
- return yarpp_related_exist(array('post'),$a,$reference_ID);
21
}
22
23
- function related_pages_exist($a = array(),$reference_ID=false) {
24
- return yarpp_related_exist(array('page'),$a,$reference_ID);
25
}
26
27
- function related_entries_exist($a = array(),$reference_ID=false) {
28
- return yarpp_related_exist(array('page','post'),$a,$reference_ID);
29
}
1
<?php
2
3
+ function yarpp_related($reference_ID, $args = array(), $echo = false) {
4
+ global $yarpp;
5
6
+ if ( is_array($reference_ID) ) {
7
+ _doing_it_wrong( __FUNCTION__, "YARPP's (internal) related function signature now takes the \$reference_ID first.", '3.4');
8
+ return;
9
+ }
10
+
11
+ return $yarpp->display_related($reference_ID, $args, $echo);
12
+ }
13
+
14
+ function yarpp_related_exist($reference_ID, $args = array()) {
15
+ global $yarpp;
16
+
17
+ if ( is_array($reference_ID) ) {
18
+ _doing_it_wrong( __FUNCTION__, "YARPP's (internal) related function signature now takes the \$reference_ID first.", '3.4');
19
+ return;
20
+ }
21
+
22
+ return $yarpp->related_exist($reference_ID, $args, $echo);
23
+ }
24
+
25
+ function yarpp_get_related($reference_ID, $args = array()) {
26
+ global $yarpp;
27
+ return $yarpp->get_related($reference_ID, $args);
28
+ }
29
+
30
+ // Here are the related_WHATEVER functions, as introduced in 1.1
31
// Since YARPP 2.1, these functions receive (optionally) one array argument. See the documentation for instructions on how to customize their output.
32
33
+ function related_posts($args = array(),$echo=true,$reference_ID=false) {
34
+ $args['type'] = array('post');
35
+ if ( yarpp_get_option('cross_relate') )
36
+ $args['type'] = array('post', 'page');
37
+ return yarpp_related($reference_ID, $args, $echo);
38
}
39
40
+ function related_pages($args = array(),$echo=true,$reference_ID=false) {
41
+ $args['type'] = array('page');
42
+ if ( yarpp_get_option('cross_relate') )
43
+ $args['type'] = array('post', 'page');
44
+ return yarpp_related($reference_ID, $args, $echo);
45
}
46
47
+ function related_entries($args = array(),$echo=true,$reference_ID=false) {
48
+ $args['type'] = array('post', 'page');
49
+ return yarpp_related($reference_ID, $args, $echo);
50
}
51
52
+ function related_posts_exist($args = array(),$reference_ID=false) {
53
+ $args['type'] = array('post');
54
+ if ( yarpp_get_option('cross_relate') )
55
+ $args['type'] = array('post', 'page');
56
+ return yarpp_related_exist($reference_ID, $args);
57
}
58
59
+ function related_pages_exist($args = array(),$reference_ID=false) {
60
+ $args['type'] = array('page');
61
+ if ( yarpp_get_option('cross_relate') )
62
+ $args['type'] = array('post', 'page');
63
+ return yarpp_related_exist($reference_ID, $args);
64
}
65
66
+ function related_entries_exist($args = array(),$reference_ID=false) {
67
+ $args['type'] = array('post', 'page');
68
+ return yarpp_related_exist($reference_ID, $args);
69
}
template-builtin.php CHANGED
@@ -1,34 +1,23 @@
1
- <?php /*
2
- YARPP's built-in "template"
3
-
4
- This "template" is used when you choose not to use a template.
5
-
6
- If you want to create a new template, look at templates/template-example.php as an example.
7
*/
8
9
- $options = array(
10
- 'before_title'=>"${domainprefix}before_title",
11
- 'after_title'=>"${domainprefix}after_title",
12
- 'show_excerpt'=>"${domainprefix}show_excerpt",
13
- 'excerpt_length'=>"${domainprefix}excerpt_length",
14
- 'before_post'=>"${domainprefix}before_post",
15
- 'after_post'=>"${domainprefix}after_post",
16
- 'before_related'=>"${domainprefix}before_related",
17
- 'after_related'=>"${domainprefix}after_related",
18
- 'no_results'=>"${domainprefix}no_results");
19
- $optvals = array();
20
- foreach (array_keys($options) as $option) {
21
- $optvals[$option] = yarpp_get_option($options[$option]);
22
- }
23
- extract($optvals);
24
25
if (have_posts()) {
26
while (have_posts()) {
27
the_post();
28
29
$output .= "$before_title<a href='" . get_permalink() . "' rel='bookmark' title='" . esc_attr(get_the_title() ? get_the_title() : get_the_ID()) . "'>".get_the_title()."";
30
- if (current_user_can('manage_options') && $domain != 'rss')
31
- $output .= ' <abbr title="'.sprintf(__('%f is the YARPP match score between the current entry and this related entry. You are seeing this value because you are logged in to WordPress as an administrator. It is not shown to regular visitors.','yarpp'),round(get_the_score(),3)).'">('.round(get_the_score(),3).')</abbr>';
32
$output .= '</a>';
33
if ($show_excerpt) {
34
$excerpt = strip_tags( (string) get_the_excerpt() );
@@ -39,7 +28,7 @@ if (have_posts()) {
39
$output .= $after_title."\n";
40
41
}
42
- $output = $before_related . $output . $after_related;
43
} else {
44
$output = $no_results;
45
}
1
+ <?php
2
+ /*
3
+ * YARPP's built-in "template"
4
+ *
5
+ * This "template" is used when you choose not to use a template.
6
+ * If you want to create a new template, look at templates/template-example.php as an example.
7
*/
8
9
+ get_currentuserinfo();
10
+
11
+ $options = array( 'before_title', 'after_title', 'show_excerpt', 'excerpt_length', 'before_post', 'after_post', 'before_related', 'after_related', 'no_results' );
12
+ extract( $this->parse_args( $args, $options ) );
13
14
if (have_posts()) {
15
while (have_posts()) {
16
the_post();
17
18
$output .= "$before_title<a href='" . get_permalink() . "' rel='bookmark' title='" . esc_attr(get_the_title() ? get_the_title() : get_the_ID()) . "'>".get_the_title()."";
19
+ if (current_user_can('manage_options') && $domain != 'rss' && !is_admin() )
20
+ $output .= ' <abbr title="'.sprintf(__('%f is the YARPP match score between the current entry and this related entry. You are seeing this value because you are logged in to WordPress as an administrator. It is not shown to regular visitors.','yarpp'),round(get_the_score(),1)).'">('.round(get_the_score(),1).')</abbr>';
21
$output .= '</a>';
22
if ($show_excerpt) {
23
$excerpt = strip_tags( (string) get_the_excerpt() );
28
$output .= $after_title."\n";
29
30
}
31
+ $output = $before_related . "\n" . $output . $after_related;
32
} else {
33
$output = $no_results;
34
}
yarpp.php CHANGED
@@ -3,13 +3,13 @@
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.4b8
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.4b8');
13
define('YARPP_DIR', dirname(__FILE__));
14
define('YARPP_NO_RELATED', ':(');
15
define('YARPP_RELATED', ':)');
@@ -17,12 +17,15 @@ define('YARPP_NOT_CACHED', ':/');
17
define('YARPP_DONT_RUN', 'X(');
18
19
require_once(YARPP_DIR.'/class-core.php');
20
- require_once(YARPP_DIR.'/includes.php');
21
- require_once(YARPP_DIR.'/magic.php');
22
require_once(YARPP_DIR.'/related-functions.php');
23
require_once(YARPP_DIR.'/template-functions.php');
24
require_once(YARPP_DIR.'/class-widget.php');
25
26
// New in 3.2: load YARPP cache engine
27
// By default, this is tables, which uses custom db tables.
28
// Use postmeta instead and avoid custom tables by adding the following to wp-config:
@@ -40,3 +43,25 @@ function yarpp_init() {
40
if ( file_exists( YARPP_DIR . '/blogglue.php' ) )
41
include_once( YARPP_DIR . '/blogglue.php' );
42
}
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.4b9
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.4b9');
13
define('YARPP_DIR', dirname(__FILE__));
14
define('YARPP_NO_RELATED', ':(');
15
define('YARPP_RELATED', ':)');
17
define('YARPP_DONT_RUN', 'X(');
18
19
require_once(YARPP_DIR.'/class-core.php');
20
require_once(YARPP_DIR.'/related-functions.php');
21
require_once(YARPP_DIR.'/template-functions.php');
22
require_once(YARPP_DIR.'/class-widget.php');
23
24
+ if ( !defined('WP_CONTENT_URL') )
25
+ define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
26
+ if ( !defined('WP_CONTENT_DIR') )
27
+ define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
28
+
29
// New in 3.2: load YARPP cache engine
30
// By default, this is tables, which uses custom db tables.
31
// Use postmeta instead and avoid custom tables by adding the following to wp-config:
43
if ( file_exists( YARPP_DIR . '/blogglue.php' ) )
44
include_once( YARPP_DIR . '/blogglue.php' );
45
}
46
+
47
+ function yarpp_set_option($options, $value = null) {
48
+ global $yarpp;
49
+ $yarpp->set_option($options, $value);
50
+ }
51
+
52
+ function yarpp_get_option($option = null) {
53
+ global $yarpp;
54
+ return $yarpp->get_option($option);
55
+ }
56
+
57
+ // since 3.3.2: fix for WP 3.0.x
58
+ if ( !function_exists( 'self_admin_url' ) ) {
59
+ function self_admin_url($path = '', $scheme = 'admin') {
60
+ if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
61
+ return network_admin_url($path, $scheme);
62
+ elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
63
+ return user_admin_url($path, $scheme);
64
+ else
65
+ return admin_url($path, $scheme);
66
+ }
67
+ }