1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
|
<?php
if ( ! class_exists( 'WMobilePack_Formatter' ) ) {
require_once(WMP_PLUGIN_PATH.'inc/class-wmp-formatter.php');
}
if ( ! class_exists( 'WMobilePack_Export' ) ) {
/**
* Class WMobilePack_Export
*
* Contains different methods for exporting categories, articles and comments
*
* @improvement - Move this class to the frontend folder (similar to the PRO version)?
*
* @todo (Future releases) Remove category_id and category_name from the exports after apps have been modified to use multiple categories per post
*
*/
class WMobilePack_Export
{
/* ----------------------------------*/
/* Attributes */
/* ----------------------------------*/
public $purifier;
private $inactive_categories = array();
private $inactive_pages = array();
/* ----------------------------------*/
/* Methods */
/* ----------------------------------*/
/**
*
* Init purifier, inactive categories and pages properties
*
*/
public function __construct()
{
$this->purifier = WMobilePack_Formatter::init_purifier();
$this->inactive_categories = WMobilePack_Options::get_setting('inactive_categories');
$this->inactive_pages = WMobilePack_Options::get_setting('inactive_pages');
}
/**
*
* Create an uploads management object and return it
*
* @return object
*
*/
protected function get_uploads_manager()
{
return new WMobilePack_Uploads();
}
/**
*
* Verify if a post has a featured image and return it
*
* @param $post_id
* @return array
*/
protected function get_post_image($post_id)
{
if (has_post_thumbnail($post_id)) {
$post_thumbnail_id = get_post_thumbnail_id($post_id);
$full_url = wp_get_attachment_url($post_thumbnail_id);
$image_metadata = wp_get_attachment_metadata($post_thumbnail_id, true);
if ($full_url && is_array($image_metadata) && !empty($image_metadata)) {
if (isset($image_metadata['sizes']) && is_array($image_metadata['sizes'])){
$thumbnail = false;
// get the first image size that fits the format
foreach (array('medium_large', 'large') as $size_option){
if ( isset($image_metadata['sizes'][$size_option]) ){
$thumbnail = $image_metadata['sizes'][$size_option];
break;
}
}
if ($thumbnail !== false && isset($thumbnail['file']) && isset($thumbnail['width']) && isset($thumbnail['height'])) {
return array(
"src" => str_replace(basename($full_url), $thumbnail['file'], $full_url),
"width" => $thumbnail['width'],
"height" => $thumbnail['height']
);
}
}
if (isset($image_metadata['width']) && isset($image_metadata['height'])) {
return array(
"src" => $full_url,
"width" => $image_metadata['width'],
"height" => $image_metadata['height']
);
}
}
}
return array();
}
/**
*
* Compose array with a post's details for a posts list
*
* @param $post
* @return array
*
*/
protected function format_post_short($post)
{
// check if the post has a post thumbnail assigned to it and save it in an array
$image_details = $this->get_post_image($post->ID);
// Build post array - get_the_title(), get_permalink() methods can be used inside or outside of The Loop.
// If used outside the loop an ID must be specified.
$arr_article = array(
'id' => $post->ID,
"title" => get_the_title(),
"author" => get_the_author_meta('display_name'),
"link" => get_permalink(),
"image" => !empty($image_details) ? $image_details : "",
"date" => WMobilePack_Formatter::format_date(strtotime($post->post_date)),
"timestamp" => strtotime($post->post_date),
"description" => apply_filters('the_excerpt', get_the_excerpt()),
"content" => '',
"categories" => $this->get_visible_categories_ids($post)
);
return $arr_article;
}
/**
*
* Compose array with a post's details and full content for the post details page
*
* @param $post
* @return array
*
* @todo Generated description is different from the format_post_short() method, unify them or remove description field.
*
*/
protected function format_post_full($post)
{
// check if the post has a post thumbnail assigned to it and save it in an array
$image_details = $this->get_post_image($post->ID);
// Build post array - get_the_title(), get_permalink() methods can be used inside or outside of The Loop.
// If used outside the loop an ID must be specified.
// get & filter content
$content = apply_filters("the_content", $post->post_content);
// remove script tags
$content = WMobilePack_Formatter::remove_script_tags($content);
$content = $this->purifier->purify($content);
// remove all urls from attachment images
$content = preg_replace(array('{<a(.*?)(wp-att|wp-content\/uploads|attachment)[^>]*><img}', '{ wp-image-[0-9]*" /></a>}'), array('<img', '" />'), $content);
// check if the post has a manually edited excerpt, otherwise create an excerpt from the content
if (has_excerpt($post->ID)) {
$description = $this->purifier->purify($post->post_excerpt);
} else {
$description = WMobilePack_Formatter::truncate_html(strip_tags($content), 100, '...', false, false);
$description = apply_filters('the_excerpt', $description);
}
$avatar = "";
$get_avatar = get_avatar($post->post_author, 50);
preg_match("/src='(.*?)'/i", $get_avatar, $matches);
if (isset($matches[1])) {
$avatar = $matches[1];
}
$arr_article = array(
'id' => $post->ID,
"title" => get_the_title($post->ID),
"author" => get_the_author_meta('display_name', $post->post_author),
"author_description" => get_the_author_meta( 'description', $post->post_author ),
"author_avatar" => $avatar,
"link" => get_permalink($post->ID),
"image" => !empty($image_details) ? $image_details : "",
"date" => WMobilePack_Formatter::format_date(strtotime($post->post_date)),
"timestamp" => strtotime($post->post_date),
"description" => $description,
"content" => $content,
"categories" => $this->get_visible_categories_ids($post)
);
return $arr_article;
}
/**
*
* If 'inactive_categories' has been set, return an array with only the active categories ids.
* Otherwise, return false.
*
* @return array|bool
*
*/
protected function get_active_categories(){
// build array with the active categories ids
$active_categories_ids = false;
// check if we must limit search to some categories ids
if (count($this->inactive_categories) > 0) {
// read all categories
$categories = get_categories(array('hierarchical' => 0));
$active_categories_ids = array();
foreach ($categories as $category) {
if (!in_array($category->cat_ID, $this->inactive_categories))
$active_categories_ids[] = $category->cat_ID;
}
}
return $active_categories_ids;
}
/**
*
* Order response categories array
*
* @param $arr_categories
* @return array
*
*/
protected function order_categories($arr_categories)
{
// build array with the ordered categories
$arr_ordered_categories = array();
if (!empty($arr_categories)) {
// check if the categories were ordered from the admin panel
$order_categories = WMobilePack_Options::get_setting('ordered_categories');
// check if we have a latest category (should be the first one to appear)
$has_latest = 0;
if (isset($arr_categories[0])) {
// set order for the latest category and add it in the list
$arr_categories[0]['order'] = 1;
$has_latest = 1;
$arr_ordered_categories[] = $arr_categories[0];
}
// if the categories have been ordered
if (!empty($order_categories)) {
// last ordered used for a category
$last_order = 1;
foreach ($order_categories as $category_id) {
// inactive categories & latest will be skipped
if (array_key_exists($category_id, $arr_categories)) {
// set the order for the category and add it in the list
$arr_categories[$category_id]['order'] = $last_order + $has_latest;
$arr_ordered_categories[] = $arr_categories[$category_id];
$last_order++;
}
}
foreach ($arr_categories as $key => $category) {
if ($category['order'] === false) {
$arr_categories[$key]['order'] = $last_order + $has_latest;
$arr_ordered_categories[] = $arr_categories[$key];
$last_order++;
}
}
} else {
// the categories were not ordered from the admin panel, so just init the order field for each
// last order used for a category
$last_order = 1;
// set order for all the categories besides latest
foreach ($arr_categories as $key => $category) {
if ($category['id'] != 0) {
// set the order for the category and add it in the list
$arr_categories[$key]['order'] = $last_order + $has_latest;
$arr_ordered_categories[] = $arr_categories[$key];
$last_order++;
}
}
}
}
return $arr_ordered_categories;
}
/**
* Returns a post's visible category.
* If the post doesn't belong to any visible categories, returns false.
*
* @param $post
* @return null or category object
*/
protected function get_visible_category($post)
{
// get post categories
$categories = get_the_category($post->ID);
// check if at least one of the categories is visible
$visible_category = null;
foreach ($categories as $category) {
if (!in_array($category->cat_ID, $this->inactive_categories)) {
$visible_category = clone $category;
}
}
return $visible_category;
}
/**
* Parse the 'categories_details' array and return an array with icon paths, indexed by category id.
* The method checks if an icon exists before adding it in the array.
*
* @return array
*/
protected function get_categories_images(){
$categories_images = array();
$categories_details = WMobilePack_Options::get_setting('categories_details');
// create an uploads manager object
$WMP_Uploads = $this->get_uploads_manager();
if (is_array($categories_details) && !empty($categories_details)) {
foreach ($categories_details as $category_id => $category_details){
if (is_array($category_details) && array_key_exists('icon', $category_details)) {
$icon_path = $category_details['icon'];
if ($icon_path != ''){
$icon_path = $WMP_Uploads->get_file_url($icon_path);
}
if ($icon_path != ''){
// categories icons are used as backgrounds,
// so we can use the default width / height in the exports
$categories_images[$category_id] = array(
'src' => $icon_path,
'width' => WMobilePack_Uploads::$allowed_files['category_icon']['max_width'],
'height' => WMobilePack_Uploads::$allowed_files['category_icon']['max_height']
);
}
}
}
}
return $categories_images;
}
/**
* Returns a post's visible categories ids.
*
* @param $post
* @return array
*/
protected function get_visible_categories_ids($post)
{
// get post categories
$categories = get_the_category($post->ID);
// check if at least one of the categories is visible
$arr_categories_ids = array();
foreach ($categories as $category) {
if (!in_array($category->cat_ID, $this->inactive_categories)) {
$arr_categories_ids[] = $category->cat_ID;
}
}
return $arr_categories_ids;
}
/**
*
* The comment_closed method is used to determine the comment status for an article.
* The method returns 'open' if the users can comment and 'closed' otherwise.
*
* @param $post
* @return string
*
*/
protected function comment_closed($post)
{
// set initial status for comments
if ($post->comment_status == 'open' && get_option('comment_registration') == 0)
$comment_status = 'open';
else
$comment_status = 'closed';
// if the option close_comments_for_old_posts is not set, return comment status
if (!get_option('close_comments_for_old_posts'))
return $comment_status;
// if the number of old days is not set, return comment_status
$days_old = (int)get_option('close_comments_days_old');
if (!$days_old)
return $comment_status;
/** This filter is documented in wp-includes/comment.php */
$post_types = apply_filters('close_comments_for_post_types', array('post'));
if (!in_array($post->post_type, $post_types))
$comment_status = 'open';
// if the post is older than the number of days set, change comment_status to false
if (time() - strtotime($post->post_date_gmt) > ($days_old * DAY_IN_SECONDS))
$comment_status = 'closed';
// return comment status
return $comment_status;
}
/**
*
* The export_categories method is used for exporting every category with a fixed number of articles.
*
* This method returns a JSON with the following format:
*
* - ex :
* {
* "categories": [
* {
* "id": 0,
* "order": 1,
* "name": "Latest",
* "link": "",
* "image": {
* "src": "{image_path}",
* "width": 480,
* "height": 270
* },
* "parent_id":0,
* "articles": [
* {
* "id": "123456",
* "title": "Post title",
* "timestamp": 1398969000,
* "author": "Author's name",
* "date": "Thu, May 01, 2014 06:30",
* "link": "{post_link}",
* "image": "",
* "description" : "<p>Lorem ipsum sit dolor amet..</p>",
* "content": '',
* "category_id": 3,
* "category_name": "Post category"
* }
* ]
* }
* ]
* }
*
* - The "Latest" category will be composed from all the visible categories and articles.
*
* Receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportcategories'
* - page = (optional) Number of the page to be displayed
* - rows = (optional) Number of rows per page
* - limit = (optional) The number of articles to be added for each category. Default value is 7.
* - withArticles = (optional) Whether the categories will be returned with articles or not.
* Default value is 1 (read articles), any other value will skip over reading the articles.
*/
public function export_categories()
{
$page = false;
if (isset($_GET["page"]) && is_numeric($_GET["page"]))
$page = $_GET["page"];
$rows = false;
if (isset($_GET["rows"]) && is_numeric($_GET["rows"]))
$rows = $_GET["rows"];
if ($page && $rows == false) {
$rows = 5;
} elseif ($rows && $page == false) {
$page = 1;
}
// set default limit
$limit = 7;
if (isset($_GET["limit"]) && is_numeric($_GET["limit"]))
$limit = $_GET["limit"];
$with_articles = 1;
if (isset($_GET["withArticles"]) && is_numeric($_GET["withArticles"]))
$with_articles = $_GET["withArticles"];
// get categories that have posts
$categories = get_categories(array('hide_empty' => 1));
// build array with the active categories ids
$active_categories_ids = array();
foreach ($categories as $category) {
if (!in_array($category->term_id, $this->inactive_categories))
$active_categories_ids[] = $category->term_id;
}
// init categories array
$arr_categories = array();
// remove inline style for the photos types of posts
add_filter('use_default_gallery_style', '__return_false');
if (count($active_categories_ids) > 0) {
$categories_images = $this->get_categories_images();
foreach ($categories as $key => $category) {
if (in_array($category->term_id, $active_categories_ids)) {
$arr_categories[$category->term_id] = array(
'id' => $category->term_id,
'order' => false,
'name' => $category->name,
'name_slug' => $category->slug,
'parent_id' => isset($category->parent) ? $category->parent : 0,
'link' => get_category_link($category->term_id),
'image' => array_key_exists($category->term_id, $categories_images) ? $categories_images[$category->term_id] : ''
);
}
}
}
// activate latest category only if we have at least 2 visible categories
if (count($arr_categories) > 1) {
$arr_categories[0] = array(
'id' => 0,
'order' => false,
'name' => 'Latest',
'name_slug' => 'Latest',
'image' => '',
'parent_id' => 0
);
}
$arr_categories = $this->order_categories($arr_categories);
if ($page && $rows) {
$nr_categories = count($arr_categories);
if ($page > ceil($nr_categories/$rows)) {
return '{"categories":' . json_encode(array()) . ',"page":"' .$page . '","rows":"' .$rows .'"' .',"wpmp":"'.WMP_VERSION.'"}';
}
$start = $rows * ($page-1);
$arr_categories = array_slice($arr_categories, $start, $rows );
}
if ($with_articles == 1) {
foreach ($arr_categories as $key => $arr_category) {
// Reset query & search posts from this category
$posts_query = array(
'numberposts' => $limit,
'posts_per_page' => $limit,
'post_type' => 'post',
'post_status' => 'publish',
'post_password' => ''
);
if ($arr_category['id'] == 0){
// read posts for the latest category (use all active categories)
$posts_query['cat'] = implode(', ', $active_categories_ids);
} else {
$posts_query['category__in'] = $arr_category['id'];
}
$cat_posts_query = new WP_Query($posts_query);
while ($cat_posts_query->have_posts()) {
$cat_posts_query->the_post();
$post = $cat_posts_query->post;
if ($post->post_type == 'post' && $post->post_password == '' && $post->post_status == 'publish') {
// retrieve array with the post's details
$post_details = $this->format_post_short($post);
// if the category doesn't have a featured image yet, use the one from the current post
if (!is_array($arr_categories[$key]["image"]) && !empty($post_details['image'])) {
$arr_categories[$key]["image"] = $post_details['image'];
}
// if this is the first article from the category, create the 'articles' array
if (!isset($arr_categories[$key]["articles"]))
$arr_categories[$key]["articles"] = array();
if ($arr_category['id'] == 0){
// get post category
$visible_category = $this->get_visible_category($post);
if ($visible_category !== null) {
$post_details['category_id'] = $visible_category->term_id;
$post_details['category_name'] = $visible_category->name;
}
} else {
$post_details['category_id'] = $arr_category['id'];
$post_details['category_name'] = $arr_category['name'];
}
// add article in the array
$arr_categories[$key]["articles"][] = $post_details;
}
}
}
foreach ($arr_categories as $key => $arr_category) {
if (!isset($arr_category['articles']) || empty($arr_category['articles'])) {
unset($arr_categories[$key]);
}
}
$arr_categories = array_values($arr_categories);
}
if ($page && $rows) {
return '{"categories":' . json_encode($arr_categories) . ',"page":"' .$page . '","rows":"' .$rows .'"' .',"wpmp":"'.WMP_VERSION.'"}';
} else {
return '{"categories":' . json_encode($arr_categories) . ',"wpmp":"'.WMP_VERSION.'"}';
}
}
/**
*
* The export_category method is used for exporting a category's details without it's articles
*
* This method returns a JSON with the following format:
*
* - ex :
* {
* "category":
* {
* "id":"",
* "name":"",
* "name_slug":"",
* "parent_id":"",
* "link": "",
* "image": {
* "src": "{image_path}",
* "width": 480,
* "height":270
* }
* }
* }
*
*
* Receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportcategory'
* - categoryId = The id of the category we want
*
*/
public function export_category()
{
if (isset($_GET["categoryId"]) && is_numeric($_GET["categoryId"])) {
if ($_GET["categoryId"] == 0){
$arr_category = array(
'id' => 0,
'name' => 'Latest',
'name_slug' => 'Latest',
'image' => ""
);
return '{"category":' . json_encode($arr_category) . '}' ;
}
$the_category = get_term($_GET["categoryId"], 'category');
if ($the_category && !in_array($the_category->term_id, $this->inactive_categories)) {
$category_details = WMobilePack_Options::get_setting('categories_details');
if (is_array($category_details) && !empty($category_details)) {
if (isset($category_details[$the_category->term_id]) &&
is_array($category_details[$the_category->term_id]) &&
array_key_exists('icon', $category_details[$the_category->term_id])) {
$icon_path = $category_details[$the_category->term_id]['icon'];
if ($icon_path != ''){
$WMP_Uploads = $this->get_uploads_manager();
$icon_path = $WMP_Uploads->get_file_url($icon_path);
}
if ($icon_path != ''){
$category_image = array(
'src' => $icon_path,
'width' => WMobilePack_Uploads::$allowed_files['category_icon']['max_width'],
'height' => WMobilePack_Uploads::$allowed_files['category_icon']['max_height']
);
}
}
}
$arr_category = array (
'id' => $the_category->term_id,
'name' => $the_category->name,
'name_slug' => $the_category->slug,
'parent_id' => $the_category->parent,
'link' => get_category_link($the_category->term_id),
'image' => isset($category_image) ? $category_image : ''
);
return '{"category":' . json_encode($arr_category) . '}' ;
}
return '{"error":"Category does not exist"}' ;
}
return '{"error":"Invalid category id"}' ;
}
/**
*
* The export_articles method is used for exporting a number of articles for each category.
*
* The method returns a JSON with the following format:
*
* - ex :
* {
* "articles": [
* {
* "id": "123456",
* "title": "Post title",
* "timestamp": 1398950385,
* "author": "",
* "date": "Thu, May 01, 2014 01:19",
* "link": "{post_link}",
* "image": "",
* "description":"<p>Post content goes here...</p>",
* "content": '',
* "category_id": 5,
* "category_name": "Post category"
* },
* ...
* ]
* }
*
* Receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportarticles'
* - lastTimestamp = (optional) Read articles that were published before this date
* - categoryId = (optional) The category id. Default value is 0 (for the 'Latest' category).
* - limit = (optional) The number of articles to be read from the category. Default value is 7.
*
*/
public function export_articles()
{
// init articles array
$arr_articles = array();
// set last timestamp
$last_timestamp = date("Y-m-d H:i:s");
if (isset($_GET["lastTimestamp"]) && is_numeric($_GET["lastTimestamp"]))
$last_timestamp = date("Y-m-d H:i:s", $_GET["lastTimestamp"]);
// set category id
$category_id = 0;
if (isset($_GET["categoryId"]) && is_numeric($_GET["categoryId"]))
$category_id = $_GET["categoryId"];
// set limit
$limit = 7;
if (isset($_GET["limit"]) && is_numeric($_GET["limit"]))
$limit = $_GET["limit"];
// set args for posts
$args = array(
'date_query' => array('before' => $last_timestamp),
'numberposts' => $limit,
'posts_per_page' => $limit,
'post_status' => 'publish',
'post_password' => ''
);
// if the selected category is active
$is_active_category = false;
// remove inline style for the photos types of posts
add_filter('use_default_gallery_style', '__return_false');
if ($category_id != 0) {
$args["cat"] = $category_id;
// check if this category was not deactivated
if (!in_array($category_id, $this->inactive_categories))
$is_active_category = true;
} else {
// latest category will always be active
$is_active_category = true;
// build array with the active categories ids
$active_categories_ids = $this->get_active_categories();
// if we have to limit the categories, search posts that belong to the active categories
if ($active_categories_ids !== false)
$args["category__in"] = $active_categories_ids;
}
if ($is_active_category) {
$posts_query = new WP_Query($args);
if ($posts_query->have_posts()) {
while ($posts_query->have_posts()) {
$posts_query->the_post();
$post = $posts_query->post;
if ($post->post_type == 'post' && $post->post_password == '' && $post->post_status == 'publish') {
// retrieve array with the post's details
$post_details = $this->format_post_short($post);
// get post category
$category = null;
if ($category_id > 0) {
$category = get_category($category_id);
} else {
// since a post can have many categories and we have set inactive categories,
// search for a category that is active
if ($active_categories_ids !== false) {
$post_categories = wp_get_post_categories($post->ID);
foreach ($post_categories as $post_category_id) {
if (in_array($post_category_id, $active_categories_ids)) {
$category = get_category($post_category_id);
break;
}
}
} else {
// get a random post category
$cat = get_the_category();
$category = $cat[0];
}
}
if ($category !== null) {
$post_details['category_id'] = $category->term_id;
$post_details['category_name'] = $category->name;
$arr_articles[] = $post_details;
}
}
}
}
}
return '{"articles":' . json_encode($arr_articles) . "}";
}
/**
*
* The exportArticle method is used for exporting a single post.
*
* The method returns a JSON with the following format:
*
* - ex :
* {
* "article": {
* "id": "123456",
* "title": "Post title",
* "author": "",
* "author_description":"",
* "author_avatar":"",
* "link": "{post_link}",
* "image": "",
* "date": "Thu, May 01, 2014 04:07",
* "timestamp": 1398960437,
* "description":"<p>The first of the content goes here</p>",
* "content": "<p>The full content goes here</p>",
* "categories":"",
* "category_id": 5,
* "category_name": "Post category"
* "comment_status": "open", (the values can be 'opened' or 'closed')
* "no_comments": 2,
* "show_avatars" : true,
* "require_name_email" : true,
* }
* }
*
*
* Receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportarticle'
* - articleId = The article's id.
*
*/
public function export_article()
{
global $post;
if (isset($_GET["articleId"]) && is_numeric($_GET["articleId"])) {
$post_details = array();
// get post by id
$post = get_post($_GET["articleId"]);
if ($post != null && $post->post_type == 'post' && $post->post_password == '' && $post->post_status == 'publish') {
// check if at least one of the post's categories is visible
$visible_category = $this->get_visible_category($post);
if ($visible_category !== null) {
$post_details = $this->format_post_full($post);
// add category data
$post_details['category_id'] = $visible_category->term_id;
$post_details['category_name'] = $visible_category->name;
// get comments status
$comment_status = $this->comment_closed($post);
// check we have at least one approved comment that needs to be displayed
$comment_count = wp_count_comments($post->ID);
$no_comments = $comment_count->approved;
if ($comment_status == 'closed') {
if ($comment_count)
if ($comment_count->approved == 0)
$comment_status = 'disabled';
}
// add comments data
$post_details['comment_status'] = $comment_status;
$post_details['no_comments'] = $no_comments;
$post_details['show_avatars'] = intval(get_option("show_avatars"));
$post_details['require_name_email'] = intval(get_option("require_name_email"));
}
}
return '{"article":' . json_encode($post_details) . "}";
}
return '{"error":"Invalid post id"}';
}
/**
*
* The export_comments method is used for exporting the comments for an article.
*
* The method returns a JSON with the specific content:
*
* - ex :
* {
* "comments": [
* {
* "id": "1234",
* "author": "Comment author",
* "author_url": "{author_url}",
* "date": "Thu, May 01, 2014 04:07",
* "content": "<p>The comment's text goes here.</p>",
* "article_id": "123456",
* "article_title": "Post title",
* "avatar": "{avatar}",
* },
* ...
* ]
* }
*
* Receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportcomments'
* - articleId = The article's id
*
*/
public function export_comments()
{
// check if the export call is correct
if (isset($_GET["articleId"]) && is_numeric($_GET["articleId"])) {
$arr_comments = array();
// get post by id
$post = get_post($_GET["articleId"]);
if ($post != null && $post->post_type == 'post' && $post->post_password == '' && $post->post_status == 'publish') {
// check if at least one of the post's categories is visible
$visible_category = $this->get_visible_category($post);
if ($visible_category !== null) {
$args = array(
'parent' => '',
'post_id' => $post->ID,
'post_type' => 'post',
'status' => 'approve',
);
// order comments
if (get_bloginfo('version') >= 3.6) {
$comments_order = strtoupper(get_option('comment_order'));
if (!in_array($comments_order, array('ASC', 'DESC'))){
$comments_order = 'ASC';
}
$args['orderby'] = 'comment_date_gmt';
$args['order'] = $comments_order;
}
// read comments
$comments = get_comments($args);
if (is_array($comments) && !empty($comments)) {
foreach ($comments as $comment) {
$avatar = '';
// get avatar only if the author wants it displayed
if (get_option("show_avatars")) {
$get_avatar = get_avatar($comment, 50);
preg_match("/src='(.*?)'/i", $get_avatar, $matches);
if (isset($matches[1]))
$avatar = $matches[1];
}
$arr_comments[] = array(
'id' => $comment->comment_ID,
'author' => $comment->comment_author != '' ? ucfirst($comment->comment_author) : 'Anonymous',
'author_url' => $comment->comment_author_url,
'date' => WMobilePack_Formatter::format_date(strtotime($comment->comment_date)),
'content' => $this->purifier->purify($comment->comment_content),
'article_id' => $post->ID,
'article_title' => strip_tags(trim($post->post_title)),
'avatar' => $avatar
);
}
}
}
}
// return comments json
return '{"comments":' . json_encode($arr_comments) . "}";
}
return '{"error":"Invalid post id"}';
}
/**
* The save_comment method is used for adding a comment to an article.
*
* The method returns a JSON with the success/error message.
*
* Receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'savecomment'
* - articleId = The article's id
* - author
* - email
* - url
* - comment
* - comment_parent
* - code = Access token for saving comments
*
* @todo Translate error messages
*/
public function save_comment()
{
$allowed_hosts = array(
$_SERVER["HTTP_HOST"]
);
$is_allowed = false;
if (!isset($_SERVER['HTTP_REFERER'])){
$is_allowed = true;
} else {
foreach ($allowed_hosts as $http_host){
if (strpos($_SERVER['HTTP_REFERER'], $http_host) !== false){
$is_allowed = true;
}
}
}
if ($is_allowed) {
if (isset($_GET["articleId"]) && is_numeric($_GET["articleId"])) {
// check token
if (isset($_GET['code']) && $_GET["code"] !== '') {
if (!class_exists('WMobilePack_Tokens')) {
require_once(WMP_PLUGIN_PATH . 'inc/class-wmp-tokens.php');
}
// if the token is valid, go ahead and save comment to the DB
if (WMobilePack_Tokens::check_token($_GET['code'])) {
$arr_response = array(
'status' => 0,
'message' => ''
);
// get post by id
$post = get_post($_GET["articleId"]);
if ($post != null && $post->post_type == 'post' && $post->post_password == '' && $post->post_status == 'publish') {
// check if at least one of the post's categories is visible
$visible_category = $this->get_visible_category($post);
if ($visible_category !== null) {
// check if the post accepts comments
if (comments_open($post->ID)) {
// get post variables
$comment_post_ID = $post->ID;
$comment_author = (isset($_GET['author'])) ? trim(strip_tags($_GET['author'])) : '';
$comment_author_email = (isset($_GET['email'])) ? trim($_GET['email']) : '';
$comment_author_url = (isset($_GET['url'])) ? trim(filter_var($_GET['url'], FILTER_SANITIZE_URL)) : '';
$comment_content = (isset($_GET['comment'])) ? trim($this->purifier->purify($_GET['comment'])) : '';
$comment_type = 'comment';
$comment_parent = isset($_GET['comment_parent']) ? absint($_GET['comment_parent']) : 0;
// return errors for empty fields
if (get_option('require_name_email')) {
if ($comment_author_email == '' || $comment_author == '') {
$arr_response['message'] = "Missing name or email"; //Please fill the required fields (name, email).
return json_encode($arr_response);
} elseif (!is_email($comment_author_email)) {
$arr_response['message'] = "Invalid email address";
return json_encode($arr_response);
}
}
if ($comment_content == '') {
$arr_response['message'] = "Missing comment"; // Please type a comment
return json_encode($arr_response);
}
// set comment data
$comment_data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');
// add a hook for duplicate comments
add_action("comment_duplicate_trigger", array(&$this, 'duplicate_comment'));
// get comment id
$comment_id = wp_new_comment($comment_data);
// get status
if (is_numeric($comment_id)) {
// get comment
$comment = get_comment($comment_id);
// set status by comment status
if ($comment->comment_approved == 1) {
$arr_response['status'] = 1;
$arr_response['message'] = "Your comment was successfully added";
} else {
$arr_response['status'] = 2;
$arr_response['message'] = "Your comment is awaiting moderation";
}
return json_encode($arr_response);
}
} else {
// Sorry, comments are closed for this item
$arr_response['message'] = "Comments are closed";
}
} else {
// Sorry, the post belongs to a hidden category and is not available
$arr_response['message'] = "Invalid post id";
}
} else {
// Sorry, the post is not available
$arr_response['message'] = "Invalid post id";
}
return json_encode($arr_response);
}
}
}
}
}
/**
*
* Action hook that is called when a duplicate comment is detected.
*
* The method is used to echo a JSON with an error and applies an exit to prevent wp_die().
*
* @improvement
* If possible, improve this method by registering it as an ajax request and using wp_die() instead of exit()
* to allow unit testing.
*/
public function duplicate_comment()
{
// display the json
$arr_response = array(
'status' => 0,
'message' => 'Duplicate comment'
);
echo filter_var($_GET['callback'], FILTER_SANITIZE_STRING). '(' . json_encode($arr_response) . ')';
// end
exit();
}
/**
*
* The exportPages method is used for exporting all the visible pages.
*
* This method returns a JSON with the following format:
*
* - ex :
* {
* "pages": [
* {
* "id": "123456",
* "order": 3,
* "title": "Page title",
* "image": "",
* "content": "<p>The page's content goes here.</p>",
* },
* ...
* ]
* }
*
* The method receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportpages'
* - page = (optional) Used for pagination
* - rows = (optional) Number of rows per page, used for pagination
* - limit = (optional) Deprecated, alias for 'rows'. Should be removed after migrating Sencha apps.
*
* @todo Eliminate pages with inactive grandparents when using pagination params ('page' and 'rows').
*/
public function export_pages()
{
// init pages arrays
$arr_pages = array();
// init pagination params
$pagination = false;
if (isset($_GET["page"]) && is_numeric($_GET["page"])) {
$pagination = $_GET["page"];
}
$rows = false;
if (isset($_GET["rows"]) && is_numeric($_GET["rows"])){
$rows = $_GET["rows"];
if ($pagination === false){
$pagination = 1;
}
} elseif (isset($_GET["limit"]) && is_numeric($_GET["limit"])){
$rows = $_GET["limit"];
} else {
$rows = $pagination ? 5 : 100;
}
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_password' => '',
'post__not_in' => $this->inactive_pages,
'posts_per_page' => $rows,
'post_parent__not_in' => $this->inactive_pages,
'orderby' => 'menu_order parent title',
'order' => 'ASC'
);
if ($pagination !== false){
$args['paged'] = $pagination;
}
// remove inline style for the photos types of posts
add_filter('use_default_gallery_style', '__return_false');
$pages_query = new WP_Query($args);
if ($pages_query->have_posts()) {
// get array with the ordered pages by hierarchy
$order_pages = array();
if ($pagination === false){
$order_pages = array_keys(get_page_hierarchy($pages_query->posts));
}
if (!empty($order_pages) || $pagination !== false) {
$index = 0;
while ($pages_query->have_posts()) {
$pages_query->the_post();
$page = $pages_query->post;
if ($page->post_type == 'page' && $page->post_password == '' && $page->post_status == 'publish') {
// if the page has a title that is not empty
if (strip_tags(trim(get_the_title())) != '') {
// read featured image
$image_details = $this->get_post_image($page->ID);
if (get_option(WMobilePack_Options::$prefix.'page_' . $page->ID) === false)
$content = apply_filters("the_content", $page->post_content);
else
$content = apply_filters("the_content", get_option(WMobilePack_Options::$prefix.'page_' . $page->ID));
// if the page and its parent are visible, they should exist in the order array
if ($pagination === false) {
$index_order = array_search($page->ID, $order_pages);
} else {
$index_order = ($pagination - 1) * $rows + $index;
$index++;
}
if (is_numeric($index_order)) {
$current_key = $index_order + 1;
$arr_pages[] = array(
'id' => $page->ID,
'parent_id' => intval($page->post_parent),
'order' => $current_key,
'title' => strip_tags(trim(get_the_title())),
'link' => get_permalink(),
'image' => !empty($image_details) ? $image_details : "",
'content' => '',
'has_content' => $content != '' ? 1 : 0
);
}
}
}
}
}
}
if ($pagination && $rows) {
return '{"pages":' . json_encode($arr_pages) . ',"pagination":' .json_encode(array('page' => $pagination, 'rows' => $rows)).'}';
} else {
return '{"pages":' . json_encode($arr_pages) . '}';
}
}
/**
*
* The export_page method is used for exporting a single page.
*
* The method returns a JSON with the following format:
*
* - ex :
* {
* "page": {
* "id": "123456",
* "title": "Page title",
* "link": "{page_link}",
* "image": "",
* "content": "<p>Page content goes here</p>"
* }
* }
*
* The method receives the following GET params:
*
* - callback = The JavaScript callback method
* - content = 'exportpage'
* - pageId = The page's id
*
*/
public function export_page()
{
global $post;
if (isset($_GET["pageId"]) && is_numeric($_GET["pageId"])) {
// init page array
$arr_page = array();
// get page by id
$post = get_post($_GET["pageId"]);
if ($post != null && $post->post_type == 'page' && $post->post_password == '' && $post->post_status == 'publish' && strip_tags(trim($post->post_title)) != '') {
// check if page is visible
$is_visible = true;
if (in_array($post->ID, $this->inactive_pages)){
$is_visible = false;
}
// check if the page's ancestors are all visible
if ($is_visible) {
$ancestors = get_post_ancestors($post);
if (count(array_intersect($ancestors, $this->inactive_pages)) > 0){
$is_visible = false;
}
}
if ($is_visible) {
// featured image details
$image_details = $this->get_post_image($post->ID);
// for the content, first check if the admin edited the content for this page
if (get_option(WMobilePack_Options::$prefix.'page_' . $post->ID) === false)
$content = apply_filters("the_content", $post->post_content);
else
$content = apply_filters("the_content", get_option(WMobilePack_Options::$prefix.'page_' . $post->ID));
// remove script tags
$content = WMobilePack_Formatter::remove_script_tags($content);
$content = $this->purifier->purify($content);
// remove all urls from attachment images
$content = preg_replace(array('{<a(.*?)(wp-att|wp-content\/uploads|attachment)[^>]*><img}', '{ wp-image-[0-9]*" /></a>}'), array('<img', '" />'), $content);
$arr_page = array(
"id" => $post->ID,
"parent_id" => wp_get_post_parent_id($post->ID),
"title" => get_the_title($post->ID),
"link" => get_permalink($post->ID),
"image" => !empty($image_details) ? $image_details : "",
"content" => $content,
"has_content" => $content != '' ? 1 : 0
);
}
}
// return page json
return '{"page":' . json_encode($arr_page) . "}";
}
return '{"error":"Invalid post id"}';
}
}
}
|