Yet Another Related Posts Plugin (YARPP) - Version 4.1

Version Description

  • Added optional YARPP Pro enhancements:
    • Revenue-generating ad content interspersed with related posts display
    • Detailed reports for Clicks, Click-Through Rate, and Revenue
    • Ability to display related content from multiple domains
    • UI settings for related content display, including thumbnail size and layout
    • Refined "relatedness" algorithm is now independent of MyISAM or InnoDB engines
    • "Relatedness" is calculated and stored externally, minimizing server load
  • Based on user feedback, the "Screen Options" section displays all YARPP options panels by default.
Download this release

Release Info

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

Code changes from version 4.0.8 to 4.1

Files changed (85) hide show
  1. class-admin.php +0 -570
  2. class-cache.php +0 -621
  3. class-core.php +0 -1427
  4. class-widget.php +0 -143
  5. classes/YARPP_Admin.php +616 -0
  6. classes/YARPP_Cache.php +487 -0
  7. classes/YARPP_Cache_Bypass.php +219 -0
  8. cache-postmeta.php → classes/YARPP_Cache_Postmeta.php +1 -7
  9. cache-tables.php → classes/YARPP_Cache_Tables.php +15 -19
  10. classes/YARPP_Core.php +1546 -0
  11. options-meta-boxes.php → classes/YARPP_Meta_Boxes.php +114 -88
  12. classes/YARPP_Widget.php +192 -0
  13. default.png +0 -0
  14. default_small.png +0 -0
  15. images/default.png +0 -0
  16. images/default_small.png +0 -0
  17. images/sprites-2x.png +0 -0
  18. images/sprites.png +0 -0
  19. images/template-buttons-2x.png +0 -0
  20. includes/init_functions.php +22 -0
  21. includes/optin_notice.js.php +23 -0
  22. includes/related_functions.php +111 -0
  23. styles-thumbnails.php → includes/styles-thumbnails.css.php +10 -17
  24. template-metabox.php → includes/template-metabox.php +0 -0
  25. template-widget.php → includes/template-widget.php +0 -0
  26. template-builtin.php → includes/template_builtin.php +0 -0
  27. template-functions.php → includes/template_functions.php +2 -1
  28. template-thumbnails.php → includes/template_thumbnails.php +0 -0
  29. includes/yarpp_options.php +315 -0
  30. includes/yarpp_pro_options.php +86 -0
  31. includes/yarpp_switch.php +34 -0
  32. js/{options.js → options_basic.js} +4 -4
  33. js/options_switch.js +17 -0
  34. lang/yarpp-ar_AR.po +131 -131
  35. lang/yarpp-ar_EG.po +131 -131
  36. lang/yarpp-bg_BG.po +131 -131
  37. lang/yarpp-by_BY.po +16 -16
  38. lang/yarpp-cs_CZ.po +14 -14
  39. lang/yarpp-de_DE.po +130 -130
  40. lang/yarpp-el_CY.po +151 -151
  41. lang/yarpp-el_EL.po +151 -151
  42. lang/yarpp-es_ES.po +130 -130
  43. lang/yarpp-est_EST.po +16 -16
  44. lang/yarpp-fa_IR.po +16 -16
  45. lang/yarpp-fr_FR.po +14 -14
  46. lang/yarpp-gb_IR.po +17 -17
  47. lang/yarpp-ge_KA.po +16 -16
  48. lang/yarpp-gu_IN.po +16 -16
  49. lang/yarpp-he_IL.po +14 -14
  50. lang/yarpp-hi_IN.po +16 -16
  51. lang/yarpp-hr.po +14 -14
  52. lang/yarpp-hu_HU.po +16 -16
  53. lang/yarpp-id_ID.po +16 -16
  54. lang/yarpp-it_IT.po +14 -14
  55. lang/yarpp-ja.po +14 -14
  56. lang/yarpp-kk_KZ.po +16 -16
  57. lang/yarpp-ko_KR.po +14 -14
  58. lang/yarpp-lt_LT.po +16 -16
  59. lang/yarpp-lv_LV.po +16 -16
  60. lang/yarpp-mk-MK.po +14 -14
  61. lang/yarpp-nb_NO.po +16 -16
  62. lang/yarpp-nl_NL.po +14 -14
  63. lang/yarpp-pt_BR.po +16 -16
  64. lang/yarpp-pt_PT.po +16 -16
  65. lang/yarpp-ro_RO.po +16 -16
  66. lang/yarpp-ru_RU.po +16 -16
  67. lang/yarpp-sk_SK.po +14 -14
  68. lang/yarpp-sl_SI.po +14 -14
  69. lang/yarpp-sr_RS.po +14 -14
  70. lang/yarpp-sv_SE.po +14 -14
  71. lang/yarpp-tr_TR.po +16 -16
  72. lang/yarpp-uk_UA.po +16 -16
  73. lang/yarpp-uz_UZ.po +16 -16
  74. lang/yarpp-vi_VN.po +16 -16
  75. lang/yarpp-zh_CN.po +16 -16
  76. lang/yarpp-zh_TW.po +14 -14
  77. options.php +0 -239
  78. readme.txt +48 -25
  79. related-functions.php +0 -100
  80. sprites-2x.png +0 -0
  81. sprites.png +0 -0
  82. options.css → style/options_basic.css +11 -5
  83. style/options_switch.css +90 -0
  84. uninstall.php +32 -22
  85. yarpp.php +48 -49
