WordPress Related Posts - Version 3.3

Version Description

  • Fixed compatibility issues with Zemanta Editorial Assistant
  • Fixed transparency for images on settings page
Download this release

Release Info

Developer zemanta-panco
Plugin Icon wp plugin WordPress Related Posts
Version 3.3
Comparing to
See all releases

Code changes from version 3.2 to 3.3

config.php CHANGED
@@ -1,664 +1,677 @@
1
- <?php
2
-
3
- define('WP_RP_STATIC_BASE_URL', 'http://wprp.zemanta.com/static/');
4
-
5
- define('WP_RP_STATIC_THEMES_PATH', 'static/themes/');
6
-
7
- define("WP_RP_DEFAULT_CUSTOM_CSS",
8
- ".related_post_title {
9
- }
10
- ul.related_post {
11
- }
12
- ul.related_post li {
13
- }
14
- ul.related_post li a {
15
- }
16
- ul.related_post li img {
17
- }");
18
-
19
- define('WP_RP_THUMBNAILS_NAME', 'wp_rp_thumbnail');
20
- define('WP_RP_THUMBNAILS_PROP_NAME', 'wp_rp_thumbnail_prop');
21
- define('WP_RP_THUMBNAILS_WIDTH', 150);
22
- define('WP_RP_THUMBNAILS_HEIGHT', 150);
23
- define('WP_RP_CUSTOM_THUMBNAILS_WIDTH', 150);
24
- define('WP_RP_CUSTOM_THUMBNAILS_HEIGHT', 150);
25
- define('WP_RP_THUMBNAILS_DEFAULTS_COUNT', 31);
26
-
27
- define("WP_RP_MAX_LABEL_LENGTH", 32);
28
-
29
- define("WP_RP_CTR_DASHBOARD_URL", "http://d.zemanta.com/");
30
- define("WP_RP_STATIC_LOADER_FILE", "js/loader.js");
31
-
32
- define("WP_RP_STATIC_INFINITE_RECS_JS_FILE", "js/infiniterecs.js");
33
- define("WP_RP_STATIC_PINTEREST_JS_FILE", "js/pinterest.js");
34
-
35
- define("WP_RP_RECOMMENDATIONS_AUTO_TAGS_MAX_WORDS", 200);
36
- define("WP_RP_RECOMMENDATIONS_AUTO_TAGS_MAX_TAGS", 15);
37
-
38
- define("WP_RP_RECOMMENDATIONS_AUTO_TAGS_SCORE", 2);
39
- define("WP_RP_RECOMMENDATIONS_TAGS_SCORE", 10);
40
- define("WP_RP_RECOMMENDATIONS_CATEGORIES_SCORE", 5);
41
-
42
- define("WP_RP_RECOMMENDATIONS_NUM_PREGENERATED_POSTS", 50);
43
-
44
- define("WP_RP_THUMBNAILS_NUM_PREGENERATED_POSTS", 50);
45
-
46
- global $wp_rp_options, $wp_rp_meta;
47
- $wp_rp_options = false;
48
- $wp_rp_meta = false;
49
-
50
- function wp_rp_get_options() {
51
- global $wp_rp_options, $wp_rp_meta;
52
- if($wp_rp_options) {
53
- return $wp_rp_options;
54
- }
55
-
56
- $wp_rp_options = get_option('wp_rp_options', false);
57
- $wp_rp_meta = get_option('wp_rp_meta', false);
58
-
59
- if(!$wp_rp_meta || !$wp_rp_options || $wp_rp_meta['version'] !== WP_RP_VERSION) {
60
- wp_rp_upgrade();
61
- $wp_rp_meta = get_option('wp_rp_meta');
62
- $wp_rp_options = get_option('wp_rp_options');
63
- }
64
-
65
- $wp_rp_meta = new ArrayObject($wp_rp_meta);
66
- $wp_rp_options = new ArrayObject($wp_rp_options);
67
-
68
- return $wp_rp_options;
69
- }
70
-
71
- function wp_rp_get_meta() {
72
- global $wp_rp_meta;
73
-
74
- if (!$wp_rp_meta) {
75
- wp_rp_get_options();
76
- }
77
-
78
- return $wp_rp_meta;
79
- }
80
-
81
- function wp_rp_update_meta($new_meta) {
82
- global $wp_rp_meta;
83
-
84
- $new_meta = (array) $new_meta;
85
-
86
- $r = update_option('wp_rp_meta', $new_meta);
87
-
88
- if($r && $wp_rp_meta !== false) {
89
- $wp_rp_meta->exchangeArray($new_meta);
90
- }
91
-
92
- return $r;
93
- }
94
-
95
- function wp_rp_update_options($new_options) {
96
- global $wp_rp_options;
97
-
98
- $new_options = (array) $new_options;
99
- $r = update_option('wp_rp_options', $new_options);
100
- if($r && $wp_rp_options !== false) {
101
- $wp_rp_options->exchangeArray($new_options);
102
- }
103
-
104
- return $r;
105
- }
106
-
107
- function wp_rp_activate_hook() {
108
- wp_rp_get_options();
109
- wp_rp_schedule_notifications_cron();
110
- }
111
-
112
- function wp_rp_deactivate_hook() {
113
- wp_rp_unschedule_notifications_cron();
114
- }
115
-
116
- function wp_rp_upgrade() {
117
- $wp_rp_meta = get_option('wp_rp_meta', false);
118
- $version = false;
119
-
120
- if($wp_rp_meta) {
121
- $version = $wp_rp_meta['version'];
122
- } else {
123
- $wp_rp_old_options = get_option('wp_rp', false);
124
- if($wp_rp_old_options) {
125
- $version = '1.4';
126
- }
127
- }
128
-
129
- if($version) {
130
- if(version_compare($version, WP_RP_VERSION, '<')) {
131
- call_user_func('wp_rp_migrate_' . str_replace('.', '_', $version));
132
- wp_rp_upgrade();
133
- }
134
- } else {
135
- wp_rp_install();
136
- }
137
- }
138
-
139
- function wp_rp_related_posts_db_table_uninstall() {
140
- global $wpdb;
141
-
142
- $tags_table_name = $wpdb->prefix . "wp_rp_tags";
143
-
144
- $sql = "DROP TABLE " . $tags_table_name;
145
-
146
- $wpdb->query($sql);
147
- }
148
-
149
- function wp_rp_related_posts_db_table_install() {
150
- global $wpdb;
151
-
152
- $tags_table_name = $wpdb->prefix . "wp_rp_tags";
153
- $sql_tags = "CREATE TABLE $tags_table_name (
154
- post_id mediumint(9),
155
- post_date datetime NOT NULL,
156
- label VARCHAR(" . WP_RP_MAX_LABEL_LENGTH . ") NOT NULL,
157
- weight float,
158
- INDEX post_id (post_id),
159
- INDEX label (label)
160
- );";
161
-
162
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
163
- dbDelta($sql_tags);
164
-
165
- $latest_posts = get_posts(array('numberposts' => WP_RP_RECOMMENDATIONS_NUM_PREGENERATED_POSTS));
166
- foreach ($latest_posts as $post) {
167
- wp_rp_generate_tags($post);
168
- }
169
- }
170
-
171
- function wp_rp_install() {
172
- $wp_rp_meta = array(
173
- 'blog_id' => false,
174
- 'auth_key' => false,
175
- 'version' => WP_RP_VERSION,
176
- 'first_version' => WP_RP_VERSION,
177
- 'new_user' => true,
178
- 'blog_tg' => rand(0, 1),
179
- 'remote_recommendations' => false,
180
- 'show_turn_on_button' => true,
181
- 'name' => '',
182
- 'email' => '',
183
- 'remote_notifications' => array(),
184
- 'turn_on_button_pressed' => false,
185
- 'show_statistics' => false,
186
- 'show_traffic_exchange' => false,
187
- 'show_zemanta_linky_option' => true,
188
- 'classic_user' => strpos(get_bloginfo('language'), 'en') === 0 // Enable only if "any" english is the default language
189
- );
190
-
191
- $wp_rp_options = array(
192
- 'related_posts_title' => __('More from my site', 'wp_related_posts'),
193
- 'max_related_posts' => 6,
194
- 'exclude_categories' => '',
195
- 'on_single_post' => true,
196
- 'on_rss' => false,
197
- 'max_related_post_age_in_days' => 0,
198
- 'default_thumbnail_path' => false,
199
- 'ctr_dashboard_enabled' => false,
200
- 'promoted_content_enabled' => false,
201
- 'enable_themes' => false,
202
- 'traffic_exchange_enabled' => false,
203
- 'custom_size_thumbnail_enabled' => false,
204
- 'custom_thumbnail_width' => WP_RP_CUSTOM_THUMBNAILS_WIDTH,
205
- 'custom_thumbnail_height' => WP_RP_CUSTOM_THUMBNAILS_HEIGHT,
206
- 'thumbnail_use_custom' => false,
207
- 'thumbnail_custom_field' => false,
208
- 'display_zemanta_linky' => false,
209
-
210
- 'mobile' => array(
211
- 'display_comment_count' => false,
212
- 'display_publish_date' => false,
213
- 'display_excerpt' => false,
214
- 'display_thumbnail' => false,
215
- 'excerpt_max_length' => 200,
216
- 'theme_name' => 'm-modern.css',
217
- 'theme_custom_css' => WP_RP_DEFAULT_CUSTOM_CSS,
218
- ),
219
- 'desktop' => array(
220
- 'display_comment_count' => false,
221
- 'display_publish_date' => false,
222
- 'display_thumbnail' => false,
223
- 'display_excerpt' => false,
224
- 'excerpt_max_length' => 200,
225
- 'theme_name' => 'vertical-m.css',
226
- 'theme_custom_css' => WP_RP_DEFAULT_CUSTOM_CSS,
227
- 'custom_theme_enabled' => false,
228
- )
229
- );
230
-
231
- update_option('wp_rp_meta', $wp_rp_meta);
232
- update_option('wp_rp_options', $wp_rp_options);
233
-
234
- wp_rp_related_posts_db_table_install();
235
- }
236
-
237
- function wp_rp_is_classic() {
238
- $meta = wp_rp_get_meta();
239
- if (isset($meta['classic_user']) && $meta['classic_user']) {
240
- return true;
241
- }
242
- return false;
243
- }
244
-
245
- function wp_rp_migrate_3_1() {
246
- global $wpdb;
247
-
248
- $wp_rp_meta = get_option('wp_rp_meta');
249
- $wp_rp_meta['version'] = '3.2';
250
- $wp_rp_meta['new_user'] = false;
251
- if (floatval($wp_rp_meta['first_version']) < 2.8 && strpos(get_bloginfo('language'), 'en') === 0) { // Enable widget to all "old" users out there (old = users that started with plugin version 2.7 or below), that have their interface in english.
252
- $wp_rp_meta['classic_user'] = true;
253
- }
254
- $wp_rp_options = get_option('wp_rp_options');
255
- $wp_rp_options['custom_size_thumbnail_enabled'] = false;
256
- $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
257
- $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
258
-
259
- update_option('wp_rp_meta', $wp_rp_meta);
260
- update_option('wp_rp_options', $wp_rp_options);
261
-
262
- }
263
-
264
-
265
- function wp_rp_migrate_3_0() {
266
- global $wpdb;
267
-
268
- $wp_rp_meta = get_option('wp_rp_meta');
269
- $wp_rp_meta['version'] = '3.1';
270
- $wp_rp_meta['new_user'] = false;
271
- if (floatval($wp_rp_meta['first_version']) < 2.8 && strpos(get_bloginfo('language'), 'en') === 0) { // Enable widget to all "old" users out there (old = users that started with plugin version 2.7 or below), that have their interface in english.
272
- $wp_rp_meta['classic_user'] = true;
273
- }
274
- $wp_rp_options = get_option('wp_rp_options');
275
- $wp_rp_options['custom_size_thumbnail_enabled'] = false;
276
- $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
277
- $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
278
-
279
-
280
- update_option('wp_rp_meta', $wp_rp_meta);
281
- update_option('wp_rp_options', $wp_rp_options);
282
- }
283
-
284
- function wp_rp_migrate_2_9() {
285
- global $wpdb;
286
-
287
- $wp_rp_meta = get_option('wp_rp_meta');
288
- $wp_rp_meta['version'] = '3.0';
289
- $wp_rp_meta['new_user'] = false;
290
- $wp_rp_options = get_option('wp_rp_options');
291
- $wp_rp_options['custom_size_thumbnail_enabled'] = false;
292
- $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
293
- $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
294
-
295
- update_option('wp_rp_meta', $wp_rp_meta);
296
- update_option('wp_rp_options', $wp_rp_options);
297
- }
298
-
299
- function wp_rp_migrate_2_8() {
300
- global $wpdb;
301
-
302
- $wp_rp_meta = get_option('wp_rp_meta');
303
- $wp_rp_meta['version'] = '2.9';
304
- $wp_rp_meta['new_user'] = false;
305
- $wp_rp_options = get_option('wp_rp_options');
306
- $wp_rp_options['custom_size_thumbnail_enabled'] = false;
307
- $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
308
- $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
309
-
310
- update_option('wp_rp_meta', $wp_rp_meta);
311
- update_option('wp_rp_options', $wp_rp_options);
312
- }
313
-
314
- function wp_rp_migrate_2_7() {
315
- global $wpdb;
316
-
317
- $wp_rp_meta = get_option('wp_rp_meta');
318
- $wp_rp_meta['version'] = '2.8';
319
- $wp_rp_meta['new_user'] = false;
320
- $wp_rp_meta['classic_user'] = false;
321
- $wp_rp_options = get_option('wp_rp_options');
322
- $wp_rp_options['custom_size_thumbnail_enabled'] = false;
323
- $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
324
- $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
325
-
326
- $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key IN ('_wp_rp_extracted_image_url', '_wp_rp_extracted_image_url_full')");
327
-
328
- update_option('wp_rp_meta', $wp_rp_meta);
329
- update_option('wp_rp_options', $wp_rp_options);
330
- }
331
-
332
- function wp_rp_migrate_2_6() {
333
- $wp_rp_meta = get_option('wp_rp_meta');
334
- $wp_rp_meta['version'] = '2.7';
335
- $wp_rp_meta['new_user'] = false;
336
- update_option('wp_rp_meta', $wp_rp_meta);
337
- }
338
-
339
- function wp_rp_migrate_2_5() {
340
- $wp_rp_meta = get_option('wp_rp_meta');
341
- $wp_rp_options = get_option('wp_rp_options');
342
-
343
- $wp_rp_meta['version'] = '2.6';
344
-
345
- if (!isset($wp_rp_meta['blog_tg'])) {
346
- $wp_rp_meta['blog_tg'] = rand(0, 1);
347
- }
348
-
349
- $wp_rp_meta['new_user'] = false;
350
-
351
- update_option('wp_rp_meta', $wp_rp_meta);
352
- update_option('wp_rp_options', $wp_rp_options);
353
- }
354
-
355
- function wp_rp_migrate_2_4_1() {
356
- $wp_rp_meta = get_option('wp_rp_meta');
357
- $wp_rp_options = get_option('wp_rp_options');
358
-
359
- $wp_rp_meta['version'] = '2.5';
360
-
361
- $wp_rp_meta['blog_tg'] = rand(0, 1);
362
-
363
- $display_options = array(
364
- 'display_comment_count' => $wp_rp_options['display_comment_count'],
365
- 'display_publish_date' => $wp_rp_options['display_publish_date'],
366
- 'display_thumbnail' => $wp_rp_options['display_thumbnail'],
367
- 'display_excerpt' => $wp_rp_options['display_excerpt'],
368
- 'excerpt_max_length' => $wp_rp_options['excerpt_max_length'],
369
- 'theme_name' => $wp_rp_options['theme_name'],
370
- 'theme_custom_css' => $wp_rp_options['theme_custom_css'],
371
- 'custom_theme_enabled' => $wp_rp_options['custom_theme_enabled']
372
- );
373
-
374
- $wp_rp_options['desktop'] = $display_options;
375
- $wp_rp_options['mobile'] = $display_options;
376
-
377
- if($wp_rp_options['mobile']['theme_name'] !== 'plain.css') {
378
- $wp_rp_options['mobile']['theme_name'] = 'm-modern.css';
379
- }
380
-
381
- unset($wp_rp_options['related_posts_title_tag']);
382
- unset($wp_rp_options['thumbnail_display_title']);
383
- unset($wp_rp_options['thumbnail_use_attached']);
384
- unset($wp_rp_options['display_comment_count']);
385
- unset($wp_rp_options['display_publish_date']);
386
- unset($wp_rp_options['display_thumbnail']);
387
- unset($wp_rp_options['display_excerpt']);
388
- unset($wp_rp_options['excerpt_max_length']);
389
- unset($wp_rp_options['theme_name']);
390
- unset($wp_rp_options['theme_custom_css']);
391
- unset($wp_rp_options['custom_theme_enabled']);
392
-
393
- $wp_rp_options['display_zemanta_linky'] = false;
394
- $wp_rp_meta['show_zemanta_linky_option'] = true;
395
-
396
- update_option('wp_rp_meta', $wp_rp_meta);
397
- update_option('wp_rp_options', $wp_rp_options);
398
- }
399
-
400
- function wp_rp_migrate_2_4() {
401
- $wp_rp_meta = get_option('wp_rp_meta');
402
- $wp_rp_options = get_option('wp_rp_options');
403
-
404
- $wp_rp_meta['version'] = '2.4.1';
405
-
406
- update_option('wp_rp_meta', $wp_rp_meta);
407
- update_option('wp_rp_options', $wp_rp_options);
408
- }
409
- function wp_rp_migrate_2_3() {
410
- $wp_rp_meta = get_option('wp_rp_meta');
411
- $wp_rp_options = get_option('wp_rp_options');
412
-
413
- $wp_rp_meta['version'] = '2.4';
414
-
415
- $wp_rp_options['max_related_post_age_in_days'] = 0;
416
-
417
- wp_rp_related_posts_db_table_uninstall();
418
- wp_rp_related_posts_db_table_install();
419
-
420
- update_option('wp_rp_meta', $wp_rp_meta);
421
- update_option('wp_rp_options', $wp_rp_options);
422
- }
423
-
424
- function wp_rp_migrate_2_2() {
425
- $wp_rp_meta = get_option('wp_rp_meta');
426
- $wp_rp_options = get_option('wp_rp_options');
427
-
428
- $wp_rp_meta['version'] = '2.3';
429
-
430
- if(isset($wp_rp_options['show_santa_hat'])) {
431
- unset($wp_rp_options['show_santa_hat']);
432
- }
433
- if(isset($wp_rp_options['show_RP_in_posts'])) {
434
- unset($wp_rp_options['show_RP_in_posts']);
435
- }
436
-
437
- update_option('wp_rp_meta', $wp_rp_meta);
438
- update_option('wp_rp_options', $wp_rp_options);
439
- }
440
-
441
- function wp_rp_migrate_2_1() {
442
- $wp_rp_meta = get_option('wp_rp_meta');
443
- $wp_rp_options = get_option('wp_rp_options');
444
-
445
- $wp_rp_meta['version'] = '2.2';
446
-
447
- $wp_rp_options['custom_theme_enabled'] = $wp_rp_options['theme_name'] == 'custom.css';
448
- if ($wp_rp_options['custom_theme_enabled']) {
449
- $wp_rp_options['theme_name'] = 'plain.css';
450
- }
451
-
452
- $wp_rp_options['show_RP_in_posts'] = false;
453
-
454
- $wp_rp_options['traffic_exchange_enabled'] = false;
455
- $wp_rp_meta['show_traffic_exchange'] = false;
456
-
457
- update_option('wp_rp_options', $wp_rp_options);
458
- update_option('wp_rp_meta', $wp_rp_meta);
459
- }
460
-
461
- function wp_rp_migrate_2_0() {
462
- $wp_rp_meta = get_option('wp_rp_meta');
463
- $wp_rp_options = get_option('wp_rp_options');
464
-
465
- $wp_rp_meta['version'] = '2.1';
466
-
467
- if ($wp_rp_options['default_thumbnail_path']) {
468
- $upload_dir = wp_upload_dir();
469
- $wp_rp_options['default_thumbnail_path'] = $upload_dir['baseurl'] . $wp_rp_options['default_thumbnail_path'];
470
- }
471
-
472
- update_option('wp_rp_options', $wp_rp_options);
473
- update_option('wp_rp_meta', $wp_rp_meta);
474
-
475
- if($wp_rp_options['display_thumbnail'] && $wp_rp_options['thumbnail_use_attached']) {
476
- wp_rp_process_latest_post_thumbnails();
477
- }
478
- }
479
-
480
- function wp_rp_migrate_1_7() {
481
- $wp_rp_meta = get_option('wp_rp_meta');
482
- $wp_rp_options = get_option('wp_rp_options');
483
-
484
- $wp_rp_meta['version'] = '2.0';
485
-
486
- $wp_rp_options['promoted_content_enabled'] = $wp_rp_options['ctr_dashboard_enabled'];
487
- $wp_rp_options['exclude_categories'] = $wp_rp_options['not_on_categories'];
488
-
489
- $wp_rp_meta['show_statistics'] = $wp_rp_options['ctr_dashboard_enabled'];
490
-
491
- // Commented out since we don't want to lose this info for users that will downgrade the plugin because of the change
492
- //unset($wp_rp_options['missing_rp_algorithm']);
493
- //unset($wp_rp_options['missing_rp_title']);
494
- //unset($wp_rp_options['not_on_categories']);
495
-
496
- // Forgot to unset this the last time.
497
- unset($wp_rp_meta['show_invite_friends_form']);
498
-
499
- update_option('wp_rp_options', $wp_rp_options);
500
- update_option('wp_rp_meta', $wp_rp_meta);
501
-
502
- wp_rp_schedule_notifications_cron();
503
- wp_rp_related_posts_db_table_install();
504
- }
505
-
506
- function wp_rp_migrate_1_6() {
507
- $wp_rp_meta = get_option('wp_rp_meta');
508
- $wp_rp_options = get_option('wp_rp_options');
509
-
510
- $wp_rp_meta['version'] = '1.7';
511
-
512
- unset($wp_rp_options['scroll_up_related_posts']);
513
- unset($wp_rp_options['include_promotionail_link']);
514
- unset($wp_rp_options['show_invite_friends_form']);
515
-
516
- $wp_rp_meta['show_blogger_network_form'] = false;
517
- $wp_rp_meta['remote_notifications'] = array();
518
-
519
- $wp_rp_meta['turn_on_button_pressed'] = false;
520
-
521
- update_option('wp_rp_options', $wp_rp_options);
522
- update_option('wp_rp_meta', $wp_rp_meta);
523
- }
524
-
525
- function wp_rp_migrate_1_5_2_1() { # This was a silent release, but WP_RP_VERSION was not properly updated, so we don't know exactly what happened...
526
- $wp_rp_meta = get_option('wp_rp_meta');
527
-
528
- $wp_rp_meta['version'] = '1.5.2';
529
-
530
- update_option('wp_rp_meta', $wp_rp_meta);
531
- }
532
-
533
- function wp_rp_migrate_1_5_2() {
534
- $wp_rp_meta = get_option('wp_rp_meta');
535
- $wp_rp_options = get_option('wp_rp_options');
536
-
537
- $wp_rp_meta['version'] = '1.6';
538
-
539
- $wp_rp_meta['show_install_tooltip'] = false;
540
- $wp_rp_meta['remote_recommendations'] = false;
541
- $wp_rp_meta['show_turn_on_button'] = !($wp_rp_options['ctr_dashboard_enabled'] && $wp_rp_options['display_thumbnail']);
542
- $wp_rp_meta['name'] = '';
543
- $wp_rp_meta['email'] = '';
544
- $wp_rp_meta['show_invite_friends_form'] = false;
545
-
546
- unset($wp_rp_meta['show_ctr_banner']);
547
- unset($wp_rp_meta['show_blogger_network']);
548
-
549
- $wp_rp_options['scroll_up_related_posts'] = false;
550
-
551
- update_option('wp_rp_meta', $wp_rp_meta);
552
- update_option('wp_rp_options', $wp_rp_options);
553
- }
554
- function wp_rp_migrate_1_5_1() {
555
- $wp_rp_options = get_option('wp_rp_options');
556
- $wp_rp_meta = get_option('wp_rp_meta');
557
-
558
- $wp_rp_options['enable_themes'] = true;
559
- $wp_rp_meta['version'] = '1.5.2';
560
-
561
- update_option('wp_rp_options', $wp_rp_options);
562
- update_option('wp_rp_meta', $wp_rp_meta);
563
- }
564
- function wp_rp_migrate_1_5() {
565
- $wp_rp_options = get_option('wp_rp_options');
566
- $wp_rp_meta = get_option('wp_rp_meta');
567
-
568
- $wp_rp_meta['show_blogger_network'] = false;
569
- $wp_rp_meta['version'] = '1.5.1';
570
-
571
- $wp_rp_options['include_promotionail_link'] = false;
572
- $wp_rp_options['ctr_dashboard_enabled'] = !!$wp_rp_options['ctr_dashboard_enabled'];
573
-
574
- update_option('wp_rp_options', $wp_rp_options);
575
- update_option('wp_rp_meta', $wp_rp_meta);
576
- }
577
-
578
- function wp_rp_migrate_1_4() {
579
- global $wpdb;
580
-
581
- $wp_rp = get_option('wp_rp');
582
-
583
- $wp_rp_options = array();
584
-
585
- ////////////////////////////////
586
-
587
- $wp_rp_options['missing_rp_algorithm'] = (isset($wp_rp['wp_no_rp']) && in_array($wp_rp['wp_no_rp'], array('text', 'random', 'commented', 'popularity'))) ? $wp_rp['wp_no_rp'] : 'random';
588
-
589
- if(isset($wp_rp['wp_no_rp_text']) && $wp_rp['wp_no_rp_text']) {
590
- $wp_rp_options['missing_rp_title'] = $wp_rp['wp_no_rp_text'];
591
- } else {
592
- if($wp_rp_options['missing_rp_algorithm'] === 'text') {
593
- $wp_rp_options['missing_rp_title'] = __('No Related Posts', 'wp_related_posts');
594
- } else {
595
- $wp_rp_options['missing_rp_title'] = __('Random Posts', 'wp_related_posts');
596
- }
597
- }
598
-
599
- $wp_rp_options['on_single_post'] = isset($wp_rp['wp_rp_auto']) ? !!$wp_rp['wp_rp_auto'] : true;
600
-
601
- $wp_rp_options['display_comment_count'] = isset($wp_rp['wp_rp_comments']) ? !!$wp_rp['wp_rp_comments'] : false;
602
-
603
- $wp_rp_options['display_publish_date'] = isset($wp_rp['wp_rp_date']) ? !!$wp_rp['wp_rp_date'] : false;
604
-
605
- $wp_rp_options['display_excerpt'] = isset($wp_rp['wp_rp_except']) ? !!$wp_rp['wp_rp_except'] : false;
606
-
607
- if(isset($wp_rp['wp_rp_except_number']) && is_numeric(trim($wp_rp['wp_rp_except_number']))) {
608
- $wp_rp_options['excerpt_max_length'] = intval(trim($wp_rp['wp_rp_except_number']));
609
- } else {
610
- $wp_rp_options['excerpt_max_length'] = 200;
611
- }
612
-
613
- $wp_rp_options['not_on_categories'] = isset($wp_rp['wp_rp_exclude']) ? $wp_rp['wp_rp_exclude'] : '';
614
-
615
- if(isset($wp_rp['wp_rp_limit']) && is_numeric(trim($wp_rp['wp_rp_limit']))) {
616
- $wp_rp_options['max_related_posts'] = intval(trim($wp_rp['wp_rp_limit']));
617
- } else {
618
- $wp_rp_options['max_related_posts'] = 5;
619
- }
620
-
621
- $wp_rp_options['on_rss'] = isset($wp_rp['wp_rp_rss']) ? !!$wp_rp['wp_rp_rss'] : false;
622
-
623
- $wp_rp_options['theme_name'] = isset($wp_rp['wp_rp_theme']) ? $wp_rp['wp_rp_theme'] : 'plain.css';
624
-
625
- $wp_rp_options['display_thumbnail'] = isset($wp_rp['wp_rp_thumbnail']) ? !!$wp_rp['wp_rp_thumbnail'] : false;
626
-
627
- $custom_fields = $wpdb->get_col("SELECT meta_key FROM $wpdb->postmeta GROUP BY meta_key HAVING meta_key NOT LIKE '\_%' ORDER BY LOWER(meta_key)");
628
- if(isset($wp_rp['wp_rp_thumbnail_post_meta']) && in_array($wp_rp['wp_rp_thumbnail_post_meta'], $custom_fields)) {
629
- $wp_rp_options['thumbnail_custom_field'] = $wp_rp['wp_rp_thumbnail_post_meta'];
630
- } else {
631
- $wp_rp_options['thumbnail_custom_field'] = false;
632
- }
633
-
634
- $wp_rp_options['thumbnail_display_title'] = isset($wp_rp['wp_rp_thumbnail_text']) ? !!$wp_rp['wp_rp_thumbnail_text'] : false;
635
-
636
- $wp_rp_options['related_posts_title'] = isset($wp_rp['wp_rp_title']) ? $wp_rp['wp_rp_title'] : '';
637
-
638
- $wp_rp_options['related_posts_title_tag'] = isset($wp_rp['wp_rp_title_tag']) ? $wp_rp['wp_rp_title_tag'] : 'h3';
639
-
640
- $wp_rp_options['default_thumbnail_path'] = (isset($wp_rp['wp_rp_default_thumbnail_path']) && $wp_rp['wp_rp_default_thumbnail_path']) ? $wp_rp['wp_rp_default_thumbnail_path'] : false;
641
-
642
- $wp_rp_options['thumbnail_use_attached'] = isset($wp_rp["wp_rp_thumbnail_extract"]) && ($wp_rp["wp_rp_thumbnail_extract"] === 'yes');
643
-
644
- $wp_rp_options['thumbnail_use_custom'] = $wp_rp_options['thumbnail_custom_field'] && !(isset($wp_rp['wp_rp_thumbnail_featured']) && $wp_rp['wp_rp_thumbnail_featured'] === 'yes');
645
-
646
- $wp_rp_options['theme_custom_css'] = WP_RP_DEFAULT_CUSTOM_CSS;
647
-
648
- $wp_rp_options['ctr_dashboard_enabled'] = false;
649
-
650
- ////////////////////////////////
651
-
652
- $wp_rp_meta = array(
653
- 'blog_id' => false,
654
- 'auth_key' => false,
655
- 'version' => '1.5',
656
- 'first_version' => '1.4',
657
- 'new_user' => false,
658
- 'show_upgrade_tooltip' => true,
659
- 'show_ctr_banner' => true
660
- );
661
-
662
- update_option('wp_rp_meta', $wp_rp_meta);
663
- update_option('wp_rp_options', $wp_rp_options);
664
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ define('WP_RP_STATIC_BASE_URL', 'http://wprp.zemanta.com/static/');
4
+
5
+ define('WP_RP_STATIC_THEMES_PATH', 'static/themes/');
6
+
7
+ define("WP_RP_DEFAULT_CUSTOM_CSS",
8
+ ".related_post_title {
9
+ }
10
+ ul.related_post {
11
+ }
12
+ ul.related_post li {
13
+ }
14
+ ul.related_post li a {
15
+ }
16
+ ul.related_post li img {
17
+ }");
18
+
19
+ define('WP_RP_THUMBNAILS_NAME', 'wp_rp_thumbnail');
20
+ define('WP_RP_THUMBNAILS_PROP_NAME', 'wp_rp_thumbnail_prop');
21
+ define('WP_RP_THUMBNAILS_WIDTH', 150);
22
+ define('WP_RP_THUMBNAILS_HEIGHT', 150);
23
+ define('WP_RP_CUSTOM_THUMBNAILS_WIDTH', 150);
24
+ define('WP_RP_CUSTOM_THUMBNAILS_HEIGHT', 150);
25
+ define('WP_RP_THUMBNAILS_DEFAULTS_COUNT', 31);
26
+
27
+ define("WP_RP_MAX_LABEL_LENGTH", 32);
28
+
29
+ define("WP_RP_CTR_DASHBOARD_URL", "http://d.zemanta.com/");
30
+ define("WP_RP_STATIC_LOADER_FILE", "js/loader.js");
31
+
32
+ define("WP_RP_STATIC_INFINITE_RECS_JS_FILE", "js/infiniterecs.js");
33
+ define("WP_RP_STATIC_PINTEREST_JS_FILE", "js/pinterest.js");
34
+
35
+ define("WP_RP_RECOMMENDATIONS_AUTO_TAGS_MAX_WORDS", 200);
36
+ define("WP_RP_RECOMMENDATIONS_AUTO_TAGS_MAX_TAGS", 15);
37
+
38
+ define("WP_RP_RECOMMENDATIONS_AUTO_TAGS_SCORE", 2);
39
+ define("WP_RP_RECOMMENDATIONS_TAGS_SCORE", 10);
40
+ define("WP_RP_RECOMMENDATIONS_CATEGORIES_SCORE", 5);
41
+
42
+ define("WP_RP_RECOMMENDATIONS_NUM_PREGENERATED_POSTS", 50);
43
+
44
+ define("WP_RP_THUMBNAILS_NUM_PREGENERATED_POSTS", 50);
45
+
46
+ global $wp_rp_options, $wp_rp_meta;
47
+ $wp_rp_options = false;
48
+ $wp_rp_meta = false;
49
+
50
+ function wp_rp_get_options() {
51
+ global $wp_rp_options, $wp_rp_meta;
52
+ if($wp_rp_options) {
53
+ return $wp_rp_options;
54
+ }
55
+
56
+ $wp_rp_options = get_option('wp_rp_options', false);
57
+ $wp_rp_meta = get_option('wp_rp_meta', false);
58
+
59
+ if(!$wp_rp_meta || !$wp_rp_options || $wp_rp_meta['version'] !== WP_RP_VERSION) {
60
+ wp_rp_upgrade();
61
+ $wp_rp_meta = get_option('wp_rp_meta');
62
+ $wp_rp_options = get_option('wp_rp_options');
63
+ }
64
+
65
+ $wp_rp_meta = new ArrayObject($wp_rp_meta);
66
+ $wp_rp_options = new ArrayObject($wp_rp_options);
67
+
68
+ return $wp_rp_options;
69
+ }
70
+
71
+ function wp_rp_get_meta() {
72
+ global $wp_rp_meta;
73
+
74
+ if (!$wp_rp_meta) {
75
+ wp_rp_get_options();
76
+ }
77
+
78
+ return $wp_rp_meta;
79
+ }
80
+
81
+ function wp_rp_update_meta($new_meta) {
82
+ global $wp_rp_meta;
83
+
84
+ $new_meta = (array) $new_meta;
85
+
86
+ $r = update_option('wp_rp_meta', $new_meta);
87
+
88
+ if($r && $wp_rp_meta !== false) {
89
+ $wp_rp_meta->exchangeArray($new_meta);
90
+ }
91
+
92
+ return $r;
93
+ }
94
+
95
+ function wp_rp_update_options($new_options) {
96
+ global $wp_rp_options;
97
+
98
+ $new_options = (array) $new_options;
99
+ $r = update_option('wp_rp_options', $new_options);
100
+ if($r && $wp_rp_options !== false) {
101
+ $wp_rp_options->exchangeArray($new_options);
102
+ }
103
+
104
+ return $r;
105
+ }
106
+
107
+ function wp_rp_activate_hook() {
108
+ wp_rp_get_options();
109
+ wp_rp_schedule_notifications_cron();
110
+ }
111
+
112
+ function wp_rp_deactivate_hook() {
113
+ wp_rp_unschedule_notifications_cron();
114
+ }
115
+
116
+ function wp_rp_upgrade() {
117
+ $wp_rp_meta = get_option('wp_rp_meta', false);
118
+ $version = false;
119
+
120
+ if($wp_rp_meta) {
121
+ $version = $wp_rp_meta['version'];
122
+ } else {
123
+ $wp_rp_old_options = get_option('wp_rp', false);
124
+ if($wp_rp_old_options) {
125
+ $version = '1.4';
126
+ }
127
+ }
128
+
129
+ if($version) {
130
+ if(version_compare($version, WP_RP_VERSION, '<')) {
131
+ call_user_func('wp_rp_migrate_' . str_replace('.', '_', $version));
132
+ wp_rp_upgrade();
133
+ }
134
+ } else {
135
+ wp_rp_install();
136
+ }
137
+ }
138
+
139
+ function wp_rp_related_posts_db_table_uninstall() {
140
+ global $wpdb;
141
+
142
+ $tags_table_name = $wpdb->prefix . "wp_rp_tags";
143
+
144
+ $sql = "DROP TABLE " . $tags_table_name;
145
+
146
+ $wpdb->query($sql);
147
+ }
148
+
149
+ function wp_rp_related_posts_db_table_install() {
150
+ global $wpdb;
151
+
152
+ $tags_table_name = $wpdb->prefix . "wp_rp_tags";
153
+ $sql_tags = "CREATE TABLE $tags_table_name (
154
+ post_id mediumint(9),
155
+ post_date datetime NOT NULL,
156
+ label VARCHAR(" . WP_RP_MAX_LABEL_LENGTH . ") NOT NULL,
157
+ weight float,
158
+ INDEX post_id (post_id),
159
+ INDEX label (label)
160
+ );";
161
+
162
+ require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
163
+ dbDelta($sql_tags);
164
+
165
+ $latest_posts = get_posts(array('numberposts' => WP_RP_RECOMMENDATIONS_NUM_PREGENERATED_POSTS));
166
+ foreach ($latest_posts as $post) {
167
+ wp_rp_generate_tags($post);
168
+ }
169
+ }
170
+
171
+ function wp_rp_install() {
172
+ $wp_rp_meta = array(
173
+ 'blog_id' => false,
174
+ 'auth_key' => false,
175
+ 'version' => WP_RP_VERSION,
176
+ 'first_version' => WP_RP_VERSION,
177
+ 'new_user' => true,
178
+ 'blog_tg' => rand(0, 1),
179
+ 'remote_recommendations' => false,
180
+ 'show_turn_on_button' => true,
181
+ 'name' => '',
182
+ 'email' => '',
183
+ 'remote_notifications' => array(),
184
+ 'turn_on_button_pressed' => false,
185
+ 'show_statistics' => false,
186
+ 'show_traffic_exchange' => false,
187
+ 'show_zemanta_linky_option' => true,
188
+ 'classic_user' => strpos(get_bloginfo('language'), 'en') === 0 // Enable only if "any" english is the default language
189
+ );
190
+
191
+ $wp_rp_options = array(
192
+ 'related_posts_title' => __('More from my site', 'wp_related_posts'),
193
+ 'max_related_posts' => 6,
194
+ 'exclude_categories' => '',
195
+ 'on_single_post' => true,
196
+ 'on_rss' => false,
197
+ 'max_related_post_age_in_days' => 0,
198
+ 'default_thumbnail_path' => false,
199
+ 'ctr_dashboard_enabled' => false,
200
+ 'promoted_content_enabled' => false,
201
+ 'enable_themes' => false,
202
+ 'traffic_exchange_enabled' => false,
203
+ 'custom_size_thumbnail_enabled' => false,
204
+ 'custom_thumbnail_width' => WP_RP_CUSTOM_THUMBNAILS_WIDTH,
205
+ 'custom_thumbnail_height' => WP_RP_CUSTOM_THUMBNAILS_HEIGHT,
206
+ 'thumbnail_use_custom' => false,
207
+ 'thumbnail_custom_field' => false,
208
+ 'display_zemanta_linky' => false,
209
+
210
+ 'mobile' => array(
211
+ 'display_comment_count' => false,
212
+ 'display_publish_date' => false,
213
+ 'display_excerpt' => false,
214
+ 'display_thumbnail' => false,
215
+ 'excerpt_max_length' => 200,
216
+ 'theme_name' => 'm-modern.css',
217
+ 'theme_custom_css' => WP_RP_DEFAULT_CUSTOM_CSS,
218
+ ),
219
+ 'desktop' => array(
220
+ 'display_comment_count' => false,
221
+ 'display_publish_date' => false,
222
+ 'display_thumbnail' => false,
223
+ 'display_excerpt' => false,
224
+ 'excerpt_max_length' => 200,
225
+ 'theme_name' => 'vertical-m.css',
226
+ 'theme_custom_css' => WP_RP_DEFAULT_CUSTOM_CSS,
227
+ 'custom_theme_enabled' => false,
228
+ )
229
+ );
230
+
231
+ update_option('wp_rp_meta', $wp_rp_meta);
232
+ update_option('wp_rp_options', $wp_rp_options);
233
+
234
+ wp_rp_related_posts_db_table_install();
235
+ }
236
+
237
+ function wp_rp_is_classic() {
238
+ $meta = wp_rp_get_meta();
239
+ if (isset($meta['classic_user']) && $meta['classic_user']) {
240
+ return true;
241
+ }
242
+ return false;
243
+ }
244
+
245
+ function wp_rp_migrate_3_2() {
246
+ global $wpdb;
247
+
248
+ $wp_rp_meta = get_option('wp_rp_meta');
249
+ $wp_rp_meta['version'] = '3.3';
250
+ $wp_rp_meta['new_user'] = false;
251
+ if (floatval($wp_rp_meta['first_version']) < 2.8 && strpos(get_bloginfo('language'), 'en') === 0) { // Enable widget to all "old" users out there (old = users that started with plugin version 2.7 or below), that have their interface in english.
252
+ $wp_rp_meta['classic_user'] = true;
253
+ }
254
+ update_option('wp_rp_meta', $wp_rp_meta);
255
+
256
+ }
257
+
258
+ function wp_rp_migrate_3_1() {
259
+ global $wpdb;
260
+
261
+ $wp_rp_meta = get_option('wp_rp_meta');
262
+ $wp_rp_meta['version'] = '3.2';
263
+ $wp_rp_meta['new_user'] = false;
264
+ if (floatval($wp_rp_meta['first_version']) < 2.8 && strpos(get_bloginfo('language'), 'en') === 0) { // Enable widget to all "old" users out there (old = users that started with plugin version 2.7 or below), that have their interface in english.
265
+ $wp_rp_meta['classic_user'] = true;
266
+ }
267
+ $wp_rp_options = get_option('wp_rp_options');
268
+ $wp_rp_options['custom_size_thumbnail_enabled'] = false;
269
+ $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
270
+ $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
271
+
272
+ update_option('wp_rp_meta', $wp_rp_meta);
273
+ update_option('wp_rp_options', $wp_rp_options);
274
+
275
+ }
276
+
277
+
278
+ function wp_rp_migrate_3_0() {
279
+ global $wpdb;
280
+
281
+ $wp_rp_meta = get_option('wp_rp_meta');
282
+ $wp_rp_meta['version'] = '3.1';
283
+ $wp_rp_meta['new_user'] = false;
284
+ if (floatval($wp_rp_meta['first_version']) < 2.8 && strpos(get_bloginfo('language'), 'en') === 0) { // Enable widget to all "old" users out there (old = users that started with plugin version 2.7 or below), that have their interface in english.
285
+ $wp_rp_meta['classic_user'] = true;
286
+ }
287
+ $wp_rp_options = get_option('wp_rp_options');
288
+ $wp_rp_options['custom_size_thumbnail_enabled'] = false;
289
+ $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
290
+ $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
291
+
292
+
293
+ update_option('wp_rp_meta', $wp_rp_meta);
294
+ update_option('wp_rp_options', $wp_rp_options);
295
+ }
296
+
297
+ function wp_rp_migrate_2_9() {
298
+ global $wpdb;
299
+
300
+ $wp_rp_meta = get_option('wp_rp_meta');
301
+ $wp_rp_meta['version'] = '3.0';
302
+ $wp_rp_meta['new_user'] = false;
303
+ $wp_rp_options = get_option('wp_rp_options');
304
+ $wp_rp_options['custom_size_thumbnail_enabled'] = false;
305
+ $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
306
+ $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
307
+
308
+ update_option('wp_rp_meta', $wp_rp_meta);
309
+ update_option('wp_rp_options', $wp_rp_options);
310
+ }
311
+
312
+ function wp_rp_migrate_2_8() {
313
+ global $wpdb;
314
+
315
+ $wp_rp_meta = get_option('wp_rp_meta');
316
+ $wp_rp_meta['version'] = '2.9';
317
+ $wp_rp_meta['new_user'] = false;
318
+ $wp_rp_options = get_option('wp_rp_options');
319
+ $wp_rp_options['custom_size_thumbnail_enabled'] = false;
320
+ $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
321
+ $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
322
+
323
+ update_option('wp_rp_meta', $wp_rp_meta);
324
+ update_option('wp_rp_options', $wp_rp_options);
325
+ }
326
+
327
+ function wp_rp_migrate_2_7() {
328
+ global $wpdb;
329
+
330
+ $wp_rp_meta = get_option('wp_rp_meta');
331
+ $wp_rp_meta['version'] = '2.8';
332
+ $wp_rp_meta['new_user'] = false;
333
+ $wp_rp_meta['classic_user'] = false;
334
+ $wp_rp_options = get_option('wp_rp_options');
335
+ $wp_rp_options['custom_size_thumbnail_enabled'] = false;
336
+ $wp_rp_options['custom_thumbnail_width'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
337
+ $wp_rp_options['custom_thumbnail_height'] = WP_RP_CUSTOM_THUMBNAILS_WIDTH;
338
+
339
+ $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key IN ('_wp_rp_extracted_image_url', '_wp_rp_extracted_image_url_full')");
340
+
341
+ update_option('wp_rp_meta', $wp_rp_meta);
342
+ update_option('wp_rp_options', $wp_rp_options);
343
+ }
344
+
345
+ function wp_rp_migrate_2_6() {
346
+ $wp_rp_meta = get_option('wp_rp_meta');
347
+ $wp_rp_meta['version'] = '2.7';
348
+ $wp_rp_meta['new_user'] = false;
349
+ update_option('wp_rp_meta', $wp_rp_meta);
350
+ }
351
+
352
+ function wp_rp_migrate_2_5() {
353
+ $wp_rp_meta = get_option('wp_rp_meta');
354
+ $wp_rp_options = get_option('wp_rp_options');
355
+
356
+ $wp_rp_meta['version'] = '2.6';
357
+
358
+ if (!isset($wp_rp_meta['blog_tg'])) {
359
+ $wp_rp_meta['blog_tg'] = rand(0, 1);
360
+ }
361
+
362
+ $wp_rp_meta['new_user'] = false;
363
+
364
+ update_option('wp_rp_meta', $wp_rp_meta);
365
+ update_option('wp_rp_options', $wp_rp_options);
366
+ }
367
+
368
+ function wp_rp_migrate_2_4_1() {
369
+ $wp_rp_meta = get_option('wp_rp_meta');
370
+ $wp_rp_options = get_option('wp_rp_options');
371
+
372
+ $wp_rp_meta['version'] = '2.5';
373
+
374
+ $wp_rp_meta['blog_tg'] = rand(0, 1);
375
+
376
+ $display_options = array(
377
+ 'display_comment_count' => $wp_rp_options['display_comment_count'],
378
+ 'display_publish_date' => $wp_rp_options['display_publish_date'],
379
+ 'display_thumbnail' => $wp_rp_options['display_thumbnail'],
380
+ 'display_excerpt' => $wp_rp_options['display_excerpt'],
381
+ 'excerpt_max_length' => $wp_rp_options['excerpt_max_length'],
382
+ 'theme_name' => $wp_rp_options['theme_name'],
383
+ 'theme_custom_css' => $wp_rp_options['theme_custom_css'],
384
+ 'custom_theme_enabled' => $wp_rp_options['custom_theme_enabled']
385
+ );
386
+
387
+ $wp_rp_options['desktop'] = $display_options;
388
+ $wp_rp_options['mobile'] = $display_options;
389
+
390
+ if($wp_rp_options['mobile']['theme_name'] !== 'plain.css') {
391
+ $wp_rp_options['mobile']['theme_name'] = 'm-modern.css';
392
+ }
393
+
394
+ unset($wp_rp_options['related_posts_title_tag']);
395
+ unset($wp_rp_options['thumbnail_display_title']);
396
+ unset($wp_rp_options['thumbnail_use_attached']);
397
+ unset($wp_rp_options['display_comment_count']);
398
+ unset($wp_rp_options['display_publish_date']);
399
+ unset($wp_rp_options['display_thumbnail']);
400
+ unset($wp_rp_options['display_excerpt']);
401
+ unset($wp_rp_options['excerpt_max_length']);
402
+ unset($wp_rp_options['theme_name']);
403
+ unset($wp_rp_options['theme_custom_css']);
404
+ unset($wp_rp_options['custom_theme_enabled']);
405
+
406
+ $wp_rp_options['display_zemanta_linky'] = false;
407
+ $wp_rp_meta['show_zemanta_linky_option'] = true;
408
+
409
+ update_option('wp_rp_meta', $wp_rp_meta);
410
+ update_option('wp_rp_options', $wp_rp_options);
411
+ }
412
+
413
+ function wp_rp_migrate_2_4() {
414
+ $wp_rp_meta = get_option('wp_rp_meta');
415
+ $wp_rp_options = get_option('wp_rp_options');
416
+
417
+ $wp_rp_meta['version'] = '2.4.1';
418
+
419
+ update_option('wp_rp_meta', $wp_rp_meta);
420
+ update_option('wp_rp_options', $wp_rp_options);
421
+ }
422
+ function wp_rp_migrate_2_3() {
423
+ $wp_rp_meta = get_option('wp_rp_meta');
424
+ $wp_rp_options = get_option('wp_rp_options');
425
+
426
+ $wp_rp_meta['version'] = '2.4';
427
+
428
+ $wp_rp_options['max_related_post_age_in_days'] = 0;
429
+
430
+ wp_rp_related_posts_db_table_uninstall();
431
+ wp_rp_related_posts_db_table_install();
432
+
433
+ update_option('wp_rp_meta', $wp_rp_meta);
434
+ update_option('wp_rp_options', $wp_rp_options);
435
+ }
436
+
437
+ function wp_rp_migrate_2_2() {
438
+ $wp_rp_meta = get_option('wp_rp_meta');
439
+ $wp_rp_options = get_option('wp_rp_options');
440
+
441
+ $wp_rp_meta['version'] = '2.3';
442
+
443
+ if(isset($wp_rp_options['show_santa_hat'])) {
444
+ unset($wp_rp_options['show_santa_hat']);
445
+ }
446
+ if(isset($wp_rp_options['show_RP_in_posts'])) {
447
+ unset($wp_rp_options['show_RP_in_posts']);
448
+ }
449
+
450
+ update_option('wp_rp_meta', $wp_rp_meta);
451
+ update_option('wp_rp_options', $wp_rp_options);
452
+ }
453
+
454
+ function wp_rp_migrate_2_1() {
455
+ $wp_rp_meta = get_option('wp_rp_meta');
456
+ $wp_rp_options = get_option('wp_rp_options');
457
+
458
+ $wp_rp_meta['version'] = '2.2';
459
+
460
+ $wp_rp_options['custom_theme_enabled'] = $wp_rp_options['theme_name'] == 'custom.css';
461
+ if ($wp_rp_options['custom_theme_enabled']) {
462
+ $wp_rp_options['theme_name'] = 'plain.css';
463
+ }
464
+
465
+ $wp_rp_options['show_RP_in_posts'] = false;
466
+
467
+ $wp_rp_options['traffic_exchange_enabled'] = false;
468
+ $wp_rp_meta['show_traffic_exchange'] = false;
469
+
470
+ update_option('wp_rp_options', $wp_rp_options);
471
+ update_option('wp_rp_meta', $wp_rp_meta);
472
+ }
473
+
474
+ function wp_rp_migrate_2_0() {
475
+ $wp_rp_meta = get_option('wp_rp_meta');
476
+ $wp_rp_options = get_option('wp_rp_options');
477
+
478
+ $wp_rp_meta['version'] = '2.1';
479
+
480
+ if ($wp_rp_options['default_thumbnail_path']) {
481
+ $upload_dir = wp_upload_dir();
482
+ $wp_rp_options['default_thumbnail_path'] = $upload_dir['baseurl'] . $wp_rp_options['default_thumbnail_path'];
483
+ }
484
+
485
+ update_option('wp_rp_options', $wp_rp_options);
486
+ update_option('wp_rp_meta', $wp_rp_meta);
487
+
488
+ if($wp_rp_options['display_thumbnail'] && $wp_rp_options['thumbnail_use_attached']) {
489
+ wp_rp_process_latest_post_thumbnails();
490
+ }
491
+ }
492
+
493
+ function wp_rp_migrate_1_7() {
494
+ $wp_rp_meta = get_option('wp_rp_meta');
495
+ $wp_rp_options = get_option('wp_rp_options');
496
+
497
+ $wp_rp_meta['version'] = '2.0';
498
+
499
+ $wp_rp_options['promoted_content_enabled'] = $wp_rp_options['ctr_dashboard_enabled'];
500
+ $wp_rp_options['exclude_categories'] = $wp_rp_options['not_on_categories'];
501
+
502
+ $wp_rp_meta['show_statistics'] = $wp_rp_options['ctr_dashboard_enabled'];
503
+
504
+ // Commented out since we don't want to lose this info for users that will downgrade the plugin because of the change
505
+ //unset($wp_rp_options['missing_rp_algorithm']);
506
+ //unset($wp_rp_options['missing_rp_title']);
507
+ //unset($wp_rp_options['not_on_categories']);
508
+
509
+ // Forgot to unset this the last time.
510
+ unset($wp_rp_meta['show_invite_friends_form']);
511
+
512
+ update_option('wp_rp_options', $wp_rp_options);
513
+ update_option('wp_rp_meta', $wp_rp_meta);
514
+
515
+ wp_rp_schedule_notifications_cron();
516
+ wp_rp_related_posts_db_table_install();
517
+ }
518
+
519
+ function wp_rp_migrate_1_6() {
520
+ $wp_rp_meta = get_option('wp_rp_meta');
521
+ $wp_rp_options = get_option('wp_rp_options');
522
+
523
+ $wp_rp_meta['version'] = '1.7';
524
+
525
+ unset($wp_rp_options['scroll_up_related_posts']);
526
+ unset($wp_rp_options['include_promotionail_link']);
527
+ unset($wp_rp_options['show_invite_friends_form']);
528
+
529
+ $wp_rp_meta['show_blogger_network_form'] = false;
530
+ $wp_rp_meta['remote_notifications'] = array();
531
+
532
+ $wp_rp_meta['turn_on_button_pressed'] = false;
533
+
534
+ update_option('wp_rp_options', $wp_rp_options);
535
+ update_option('wp_rp_meta', $wp_rp_meta);
536
+ }
537
+
538
+ function wp_rp_migrate_1_5_2_1() { # This was a silent release, but WP_RP_VERSION was not properly updated, so we don't know exactly what happened...
539
+ $wp_rp_meta = get_option('wp_rp_meta');
540
+
541
+ $wp_rp_meta['version'] = '1.5.2';
542
+
543
+ update_option('wp_rp_meta', $wp_rp_meta);
544
+ }
545
+
546
+ function wp_rp_migrate_1_5_2() {
547
+ $wp_rp_meta = get_option('wp_rp_meta');
548
+ $wp_rp_options = get_option('wp_rp_options');
549
+
550
+ $wp_rp_meta['version'] = '1.6';
551
+
552
+ $wp_rp_meta['show_install_tooltip'] = false;
553
+ $wp_rp_meta['remote_recommendations'] = false;
554
+ $wp_rp_meta['show_turn_on_button'] = !($wp_rp_options['ctr_dashboard_enabled'] && $wp_rp_options['display_thumbnail']);
555
+ $wp_rp_meta['name'] = '';
556
+ $wp_rp_meta['email'] = '';
557
+ $wp_rp_meta['show_invite_friends_form'] = false;
558
+
559
+ unset($wp_rp_meta['show_ctr_banner']);
560
+ unset($wp_rp_meta['show_blogger_network']);
561
+
562
+ $wp_rp_options['scroll_up_related_posts'] = false;
563
+
564
+ update_option('wp_rp_meta', $wp_rp_meta);
565
+ update_option('wp_rp_options', $wp_rp_options);
566
+ }
567
+ function wp_rp_migrate_1_5_1() {
568
+ $wp_rp_options = get_option('wp_rp_options');
569
+ $wp_rp_meta = get_option('wp_rp_meta');
570
+
571
+ $wp_rp_options['enable_themes'] = true;
572
+ $wp_rp_meta['version'] = '1.5.2';
573
+
574
+ update_option('wp_rp_options', $wp_rp_options);
575
+ update_option('wp_rp_meta', $wp_rp_meta);
576
+ }
577
+ function wp_rp_migrate_1_5() {
578
+ $wp_rp_options = get_option('wp_rp_options');
579
+ $wp_rp_meta = get_option('wp_rp_meta');
580
+
581
+ $wp_rp_meta['show_blogger_network'] = false;
582
+ $wp_rp_meta['version'] = '1.5.1';
583
+
584
+ $wp_rp_options['include_promotionail_link'] = false;
585
+ $wp_rp_options['ctr_dashboard_enabled'] = !!$wp_rp_options['ctr_dashboard_enabled'];
586
+
587
+ update_option('wp_rp_options', $wp_rp_options);
588
+ update_option('wp_rp_meta', $wp_rp_meta);
589
+ }
590
+
591
+ function wp_rp_migrate_1_4() {
592
+ global $wpdb;
593
+
594
+ $wp_rp = get_option('wp_rp');
595
+
596
+ $wp_rp_options = array();
597
+
598
+ ////////////////////////////////
599
+
600
+ $wp_rp_options['missing_rp_algorithm'] = (isset($wp_rp['wp_no_rp']) && in_array($wp_rp['wp_no_rp'], array('text', 'random', 'commented', 'popularity'))) ? $wp_rp['wp_no_rp'] : 'random';
601
+
602
+ if(isset($wp_rp['wp_no_rp_text']) && $wp_rp['wp_no_rp_text']) {
603
+ $wp_rp_options['missing_rp_title'] = $wp_rp['wp_no_rp_text'];
604
+ } else {
605
+ if($wp_rp_options['missing_rp_algorithm'] === 'text') {
606
+ $wp_rp_options['missing_rp_title'] = __('No Related Posts', 'wp_related_posts');
607
+ } else {
608
+ $wp_rp_options['missing_rp_title'] = __('Random Posts', 'wp_related_posts');
609
+ }
610
+ }
611
+
612
+ $wp_rp_options['on_single_post'] = isset($wp_rp['wp_rp_auto']) ? !!$wp_rp['wp_rp_auto'] : true;
613
+
614
+ $wp_rp_options['display_comment_count'] = isset($wp_rp['wp_rp_comments']) ? !!$wp_rp['wp_rp_comments'] : false;
615
+
616
+ $wp_rp_options['display_publish_date'] = isset($wp_rp['wp_rp_date']) ? !!$wp_rp['wp_rp_date'] : false;
617
+
618
+ $wp_rp_options['display_excerpt'] = isset($wp_rp['wp_rp_except']) ? !!$wp_rp['wp_rp_except'] : false;
619
+
620
+ if(isset($wp_rp['wp_rp_except_number']) && is_numeric(trim($wp_rp['wp_rp_except_number']))) {
621
+ $wp_rp_options['excerpt_max_length'] = intval(trim($wp_rp['wp_rp_except_number']));
622
+ } else {
623
+ $wp_rp_options['excerpt_max_length'] = 200;
624
+ }
625
+
626
+ $wp_rp_options['not_on_categories'] = isset($wp_rp['wp_rp_exclude']) ? $wp_rp['wp_rp_exclude'] : '';
627
+
628
+ if(isset($wp_rp['wp_rp_limit']) && is_numeric(trim($wp_rp['wp_rp_limit']))) {
629
+ $wp_rp_options['max_related_posts'] = intval(trim($wp_rp['wp_rp_limit']));
630
+ } else {
631
+ $wp_rp_options['max_related_posts'] = 5;
632
+ }
633
+
634
+ $wp_rp_options['on_rss'] = isset($wp_rp['wp_rp_rss']) ? !!$wp_rp['wp_rp_rss'] : false;
635
+
636
+ $wp_rp_options['theme_name'] = isset($wp_rp['wp_rp_theme']) ? $wp_rp['wp_rp_theme'] : 'plain.css';
637
+
638
+ $wp_rp_options['display_thumbnail'] = isset($wp_rp['wp_rp_thumbnail']) ? !!$wp_rp['wp_rp_thumbnail'] : false;
639
+
640
+ $custom_fields = $wpdb->get_col("SELECT meta_key FROM $wpdb->postmeta GROUP BY meta_key HAVING meta_key NOT LIKE '\_%' ORDER BY LOWER(meta_key)");
641
+ if(isset($wp_rp['wp_rp_thumbnail_post_meta']) && in_array($wp_rp['wp_rp_thumbnail_post_meta'], $custom_fields)) {
642
+ $wp_rp_options['thumbnail_custom_field'] = $wp_rp['wp_rp_thumbnail_post_meta'];
643
+ } else {
644
+ $wp_rp_options['thumbnail_custom_field'] = false;
645
+ }
646
+
647
+ $wp_rp_options['thumbnail_display_title'] = isset($wp_rp['wp_rp_thumbnail_text']) ? !!$wp_rp['wp_rp_thumbnail_text'] : false;
648
+
649
+ $wp_rp_options['related_posts_title'] = isset($wp_rp['wp_rp_title']) ? $wp_rp['wp_rp_title'] : '';
650
+
651
+ $wp_rp_options['related_posts_title_tag'] = isset($wp_rp['wp_rp_title_tag']) ? $wp_rp['wp_rp_title_tag'] : 'h3';
652
+
653
+ $wp_rp_options['default_thumbnail_path'] = (isset($wp_rp['wp_rp_default_thumbnail_path']) && $wp_rp['wp_rp_default_thumbnail_path']) ? $wp_rp['wp_rp_default_thumbnail_path'] : false;
654
+
655
+ $wp_rp_options['thumbnail_use_attached'] = isset($wp_rp["wp_rp_thumbnail_extract"]) && ($wp_rp["wp_rp_thumbnail_extract"] === 'yes');
656
+
657
+ $wp_rp_options['thumbnail_use_custom'] = $wp_rp_options['thumbnail_custom_field'] && !(isset($wp_rp['wp_rp_thumbnail_featured']) && $wp_rp['wp_rp_thumbnail_featured'] === 'yes');
658
+
659
+ $wp_rp_options['theme_custom_css'] = WP_RP_DEFAULT_CUSTOM_CSS;
660
+
661
+ $wp_rp_options['ctr_dashboard_enabled'] = false;
662
+
663
+ ////////////////////////////////
664
+
665
+ $wp_rp_meta = array(
666
+ 'blog_id' => false,
667
+ 'auth_key' => false,
668
+ 'version' => '1.5',
669
+ 'first_version' => '1.4',
670
+ 'new_user' => false,
671
+ 'show_upgrade_tooltip' => true,
672
+ 'show_ctr_banner' => true
673
+ );
674
+
675
+ update_option('wp_rp_meta', $wp_rp_meta);
676
+ update_option('wp_rp_options', $wp_rp_options);
677
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: related,posts,post,related posts,plugin
4
  License: GPLv2
5
  Requires at least: 3.3
6
  Tested up to: 3.6
7
- Stable tag: 3.2
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
@@ -81,6 +81,10 @@ Fix for security vulnerability. Upgrade immediately.
81
 
82
  == Changelog ==
83
 
 
 
 
 
84
  = 3.2 =
85
  * Bug fixes for "Edit related posts" feature
86
  * Changed URL for static content
4
  License: GPLv2
5
  Requires at least: 3.3
6
  Tested up to: 3.6
7
+ Stable tag: 3.3
8
 
9
  WordPress Related Posts generates a list of related posts with thumbnails and gives you click-through statistics.
10
 
81
 
82
  == Changelog ==
83
 
84
+ = 3.3 =
85
+ * Fixed compatibility issues with Zemanta Editorial Assistant
86
+ * Fixed transparency for images on settings page
87
+
88
  = 3.2 =
89
  * Bug fixes for "Edit related posts" feature
90
  * Changed URL for static content
settings.php CHANGED
@@ -502,7 +502,8 @@ function wp_rp_settings_page() {
502
  <table class="form-table">
503
  <tbody>
504
  <tr><td>
505
- <p>All themes are <strong>optimized</strong> for thumbnails of size 150x150px. They might not work well with custom size thumbnails.<br> Specify your custom css to override theme's style.</p>
 
506
  </td></tr>
507
  <tr><td>
508
  <label>
502
  <table class="form-table">
503
  <tbody>
504
  <tr><td>
505
+ Our themes were created with thumbnail size of 150x150px in mind.<br>
506
+ If you want to use custom sizes, override theme's CSS rules in the Custom CSS section under Theme Settings above.
507
  </td></tr>
508
  <tr><td>
509
  <label>
static/css/dashboard.css CHANGED
@@ -122,8 +122,8 @@ form.wp_rp_message_form a.dismiss {float: right;}
122
  #wp_rp_wrap .excluded-categories {width: 250px; height: 12em; overflow: auto; padding: 0 5px; border: 1px solid #dfdfdf;}
123
 
124
  /* collapsible CSS */
125
- #wp_rp_wrap .collapsible .collapse-handle {margin-top: 16px; margin-right: 16px; float: right; width: 30px; height: 20px; background: url('../img/up.jpg') 0% 0% no-repeat; text-indent: -9999px; overflow: hidden; outline: 0;}
126
- #wp_rp_wrap .collapsible.collapsed .collapse-handle {background-image: url('../img/down.jpg');}
127
  #wp_rp_wrap .collapsible .collapse-handle:after {clear:both;overflow:hidden;height:0;}
128
 
129
  #wp_rp_theme_options_wrap .wp_rp_settings_button {font-size: 1.5em;padding:0 0 0 20px;background: url('../img/arrow_right.png') left center no-repeat;background-position: 3px 3px;}
122
  #wp_rp_wrap .excluded-categories {width: 250px; height: 12em; overflow: auto; padding: 0 5px; border: 1px solid #dfdfdf;}
123
 
124
  /* collapsible CSS */
125
+ #wp_rp_wrap .collapsible .collapse-handle {margin-top: 16px; margin-right: 16px; float: right; width: 30px; height: 20px; background: url('../img/up.png') 0% 0% no-repeat; text-indent: -9999px; overflow: hidden; outline: 0;}
126
+ #wp_rp_wrap .collapsible.collapsed .collapse-handle {background-image: url('../img/down.png');}
127
  #wp_rp_wrap .collapsible .collapse-handle:after {clear:both;overflow:hidden;height:0;}
128
 
129
  #wp_rp_theme_options_wrap .wp_rp_settings_button {font-size: 1.5em;padding:0 0 0 20px;background: url('../img/arrow_right.png') left center no-repeat;background-position: 3px 3px;}
static/img/down.png ADDED
Binary file
static/img/turnonscreen.jpg CHANGED
Binary file
static/img/up.png ADDED
Binary file
wp_related_posts.php CHANGED
@@ -1,555 +1,555 @@
1
- <?php
2
- /*
3
- Plugin Name: WordPress Related Posts
4
- Version: 3.2
5
- Plugin URI: http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/
6
- Description: Quickly increase your readers' engagement with your posts by adding Related Posts in the footer of your content. Click on <a href="admin.php?page=wordpress-related-posts">Related Posts tab</a> to configure your settings.
7
- Author: Zemanta Ltd.
8
- Author URI: http://www.zemanta.com
9
- */
10
-
11
- define('WP_RP_VERSION', '3.2');
12
-
13
- define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
14
-
15
- include_once(dirname(__FILE__) . '/config.php');
16
- include_once(dirname(__FILE__) . '/lib/stemmer.php');
17
- include_once(dirname(__FILE__) . '/lib/mobile_detect.php');
18
-
19
- include_once(dirname(__FILE__) . '/admin_notices.php');
20
- include_once(dirname(__FILE__) . '/notifications.php');
21
- include_once(dirname(__FILE__) . '/widget.php');
22
- include_once(dirname(__FILE__) . '/thumbnailer.php');
23
- include_once(dirname(__FILE__) . '/settings.php');
24
- include_once(dirname(__FILE__) . '/recommendations.php');
25
- include_once(dirname(__FILE__) . '/dashboard_widget.php');
26
- include_once(dirname(__FILE__) . '/edit_related_posts.php');
27
- include_once(dirname(__FILE__) . '/compatibility.php');
28
-
29
- register_activation_hook(__FILE__, 'wp_rp_activate_hook');
30
- register_deactivation_hook(__FILE__, 'wp_rp_deactivate_hook');
31
-
32
- add_action('wp_head', 'wp_rp_head_resources');
33
- add_action('wp_before_admin_bar_render', 'wp_rp_extend_adminbar');
34
-
35
- add_action('plugins_loaded', 'wp_rp_init_zemanta');
36
- function wp_rp_init_zemanta() {
37
- include_once(dirname(__FILE__) . '/zemanta/zemanta.php');
38
- if (wp_rp_is_classic()) {
39
- $wprp_zemanta = new WPRPZemanta();
40
- }
41
- }
42
-
43
- function wp_rp_extend_adminbar() {
44
- global $wp_admin_bar;
45
-
46
- if(!is_super_admin() || !is_admin_bar_showing())
47
- return;
48
-
49
- $wp_admin_bar->add_menu(array(
50
- 'id' => 'wp_rp_adminbar_menu',
51
- 'title' => __('Related Posts', 'wp_related_posts'),
52
- 'href' => admin_url('admin.php?page=wordpress-related-posts&ref=adminbar')
53
- ));
54
- }
55
-
56
- global $wp_rp_output;
57
- $wp_rp_output = array();
58
- function wp_rp_add_related_posts_hook($content) {
59
- global $wp_rp_output, $post;
60
-
61
- $options = wp_rp_get_options();
62
-
63
- if ($post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
64
- if (!isset($wp_rp_output[$post->ID])) {
65
- $wp_rp_output[$post->ID] = wp_rp_get_related_posts();
66
- }
67
- $content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
68
- $content = $content . $wp_rp_output[$post->ID];
69
- }
70
-
71
- return $content;
72
- }
73
- add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
74
-
75
- global $wp_rp_is_phone;
76
- function wp_rp_is_phone() {
77
- global $wp_rp_is_phone;
78
-
79
- if (!isset($wp_rp_is_phone)) {
80
- $detect = new WpRpMobileDetect();
81
- $wp_rp_is_phone = $detect->isMobile() && !$detect->isTablet();
82
- }
83
-
84
- return $wp_rp_is_phone;
85
- }
86
-
87
- function wp_rp_get_platform_options() {
88
- $options = wp_rp_get_options();
89
-
90
- $thumb_options = array('custom_size_thumbnail_enabled' => false);
91
-
92
- if (!empty($options['custom_size_thumbnail_enabled'])) {
93
- $thumb_options['custom_size_thumbnail_enabled'] = $options['custom_size_thumbnail_enabled'];
94
- $thumb_options['custom_thumbnail_width'] = $options['custom_thumbnail_width'];
95
- $thumb_options['custom_thumbnail_height'] = $options['custom_thumbnail_height'];
96
- }
97
- if (wp_rp_is_phone()) {
98
- return $options['mobile'];
99
- }
100
- return $options['desktop'] + $thumb_options;
101
- }
102
-
103
- function wp_rp_ajax_load_articles_callback() {
104
- global $post;
105
-
106
- $getdata = stripslashes_deep($_GET);
107
- if (!isset($getdata['post_id'])) {
108
- die('error');
109
- }
110
-
111
- $post = get_post($getdata['post_id']);
112
- if(!$post) {
113
- die('error');
114
- }
115
-
116
- $from = (isset($getdata['from']) && is_numeric($getdata['from'])) ? intval($getdata['from']) : 0;
117
- $count = (isset($getdata['count']) && is_numeric($getdata['count'])) ? intval($getdata['count']) : 50;
118
-
119
- $search = isset($getdata['search']) && $getdata['search'] ? $getdata['search'] : false;
120
-
121
- $image_size = isset($getdata['size']) ? $getdata['size'] : 'thumbnail';
122
- if(!($image_size == 'thumbnail' || $image_size == 'full')) {
123
- die('error');
124
- }
125
-
126
- $limit = $count + $from;
127
-
128
- if ($search) {
129
- $the_query = new WP_Query(array(
130
- 's' => $search,
131
- 'post_type' => 'post',
132
- 'post_status'=>'publish',
133
- 'post_count' => $limit));
134
- $related_posts = $the_query->get_posts();
135
- } else {
136
- $related_posts = array();
137
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
138
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
139
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
140
- }
141
-
142
- if(function_exists('qtrans_postsFilter')) {
143
- $related_posts = qtrans_postsFilter($related_posts);
144
- }
145
-
146
- $response_list = array();
147
-
148
- foreach (array_slice($related_posts, $from) as $related_post) {
149
- array_push($response_list, array(
150
- 'id' => $related_post->ID,
151
- 'url' => get_permalink($related_post->ID),
152
- 'title' => $related_post->post_title,
153
- 'excerpt' => $related_post->post_excerpt,
154
- 'date' => $related_post->post_date,
155
- 'comments' => $related_post->comment_count,
156
- 'img' => wp_rp_get_post_thumbnail_img($related_post, $image_size)
157
- ));
158
- }
159
-
160
- header('Content-Type: text/javascript');
161
- die(json_encode($response_list));
162
- }
163
- add_action('wp_ajax_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
164
- add_action('wp_ajax_nopriv_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
165
-
166
- function wp_rp_append_posts(&$related_posts, $fetch_function_name, $limit) {
167
- $options = wp_rp_get_options();
168
-
169
- $len = sizeof($related_posts);
170
- $num_missing_posts = $limit - $len;
171
- if ($num_missing_posts > 0) {
172
- $exclude_ids = array_map(create_function('$p', 'return $p->ID;'), $related_posts);
173
-
174
- $posts = call_user_func($fetch_function_name, $num_missing_posts, $exclude_ids);
175
- if ($posts) {
176
- $related_posts = array_merge($related_posts, $posts);
177
- }
178
- }
179
- }
180
-
181
- function wp_rp_fetch_posts_and_title() {
182
- $options = wp_rp_get_options();
183
-
184
- $limit = $options['max_related_posts'];
185
- $title = $options["related_posts_title"];
186
-
187
- $related_posts = array();
188
-
189
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
190
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
191
- wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
192
-
193
- if(function_exists('qtrans_postsFilter')) {
194
- $related_posts = qtrans_postsFilter($related_posts);
195
- }
196
-
197
- return array(
198
- "posts" => $related_posts,
199
- "title" => $title
200
- );
201
- }
202
-
203
- function wp_rp_get_next_post(&$related_posts, &$selected_related_posts, &$inserted_urls, &$special_urls, $default_post_type) {
204
- $post = false;
205
-
206
- while (!($post && $post->ID) && !(empty($related_posts) && empty($selected_related_posts))) {
207
- $post_type = $default_post_type;
208
-
209
- $post = array_shift($selected_related_posts);
210
-
211
- if ($post && $post->type) {
212
- $post_type = $post->type;
213
- }
214
-
215
- if (!$post || !$post->ID) {
216
- while (!empty($related_posts) && (!($post = array_shift($related_posts)) || isset($special_urls[get_permalink($post->ID)])));
217
- }
218
-
219
- if ($post && $post->ID) {
220
- $post_url = property_exists($post, 'post_url') ? $post->post_url : get_permalink($post->ID);
221
- if (isset($inserted_urls[$post_url])) {
222
- $post = false;
223
- } else {
224
- $post->type = $post_type;
225
- }
226
- }
227
- }
228
-
229
- if (!$post || !$post->ID) {
230
- return false;
231
- }
232
-
233
- $inserted_urls[$post_url] = true;
234
-
235
- return $post;
236
- }
237
-
238
- function wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts) {
239
- $options = wp_rp_get_options();
240
- $platform_options = wp_rp_get_platform_options();
241
- $output = "";
242
-
243
- $statistics_enabled = $options['ctr_dashboard_enabled'];
244
-
245
- $limit = $options['max_related_posts'];
246
-
247
- $inserted_urls = array(); // Used to prevent duplicates
248
- $special_urls = array();
249
-
250
- foreach ($selected_related_posts as $post) {
251
- if (property_exists($post, 'post_url') && $post->post_url) {
252
- $special_urls[$post->post_url] = true;
253
- }
254
- }
255
-
256
- $default_post_type = empty($selected_related_posts) ? 'none' : 'empty';
257
-
258
- $image_size = ($platform_options['theme_name'] == 'pinterest.css') ? 'full' : 'thumbnail';
259
-
260
- for ($i = 0; $i < $limit; $i++) {
261
- $related_post = wp_rp_get_next_post($related_posts, $selected_related_posts, $inserted_urls, $special_urls, $default_post_type);
262
-
263
- if (!$related_post) {
264
- break;
265
- }
266
-
267
- if (property_exists($related_post, 'type')) {
268
- $post_type = $related_post->type;
269
- } else {
270
- $post_type = $default_post_type;
271
- }
272
-
273
- if (in_array($post_type, array('empty', 'none'))) {
274
- $post_id = 'in-' . $related_post->ID;
275
- } else {
276
- $post_id = 'ex-' . $related_post->ID;
277
- }
278
-
279
- $data_attrs = '';
280
- if ($statistics_enabled) {
281
- $data_attrs .= 'data-position="' . $i . '" data-poid="' . $post_id . '" data-post-type="' . $post_type . '" ';
282
- }
283
-
284
- $output .= '<li ' . $data_attrs . '>';
285
-
286
- $post_url = property_exists($related_post, 'post_url') ? $related_post->post_url : get_permalink($related_post->ID);
287
-
288
- $img = wp_rp_get_post_thumbnail_img($related_post, $image_size);
289
- if ($img) {
290
- $output .= '<a href="' . $post_url . '" class="wp_rp_thumbnail">' . $img . '</a>';
291
- }
292
-
293
- if ($platform_options["display_publish_date"]){
294
- $dateformat = get_option('date_format');
295
- $output .= '<small class="wp_rp_publish_date">' . mysql2date($dateformat, $related_post->post_date) . '</small> ';
296
- //$output .= mysql2date($dateformat, $related_post->post_date) . " -- ";
297
- }
298
-
299
- $output .= '<a href="' . $post_url . '" class="wp_rp_title">' . wptexturize($related_post->post_title) . '</a>';
300
-
301
- if ($platform_options["display_comment_count"] && property_exists($related_post, 'comment_count')){
302
- $output .= '<small class="wp_rp_comments_count"> (' . $related_post->comment_count . ')</small><br />';
303
- }
304
-
305
- if ($platform_options["display_excerpt"]){
306
- $excerpt_max_length = $platform_options["excerpt_max_length"];
307
- $excerpt = '';
308
-
309
- if ($related_post->post_excerpt){
310
- $excerpt = strip_shortcodes(strip_tags($related_post->post_excerpt));
311
- }
312
- if (!$excerpt) {
313
- $excerpt = strip_shortcodes(strip_tags($related_post->post_content));
314
- }
315
-
316
- if ($excerpt) {
317
- if (strlen($excerpt) > $excerpt_max_length) {
318
- $excerpt = mb_substr($excerpt, 0, $excerpt_max_length - 3) . '...';
319
- }
320
- $output .= ' <small class="wp_rp_excerpt">' . $excerpt . '</small>';
321
- }
322
- }
323
- $output .= '</li>';
324
- }
325
-
326
- return $output;
327
- }
328
-
329
- function wp_rp_should_exclude() {
330
- global $wpdb, $post;
331
-
332
- if (!$post || !$post->ID) {
333
- return true;
334
- }
335
-
336
- $options = wp_rp_get_options();
337
-
338
- if(!$options['exclude_categories']) { return false; }
339
-
340
- $q = 'SELECT COUNT(tt.term_id) FROM '. $wpdb->term_taxonomy.' tt, ' . $wpdb->term_relationships.' tr WHERE tt.taxonomy = \'category\' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = '. $post->ID . ' AND tt.term_id IN (' . $options['exclude_categories'] . ')';
341
-
342
- $result = $wpdb->get_col($q);
343
-
344
- $count = (int) $result[0];
345
-
346
- return $count > 0;
347
- }
348
-
349
- function wp_rp_ajax_blogger_network_blacklist_callback() {
350
- check_ajax_referer('wp_rp_ajax_nonce');
351
- if (!current_user_can('delete_users')) {
352
- die();
353
- }
354
-
355
- $sourcefeed = (int) $_GET['sourcefeed'];
356
-
357
- $meta = wp_rp_get_meta();
358
-
359
- $blog_id = $meta['blog_id'];
360
- $auth_key = $meta['auth_key'];
361
- $req_options = array(
362
- 'timeout' => 5
363
- );
364
- $url = WP_RP_CTR_DASHBOARD_URL . "blacklist/?blog_id=$blog_id&auth_key=$auth_key&sfid=$sourcefeed";
365
- $response = wp_remote_get($url, $req_options);
366
-
367
- if (wp_remote_retrieve_response_code($response) == 200) {
368
- $body = wp_remote_retrieve_body($response);
369
- if ($body) {
370
- $doc = json_decode($body);
371
- if ($doc && $doc->status === 'ok') {
372
- header('Content-Type: text/javascript');
373
- echo "if(window['_wp_rp_blacklist_callback$sourcefeed']) window._wp_rp_blacklist_callback$sourcefeed();";
374
- }
375
- }
376
- }
377
- die();
378
- }
379
-
380
- add_action('wp_ajax_rp_blogger_network_blacklist', 'wp_rp_ajax_blogger_network_blacklist_callback');
381
-
382
- function wp_rp_head_resources() {
383
- global $post, $wpdb;
384
-
385
- //error_log("call to wp_rp_head_resources");
386
-
387
- if (wp_rp_should_exclude()) {
388
- return;
389
- }
390
-
391
- $meta = wp_rp_get_meta();
392
- $options = wp_rp_get_options();
393
- $platform_options = wp_rp_get_platform_options();
394
- //error_log('theme name 1: ' . $platform_options['theme_name']);
395
- $statistics_enabled = false;
396
- $remote_recommendations = false;
397
- $output = '';
398
-
399
- if (is_single()) {
400
- $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
401
- $remote_recommendations = $meta['remote_recommendations'] && $statistics_enabled;
402
- }
403
-
404
- $output_vars = "\twindow._wp_rp_static_base_url = '" . esc_js(WP_RP_STATIC_BASE_URL) . "';\n" .
405
- "\twindow._wp_rp_wp_ajax_url = \"" . admin_url('admin-ajax.php') . "\";\n" .
406
- "\twindow._wp_rp_plugin_version = '" . WP_RP_VERSION . "';\n" .
407
- "\twindow._wp_rp_post_id = '" . esc_js($post->ID) . "';\n" .
408
- "\twindow._wp_rp_num_rel_posts = '" . $options['max_related_posts'] . "';\n";
409
-
410
-
411
- if ($statistics_enabled) {
412
- $tags = $wpdb->get_col("SELECT DISTINCT(label) FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID ORDER BY weight desc;", 0);
413
- if (!empty($tags)) {
414
- $post_tags = '[' . implode(', ', array_map(create_function('$v', 'return "\'" . urlencode(substr($v, strpos($v, \'_\') + 1)) . "\'";'), $tags)) . ']';
415
- } else {
416
- $post_tags = '[]';
417
- }
418
-
419
- $output_vars .= "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
420
- "\twindow._wp_rp_thumbnails = " . ($platform_options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
421
- "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
422
- "\twindow._wp_rp_post_tags = {$post_tags};\n" .
423
- "\twindow._wp_rp_remote_recommendations = " . ($remote_recommendations ? 'true' : 'false') . ";\n" .
424
- "\twindow._wp_rp_promoted_content = " . ($options['promoted_content_enabled'] ? 'true' : 'false') . ";\n" .
425
- "\twindow._wp_rp_traffic_exchange = " . ($options['traffic_exchange_enabled'] ? 'true' : 'false') . ";\n" .
426
- (current_user_can('edit_posts') ?
427
- "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" .
428
- "\twindow._wp_rp_plugin_static_base_url = '" . esc_js(plugins_url('static/' , __FILE__)) . "';\n" .
429
- "\twindow._wp_rp_ajax_nonce = '" . wp_create_nonce("wp_rp_ajax_nonce") . "';\n" .
430
- "\twindow._wp_rp_erp_search = true;\n"
431
- : '');
432
- }
433
-
434
- $output .= "<script type=\"text/javascript\">\n" . $output_vars . "</script>\n";
435
-
436
- if($statistics_enabled) {
437
- $output .= '<script type="text/javascript" src="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_LOADER_FILE . '?version=' . WP_RP_VERSION . '" async></script>' . "\n";
438
- }
439
-
440
- if ($options['enable_themes']) {
441
- $theme_url = plugins_url(WP_RP_STATIC_THEMES_PATH, __FILE__);
442
-
443
- if ($platform_options['theme_name'] !== 'plain.css' && $platform_options['theme_name'] !== 'm-plain.css') {
444
- $output .= '<link rel="stylesheet" href="' . $theme_url . $platform_options['theme_name'] . '?version=' . WP_RP_VERSION . '" />' . "\n";
445
- }
446
-
447
- if ($platform_options['custom_theme_enabled']) {
448
- $output .= '<style type="text/css">' . "\n" . $platform_options['theme_custom_css'] . "</style>\n";
449
- }
450
-
451
- if ($platform_options['theme_name'] === 'm-stream.css') {
452
- wp_enqueue_script('wp_rp_infiniterecs', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_INFINITE_RECS_JS_FILE, array('jquery'), WP_RP_VERSION);
453
- }
454
-
455
- if ($platform_options['theme_name'] === 'pinterest.css') {
456
- wp_enqueue_script('wp_rp_pinterest', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_PINTEREST_JS_FILE, array('jquery'), WP_RP_VERSION);
457
- }
458
- }
459
-
460
- if (current_user_can('edit_posts') && $statistics_enabled) {
461
- wp_enqueue_style('wp_rp_edit_related_posts_css', WP_RP_STATIC_BASE_URL . 'wp-rp-css/edit_related_posts.css', array(), WP_RP_VERSION);
462
- wp_enqueue_script('wp_rp_edit_related_posts_js', WP_RP_STATIC_BASE_URL . 'js/edit_related_posts.js', array('jquery'), WP_RP_VERSION);
463
- }
464
-
465
- echo $output;
466
- }
467
-
468
- function wp_rp_get_selected_posts() {
469
- global $post;
470
-
471
- $selected_related_posts = get_post_meta($post->ID, '_wp_rp_selected_related_posts');
472
- if (empty($selected_related_posts)) {
473
- return array();
474
- }
475
-
476
- $selected_related_posts = $selected_related_posts[0];
477
- if (empty($selected_related_posts)) {
478
- return array();
479
- }
480
-
481
- $options = wp_rp_get_options();
482
- $limit = $options['max_related_posts'];
483
-
484
- return array_slice((array)$selected_related_posts, 0, $limit);
485
- }
486
-
487
- global $wp_rp_is_first_widget;
488
- $wp_rp_is_first_widget = true;
489
- function wp_rp_get_related_posts($before_title = '', $after_title = '') {
490
- if (wp_rp_should_exclude()) {
491
- return;
492
- }
493
-
494
- global $post, $wp_rp_is_first_widget;
495
- global $wp_rp_test_group; // used for AB testing on mobile
496
-
497
- $options = wp_rp_get_options();
498
- $platform_options = wp_rp_get_platform_options();
499
- $meta = wp_rp_get_meta();
500
-
501
- $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
502
- $remote_recommendations = is_single() && $meta['remote_recommendations'] && $statistics_enabled;
503
-
504
- $posts_and_title = wp_rp_fetch_posts_and_title();
505
- $related_posts = $posts_and_title['posts'];
506
- $title = $posts_and_title['title'];
507
-
508
- $selected_related_posts = wp_rp_get_selected_posts();
509
-
510
- $related_posts_content = "";
511
-
512
- if (!$related_posts) {
513
- return;
514
- }
515
-
516
- $posts_footer = '';
517
- if (current_user_can('edit_posts') && $statistics_enabled) {
518
- $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_edit" href="#" id="wp_rp_edit_related_posts">Edit Related Posts</a></div>';
519
- }
520
- if ($options['display_zemanta_linky']) {
521
- $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_backlink" target="_blank" href="http://www.zemanta.com/?wp-related-posts" rel="nofollow">Zemanta</a></div>';
522
- }
523
-
524
- $css_classes = 'related_post wp_rp';
525
- $css_classes_wrap = ' ' . str_replace(array('.css', '-'), array('', '_'), esc_attr('wp_rp_' . $platform_options['theme_name']));
526
-
527
- $related_posts_lis = wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts);
528
- $related_posts_ul = '<ul class="' . $css_classes . '" style="visibility: ' . ($remote_recommendations ? 'hidden' : 'visible') . '">' . $related_posts_lis . '</ul>';
529
-
530
- $related_posts_title = $title ? ($before_title ? $before_title . $title . $after_title : '<h3 class="related_post_title">' . $title . '</h3>') : '';
531
-
532
- $first_id_attr = '';
533
- if($wp_rp_is_first_widget) {
534
- $wp_rp_is_first_widget = false;
535
- $first_id_attr = 'id="wp_rp_first"';
536
- }
537
-
538
- $wrap_style = '';
539
- //error_log('test group when content:' . $wp_rp_test_group);
540
- if ($wp_rp_test_group == 2) {
541
- $wrap_style = ' style="display:none;"';
542
- }
543
-
544
- $output = '<div class="wp_rp_wrap ' . $css_classes_wrap . '" ' . $first_id_attr . $wrap_style . '>' .
545
- '<div class="wp_rp_content">' .
546
- $related_posts_title .
547
- $related_posts_ul .
548
- $posts_footer .
549
- '</div>' .
550
- ($remote_recommendations ? '<script type="text/javascript">window._wp_rp_callback_widget_exists ? window._wp_rp_callback_widget_exists() : false;</script>' : '') .
551
- '</div>';
552
-
553
- return "\n" . $output . "\n";
554
- }
555
-
1
+ <?php
2
+ /*
3
+ Plugin Name: WordPress Related Posts
4
+ Version: 3.3
5
+ Plugin URI: http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/
6
+ Description: Quickly increase your readers' engagement with your posts by adding Related Posts in the footer of your content. Click on <a href="admin.php?page=wordpress-related-posts">Related Posts tab</a> to configure your settings.
7
+ Author: Zemanta Ltd.
8
+ Author URI: http://www.zemanta.com
9
+ */
10
+
11
+ define('WP_RP_VERSION', '3.3');
12
+
13
+ define('WP_RP_PLUGIN_FILE', plugin_basename(__FILE__));
14
+
15
+ include_once(dirname(__FILE__) . '/config.php');
16
+ include_once(dirname(__FILE__) . '/lib/stemmer.php');
17
+ include_once(dirname(__FILE__) . '/lib/mobile_detect.php');
18
+
19
+ include_once(dirname(__FILE__) . '/admin_notices.php');
20
+ include_once(dirname(__FILE__) . '/notifications.php');
21
+ include_once(dirname(__FILE__) . '/widget.php');
22
+ include_once(dirname(__FILE__) . '/thumbnailer.php');
23
+ include_once(dirname(__FILE__) . '/settings.php');
24
+ include_once(dirname(__FILE__) . '/recommendations.php');
25
+ include_once(dirname(__FILE__) . '/dashboard_widget.php');
26
+ include_once(dirname(__FILE__) . '/edit_related_posts.php');
27
+ include_once(dirname(__FILE__) . '/compatibility.php');
28
+
29
+ register_activation_hook(__FILE__, 'wp_rp_activate_hook');
30
+ register_deactivation_hook(__FILE__, 'wp_rp_deactivate_hook');
31
+
32
+ add_action('wp_head', 'wp_rp_head_resources');
33
+ add_action('wp_before_admin_bar_render', 'wp_rp_extend_adminbar');
34
+
35
+ add_action('plugins_loaded', 'wp_rp_init_zemanta');
36
+ function wp_rp_init_zemanta() {
37
+ include_once(dirname(__FILE__) . '/zemanta/zemanta.php');
38
+ if (wp_rp_is_classic()) {
39
+ $wprp_zemanta = new WPRPZemanta();
40
+ }
41
+ }
42
+
43
+ function wp_rp_extend_adminbar() {
44
+ global $wp_admin_bar;
45
+
46
+ if(!is_super_admin() || !is_admin_bar_showing())
47
+ return;
48
+
49
+ $wp_admin_bar->add_menu(array(
50
+ 'id' => 'wp_rp_adminbar_menu',
51
+ 'title' => __('Related Posts', 'wp_related_posts'),
52
+ 'href' => admin_url('admin.php?page=wordpress-related-posts&ref=adminbar')
53
+ ));
54
+ }
55
+
56
+ global $wp_rp_output;
57
+ $wp_rp_output = array();
58
+ function wp_rp_add_related_posts_hook($content) {
59
+ global $wp_rp_output, $post;
60
+
61
+ $options = wp_rp_get_options();
62
+
63
+ if ($post->post_type === 'post' && (($options["on_single_post"] && is_single()) || (is_feed() && $options["on_rss"]))) {
64
+ if (!isset($wp_rp_output[$post->ID])) {
65
+ $wp_rp_output[$post->ID] = wp_rp_get_related_posts();
66
+ }
67
+ $content = str_replace('%RELATEDPOSTS%', '', $content); // used for gp
68
+ $content = $content . $wp_rp_output[$post->ID];
69
+ }
70
+
71
+ return $content;
72
+ }
73
+ add_filter('the_content', 'wp_rp_add_related_posts_hook', 10);
74
+
75
+ global $wp_rp_is_phone;
76
+ function wp_rp_is_phone() {
77
+ global $wp_rp_is_phone;
78
+
79
+ if (!isset($wp_rp_is_phone)) {
80
+ $detect = new WpRpMobileDetect();
81
+ $wp_rp_is_phone = $detect->isMobile() && !$detect->isTablet();
82
+ }
83
+
84
+ return $wp_rp_is_phone;
85
+ }
86
+
87
+ function wp_rp_get_platform_options() {
88
+ $options = wp_rp_get_options();
89
+
90
+ $thumb_options = array('custom_size_thumbnail_enabled' => false);
91
+
92
+ if (!empty($options['custom_size_thumbnail_enabled'])) {
93
+ $thumb_options['custom_size_thumbnail_enabled'] = $options['custom_size_thumbnail_enabled'];
94
+ $thumb_options['custom_thumbnail_width'] = $options['custom_thumbnail_width'];
95
+ $thumb_options['custom_thumbnail_height'] = $options['custom_thumbnail_height'];
96
+ }
97
+ if (wp_rp_is_phone()) {
98
+ return $options['mobile'];
99
+ }
100
+ return $options['desktop'] + $thumb_options;
101
+ }
102
+
103
+ function wp_rp_ajax_load_articles_callback() {
104
+ global $post;
105
+
106
+ $getdata = stripslashes_deep($_GET);
107
+ if (!isset($getdata['post_id'])) {
108
+ die('error');
109
+ }
110
+
111
+ $post = get_post($getdata['post_id']);
112
+ if(!$post) {
113
+ die('error');
114
+ }
115
+
116
+ $from = (isset($getdata['from']) && is_numeric($getdata['from'])) ? intval($getdata['from']) : 0;
117
+ $count = (isset($getdata['count']) && is_numeric($getdata['count'])) ? intval($getdata['count']) : 50;
118
+
119
+ $search = isset($getdata['search']) && $getdata['search'] ? $getdata['search'] : false;
120
+
121
+ $image_size = isset($getdata['size']) ? $getdata['size'] : 'thumbnail';
122
+ if(!($image_size == 'thumbnail' || $image_size == 'full')) {
123
+ die('error');
124
+ }
125
+
126
+ $limit = $count + $from;
127
+
128
+ if ($search) {
129
+ $the_query = new WP_Query(array(
130
+ 's' => $search,
131
+ 'post_type' => 'post',
132
+ 'post_status'=>'publish',
133
+ 'post_count' => $limit));
134
+ $related_posts = $the_query->get_posts();
135
+ } else {
136
+ $related_posts = array();
137
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
138
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
139
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
140
+ }
141
+
142
+ if(function_exists('qtrans_postsFilter')) {
143
+ $related_posts = qtrans_postsFilter($related_posts);
144
+ }
145
+
146
+ $response_list = array();
147
+
148
+ foreach (array_slice($related_posts, $from) as $related_post) {
149
+ array_push($response_list, array(
150
+ 'id' => $related_post->ID,
151
+ 'url' => get_permalink($related_post->ID),
152
+ 'title' => $related_post->post_title,
153
+ 'excerpt' => $related_post->post_excerpt,
154
+ 'date' => $related_post->post_date,
155
+ 'comments' => $related_post->comment_count,
156
+ 'img' => wp_rp_get_post_thumbnail_img($related_post, $image_size)
157
+ ));
158
+ }
159
+
160
+ header('Content-Type: text/javascript');
161
+ die(json_encode($response_list));
162
+ }
163
+ add_action('wp_ajax_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
164
+ add_action('wp_ajax_nopriv_wp_rp_load_articles', 'wp_rp_ajax_load_articles_callback');
165
+
166
+ function wp_rp_append_posts(&$related_posts, $fetch_function_name, $limit) {
167
+ $options = wp_rp_get_options();
168
+
169
+ $len = sizeof($related_posts);
170
+ $num_missing_posts = $limit - $len;
171
+ if ($num_missing_posts > 0) {
172
+ $exclude_ids = array_map(create_function('$p', 'return $p->ID;'), $related_posts);
173
+
174
+ $posts = call_user_func($fetch_function_name, $num_missing_posts, $exclude_ids);
175
+ if ($posts) {
176
+ $related_posts = array_merge($related_posts, $posts);
177
+ }
178
+ }
179
+ }
180
+
181
+ function wp_rp_fetch_posts_and_title() {
182
+ $options = wp_rp_get_options();
183
+
184
+ $limit = $options['max_related_posts'];
185
+ $title = $options["related_posts_title"];
186
+
187
+ $related_posts = array();
188
+
189
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts_v2', $limit);
190
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_related_posts', $limit);
191
+ wp_rp_append_posts($related_posts, 'wp_rp_fetch_random_posts', $limit);
192
+
193
+ if(function_exists('qtrans_postsFilter')) {
194
+ $related_posts = qtrans_postsFilter($related_posts);
195
+ }
196
+
197
+ return array(
198
+ "posts" => $related_posts,
199
+ "title" => $title
200
+ );
201
+ }
202
+
203
+ function wp_rp_get_next_post(&$related_posts, &$selected_related_posts, &$inserted_urls, &$special_urls, $default_post_type) {
204
+ $post = false;
205
+
206
+ while (!($post && $post->ID) && !(empty($related_posts) && empty($selected_related_posts))) {
207
+ $post_type = $default_post_type;
208
+
209
+ $post = array_shift($selected_related_posts);
210
+
211
+ if ($post && $post->type) {
212
+ $post_type = $post->type;
213
+ }
214
+
215
+ if (!$post || !$post->ID) {
216
+ while (!empty($related_posts) && (!($post = array_shift($related_posts)) || isset($special_urls[get_permalink($post->ID)])));
217
+ }
218
+
219
+ if ($post && $post->ID) {
220
+ $post_url = property_exists($post, 'post_url') ? $post->post_url : get_permalink($post->ID);
221
+ if (isset($inserted_urls[$post_url])) {
222
+ $post = false;
223
+ } else {
224
+ $post->type = $post_type;
225
+ }
226
+ }
227
+ }
228
+
229
+ if (!$post || !$post->ID) {
230
+ return false;
231
+ }
232
+
233
+ $inserted_urls[$post_url] = true;
234
+
235
+ return $post;
236
+ }
237
+
238
+ function wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts) {
239
+ $options = wp_rp_get_options();
240
+ $platform_options = wp_rp_get_platform_options();
241
+ $output = "";
242
+
243
+ $statistics_enabled = $options['ctr_dashboard_enabled'];
244
+
245
+ $limit = $options['max_related_posts'];
246
+
247
+ $inserted_urls = array(); // Used to prevent duplicates
248
+ $special_urls = array();
249
+
250
+ foreach ($selected_related_posts as $post) {
251
+ if (property_exists($post, 'post_url') && $post->post_url) {
252
+ $special_urls[$post->post_url] = true;
253
+ }
254
+ }
255
+
256
+ $default_post_type = empty($selected_related_posts) ? 'none' : 'empty';
257
+
258
+ $image_size = ($platform_options['theme_name'] == 'pinterest.css') ? 'full' : 'thumbnail';
259
+
260
+ for ($i = 0; $i < $limit; $i++) {
261
+ $related_post = wp_rp_get_next_post($related_posts, $selected_related_posts, $inserted_urls, $special_urls, $default_post_type);
262
+
263
+ if (!$related_post) {
264
+ break;
265
+ }
266
+
267
+ if (property_exists($related_post, 'type')) {
268
+ $post_type = $related_post->type;
269
+ } else {
270
+ $post_type = $default_post_type;
271
+ }
272
+
273
+ if (in_array($post_type, array('empty', 'none'))) {
274
+ $post_id = 'in-' . $related_post->ID;
275
+ } else {
276
+ $post_id = 'ex-' . $related_post->ID;
277
+ }
278
+
279
+ $data_attrs = '';
280
+ if ($statistics_enabled) {
281
+ $data_attrs .= 'data-position="' . $i . '" data-poid="' . $post_id . '" data-post-type="' . $post_type . '" ';
282
+ }
283
+
284
+ $output .= '<li ' . $data_attrs . '>';
285
+
286
+ $post_url = property_exists($related_post, 'post_url') ? $related_post->post_url : get_permalink($related_post->ID);
287
+
288
+ $img = wp_rp_get_post_thumbnail_img($related_post, $image_size);
289
+ if ($img) {
290
+ $output .= '<a href="' . $post_url . '" class="wp_rp_thumbnail">' . $img . '</a>';
291
+ }
292
+
293
+ if ($platform_options["display_publish_date"]) {
294
+ $dateformat = get_option('date_format');
295
+ $output .= '<small class="wp_rp_publish_date">' . mysql2date($dateformat, $related_post->post_date) . '</small> ';
296
+ //$output .= mysql2date($dateformat, $related_post->post_date) . " -- ";
297
+ }
298
+
299
+ $output .= '<a href="' . $post_url . '" class="wp_rp_title">' . wptexturize($related_post->post_title) . '</a>';
300
+
301
+ if ($platform_options["display_comment_count"] && property_exists($related_post, 'comment_count')){
302
+ $output .= '<small class="wp_rp_comments_count"> (' . $related_post->comment_count . ')</small><br />';
303
+ }
304
+
305
+ if ($platform_options["display_excerpt"]){
306
+ $excerpt_max_length = $platform_options["excerpt_max_length"];
307
+ $excerpt = '';
308
+
309
+ if ($related_post->post_excerpt){
310
+ $excerpt = strip_shortcodes(strip_tags($related_post->post_excerpt));
311
+ }
312
+ if (!$excerpt) {
313
+ $excerpt = strip_shortcodes(strip_tags($related_post->post_content));
314
+ }
315
+
316
+ if ($excerpt) {
317
+ if (strlen($excerpt) > $excerpt_max_length) {
318
+ $excerpt = mb_substr($excerpt, 0, $excerpt_max_length - 3) . '...';
319
+ }
320
+ $output .= ' <small class="wp_rp_excerpt">' . $excerpt . '</small>';
321
+ }
322
+ }
323
+ $output .= '</li>';
324
+ }
325
+
326
+ return $output;
327
+ }
328
+
329
+ function wp_rp_should_exclude() {
330
+ global $wpdb, $post;
331
+
332
+ if (!$post || !$post->ID) {
333
+ return true;
334
+ }
335
+
336
+ $options = wp_rp_get_options();
337
+
338
+ if(!$options['exclude_categories']) { return false; }
339
+
340
+ $q = 'SELECT COUNT(tt.term_id) FROM '. $wpdb->term_taxonomy.' tt, ' . $wpdb->term_relationships.' tr WHERE tt.taxonomy = \'category\' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = '. $post->ID . ' AND tt.term_id IN (' . $options['exclude_categories'] . ')';
341
+
342
+ $result = $wpdb->get_col($q);
343
+
344
+ $count = (int) $result[0];
345
+
346
+ return $count > 0;
347
+ }
348
+
349
+ function wp_rp_ajax_blogger_network_blacklist_callback() {
350
+ check_ajax_referer('wp_rp_ajax_nonce');
351
+ if (!current_user_can('delete_users')) {
352
+ die();
353
+ }
354
+
355
+ $sourcefeed = (int) $_GET['sourcefeed'];
356
+
357
+ $meta = wp_rp_get_meta();
358
+
359
+ $blog_id = $meta['blog_id'];
360
+ $auth_key = $meta['auth_key'];
361
+ $req_options = array(
362
+ 'timeout' => 5
363
+ );
364
+ $url = WP_RP_CTR_DASHBOARD_URL . "blacklist/?blog_id=$blog_id&auth_key=$auth_key&sfid=$sourcefeed";
365
+ $response = wp_remote_get($url, $req_options);
366
+
367
+ if (wp_remote_retrieve_response_code($response) == 200) {
368
+ $body = wp_remote_retrieve_body($response);
369
+ if ($body) {
370
+ $doc = json_decode($body);
371
+ if ($doc && $doc->status === 'ok') {
372
+ header('Content-Type: text/javascript');
373
+ echo "if(window['_wp_rp_blacklist_callback$sourcefeed']) window._wp_rp_blacklist_callback$sourcefeed();";
374
+ }
375
+ }
376
+ }
377
+ die();
378
+ }
379
+
380
+ add_action('wp_ajax_rp_blogger_network_blacklist', 'wp_rp_ajax_blogger_network_blacklist_callback');
381
+
382
+ function wp_rp_head_resources() {
383
+ global $post, $wpdb;
384
+
385
+ //error_log("call to wp_rp_head_resources");
386
+
387
+ if (wp_rp_should_exclude()) {
388
+ return;
389
+ }
390
+
391
+ $meta = wp_rp_get_meta();
392
+ $options = wp_rp_get_options();
393
+ $platform_options = wp_rp_get_platform_options();
394
+ //error_log('theme name 1: ' . $platform_options['theme_name']);
395
+ $statistics_enabled = false;
396
+ $remote_recommendations = false;
397
+ $output = '';
398
+
399
+ if (is_single()) {
400
+ $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
401
+ $remote_recommendations = $meta['remote_recommendations'] && $statistics_enabled;
402
+ }
403
+
404
+ $output_vars = "\twindow._wp_rp_static_base_url = '" . esc_js(WP_RP_STATIC_BASE_URL) . "';\n" .
405
+ "\twindow._wp_rp_wp_ajax_url = \"" . admin_url('admin-ajax.php') . "\";\n" .
406
+ "\twindow._wp_rp_plugin_version = '" . WP_RP_VERSION . "';\n" .
407
+ "\twindow._wp_rp_post_id = '" . esc_js($post->ID) . "';\n" .
408
+ "\twindow._wp_rp_num_rel_posts = '" . $options['max_related_posts'] . "';\n";
409
+
410
+
411
+ if ($statistics_enabled) {
412
+ $tags = $wpdb->get_col("SELECT DISTINCT(label) FROM " . $wpdb->prefix . "wp_rp_tags WHERE post_id=$post->ID ORDER BY weight desc;", 0);
413
+ if (!empty($tags)) {
414
+ $post_tags = '[' . implode(', ', array_map(create_function('$v', 'return "\'" . urlencode(substr($v, strpos($v, \'_\') + 1)) . "\'";'), $tags)) . ']';
415
+ } else {
416
+ $post_tags = '[]';
417
+ }
418
+
419
+ $output_vars .= "\twindow._wp_rp_blog_id = '" . esc_js($meta['blog_id']) . "';\n" .
420
+ "\twindow._wp_rp_thumbnails = " . ($platform_options['display_thumbnail'] ? 'true' : 'false') . ";\n" .
421
+ "\twindow._wp_rp_post_title = '" . urlencode($post->post_title) . "';\n" .
422
+ "\twindow._wp_rp_post_tags = {$post_tags};\n" .
423
+ "\twindow._wp_rp_remote_recommendations = " . ($remote_recommendations ? 'true' : 'false') . ";\n" .
424
+ "\twindow._wp_rp_promoted_content = " . ($options['promoted_content_enabled'] ? 'true' : 'false') . ";\n" .
425
+ "\twindow._wp_rp_traffic_exchange = " . ($options['traffic_exchange_enabled'] ? 'true' : 'false') . ";\n" .
426
+ (current_user_can('edit_posts') ?
427
+ "\twindow._wp_rp_admin_ajax_url = '" . admin_url('admin-ajax.php') . "';\n" .
428
+ "\twindow._wp_rp_plugin_static_base_url = '" . esc_js(plugins_url('static/' , __FILE__)) . "';\n" .
429
+ "\twindow._wp_rp_ajax_nonce = '" . wp_create_nonce("wp_rp_ajax_nonce") . "';\n" .
430
+ "\twindow._wp_rp_erp_search = true;\n"
431
+ : '');
432
+ }
433
+
434
+ $output .= "<script type=\"text/javascript\">\n" . $output_vars . "</script>\n";
435
+
436
+ if($statistics_enabled) {
437
+ $output .= '<script type="text/javascript" src="' . WP_RP_STATIC_BASE_URL . WP_RP_STATIC_LOADER_FILE . '?version=' . WP_RP_VERSION . '" async></script>' . "\n";
438
+ }
439
+
440
+ if ($options['enable_themes']) {
441
+ $theme_url = plugins_url(WP_RP_STATIC_THEMES_PATH, __FILE__);
442
+
443
+ if ($platform_options['theme_name'] !== 'plain.css' && $platform_options['theme_name'] !== 'm-plain.css') {
444
+ $output .= '<link rel="stylesheet" href="' . $theme_url . $platform_options['theme_name'] . '?version=' . WP_RP_VERSION . '" />' . "\n";
445
+ }
446
+
447
+ if ($platform_options['custom_theme_enabled']) {
448
+ $output .= '<style type="text/css">' . "\n" . $platform_options['theme_custom_css'] . "</style>\n";
449
+ }
450
+
451
+ if ($platform_options['theme_name'] === 'm-stream.css') {
452
+ wp_enqueue_script('wp_rp_infiniterecs', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_INFINITE_RECS_JS_FILE, array('jquery'), WP_RP_VERSION);
453
+ }
454
+
455
+ if ($platform_options['theme_name'] === 'pinterest.css') {
456
+ wp_enqueue_script('wp_rp_pinterest', WP_RP_STATIC_BASE_URL . WP_RP_STATIC_PINTEREST_JS_FILE, array('jquery'), WP_RP_VERSION);
457
+ }
458
+ }
459
+
460
+ if (current_user_can('edit_posts') && $statistics_enabled) {
461
+ wp_enqueue_style('wp_rp_edit_related_posts_css', WP_RP_STATIC_BASE_URL . 'wp-rp-css/edit_related_posts.css', array(), WP_RP_VERSION);
462
+ wp_enqueue_script('wp_rp_edit_related_posts_js', WP_RP_STATIC_BASE_URL . 'js/edit_related_posts.js', array('jquery'), WP_RP_VERSION);
463
+ }
464
+
465
+ echo $output;
466
+ }
467
+
468
+ function wp_rp_get_selected_posts() {
469
+ global $post;
470
+
471
+ $selected_related_posts = get_post_meta($post->ID, '_wp_rp_selected_related_posts');
472
+ if (empty($selected_related_posts)) {
473
+ return array();
474
+ }
475
+
476
+ $selected_related_posts = $selected_related_posts[0];
477
+ if (empty($selected_related_posts)) {
478
+ return array();
479
+ }
480
+
481
+ $options = wp_rp_get_options();
482
+ $limit = $options['max_related_posts'];
483
+
484
+ return array_slice((array)$selected_related_posts, 0, $limit);
485
+ }
486
+
487
+ global $wp_rp_is_first_widget;
488
+ $wp_rp_is_first_widget = true;
489
+ function wp_rp_get_related_posts($before_title = '', $after_title = '') {
490
+ if (wp_rp_should_exclude()) {
491
+ return;
492
+ }
493
+
494
+ global $post, $wp_rp_is_first_widget;
495
+ global $wp_rp_test_group; // used for AB testing on mobile
496
+
497
+ $options = wp_rp_get_options();
498
+ $platform_options = wp_rp_get_platform_options();
499
+ $meta = wp_rp_get_meta();
500
+
501
+ $statistics_enabled = $options['ctr_dashboard_enabled'] && $meta['blog_id'] && $meta['auth_key'];
502
+ $remote_recommendations = is_single() && $meta['remote_recommendations'] && $statistics_enabled;
503
+
504
+ $posts_and_title = wp_rp_fetch_posts_and_title();
505
+ $related_posts = $posts_and_title['posts'];
506
+ $title = $posts_and_title['title'];
507
+
508
+ $selected_related_posts = wp_rp_get_selected_posts();
509
+
510
+ $related_posts_content = "";
511
+
512
+ if (!$related_posts) {
513
+ return;
514
+ }
515
+
516
+ $posts_footer = '';
517
+ if (current_user_can('edit_posts') && $statistics_enabled) {
518
+ $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_edit" href="#" id="wp_rp_edit_related_posts">Edit Related Posts</a></div>';
519
+ }
520
+ if ($options['display_zemanta_linky']) {
521
+ $posts_footer .= '<div class="wp_rp_footer"><a class="wp_rp_backlink" target="_blank" href="http://www.zemanta.com/?wp-related-posts" rel="nofollow">Zemanta</a></div>';
522
+ }
523
+
524
+ $css_classes = 'related_post wp_rp';
525
+ $css_classes_wrap = ' ' . str_replace(array('.css', '-'), array('', '_'), esc_attr('wp_rp_' . $platform_options['theme_name']));
526
+
527
+ $related_posts_lis = wp_rp_generate_related_posts_list_items($related_posts, $selected_related_posts);
528
+ $related_posts_ul = '<ul class="' . $css_classes . '" style="visibility: ' . ($remote_recommendations ? 'hidden' : 'visible') . '">' . $related_posts_lis . '</ul>';
529
+
530
+ $related_posts_title = $title ? ($before_title ? $before_title . $title . $after_title : '<h3 class="related_post_title">' . $title . '</h3>') : '';
531
+
532
+ $first_id_attr = '';
533
+ if($wp_rp_is_first_widget) {
534
+ $wp_rp_is_first_widget = false;
535
+ $first_id_attr = 'id="wp_rp_first"';
536
+ }
537
+
538
+ $wrap_style = '';
539
+ //error_log('test group when content:' . $wp_rp_test_group);
540
+ if ($wp_rp_test_group == 2) {
541
+ $wrap_style = ' style="display:none;"';
542
+ }
543
+
544
+ $output = '<div class="wp_rp_wrap ' . $css_classes_wrap . '" ' . $first_id_attr . $wrap_style . '>' .
545
+ '<div class="wp_rp_content">' .
546
+ $related_posts_title .
547
+ $related_posts_ul .
548
+ $posts_footer .
549
+ '</div>' .
550
+ ($remote_recommendations ? '<script type="text/javascript">window._wp_rp_callback_widget_exists ? window._wp_rp_callback_widget_exists() : false;</script>' : '') .
551
+ '</div>';
552
+
553
+ return "\n" . $output . "\n";
554
+ }
555
+
zemanta/zemanta.php CHANGED
@@ -1,708 +1,711 @@
1
- <?php
2
-
3
- define('WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION', 'zemanta_plugin_version');
4
- define('WP_RP_ZEMANTA_PLUGIN_FLASH_META', 'zemanta_plugin_flash');
5
-
6
- if(!class_exists('WP_Http')) {
7
- require_once(ABSPATH . WPINC . '/class-http.php');
8
- }
9
-
10
- require_once(ABSPATH . 'wp-admin/includes/image.php');
11
-
12
- class WPRPZemanta {
13
-
14
- var $version = '1.2.3';
15
- var $api_url = 'http://api.zemanta.com/services/rest/0.0/';
16
- var $api_key = '';
17
- var $options = array();
18
- var $supported_features = array();
19
- var $update_notes = array();
20
- var $flash_data = null;
21
- var $top_menu_slug = null;
22
-
23
- public function __construct()
24
- {
25
- global $wp_version;
26
-
27
- // initialize update notes shown once on plugin update
28
- $this->update_notes['1.0.5'] = __('Please double-check your upload paths in Zemanta Settings, we changed some things that might affect your images.', 'zemanta');
29
- $this->update_notes['1.0.7'] = __('Please double-check your upload paths in Zemanta Settings, we changed some things that might affect your images.', 'zemanta');
30
- $this->update_notes['1.0.8'] = __('Please double-check your upload paths in Zemanta Settings, we changed some things that might affect your images.', 'zemanta');
31
-
32
- add_action('admin_init', array($this, 'init'));
33
- add_action('admin_init', array($this, 'register_options'));
34
-
35
- register_activation_hook(dirname(__FILE__) . '/zemanta.php', array($this, 'activate'));
36
-
37
- $this->supported_features['featured_image'] = version_compare($wp_version, '3.1', '>=') >= 0;
38
- }
39
-
40
- /**
41
- * admin_init
42
- *
43
- * Initialize plugin
44
- *
45
- */
46
- public function init()
47
- {
48
- add_action('wp_ajax_zemanta_set_featured_image', array($this, 'ajax_zemanta_set_featured_image'));
49
- add_action('edit_form_advanced', array($this, 'assets'), 1);
50
- add_action('edit_page_form', array($this, 'assets'), 1);
51
- add_action('save_post', array($this, 'save_post'), 20);
52
-
53
- $this->check_plugin_updated();
54
- $this->create_options();
55
- $this->check_options();
56
-
57
- if(!$this->check_dependencies())
58
- add_action('admin_notices', array($this, 'warning'));
59
- }
60
-
61
- /**
62
- * activate
63
- *
64
- * Run any functions needed for plugin activation
65
- */
66
- public function activate()
67
- {
68
- $this->fix_user_meta();
69
- }
70
-
71
- /**
72
- * admin_head
73
- *
74
- * Add any assets to the edit page
75
- */
76
- public function assets()
77
- {
78
- $this->render('assets', array(
79
- 'api_key' => $this->api_key,
80
- 'version' => $this->version,
81
- 'features' => $this->supported_features
82
- ));
83
- }
84
-
85
- /**
86
- * warning for no api key
87
- *
88
- * Display api key warning
89
- */
90
- public function warning_no_api_key()
91
- {
92
- $this->render('message', array(
93
- 'type' => 'error'
94
- ,'message' => __('You have no Zemanta API key and the plugin was unable to retrieve one. You can still use Zemanta, '.
95
- 'but until the new key is successfully obtained you will not be able to customize the widget or remove '.
96
- 'this warning. You may try to deactivate and activate the plugin again to make it retry to obtain the key.', 'zemanta')
97
- ));
98
- }
99
-
100
- /**
101
- * warning
102
- *
103
- * Display plugin warning
104
- */
105
- public function warning()
106
- {
107
- $this->render('message', array(
108
- 'type' => 'updated fade'
109
- ,'message' => __('Zemanta needs either the cURL PHP module or allow_url_fopen enabled to work. Please ask your server administrator to set either of these up.', 'zemanta')
110
- ));
111
- }
112
-
113
- /**
114
- * add_options
115
- *
116
- * Add configuration page to menu
117
- */
118
- public function add_options()
119
- {
120
- $this->top_menu_slug = add_menu_page(
121
- __('Zemanta', 'zemanta'),
122
- __('Zemanta', 'zemanta'),
123
- 'manage_options', 'zemanta',
124
- array($this, 'options'),
125
- plugins_url('/img/menu_icon.png', __FILE__)
126
- );
127
- }
128
-
129
- /**
130
- * check_options
131
- *
132
- * Check to see if we need to create or import options
133
- */
134
- public function check_options()
135
- {
136
- $this->api_key = $this->get_api_key();
137
-
138
- if (!$this->api_key)
139
- {
140
- $options = get_option('zemanta_options');
141
-
142
- if (!$options)
143
- {
144
- $options = $this->legacy_options($options);
145
- }
146
-
147
- $this->api_key = $this->get_api_key();
148
- if (!$this->api_key)
149
- {
150
- $this->api_key = $this->fetch_api_key();
151
- if ($this->api_key)
152
- {
153
- $this->set_api_key($this->api_key);
154
- }
155
- else
156
- {
157
- add_action('admin_notices', array($this, 'warning_no_api_key'));
158
- }
159
- }
160
- }
161
- }
162
-
163
- /**
164
- * create_options
165
- *
166
- * Create the Initial Options
167
- */
168
- public function create_options()
169
- {
170
- $wp_upload_dir = wp_upload_dir();
171
- $options = array(
172
- 'zemanta_option_api_key' => array(
173
- 'type' => 'apikey'
174
- ,'title' => __('Your API key (in case you need to contact support)', 'zemanta')
175
- ,'field' => 'api_key'
176
- ,'default_value' => $this->api_key
177
- )
178
- ,'zemanta_option_image_upload' => array(
179
- 'type' => 'checkbox'
180
- ,'title' => __('Automatically upload inserted images to your blog', 'zemanta')
181
- ,'field' => 'image_uploader'
182
- //,'description' => __('Using Zemanta image uploader in this way may download copyrighted images to your blog. Make sure you and your blog writers check and understand licenses of each and every image before using them in your blog posts and delete them if they infringe on author\'s rights.')
183
- )
184
- );
185
-
186
- $this->options = apply_filters('zemanta_options', $options);
187
- }
188
-
189
- /**
190
- * register_options
191
- *
192
- * Register options with Settings API
193
- */
194
- public function register_options()
195
- {
196
- register_setting('zemanta_options', 'zemanta_options', array($this, 'validate_options'));
197
-
198
- add_settings_section('zemanta_options_plugin', null, array($this, 'callback_options_dummy'), 'zemanta');
199
- add_settings_field('zemanta_option_api_key', 'Your API key', array($this, 'options_set'), 'zemanta', 'zemanta_options_plugin', $this->options['zemanta_option_api_key']);
200
-
201
- add_settings_section('zemanta_options_image', null, array($this, 'callback_options_dummy'), 'zemanta');
202
- add_settings_field('zemanta_option_image_upload', 'Enable image uploader', array($this, 'options_set'), 'zemanta', 'zemanta_options_image', $this->options['zemanta_option_image_upload']);
203
- }
204
-
205
- /**
206
- * callback_options_dummy
207
- *
208
- * Dummy callback for add_settings_sections
209
- */
210
- public function callback_options_dummy()
211
- {
212
- }
213
-
214
- /**
215
- * options_set
216
- *
217
- * Output the fields for the options
218
- */
219
- public function options_set($option = null)
220
- {
221
- // WordPress < 2.9 has a bug where the settings callback is not passed the arguments value so we check for it here.
222
- if ($option == null)
223
- {
224
- $option = array_shift($this->options);
225
- }
226
-
227
- $this->render('options-input-' . $option['type'], array(
228
- 'option' => $this->get_option($option['field']),
229
- 'field' => $option['field'],
230
- 'title' => isset($option['title']) ? $option['title'] : null,
231
- 'default_value' => isset($option['default_value']) ? $option['default_value'] : null,
232
- 'description' => isset($option['description']) ? $option['description'] : null
233
- ));
234
- }
235
-
236
- /**
237
- * validate_options
238
- *
239
- * Handle input Validation
240
- */
241
- public function validate_options($input) {
242
- return $input;
243
- }
244
-
245
- /**
246
- * options
247
- *
248
- * Add configuration page
249
- */
250
- public function options()
251
- {
252
- if(!$this->api_key)
253
- {
254
- $this->api_key = $this->fetch_api_key();
255
- $this->set_option('api_key', $this->api_key);
256
- }
257
-
258
- $this->render('options', array(
259
- 'api_key' => $this->api_key
260
- ));
261
- }
262
-
263
- /**
264
- * sideload_image
265
- *
266
- * New image uploader, this is slightly modified version of media_sideload_image from wp-admin/includes/media.php
267
- *
268
- * @param string $file the URL of the image to download
269
- * @param int $post_id The post ID the media is to be associated with
270
- * @param string $desc Optional. Description of the image
271
- * @return string|WP_Error uploaded image URL on success
272
- */
273
- public function sideload_image($file, $post_id, $desc = null) {
274
- $tmp = download_url($file);
275
- preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
276
-
277
- $file_array = array(
278
- // sometimes wikipedia images have % in file names
279
- // so let's fix it to avoid URL encoding conflicts
280
- 'name' => str_replace('%', '_', basename($matches[0])),
281
- 'tmp_name' => $tmp
282
- );
283
-
284
- // If error storing temporarily, unlink
285
- if(is_wp_error($tmp)) {
286
- @unlink($file_array['tmp_name']);
287
- $file_array['tmp_name'] = '';
288
- }
289
-
290
- $id = media_handle_sideload($file_array, $post_id, $desc);
291
- if(is_wp_error($id)) {
292
- @unlink($file_array['tmp_name']);
293
- return $id;
294
- }
295
-
296
- return wp_get_attachment_url($id);
297
- }
298
-
299
- /**
300
- * is_uploader_enabled
301
- *
302
- */
303
- public function is_uploader_enabled()
304
- {
305
- return $this->get_option('image_uploader');
306
- }
307
-
308
- /**
309
- * save_post
310
- *
311
- * Download images if necessary and update post
312
- */
313
- public function save_post($post_id)
314
- {
315
- // do not process revisions, autosaves and auto-drafts
316
- if(wp_is_post_revision($post_id) || wp_is_post_autosave($post_id) || get_post_status($post_id) == 'auto-draft' || isset($_POST['autosave']))
317
- return;
318
-
319
- // do not process if uploader disabled
320
- if(!$this->is_uploader_enabled())
321
- return;
322
-
323
- $content = stripslashes($_POST['post_content']);
324
- $nlcontent = str_replace("\n", '', $content);
325
- $urls = array();
326
- $descs = array();
327
-
328
- // this thingy looks for href instead of alt attributes in images
329
- // seems like we didn't have alts before
330
- // it's sort of legacy code that must be dropped at some point
331
- // @deprecated
332
- if(preg_match_all('/<div[^>]+zemanta-img[^>]+>.+?<\/div>/', $nlcontent, $matches))
333
- {
334
- foreach($matches[0] as $str)
335
- {
336
- if(preg_match('/src="([^"]+)"/', $str, $srcurl))
337
- {
338
- if(preg_match('/href="([^"]+)"/', $str, $desc))
339
- $descs[] = $desc[1];
340
- else
341
- $descs[] = '';
342
-
343
- $urls[] = $srcurl[1];
344
- }
345
- }
346
- }
347
-
348
- // this code looks for all images in the post
349
- // extracts alt and src attributes for image downloader
350
- if(preg_match_all('/<img .*?src="[^"]+".*?>/', $nlcontent, $matches))
351
- {
352
- foreach($matches[0] as $str)
353
- {
354
- if(preg_match('/src="([^"]+)"/', $str, $srcurl))
355
- {
356
- if(!in_array($srcurl[1], $urls))
357
- {
358
- if(preg_match('/alt="([^"]+)"/', $str, $desc))
359
- $descs[] = strlen($desc[1]) ? $desc[1] : $srcurl[1];
360
- else
361
- $descs[] = $srcurl[1];
362
-
363
- $urls[] = $srcurl[1];
364
- }
365
- }
366
- }
367
- }
368
-
369
- // do not do anything if there no images found in the post
370
- if(empty($urls))
371
- return;
372
-
373
- // download images to blog and replace external URLs with local
374
- for($i = 0, $c = sizeof($urls); $i < $c; $i++)
375
- {
376
- $url = $urls[$i];
377
- $desc = $descs[$i];
378
-
379
- // skip images from img.zemanta.com and FMP
380
- if(strpos($url, 'http://img.zemanta.com/') !== false || preg_match('#https?://.+\.fmpub\.net/#i', $url))
381
- continue;
382
-
383
- // skip if already hosted on our blog
384
- if(strpos($url, get_bloginfo('url')) !== false) {
385
- continue;
386
- }
387
- // upload image from URL and replace URL in the post content
388
- $localurl = $this->sideload_image($url, $post_id, $desc);
389
- if(!is_wp_error($localurl) && !empty($localurl))
390
- $content = str_replace($url, $localurl, $content);
391
- }
392
-
393
- // unhook this function so it doesn't loop infinitely
394
- remove_action('save_post', array($this, 'save_post'), 20);
395
-
396
- // put modified content back to _POST so other plugins can reuse it
397
- $_POST['post_content'] = addslashes($content);
398
-
399
- // update post in database
400
- wp_update_post(array(
401
- 'ID' => $post_id,
402
- 'post_content' => $content)
403
- );
404
-
405
- // re-hook this function
406
- add_action('save_post', array($this, 'save_post'), 20);
407
- }
408
-
409
- /**
410
- * api
411
- *
412
- * API Call
413
- *
414
- * @param array $arguments Arguments to pass to the API
415
- */
416
- public function api($arguments)
417
- {
418
- $arguments = array_merge($arguments, array(
419
- 'api_key'=> $this->api_key
420
- ));
421
-
422
- if (!isset($arguments['format']))
423
- {
424
- $arguments['format'] = 'xml';
425
- }
426
-
427
- return wp_remote_post($this->api_url, array('method' => 'POST', 'body' => $arguments));
428
- }
429
-
430
- /**
431
- * ajax_error
432
- *
433
- * Helper function to throw WP_Errors to ajax as json
434
- */
435
- public function ajax_error($wp_error) {
436
- if(is_wp_error($wp_error)) {
437
- die(json_encode(array(
438
- 'error' => array(
439
- 'code' => $wp_error->get_error_code(),
440
- 'message' => $wp_error->get_error_message(),
441
- 'data' => $wp_error->get_error_data()
442
- )
443
- )));
444
- }
445
- }
446
-
447
- /**
448
- * ajax_zemanta_set_featured_image
449
- *
450
- * Download and set featured image by URL
451
- * @require WordPress 3.1+
452
- */
453
- public function ajax_zemanta_set_featured_image()
454
- {
455
- global $post_ID;
456
-
457
- if(!isset($this->supported_features['featured_image'])) {
458
- $this->ajax_error(new WP_Error(4, __('Featured image feature is not supported on current platform.', 'zemanta')));
459
- }
460
-
461
- $args = wp_parse_args($_REQUEST, array('post_id' => 0, 'image_url' => ''));
462
- extract($args);
463
-
464
- $post_id = (int)$post_id;
465
-
466
- if(!empty($image_url) && $post_id)
467
- {
468
- $http_response = wp_remote_get($image_url, array('timeout' => 10));
469
-
470
- if(!is_wp_error($http_response))
471
- {
472
- $data = wp_remote_retrieve_body($http_response);
473
-
474
- // throw error if there no data
475
- if(empty($data)) {
476
- $this->ajax_error(new WP_Error(5, __('Featured image has invalid data.', 'zemanta')));
477
- }
478
-
479
- $upload = wp_upload_bits(basename($image_url), null, $data);
480
-
481
- if(!is_wp_error($upload) && !$upload['error'])
482
- {
483
- $filename = $upload['file'];
484
- $wp_filetype = wp_check_filetype(basename($filename), null );
485
- $attachment = array(
486
- 'post_mime_type' => $wp_filetype['type'],
487
- 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
488
- 'post_content' => '',
489
- 'post_status' => 'inherit'
490
- );
491
- $attach_id = wp_insert_attachment($attachment, $filename, $post_id);
492
- $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
493
- wp_update_attachment_metadata($attach_id, $attach_data);
494
-
495
- // this is necessary, or _wp_post_thumbnail_html returns broken remove link
496
- $post_ID = $post_id;
497
-
498
- // set_post_thumbnail available only since WordPress 3.1
499
- if(set_post_thumbnail($post_id, $attach_id)) {
500
- die(json_encode(array(
501
- // _wp_post_thumbnail_html is private function but we really need it to behave natively
502
- 'html' => _wp_post_thumbnail_html($attach_id), // call WPSetThumbnailHTML(html) from javascript
503
- 'attach_id' => $attach_id // call WPSetThumbnailID(attach_id) from javascript
504
- ))
505
- );
506
- } else {
507
- $this->ajax_error(new WP_Error(1, __('An unexpected error occurred.', 'zemanta')));
508
- }
509
- } else {
510
- $this->ajax_error(new WP_Error(2, sprintf(__('An upload error occurred: %s', 'zemanta'), $upload->get_error_message())));
511
- }
512
- } else {
513
- $this->ajax_error(new WP_Error(3, sprintf(__('An error occurred while image download: %s', 'zemanta'), $http_response->get_error_message())));
514
- }
515
- }
516
-
517
- die(0);
518
- }
519
-
520
- /**
521
- * fetch_api_key
522
- *
523
- * Get API Key
524
- */
525
- public function fetch_api_key()
526
- {
527
- $response = $this->api(array(
528
- 'method' => 'zemanta.auth.create_user',
529
- 'partner_id' => 'wordpress-wprp'
530
- ));
531
-
532
- if(!is_wp_error($response))
533
- {
534
- if(preg_match('/<status>(.+?)<\/status>/', $response['body'], $matches))
535
- {
536
- if($matches[1] == 'ok' && preg_match('/<apikey>(.+?)<\/apikey>/', $response['body'], $matches))
537
- return $matches[1];
538
- }
539
- }
540
-
541
- return '';
542
- }
543
-
544
- /**
545
- * shim
546
- *
547
- * Adds Shim to Edit Page for Zemanta Plugin
548
- */
549
- public function shim()
550
- {
551
- echo '<div id="zemanta-sidebar"></div>';
552
- }
553
-
554
- /**
555
- * legacy_options
556
- *
557
- * Get Options from Legacy Options if available
558
- */
559
- protected function legacy_options($options)
560
- {
561
- if (empty($this->options))
562
- {
563
- return false;
564
- }
565
-
566
- foreach ($this->options as $option => $details)
567
- {
568
- $old_option = get_option('zemanta_' . $details['field']);
569
-
570
- if ($old_option && !isset($options[$details['field']]))
571
- {
572
- $options[$details['field']] = $old_option == 'on' ? 1 : $old_option;
573
- }
574
- }
575
-
576
- update_option('zemanta_options', $options);
577
-
578
- return get_option('zemanta_options');
579
- }
580
-
581
- /**
582
- * get_option
583
- *
584
- * Get Option
585
- *
586
- * @param string $name Name of option to retrieve
587
- */
588
- protected function get_option($name)
589
- {
590
- $options = get_option('zemanta_options');
591
-
592
- return isset($options[$name]) ? $options[$name] : null;
593
- }
594
-
595
- /**
596
- * set_option
597
- *
598
- * Set option
599
- *
600
- * @param string $name Name of option to set
601
- * @param string $value Value of option
602
- */
603
- protected function set_option($name, $value)
604
- {
605
- $options = get_option('zemanta_options');
606
-
607
- if ($value === null)
608
- {
609
- unset($options[$name]);
610
- }
611
- else
612
- {
613
- $options[$name] = $value;
614
- }
615
-
616
- return update_option('zemanta_options', $options);
617
- }
618
-
619
- /**
620
- * get_api_key
621
- *
622
- * Get API Key
623
- */
624
- public function get_api_key()
625
- {
626
- return $this->get_option('api_key');
627
- }
628
-
629
- /**
630
- * set_api_key
631
- *
632
- * Get API Key
633
- *
634
- * @param string $api_key API Key to set
635
- */
636
- protected function set_api_key($api_key)
637
- {
638
- $this->set_option('api_key', $api_key);
639
- }
640
-
641
- /**
642
- * check_dependencies
643
- *
644
- * Return true if CURL and DOM XML modules exist and false otherwise
645
- *
646
- * @return boolean
647
- */
648
- protected function check_dependencies()
649
- {
650
- return ((function_exists('curl_init') || ini_get('allow_url_fopen')) && (function_exists('preg_match') || function_exists('ereg')));
651
- }
652
-
653
- /**
654
- * check_plugin_updated
655
- *
656
- * Checks whether plugin update happened and triggers update notice
657
- *
658
- */
659
- protected function check_plugin_updated()
660
- {
661
- $last_plugin_version = get_option(WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION);
662
-
663
- // setup current version for new plugin installations
664
- // zemanta_api_key option presents on older 0.8 versions
665
- if(!$last_plugin_version && !get_option('zemanta_api_key')) {
666
- update_option(WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION, $this->version, '', true);
667
- }
668
-
669
- // it'll trigger only if different version of plugin was installed before
670
- if(!$last_plugin_version || version_compare($last_plugin_version, $this->version, '!='))
671
- {
672
- // save new version string to database to avoid event doubling
673
- update_option(WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION, $this->version);
674
- }
675
- }
676
-
677
- /**
678
- * render
679
- *
680
- * Render HTML/JS/CSS to screen
681
- *
682
- * @param string $view File to display
683
- * @param array $arguments Arguments to pass to file
684
- * @param boolean $return Whether or not to return the output or print it
685
- */
686
- protected function render($view, $arguments = array(), $return = false)
687
- {
688
- $view_file = untrailingslashit(dirname(__FILE__)) . '/views/' . $view . '.php';
689
-
690
- extract($arguments, EXTR_SKIP);
691
-
692
- if ($return)
693
- ob_start();
694
-
695
- if(file_exists($view_file))
696
- include($view_file);
697
- else
698
- echo '<pre>View Not Found: ' . $view . '</pre>';
699
-
700
- if ($return)
701
- return ob_get_clean();
702
- }
703
-
704
- }
705
-
706
- //
707
- // End of file zemanta.php
708
- //
 
 
 
1
+ <?php
2
+
3
+ define('WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION', 'zemanta_plugin_version');
4
+ define('WP_RP_ZEMANTA_PLUGIN_FLASH_META', 'zemanta_plugin_flash');
5
+
6
+ if(!class_exists('WP_Http')) {
7
+ require_once(ABSPATH . WPINC . '/class-http.php');
8
+ }
9
+
10
+ require_once(ABSPATH . 'wp-admin/includes/image.php');
11
+
12
+ class WPRPZemanta {
13
+
14
+ var $version = '1.2.3';
15
+ var $api_url = 'http://api.zemanta.com/services/rest/0.0/';
16
+ var $api_key = '';
17
+ var $options = array();
18
+ var $supported_features = array();
19
+ var $update_notes = array();
20
+ var $flash_data = null;
21
+ var $top_menu_slug = null;
22
+
23
+ public function __construct()
24
+ {
25
+ global $wp_version;
26
+
27
+ // initialize update notes shown once on plugin update
28
+ $this->update_notes['1.0.5'] = __('Please double-check your upload paths in Zemanta Settings, we changed some things that might affect your images.', 'zemanta');
29
+ $this->update_notes['1.0.7'] = __('Please double-check your upload paths in Zemanta Settings, we changed some things that might affect your images.', 'zemanta');
30
+ $this->update_notes['1.0.8'] = __('Please double-check your upload paths in Zemanta Settings, we changed some things that might affect your images.', 'zemanta');
31
+
32
+ add_action('admin_init', array($this, 'init'));
33
+ add_action('admin_init', array($this, 'register_options'));
34
+
35
+ register_activation_hook(dirname(__FILE__) . '/zemanta.php', array($this, 'activate'));
36
+
37
+ $this->supported_features['featured_image'] = version_compare($wp_version, '3.1', '>=') >= 0;
38
+ }
39
+
40
+ /**
41
+ * admin_init
42
+ *
43
+ * Initialize plugin
44
+ *
45
+ */
46
+ public function init() {
47
+ if (defined('ZEMANTA_PLUGIN_VERSION_OPTION')) { // Make sure this doesn't clash with the Editorial Assistant
48
+ return;
49
+ }
50
+
51
+ add_action('wp_ajax_zemanta_set_featured_image', array($this, 'ajax_zemanta_set_featured_image'));
52
+ add_action('edit_form_advanced', array($this, 'assets'), 1);
53
+ add_action('edit_page_form', array($this, 'assets'), 1);
54
+ add_action('save_post', array($this, 'save_post'), 20);
55
+
56
+ $this->check_plugin_updated();
57
+ $this->create_options();
58
+ $this->check_options();
59
+
60
+ if(!$this->check_dependencies())
61
+ add_action('admin_notices', array($this, 'warning'));
62
+ }
63
+
64
+ /**
65
+ * activate
66
+ *
67
+ * Run any functions needed for plugin activation
68
+ */
69
+ public function activate()
70
+ {
71
+ $this->fix_user_meta();
72
+ }
73
+
74
+ /**
75
+ * admin_head
76
+ *
77
+ * Add any assets to the edit page
78
+ */
79
+ public function assets()
80
+ {
81
+ $this->render('assets', array(
82
+ 'api_key' => $this->api_key,
83
+ 'version' => $this->version,
84
+ 'features' => $this->supported_features
85
+ ));
86
+ }
87
+
88
+ /**
89
+ * warning for no api key
90
+ *
91
+ * Display api key warning
92
+ */
93
+ public function warning_no_api_key()
94
+ {
95
+ $this->render('message', array(
96
+ 'type' => 'error'
97
+ ,'message' => __('You have no Zemanta API key and the plugin was unable to retrieve one. You can still use Zemanta, '.
98
+ 'but until the new key is successfully obtained you will not be able to customize the widget or remove '.
99
+ 'this warning. You may try to deactivate and activate the plugin again to make it retry to obtain the key.', 'zemanta')
100
+ ));
101
+ }
102
+
103
+ /**
104
+ * warning
105
+ *
106
+ * Display plugin warning
107
+ */
108
+ public function warning()
109
+ {
110
+ $this->render('message', array(
111
+ 'type' => 'updated fade'
112
+ ,'message' => __('Zemanta needs either the cURL PHP module or allow_url_fopen enabled to work. Please ask your server administrator to set either of these up.', 'zemanta')
113
+ ));
114
+ }
115
+
116
+ /**
117
+ * add_options
118
+ *
119
+ * Add configuration page to menu
120
+ */
121
+ public function add_options()
122
+ {
123
+ $this->top_menu_slug = add_menu_page(
124
+ __('Zemanta', 'zemanta'),
125
+ __('Zemanta', 'zemanta'),
126
+ 'manage_options', 'zemanta',
127
+ array($this, 'options'),
128
+ plugins_url('/img/menu_icon.png', __FILE__)
129
+ );
130
+ }
131
+
132
+ /**
133
+ * check_options
134
+ *
135
+ * Check to see if we need to create or import options
136
+ */
137
+ public function check_options()
138
+ {
139
+ $this->api_key = $this->get_api_key();
140
+
141
+ if (!$this->api_key)
142
+ {
143
+ $options = get_option('zemanta_options');
144
+
145
+ if (!$options)
146
+ {
147
+ $options = $this->legacy_options($options);
148
+ }
149
+
150
+ $this->api_key = $this->get_api_key();
151
+ if (!$this->api_key)
152
+ {
153
+ $this->api_key = $this->fetch_api_key();
154
+ if ($this->api_key)
155
+ {
156
+ $this->set_api_key($this->api_key);
157
+ }
158
+ else
159
+ {
160
+ add_action('admin_notices', array($this, 'warning_no_api_key'));
161
+ }
162
+ }
163
+ }
164
+ }
165
+
166
+ /**
167
+ * create_options
168
+ *
169
+ * Create the Initial Options
170
+ */
171
+ public function create_options()
172
+ {
173
+ $wp_upload_dir = wp_upload_dir();
174
+ $options = array(
175
+ 'zemanta_option_api_key' => array(
176
+ 'type' => 'apikey'
177
+ ,'title' => __('Your API key (in case you need to contact support)', 'zemanta')
178
+ ,'field' => 'api_key'
179
+ ,'default_value' => $this->api_key
180
+ )
181
+ ,'zemanta_option_image_upload' => array(
182
+ 'type' => 'checkbox'
183
+ ,'title' => __('Automatically upload inserted images to your blog', 'zemanta')
184
+ ,'field' => 'image_uploader'
185
+ //,'description' => __('Using Zemanta image uploader in this way may download copyrighted images to your blog. Make sure you and your blog writers check and understand licenses of each and every image before using them in your blog posts and delete them if they infringe on author\'s rights.')
186
+ )
187
+ );
188
+
189
+ $this->options = apply_filters('zemanta_options', $options);
190
+ }
191
+
192
+ /**
193
+ * register_options
194
+ *
195
+ * Register options with Settings API
196
+ */
197
+ public function register_options()
198
+ {
199
+ register_setting('zemanta_options', 'zemanta_options', array($this, 'validate_options'));
200
+
201
+ add_settings_section('zemanta_options_plugin', null, array($this, 'callback_options_dummy'), 'zemanta');
202
+ add_settings_field('zemanta_option_api_key', 'Your API key', array($this, 'options_set'), 'zemanta', 'zemanta_options_plugin', $this->options['zemanta_option_api_key']);
203
+
204
+ add_settings_section('zemanta_options_image', null, array($this, 'callback_options_dummy'), 'zemanta');
205
+ add_settings_field('zemanta_option_image_upload', 'Enable image uploader', array($this, 'options_set'), 'zemanta', 'zemanta_options_image', $this->options['zemanta_option_image_upload']);
206
+ }
207
+
208
+ /**
209
+ * callback_options_dummy
210
+ *
211
+ * Dummy callback for add_settings_sections
212
+ */
213
+ public function callback_options_dummy()
214
+ {
215
+ }
216
+
217
+ /**
218
+ * options_set
219
+ *
220
+ * Output the fields for the options
221
+ */
222
+ public function options_set($option = null)
223
+ {
224
+ // WordPress < 2.9 has a bug where the settings callback is not passed the arguments value so we check for it here.
225
+ if ($option == null)
226
+ {
227
+ $option = array_shift($this->options);
228
+ }
229
+
230
+ $this->render('options-input-' . $option['type'], array(
231
+ 'option' => $this->get_option($option['field']),
232
+ 'field' => $option['field'],
233
+ 'title' => isset($option['title']) ? $option['title'] : null,
234
+ 'default_value' => isset($option['default_value']) ? $option['default_value'] : null,
235
+ 'description' => isset($option['description']) ? $option['description'] : null
236
+ ));
237
+ }
238
+
239
+ /**
240
+ * validate_options
241
+ *
242
+ * Handle input Validation
243
+ */
244
+ public function validate_options($input) {
245
+ return $input;
246
+ }
247
+
248
+ /**
249
+ * options
250
+ *
251
+ * Add configuration page
252
+ */
253
+ public function options()
254
+ {
255
+ if(!$this->api_key)
256
+ {
257
+ $this->api_key = $this->fetch_api_key();
258
+ $this->set_option('api_key', $this->api_key);
259
+ }
260
+
261
+ $this->render('options', array(
262
+ 'api_key' => $this->api_key
263
+ ));
264
+ }
265
+
266
+ /**
267
+ * sideload_image
268
+ *
269
+ * New image uploader, this is slightly modified version of media_sideload_image from wp-admin/includes/media.php
270
+ *
271
+ * @param string $file the URL of the image to download
272
+ * @param int $post_id The post ID the media is to be associated with
273
+ * @param string $desc Optional. Description of the image
274
+ * @return string|WP_Error uploaded image URL on success
275
+ */
276
+ public function sideload_image($file, $post_id, $desc = null) {
277
+ $tmp = download_url($file);
278
+ preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
279
+
280
+ $file_array = array(
281
+ // sometimes wikipedia images have % in file names
282
+ // so let's fix it to avoid URL encoding conflicts
283
+ 'name' => str_replace('%', '_', basename($matches[0])),
284
+ 'tmp_name' => $tmp
285
+ );
286
+
287
+ // If error storing temporarily, unlink
288
+ if(is_wp_error($tmp)) {
289
+ @unlink($file_array['tmp_name']);
290
+ $file_array['tmp_name'] = '';
291
+ }
292
+
293
+ $id = media_handle_sideload($file_array, $post_id, $desc);
294
+ if(is_wp_error($id)) {
295
+ @unlink($file_array['tmp_name']);
296
+ return $id;
297
+ }
298
+
299
+ return wp_get_attachment_url($id);
300
+ }
301
+
302
+ /**
303
+ * is_uploader_enabled
304
+ *
305
+ */
306
+ public function is_uploader_enabled()
307
+ {
308
+ return $this->get_option('image_uploader');
309
+ }
310
+
311
+ /**
312
+ * save_post
313
+ *
314
+ * Download images if necessary and update post
315
+ */
316
+ public function save_post($post_id)
317
+ {
318
+ // do not process revisions, autosaves and auto-drafts
319
+ if(wp_is_post_revision($post_id) || wp_is_post_autosave($post_id) || get_post_status($post_id) == 'auto-draft' || isset($_POST['autosave']))
320
+ return;
321
+
322
+ // do not process if uploader disabled
323
+ if(!$this->is_uploader_enabled())
324
+ return;
325
+
326
+ $content = stripslashes($_POST['post_content']);
327
+ $nlcontent = str_replace("\n", '', $content);
328
+ $urls = array();
329
+ $descs = array();
330
+
331
+ // this thingy looks for href instead of alt attributes in images
332
+ // seems like we didn't have alts before
333
+ // it's sort of legacy code that must be dropped at some point
334
+ // @deprecated
335
+ if(preg_match_all('/<div[^>]+zemanta-img[^>]+>.+?<\/div>/', $nlcontent, $matches))
336
+ {
337
+ foreach($matches[0] as $str)
338
+ {
339
+ if(preg_match('/src="([^"]+)"/', $str, $srcurl))
340
+ {
341
+ if(preg_match('/href="([^"]+)"/', $str, $desc))
342
+ $descs[] = $desc[1];
343
+ else
344
+ $descs[] = '';
345
+
346
+ $urls[] = $srcurl[1];
347
+ }
348
+ }
349
+ }
350
+
351
+ // this code looks for all images in the post
352
+ // extracts alt and src attributes for image downloader
353
+ if(preg_match_all('/<img .*?src="[^"]+".*?>/', $nlcontent, $matches))
354
+ {
355
+ foreach($matches[0] as $str)
356
+ {
357
+ if(preg_match('/src="([^"]+)"/', $str, $srcurl))
358
+ {
359
+ if(!in_array($srcurl[1], $urls))
360
+ {
361
+ if(preg_match('/alt="([^"]+)"/', $str, $desc))
362
+ $descs[] = strlen($desc[1]) ? $desc[1] : $srcurl[1];
363
+ else
364
+ $descs[] = $srcurl[1];
365
+
366
+ $urls[] = $srcurl[1];
367
+ }
368
+ }
369
+ }
370
+ }
371
+
372
+ // do not do anything if there no images found in the post
373
+ if(empty($urls))
374
+ return;
375
+
376
+ // download images to blog and replace external URLs with local
377
+ for($i = 0, $c = sizeof($urls); $i < $c; $i++)
378
+ {
379
+ $url = $urls[$i];
380
+ $desc = $descs[$i];
381
+
382
+ // skip images from img.zemanta.com and FMP
383
+ if(strpos($url, 'http://img.zemanta.com/') !== false || preg_match('#https?://.+\.fmpub\.net/#i', $url))
384
+ continue;
385
+
386
+ // skip if already hosted on our blog
387
+ if(strpos($url, get_bloginfo('url')) !== false) {
388
+ continue;
389
+ }
390
+ // upload image from URL and replace URL in the post content
391
+ $localurl = $this->sideload_image($url, $post_id, $desc);
392
+ if(!is_wp_error($localurl) && !empty($localurl))
393
+ $content = str_replace($url, $localurl, $content);
394
+ }
395
+
396
+ // unhook this function so it doesn't loop infinitely
397
+ remove_action('save_post', array($this, 'save_post'), 20);
398
+
399
+ // put modified content back to _POST so other plugins can reuse it
400
+ $_POST['post_content'] = addslashes($content);
401
+
402
+ // update post in database
403
+ wp_update_post(array(
404
+ 'ID' => $post_id,
405
+ 'post_content' => $content)
406
+ );
407
+
408
+ // re-hook this function
409
+ add_action('save_post', array($this, 'save_post'), 20);
410
+ }
411
+
412
+ /**
413
+ * api
414
+ *
415
+ * API Call
416
+ *
417
+ * @param array $arguments Arguments to pass to the API
418
+ */
419
+ public function api($arguments)
420
+ {
421
+ $arguments = array_merge($arguments, array(
422
+ 'api_key'=> $this->api_key
423
+ ));
424
+
425
+ if (!isset($arguments['format']))
426
+ {
427
+ $arguments['format'] = 'xml';
428
+ }
429
+
430
+ return wp_remote_post($this->api_url, array('method' => 'POST', 'body' => $arguments));
431
+ }
432
+
433
+ /**
434
+ * ajax_error
435
+ *
436
+ * Helper function to throw WP_Errors to ajax as json
437
+ */
438
+ public function ajax_error($wp_error) {
439
+ if(is_wp_error($wp_error)) {
440
+ die(json_encode(array(
441
+ 'error' => array(
442
+ 'code' => $wp_error->get_error_code(),
443
+ 'message' => $wp_error->get_error_message(),
444
+ 'data' => $wp_error->get_error_data()
445
+ )
446
+ )));
447
+ }
448
+ }
449
+
450
+ /**
451
+ * ajax_zemanta_set_featured_image
452
+ *
453
+ * Download and set featured image by URL
454
+ * @require WordPress 3.1+
455
+ */
456
+ public function ajax_zemanta_set_featured_image()
457
+ {
458
+ global $post_ID;
459
+
460
+ if(!isset($this->supported_features['featured_image'])) {
461
+ $this->ajax_error(new WP_Error(4, __('Featured image feature is not supported on current platform.', 'zemanta')));
462
+ }
463
+
464
+ $args = wp_parse_args($_REQUEST, array('post_id' => 0, 'image_url' => ''));
465
+ extract($args);
466
+
467
+ $post_id = (int)$post_id;
468
+
469
+ if(!empty($image_url) && $post_id)
470
+ {
471
+ $http_response = wp_remote_get($image_url, array('timeout' => 10));
472
+
473
+ if(!is_wp_error($http_response))
474
+ {
475
+ $data = wp_remote_retrieve_body($http_response);
476
+
477
+ // throw error if there no data
478
+ if(empty($data)) {
479
+ $this->ajax_error(new WP_Error(5, __('Featured image has invalid data.', 'zemanta')));
480
+ }
481
+
482
+ $upload = wp_upload_bits(basename($image_url), null, $data);
483
+
484
+ if(!is_wp_error($upload) && !$upload['error'])
485
+ {
486
+ $filename = $upload['file'];
487
+ $wp_filetype = wp_check_filetype(basename($filename), null );
488
+ $attachment = array(
489
+ 'post_mime_type' => $wp_filetype['type'],
490
+ 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
491
+ 'post_content' => '',
492
+ 'post_status' => 'inherit'
493
+ );
494
+ $attach_id = wp_insert_attachment($attachment, $filename, $post_id);
495
+ $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
496
+ wp_update_attachment_metadata($attach_id, $attach_data);
497
+
498
+ // this is necessary, or _wp_post_thumbnail_html returns broken remove link
499
+ $post_ID = $post_id;
500
+
501
+ // set_post_thumbnail available only since WordPress 3.1
502
+ if(set_post_thumbnail($post_id, $attach_id)) {
503
+ die(json_encode(array(
504
+ // _wp_post_thumbnail_html is private function but we really need it to behave natively
505
+ 'html' => _wp_post_thumbnail_html($attach_id), // call WPSetThumbnailHTML(html) from javascript
506
+ 'attach_id' => $attach_id // call WPSetThumbnailID(attach_id) from javascript
507
+ ))
508
+ );
509
+ } else {
510
+ $this->ajax_error(new WP_Error(1, __('An unexpected error occurred.', 'zemanta')));
511
+ }
512
+ } else {
513
+ $this->ajax_error(new WP_Error(2, sprintf(__('An upload error occurred: %s', 'zemanta'), $upload->get_error_message())));
514
+ }
515
+ } else {
516
+ $this->ajax_error(new WP_Error(3, sprintf(__('An error occurred while image download: %s', 'zemanta'), $http_response->get_error_message())));
517
+ }
518
+ }
519
+
520
+ die(0);
521
+ }
522
+
523
+ /**
524
+ * fetch_api_key
525
+ *
526
+ * Get API Key
527
+ */
528
+ public function fetch_api_key()
529
+ {
530
+ $response = $this->api(array(
531
+ 'method' => 'zemanta.auth.create_user',
532
+ 'partner_id' => 'wordpress-wprp'
533
+ ));
534
+
535
+ if(!is_wp_error($response))
536
+ {
537
+ if(preg_match('/<status>(.+?)<\/status>/', $response['body'], $matches))
538
+ {
539
+ if($matches[1] == 'ok' && preg_match('/<apikey>(.+?)<\/apikey>/', $response['body'], $matches))
540
+ return $matches[1];
541
+ }
542
+ }
543
+
544
+ return '';
545
+ }
546
+
547
+ /**
548
+ * shim
549
+ *
550
+ * Adds Shim to Edit Page for Zemanta Plugin
551
+ */
552
+ public function shim()
553
+ {
554
+ echo '<div id="zemanta-sidebar"></div>';
555
+ }
556
+
557
+ /**
558
+ * legacy_options
559
+ *
560
+ * Get Options from Legacy Options if available
561
+ */
562
+ protected function legacy_options($options)
563
+ {
564
+ if (empty($this->options))
565
+ {
566
+ return false;
567
+ }
568
+
569
+ foreach ($this->options as $option => $details)
570
+ {
571
+ $old_option = get_option('zemanta_' . $details['field']);
572
+
573
+ if ($old_option && !isset($options[$details['field']]))
574
+ {
575
+ $options[$details['field']] = $old_option == 'on' ? 1 : $old_option;
576
+ }
577
+ }
578
+
579
+ update_option('zemanta_options', $options);
580
+
581
+ return get_option('zemanta_options');
582
+ }
583
+
584
+ /**
585
+ * get_option
586
+ *
587
+ * Get Option
588
+ *
589
+ * @param string $name Name of option to retrieve
590
+ */
591
+ protected function get_option($name)
592
+ {
593
+ $options = get_option('zemanta_options');
594
+
595
+ return isset($options[$name]) ? $options[$name] : null;
596
+ }
597
+
598
+ /**
599
+ * set_option
600
+ *
601
+ * Set option
602
+ *
603
+ * @param string $name Name of option to set
604
+ * @param string $value Value of option
605
+ */
606
+ protected function set_option($name, $value)
607
+ {
608
+ $options = get_option('zemanta_options');
609
+
610
+ if ($value === null)
611
+ {
612
+ unset($options[$name]);
613
+ }
614
+ else
615
+ {
616
+ $options[$name] = $value;
617
+ }
618
+
619
+ return update_option('zemanta_options', $options);
620
+ }
621
+
622
+ /**
623
+ * get_api_key
624
+ *
625
+ * Get API Key
626
+ */
627
+ public function get_api_key()
628
+ {
629
+ return $this->get_option('api_key');
630
+ }
631
+
632
+ /**
633
+ * set_api_key
634
+ *
635
+ * Get API Key
636
+ *
637
+ * @param string $api_key API Key to set
638
+ */
639
+ protected function set_api_key($api_key)
640
+ {
641
+ $this->set_option('api_key', $api_key);
642
+ }
643
+
644
+ /**
645
+ * check_dependencies
646
+ *
647
+ * Return true if CURL and DOM XML modules exist and false otherwise
648
+ *
649
+ * @return boolean
650
+ */
651
+ protected function check_dependencies()
652
+ {
653
+ return ((function_exists('curl_init') || ini_get('allow_url_fopen')) && (function_exists('preg_match') || function_exists('ereg')));
654
+ }
655
+
656
+ /**
657
+ * check_plugin_updated
658
+ *
659
+ * Checks whether plugin update happened and triggers update notice
660
+ *
661
+ */
662
+ protected function check_plugin_updated()
663
+ {
664
+ $last_plugin_version = get_option(WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION);
665
+
666
+ // setup current version for new plugin installations
667
+ // zemanta_api_key option presents on older 0.8 versions
668
+ if(!$last_plugin_version && !get_option('zemanta_api_key')) {
669
+ update_option(WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION, $this->version, '', true);
670
+ }
671
+
672
+ // it'll trigger only if different version of plugin was installed before
673
+ if(!$last_plugin_version || version_compare($last_plugin_version, $this->version, '!='))
674
+ {
675
+ // save new version string to database to avoid event doubling
676
+ update_option(WP_RP_ZEMANTA_PLUGIN_VERSION_OPTION, $this->version);
677
+ }
678
+ }
679
+
680
+ /**
681
+ * render
682
+ *
683
+ * Render HTML/JS/CSS to screen
684
+ *
685
+ * @param string $view File to display
686
+ * @param array $arguments Arguments to pass to file
687
+ * @param boolean $return Whether or not to return the output or print it
688
+ */
689
+ protected function render($view, $arguments = array(), $return = false)
690
+ {
691
+ $view_file = untrailingslashit(dirname(__FILE__)) . '/views/' . $view . '.php';
692
+
693
+ extract($arguments, EXTR_SKIP);
694
+
695
+ if ($return)
696
+ ob_start();
697
+
698
+ if(file_exists($view_file))
699
+ include($view_file);
700
+ else
701
+ echo '<pre>View Not Found: ' . $view . '</pre>';
702
+
703
+ if ($return)
704
+ return ob_get_clean();
705
+ }
706
+
707
+ }
708
+
709
+ //
710
+ // End of file zemanta.php
711
+ //