forked from gordielachance/pinterest-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinim-pin-class.php
968 lines (755 loc) · 34.1 KB
/
pinim-pin-class.php
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
<?php
if(!class_exists('WP_List_Table')){
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Pinim_Pin{
var $pin_id;
var $options;
var $datas_raw;
var $datas;
var $board_id;
var $board;
var $post_id; //wp post
var $post;
function __construct($pin_id){
$this->pin_id = $pin_id;
$this->pin_datas_raw = $this->get_raw_datas();
$this->is_like = (isset($this->pin_datas_raw['is_like']));
$this->board_id = $this->pin_datas_raw['board']['id'];
$this->datas = $this->sanitize_raw_datas($this->pin_datas_raw);
}
function get_raw_datas(){
$all_pins = pinim_tool_page()->get_all_cached_pins();
//remove unecessary items
$current_pin_id = $this->pin_id;
$pins = array_filter(
$all_pins,
function ($e) use ($current_pin_id) {
return $e['id'] == $current_pin_id;
}
);
if (empty($pins)) return false;
$pin = array_shift($pins);
return $pin;
}
function get_board(){
if ( !isset($this->board) ){
if ($this->is_like){
$this->board = new Pinim_Board('likes');
}else{
$this->board = new Pinim_Board($this->board_id);
}
}
return $this->board;
}
/**
* When loading raw datas
* @param type $pin_datas
* @return type
*/
function sanitize_raw_datas($pin_datas){
/*
* Hook/unhook filters here to sanitize raw datas
*/
$pin_datas = apply_filters('pin_sanitize_raw_datas',$pin_datas);
//$pin_datas = array_filter($pin_datas);
return $pin_datas;
}
/*
* Get data from Pinterest
*/
function get_datas($key = null,$raw = false){
$pin_datas = $this->datas;
if (!isset($key)) return $pin_datas;
if (!isset($pin_datas[$key])) return false;
return $pin_datas[$key];
}
/*
* Get post saved on Wordpress
*/
function get_post(){
if (!$this->post){
if ($post_id = pinim_get_post_by_pin_id($this->pin_id)){
$this->post_id = $post_id;
$this->post = get_post($this->post_id);
}
}
return $this->post;
}
function get_link_action_import(){
//Refresh cache
$link_args = array(
'step' => 2,
'action' => 'pins_import_pins',
'pin_ids' => $this->pin_id,
'paged' => ( isset($_REQUEST['paged']) ? $_REQUEST['paged'] : null),
//'boards_ids' => ( isset($_REQUEST['board_ids']) ? $_REQUEST['board_ids'] : null)
);
$link = sprintf(
'<a href="%1$s">%2$s</a>',
pinim_get_tool_page_url($link_args),
__('Import pin','pinim')
);
return $link;
}
function get_link_action_update(){
if (!in_array($this->pin_id,pinim_tool_page()->existing_pin_ids)) return;
$link_args = array(
'step' => 2,
'action' => 'pins_update_pins',
'pin_ids' => $this->pin_id,
'paged' => ( isset($_REQUEST['paged']) ? $_REQUEST['paged'] : null),
'boards_ids' => ( isset($_REQUEST['board_ids']) ? $_REQUEST['board_ids'] : null)
);
$link = sprintf(
'<a href="%1$s">%2$s</a>',
pinim_get_tool_page_url($link_args),
__('Update pin','pinim')
);
return $link;
}
function get_link_action_edit(){
if (!in_array($this->pin_id,pinim_tool_page()->existing_pin_ids)) return;
$post = $this->get_post();
$link = sprintf(
'<a href="%1$s">%2$s</a>',
get_edit_post_link( $post->ID ),
__('Edit post','pinim')
);
return $link;
}
function get_remote_url(){
$url = pinim()->pinterest_url.'/pin/'.$this->pin_id;
return $url;
}
function get_blank_post(){
$blank_post = array(
'post_author' => get_current_user_id(),
'post_type' => 'post',
'post_status' =>'publish',
'post_category' => array(pinim_get_root_category_id()),
'tags_input' => array()
);
return apply_filters('pinim_get_blank_post',$blank_post);
}
function get_post_status(){
$board = $this->get_board();
$private = $board->is_private_board();
if ($private = $board->is_private_board()){
return 'private';
}else{
return 'publish';
}
}
function get_post_tags(){
$tags = array();
if ($description = $this->get_datas('description')){
$tags = pinim_get_hashtags($description);
}
return $tags;
}
function get_post_title(){
$title = $this->get_datas('title');
if (!$title = $this->get_datas('title')){
if ($description = $this->get_datas('description')){
$tags = pinim_get_hashtags($description);
foreach ((array)$tags as $tag){
$description = str_replace('#'.$tag,'',$description);
}
$title = wp_trim_words( $description, 30, '...' );
}
}
if (!$title) {
return sprintf(__('Pin #%1s','pinim'),$this->pin_id);
}
return $title;
}
function get_post_format(){
//default format
$format = 'image';
if ($this->get_datas('is_video')){
$format = 'video';
}
return $format;
}
function get_post_log_meta(){
$datas = apply_filters('pin_sanitize_before_insert',$this->get_datas());
$prefix = '_pinterest-';
$metas = array(
$prefix.'pin_id' => $this->pin_id,
$prefix.'board_id' => $this->board_id,
$prefix.'log' => $datas
);
return $metas;
}
function set_post_content($post){
$post_format = get_post_format( $post->ID );
$link = $this->get_datas('link');
$domain = $this->get_datas('domain');
$content = null;
$log = pinim_get_pin_meta('log',$post->ID,true);
switch($post_format){
case 'image':
$content = get_the_post_thumbnail($post->ID,'full');
$content ='<a href="' . $link . '" title="' . the_title_attribute('echo=0') . '" >'.$content.'</a>';
break;
case 'video':
//https://codex.wordpress.org/Embeds
$content = $link;
break;
}
$content .= "\n";//line break (avoid problems with embeds)
$content .='<p class="pinim-pin-source"><a href="' . $link . '" title="' . $domain . '" >'.$domain.'</a></p>';
//allow to filter
$content = apply_filters('pinim_get_post_content',$content,$post,$this);
//print_r("<xmp>".$content."</xmp>");exit;
$my_post = array();
$my_post['ID'] = $post->ID;
$my_post['post_content'] = $content;
if (!wp_update_post( $my_post )){
return false;
}
return true;
}
function save($update=false){
$error = new WP_Error();
$datas = apply_filters('pin_sanitize_before_insert',$this->get_datas());
$board = $this->get_board();
if (!$update){
$post = $this->get_blank_post();
}elseif(!$post = (array)$this->get_post()){
$error->add('nothing_to_update',__("The current pin has never been imported and can't be updated",'pinim'));
return $error;
}
if (!isset($datas['image'])){
$error->add('no_pin_image',__("The current pin does not have an image file associated",'pinim'));
return $error;
}
//set title
$post['post_title'] = $this->get_post_title();
//set tags
$tags_input = array();
if (isset($post['tags_input'])){
$tags_input = $post['tags_input'];
}
$post['tags_input'] = array_merge( $tags_input,$this->get_post_tags() );
//set post status
$post['post_status'] = $this->get_post_status();
//set post categories
$post['post_category'] = (array)$board->get_category();
//set post date
$post['post_date'] = date('Y-m-d H:i:s', $this->get_datas('created_at'));
$post = array_filter($post);
$image_url = $datas['image'];
//insert post
$post_id = wp_insert_post( $post, true );
if ( is_wp_error($post_id) ) return $post_id;
//TO FIX : post is created before image is checked.
// We should reverse that.
$new_post = get_post($post_id);
//set post format
$post_format = $this->get_post_format();
if ( !set_post_format( $post_id , $post_format )){
$error->add('pin_post_format',sprintf(__('Unable to set post format "%1$s"','pinim'),$post_format));
return $error;
}
$attachment_id = pinim_attach_remote_image($new_post,$image_url);
//set featured image
if ( !is_wp_error($attachment_id) ){
if ($update){ //delete old thumb
if ($old_thumb_id = get_post_thumbnail_id( $post_id )){
wp_delete_attachment( $old_thumb_id, true );
}
}
set_post_thumbnail($new_post, $attachment_id);
$hd_file = wp_get_attachment_image_src($attachment_id, 'full');
$hd_url = $hd_file[0];
}else{
wp_delete_post( $post_id, true );
$error_code = $attachment_id->get_error_code();
$error_message = $attachment_id->get_error_message($error_code);
$image_name = basename( $image_url );
$error_msg = sprintf(
__('Error while setting post thumbnail %1s : %2s','pinim'),
'<a href="'.$image_url.'" target="_blank">'.$image_name.'</a>',
$error_message
);
$error->add('pin_thumbnail_error', $error_msg, $post_id);
return $error;
}
//set post metas
$post_metas = $this->get_post_log_meta();
foreach ( $post_metas as $key=>$value ) {
update_post_meta( $post_id, $key, $value );
}
//set post content
if (!$this->set_post_content($new_post)){
//feedback
$error_msg = __('Error while updating post content', 'pinim');
$error->add('pin_content_error', $error_msg, $post_id);
return $error;
}
return $post_id;
}
}
class Pinim_Pins_Table extends WP_List_Table {
var $input_data = array();
/** ************************************************************************
* REQUIRED. Set up a constructor that references the parent constructor. We
* use the parent reference to set some default configs.
***************************************************************************/
function __construct($data){
global $status, $page;
$this->input_data = $data;
//Set parent defaults
parent::__construct( array(
'singular' => __('pin','pinim'), //singular name of the listed records
'plural' => __('pins','pinim'), //plural name of the listed records
'ajax' => true //does this table support ajax?
) );
}
/** ************************************************************************
* Recommended. This method is called when the parent class can't find a method
* specifically build for a given column. Generally, it's recommended to include
* one method for each column you want to render, keeping your package class
* neat and organized. For example, if the class needs to process a column
* named 'title', it would first see if a method named $this->column_title()
* exists - if it does, that method will be used. If it doesn't, this one will
* be used. Generally, you should try to use custom column methods as much as
* possible.
*
* Since we have defined a column_title() method later on, this method doesn't
* need to concern itself with any column with a name of 'title'. Instead, it
* needs to handle everything else.
*
* For more detailed insight into how columns are handled, take a look at
* WP_List_Table::single_row_columns()
*
* @param array $item A singular item (one full row's worth of data)
* @param array $column_name The name/slug of the column to be processed
* @return string Text or HTML to be placed inside the column <td>
**************************************************************************/
function column_default($item, $column_name){
switch($column_name){
default:
return print_r($item,true); //Show the whole array for troubleshooting purposes
}
}
/** ************************************************************************
* Recommended. This is a custom column method and is responsible for what
* is rendered in any column with a name/slug of 'title'. Every time the class
* needs to render a column, it first looks for a method named
* column_{$column_title} - if it exists, that method is run. If it doesn't
* exist, column_default() is called instead.
*
* This example also illustrates how to implement rollover actions. Actions
* should be an associative array formatted as 'slug'=>'link html' - and you
* will need to generate the URLs yourself. You could even ensure the links
*
*
* @see WP_List_Table::::single_row_columns()
* @param array $item A singular item (one full row's worth of data)
* @return string Text to be placed inside the column <td> (movie title only)
**************************************************************************/
function column_title($pin){
//Build row actions
$actions = array(
'view' => sprintf('<a href="%1$s" target="_blank">%2$s</a>',$pin->get_remote_url(),__('View on Pinterest','pinim'),'view'),
);
if ($pin->get_post()){ //only if post exists
$actions['update'] = $pin->get_link_action_update();
$actions['edit'] = $pin->get_link_action_edit();
}else{
$actions['import'] = $pin->get_link_action_import();
}
$title = $pin->get_datas('title');
//Return the title contents
$title = sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
/*$1%s*/ $title,
/*$2%s*/ $pin->pin_id,
/*$3%s*/ $this->row_actions($actions)
);
//post icon
$icon_classes = array('post-state-format','post-format-icon');
if ( $pin->get_datas('is_video') ){
$icon_classes[] = 'post-format-video';
}else{
$icon_classes[] = 'post-format-image';
}
$icon = sprintf('<span%1$s></span>',pinim_get_classes($icon_classes));
return $icon.$title;
}
/** ************************************************************************
* REQUIRED if displaying checkboxes or using bulk actions! The 'cb' column
* is given special treatment when columns are processed. It ALWAYS needs to
* have it's own method.
*
* @see WP_List_Table::::single_row_columns()
* @param array $item A singular item (one full row's worth of data)
* @return string Text to be placed inside the column <td> (movie title only)
**************************************************************************/
function column_cb($pin){
$hidden = sprintf('<input type="hidden" name="%1$s[pins][%2$s][id]" value="%2$s" />',
'pinim_tool',
$pin->pin_id
);
$bulk = sprintf(
'<input type="checkbox" name="%1$s[pins][%2$s][bulk]" value="on" />',
'pinim_tool',
$pin->pin_id
);
return $hidden.$bulk;
}
function column_private($pin){
//privacy
$secret_checked_str = checked(true, false, false );
$is_secret_pin = ($pin->get_datas('privacy')=='secret');
if ($private = $pin->get_options('private') ){
$secret_checked_str = checked($private, 'on', false );
}else{
$secret_checked_str = checked($is_secret_pin, true, false );
}
return sprintf(
'<input type="checkbox" name="%1$s[pins][%2$s][private]" value="on" %3$s/>',
'pinim_tool',
$pin->pin_id,
$secret_checked_str
);
}
function column_thumbnail($pin){
if (!$images = $pin->get_datas('images')) return;
//get last one
$image = array_pop($images);
return sprintf(
'<img src="%1$s" />',
$image['url']
);
}
function column_source($pin){
$text = $pin->get_datas('domain');
$url = get_option( 'link' );
return sprintf(
'<a target="_blank" href="%1$s">%2$s</a>',
esc_url($url),
$text
);
}
function column_date($pin){
$timestamp = $pin->get_datas('created_at');
$date = date_i18n( get_option( 'date_format'), $timestamp );
$time = date_i18n( get_option( 'time_format'), $timestamp );
return sprintf( __('%1$s at %2$s','pinim'), $date, $time );
}
function column_updated($pin){
if (!$post = $pin->get_post()) return;
$timestamp = get_post_modified_time( 'U', false, $post );
$date = date_i18n( get_option( 'date_format'), $timestamp );
$time = date_i18n( get_option( 'time_format'), $timestamp );
return sprintf( __('%1$s at %2$s','pinim'), $date, $time );
}
function column_board($pin){
$board = $pin->get_board();
$board_name = $board->get_datas('name');
return $board_name;
}
/** ************************************************************************
* REQUIRED! This method dictates the table's columns and titles. This should
* return an array where the key is the column slug (and class) and the value
* is the column's title text. If you need a checkbox for bulk actions, refer
* to the $columns array below.
*
* The 'cb' column is treated differently than the rest. If including a checkbox
* column in your table you must create a column_cb() method. If you don't need
* bulk actions or checkboxes, simply leave the 'cb' entry out of your array.
*
* @see WP_List_Table::::single_row_columns()
* @return array An associative array containing column information: 'slugs'=>'Visible Titles'
**************************************************************************/
function get_columns(){
$columns = array(
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
'thumbnail' => '',
'title' => __('Pin Title','pinim'),
'source' => __('Source','pinim'),
'date' => __('Date','pinim'),
'board' => __('Board','pinim')
);
if (pinim_tool_page()->get_screen_pins_filter() =='processed'){
$columns['updated'] = __('Last Updated','pinim');
}
return $columns;
}
/** ************************************************************************
* Optional. If you want one or more columns to be sortable (ASC/DESC toggle),
* you will need to register it here. This should return an array where the
* key is the column that needs to be sortable, and the value is db column to
* sort by. Often, the key and value will be the same, but this is not always
* the case (as the value is a column name from the database, not the list table).
*
* This method merely defines which columns should be sortable and makes them
* clickable - it does not handle the actual sorting. You still need to detect
* the ORDERBY and ORDER querystring variables within prepare_items() and sort
* your data accordingly (usually by modifying your query).
*
* @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values',bool)
**************************************************************************/
function get_sortable_columns() {
$sortable_columns = array(
'title' => array('title',false), //true means it's already sorted
'date' => array('date',false),
'updated' => array('updated',false),
);
return $sortable_columns;
}
/**
* @param string $which
*/
protected function extra_tablenav( $which ) {
?>
<div class="alignleft actions">
<?php
if ( 'top' == $which && !is_singular() ) {
switch (pinim_tool_page()->get_screen_pins_filter()){
case 'pending':
//Import All Pins
submit_button( pinim_tool_page()->all_action_str['import_all_pins'], 'button', 'all_pins_action', false, array('id'=>'import_all_bt') );
break;
case 'processed':
//Update All Pins
submit_button( pinim_tool_page()->all_action_str['update_all_pins'], 'button', 'all_pins_action', false, array('id'=>'update_all_bt') );
break;
}
}
?>
</div>
<?php
}
/**
* Get an associative array ( id => link ) with the list
* of views available on this table.
*
* @since 3.1.0
* @access protected
*
* @return array
*/
protected function get_views() {
$link_args = array(
'step' => 2,
'board_ids' => implode(',',(array)pinim_tool_page()->get_requested_boards_ids())
);
$link_args = array_filter($link_args);
$link_processed_args = $link_args;
$link_processed_args['pins_filter'] = 'processed';
$link_pending_args = $link_args;
$link_pending_args['pins_filter'] = 'pending';
$link_processed_classes = array();
$link_pending_classes = array();
$processed_count = pinim_tool_page()->get_pins_count_processed();
$pending_count = pinim_tool_page()->get_pins_count_pending();
switch (pinim_tool_page()->get_screen_pins_filter()){
case 'pending':
$link_pending_classes[] = 'current';
break;
case 'processed':
$link_processed_classes[] = 'current';
break;
}
$link_processed = sprintf(
__('<a href="%1$s"%2$s>%3$s <span class="count">(<span class="imported-count">%4$s</span>)</span></a>'),
pinim_get_tool_page_url($link_processed_args),
pinim_get_classes($link_processed_classes),
__('Processed','pinim'),
$processed_count
);
$link_pending = sprintf(
__('<a href="%1$s"%2$s>%3$s <span class="count">(<span class="imported-count">%4$s</span>)</span></a>'),
pinim_get_tool_page_url($link_pending_args),
pinim_get_classes($link_pending_classes),
__('Pending','pinim'),
$pending_count
);
return array(
'pending' => $link_pending,
'processed' => $link_processed
);
}
/**
* Display the list of views available on this table.
*
* @since 3.1.0
* @access public
*/
public function views() {
$views = $this->get_views();
if ( empty( $views ) )
return;
echo "<ul class='subsubsub'>\n";
foreach ( $views as $class => $view ) {
$views[ $class ] = "\t<li class='$class'>$view";
}
echo implode( " |</li>\n", $views ) . "</li>\n";
echo "</ul>";
}
/** ************************************************************************
* Optional. If you need to include bulk actions in your list table, this is
* the place to define them. Bulk actions are an associative array in the format
* 'slug'=>'Visible Title'
*
* If this method returns an empty value, no bulk action will be rendered. If
* you specify any bulk actions, the bulk actions box will be rendered with
* the table automatically on display().
*
* Also note that list tables are not automatically wrapped in <form> elements,
* so you will need to create those manually in order for bulk actions to function.
*
* @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'
**************************************************************************/
function get_bulk_actions() {
$actions = array(
'pins_import_pins' => __('Import Pins','pinim'),
'pins_update_pins' => __('Update Pins','pinim')
);
return $actions;
}
/** ************************************************************************
* Optional. You can handle your bulk actions anywhere or anyhow you prefer.
* For this example package, we will handle it in the class to keep things
* clean and organized.
*
* @see $this->prepare_items()
**************************************************************************/
function process_bulk_action() {
}
/** ************************************************************************
* REQUIRED! This is where you prepare your data for display. This method will
* usually be used to query the database, sort and filter the data, and generally
* get it ready to be displayed. At a minimum, we should set $this->items and
* $this->set_pagination_args(), although the following properties and methods
* are frequently interacted with here...
*
* @global WPDB $wpdb
* @uses $this->_column_headers
* @uses $this->items
* @uses $this->get_columns()
* @uses $this->get_sortable_columns()
* @uses $this->get_pagenum()
* @uses $this->set_pagination_args()
**************************************************************************/
function prepare_items() {
global $wpdb; //This is used only if making any database queries
/**
* First, lets decide how many records per page to show
*/
$per_page = pinim()->get_options('pins_per_page');
/**
* REQUIRED. Now we need to define our column headers. This includes a complete
* array of columns to be displayed (slugs & titles), a list of columns
* to keep hidden, and a list of columns that are sortable. Each of these
* can be defined in another method (as we've done here) before being
* used to build the value for our _column_headers property.
*/
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
/**
* REQUIRED. Finally, we build an array to be used by the class for column
* headers. The $this->_column_headers property takes an array which contains
* 3 other arrays. One for all columns, one for hidden columns, and one
* for sortable columns.
*/
$this->_column_headers = array($columns, $hidden, $sortable);
/**
* Optional. You can handle your bulk actions however you see fit. In this
* case, we'll handle them within our package just to keep things clean.
*/
$this->process_bulk_action();
/**
* Instead of querying a database, we're going to fetch the example data
* property we created for use in this plugin. This makes this example
* package slightly different than one you might build on your own. In
* this example, we'll be using array manipulation to sort and paginate
* our data. In a real-world implementation, you will probably want to
* use sort and pagination data to build a custom query instead, as you'll
* be able to use your precisely-queried data immediately.
*/
$data = $this->input_data;
/**
* This checks for sorting input and sorts the data in our array accordingly.
*
* In a real-world situation involving a database, you would probably want
* to handle sorting by passing the 'orderby' and 'order' values directly
* to a custom query. The returned data will be pre-sorted, and this array
* sorting technique would be unnecessary.
*/
function usort_reorder($a,$b){
$orderby_default = 'date';
$order_default = 'desc';
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : $orderby_default; //If no sort, default to date
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : $order_default; //If no order, default to desc
switch ($orderby){
case 'title':
$title_a = ($a->get_datas('title')) ? $a->get_datas('title') : $a->pin_id;
$title_b = ($b->get_datas('title')) ? $b->get_datas('title') : $b->pin_id;
$result = strcmp($title_a, $title_b);
break;
case 'date':
$result = strcmp($a->get_datas('created_at'), $b->get_datas('created_at'));
break;
case 'updated':
$post_a = $a->get_post();
$post_b = $b->get_post();
$timestamp_a = get_post_modified_time( 'U', false, $post_a );
$timestamp_b = get_post_modified_time( 'U', false, $post_b );
$result = strcmp($timestamp_a, $timestamp_b);
break;
}
return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
}
usort($data, 'usort_reorder');
/***********************************************************************
* ---------------------------------------------------------------------
* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
*
* In a real-world situation, this is where you would place your query.
*
* For information on making queries in WordPress, see this Codex entry:
* http://codex.wordpress.org/Class_Reference/wpdb
*
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* ---------------------------------------------------------------------
**********************************************************************/
/**
* REQUIRED for pagination. Let's figure out what page the user is currently
* looking at. We'll need this later, so you should always include it in
* your own package classes.
*/
$current_page = $this->get_pagenum();
/**
* REQUIRED for pagination. Let's check how many items are in our data array.
* In real-world use, this would be the total number of items in your database,
* without filtering. We'll need this later, so you should always include it
* in your own package classes.
*/
$total_items = count($data);
/**
* The WP_List_Table class does not handle pagination for us, so we need
* to ensure that the data is trimmed to only the current page. We can use
* array_slice() to
*/
$data = array_slice($data,(($current_page-1)*$per_page),$per_page);
/**
* REQUIRED. Now we can add our *sorted* data to the items property, where
* it can be used by the rest of the class.
*/
$this->items = $data;
/**
* REQUIRED. We also have to register our pagination options & calculations.
*/
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
}