class-admin.php DELETED
@@ -1,570 +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_enable', array( $this,'ajax_optin_enable'));
47
- add_action( 'wp_ajax_yarpp_optin_disable', array( $this,'ajax_optin_disable'));
48
- add_action( 'wp_ajax_yarpp_set_display_code', array( $this, 'ajax_set_display_code' ) );
49
- }
50
- }
51
-
52
- function ui_register() {
53
- global $wp_version;
54
-
55
- if (get_option('yarpp_activated')) {
56
-
57
- delete_option('yarpp_activated');
58
- delete_option('yarpp_upgraded');
59
-
60
- if ( $this->core->get_option('optin') ){
61
- add_action('admin_notices', array($this, 'install_notice_disable'));
62
- }else{
63
- add_action('admin_notices', array($this, 'install_notice_enable'));
64
- }
65
-
66
- } elseif ( $this->core->get_option('optin') &&
67
- current_user_can('manage_options') &&
68
- get_option( 'yarpp_upgraded' )
69
- ) {
70
-
71
- add_action( 'admin_notices', array( $this, 'upgrade_notice' ) );
72
-
73
- } elseif ( $this->core->get_option('optin') &&
74
- current_user_can('manage_options') &&
75
- !get_user_option( 'yarpp_saw_optin' )
76
- ) {
77
-
78
- add_action( 'admin_notices', array( $this, 'optin_notice' ) );
79
-
80
- }/*end get_optin if*/
81
-
82
- if ( $this->core->get_option('optin') )
83
- delete_option( 'yarpp_upgraded' );
84
-
85
- // setup admin
86
- $this->hook = add_options_page(__('Related Posts (YARPP)','yarpp'),__('Related Posts (YARPP)','yarpp'), 'manage_options', 'yarpp', array( $this, 'options_page' ) );
87
-
88
- // new in 3.0.12: add settings link to the plugins page
89
- add_filter('plugin_action_links', array( $this, 'settings_link' ), 10, 2);
90
-
91
- $metabox_post_types = $this->core->get_option( 'auto_display_post_types' );
92
- if ( !in_array( 'post', $metabox_post_types ) )
93
- $metabox_post_types[] = 'post';
94
-
95
- // new in 3.0: add meta box
96
- foreach ( $metabox_post_types as $post_type ) {
97
- 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' );
98
- }
99
-
100
- // new in 3.3: properly enqueue scripts for admin:
101
- add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
102
- }/*end ui_register*/
103
-
104
- // 3.5.4: only load metabox code if we're going to be on the settings page
105
- function settings_screen( $current_screen ) {
106
- if ( $current_screen->id != 'settings_page_yarpp' )
107
- return $current_screen;
108
-
109
- // new in 3.3: load options page sections as metaboxes
110
- require_once('options-meta-boxes.php');
111
-
112
- // 3.5.5: check that add_help_tab method callable (WP >= 3.3)
113
- if ( is_callable(array($current_screen, 'add_help_tab')) ) {
114
- $current_screen->add_help_tab(array(
115
- 'id' => 'faq',
116
- 'title' => __('Frequently Asked Questions', 'yarpp'),
117
- 'callback' => array( &$this, 'help_faq' )
118
- ));
119
- $current_screen->add_help_tab(array(
120
- 'id' => 'dev',
121
- 'title' => __('Developing with YARPP', 'yarpp'),
122
- 'callback' => array( &$this, 'help_dev' )
123
- ));
124
- $current_screen->add_help_tab(array(
125
- 'id' => 'optin',
126
- 'title' => __('Optional Data Collection', 'yarpp'),
127
- 'callback' => array( &$this, 'help_optin' )
128
- ));
129
- }
130
-
131
- return $current_screen;
132
- }
133
-
134
- private $readme = null;
135
-
136
- public function help_faq() {
137
- if ( is_null($this->readme) )
138
- $this->readme = file_get_contents( YARPP_DIR . '/readme.txt' );
139
-
140
- $matches = array();
141
- if ( preg_match('!== Frequently Asked Questions ==(.*?)^==!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/faq/">' . __(
145
- 'Frequently Asked Questions', 'yarpp') . '</a>';
146
- }
147
-
148
- public function help_dev() {
149
- if ( is_null($this->readme) )
150
- $this->readme = file_get_contents( YARPP_DIR . '/readme.txt' );
151
-
152
- $matches = array();
153
- if ( preg_match('!== Developing with YARPP ==(.*?)^==!sm', $this->readme, $matches) )
154
- echo $this->markdown( $matches[1] );
155
- else
156
- echo '<a href="https://wordpress.org/extend/plugins/yet-another-related-posts-plugin/other_notes/">' . __(
157
- 'Developing with YARPP', 'yarpp') . '</a>';
158
- }
159
-
160
- public function help_optin() {
161
- // todo: i18n
162
- echo(
163
- '<p>'.
164
- __("With your permission, YARPP will send information about YARPP's settings, usage, and environment
165
- back to a central server at ", 'yarpp').'<code>yarpp.org</code>'.'.&nbsp;'.
166
- '</p>'.
167
- '<p>'.
168
- 'We would really appreciate your input to help us continue to improve the product. We are primarily looking '.
169
- 'for country, domain, and date installed information.'.
170
- '</p>'.
171
- '<p>'.
172
- __("This information will be used to improve YARPP in the future and help decide future development
173
- decisions for YARPP.",
174
- 'yarpp'
175
- ).' '.
176
- '</p>'.
177
- '<p>'.
178
- '<strong>'.
179
- __("Contributing this data will help make YARPP better for you and for other YARPP users.",
180
- 'yarpp' ).'</strong>'.
181
- '</p>'
182
- );
183
-
184
- if(!$this->core->get_option('optin')) {
185
- echo '<p>';
186
- $this->print_optin_button('Enable');
187
- $this->optin_button_script('enable');
188
- echo '</p>';
189
- }
190
-
191
- echo(
192
- '<p>'.
193
- __("The following information is sent back to YARPP:", 'yarpp').
194
- '</p>'.
195
- '<div id="optin_data_frame"></div>'.
196
- '<p>'.
197
- __("In addition, YARPP also loads an invisible pixel image with your YARPP results to know how often YARPP is being used.", 'yarpp').
198
- '</p>'
199
- );
200
- }
201
-
202
- function print_optin_button($action = 'Disable') {
203
- echo $this->the_optin_button($action);
204
- }
205
-
206
- function the_optin_button($action = 'Disable') {
207
- $out = '<a id="yarpp-optin-button" class="button">'.
208
- __($action.' sending usage data back to YARPP', 'yarpp').
209
- '</a>'.
210
- '<span class="yarpp-thankyou" style="display:none; margin-right: 10px;">'.
211
- '<strong>'.__('Thank you!', 'yarpp').'</strong>'.
212
- '</span>';
213
- return $out;
214
- }
215
-
216
- function optin_button_script($action = 'disable') {
217
- wp_nonce_field('yarpp_optin_'.$action, 'yarpp_optin-nonce', false);
218
-
219
- echo(
220
- "<script type='text/javascript'>
221
- jQuery(function($){
222
- $(document.body).on('click', '#yarpp-optin-button', function(){
223
- $(this).hide().siblings('.yarpp-thankyou').show('slow');
224
- $('#yarpp-optin').attr('checked', true);
225
- $.ajax({
226
- type:'POST',
227
- url : ajaxurl,
228
- data: {
229
- action: 'yarpp_optin_".$action."',
230
- '_ajax_nonce': $('#yarpp_optin-nonce').val()
231
- }
232
- });
233
- });
234
- });
235
- </script>\n"
236
- );
237
- }
238
-
239
- function upgrade_notice() {
240
- $this->optin_notice('upgrade');
241
- }
242
-
243
- function install_notice_enable() {
244
- $this->optin_notice('install','Enable');
245
- }
246
-
247
- function install_notice_disable() {
248
- $this->optin_notice('install');
249
- }
250
-
251
- function optin_notice($type = false, $action = 'Disable') {
252
- $screen = get_current_screen();
253
- if(is_null($screen) || $screen->id == 'settings_page_yarpp') return;
254
-
255
- switch($type) {
256
- case 'upgrade':
257
- delete_option('yarpp_upgraded');
258
- break;
259
- case 'install':
260
- break;
261
- default:
262
- $user = get_current_user_id();
263
- update_user_option($user, 'yarpp_saw_optin', true);
264
- }
265
-
266
- echo '<div class="updated fade"><p>';
267
-
268
- if($type === 'upgrade'){
269
- echo '<strong>'.sprintf(__('%1$s updated successfully.'), 'Yet Another Related Posts Plugin' ).'</strong>';
270
- }
271
-
272
- if ( $type == 'install' ){
273
- /* TODO: Why translate YARPP and use str_replace. Performance??? --Eli */
274
- echo(
275
- '<strong>'.
276
- str_replace('<span>','<span style="font-style:italic; font-weight: inherit;">',
277
- __('Thank you for installing <span>Yet Another Related Posts Plugin</span>!', 'yarpp')).
278
- '</strong>'
279
- );
280
- }
281
-
282
- echo (
283
- '<p>'.
284
- 'We would really appreciate your input to help us continue to improve the product. We are primarily looking '.
285
- 'for country, domain, and date installed information.'.
286
- '</p>'.
287
- '<p>'.
288
- 'Please help us make YARPP better providing this information and by filling out our quick, '.
289
- '5 question survey: '.
290
- '<a href="http://www.surveymonkey.com/s/Z278L88" target="_blank">'.
291
- 'http://www.surveymonkey.com/s/Z278L88'.
292
- '</a>'.
293
- '</p>'
294
- );
295
-
296
- echo '</p><p>';
297
- $this->print_optin_button($action);
298
- $this->optin_button_script(strtolower($action));
299
- echo '<a class="button" href="options-general.php?page=yarpp#help-optin">'.__('Learn More', 'yarpp').'</a>';
300
- echo '</p></div>';
301
- }
302
-
303
- // faux-markdown, required for the help text rendering
304
- protected function markdown( $text ) {
305
- $replacements = array(
306
- // strip each line
307
- '!\s*[\r\n] *!' => "\n",
308
-
309
- // headers
310
- '!^=(.*?)=\s*$!m' => '<h3>\1</h3>',
311
-
312
- // bullets
313
- '!^(\* .*([\r\n]\* .*)*)$!m' => "<ul>\n\\1\n</ul>",
314
- '!^\* (.*?)$!m' => '<li>\1</li>',
315
- '!^(\d+\. .*([\r\n]\d+\. .*)*)$!m' => "<ol>\n\\1\n</ol>",
316
- '!^\d+\. (.*?)$!m' => '<li>\1</li>',
317
-
318
- // code block
319
- '!^(\t.*([\r\n]\t.*)*)$!m' => "<pre>\n\\1\n</pre>",
320
-
321
- // wrap p
322
- '!^([^<\t].*[^>])$!m' => '<p>\1</p>',
323
- // bold
324
- '!\*([^*]*?)\*!' => '<strong>\1</strong>',
325
- // code
326
- '!`([^`]*?)`!' => '<code>\1</code>',
327
- // links
328
- '!\[([^]]+)\]\(([^)]+)\)!' => '<a href="\2" target="_new">\1</a>',
329
- );
330
- $text = preg_replace(array_keys($replacements), array_values($replacements), $text);
331
-
332
- return $text;
333
- }
334
-
335
- function render_screen_settings( $output, $current_screen ) {
336
- if ( $current_screen->id != 'settings_page_yarpp' )
337
- return $output;
338
-
339
- $output .= "<div id='yarpp_extra_screen_settings'><label for='yarpp_display_code'><input type='checkbox' name='yarpp_display_code' id='yarpp_display_code'";
340
- $output .= checked( $this->core->get_option('display_code'), true, false );
341
- $output .= " />";
342
- $output .= __('Show example code output', 'yarpp');
343
- $output .= '</label></div>';
344
-
345
- return $output;
346
- }
347
-
348
- // since 3.3
349
- function enqueue() {
350
- $version = defined('WP_DEBUG') && WP_DEBUG ? time() : YARPP_VERSION;
351
- $screen = get_current_screen();
352
- if ( !is_null($screen) && $screen->id == 'settings_page_yarpp' ) {
353
- wp_enqueue_script( 'postbox' );
354
- $this->pointer_enqueue();
355
- wp_enqueue_style( 'yarpp_options', plugins_url( 'options.css', __FILE__ ), array(), $version );
356
- wp_enqueue_script( 'yarpp_options', plugins_url( 'js/options.js', __FILE__ ), array('jquery'), $version );
357
- }
358
-
359
- $metabox_post_types = $this->core->get_option( 'auto_display_post_types' );
360
- if ( !is_null($screen) && ($screen->id == 'post' || in_array( $screen->id, $metabox_post_types )) ) {
361
- wp_enqueue_script( 'yarpp_metabox', plugins_url( 'js/metabox.js', __FILE__ ), array('jquery'), $version );
362
- }
363
- }
364
-
365
- // since 3.4 and WP 3.3
366
- function pointer_enqueue() {
367
- wp_enqueue_style( 'wp-pointer' );
368
- wp_enqueue_script( 'wp-pointer' );
369
- }
370
-
371
- function settings_link($links, $file) {
372
- $this_plugin = dirname(plugin_basename(__FILE__)) . '/yarpp.php';
373
- if($file == $this_plugin) {
374
- $links[] = '<a href="options-general.php?page=yarpp">' . __('Settings') . '</a>';
375
- }
376
- return $links;
377
- }
378
-
379
- function options_page() {
380
- // for proper metabox support:
381
- require(YARPP_DIR.'/options.php');
382
- }
383
-
384
- // @since 3.4: don't actually compute results here, but use ajax instead
385
- function metabox() {
386
- ?>
387
- <style>
388
- #yarpp_relatedposts h3 .postbox-title-action {
389
- right: 30px;
390
- top: 5px;
391
- position: absolute;
392
- padding: 0;
393
- }
394
- #yarpp_relatedposts:hover .edit-box {
395
- display: inline;
396
- }
397
- </style>
398
- <?php
399
- if ( !get_the_ID() ) {
400
- echo "<div><p>".__("Related entries may be displayed once you save your entry",'yarpp').".</p></div>";
401
- } else {
402
- wp_nonce_field( 'yarpp_display', 'yarpp_display-nonce', false );
403
- echo '<div id="yarpp-related-posts"><img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" alt="" /></div>';
404
- }
405
- }
406
-
407
- // @since 3.3: default metaboxes to show:
408
- function default_hidden_meta_boxes($hidden, $screen) {
409
- if ( 'settings_page_yarpp' == $screen->id )
410
- $hidden = $this->core->default_hidden_metaboxes;
411
- return $hidden;
412
- }
413
-
414
- // @since 4: UI to copy templates
415
- function can_copy_templates() {
416
- $theme_dir = get_stylesheet_directory();
417
- // If we can't write to the theme, return false
418
- if ( !is_dir($theme_dir) || !is_writable($theme_dir) )
419
- return false;
420
-
421
- require_once(ABSPATH . 'wp-admin/includes/file.php');
422
- WP_Filesystem( false, get_stylesheet_directory() );
423
- global $wp_filesystem;
424
- // direct method is the only method that I've tested so far
425
- return $wp_filesystem->method == 'direct';
426
- }
427
-
428
- function copy_templates() {
429
- $templates_dir = trailingslashit(trailingslashit(YARPP_DIR) . 'yarpp-templates');
430
-
431
- require_once(ABSPATH . 'wp-admin/includes/file.php');
432
- WP_Filesystem( false, get_stylesheet_directory() );
433
- global $wp_filesystem;
434
- if ( $wp_filesystem->method != 'direct' )
435
- return false;
436
-
437
- return copy_dir( $templates_dir, get_stylesheet_directory(), array('.svn') );
438
- }
439
-
440
- /*
441
- * AJAX SERVICES
442
- */
443
-
444
- function ajax_display_exclude_terms() {
445
- check_ajax_referer( 'yarpp_display_exclude_terms' );
446
-
447
- if ( !isset($_REQUEST['taxonomy']) )
448
- return;
449
-
450
- $taxonomy = (string) $_REQUEST['taxonomy'];
451
-
452
- header("HTTP/1.1 200");
453
- header("Content-Type: text/html; charset=UTF-8");
454
-
455
- $exclude_tt_ids = wp_parse_id_list($this->core->get_option('exclude'));
456
- $exclude_term_ids = $this->get_term_ids_from_tt_ids( $taxonomy, $exclude_tt_ids );
457
- // if ( 'category' == $taxonomy )
458
- // $exclude .= ',' . get_option( 'default_category' );
459
-
460
- $terms = get_terms($taxonomy, array(
461
- 'exclude' => $exclude_term_ids,
462
- 'hide_empty' => false,
463
- 'hierarchical' => false,
464
- 'number' => 100,
465
- 'offset' => $_REQUEST['offset']
466
- ));
467
-
468
- if ( !count($terms) ) {
469
- echo ':('; // no more :(
470
- exit;
471
- }
472
-
473
- foreach ($terms as $term) {
474
- 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> ";
475
- }
476
- exit;
477
- }
478
-
479
- function get_term_ids_from_tt_ids( $taxonomy, $tt_ids ) {
480
- global $wpdb;
481
- $tt_ids = wp_parse_id_list($tt_ids);
482
- if ( empty($tt_ids) )
483
- return array();
484
- return $wpdb->get_col("select term_id from $wpdb->term_taxonomy where taxonomy = '{$taxonomy}' and term_taxonomy_id in (" . join(',', $tt_ids) . ")");
485
- }
486
-
487
- function ajax_display() {
488
- check_ajax_referer( 'yarpp_display' );
489
-
490
- if ( !isset($_REQUEST['ID']) )
491
- return;
492
-
493
- header("HTTP/1.1 200");
494
- header("Content-Type: text/html; charset=UTF-8");
495
-
496
- $args = array(
497
- 'post_type' => array('post'),
498
- 'domain' => isset($_REQUEST['domain']) ? $_REQUEST['domain'] : 'website'
499
- );
500
- if ( $this->core->get_option('cross_relate') )
501
- $args['post_type'] = $this->core->get_post_types();
502
-
503
- $return = $this->core->display_related(absint($_REQUEST['ID']), $args, false);
504
- echo $return;
505
- exit;
506
- }
507
-
508
- function ajax_display_demo() {
509
- check_ajax_referer( 'yarpp_display_demo' );
510
-
511
- header("HTTP/1.1 200");
512
- header("Content-Type: text/html; charset=UTF-8");
513
-
514
- $args = array(
515
- 'post_type' => array('post'),
516
- 'domain' => isset($_REQUEST['domain']) ? $_REQUEST['domain'] : 'website'
517
- );
518
-
519
- $return = $this->core->display_demo_related($args, false);
520
- echo preg_replace("/[\n\r]/",'',nl2br(htmlspecialchars($return)));
521
- exit;
522
- }
523
-
524
- function ajax_optin_data() {
525
- check_ajax_referer('yarpp_optin_data');
526
-
527
- header("HTTP/1.1 200");
528
- header("Content-Type: text/html; charset=UTF-8");
529
-
530
- $data = $this->core->optin_data();
531
- $this->core->pretty_echo($data);
532
- exit;
533
- }
534
-
535
- function ajax_optin_disable() {
536
- check_ajax_referer('yarpp_optin_disable');
537
-
538
- header("HTTP/1.1 200");
539
- header("Content-Type: text; charset=UTF-8");
540
-
541
- $data = $this->core->set_option('optin', false);
542
-
543
- echo 'ok';
544
- exit;
545
- }
546
-
547
- function ajax_optin_enable() {
548
- check_ajax_referer('yarpp_optin_enable');
549
-
550
- header("HTTP/1.1 200");
551
- header("Content-Type: text; charset=UTF-8");
552
-
553
- $data = $this->core->set_option('optin', true);
554
-
555
- $this->core->optin_ping();
556
- echo 'ok';
557
- exit;
558
- }
559
-
560
- function ajax_set_display_code() {
561
- check_ajax_referer( 'yarpp_set_display_code' );
562
-
563
- header("HTTP/1.1 200");
564
- header("Content-Type: text; charset=UTF-8");
565
-
566
- $data = $this->core->set_option( 'display_code', isset($_REQUEST['checked']) );
567
- echo 'ok';
568
- exit;
569
- }
570
- }
class-cache.php DELETED
@@ -1,621 +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 = ( isset($q->query_vars['orderby']) &&
123
- $q->query_vars['orderby'] == 'score' );
124
-
125
- if ( !empty($q->query_vars['showposts']) ) {
126
- $this->online_limit = $q->query_vars['showposts'];
127
- } else {
128
- $this->online_limit = false;
129
- }
130
- } else {
131
- $this->score_override = false;
132
- $this->online_limit = false;
133
- }
134
- }
135
-
136
- /**
137
- * SQL!
138
- */
139
-
140
- protected function sql( $reference_ID = false, $args = array() ) {
141
- global $wpdb, $post;
142
-
143
- if ( is_object($post) && !$reference_ID ) {
144
- $reference_ID = $post->ID;
145
- }
146
-
147
- if ( !is_object($post) || $reference_ID != $post->ID ) {
148
- $reference_post = get_post( $reference_ID );
149
- } else {
150
- $reference_post = $post;
151
- }
152
-
153
- $options = array( 'threshold', 'show_pass_post', 'past_only', 'weight', 'require_tax', 'exclude', 'recent', 'limit' );
154
- extract( $this->core->parse_args($args, $options) );
155
- // The maximum number of items we'll ever want to cache
156
- $limit = max($limit, $this->core->get_option('rss_limit'));
157
-
158
- // Fetch keywords
159
- $keywords = $this->get_keywords($reference_ID);
160
-
161
- // SELECT
162
- $newsql = "SELECT $reference_ID as reference_ID, ID, "; //post_title, post_date, post_content, post_excerpt,
163
-
164
- $newsql .= 'ROUND(0';
165
-
166
- if ( isset($weight['body']) && (int) $weight['body'] )
167
- $newsql .= " + (MATCH (post_content) AGAINST ('".$wpdb->escape($keywords['body'])."')) * ". absint($weight['body']);
168
- if ( isset($weight['body']) && (int) $weight['title'] )
169
- $newsql .= " + (MATCH (post_title) AGAINST ('".$wpdb->escape($keywords['title'])."')) * ". absint($weight['title']);
170
-
171
- // Build tax criteria query parts based on the weights
172
- foreach ( (array) $weight['tax'] as $tax => $tax_weight ) {
173
- $newsql .= " + " . $this->tax_criteria($reference_ID, $tax) . " * " . intval($tax_weight);
174
- }
175
-
176
- $newsql .= ',1) as score';
177
-
178
- $newsql .= "\n from $wpdb->posts \n";
179
-
180
- $exclude_tt_ids = wp_parse_id_list( $exclude );
181
- if ( count($exclude_tt_ids) || count((array) $weight['tax']) || count($require_tax) ) {
182
- $newsql .= "left join $wpdb->term_relationships as terms on ( terms.object_id = $wpdb->posts.ID ) \n";
183
- }
184
-
185
- // WHERE
186
-
187
- $newsql .= " where post_status in ( 'publish', 'static' ) and ID != '$reference_ID'";
188
-
189
- if ( $past_only ) // 3.1.8: revised $past_only option
190
- $newsql .= " and post_date <= '$reference_post->post_date' ";
191
- if ( !$show_pass_post )
192
- $newsql .= " and post_password ='' ";
193
- if ( !!$recent )
194
- $newsql .= " and post_date > date_sub(now(), interval {$recent}) ";
195
-
196
- $newsql .= " and post_type = 'post'";
197
-
198
- // GROUP BY
199
- $newsql .= "\n group by ID \n";
200
-
201
- // HAVING
202
- // number_format fix suggested by vkovalcik! :)
203
- $safethreshold = number_format(max($threshold,0.1), 2, '.', '');
204
- // @since 3.5.3: ID=0 is a special value; never save such a result.
205
- $newsql .= " having score >= $safethreshold and ID != 0";
206
- if ( count($exclude_tt_ids) ) {
207
- $newsql .= " and bit_or(terms.term_taxonomy_id in (" . join(',', $exclude_tt_ids) . ")) = 0";
208
- }
209
-
210
- foreach ( (array) $require_tax as $tax => $number ) {
211
- $newsql .= ' and ' . $this->tax_criteria($reference_ID, $tax) . ' >= ' . intval($number);
212
- }
213
-
214
- $newsql .= " order by score desc limit $limit";
215
-
216
- if ( isset($args['post_type']) )
217
- $post_types = (array) $args['post_type'];
218
- else
219
- $post_types = $this->core->get_post_types();
220
-
221
- $queries = array();
222
- foreach ( $post_types as $post_type ) {
223
- $queries[] = '(' . str_replace("post_type = 'post'", "post_type = '{$post_type}'", $newsql) . ')';
224
- }
225
- $sql = implode( ' union ', $queries );
226
-
227
- if ($this->core->debug) echo "<!--$sql-->";
228
-
229
- $this->last_sql = $sql;
230
-
231
- return $sql;
232
- }
233
-
234
- private function tax_criteria( $reference_ID, $taxonomy ) {
235
- // @todo maybe reinforce the object term cache?
236
- $terms = get_the_terms( $reference_ID, $taxonomy );
237
- // if there are no terms of that tax
238
- if ( false === $terms )
239
- return '(1 = 0)';
240
-
241
- $tt_ids = wp_list_pluck($terms, 'term_taxonomy_id');
242
- return "count(distinct if( terms.term_taxonomy_id in (" . join(',',$tt_ids) . "), terms.term_taxonomy_id, null ))";
243
- }
244
-
245
- /**
246
- * KEYWORDS
247
- */
248
-
249
- // @param $ID (int)
250
- // @param $type (string) body | title | all
251
- // @return (string|array) depending on whether "all" were requested or not
252
- public function get_keywords( $ID, $type = 'all' ) {
253
- if ( !$ID = absint($ID) )
254
- return false;
255
-
256
- $keywords = array(
257
- 'body' => $this->body_keywords($ID),
258
- 'title' => $this->title_keywords($ID)
259
- );
260
-
261
- if ( empty($keywords) )
262
- return false;
263
-
264
- if ( 'all' == $type )
265
- return $keywords;
266
- return $keywords[$type];
267
- }
268
-
269
- protected function title_keywords( $ID, $max = 20 ) {
270
- return apply_filters( 'yarpp_title_keywords',
271
- $this->extract_keywords( get_the_title($ID), $max, $ID ), $max, $ID );
272
- }
273
-
274
- protected function body_keywords( $ID, $max = 20 ) {
275
- $post = get_post( $ID );
276
- if ( empty($post) )
277
- return '';
278
- $content = $this->apply_filters_if_white( 'the_content', $post->post_content );
279
- return apply_filters( 'yarpp_body_keywords',
280
- $this->extract_keywords( $content, $max, $ID ), $max, $ID );
281
-
282
- }
283
-
284
- private function extract_keywords( $html, $max = 20, $ID = 0 ) {
285
-
286
- /**
287
- * @filter yarpp_extract_keywords
288
- *
289
- * Use this filter to override YARPP's built-in keyword computation
290
- * Return values should be a string of space-delimited words
291
- *
292
- * @param $keywords
293
- * @param $html unfiltered HTML content
294
- * @param (int) $max maximum number of keywords
295
- * @param (int) $ID
296
- */
297
- if ( $keywords = apply_filters( 'yarpp_extract_keywords', false, $html, $max, $ID ) )
298
- return $keywords;
299
-
300
- $lang = 'en_US';
301
- if ( defined('WPLANG') ) {
302
- switch ( substr(WPLANG, 0, 2) ) {
303
- case 'de':
304
- $lang = 'de_DE';
305
- case 'it':
306
- $lang = 'it_IT';
307
- case 'pl':
308
- $lang = 'pl_PL';
309
- case 'bg':
310
- $lang = 'bg_BG';
311
- case 'fr':
312
- $lang = 'fr_FR';
313
- case 'cs':
314
- $lang = 'cs_CZ';
315
- case 'nl':
316
- $lang = 'nl_NL';
317
- default:
318
- $lang = 'en_US';
319
- }
320
- }
321
-
322
- $words_file = YARPP_DIR . '/lang/words-' . $lang . '.php';
323
- if ( file_exists($words_file) )
324
- include( $words_file );
325
- if ( !isset($overusedwords) )
326
- $overusedwords = array();
327
-
328
- // strip tags and html entities
329
- $text = preg_replace('/&(#x[0-9a-f]+|#[0-9]+|[a-zA-Z]+);/', '', strip_tags($html) );
330
-
331
- // 3.2.2: ignore soft hyphens
332
- // Requires PHP 5: http://bugs.php.net/bug.php?id=25670
333
- $softhyphen = html_entity_decode('&#173;',ENT_NOQUOTES,'UTF-8');
334
- $text = str_replace($softhyphen, '', $text);
335
-
336
- $charset = get_option('blog_charset');
337
- if ( function_exists('mb_split') && !empty($charset) ) {
338
- mb_regex_encoding($charset);
339
- $wordlist = mb_split('\s*\W+\s*', mb_strtolower($text, $charset));
340
- } else
341
- $wordlist = preg_split('%\s*\W+\s*%', strtolower($text));
342
-
343
- // Build an array of the unique words and number of times they occur.
344
- $tokens = array_count_values($wordlist);
345
-
346
- // Remove the stop words from the list.
347
- $overusedwords = apply_filters( 'yarpp_keywords_overused_words', $overusedwords );
348
- if ( is_array($overusedwords) ) {
349
- foreach ($overusedwords as $word) {
350
- unset($tokens[$word]);
351
- }
352
- }
353
- // Remove words which are only a letter
354
- foreach (array_keys($tokens) as $word) {
355
- if ( function_exists('mb_strlen') )
356
- if (mb_strlen($word) < 2) unset($tokens[$word]);
357
- else
358
- if (strlen($word) < 2) unset($tokens[$word]);
359
- }
360
-
361
- arsort($tokens, SORT_NUMERIC);
362
-
363
- $types = array_keys($tokens);
364
-
365
- if (count($types) > $max)
366
- $types = array_slice($types, 0, $max);
367
- return implode(' ', $types);
368
- }
369
-
370
- /* new in 2.0! apply_filters_if_white (previously apply_filters_without) now has a blacklist.
371
- * It can be modified via the yarpp_blacklist and yarpp_blackmethods filters.
372
- */
373
- /* blacklisted so far:
374
- - diggZ-Et
375
- - reddZ-Et
376
- - dzoneZ-Et
377
- - WP-Syntax
378
- - Viper's Video Quicktags
379
- - WP-CodeBox
380
- - WP shortcodes
381
- - WP Greet Box
382
- - Jetpack ShareDaddy
383
- //- Tweet This - could not reproduce problem.
384
- */
385
- function white( $filter ) {
386
- static $blacklist, $blackmethods;
387
-
388
- if ( is_null($blacklist) || is_null($blackmethods) ) {
389
- $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'
390
- $yarpp_blackmethods = array('addinlinejs', 'replacebbcode', 'filter_content');
391
-
392
- $blacklist = (array) apply_filters( 'yarpp_blacklist', $yarpp_blacklist );
393
- $blackmethods = (array) apply_filters( 'yarpp_blackmethods', $yarpp_blackmethods );
394
- }
395
-
396
- if ( is_array($filter) && is_a( $filter[0], 'YARPP' ) )
397
- return false;
398
- if ( is_array($filter) && in_array( $filter[1], $blackmethods ) )
399
- return false;
400
- return !in_array( $filter, $blacklist );
401
- }
402
-
403
- /* 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').*/
404
- function apply_filters_if_white($tag, $value) {
405
- global $wp_filter, $merged_filters, $wp_current_filter;
406
-
407
- $args = array();
408
-
409
- // Do 'all' actions first
410
- if ( isset($wp_filter['all']) ) {
411
- $wp_current_filter[] = $tag;
412
- $args = func_get_args();
413
- _wp_call_all_hook($args);
414
- }
415
-
416
- if ( !isset($wp_filter[$tag]) ) {
417
- if ( isset($wp_filter['all']) )
418
- array_pop($wp_current_filter);
419
- return $value;
420
- }
421
-
422
- if ( !isset($wp_filter['all']) )
423
- $wp_current_filter[] = $tag;
424
-
425
- // Sort
426
- if ( !isset( $merged_filters[ $tag ] ) ) {
427
- ksort($wp_filter[$tag]);
428
- $merged_filters[ $tag ] = true;
429
- }
430
-
431
- reset( $wp_filter[ $tag ] );
432
-
433
- if ( empty($args) )
434
- $args = func_get_args();
435
-
436
- do {
437
- foreach( (array) current($wp_filter[$tag]) as $the_ )
438
- if ( !is_null($the_['function'])
439
- and $this->white($the_['function'])){ // HACK
440
- $args[1] = $value;
441
- $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
442
- }
443
-
444
- } while ( next($wp_filter[$tag]) !== false );
445
-
446
- array_pop( $wp_current_filter );
447
-
448
- return $value;
449
- }
450
- }
451
-
452
- class YARPP_Cache_Bypass extends YARPP_Cache {
453
-
454
- public $name = "bypass";
455
-
456
- // variables used for lookup
457
- private $related_postdata = array();
458
- private $related_IDs = array();
459
-
460
- public $demo_time = false;
461
- private $demo_limit = 0;
462
-
463
- /**
464
- * SETUP/STATUS
465
- */
466
- function __construct( &$core ) {
467
- parent::__construct( $core );
468
- }
469
-
470
- public function is_enabled() {
471
- return true; // always enabled.
472
- }
473
-
474
- public function cache_status() {
475
- return 0; // always uncached
476
- }
477
-
478
- public function stats() {
479
- return array(); // always unknown
480
- }
481
-
482
- public function uncached($limit = 20, $offset = 0) {
483
- return array(); // nothing to cache
484
- }
485
-
486
- /**
487
- * MAGIC FILTERS
488
- */
489
- public function where_filter($arg) {
490
- global $wpdb;
491
- // modify the where clause to use the related ID list.
492
- if (!count($this->related_IDs))
493
- $this->related_IDs = array(0);
494
- $arg = preg_replace("!{$wpdb->posts}.ID = \d+!","{$wpdb->posts}.ID in (".join(',',$this->related_IDs).")",$arg);
495
-
496
- // if we have recent set, add an additional condition
497
- if ( !!$this->args['recent'] )
498
- $arg .= " and post_date > date_sub(now(), interval {$this->args['recent']}) ";
499
- return $arg;
500
- }
501
-
502
- public function orderby_filter($arg) {
503
- global $wpdb;
504
- // only order by score if the score function is added in fields_filter, which only happens
505
- // if there are related posts in the postdata
506
- if ($this->score_override &&
507
- is_array($this->related_postdata) && count($this->related_postdata))
508
- return str_replace("$wpdb->posts.post_date","score",$arg);
509
- return $arg;
510
- }
511
-
512
- public function fields_filter($arg) {
513
- global $wpdb;
514
- if (is_array($this->related_postdata) && count($this->related_postdata)) {
515
- $scores = array();
516
- foreach ($this->related_postdata as $related_entry) {
517
- $scores[] = " WHEN {$related_entry['ID']} THEN {$related_entry['score']}";
518
- }
519
- $arg .= ", CASE {$wpdb->posts}.ID" . join('',$scores) ." END as score";
520
- }
521
- return $arg;
522
- }
523
-
524
- public function demo_request_filter($arg) {
525
- global $wpdb;
526
- $wpdb->query("set @count = 0;");
527
-
528
- $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.';
529
-
530
- return "SELECT SQL_CALC_FOUND_ROWS ID + {$this->demo_limit} as ID, post_author, post_date, post_date_gmt, '{$loremipsum}' as post_content,
531
- 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
532
- FROM $wpdb->posts
533
- ORDER BY ID DESC LIMIT 0, {$this->demo_limit}";
534
- }
535
-
536
- public function limit_filter($arg) {
537
- global $wpdb;
538
- if ($this->online_limit)
539
- return " limit {$this->online_limit} ";
540
- return $arg;
541
- }
542
-
543
- /**
544
- * RELATEDNESS CACHE CONTROL
545
- */
546
- public function begin_yarpp_time( $reference_ID, $args ) {
547
- global $wpdb;
548
-
549
- $this->yarpp_time = true;
550
- $options = array( 'threshold', 'show_pass_post', 'past_only', 'weight', 'require_tax', 'exclude', 'recent', 'limit' );
551
- $this->args = $this->core->parse_args($args, $options);
552
-
553
- $this->related_postdata = $wpdb->get_results($this->sql($reference_ID, $args), ARRAY_A);
554
- $this->related_IDs = wp_list_pluck( $this->related_postdata, 'ID' );
555
-
556
- add_filter('posts_where',array(&$this,'where_filter'));
557
- add_filter('posts_orderby',array(&$this,'orderby_filter'));
558
- add_filter('posts_fields',array(&$this,'fields_filter'));
559
- add_filter('post_limits',array(&$this,'limit_filter'));
560
- add_action('pre_get_posts',array(&$this,'add_signature'));
561
- // sets the score override flag.
562
- add_action('parse_query',array(&$this,'set_score_override_flag'));
563
- }
564
-
565
- public function begin_demo_time( $limit ) {
566
- $this->demo_time = true;
567
- $this->demo_limit = $limit;
568
- add_action('pre_get_posts',array(&$this,'add_signature'));
569
- add_filter('posts_request',array(&$this,'demo_request_filter'));
570
- }
571
-
572
- public function end_yarpp_time() {
573
- $this->yarpp_time = false;
574
- remove_filter('posts_where',array(&$this,'where_filter'));
575
- remove_filter('posts_orderby',array(&$this,'orderby_filter'));
576
- remove_filter('posts_fields',array(&$this,'fields_filter'));
577
- remove_filter('post_limits',array(&$this,'limit_filter'));
578
- remove_action('pre_get_posts',array(&$this,'add_signature'));
579
- remove_action('parse_query',array(&$this,'set_score_override_flag'));
580
- }
581
-
582
- public function end_demo_time() {
583
- $this->demo_time = false;
584
- remove_action('pre_get_posts',array(&$this,'add_signature'));
585
- remove_filter('posts_request',array(&$this,'demo_request_filter'));
586
- }
587
-
588
- // @return YARPP_NO_RELATED | YARPP_RELATED
589
- // @used by enforce
590
- protected function update($reference_ID) {
591
- global $wpdb;
592
-
593
- return YARPP_RELATED;
594
- }
595
-
596
- public function related($reference_ID = null, $related_ID = null) {
597
- global $wpdb;
598
-
599
- if ( !is_int( $reference_ID ) && !is_int( $related_ID ) ) {
600
- _doing_it_wrong( __METHOD__, 'reference ID and/or related ID must be set', '3.4' );
601
- return;
602
- }
603
-
604
- // reverse lookup
605
- if ( is_int($related_ID) && is_null($reference_ID) ) {
606
- _doing_it_wrong( __METHOD__, 'YARPP_Cache_Bypass::related cannot do a reverse lookup', '3.4' );
607
- return;
608
- }
609
-
610
- $results = $wpdb->get_results($this->sql($reference_ID), ARRAY_A);
611
- if ( !$results || !count($results) )
612
- return false;
613
-
614
- $results_ids = wp_list_pluck( $results, 'ID' );
615
- if ( is_null($related_ID) ) {
616
- return $results_ids;
617
- } else {
618
- return in_array( $related_ID, $results_ids );
619
- }
620
- }
621
- }
class-core.php DELETED
@@ -1,1427 +0,0 @@
1
- <?php
2
-
3
- // new in 3.4: put everything YARPP into an object, expected to be a singleton global $yarpp
4
- class YARPP {
5
-
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
-
15
- // here's a list of all the options YARPP uses (except version), as well as their default values, sans the yarpp_ prefix, split up into binary options and value options. These arrays are used in updating settings (options.php) and other tasks.
16
- public $default_options = array();
17
- public $default_hidden_metaboxes = array( 'yarpp_pool', 'yarpp_relatedness' );
18
-
19
- function __construct() {
20
- $this->load_default_options();
21
-
22
- // register text domain
23
- load_plugin_textdomain( 'yarpp', false, dirname(plugin_basename(__FILE__)) . '/lang' );
24
-
25
- // load cache object
26
- require_once(YARPP_DIR . '/class-cache.php');
27
- require_once(YARPP_DIR . '/cache-' . YARPP_CACHE_TYPE . '.php');
28
- $this->storage_class = $yarpp_storage_class;
29
- $this->cache = new $this->storage_class( $this );
30
- $this->cache_bypass = new YARPP_Cache_Bypass( $this );
31
-
32
- register_activation_hook( __FILE__, array($this, 'activate') );
33
-
34
- // new in 3.2: update cache on delete
35
- add_action( 'delete_post', array($this->cache, 'delete_post'), 10, 1 );
36
- // new in 3.2.1: handle post_status transitions
37
- // new in 3.5.3: use transition_post_status instead of save_post hook
38
- add_action( 'transition_post_status', array($this->cache, 'transition_post_status'), 10, 3);
39
-
40
- // automatic display hooks:
41
- add_filter( 'the_content', array( $this, 'the_content' ), 1200 );
42
- add_filter( 'the_content_feed', array( $this, 'the_content_feed' ), 600 );
43
- add_filter( 'the_excerpt_rss', array( $this, 'the_excerpt_rss' ), 600 );
44
- add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_thumbnails' ) );
45
-
46
- // if we're using thumbnails, register yarpp-thumbnail size, if theme has not already
47
- // @todo: make these UI-configurable?
48
- // Note: see FAQ in the readme if you would like to change the YARPP thumbnail size.
49
-
50
- if ( $this->diagnostic_using_thumbnails() &&
51
- ( !($dimensions = $this->thumbnail_dimensions()) || isset($dimensions['_default']) ) ) {
52
- $width = 120;
53
- $height = 120;
54
- $crop = true;
55
- add_image_size( 'yarpp-thumbnail', $width, $height, $crop );
56
- }
57
-
58
- if ( isset($_REQUEST['yarpp_debug']) )
59
- $this->debug = true;
60
-
61
- if ( !get_option('yarpp_version') )
62
- update_option( 'yarpp_activated', true );
63
-
64
- // new in 3.4: only load UI if we're in the admin
65
- if ( is_admin() ) {
66
- require_once(YARPP_DIR . '/class-admin.php');
67
- $this->admin = new YARPP_Admin( $this );
68
- $this->enforce();
69
- }
70
- }
71
-
72
- /*
73
- * OPTIONS
74
- */
75
-
76
- private function load_default_options() {
77
- $this->default_options = array(
78
- 'threshold' => 4, // changed default in 4
79
- 'limit' => 4, // changed default in 4
80
- 'excerpt_length' => 10,
81
- 'recent' => false, // new in 3.5
82
- 'before_title' => '<li>',
83
- 'after_title' => '</li>',
84
- 'before_post' => ' <small>',
85
- 'after_post' => '</small>',
86
- 'before_related' => '<h3>'.__('Related posts:','yarpp').'</h3><ol>',
87
- 'after_related' => '</ol>',
88
- 'no_results' => '<p>'.__('No related posts.','yarpp').'</p>',
89
- 'order' => 'score DESC',
90
- 'rss_limit' => 3,
91
- 'rss_excerpt_length' => 10,
92
- 'rss_before_title' => '<li>',
93
- 'rss_after_title' => '</li>',
94
- 'rss_before_post' => ' <small>',
95
- 'rss_after_post' => '</small>',
96
- 'rss_before_related' => '<h3>'.__('Related posts:','yarpp').'</h3><ol>',
97
- 'rss_after_related' => '</ol>',
98
- 'rss_no_results' => '<p>'.__('No related posts.','yarpp').'</p>',
99
- 'rss_order' => 'score DESC',
100
- 'past_only' => false,
101
- 'show_excerpt' => false,
102
- 'rss_show_excerpt' => false,
103
- 'template' => false, // new in 3.5
104
- 'rss_template' => false, // new in 3.5
105
- 'show_pass_post' => false,
106
- 'cross_relate' => false,
107
- 'rss_display' => false, // changed default in 3.1.7
108
- 'rss_excerpt_display' => true,
109
- 'promote_yarpp' => false,
110
- 'rss_promote_yarpp' => false,
111
- 'myisam_override' => false,
112
- 'exclude' => '', // conslidated YARPP 3.4 and further in 3.5
113
- 'weight' => array( // consolidated in YARPP 3.4, format changed in 3.5
114
- 'title' => 1,
115
- 'body' => 1,
116
- 'tax' => array(
117
- 'category' => 1, // changed default in 3.4
118
- 'post_tag' => 1
119
- )
120
- ),
121
- 'require_tax' => array(), // new in 3.5
122
- 'optin' => false, // new in 4, default on 4.0.7
123
- 'thumbnails_heading' => __('Related posts:','yarpp'), // new in 4
124
- 'thumbnails_default' => plugins_url( 'default.png', __FILE__ ), // new in 4
125
- 'rss_thumbnails_heading' => __('Related posts:','yarpp'), // new in 4
126
- 'rss_thumbnails_default' => plugins_url( 'default.png', __FILE__ ), // new in 4
127
- 'display_code' => false, // new in 4
128
- 'auto_display_archive' => false, // new in 4
129
- 'auto_display_post_types' => array( 'post' ), // new in 4, replacing auto_display
130
- 'pools' => array(), // new in 4
131
- 'manually_using_thumbnails' => false, // new in 4.0.6
132
- );
133
- }
134
-
135
- function set_option( $options, $value = null ) {
136
- $current_options = $this->get_option();
137
-
138
- // we can call yarpp_set_option(key,value) if we like:
139
- if ( !is_array($options) ) {
140
- if ( isset($value) )
141
- $options = array( $options => $value );
142
- else
143
- return false;
144
- }
145
-
146
- $new_options = array_merge( $current_options, $options );
147
- update_option( 'yarpp', $new_options );
148
-
149
- // new in 3.1: clear cache when updating certain settings.
150
- $clear_cache_options = array( 'show_pass_post' => 1, 'recent' => 1, 'threshold' => 1, 'past_only' => 1 );
151
-
152
- $relevant_options = array_intersect_key( $options, $clear_cache_options );
153
- $relevant_current_options = array_intersect_key( $current_options, $clear_cache_options );
154
- $new_options_which_require_flush = array_diff_assoc($relevant_options, $relevant_current_options);
155
- if ( count($new_options_which_require_flush) ||
156
- ( $new_options['limit'] > $current_options['limit'] ) ||
157
- ( $new_options['weight'] != $current_options['weight'] ) ||
158
- ( $new_options['exclude'] != $current_options['exclude'] ) ||
159
- ( $new_options['require_tax'] != $current_options['require_tax'] ) )
160
- $this->cache->flush();
161
- }
162
-
163
- // 3.4b8: $option can be a path, of the query_str variety, i.e. "option[suboption][subsuboption]"
164
- function get_option( $option = null ) {
165
- $options = (array) get_option( 'yarpp', array() );
166
-
167
- // ensure defaults if not set:
168
- $options = array_merge( $this->default_options, $options );
169
-
170
- if ( is_null( $option ) )
171
- return $options;
172
-
173
- $optionpath = array();
174
- $parsed_option = array();
175
- wp_parse_str($option, $parsed_option);
176
- $optionpath = $this->array_flatten($parsed_option);
177
-
178
- $current = $options;
179
- foreach ( $optionpath as $optionpart ) {
180
- if ( !is_array($current) || !isset($current[$optionpart]) )
181
- return null;
182
- $current = $current[$optionpart];
183
- }
184
- return $current;
185
- }
186
-
187
- private function array_flatten($array, $given = array()) {
188
- foreach ($array as $key => $val) {
189
- $given[] = $key;
190
- if ( is_array($val) )
191
- $given = $this->array_flatten($val, $given);
192
- }
193
- return $given;
194
- }
195
-
196
- /*
197
- * INFRASTRUCTURE
198
- */
199
-
200
- function enabled() {
201
- global $wpdb;
202
-
203
- if ( $this->cache->is_enabled() === false )
204
- return false;
205
-
206
- if ( !$this->diagnostic_fulltext_disabled() )
207
- return $this->diagnostic_fulltext_indices();
208
-
209
- return true;
210
- }
211
-
212
- // @since 3.5.2: function to enforce YARPP setup
213
- // if not ready, activate; else upgrade
214
- function enforce() {
215
- if ( !$this->enabled() )
216
- $this->activate(); // activate calls upgrade later, so it's covered.
217
- else
218
- $this->upgrade();
219
-
220
- if ( $this->get_option('optin') )
221
- $this->optin_ping();
222
- }
223
-
224
- function activate() {
225
- global $wpdb;
226
-
227
- // if it's not known to be disabled, but the indexes aren't there:
228
- if ( !$this->diagnostic_fulltext_disabled() && !$this->diagnostic_fulltext_indices() ) {
229
- $this->enable_fulltext();
230
- }
231
-
232
- if ( $this->cache->is_enabled() === false ) {
233
- // Run the cache abstraction's setup method.
234
- $this->cache->setup();
235
- }
236
-
237
- // If we're not enabled, give up.
238
- if ( !$this->enabled() )
239
- return false;
240
-
241
- if ( !get_option('yarpp_version') ) {
242
- // new install
243
-
244
- add_option( 'yarpp_version', YARPP_VERSION );
245
- $this->version_info(true);
246
- } else {
247
- // upgrade
248
- $this->upgrade();
249
- }
250
-
251
- return true;
252
- }
253
-
254
- /*
255
- * DIAGNOSTICS
256
- * @since 4 moved into separate functions. Note return value types can differ.
257
- */
258
-
259
- function diagnostic_myisam_posts() {
260
- global $wpdb;
261
- $tables = $wpdb->get_results("show table status like '{$wpdb->posts}'");
262
- foreach ($tables as $table) {
263
- if ($table->Engine == 'MyISAM')
264
- return true;
265
- else
266
- return $table->Engine;
267
- }
268
- return 'UNKNOWN';
269
- }
270
-
271
- function diagnostic_fulltext_disabled() {
272
- return get_option( 'yarpp_fulltext_disabled', false );
273
- }
274
-
275
- function enable_fulltext( $override_myisam = false ) {
276
- global $wpdb;
277
-
278
- // todo: check the myisam_override option instead.
279
- if ( !$override_myisam ) {
280
- $table_type = $this->diagnostic_myisam_posts();
281
- if ( $table_type !== true ) {
282
- $this->disable_fulltext();
283
- return;
284
- }
285
- }
286
-
287
- // temporarily ensure that errors are not displayed:
288
- $previous_value = $wpdb->hide_errors();
289
-
290
- $wpdb->query("ALTER TABLE $wpdb->posts ADD FULLTEXT `yarpp_title` ( `post_title` )");
291
- if ( !empty($wpdb->last_error) )
292
- $this->disable_fulltext();
293
-
294
- $wpdb->query("ALTER TABLE $wpdb->posts ADD FULLTEXT `yarpp_content` ( `post_content` )");
295
- if ( !empty($wpdb->last_error) )
296
- $this->disable_fulltext();
297
-
298
- // restore previous setting
299
- $wpdb->show_errors( $previous_value );
300
- }
301
-
302
- function disable_fulltext() {
303
- if ( get_option( 'yarpp_fulltext_disabled', false ) == true )
304
- return;
305
-
306
- // rm title and body weights:
307
- $weight = $this->get_option('weight');
308
- unset($weight['title']);
309
- unset($weight['body']);
310
- $this->set_option(array('weight' => $weight));
311
-
312
- // cut threshold by half:
313
- $threshold = (float) $this->get_option('threshold');
314
- $this->set_option(array('threshold' => round($threshold / 2) ));
315
-
316
- update_option( 'yarpp_fulltext_disabled', true );
317
- }
318
-
319
- function diagnostic_fulltext_indices() {
320
- global $wpdb;
321
- $wpdb->get_results("show index from $wpdb->posts where Key_name = 'yarpp_title' or Key_name = 'yarpp_content'");
322
- return ( $wpdb->num_rows >= 2 );
323
- }
324
-
325
- function diagnostic_hidden_metaboxes() {
326
- global $wpdb;
327
- $raw = $wpdb->get_var("select meta_value from $wpdb->usermeta where meta_key = 'metaboxhidden_settings_page_yarpp' order by length(meta_value) asc limit 1");
328
-
329
- if ( !$raw )
330
- return $this->default_hidden_metaboxes;
331
-
332
- $list = maybe_unserialize( $raw );
333
- if ( !is_array($list) )
334
- return $this->default_hidden_metaboxes;
335
-
336
- return implode('|', $list);
337
- }
338
-
339
- function diagnostic_post_thumbnails() {
340
- return current_theme_supports( 'post-thumbnails', 'post' );
341
- }
342
-
343
- function diagnostic_custom_templates() {
344
- return count( $this->admin->get_templates() );
345
- }
346
-
347
- function diagnostic_happy() {
348
- $stats = $this->cache->stats();
349
-
350
- if ( !(array_sum( $stats ) > 0) )
351
- return false;
352
-
353
- $sum = array_sum(array_map('array_product', array_map(null, array_values($stats), array_keys($stats))));
354
- $avg = $sum / array_sum( $stats );
355
-
356
- return $this->cache->cache_status() > 0.1 && $avg > 2;
357
- }
358
-
359
- function diagnostic_generate_thumbnails() {
360
- return defined('YARPP_GENERATE_THUMBNAILS') && YARPP_GENERATE_THUMBNAILS;
361
- }
362
-
363
- private $default_dimensions = array(
364
- 'width' => 120,
365
- 'height' => 120,
366
- 'crop' => false, // @todo true for crop?
367
- 'size' => '120x120',
368
- '_default' => true
369
- );
370
-
371
- function diagnostic_using_thumbnails() {
372
- if ( $this->get_option( 'manually_using_thumbnails' ) )
373
- return true;
374
- if ( $this->get_option( 'template' ) == 'thumbnails' )
375
- return true;
376
- if ( $this->get_option( 'rss_template' ) == 'thumbnails' && $this->get_option( 'rss_display' ) )
377
- return true;
378
- return false;
379
- }
380
-
381
- function thumbnail_dimensions() {
382
- global $_wp_additional_image_sizes;
383
- if ( !isset($_wp_additional_image_sizes['yarpp-thumbnail']) )
384
- return $this->default_dimensions;
385
-
386
- $dimensions = $_wp_additional_image_sizes['yarpp-thumbnail'];
387
- $dimensions['size'] = 'yarpp-thumbnail';
388
-
389
- // ensure YARPP dimensions format:
390
- $dimensions['width'] = (int) $dimensions['width'];
391
- $dimensions['height'] = (int) $dimensions['height'];
392
- return $dimensions;
393
- }
394
-
395
- function maybe_enqueue_thumbnails() {
396
- if ( is_feed() )
397
- return;
398
-
399
- $auto_display_post_types = $this->get_option( 'auto_display_post_types' );
400
-
401
- // if it's not an auto-display post type, return
402
- if ( !in_array( get_post_type(), $auto_display_post_types ) )
403
- return;
404
-
405
- if ( !is_singular() && !(
406
- $this->get_option('auto_display_archive') &&
407
- ( is_archive() || is_home() )
408
- ) )
409
- return;
410
-
411
- if ( $this->get_option('template') !== 'thumbnails' )
412
- return;
413
-
414
- $this->enqueue_thumbnails( $this->thumbnail_dimensions() );
415
- }
416
-
417
- function enqueue_thumbnails( $dimensions ) {
418
- wp_enqueue_style( "yarpp-thumbnails-" . $dimensions['size'], plugins_url( 'styles-thumbnails.php?' . http_build_query( array( 'width' => $dimensions['width'], 'height' => $dimensions['height'] ) ), __FILE__ ), array(), YARPP_VERSION, 'all' );
419
- }
420
-
421
- // code based on Viper's Regenerate Thumbnails plugin
422
- // $dimensions must be an array with size, crop, height, width attributes
423
- function ensure_resized_post_thumbnail( $post_id, $dimensions ) {
424
- $thumbnail_id = get_post_thumbnail_id( $post_id );
425
- $downsized = image_downsize( $thumbnail_id, $dimensions['size'] );
426
- if ( $dimensions['crop'] && $downsized[1] && $downsized[2] &&
427
- ( $downsized[1] != $dimensions['width'] || $downsized[2] != $dimensions['height'] ) ) {
428
- // we want to trigger recomputation of the thumbnail here
429
- // (only if downsized width and height are specified, for Photon behavior)
430
- $fullsizepath = get_attached_file( $thumbnail_id );
431
- if ( false !== $fullsizepath && file_exists( $fullsizepath ) ) {
432
- require_once(ABSPATH . 'wp-admin/includes/image.php');
433
- $metadata = wp_generate_attachment_metadata( $thumbnail_id, $fullsizepath );
434
- if ( !is_wp_error( $metadata ) ) {
435
- wp_update_attachment_metadata( $thumbnail_id, $metadata );
436
- }
437
- }
438
- }
439
- }
440
-
441
- private $templates = null;
442
- public function get_templates() {
443
- if ( is_null($this->templates) ) {
444
- $this->templates = glob(STYLESHEETPATH . '/yarpp-template-*.php');
445
- // if glob hits an error, it returns false.
446
- if ( $this->templates === false )
447
- $this->templates = array();
448
- // get basenames only
449
- $this->templates = array_map(array($this, 'get_template_data'), $this->templates);
450
- }
451
- return (array) $this->templates;
452
- }
453
-
454
- public function get_template_data( $file ) {
455
- $headers = array(
456
- 'name' => 'YARPP Template',
457
- 'description' => 'Description',
458
- 'author' => 'Author',
459
- 'uri' => 'Author URI',
460
- );
461
- $data = get_file_data( $file, $headers );
462
- $data['file'] = $file;
463
- $data['basename'] = basename($file);
464
- if ( empty($data['name']) )
465
- $data['name'] = $data['basename'];
466
- return $data;
467
- }
468
-
469
- /*
470
- * UPGRADE ROUTINES
471
- */
472
-
473
- function upgrade() {
474
- $last_version = get_option( 'yarpp_version' );
475
- if (version_compare(YARPP_VERSION, $last_version) === 0)
476
- return;
477
-
478
- if ( $last_version && version_compare('3.4b2', $last_version) > 0 )
479
- $this->upgrade_3_4b2();
480
- if ( $last_version && version_compare('3.4b5', $last_version) > 0 )
481
- $this->upgrade_3_4b5();
482
- if ( $last_version && version_compare('3.4b8', $last_version) > 0 )
483
- $this->upgrade_3_4b8();
484
- if ( $last_version && version_compare('3.4.4b2', $last_version) > 0 )
485
- $this->upgrade_3_4_4b2();
486
- if ( $last_version && version_compare('3.4.4b3', $last_version) > 0 )
487
- $this->upgrade_3_4_4b3();
488
- if ( $last_version && version_compare('3.4.4b4', $last_version) > 0 )
489
- $this->upgrade_3_4_4b4();
490
- if ( $last_version && version_compare('3.5.2b2', $last_version) > 0 )
491
- $this->upgrade_3_5_2b2();
492
- if ( $last_version && version_compare('3.6b7', $last_version) > 0 )
493
- $this->upgrade_3_6b7();
494
- if ( $last_version && version_compare('4.0.1', $last_version) > 0 )
495
- $this->upgrade_4_0_1();
496
-
497
- $this->cache->upgrade($last_version);
498
- // flush cache in 3.4.1b5 as 3.4 messed up calculations.
499
- if ( $last_version && version_compare('3.4.1b5', $last_version) > 0 )
500
- $this->cache->flush();
501
-
502
- $this->version_info(true);
503
-
504
- update_option( 'yarpp_version', YARPP_VERSION );
505
- update_option( 'yarpp_upgraded', true );
506
- $this->delete_transient( 'yarpp_optin' );
507
- }
508
-
509
- function upgrade_3_4b2() {
510
- global $wpdb;
511
-
512
- $yarpp_3_3_options = array(
513
- 'threshold' => 4,
514
- 'limit' => 4,
515
- 'template_file' => '', // new in 2.2
516
- 'excerpt_length' => 10,
517
- 'recent_number' => 12,
518
- 'recent_units' => 'month',
519
- 'before_title' => '<li>',
520
- 'after_title' => '</li>',
521
- 'before_post' => ' <small>',
522
- 'after_post' => '</small>',
523
- 'before_related' => '<h3>'.__('Related posts:','yarpp').'</h3><ol>',
524
- 'after_related' => '</ol>',
525
- 'no_results' => '<p>'.__('No related posts.','yarpp').'</p>',
526
- 'order' => 'score DESC',
527
- 'rss_limit' => 3,
528
- 'rss_template_file' => '', // new in 2.2
529
- 'rss_excerpt_length' => 10,
530
- 'rss_before_title' => '<li>',
531
- 'rss_after_title' => '</li>',
532
- 'rss_before_post' => ' <small>',
533
- 'rss_after_post' => '</small>',
534
- 'rss_before_related' => '<h3>'.__('Related posts:','yarpp').'</h3><ol>',
535
- 'rss_after_related' => '</ol>',
536
- 'rss_no_results' => '<p>'.__('No related posts.','yarpp').'</p>',
537
- 'rss_order' => 'score DESC',
538
- 'title' => '2',
539
- 'body' => '2',
540
- 'categories' => '1', // changed default in 3.3
541
- 'tags' => '2',
542
- 'distags' => '',
543
- 'discats' => '',
544
- 'past_only' => false,
545
- 'show_excerpt' => false,
546
- 'recent_only' => false, // new in 3.0
547
- 'use_template' => false, // new in 2.2
548
- 'rss_show_excerpt' => false,
549
- 'rss_use_template' => false, // new in 2.2
550
- 'show_pass_post' => false,
551
- 'cross_relate' => false,
552
- 'auto_display' => true,
553
- 'rss_display' => false, // changed default in 3.1.7
554
- 'rss_excerpt_display' => true,
555
- 'promote_yarpp' => false,
556
- 'rss_promote_yarpp' => false);
557
-
558
- $yarpp_options = array();
559
- foreach ( $yarpp_3_3_options as $key => $default ) {
560
- $value = get_option( "yarpp_$key", null );
561
- if ( is_null($value) )
562
- continue;
563
-
564
- if ( is_bool($default) ) {
565
- $yarpp_options[$key] = (boolean) $value;
566
- continue;
567
- }
568
-
569
- // value options used to be stored with a bajillion slashes...
570
- $value = stripslashes(stripslashes($value));
571
- // value options used to be stored with a blank space at the end... don't ask.
572
- $value = rtrim($value, ' ');
573
-
574
- if ( is_int($default) )
575
- $yarpp_options[$key] = absint($value);
576
- else
577
- $yarpp_options[$key] = $value;
578
- }
579
-
580
- // add the options directly first, then call set_option which will ensure defaults,
581
- // in case any new options have been added.
582
- update_option( 'yarpp', $yarpp_options );
583
- $this->set_option( $yarpp_options );
584
-
585
- $option_keys = array_keys( $yarpp_options );
586
- // append some keys for options which are long deprecated:
587
- $option_keys[] = 'ad_hoc_caching';
588
- $option_keys[] = 'excerpt_len';
589
- $option_keys[] = 'show_score';
590
- if ( count($option_keys) ) {
591
- $in = "('yarpp_" . join("', 'yarpp_", $option_keys) . "')";
592
- $wpdb->query("delete from {$wpdb->options} where option_name in {$in}");
593
- }
594
- }
595
-
596
- function upgrade_3_4b5() {
597
- $options = $this->get_option();
598
- $options['exclude'] = array(
599
- 'post_tag' => $options['distags'],
600
- 'category' => $options['discats']
601
- );
602
- unset( $options['distags'] );
603
- unset( $options['discats'] );
604
- update_option( 'yarpp', $options );
605
- }
606
-
607
- function upgrade_3_4b8() {
608
- $options = $this->get_option();
609
- $options['weight'] = array(
610
- 'title' => (int) @$options['title'],
611
- 'body' => (int) @$options['body'],
612
- 'tax' => array(
613
- 'post_tag' => (int) @$options['tags'],
614
- 'category' => (int) @$options['categories'],
615
- )
616
- );
617
-
618
- // ensure that we consider something
619
- if ( $options['weight']['title'] < 2 &&
620
- $options['weight']['body'] < 2 &&
621
- $options['weight']['tax']['post_tag'] < 2 &&
622
- $options['weight']['tax']['category'] < 2 )
623
- $options['weight'] = $this->default_options['weight'];
624
-
625
- unset( $options['title'] );
626
- unset( $options['body'] );
627
- unset( $options['tags'] );
628
- unset( $options['categories'] );
629
- update_option( 'yarpp', $options );
630
- }
631
-
632
- function upgrade_3_4_4b2() {
633
- $options = $this->get_option();
634
-
635
- // update weight values; split out tax weights into weight[tax] and require_tax
636
- $weight_map = array( 2 => 1, 3 => YARPP_EXTRA_WEIGHT );
637
- if ((int) $options['weight']['title'] == 1)
638
- unset( $options['weight']['title'] );
639
- else
640
- $options['weight']['title'] = $weight_map[(int) $options['weight']['title']];
641
-
642
- if ((int) $options['weight']['body'] == 1)
643
- unset( $options['weight']['body'] );
644
- else
645
- $options['weight']['body'] = $weight_map[(int) $options['weight']['body']];
646
-
647
- $options['require_tax'] = array();
648
- foreach ( $options['weight']['tax'] as $tax => $value ) {
649
- if ( $value == 3 )
650
- $options['require_tax'][$tax] = 1;
651
- if ( $value == 4 )
652
- $options['require_tax'][$tax] = 2;
653
-
654
- if ( $value > 1 )
655
- $options['weight']['tax'][$tax] = 1;
656
- else
657
- unset( $options['weight']['tax'][$tax] );
658
- }
659
-
660
- // consolidate excludes, using tt_ids.
661
- $exclude_tt_ids = array();
662
- if ( isset($options['exclude']) && is_array($options['exclude']) ) {
663
- foreach ($options['exclude'] as $tax => $term_ids) {
664
- if ( !empty($term_ids) )
665
- $exclude_tt_ids = array_merge( wp_list_pluck(get_terms( $tax, array('include' => $term_ids) ), 'term_taxonomy_id'), $exclude_tt_ids );
666
- }
667
- }
668
- $options['exclude'] = join(',', $exclude_tt_ids);
669
-
670
- update_option( 'yarpp', $options );
671
- }
672
-
673
- function upgrade_3_4_4b3() {
674
- $options = $this->get_option();
675
- $options['template'] = $options['use_template'] ? $options['template_file'] : false;
676
- $options['rss_template'] = $options['rss_use_template'] ? $options['rss_template_file'] : false;
677
- unset( $options['use_template'] );
678
- unset( $options['template_file'] );
679
- unset( $options['rss_use_template'] );
680
- unset( $options['rss_template_file'] );
681
- update_option( 'yarpp', $options );
682
- }
683
-
684
- function upgrade_3_4_4b4() {
685
- $options = $this->get_option();
686
- $options['recent'] = $options['recent_only'] ?
687
- $options['recent_number'] . ' ' . $options['recent_units'] : false;
688
- unset( $options['recent_only'] );
689
- unset( $options['recent_number'] );
690
- unset( $options['recent_units'] );
691
- update_option( 'yarpp', $options );
692
- }
693
-
694
- function upgrade_3_5_2b2() {
695
- // fixing the effects of a previous bug affecting non-MyISAM users
696
- if ( is_null( $this->get_option('weight') ) ||
697
- !is_array( $this->get_option('weight') ) ) {
698
- $weight = $this->default_options['weight'];
699
- // if we're still not using MyISAM
700
- if ( !$this->get_option('myisam_override') &&
701
- $this->diagnostic_myisam_posts() !== true ) {
702
- unset( $weight['title'] );
703
- unset( $weight['body'] );
704
- }
705
- $this->set_option(array('weight' => $weight));
706
- }
707
- }
708
-
709
- function upgrade_3_6b7() {
710
- // migrate auto_display setting to auto_display_post_types
711
- $options = $this->get_option();
712
- $options['auto_display_post_types'] = $options['auto_display'] ?
713
- array( 'post' ) : array();
714
- unset( $options['auto_display'] );
715
- update_option( 'yarpp', $options );
716
- }
717
-
718
- function upgrade_4_0_1() {
719
- delete_transient('yarpp_version_info');
720
- }
721
-
722
- /*
723
- * UTILITIES
724
- */
725
-
726
- private $current_post;
727
- private $current_query;
728
- private $current_pagenow;
729
- // so we can return to normal later
730
- function save_post_context() {
731
- global $wp_query, $pagenow, $post;
732
- $this->current_query = $wp_query;
733
- $this->current_pagenow = $pagenow;
734
- $this->current_post = $post;
735
- }
736
- function restore_post_context() {
737
- global $wp_query, $pagenow, $post;
738
- $wp_query = $this->current_query; unset($this->current_query);
739
- $pagenow = $this->current_pagenow; unset($this->current_pagenow);
740
- if ( isset($this->current_post) ) {
741
- $post = $this->current_post;
742
- setup_postdata( $post );
743
- unset($this->current_post);
744
- }
745
- }
746
-
747
- private $post_types = null;
748
- function get_post_types( $field = 'name' ) {
749
- if ( is_null($this->post_types) ) {
750
- $this->post_types = get_post_types(array(), 'objects');
751
- $this->post_types = array_filter( $this->post_types, array($this, 'post_type_filter') );
752
- }
753
-
754
- if ( 'objects' == $field )
755
- return $this->post_types;
756
- return wp_list_pluck( $this->post_types, $field );
757
- }
758
-
759
- private function post_type_filter( $post_type ) {
760
- if ( $post_type->_builtin && $post_type->show_ui )
761
- return true;
762
- if ( isset($post_type->yarpp_support) )
763
- return $post_type->yarpp_support;
764
- return false;
765
- }
766
-
767
- private $taxonomies = null;
768
- function get_taxonomies( $field = false ) {
769
- if ( is_null($this->taxonomies) ) {
770
- $this->taxonomies = get_taxonomies(array(), 'objects');
771
- $this->taxonomies = array_filter( $this->taxonomies, array($this, 'taxonomy_filter') );
772
- }
773
-
774
- if ( $field )
775
- return wp_list_pluck( $this->taxonomies, $field );
776
- return $this->taxonomies;
777
- }
778
-
779
- private function taxonomy_filter( $taxonomy ) {
780
- if ( !count(array_intersect( $taxonomy->object_type, $this->get_post_types() )) )
781
- return false;
782
-
783
- // if yarpp_support is set, follow that; otherwise include if show_ui is true
784
- if ( isset($taxonomy->yarpp_support) )
785
- return $taxonomy->yarpp_support;
786
- return $taxonomy->show_ui;
787
- }
788
-
789
- public function optin_data() {
790
- global $wpdb;
791
-
792
- $comments = wp_count_comments();
793
- $users = count_users();
794
-
795
- $settings = $this->get_option();
796
-
797
- $collect = array_flip(array(
798
- 'threshold', 'limit', 'excerpt_length', 'recent', 'rss_limit',
799
- 'rss_excerpt_length', 'past_only', 'show_excerpt', 'rss_show_excerpt',
800
- 'template', 'rss_template', 'show_pass_post', 'cross_relate',
801
- 'rss_display', 'rss_excerpt_display', 'promote_yarpp', 'rss_promote_yarpp',
802
- 'myisam_override', 'weight', 'require_tax', 'auto_display_archive'
803
- ));
804
-
805
- $check_changed = array(
806
- 'before_title', 'after_title', 'before_post', 'after_post',
807
- 'after_related', 'no_results', 'order', 'rss_before_title',
808
- 'rss_after_title', 'rss_before_post', 'rss_after_post', 'rss_after_related',
809
- 'rss_no_results', 'rss_order', 'exclude', 'thumbnails_heading',
810
- 'thumbnails_default', 'rss_thumbnails_heading', 'rss_thumbnails_default', 'display_code'
811
- );
812
-
813
- $data = array(
814
- 'versions' => array(
815
- 'yarpp' => YARPP_VERSION,
816
- 'wp' => get_bloginfo( 'version' ),
817
- 'php' => phpversion()
818
- ),
819
- 'yarpp' => array(
820
- 'settings' => array_intersect_key( $settings, $collect ),
821
- 'cache_engine' => YARPP_CACHE_TYPE
822
- ),
823
- 'diagnostics' => array(
824
- 'myisam_posts' => $this->diagnostic_myisam_posts(),
825
- 'fulltext_disabled' => $this->diagnostic_fulltext_disabled(),
826
- 'fulltext_indices' => $this->diagnostic_fulltext_indices(),
827
- 'hidden_metaboxes' => $this->diagnostic_hidden_metaboxes(),
828
- 'post_thumbnails' => $this->diagnostic_post_thumbnails(),
829
- 'happy' => $this->diagnostic_happy(),
830
- 'using_thumbnails' => $this->diagnostic_using_thumbnails(),
831
- 'generate_thumbnails' => $this->diagnostic_generate_thumbnails(),
832
- ),
833
- 'stats' => array(
834
- 'counts' => array(),
835
- 'terms' => array(),
836
- 'comments' => array(
837
- 'moderated' => $comments->moderated,
838
- 'approved' => $comments->approved,
839
- 'total' => $comments->total_comments,
840
- 'posts' => $wpdb->get_var( "select count(ID) from $wpdb->posts where post_type = 'post' and comment_count > 0" )
841
- ),
842
- 'users' => $wpdb->get_var("select count(ID) from $wpdb->users"),
843
- ),
844
- 'locale' => get_bloginfo( 'language' ),
845
- 'url' => get_bloginfo('url'),
846
- 'plugins' => array(
847
- 'active' => implode( '|', get_option( 'active_plugins', array() ) ),
848
- 'sitewide' => implode( '|', array_keys( get_site_option( 'active_sitewide_plugins', array() ) ) )
849
- ),
850
- 'pools' => $settings['pools']
851
- );
852
- $data['yarpp']['settings']['auto_display_post_types'] = implode( '|', $settings['auto_display_post_types'] );
853
-
854
- $changed = array();
855
- foreach ( $check_changed as $key ) {
856
- if ( $this->default_options[$key] != $settings[$key] )
857
- $changed[] = $key;
858
- }
859
- foreach ( array( 'before_related', 'rss_before_related' ) as $key ) {
860
- if ( $settings[$key] != '<p>'.__('Related posts:','yarpp').'</p><ol>' &&
861
- $settings[$key] != $this->default_options[$key] )
862
- $changed[] = $key;
863
- }
864
- $data['yarpp']['changed_settings'] = implode( '|', $changed );
865
-
866
- if ( method_exists( $this->cache, 'cache_status' ) )
867
- $data['yarpp']['cache_status'] = $this->cache->cache_status();
868
- if ( method_exists( $this->cache, 'stats' ) ) {
869
- $stats = $this->cache->stats();
870
- $flattened = array();
871
- foreach ( $stats as $key => $value )
872
- $flattened[] = "$key:$value";
873
- $data['yarpp']['stats'] = implode( '|', $flattened );
874
- }
875
-
876
- if ( method_exists( $wpdb, 'db_version' ) )
877
- $data['versions']['mysql'] = preg_replace('/[^0-9.].*/', '', $wpdb->db_version());
878
-
879
- $counts = array();
880
- foreach (get_post_types( array('public' => true) ) as $post_type) {
881
- $counts[$post_type] = wp_count_posts($post_type);
882
- }
883
- $data['stats']['counts'] = wp_list_pluck($counts, 'publish');
884
-
885
- foreach (get_taxonomies( array('public' => true) ) as $taxonomy) {
886
- $data['stats']['terms'][$taxonomy] = wp_count_terms($taxonomy);
887
- }
888
-
889
- if ( is_multisite() ) {
890
- $data['multisite'] = array(
891
- 'url' => network_site_url(),
892
- 'users' => get_user_count(),
893
- 'sites' => get_blog_count()
894
- );
895
- }
896
-
897
- // $this->pretty_echo($data);
898
- return $data;
899
- }
900
-
901
- function pretty_echo( $data ) {
902
- echo "<pre>";
903
- $formatted = print_r($data, true);
904
- $formatted = str_replace(array('Array', '(', ')', "\n "), array('', '', '', "\n"), $formatted);
905
- echo preg_replace("/\n\s*\n/u", "\n", $formatted);
906
- echo "</pre>";
907
- }
908
-
909
- /*
910
- * CORE LOOKUP + DISPLAY FUNCTIONS
911
- */
912
-
913
- /* new in 2.1! the domain argument refers to {website,widget,rss} */
914
- /* new in 3.0! new query-based approach: EXTREMELY HACKY! */
915
- /*
916
- * @param (int) $reference_ID
917
- * @param (array) $args
918
- * @param (bool) $echo
919
- */
920
- function display_related($reference_ID = null, $args = array(), $echo = true) {
921
- // if we're already in a YARPP loop, stop now.
922
- if ( $this->cache->is_yarpp_time() || $this->cache_bypass->is_yarpp_time() )
923
- return false;
924
-
925
- $this->enforce();
926
-
927
- if ( is_numeric( $reference_ID ) )
928
- $reference_ID = (int) $reference_ID;
929
- else
930
- $reference_ID = get_the_ID();
931
-
932
- // @since 3.5.3: don't compute on revisions
933
- if ( $the_post = wp_is_post_revision($reference_ID) )
934
- $reference_ID = $the_post;
935
-
936
- $this->setup_active_cache( $args );
937
-
938
- $options = array( 'domain', 'limit', 'template', 'order', 'promote_yarpp', 'optin' );
939
- extract( $this->parse_args( $args, $options ) );
940
-
941
- $cache_status = $this->active_cache->enforce($reference_ID);
942
- // If cache status is YARPP_DONT_RUN, end here without returning or echoing anything.
943
- if ( YARPP_DONT_RUN == $cache_status )
944
- return;
945
-
946
- if ( YARPP_NO_RELATED == $cache_status ) {
947
- // There are no results, so no yarpp time for us... :'(
948
- } else {
949
- // Get ready for YARPP TIME!
950
- $this->active_cache->begin_yarpp_time($reference_ID, $args);
951
- }
952
-
953
- $this->save_post_context();
954
-
955
- global $wp_query;
956
- $wp_query = new WP_Query();
957
- if ( YARPP_NO_RELATED == $cache_status ) {
958
- // If there are no related posts, get no query
959
- } else {
960
- $orders = explode(' ',$order);
961
- $wp_query->query(array(
962
- 'p' => $reference_ID,
963
- 'orderby' => $orders[0],
964
- 'order' => $orders[1],
965
- 'showposts' => $limit,
966
- 'post_type' => ( isset($args['post_type']) ? $args['post_type'] : $this->get_post_types() )
967
- ));
968
- }
969
- $this->prep_query( $this->current_query->is_feed );
970
-
971
- $wp_query->posts = apply_filters('yarpp_results', $wp_query->posts, array(
972
- 'function' => 'display_related',
973
- 'args' => $args,
974
- 'related_ID' => $reference_ID));
975
-
976
- $related_query = $wp_query; // backwards compatibility
977
- $related_count = $related_query->post_count;
978
-
979
- $output = "<div class='";
980
- if ( 'website' == $domain )
981
- $output .= "yarpp-related";
982
- else
983
- $output .= "yarpp-related-{$domain}";
984
- if ( 1 > $related_count )
985
- $output .= " yarpp-related-none";
986
- $output .= "'>\n";
987
-
988
- if ( 'metabox' == $domain ) {
989
- include(YARPP_DIR . '/template-metabox.php');
990
- } elseif ( !!$template && 'thumbnails' == $template ) {
991
- include(YARPP_DIR . '/template-thumbnails.php');
992
- } elseif ( !!$template && file_exists(STYLESHEETPATH . '/' . $template) ) {
993
- global $post;
994
- ob_start();
995
- include(STYLESHEETPATH . '/' . $template);
996
- $output .= ob_get_contents();
997
- ob_end_clean();
998
- } elseif ( 'widget' == $domain ) {
999
- include(YARPP_DIR . '/template-widget.php');
1000
- } else {
1001
- include(YARPP_DIR . '/template-builtin.php');
1002
- }
1003
- $output = trim($output) . "\n";
1004
-
1005
- if ( YARPP_NO_RELATED == $cache_status ) {
1006
- // Uh, do nothing. Stay very still.
1007
- } else {
1008
- $this->active_cache->end_yarpp_time(); // YARPP time is over... :(
1009
- }
1010
-
1011
- unset($related_query);
1012
- $this->restore_post_context();
1013
-
1014
- if ($related_count > 0 && $promote_yarpp && $domain != 'metabox') {
1015
- $output .=
1016
- "<p>".
1017
- sprintf(
1018
- __("Related posts brought to you by <a href='%s'>Yet Another Related Posts Plugin</a>.",'yarpp'),
1019
- 'http://yarpp.org'
1020
- ).
1021
- "</p>\n";
1022
- }
1023
-
1024
- if($optin){
1025
- $output .= '<img src="http://yarpp.org/pixels/'.md5(get_bloginfo('url')).'" alt="Yarpp"/>'."\n";
1026
- }
1027