Yet Another Related Posts Plugin (YARPP) - Version 4.0.5

Version Description

  • Bugfix: Some upgrade code would try to access the global $yarpp before it was properly initialized
  • Added experimental graph data method to the YARPP table cache class
Download this release

Release Info

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

Code changes from version 4.0.5b1 to 4.0.5

Files changed (125) hide show
  1. class-admin.php +94 -32
  2. readme.txt +1 -1
  3. trunk/cache-postmeta.php +0 -262
  4. trunk/cache-tables.php +0 -316
  5. trunk/class-admin.php +0 -511
  6. trunk/class-cache.php +0 -620
  7. trunk/class-core.php +0 -1322
  8. trunk/class-widget.php +0 -143
  9. trunk/default.png +0 -0
  10. trunk/js/metabox.js +0 -28
  11. trunk/js/options.js +0 -274
  12. trunk/lang/words-bg_BG.php +0 -3
  13. trunk/lang/words-cs_CZ.php +0 -3
  14. trunk/lang/words-de_DE.php +0 -3
  15. trunk/lang/words-en_US.php +0 -3
  16. trunk/lang/words-fr_FR.php +0 -3
  17. trunk/lang/words-it_IT.php +0 -3
  18. trunk/lang/words-nl_NL.php +0 -3
  19. trunk/lang/words-pl_PL.php +0 -3
  20. trunk/lang/words-pt_BR.php +0 -3
  21. trunk/lang/yarpp-ar.mo +0 -0
  22. trunk/lang/yarpp-ar_AR.mo +0 -0
  23. trunk/lang/yarpp-ar_AR.po +0 -588
  24. trunk/lang/yarpp-ar_EG.mo +0 -0
  25. trunk/lang/yarpp-ar_EG.po +0 -582
  26. trunk/lang/yarpp-bg_BG.mo +0 -0
  27. trunk/lang/yarpp-bg_BG.po +0 -588
  28. trunk/lang/yarpp-by_BY.mo +0 -0
  29. trunk/lang/yarpp-by_BY.po +0 -569
  30. trunk/lang/yarpp-cs_CZ.mo +0 -0
  31. trunk/lang/yarpp-cs_CZ.po +0 -542
  32. trunk/lang/yarpp-de_DE.mo +0 -0
  33. trunk/lang/yarpp-de_DE.po +0 -572
  34. trunk/lang/yarpp-el_CY.mo +0 -0
  35. trunk/lang/yarpp-el_CY.po +0 -665
  36. trunk/lang/yarpp-el_EL.mo +0 -0
  37. trunk/lang/yarpp-el_EL.po +0 -665
  38. trunk/lang/yarpp-es_ES.mo +0 -0
  39. trunk/lang/yarpp-es_ES.po +0 -571
  40. trunk/lang/yarpp-fa_IR.mo +0 -0
  41. trunk/lang/yarpp-fa_IR.po +0 -569
  42. trunk/lang/yarpp-fr_FR.mo +0 -0
  43. trunk/lang/yarpp-fr_FR.po +0 -663
  44. trunk/lang/yarpp-gb_IR.mo +0 -0
  45. trunk/lang/yarpp-gb_IR.po +0 -573
  46. trunk/lang/yarpp-ge_KA.mo +0 -0
  47. trunk/lang/yarpp-ge_KA.po +0 -569
  48. trunk/lang/yarpp-he_IL.mo +0 -0
  49. trunk/lang/yarpp-he_IL.po +0 -708
  50. trunk/lang/yarpp-hi_IN.mo +0 -0
  51. trunk/lang/yarpp-hi_IN.po +0 -569
  52. trunk/lang/yarpp-hr.mo +0 -0
  53. trunk/lang/yarpp-hr.po +0 -634
  54. trunk/lang/yarpp-hu_HU.mo +0 -0
  55. trunk/lang/yarpp-hu_HU.po +0 -569
  56. trunk/lang/yarpp-id_ID.mo +0 -0
  57. trunk/lang/yarpp-id_ID.po +0 -569
  58. trunk/lang/yarpp-it_IT.mo +0 -0
  59. trunk/lang/yarpp-it_IT.po +0 -542
  60. trunk/lang/yarpp-ja.mo +0 -0
  61. trunk/lang/yarpp-ja.po +0 -739
  62. trunk/lang/yarpp-kk_KZ.mo +0 -0
  63. trunk/lang/yarpp-kk_KZ.po +0 -569
  64. trunk/lang/yarpp-ko_KR.mo +0 -0
  65. trunk/lang/yarpp-ko_KR.po +0 -542
  66. trunk/lang/yarpp-lt_LT.mo +0 -0
  67. trunk/lang/yarpp-lt_LT.po +0 -569
  68. trunk/lang/yarpp-lv_LV.mo +0 -0
  69. trunk/lang/yarpp-lv_LV.po +0 -569
  70. trunk/lang/yarpp-nb_NO.mo +0 -0
  71. trunk/lang/yarpp-nb_NO.po +0 -569
  72. trunk/lang/yarpp-nl_NL.mo +0 -0
  73. trunk/lang/yarpp-nl_NL.po +0 -542
  74. trunk/lang/yarpp-pl_PL.mo +0 -0
  75. trunk/lang/yarpp-pl_PL.po +0 -746
  76. trunk/lang/yarpp-pt_BR.mo +0 -0
  77. trunk/lang/yarpp-pt_BR.po +0 -569
  78. trunk/lang/yarpp-pt_PT.mo +0 -0
  79. trunk/lang/yarpp-pt_PT.po +0 -569
  80. trunk/lang/yarpp-ro_RO.mo +0 -0
  81. trunk/lang/yarpp-ro_RO.po +0 -568
  82. trunk/lang/yarpp-ru_RU.mo +0 -0
  83. trunk/lang/yarpp-ru_RU.po +0 -569
  84. trunk/lang/yarpp-sk_SK.mo +0 -0
  85. trunk/lang/yarpp-sk_SK.po +0 -543
  86. trunk/lang/yarpp-sl_SI.mo +0 -0
  87. trunk/lang/yarpp-sl_SI.po +0 -663
  88. trunk/lang/yarpp-sr_RS.mo +0 -0
  89. trunk/lang/yarpp-sr_RS.po +0 -542
  90. trunk/lang/yarpp-sv_SE.mo +0 -0
  91. trunk/lang/yarpp-sv_SE.po +0 -536
  92. trunk/lang/yarpp-tr_TR.mo +0 -0
  93. trunk/lang/yarpp-tr_TR.po +0 -568
  94. trunk/lang/yarpp-uk_UA.mo +0 -0
  95. trunk/lang/yarpp-uk_UA.po +0 -569
  96. trunk/lang/yarpp-uz_UZ.mo +0 -0
  97. trunk/lang/yarpp-uz_UZ.po +0 -569
  98. trunk/lang/yarpp-vi_VN.mo +0 -0
  99. trunk/lang/yarpp-vi_VN.po +0 -569
  100. trunk/lang/yarpp-zh_CN.mo +0 -0
  101. trunk/lang/yarpp-zh_CN.po +0 -569
  102. trunk/lang/yarpp-zh_TW.mo +0 -0
  103. trunk/lang/yarpp-zh_TW.po +0 -558
  104. trunk/options-meta-boxes.php +0 -436
  105. trunk/options.css +0 -338
  106. trunk/options.php +0 -206
  107. trunk/readme.txt +0 -744
  108. trunk/related-functions.php +0 -100
  109. trunk/sprites-2x.png +0 -0
  110. trunk/sprites.png +0 -0
  111. trunk/styles-thumbnails.php +0 -60
  112. trunk/template-builtin.php +0 -36
  113. trunk/template-buttons-2x.png +0 -0
  114. trunk/template-functions.php +0 -13
  115. trunk/template-metabox.php +0 -31
  116. trunk/template-thumbnails.php +0 -51
  117. trunk/template-widget.php +0 -14
  118. trunk/yarpp-templates/yarpp-template-example.php +0 -16
  119. trunk/yarpp-templates/yarpp-template-list.php +0 -20
  120. trunk/yarpp-templates/yarpp-template-photoblog.php +0 -19
  121. trunk/yarpp-templates/yarpp-template-random.php +0 -19
  122. trunk/yarpp-templates/yarpp-template-thumbnail.php +0 -19
  123. trunk/yarpp-templates/yarpp-template-wpml.php +0 -22
  124. trunk/yarpp.php +0 -60
  125. yarpp.php +2 -2
class-admin.php CHANGED
@@ -50,21 +50,49 @@ class YARPP_Admin {
50
51
function ui_register() {
52
global $wp_version;
53
if ( get_option( 'yarpp_activated' ) ) {
54
delete_option( 'yarpp_activated' );
55
delete_option( 'yarpp_upgraded' );
56
- add_action( 'admin_enqueue_scripts', array( $this, 'pointer_enqueue' ) );
57
- add_action( 'admin_print_footer_scripts', array( $this, 'pointer_script' ) );
58
- } elseif ( !$this->core->get_option('optin') &&
59
- current_user_can('manage_options') &&
60
- !get_user_option( 'yarpp_saw_optin' )
61
- ) {
62
- add_action( 'admin_notices', array( $this, 'optin_notice' ) );
63
} elseif ( !$this->core->get_option('optin') &&
64
current_user_can('manage_options') &&
65
get_option( 'yarpp_upgraded' )
66
) {
67
add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
68
}
69
70
if ( $this->core->get_option('optin') )
@@ -154,6 +182,7 @@ class YARPP_Admin {
154
if ( !$this->core->get_option( 'optin' ) ) {
155
echo '<p>';
156
$this->print_optin_button();
157
echo '</p>';
158
}
159
@@ -162,52 +191,72 @@ class YARPP_Admin {
162
echo '<p>' . __( "In addition, YARPP also loads an invisible pixel image with your YARPP results to know how often YARPP is being used.", 'yarpp' ) . '</p>';
163
}
164
165
- function print_optin_button() {
166
- echo '<a id="yarpp-optin-button" class="button">' . __('Send settings and usage data back to YARPP', 'yarpp') . '</a><span class="yarpp-thankyou" style="display:none"><strong>' . __('Thank you!', 'yarpp') . '</strong></span>';
167
wp_nonce_field( 'yarpp_optin', 'yarpp_optin-nonce', false );
168
echo "<script type='text/javascript'>
169
jQuery(function($){
170
- $('#yarpp-optin-button').click(function() {
171
- $(this)
172
- .hide()
173
- .siblings('.yarpp-thankyou').show('slow');
174
- $('#yarpp-optin').attr('checked', true);
175
- $.ajax({type:'POST',
176
- url: ajaxurl,
177
- data: {
178
- action: 'yarpp_optin',
179
- '_ajax_nonce': $('#yarpp_optin-nonce').val()
180
- }});
181
- });
182
});
183
</script>\n";
184
}
185
186
- function optin_notice( $upgrade = false ) {
187
$screen = get_current_screen();
188
if ( is_null($screen) || $screen->id == 'settings_page_yarpp' )
189
return;
190
191
- if ( $upgrade ) {
192
- delete_option( 'yarpp_upgraded' );
193
- } else {
194
- $user = get_current_user_id();
195
- update_user_option( $user, 'yarpp_saw_optin', true );
196
}
197
198
echo '<div class="updated fade"><p>';
199
- if ( $upgrade )
200
echo '<strong>' . sprintf( __('%1$s updated successfully.'), 'Yet Another Related Posts Plugin' ) . '</strong> ';
201
_e( "<strong>Help make YARPP better</strong> by sending information about YARPP's settings and usage statistics.", 'yarpp' );
202
203
echo '</p><p>';
204
$this->print_optin_button();
205
echo '<a class="button" href="options-general.php?page=yarpp#help-optin">' . __( 'Learn More', 'yarpp' ) . '</a>';
206
echo '</p></div>';
207
}
208
209
function upgrade_notice() {
210
- $this->optin_notice( true );
211
}
212
213
// faux-markdown, required for the help text rendering
@@ -277,9 +326,18 @@ class YARPP_Admin {
277
wp_enqueue_style( 'wp-pointer' );
278
wp_enqueue_script( 'wp-pointer' );
279
}
280
- function pointer_script() {
281
$content = '<h3>' . str_replace('<span>', '<span style="font-style:italic; font-weight: inherit;">', __('Thank you for installing <span>Yet Another Related Posts Plugin</span>!', 'yarpp') ) . '</h3>';
282
- $content .= '<p>' . str_replace('<a>', '<a href="' . esc_url(admin_url('options-general.php?page=yarpp')) .'">', __('Make sure to visit the <a>Related Posts settings page</a> to customize YARPP.', 'yarpp') ). '</p>';
283
?>
284
<script>
285
jQuery(function () {
@@ -321,7 +379,11 @@ jQuery(function () {
321
</script>
322
<?php
323
}
324
-
325
function settings_link($links, $file) {
326
$this_plugin = dirname(plugin_basename(__FILE__)) . '/yarpp.php';
327
if($file == $this_plugin) {
50
51
function ui_register() {
52
global $wp_version;
53
+
54
if ( get_option( 'yarpp_activated' ) ) {
55
delete_option( 'yarpp_activated' );
56
delete_option( 'yarpp_upgraded' );
57
+
58
+ if ( $this->core->get_option('optin') ) {
59
+ $install_notice = 0; // default
60
+ } else {
61
+ $install_notice = $this->core->get_option( 'pools[install_notice]' );
62
+ if ( is_null( $install_notice ) ) {
63
+ $install_notice = mt_rand(0, 2);
64
+ $pools = $this->core->get_option( 'pools' );
65
+ if ( !is_array($pools) )
66
+ $pools = array();
67
+ $pools['install_notice'] = $install_notice;
68
+ $this->core->set_option( 'pools', $pools );
69
+ }
70
+ }
71
+
72
+ switch ( $install_notice ) {
73
+ case 1:
74
+ add_action( 'admin_enqueue_scripts', array( $this, 'pointer_enqueue' ) );
75
+ add_action( 'admin_print_footer_scripts', array( $this, 'optin_button_script' ) );
76
+ add_action( 'admin_print_footer_scripts', array( $this, 'pointer_script_variant' ) );
77
+ break;
78
+ case 2:
79
+ add_action( 'admin_notices', array( $this, 'install_notice' ) );
80
+ break;
81
+ default:
82
+ add_action( 'admin_enqueue_scripts', array( $this, 'pointer_enqueue' ) );
83
+ add_action( 'admin_print_footer_scripts', array( $this, 'pointer_script' ) );
84
+ break;
85
+ }
86
} elseif ( !$this->core->get_option('optin') &&
87
current_user_can('manage_options') &&
88
get_option( 'yarpp_upgraded' )
89
) {
90
add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
91
+ } elseif ( !$this->core->get_option('optin') &&
92
+ current_user_can('manage_options') &&
93
+ !get_user_option( 'yarpp_saw_optin' )
94
+ ) {
95
+ add_action( 'admin_notices', array( $this, 'optin_notice' ) );
96
}
97
98
if ( $this->core->get_option('optin') )
182
if ( !$this->core->get_option( 'optin' ) ) {
183
echo '<p>';
184
$this->print_optin_button();
185
+ $this->optin_button_script();
186
echo '</p>';
187
}
188
191
echo '<p>' . __( "In addition, YARPP also loads an invisible pixel image with your YARPP results to know how often YARPP is being used.", 'yarpp' ) . '</p>';
192
}
193
194
+ function the_optin_button() {
195
+ return '<a id="yarpp-optin-button" class="button">' . __('Send settings and usage data back to YARPP', 'yarpp') . '</a><span class="yarpp-thankyou" style="display:none; margin-right: 10px;"><strong>' . __('Thank you!', 'yarpp') . '</strong></span>';
196
+ }
197
+
198
+ function optin_button_script() {
199
wp_nonce_field( 'yarpp_optin', 'yarpp_optin-nonce', false );
200
echo "<script type='text/javascript'>
201
jQuery(function($){
202
+ $(document.body).on('click', '#yarpp-optin-button', function() {
203
+ console.log(this);
204
+ $(this)
205
+ .hide()
206
+ .siblings('.yarpp-thankyou').show('slow');
207
+ $('#yarpp-optin').attr('checked', true);
208
+ $.ajax({type:'POST',
209
+ url: ajaxurl,
210
+ data: {
211
+ action: 'yarpp_optin',
212
+ '_ajax_nonce': $('#yarpp_optin-nonce').val()
213
+ }
214
+ });
215
+ });
216
});
217
</script>\n";
218
}
219
+
220
+ function print_optin_button() {
221
+ echo $this->the_optin_button();
222
+ }
223
224
+ function optin_notice( $type = false ) {
225
$screen = get_current_screen();
226
if ( is_null($screen) || $screen->id == 'settings_page_yarpp' )
227
return;
228
229
+ switch ( $type ) {
230
+ case 'upgrade':
231
+ delete_option( 'yarpp_upgraded' );
232
+ break;
233
+ case 'install':
234
+ break;
235
+ default:
236
+ $user = get_current_user_id();
237
+ update_user_option( $user, 'yarpp_saw_optin', true );
238
}
239
240
echo '<div class="updated fade"><p>';
241
+ if ( $type == 'upgrade' )
242
echo '<strong>' . sprintf( __('%1$s updated successfully.'), 'Yet Another Related Posts Plugin' ) . '</strong> ';
243
+ if ( $type == 'install' )
244
+ echo '<strong>' . str_replace('<span>', '<span style="font-style:italic; font-weight: inherit;">', __('Thank you for installing <span>Yet Another Related Posts Plugin</span>!', 'yarpp') ) . '</strong> ';
245
_e( "<strong>Help make YARPP better</strong> by sending information about YARPP's settings and usage statistics.", 'yarpp' );
246
247
echo '</p><p>';
248
$this->print_optin_button();
249
+ $this->optin_button_script();
250
echo '<a class="button" href="options-general.php?page=yarpp#help-optin">' . __( 'Learn More', 'yarpp' ) . '</a>';
251
echo '</p></div>';
252
}
253
254
function upgrade_notice() {
255
+ $this->optin_notice( 'upgrade' );
256
+ }
257
+
258
+ function install_notice() {
259
+ $this->optin_notice( 'install' );
260
}
261
262
// faux-markdown, required for the help text rendering
326
wp_enqueue_style( 'wp-pointer' );
327
wp_enqueue_script( 'wp-pointer' );
328
}
329
+ function pointer_script( $variant = false ) {
330
$content = '<h3>' . str_replace('<span>', '<span style="font-style:italic; font-weight: inherit;">', __('Thank you for installing <span>Yet Another Related Posts Plugin</span>!', 'yarpp') ) . '</h3>';
331
+
332
+ if ( $variant ) {
333
+ $content .= '<p>' . __( "<strong>Help make YARPP better</strong> by sending information about YARPP's settings and usage statistics.", 'yarpp' ) . '</p><p>' . $this->the_optin_button();
334
+
335
+ $content .= '<a class="button" style="margin-top: 10px" href="options-general.php?page=yarpp#help-optin">' . __( 'Learn More', 'yarpp' ) . '</a>' . '</p>';
336
+
337
+ $content = str_replace("'", "\\'", $content);
338
+ } else {
339
+ $content .= '<p>' . str_replace('<a>', '<a href="' . esc_url(admin_url('options-general.php?page=yarpp')) .'">', __('Make sure to visit the <a>Related Posts settings page</a> to customize YARPP.', 'yarpp') ). '</p>';
340
+ }
341
?>
342
<script>
343
jQuery(function () {
379
</script>
380
<?php
381
}
382
+
383
+ function pointer_script_variant() {
384
+ $this->pointer_script( true );
385
+ }
386
+
387
function settings_link($links, $file) {
388
$this_plugin = dirname(plugin_basename(__FILE__)) . '/yarpp.php';
389
if($file == $this_plugin) {
readme.txt CHANGED
@@ -7,7 +7,7 @@ Donate link: http://tinyurl.com/donatetomitcho
7
Tags: related, posts, post, pages, page, RSS, feed, feeds
8
Requires at least: 3.3
9
Tested up to: 3.6
10
- Stable tag: 4.0.4
11
License: GPLv2 or later
12
13
Display a list of related entries on your site and feeds based on a unique algorithm. Now with thumbnail support built-in!
7
Tags: related, posts, post, pages, page, RSS, feed, feeds
8
Requires at least: 3.3
9
Tested up to: 3.6
10
+ Stable tag: 4.0.5
11
License: GPLv2 or later
12
13
Display a list of related entries on your site and feeds based on a unique algorithm. Now with thumbnail support built-in!
trunk/cache-postmeta.php DELETED
@@ -1,262 +0,0 @@
1
- <?php
2
-
3
- $yarpp_storage_class = 'YARPP_Cache_Postmeta';
4
-
5
- define('YARPP_POSTMETA_KEYWORDS_KEY','_yarpp_keywords');
6
- define('YARPP_POSTMETA_RELATED_KEY', '_yarpp_related');
7
-
8
- class YARPP_Cache_Postmeta extends YARPP_Cache {
9
-
10
- public $name = "postmeta";
11
-
12
- // variables used for lookup
13
- private $related_postdata = array();
14
- private $related_IDs = array();
15
-
16
- /**
17
- * SETUP/STATUS
18
- */
19
- function __construct( &$core ) {
20
- parent::__construct( $core );
21
- }
22
-
23
- public function is_enabled() {
24
- return true; // always enabled.
25
- }
26
-
27
- public function setup() {
28
- }
29
-
30
- public function upgrade( $last_version ) {
31
- if ( $last_version && version_compare('3.4b1', $last_version) > 0 ) {
32
- // 3.4 moved _yarpp_body_keywords and _yarpp_title_keywords into the single
33
- // postmeta _yarpp_keywords, so clear out the old data now.
34
- delete_post_meta_by_key('_yarpp_title_keywords');
35
- delete_post_meta_by_key('_yarpp_body_keywords');
36
- }
37
- }
38
-
39
- public function cache_status() {
40
- global $wpdb;
41
- return $wpdb->get_var("select (count(p.ID)-sum(m.meta_value IS NULL))/count(p.ID)
42
- FROM `{$wpdb->posts}` as p
43
- LEFT JOIN `{$wpdb->postmeta}` as m ON (p.ID = m.post_id and m.meta_key = '" . YARPP_POSTMETA_RELATED_KEY . "')
44
- WHERE p.post_status = 'publish'");
45
- }
46
-
47
- public function uncached($limit = 20, $offset = 0) {
48
- global $wpdb;
49
- return $wpdb->get_col("select SQL_CALC_FOUND_ROWS p.ID
50
- FROM `{$wpdb->posts}` as p
51
- LEFT JOIN `{$wpdb->postmeta}` as m ON (p.ID = m.post_id and m.meta_key = '" . YARPP_POSTMETA_RELATED_KEY . "')
52
- WHERE p.post_status = 'publish' and m.meta_value IS NULL
53
- LIMIT $limit OFFSET $offset");
54
- }
55
-
56
- public function stats() {
57
- global $wpdb;
58
- return wp_list_pluck($wpdb->get_results("select num, count(*) as ct from (select 0 + if(meta_value = '" . YARPP_NO_RELATED . "', 0, substring(substring_index(meta_value,':',2),3)) as num from `{$wpdb->postmeta}` where meta_key = '" . YARPP_POSTMETA_RELATED_KEY . "') as t group by num order by num asc", OBJECT_K), 'ct');
59
- }
60
-
61
- /**
62
- * MAGIC FILTERS
63
- */
64
- public function where_filter($arg) {
65
- global $wpdb;
66
- // modify the where clause to use the related ID list.
67
- if (!count($this->related_IDs))
68
- $this->related_IDs = array(0);
69
- $arg = preg_replace("!{$wpdb->posts}.ID = \d+!","{$wpdb->posts}.ID in (".join(',',$this->related_IDs).")",$arg);
70
-
71
- // if recent is set, add an additional condition
72
- $recent = $this->core->get_option('recent');
73
- if ( !!$recent )
74
- $arg .= " and post_date > date_sub(now(), interval {$recent}) ";
75
- return $arg;
76
- }
77
-
78
- public function orderby_filter($arg) {
79
- global $wpdb;
80
- // only order by score if the score function is added in fields_filter, which only happens
81
- // if there are related posts in the postdata
82
- if ($this->score_override &&
83
- is_array($this->related_postdata) && count($this->related_postdata))
84
- return str_replace("$wpdb->posts.post_date","score",$arg);
85
- return $arg;
86
- }
87
-
88
- public function fields_filter($arg) {
89
- global $wpdb;
90
- if (is_array($this->related_postdata) && count($this->related_postdata)) {
91
- $scores = array();
92
- foreach ($this->related_postdata as $related_entry) {
93
- $scores[] = " WHEN {$related_entry['ID']} THEN {$related_entry['score']}";
94
- }
95
- $arg .= ", CASE {$wpdb->posts}.ID" . join('',$scores) ." END as score";
96
- }
97
- return $arg;
98
- }
99
-
100
- public function limit_filter($arg) {
101
- if ($this->online_limit)
102
- return " limit {$this->online_limit} ";
103
- return $arg;
104
- }
105
-
106
- /**
107
- * RELATEDNESS CACHE CONTROL
108
- */
109
- public function begin_yarpp_time($reference_ID) {
110
- $this->yarpp_time = true;
111
- // get the related posts from postmeta, and also construct the relate_IDs array
112
- $this->related_postdata = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
113
- if (is_array($this->related_postdata) && count($this->related_postdata))
114
- $this->related_IDs = wp_list_pluck( $this->related_postdata, 'ID' );
115
- add_filter('posts_where',array(&$this,'where_filter'));
116
- add_filter('posts_orderby',array(&$this,'orderby_filter'));
117
- add_filter('posts_fields',array(&$this,'fields_filter'));
118
- add_filter('post_limits',array(&$this,'limit_filter'));
119
- add_action('pre_get_posts',array(&$this,'add_signature'));
120
- // sets the score override flag.
121
- add_action('parse_query',array(&$this,'set_score_override_flag'));
122
- }
123
-
124
- public function end_yarpp_time() {
125
- $this->yarpp_time = false;
126
- $this->related_IDs = array();
127
- $this->related_postdata = array();
128
- remove_filter('posts_where',array(&$this,'where_filter'));
129
- remove_filter('posts_orderby',array(&$this,'orderby_filter'));
130
- remove_filter('posts_fields',array(&$this,'fields_filter'));
131
- remove_filter('post_limits',array(&$this,'limit_filter'));
132
- remove_action('pre_get_posts',array(&$this,'add_signature'));
133
- // sets the score override flag.
134
- remove_action('parse_query',array(&$this,'set_score_override_flag'));
135
- }
136
-
137
- // @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
138
- public function is_cached($reference_ID) {
139
- $related = get_post_meta($reference_ID, YARPP_POSTMETA_RELATED_KEY, true);
140
- if ( YARPP_NO_RELATED === $related )
141
- return YARPP_NO_RELATED;
142
- if ( '' == $related )
143
- return YARPP_NOT_CACHED;
144
- return YARPP_RELATED;
145
- }
146
-
147
- public function clear( $reference_IDs ) {
148
- $reference_IDs = wp_parse_id_list( $reference_IDs );
149
-
150
- if ( !count($reference_IDs) )
151
- return;
152
-
153
- // clear each cache
154
- foreach( $reference_IDs as $id ) {
155
- delete_post_meta( $id, YARPP_POSTMETA_RELATED_KEY );
156
- delete_post_meta( $id, YARPP_POSTMETA_KEYWORDS_KEY );
157
- }
158
- }
159
-
160
- // @return YARPP_NO_RELATED | YARPP_RELATED
161
- // @used by enforce
162
- protected function update($reference_ID) {
163
- global $wpdb;
164
-
165
- $original_related = $this->related($reference_ID);
166
- $related = $wpdb->get_results($this->sql($reference_ID), ARRAY_A);
167
- $new_related = wp_list_pluck( $related, 'ID' );
168
-
169
- if ( count($new_related) ) {
170
- update_post_meta($reference_ID, YARPP_POSTMETA_RELATED_KEY, $related);
171
- if ($this->core->debug) echo "<!--YARPP just set the cache for post $reference_ID-->";
172
-
173
- // Clear the caches of any items which are no longer related or are newly related.
174
- if (count($original_related)) {
175
- $this->clear(array_diff($original_related, $new_related));
176
- $this->clear(array_diff($new_related, $original_related));
177
- }
178
-
179
- return YARPP_RELATED;
180
- } else {
181
- update_post_meta($reference_ID, YARPP_POSTMETA_RELATED_KEY, YARPP_NO_RELATED);
182
-
183
- // Clear the caches of those which are no longer related.
184
- if (count($original_related))
185
- $this->clear($original_related);
186
-
187
- return YARPP_NO_RELATED;
188
- }
189
- }
190
-
191
- public function flush() {
192
- delete_post_meta_by_key( YARPP_POSTMETA_RELATED_KEY );
193
- delete_post_meta_by_key( YARPP_POSTMETA_KEYWORDS_KEY );
194
- }
195
-
196
- public function related($reference_ID = null, $related_ID = null) {
197
- global $wpdb;
198
-
199
- if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
200
- _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
201
- return;
202
- }
203
-
204
- if (!is_null($reference_ID) && !is_null($related_ID)) {
205
- $results = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
206
- foreach($results as $result) {
207
- if ($result['ID'] == $related_ID)
208
- return true;
209
- }
210
- return false;
211
- }
212
-
213
- // return a list of ID's of "related" entries
214
- if (!is_null($reference_ID)) {
215
- $results = get_post_meta($reference_ID,YARPP_POSTMETA_RELATED_KEY,true);
216
- if (!$results || $results == YARPP_NO_RELATED)
217
- return array();
218
- return wp_list_pluck( $results, 'ID' );
219
- }
220
-
221
- // return a list of entities which list this post as "related"
222
- if (!is_null($related_ID)) {
223
- return $wpdb->get_col("select post_id from `{$wpdb->postmeta}` where meta_key = '" . YARPP_POSTMETA_RELATED_KEY . "' and meta_value regexp 's:2:\"ID\";s:\d+:\"{$related_ID}\"'");
224
- }
225
-
226
- return false;
227
- }
228
-
229
- /**
230
- * KEYWORDS CACHE CONTROL
231
- */
232
-
233
- // @return (array) with body and title keywords
234
- private function cache_keywords($ID) {
235
- $keywords = array(
236
- 'body' => $this->body_keywords($ID),
237
- 'title' => $this->title_keywords($ID)
238
- );
239
- update_post_meta($ID, YARPP_POSTMETA_KEYWORDS_KEY, $keywords);
240
- return $keywords;
241
- }
242
-
243
- // @param $ID (int)
244
- // @param $type (string) body | title | all
245
- // @return (string|array) depending on whether "all" were requested or not
246
- public function get_keywords( $ID, $type = 'all' ) {
247
- if ( !$ID = absint($ID) )
248
- return false;
249
-
250
- $keywords = get_post_meta($ID, YARPP_POSTMETA_KEYWORDS_KEY, true);
251
-
252
- if ( empty($keywords) ) // if empty, try caching them first.
253
- $keywords = $this->cache_keywords($ID);
254
-
255
- if ( empty($keywords) )
256
- return false;
257
-
258
- if ( 'all' == $type )
259
- return $keywords;
260
- return $keywords[$type];
261
- }
262
- }
trunk/cache-tables.php DELETED
@@ -1,316 +0,0 @@
1
- <?php
2
-
3
- $yarpp_storage_class = 'YARPP_Cache_Tables';
4
-
5
- define('YARPP_TABLES_RELATED_TABLE', 'yarpp_related_cache');
6
-
7
- class YARPP_Cache_Tables extends YARPP_Cache {
8
- public $name = "custom tables";
9
-
10
- /**
11
- * SETUP/STATUS
12
- */
13
- function __construct( &$core ) {
14
- parent::__construct( $core );
15
- }
16
-
17
- public function is_enabled() {
18
- global $wpdb;
19
- // now check for the cache tables
20
- $tabledata = $wpdb->get_col("show tables");
21
- if (in_array($wpdb->prefix . YARPP_TABLES_RELATED_TABLE,$tabledata) !== false)
22
- return true;
23
- else
24
- return false;
25
- }
26
-
27
- public function setup() {
28
- global $wpdb;
29
-
30
- $charset_collate = '';
31
- if ( ! empty( $wpdb->charset ) )
32
- $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
33
- if ( ! empty( $wpdb->collate ) )
34
- $charset_collate .= " COLLATE $wpdb->collate";
35
-
36
- $wpdb->query("CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . "` (
37
- `reference_ID` bigint(20) unsigned NOT NULL default '0',
38
- `ID` bigint(20) unsigned NOT NULL default '0',
39
- `score` float unsigned NOT NULL default '0',
40
- `date` timestamp NOT NULL default CURRENT_TIMESTAMP,
41
- PRIMARY KEY ( `reference_ID` , `ID` ),
42
- INDEX (`score`), INDEX (`ID`)
43
- ) $charset_collate;");
44
- }
45
-
46
- public function upgrade($last_version) {
47
- global $wpdb;
48
- if ( $last_version && version_compare('3.2.1b4', $last_version) > 0 ) {
49
- // Change primary key to be (reference_ID, ID) to ensure that we don't
50
- // get duplicates.
51
- // We unfortunately have to clear the cache first here, to ensure that there
52
- // are no duplicates.
53
- $this->flush();
54
- $wpdb->query('ALTER TABLE ' . $wpdb->prefix . YARPP_TABLES_RELATED_TABLE .
55
- ' DROP PRIMARY KEY ,' .
56
- ' ADD PRIMARY KEY ( `reference_ID` , `ID` ),' .
57
- ' ADD INDEX (`score`), ADD INDEX (`ID`)');
58
- }
59
- if ( $last_version && version_compare('3.5.2b3', $last_version) > 0 ) {
60
- // flush object cache, as bad is_cached_* values were stored before
61
- wp_cache_flush();
62
- }
63
- if ( $last_version && version_compare('3.6b1', $last_version) > 0 ) {
64
- // remove keywords table
65
- if ( defined('YARPP_TABLES_KEYWORDS_TABLE') )
66
- $old_keywords_table = $wpdb->prefix . YARPP_TABLES_KEYWORDS_TABLE;
67
- else
68
- $old_keywords_table = $wpdb->prefix . 'yarpp_keyword_cache';
69
-
70
- $wpdb->query("drop table if exists `$old_keywords_table`");
71
- }
72
- }
73
-
74
- public function cache_status() {
75
- global $wpdb;
76
- return $wpdb->get_var("select (count(p.ID)-sum(c.ID IS NULL))/count(p.ID)
77
- FROM `{$wpdb->posts}` as p
78
- LEFT JOIN `{$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . "` as c ON (p.ID = c.reference_ID)
79
- WHERE p.post_status = 'publish' ");
80
- }
81
-
82
- public function uncached($limit = 20, $offset = 0) {
83
- global $wpdb;
84
- return $wpdb->get_col("select SQL_CALC_FOUND_ROWS p.ID
85
- FROM `{$wpdb->posts}` as p
86
- LEFT JOIN `{$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . "` as c ON (p.ID = c.reference_ID)
87
- WHERE p.post_status = 'publish' and c.ID IS NULL
88
- LIMIT $limit OFFSET $offset");
89
- }
90
-
91
- public function stats() {
92
- global $wpdb;
93
- return wp_list_pluck($wpdb->get_results("select num, count(*) as ct from (select 0 + if(id = 0, 0, count(ID)) as num from {$wpdb->prefix}yarpp_related_cache group by reference_ID) as t group by num order by num asc", OBJECT_K), 'ct');
94
- }
95
-
96
- public function graph_data( $threshold = 5 ) {
97
- global $wpdb;
98
-
99
- $threshold = absint($threshold);
100
- $results = $wpdb->get_results("select pair, sum(score) as score from
101
- ((select concat(reference_ID, '-', ID) as pair, score from {$wpdb->prefix}yarpp_related_cache where reference_ID < ID)
102
- union
103
- (select concat(ID, '-', reference_ID) as pair, score from {$wpdb->prefix}yarpp_related_cache where ID < reference_ID)) as t
104
- group by pair
105
- having sum(score) > {$threshold}");
106
- return $results;
107
- }
108
-
109
- /**
110
- * MAGIC FILTERS
111
- */
112
- public function join_filter($arg) {
113
- global $wpdb;
114
- if ($this->yarpp_time)
115
- $arg .= " join {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " as yarpp on {$wpdb->posts}.ID = yarpp.ID";
116
- return $arg;
117
- }
118
-
119
- public function where_filter($arg) {
120
- global $wpdb;
121
- $threshold = $this->core->get_option('threshold');
122
- if ($this->yarpp_time) {
123
-
124
- $arg = str_replace("$wpdb->posts.ID = ","yarpp.score >= $threshold and yarpp.reference_ID = ",$arg);
125
-
126
- $recent = $this->core->get_option('recent');
127
- if ( !!$recent )
128
- $arg .= " and post_date > date_sub(now(), interval {$recent}) ";
129
- }
130
- return $arg;
131
- }
132
-
133
- public function orderby_filter($arg) {
134
- global $wpdb;
135
- if ($this->yarpp_time and $this->score_override)
136
- $arg = str_replace("$wpdb->posts.post_date","yarpp.score",$arg);
137
- return $arg;
138
- }
139
-
140
- public function fields_filter($arg) {
141
- global $wpdb;
142
- if ($this->yarpp_time)
143
- $arg .= ", yarpp.score";
144
- return $arg;
145
- }
146
-
147
- public function limit_filter($arg) {
148
- global $wpdb;
149
- if ($this->yarpp_time and $this->online_limit) {
150
- return " limit {$this->online_limit} ";
151
- }
152
- return $arg;
153
- }
154
-
155
- /**
156
- * RELATEDNESS CACHE CONTROL
157
- */
158
- public function begin_yarpp_time() {
159
- $this->yarpp_time = true;
160
- add_filter('posts_join',array(&$this,'join_filter'));
161
- add_filter('posts_where',array(&$this,'where_filter'));
162
- add_filter('posts_orderby',array(&$this,'orderby_filter'));
163
- add_filter('posts_fields',array(&$this,'fields_filter'));
164
- add_filter('post_limits',array(&$this,'limit_filter'));
165
- add_action('pre_get_posts',array(&$this,'add_signature'));
166
- // sets the score override flag.
167
- add_action('parse_query',array(&$this,'set_score_override_flag'));
168
- }
169
-
170
- public function end_yarpp_time() {
171
- $this->yarpp_time = false;
172
- remove_filter('posts_join',array(&$this,'join_filter'));
173
- remove_filter('posts_where',array(&$this,'where_filter'));
174
- remove_filter('posts_orderby',array(&$this,'orderby_filter'));
175
- remove_filter('posts_fields',array(&$this,'fields_filter'));
176
- remove_filter('post_limits',array(&$this,'limit_filter'));
177
- remove_action('pre_get_posts',array(&$this,'add_signature'));
178
- remove_action('parse_query',array(&$this,'set_score_override_flag'));
179
- }
180
-
181
- // @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
182
- public function is_cached($reference_ID) {
183
- global $wpdb;
184
-
185
- $result = wp_cache_get( 'is_cached_' . $reference_ID, 'yarpp' );
186
- if ( false !== $result )
187
- return $result;
188
-
189
- // @since 3.5.3b3: check for max instead of min, so that if ID=0 and ID=X
190
- // are both saved, we act like there *are* related posts, because there are.
191
- $max_id = $wpdb->get_var("select max(ID) as max_id from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where reference_ID = $reference_ID");
192
-
193
- if ( is_null( $max_id ) )
194
- return YARPP_NOT_CACHED;
195
-
196
- if ( 0 == $max_id )
197
- $result = YARPP_NO_RELATED;
198
- else
199
- $result = YARPP_RELATED;
200
-
201
- wp_cache_set( 'is_cached_' . $reference_ID, $result, 'yarpp' );
202
-
203
- return $result;
204
- }
205
-
206
- public function clear( $reference_IDs ) {
207
- global $wpdb;
208
-
209
- $reference_IDs = wp_parse_id_list( $reference_IDs );
210
-
211
- if ( !count($reference_IDs) )
212
- return;
213
-
214
- $wpdb->query("delete from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where reference_ID in (".implode(',',$reference_IDs).")");
215
- // @since 3.5.2: clear is_cached_* values as well
216
- foreach ( $reference_IDs as $id )
217
- wp_cache_delete( 'is_cached_' . $id, 'yarpp' );
218
- }
219
-
220
- // @return YARPP_RELATED | YARPP_NO_RELATED
221
- // @used by enforce
222
- protected function update($reference_ID) {
223
- global $wpdb;
224
-
225
- $original_related = (array) @$this->related($reference_ID);
226
-
227
- if ( count($original_related) ) {
228
- // clear out the cruft
229
- $this->clear($reference_ID);
230
- }
231
-
232
- $wpdb->query("insert into {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " (reference_ID,ID,score) " . $this->sql($reference_ID) . " on duplicate key update date = now()");
233
-
234
- // If there were related entries saved...
235
- if ( $wpdb->rows_affected ) {
236
- $new_related = $this->related($reference_ID);
237
-
238
- if ($this->core->debug) echo "<!--YARPP just set the cache for post $reference_ID-->";
239
-
240
- // Clear the caches of any items which are no longer related or are newly related.
241
- if ( count($original_related) ) {
242
- $this->clear(array_diff($original_related, $new_related));
243
- $this->clear(array_diff($new_related, $original_related));
244
- }
245
-
246
- return YARPP_RELATED;
247
- } else {
248
- $wpdb->query("insert into {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " (reference_ID,ID,score) values ($reference_ID,0,0) on duplicate key update date = now()");
249
-
250
- // Clear the caches of those which are no longer related.
251
- if ( count($original_related) )
252
- $this->clear($original_related);
253
-
254
- return YARPP_NO_RELATED;
255
- }
256
- }
257
-
258
- public function flush() {
259
- global $wpdb;
260
- $wpdb->query("truncate table `{$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . "`");
261
- // @since 3.5.2: clear object cache, used for is_cached_* values
262
- wp_cache_flush();
263
- }
264
-
265
- public function related($reference_ID = null, $related_ID = null) {
266
- global $wpdb;
267
-
268
- if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
269
- _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
270
- return;
271
- }
272
-
273
- if (!is_null($reference_ID) && !is_null($related_ID)) {
274
- $results = $wpdb->get_col("select ID from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where reference_ID = $reference_ID and ID = $related_ID");
275
- return count($results) > 0;
276
- }
277
-
278
- // return a list of ID's of "related" entries
279
- if ( !is_null($reference_ID) ) {
280
- return $wpdb->get_col("select distinct ID from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where reference_ID = $reference_ID and ID != 0");
281
- }
282
-
283
- // return a list of entities which list this post as "related"
284
- if ( !is_null($related_ID) ) {
285
- return $wpdb->get_col("select distinct reference_ID from {$wpdb->prefix}" . YARPP_TABLES_RELATED_TABLE . " where ID = $related_ID");
286
- }
287
-
288
- return false;
289
- }
290
-
291
- // @param $ID (int)
292
- // @param $type (string) body | title | all
293
- // @return (string|array) depending on whether "all" were requested or not
294
- public function get_keywords( $ID, $type = 'all' ) {
295
- global $wpdb;
296
-
297
- if ( !is_int($ID) )
298
- return false;
299
-
300
- // @since 4: compute fresh each time, instead of using cache table.
301
- // the old keyword cache would basically have to be recomputed every time the
302
- // relatedness cache was recomputed, but no more, so there's no point in keeping
303
- // these around separately.
304
- $keywords = array(
305
- 'body' => $this->body_keywords($ID),
306
- 'title' => $this->title_keywords($ID)
307
- );
308
-
309
- if ( empty($keywords) )
310
- return false;
311
-
312
- if ( 'all' == $type )
313
- return $keywords;
314
- return $keywords[$type];
315
- }
316
- }
trunk/class-admin.php DELETED
@@ -1,511 +0,0 @@
1
- <?php
2
-
3
- class YARPP_Admin {
4
- public $core;
5
- public $hook;
6
-
7
- function __construct( &$core ) {
8
- $this->core = &$core;
9
-
10
- // if action=flush and the nonce is correct, reset the cache
11
- if ( isset($_GET['action']) && $_GET['action'] == 'flush' &&
12
- check_ajax_referer( 'yarpp_cache_flush', false, false ) !== false ) {
13
- $this->core->cache->flush();
14
- wp_redirect( admin_url( '/options-general.php?page=yarpp' ) );
15
- exit;
16
- }
17
-
18
- // if action=copy_templates and the nonce is correct, copy templates
19
- if ( isset($_GET['action']) && $_GET['action'] == 'copy_templates' &&
20
- check_ajax_referer( 'yarpp_copy_templates', false, false ) !== false ) {
21
- $this->copy_templates();
22
- wp_redirect( admin_url( '/options-general.php?page=yarpp' ) );
23
- exit;
24
- }
25
-
26
- add_action( 'admin_init', array( $this, 'ajax_register' ) );
27
- add_action( 'admin_menu', array( $this, 'ui_register' ) );
28
- add_filter( 'current_screen', array( $this, 'settings_screen' ) );
29
- add_filter( 'screen_settings', array( $this, 'render_screen_settings' ), 10, 2 );
30
- // new in 3.3: set default meta boxes to show:
31
- add_filter( 'default_hidden_meta_boxes', array( $this, 'default_hidden_meta_boxes' ), 10, 2 );
32
- }
33
-
34
- public function get_templates() {
35
- // @since 4.0.3 mv function to Core
36
- return $this->core->get_templates();
37
- }
38
-
39
- function ajax_register() {
40
- // Register AJAX services
41
- if ( defined('DOING_AJAX') && DOING_AJAX ) {
42
- add_action( 'wp_ajax_yarpp_display_exclude_terms', array( $this, 'ajax_display_exclude_terms' ) );
43
- add_action( 'wp_ajax_yarpp_display_demo', array( $this, 'ajax_display_demo' ) );
44
- add_action( 'wp_ajax_yarpp_display', array( $this, 'ajax_display' ) );
45
- add_action( 'wp_ajax_yarpp_optin_data', array( $this, 'ajax_optin_data' ) );
46
- add_action( 'wp_ajax_yarpp_optin', array( $this, 'ajax_optin' ) );
47
- add_action( 'wp_ajax_yarpp_set_display_code', array( $this, 'ajax_set_display_code' ) );
48
- }
49
- }
50
-
51
- function ui_register() {
52
- global $wp_version;
53
- if ( get_option( 'yarpp_activated' ) ) {
54
- delete_option( 'yarpp_activated' );
55
- delete_option( 'yarpp_upgraded' );
56
- add_action( 'admin_enqueue_scripts', array( $this, 'pointer_enqueue' ) );
57
- add_action( 'admin_print_footer_scripts', array( $this, 'pointer_script' ) );
58
- } elseif ( !$this->core->get_option('optin') &&
59
- current_user_can('manage_options') &&
60
- !get_user_option( 'yarpp_saw_optin' )
61
- ) {
62
- add_action( 'admin_notices', array( $this, 'optin_notice' ) );
63
- } elseif ( !$this->core->get_option('optin') &&
64
- current_user_can('manage_options') &&
65
- get_option( 'yarpp_upgraded' )
66
- ) {
67
- add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
68
- }
69
-
70
- if ( $this->core->get_option('optin') )
71
- delete_option( 'yarpp_upgraded' );
72
-
73
- // setup admin
74
- $this->hook = add_options_page(__('Related Posts (YARPP)','yarpp'),__('Related Posts (YARPP)','yarpp'), 'manage_options', 'yarpp', array( $this, 'options_page' ) );
75
-
76
- // new in 3.0.12: add settings link to the plugins page
77
- add_filter('plugin_action_links', array( $this, 'settings_link' ), 10, 2);
78
-
79
- $metabox_post_types = $this->core->get_option( 'auto_display_post_types' );
80
- if ( !in_array( 'post', $metabox_post_types ) )
81
- $metabox_post_types[] = 'post';
82
-
83
- // new in 3.0: add meta box
84
- foreach ( $metabox_post_types as $post_type ) {
85
- 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>', array( $this, 'metabox' ), $post_type, 'normal' );
86
- }
87
-
88
- // new in 3.3: properly enqueue scripts for admin:
89
- add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
90
- }
91
-
92
- // 3.5.4: only load metabox code if we're going to be on the settings page
93
- function settings_screen( $current_screen ) {
94
- if ( $current_screen->id != 'settings_page_yarpp' )
95
- return $current_screen;
96
-
97
- // new in 3.3: load options page sections as metaboxes
98
- require_once('options-meta-boxes.php');
99
-
100
- // 3.5.5: check that add_help_tab method callable (WP >= 3.3)
101
- if ( is_callable(array($current_screen, 'add_help_tab')) ) {
102
- $current_screen->add_help_tab(array(
103
- 'id' => 'faq',
104
- 'title' => __('Frequently Asked Questions', 'yarpp'),
105
- 'callback' => array( &$this, 'help_faq' )
106
- ));
107
- $current_screen->add_help_tab(array(
108
- 'id' => 'dev',
109
- 'title' => __('Developing with YARPP', 'yarpp'),
110
- 'callback' => array( &$this, 'help_dev' )
111
- ));
112
- $current_screen->add_help_tab(array(
113
- 'id' => 'optin',
114
- 'title' => __('Optional Data Collection', 'yarpp'),
115
- 'callback' => array( &$this, 'help_optin' )
116
- ));
117
- }
118
-
119
- return $current_screen;
120
- }
121
-
122
- private $readme = null;
123
-
124
- public function help_faq() {
125
- if ( is_null($this->readme) )
126
- $this->readme = file_get_contents( YARPP_DIR . '/readme.txt' );
127
-
128
- $matches = array();
129
- if ( preg_match('!== Frequently Asked Questions ==(.*?)^==!sm', $this->readme, $matches) )
130
- echo $this->markdown( $matches[1] );
131
- else
132
- echo '<a href="https://wordpress.org/extend/plugins/yet-another-related-posts-plugin/faq/">' . __(
133
- 'Frequently Asked Questions', 'yarpp') . '</a>';
134
- }
135
-
136
- public function help_dev() {
137
- if ( is_null($this->readme) )
138
- $this->readme = file_get_contents( YARPP_DIR . '/readme.txt' );
139
-
140
- $matches = array();
141
- if ( preg_match('!== Developing with YARPP ==(.*?)^==!sm', $this->readme, $matches) )
142
- echo $this->markdown( $matches[1] );
143
- else
144
- echo '<a href="https://wordpress.org/extend/plugins/yet-another-related-posts-plugin/other_notes/">' . __(
145
- 'Developing with YARPP', 'yarpp') . '</a>';
146
- }
147
-
148
- public function help_optin() {
149
- // todo: i18n
150
- echo '<p>' . sprintf( __( "With your permission, YARPP will send information about YARPP's settings, usage, and environment back to a central server at %s.", 'yarpp' ), '<code>yarpp.org</code>') . ' ';
151
- echo __( "This information will be used to improve YARPP in the future and help decide future development decisions for YARPP.", 'yarpp' ) . ' ';
152
- echo '<strong>' . __( "Contributing this data will help make YARPP better for you and for other YARPP users.", 'yarpp' ) . '</strong></p>';
153
-
154
- if ( !$this->core->get_option( 'optin' ) ) {
155
- echo '<p>';
156
- $this->print_optin_button();
157
- echo '</p>';
158
- }
159
-
160
- echo '<p>' . __( "If you opt-in, the following information is sent back to YARPP:", 'yarpp' ) . '</p>';
161
- echo '<div id="optin_data_frame"></div>';
162
- echo '<p>' . __( "In addition, YARPP also loads an invisible pixel image with your YARPP results to know how often YARPP is being used.", 'yarpp' ) . '</p>';
163
- }
164
-
165
- function print_optin_button() {
166
- echo '<a id="yarpp-optin-button" class="button">' . __('Send settings and usage data back to YARPP', 'yarpp') . '</a><span class="yarpp-thankyou" style="display:none"><strong>' . __('Thank you!', 'yarpp') . '</strong></span>';
167
- wp_nonce_field( 'yarpp_optin', 'yarpp_optin-nonce', false );
168
- echo "<script type='text/javascript'>
169
- jQuery(function($){
170
- $('#yarpp-optin-button').click(function() {
171
- $(this)
172
- .hide()
173
- .siblings('.yarpp-thankyou').show('slow');
174
- $('#yarpp-optin').attr('checked', true);
175
- $.ajax({type:'POST',
176
- url: ajaxurl,
177
- data: {
178
- action: 'yarpp_optin',
179
- '_ajax_nonce': $('#yarpp_optin-nonce').val()
180
- }});
181
- });
182
- });
183
- </script>\n";
184
- }
185
-
186
- function optin_notice( $upgrade = false ) {
187
- $screen = get_current_screen();
188
- if ( is_null($screen) || $screen->id == 'settings_page_yarpp' )
189
- return;
190
-
191
- if ( $upgrade ) {
192
- delete_option( 'yarpp_upgraded' );
193
- } else {
194
- $user = get_current_user_id();
195
- update_user_option( $user, 'yarpp_saw_optin', true );
196
- }
197
-
198
- echo '<div class="updated fade"><p>';
199
- if ( $upgrade )
200
- echo '<strong>' . sprintf( __('%1$s updated successfully.'), 'Yet Another Related Posts Plugin' ) . '</strong> ';
201
- _e( "<strong>Help make YARPP better</strong> by sending information about YARPP's settings and usage statistics.", 'yarpp' );
202
-
203
- echo '</p><p>';
204
- $this->print_optin_button();
205
- echo '<a class="button" href="options-general.php?page=yarpp#help-optin">' . __( 'Learn More', 'yarpp' ) . '</a>';
206
- echo '</p></div>';
207
- }
208
-
209
- function upgrade_notice() {
210
- $this->optin_notice( true );
211
- }
212
-
213
- // faux-markdown, required for the help text rendering
214
- protected function markdown( $text ) {
215
- $replacements = array(
216
- // strip each line
217
- '!\s*[\r\n] *!' => "\n",
218
-
219
- // headers
220
- '!^=(.*?)=\s*$!m' => '<h3>\1</h3>',
221
-
222
- // bullets
223
- '!^(\* .*([\r\n]\* .*)*)$!m' => "<ul>\n\\1\n</ul>",
224
- '!^\* (.*?)$!m' => '<li>\1</li>',
225
- '!^(\d+\. .*([\r\n]\d+\. .*)*)$!m' => "<ol>\n\\1\n</ol>",
226
- '!^\d+\. (.*?)$!m' => '<li>\1</li>',
227
-
228
- // code block
229
- '!^(\t.*([\r\n]\t.*)*)$!m' => "<pre>\n\\1\n</pre>",
230
-
231
- // wrap p
232
- '!^([^<\t].*[^>])$!m' => '<p>\1</p>',
233
- // bold
234
- '!\*([^*]*?)\*!' => '<strong>\1</strong>',
235
- // code
236
- '!`([^`]*?)`!' => '<code>\1</code>',
237
- // links
238
- '!\[([^]]+)\]\(([^)]+)\)!' => '<a href="\2" target="_new">\1</a>',
239
- );
240
- $text = preg_replace(array_keys($replacements), array_values($replacements), $text);
241
-
242
- return $text;
243
- }
244
-
245
- function render_screen_settings( $output, $current_screen ) {
246
- if ( $current_screen->id != 'settings_page_yarpp' )
247
- return $output;
248
-
249
- $output .= "<div id='yarpp_extra_screen_settings'><label for='yarpp_display_code'><input type='checkbox' name='yarpp_display_code' id='yarpp_display_code'";
250
- $output .= checked( $this->core->get_option('display_code'), true, false );
251
- $output .= " />";
252
- $output .= __('Show example code output', 'yarpp');
253
- $output .= '</label></div>';
254
-
255
- return $output;
256
- }
257
-
258
- // since 3.3
259
- function enqueue() {
260
- $version = defined('WP_DEBUG') && WP_DEBUG ? time() : YARPP_VERSION;
261
- $screen = get_current_screen();
262
- if ( !is_null($screen) && $screen->id == 'settings_page_yarpp' ) {
263
- wp_enqueue_script( 'postbox' );
264
- $this->pointer_enqueue();
265
- wp_enqueue_style( 'yarpp_options', plugins_url( 'options.css', __FILE__ ), array(), $version );
266
- wp_enqueue_script( 'yarpp_options', plugins_url( 'js/options.js', __FILE__ ), array('jquery'), $version );
267
- }
268
-
269
- $metabox_post_types = $this->core->get_option( 'auto_display_post_types' );
270
- if ( !is_null($screen) && ($screen->id == 'post' || in_array( $screen->id, $metabox_post_types )) ) {
271
- wp_enqueue_script( 'yarpp_metabox', plugins_url( 'js/metabox.js', __FILE__ ), array('jquery'), $version );
272
- }
273
- }
274
-
275
- // since 3.4 and WP 3.3
276
- function pointer_enqueue() {
277
- wp_enqueue_style( 'wp-pointer' );
278
- wp_enqueue_script( 'wp-pointer' );
279
- }
280
- function pointer_script() {
281
- $content = '<h3>' . str_replace('<span>', '<span style="font-style:italic; font-weight: inherit;">', __('Thank you for installing <span>Yet Another Related Posts Plugin</span>!', 'yarpp') ) . '</h3>';
282
- $content .= '<p>' . str_replace('<a>', '<a href="' . esc_url(admin_url('options-general.php?page=yarpp')) .'">', __('Make sure to visit the <a>Related Posts settings page</a> to customize YARPP.', 'yarpp') ). '</p>';
283
- ?>
284
- <script>
285
- jQuery(function () {
286
- var body = jQuery(document.body),
287
- menu = jQuery('#menu-settings'),
288
- collapse = jQuery('#collapse-menu'),
289
- yarpp = menu.find("a[href='options-general.php?page=yarpp']"),
290
- options = {
291
- content: '<?php echo $content; ?>',
292
- position: {
293
- edge: 'left',
294
- align: 'center',
295
- of: menu.is('.wp-menu-open') && !menu.is('.folded *') ? yarpp : menu
296
- },
297
- close: function() {
298
- menu.unbind('mouseenter mouseleave', yarpp_pointer);
299
- collapse.unbind('mouseenter mouseleave', yarpp_pointer);
300
- }};
301
-
302
- if ( !yarpp.length )
303
- return;
304
-
305
- body.pointer(options).pointer('open');
306
-
307
- if ( menu.is('.folded *') || !menu.is('.wp-menu-open') ) {
308
- function yarpp_pointer(e) {
309
- setTimeout(function() {
310
- if (yarpp.is(':visible'))
311
- options.position.of = yarpp;
312
- else
313
- options.position.of = menu;
314
- body.pointer( options );
315
- }, 200);
316
- }
317
- menu.bind('mouseenter mouseleave', yarpp_pointer);
318
- collapse.bind('mouseenter mouseleave', yarpp_pointer);
319
- }
320
- });
321
- </script>
322
- <?php
323
- }
324
-
325
- function settings_link($links, $file) {
326
- $this_plugin = dirname(plugin_basename(__FILE__)) . '/yarpp.php';
327
- if($file == $this_plugin) {
328
- $links[] = '<a href="options-general.php?page=yarpp">' . __('Settings') . '</a>';
329
- }
330
- return $links;
331
- }
332
-
333
- function options_page() {
334
- // for proper metabox support:
335
- require(YARPP_DIR.'/options.php');
336
- }
337
-
338
- // @since 3.4: don't actually compute results here, but use ajax instead
339
- function metabox() {
340
- ?>
341
- <style>
342
- #yarpp_relatedposts h3 .postbox-title-action {
343
- right: 30px;
344
- top: 5px;
345
- position: absolute;
346
- padding: 0;
347
- }
348
- #yarpp_relatedposts:hover .edit-box {
349
- display: inline;
350
- }
351
- </style>
352
- <?php
353
- if ( !get_the_ID() ) {
354
- echo "<div><p>".__("Related entries may be displayed once you save your entry",'yarpp').".</p></div>";
355
- } else {
356
- wp_nonce_field( 'yarpp_display', 'yarpp_display-nonce', false );
357
- echo '<div id="yarpp-related-posts"><img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" alt="" /></div>';
358
- }
359
- }
360
-
361
- // @since 3.3: default metaboxes to show:
362
- function default_hidden_meta_boxes($hidden, $screen) {
363
- if ( 'settings_page_yarpp' == $screen->id )
364
- $hidden = $this->core->default_hidden_metaboxes;
365
- return $hidden;
366
- }
367
-
368
- // @since 4: UI to copy templates
369
- function can_copy_templates() {
370
- $theme_dir = get_stylesheet_directory();
371
- // If we can't write to the theme, return false
372
- if ( !is_dir($theme_dir) || !is_writable($theme_dir) )
373
- return false;
374
-
375
- require_once(ABSPATH . 'wp-admin/includes/file.php');
376
- WP_Filesystem( false, get_stylesheet_directory() );
377
- global $wp_filesystem;
378
- // direct method is the only method that I've tested so far
379
- return $wp_filesystem->method == 'direct';
380
- }
381
-
382
- function copy_templates() {
383
- $templates_dir = trailingslashit(trailingslashit(YARPP_DIR) . 'yarpp-templates');
384
-
385
- require_once(ABSPATH . 'wp-admin/includes/file.php');
386
- WP_Filesystem( false, get_stylesheet_directory() );
387
- global $wp_filesystem;
388
- if ( $wp_filesystem->method != 'direct' )
389
- return false;
390
-
391
- return copy_dir( $templates_dir, get_stylesheet_directory(), array('.svn') );
392
- }
393
-
394
- /*
395
- * AJAX SERVICES
396
- */
397
-
398
- function ajax_display_exclude_terms() {
399
- check_ajax_referer( 'yarpp_display_exclude_terms' );
400
-
401
- if ( !isset($_REQUEST['taxonomy']) )
402
- return;
403
-
404
- $taxonomy = (string) $_REQUEST['taxonomy'];
405
-
406
- header("HTTP/1.1 200");
407
- header("Content-Type: text/html; charset=UTF-8");
408
-
409
- $exclude_tt_ids = wp_parse_id_list($this->core->get_option('exclude'));
410
- $exclude_term_ids = $this->get_term_ids_from_tt_ids( $taxonomy, $exclude_tt_ids );
411
- // if ( 'category' == $taxonomy )
412
- // $exclude .= ',' . get_option( 'default_category' );
413
-
414
- $terms = get_terms($taxonomy, array(
415
- 'exclude' => $exclude_term_ids,
416
- 'hide_empty' => false,
417
- 'hierarchical' => false,
418
- 'number' => 100,
419
- 'offset' => $_REQUEST['offset']
420
- ));
421
-
422
- if ( !count($terms) ) {
423
- echo ':('; // no more :(
424
- exit;
425
- }
426
-
427
- foreach ($terms as $term) {
428
- echo "<span><input type='checkbox' name='exclude[{$term->term_taxonomy_id}]' id='exclude_{$term->term_taxonomy_id}' value='true' /> <label for='exclude_{$term->term_taxonomy_id}'>" . esc_html($term->name) . "</label></span> ";
429
- }
430
- exit;
431
- }
432
-
433
- function get_term_ids_from_tt_ids( $taxonomy, $tt_ids ) {
434
- global $wpdb;
435
- $tt_ids = wp_parse_id_list($tt_ids);
436
- if ( empty($tt_ids) )
437
- return array();
438
- return $wpdb->get_col("select term_id from $wpdb->term_taxonomy where taxonomy = '{$taxonomy}' and term_taxonomy_id in (" . join(',', $tt_ids) . ")");
439
- }
440
-
441
- function ajax_display() {
442
- check_ajax_referer( 'yarpp_display' );
443
-
444
- if ( !isset($_REQUEST['ID']) )
445
- return;
446
-
447
- header("HTTP/1.1 200");
448
- header("Content-Type: text/html; charset=UTF-8");
449
-
450
- $args = array(
451
- 'post_type' => array('post'),
452
- 'domain' => isset($_REQUEST['domain']) ? $_REQUEST['domain'] : 'website'
453
- );
454
- if ( $this->core->get_option('cross_relate') )
455
- $args['post_type'] = $this->core->get_post_types();
456
-
457
- $return = $this->core->display_related(absint($_REQUEST['ID']), $args, false);
458
- echo $return;
459
- exit;
460
- }
461
-
462
- function ajax_display_demo() {
463
- check_ajax_referer( 'yarpp_display_demo' );
464
-
465
- header("HTTP/1.1 200");
466
- header("Content-Type: text/html; charset=UTF-8");
467
-
468
- $args = array(
469
- 'post_type' => array('post'),
470
- 'domain' => isset($_REQUEST['domain']) ? $_REQUEST['domain'] : 'website'
471
- );
472
-
473
- $return = $this->core->display_demo_related($args, false);
474
- echo preg_replace("/[\n\r]/",'',nl2br(htmlspecialchars($return)));
475
- exit;
476
- }
477
-
478
- function ajax_optin_data() {
479
- check_ajax_referer( 'yarpp_optin_data' );
480
-
481
- header("HTTP/1.1 200");
482
- header("Content-Type: text/html; charset=UTF-8");
483
-
484
- $data = $this->core->optin_data();
485
- $this->core->pretty_echo($data);
486
- exit;
487
- }
488
-
489
- function ajax_optin() {
490
- check_ajax_referer( 'yarpp_optin' );
491
-
492
- header("HTTP/1.1 200");
493
- header("Content-Type: text; charset=UTF-8");
494
-
495
- $data = $this->core->set_option('optin', true);
496
- $this->core->optin_ping();
497
- echo 'ok';
498
- exit;
499
- }
500
-
501
- function ajax_set_display_code() {
502
- check_ajax_referer( 'yarpp_set_display_code' );
503
-
504
- header("HTTP/1.1 200");
505
- header("Content-Type: text; charset=UTF-8");
506
-
507
- $data = $this->core->set_option( 'display_code', isset($_REQUEST['checked']) );
508
- echo 'ok';
509
- exit;
510
- }
511
- }
trunk/class-cache.php DELETED
@@ -1,620 +0,0 @@
1
- <?php
2
-
3
- abstract class YARPP_Cache {
4
-
5
- protected $core;
6
- public $score_override = false;
7
- public $online_limit = false;
8
- public $last_sql;
9
- protected $yarpp_time = false;
10
-
11
- function __construct( &$core ) {
12
- $this->core = &$core;
13
- $this->name = __($this->name, 'yarpp');
14
- }
15
-
16
- function add_signature( $query ) {
17
- $query->yarpp_cache_type = $this->name;
18
- }
19
-
20
- /**
21
- * GENERAL CACHE CONTROL
22
- */
23
- public function is_yarpp_time() {
24
- return $this->yarpp_time;
25
- }
26
-
27
- public function flush() {
28
- }
29
-
30
- public function setup() {
31
- }
32
-
33
- public function upgrade( $last_version ) {
34
- }
35
-
36
- /**
37
- * POST CACHE CONTROL
38
- */
39
- // Note: return value changed in 3.4
40
- // return YARPP_NO_RELATED | YARPP_RELATED | YARPP_DONT_RUN | false if no good input
41
- function enforce( $reference_ID, $force = false ) {
42
- // @since 3.5.3: don't compute on revisions
43
- if ( $the_post = wp_is_post_revision($reference_ID) )
44
- $reference_ID = $the_post;
45
-
46
- if ( !is_int( $reference_ID ) )
47
- return false;
48
-
49
- $status = $this->is_cached($reference_ID);
50
- $status = apply_filters( 'yarpp_cache_enforce_status', $status, $reference_ID );
51
-
52
- // There's a stop signal:
53
- if ( YARPP_DONT_RUN === $status )
54
- return YARPP_DONT_RUN;
55
-
56
- // If not cached, process now:
57
- if ( YARPP_NOT_CACHED == $status || $force )
58
- $status = $this->update((int) $reference_ID);
59
- // status now will be YARPP_NO_RELATED | YARPP_RELATED
60
-
61
- // There are no related posts
62
- if ( YARPP_NO_RELATED === $status )
63
- return YARPP_NO_RELATED;
64
-
65
- // There are results
66
- return YARPP_RELATED;
67
- }
68
-
69
- // @return YARPP_NO_RELATED | YARPP_RELATED | YARPP_NOT_CACHED
70
- public function is_cached($reference_ID) {
71
- return YARPP_NOT_CACHED;
72
- }
73
-
74
- public function clear($reference_ID) {
75
- }
76
-
77
- /**
78
- * POST STATUS INTERACTIONS
79
- */
80
-
81
- // Clear the cache for this entry and for all posts which are "related" to it.
82
- // New in 3.2: This is called when a post is deleted.
83
- function delete_post( $post_ID ) {
84
- // Clear the cache for this post.
85
- $this->clear((int) $post_ID);
86
-
87
- // Find all "peers" which list this post as a related post and clear their caches
88
- if ( $peers = $this->related(null, (int) $post_ID) )
89
- $this->clear($peers);
90
- }
91
-
92
- // New in 3.2.1: handle various post_status transitions
93
- function transition_post_status( $new_status, $old_status, $post ) {
94
- $post_ID = $post->ID;
95
-
96
- // @since 3.4: don't compute on revisions
97
- // @since 3.5: compute on the parent instead
98
- if ( $the_post = wp_is_post_revision($post_ID) )
99
- $post_ID = $the_post;
100
-
101
- // unpublish
102
- if ( $old_status == 'publish' && $new_status != 'publish' ) {
103
- // Find all "peers" which list this post as a related post and clear their caches
104
- if ( $peers = $this->related(null, (int) $post_ID) )
105
- $this->clear($peers);
106
- }
107
-
108
- // publish
109
- if ( $old_status != 'publish' && $new_status == 'publish' ) {
110
- // find everything which is related to this post, and clear them, so that this
111
- // post might show up as related to them.
112
- if ( $related = $this->related($post_ID, null) )
113
- $this->clear($related);
114
- }
115
-
116
- // @since 3.4: simply clear the cache on save; don't recompute.
117
- $this->clear((int) $post_ID);
118
- }
119
-
120
- function set_score_override_flag( $q ) {
121
- if ( $this->is_yarpp_time() ) {
122
- $this->score_override = ($q->query_vars['orderby'] == 'score');
123
-
124
- if (!empty($q->query_vars['showposts'])) {
125
- $this->online_limit = $q->query_vars['showposts'];
126
- } else {
127
- $this->online_limit = false;
128
- }
129
- } else {
130
- $this->score_override = false;
131
- $this->online_limit = false;
132
- }
133
- }
134
-
135
- /**
136
- * SQL!
137
- */
138
-
139
- protected function sql( $reference_ID = false, $args = array() ) {
140
- global $wpdb, $post;
141
-
142
- if ( is_object($post) && !$reference_ID ) {
143
- $reference_ID = $post->ID;
144
- }
145
-
146
- if ( !is_object($post) || $reference_ID != $post->ID ) {
147
- $reference_post = get_post( $reference_ID );
148
- } else {
149
- $reference_post = $post;
150
- }
151
-
152
- $options = array( 'threshold', 'show_pass_post', 'past_only', 'weight', 'require_tax', 'exclude', 'recent', 'limit' );
153
- extract( $this->core->parse_args($args, $options) );
154
- // The maximum number of items we'll ever want to cache
155
- $limit = max($limit, $this->core->get_option('rss_limit'));
156
-
157
- // Fetch keywords
158
- $keywords = $this->get_keywords($reference_ID);
159
-
160
- // SELECT
161
- $newsql = "SELECT $reference_ID as reference_ID, ID, "; //post_title, post_date, post_content, post_excerpt,
162
-
163
- $newsql .= 'ROUND(0';
164
-
165
- if ((int) @$weight['body'])
166
- $newsql .= " + (MATCH (post_content) AGAINST ('".$wpdb->escape($keywords['body'])."')) * ". absint($weight['body']);
167
- if ((int) @$weight['title'])
168
- $newsql .= " + (MATCH (post_title) AGAINST ('".$wpdb->escape($keywords['title'])."')) * ". absint($weight['title']);
169
-
170
- // Build tax criteria query parts based on the weights
171
- foreach ( (array) $weight['tax'] as $tax => $tax_weight ) {
172
- $newsql .= " + " . $this->tax_criteria($reference_ID, $tax) . " * " . intval($tax_weight);
173
- }
174
-
175
- $newsql .= ',1) as score';
176
-
177
- $newsql .= "\n from $wpdb->posts \n";
178
-
179
- $exclude_tt_ids = wp_parse_id_list( $exclude );
180
- if ( count($exclude_tt_ids) || count((array) $weight['tax']) || count($require_tax) ) {
181
- $newsql .= "left join $wpdb->term_relationships as terms on ( terms.object_id = $wpdb->posts.ID ) \n";
182
- }
183
-
184
- // WHERE
185
-
186
- $newsql .= " where post_status in ( 'publish', 'static' ) and ID != '$reference_ID'";
187
-
188
- if ( $past_only ) // 3.1.8: revised $past_only option
189
- $newsql .= " and post_date <= '$reference_post->post_date' ";
190
- if ( !$show_pass_post )
191
- $newsql .= " and post_password ='' ";
192
- if ( !!$recent )
193
- $newsql .= " and post_date > date_sub(now(), interval {$recent}) ";
194
-
195
- $newsql .= " and post_type = 'post'";
196
-
197
- // GROUP BY
198
- $newsql .= "\n group by ID \n";
199
-
200
- // HAVING
201
- // number_format fix suggested by vkovalcik! :)
202
- $safethreshold = number_format(max($threshold,0.1), 2, '.', '');
203
- // @since 3.5.3: ID=0 is a special value; never save such a result.
204
- $newsql .= " having score >= $safethreshold and ID != 0";
205
- if ( count($exclude_tt_ids) ) {
206
- $newsql .= " and bit_or(terms.term_taxonomy_id in (" . join(',', $exclude_tt_ids) . ")) = 0";
207
- }
208
-
209
- foreach ( (array) $require_tax as $tax => $number ) {
210
- $newsql .= ' and ' . $this->tax_criteria($reference_ID, $tax) . ' >= ' . intval($number);
211
- }
212
-
213
- $newsql .= " order by score desc limit $limit";
214
-
215
- if ( isset($args['post_type']) )
216
- $post_types = (array) $args['post_type'];
217
- else
218
- $post_types = $this->core->get_post_types();
219
-
220
- $queries = array();
221
- foreach ( $post_types as $post_type ) {
222
- $queries[] = '(' . str_replace("post_type = 'post'", "post_type = '{$post_type}'", $newsql) . ')';
223
- }
224
- $sql = implode( ' union ', $queries );
225
-
226
- if ($this->core->debug) echo "<!--$sql-->";
227
-
228
- $this->last_sql = $sql;
229
-
230
- return $sql;
231
- }
232
-
233
- private function tax_criteria( $reference_ID, $taxonomy ) {
234
- // @todo maybe reinforce the object term cache?
235
- $terms = get_the_terms( $reference_ID, $taxonomy );
236
- // if there are no terms of that tax
237
- if ( false === $terms )
238
- return '(1 = 0)';
239
-
240
- $tt_ids = wp_list_pluck($terms, 'term_taxonomy_id');
241
- return "count(distinct if( terms.term_taxonomy_id in (" . join(',',$tt_ids) . "), terms.term_taxonomy_id, null ))";
242
- }
243
-
244
- /**
245
- * KEYWORDS
246
- */
247
-
248
- // @param $ID (int)
249
- // @param $type (string) body | title | all
250
- // @return (string|array) depending on whether "all" were requested or not
251
- public function get_keywords( $ID, $type = 'all' ) {
252
- if ( !$ID = absint($ID) )
253
- return false;
254
-
255
- $keywords = array(
256
- 'body' => $this->body_keywords($ID),
257
- 'title' => $this->title_keywords($ID)
258
- );
259
-
260
- if ( empty($keywords) )
261
- return false;
262
-
263
- if ( 'all' == $type )
264
- return $keywords;
265
- return $keywords[$type];
266
- }
267
-
268
- protected function title_keywords( $ID, $max = 20 ) {
269
- return apply_filters( 'yarpp_title_keywords',
270
- $this->extract_keywords( get_the_title($ID), $max, $ID ), $max, $ID );
271
- }
272
-
273
- protected function body_keywords( $ID, $max = 20 ) {
274
- $post = get_post( $ID );
275
- if ( empty($post) )
276
- return '';
277
- $content = $this->apply_filters_if_white( 'the_content', $post->post_content );
278
- return apply_filters( 'yarpp_body_keywords',
279
- $this->extract_keywords( $content, $max, $ID ), $max, $ID );
280
-
281
- }
282
-
283
- private function extract_keywords( $html, $max = 20, $ID = 0 ) {
284
-
285
- /**
286
- * @filter yarpp_extract_keywords
287
- *
288
- * Use this filter to override YARPP's built-in keyword computation
289
- * Return values should be a string of space-delimited words
290
- *
291
- * @param $keywords
292
- * @param $html unfiltered HTML content
293
- * @param (int) $max maximum number of keywords
294
- * @param (int) $ID
295
- */
296
- if ( $keywords = apply_filters( 'yarpp_extract_keywords', false, $html, $max, $ID ) )
297
- return $keywords;
298
-
299
- $lang = 'en_US';
300
- if ( defined('WPLANG') ) {
301
- switch ( substr(WPLANG, 0, 2) ) {
302
- case 'de':
303
- $lang = 'de_DE';
304
- case 'it':
305
- $lang = 'it_IT';
306
- case 'pl':
307
- $lang = 'pl_PL';
308
- case 'bg':
309
- $lang = 'bg_BG';
310
- case 'fr':
311
- $lang = 'fr_FR';
312
- case 'cs':
313
- $lang = 'cs_CZ';
314
- case 'nl':
315
- $lang = 'nl_NL';
316
- default:
317
- $lang = 'en_US';
318
- }
319
- }
320
-
321
- $words_file = YARPP_DIR . '/lang/words-' . $lang . '.php';
322
- if ( file_exists($words_file) )
323
- include( $words_file );
324
- if ( !isset($overusedwords) )
325
- $overusedwords = array();
326
-
327
- // strip tags and html entities
328
- $text = preg_replace('/&(#x[0-9a-f]+|#[0-9]+|[a-zA-Z]+);/', '', strip_tags($html) );
329
-
330
- // 3.2.2: ignore soft hyphens
331
- // Requires PHP 5: http://bugs.php.net/bug.php?id=25670
332
- $softhyphen = html_entity_decode('&#173;',ENT_NOQUOTES,'UTF-8');
333
- $text = str_replace($softhyphen, '', $text);
334
-
335
- $charset = get_option('blog_charset');
336
- if ( function_exists('mb_split') && !empty($charset) ) {
337
- mb_regex_encoding($charset);
338
- $wordlist = mb_split('\s*\W+\s*', mb_strtolower($text, $charset));
339
- } else
340
- $wordlist = preg_split('%\s*\W+\s*%', strtolower($text));
341
-
342
- // Build an array of the unique words and number of times they occur.
343
- $tokens = array_count_values($wordlist);
344
-
345
- // Remove the stop words from the list.
346
- $overusedwords = apply_filters( 'yarpp_keywords_overused_words', $overusedwords );
347
- if ( is_array($overusedwords) ) {
348
- foreach ($overusedwords as $word) {
349
- unset($tokens[$word]);
350
- }
351
- }
352
- // Remove words which are only a letter
353
- foreach (array_keys($tokens) as $word) {
354
- if ( function_exists('mb_strlen') )
355
- if (mb_strlen($word) < 2) unset($tokens[$word]);
356
- else
357
- if (strlen($word) < 2) unset($tokens[$word]);
358
- }
359
-
360
- arsort($tokens, SORT_NUMERIC);
361
-
362
- $types = array_keys($tokens);
363
-
364
- if (count($types) > $max)
365
- $types = array_slice($types, 0, $max);
366
- return implode(' ', $types);
367
- }
368
-
369
- /* new in 2.0! apply_filters_if_white (previously apply_filters_without) now has a blacklist.
370
- * It can be modified via the yarpp_blacklist and yarpp_blackmethods filters.
371
- */
372
- /* blacklisted so far:
373
- - diggZ-Et
374
- - reddZ-Et
375
- - dzoneZ-Et
376
- - WP-Syntax
377
- - Viper's Video Quicktags
378
- - WP-CodeBox
379
- - WP shortcodes
380
- - WP Greet Box
381
- - Jetpack ShareDaddy
382
- //- Tweet This - could not reproduce problem.
383
- */
384
- function white( $filter ) {
385
- static $blacklist, $blackmethods;
386
-
387
- if ( is_null($blacklist) || is_null($blackmethods) ) {
388
- $yarpp_blacklist = array('diggZEt_AddBut', 'reddZEt_AddBut', 'dzoneZEt_AddBut', 'wp_syntax_before_filter', 'wp_syntax_after_filter', 'wp_codebox_before_filter', 'wp_codebox_after_filter', 'do_shortcode', 'sharing_display', 'really_simple_share_content');//,'insert_tweet_this'
389
- $yarpp_blackmethods = array('addinlinejs', 'replacebbcode', 'filter_content');
390
-
391
- $blacklist = (array) apply_filters( 'yarpp_blacklist', $yarpp_blacklist );
392
- $blackmethods = (array) apply_filters( 'yarpp_blackmethods', $yarpp_blackmethods );
393
- }
394
-
395
- if ( is_array($filter) && is_a( $filter[0], 'YARPP' ) )
396
- return false;
397
- if ( is_array($filter) && in_array( $filter[1], $blackmethods ) )
398
- return false;
399
- return !in_array( $filter, $blacklist );
400
- }
401
-
402
- /* 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').*/
403
- function apply_filters_if_white($tag, $value) {
404
- global $wp_filter, $merged_filters, $wp_current_filter;
405
-
406
- $args = array();
407
-
408
- // Do 'all' actions first
409
- if ( isset($wp_filter['all']) ) {
410
- $wp_current_filter[] = $tag;
411
- $args = func_get_args();
412
- _wp_call_all_hook($args);
413
- }
414
-
415
- if ( !isset($wp_filter[$tag]) ) {
416
- if ( isset($wp_filter['all']) )
417
- array_pop($wp_current_filter);
418
- return $value;
419
- }
420
-
421
- if ( !isset($wp_filter['all']) )
422
- $wp_current_filter[] = $tag;
423
-
424
- // Sort
425
- if ( !isset( $merged_filters[ $tag ] ) ) {
426
- ksort($wp_filter[$tag]);
427
- $merged_filters[ $tag ] = true;
428
- }
429
-
430
- reset( $wp_filter[ $tag ] );
431
-
432
- if ( empty($args) )
433
- $args = func_get_args();
434
-
435
- do {
436
- foreach( (array) current($wp_filter[$tag]) as $the_ )
437
- if ( !is_null($the_['function'])
438
- and $this->white($the_['function'])){ // HACK
439
- $args[1] = $value;
440
- $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
441
- }
442
-
443
- } while ( next($wp_filter[$tag]) !== false );
444
-
445
- array_pop( $wp_current_filter );
446
-
447
- return $value;
448
- }
449
- }
450
-
451
- class YARPP_Cache_Bypass extends YARPP_Cache {
452
-
453
- public $name = "bypass";
454
-
455
- // variables used for lookup
456
- private $related_postdata = array();
457
- private $related_IDs = array();
458
-
459
- public $demo_time = false;
460
- private $demo_limit = 0;
461
-
462
- /**
463
- * SETUP/STATUS
464
- */
465
- function __construct( &$core ) {
466
- parent::__construct( $core );
467
- }
468
-
469
- public function is_enabled() {
470
- return true; // always enabled.
471
- }
472
-
473
- public function cache_status() {
474
- return 0; // always uncached
475
- }
476
-
477
- public function stats() {
478
- return array(); // always unknown
479
- }
480
-
481
- public function uncached($limit = 20, $offset = 0) {
482
- return array(); // nothing to cache
483
- }
484
-
485
- /**
486
- * MAGIC FILTERS
487
- */
488
- public function where_filter($arg) {
489
- global $wpdb;
490
- // modify the where clause to use the related ID list.
491
- if (!count($this->related_IDs))
492
- $this->related_IDs = array(0);
493
- $arg = preg_replace("!{$wpdb->posts}.ID = \d+!","{$wpdb->posts}.ID in (".join(',',$this->related_IDs).")",$arg);
494
-
495
- // if we have recent set, add an additional condition
496
- if ( !!$this->args['recent'] )
497
- $arg .= " and post_date > date_sub(now(), interval {$this->args['recent']}) ";
498
- return $arg;
499
- }
500
-
501
- public function orderby_filter($arg) {
502
- global $wpdb;
503
- // only order by score if the score function is added in fields_filter, which only happens
504
- // if there are related posts in the postdata
505
- if ($this->score_override &&
506
- is_array($this->related_postdata) && count($this->related_postdata))
507
- return str_replace("$wpdb->posts.post_date","score",$arg);
508
- return $arg;
509
- }
510
-
511
- public function fields_filter($arg) {
512
- global $wpdb;
513
- if (is_array($this->related_postdata) && count($this->related_postdata)) {
514
- $scores = array();
515
- foreach ($this->related_postdata as $related_entry) {
516
- $scores[] = " WHEN {$related_entry['ID']} THEN {$related_entry['score']}";
517
- }
518
- $arg .= ", CASE {$wpdb->posts}.ID" . join('',$scores) ." END as score";
519
- }
520
- return $arg;
521
- }
522
-
523
- public function demo_request_filter($arg) {
524
- global $wpdb;
525
- $wpdb->query("set @count = 0;");
526
-
527
- $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.';
528
-
529
- return "SELECT SQL_CALC_FOUND_ROWS ID + {$this->demo_limit} as ID, post_author, post_date, post_date_gmt, '{$loremipsum}' as post_content,
530
- 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
531
- FROM $wpdb->posts
532
- ORDER BY ID DESC LIMIT 0, {$this->demo_limit}";
533
- }
534
-
535
- public function limit_filter($arg) {
536
- global $wpdb;
537
- if ($this->online_limit)
538
- return " limit {$this->online_limit} ";
539
- return $arg;
540
- }
541
-
542
- /**
543
- * RELATEDNESS CACHE CONTROL
544
- */
545
- public function begin_yarpp_time( $reference_ID, $args ) {
546
- global $wpdb;
547
-
548
- $this->yarpp_time = true;
549
- $options = array( 'threshold', 'show_pass_post', 'past_only', 'weight', 'require_tax', 'exclude', 'recent', 'limit' );
550
- $this->args = $this->core->parse_args($args, $options);
551
-
552
- $this->related_postdata = $wpdb->get_results($this->sql($reference_ID, $args), ARRAY_A);
553
- $this->related_IDs = wp_list_pluck( $this->related_postdata, 'ID' );
554
-
555
- add_filter('posts_where',array(&$this,'where_filter'));
556
- add_filter('posts_orderby',array(&$this,'orderby_filter'));
557
- add_filter('posts_fields',array(&$this,'fields_filter'));
558
- add_filter('post_limits',array(&$this,'limit_filter'));
559
- add_action('pre_get_posts',array(&$this,'add_signature'));
560
- // sets the score override flag.
561
- add_action('parse_query',array(&$this,'set_score_override_flag'));
562
- }
563
-
564
- public function begin_demo_time( $limit ) {
565
- $this->demo_time = true;
566
- $this->demo_limit = $limit;
567
- add_action('pre_get_posts',array(&$this,'add_signature'));
568
- add_filter('posts_request',array(&$this,'demo_request_filter'));
569
- }
570
-
571
- public function end_yarpp_time() {
572
- $this->yarpp_time = false;
573
- remove_filter('posts_where',array(&$this,'where_filter'));
574
- remove_filter('posts_orderby',array(&$this,'orderby_filter'));
575
- remove_filter('posts_fields',array(&$this,'fields_filter'));
576
- remove_filter('post_limits',array(&$this,'limit_filter'));
577
- remove_action('pre_get_posts',array(&$this,'add_signature'));
578
- remove_action('parse_query',array(&$this,'set_score_override_flag'));
579
- }
580
-
581
- public function end_demo_time() {
582
- $this->demo_time = false;
583
- remove_action('pre_get_posts',array(&$this,'add_signature'));
584
- remove_filter('posts_request',array(&$this,'demo_request_filter'));
585
- }
586
-
587
- // @return YARPP_NO_RELATED | YARPP_RELATED
588
- // @used by enforce
589
- protected function update($reference_ID) {
590
- global $wpdb;
591
-
592
- return YARPP_RELATED;
593
- }
594
-
595
- public function related($reference_ID = null, $related_ID = null) {
596
- global $wpdb;
597
-
598
- if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
599
- _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
600
- return;
601
- }
602
-
603
- // reverse lookup
604
- if ( is_int($related_ID) && is_null($reference_ID) ) {
605
- _doing_it_wrong( __METHOD__, 'YARPP_Cache_Bypass::related cannot do a reverse lookup', '3.4' );
606
- return;
607
- }
608
-
609
- $results = $wpdb->get_results($this->sql($reference_ID), ARRAY_A);
610
- if ( !$results || !count($results) )
611
- return false;
612
-
613
- $results_ids = wp_list_pluck( $results, 'ID' );
614
- if ( is_null($related_ID) ) {
615
- return $results_ids;
616
- } else {
617
- return in_array( $related_ID, $results_ids );
618
- }
619
- }
620
- }