forked from gordielachance/pinterest-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinim-tool-page.php
1204 lines (889 loc) · 41.1 KB
/
pinim-tool-page.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
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
<?php
class Pinim_Tool_Page {
var $options_page;
var $current_step = 0;
var $existing_pin_ids = array();
var $screen_boards_filter = null;
var $screen_pins_filter = null;
var $bridge = null;
var $session = null;
var $all_action_str = array(); //text on all pins | boards actions
/**
* @var The one true Instance
*/
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Pinim_Tool_Page;
self::$instance->init();
}
return self::$instance;
}
private function __construct() { /* Do nothing here */ }
function init(){
$this->all_action_str = array(
'cache_all_pins' =>__( 'Cache All Pins','pinim' ),
'import_all_pins' =>__( 'Import All Pins','pinim' ),
'update_all_pins' =>__( 'Update All Pins','pinim' ),
'update_all_boards' =>__( 'Update All Boards Settings','pinim' )
);
add_action( 'admin_init', array( $this, 'init_tool_page' ) );
add_action( 'admin_init', array( $this, 'reduce_settings_errors' ) );
add_action( 'admin_init', array( $this, 'settings_page_init' ) );
add_action( 'admin_menu',array(&$this,'admin_menu'),10,2);
add_action( 'admin_init', array( $this, 'register_session' ), 1);
add_action('wp_logout', array( $this, 'destroy_session' ) );
add_action('wp_login', array( $this, 'destroy_session' ) );
}
function init_tool_page(){
if (!pinim_is_tool_page()) return false;
/*
//dependencies
if ( !is_plugin_active( 'wp-session-manager/wp-session-manager.php' ) ) {
$dep_install_url = admin_url( 'plugin-install.php?tab=search&s=WP+Session+Manager' );
$dep_link = '<a href="https://wordpress.org/plugins/wp-session-manager" target="_blank">WP Session Manager</a>';
$message = sprintf(__( 'Pinterest Importer requires the plugin %1$s by Eric Mann to be installed. Click <a href="%2$s">here</a> !', 'pinim' ),$dep_link,$dep_install_url);
add_settings_error('pinim', 'require_wp_session_manager', $message);
return;
}else{
$this->session = WP_Session::get_instance();
}
*/
$this->existing_pin_ids = pinim_get_meta_value_by_key('_pinterest-pin_id');
$this->bridge = new Pinim_Bridge;
$step = pinim_get_tool_page_step();
if($step!==false){
$this->current_step = $step;
$this->screen_boards_filter = $this->get_screen_boards_filter();
$this->screen_pins_filter = $this->get_screen_pins_filter();
}else{
if ( $boards_data = $this->get_session_data('user_boards') ){ //check cache exists
$url = pinim_get_tool_page_url(array('step'=>1));
wp_redirect( $url );
die();
}
}
$this->save_step();
$this->init_step();
}
/**
* Removes duplicate settings errors (based on their messages)
* @global type $wp_settings_errors
*/
function reduce_settings_errors(){
//remove duplicates errors based on their message
global $wp_settings_errors;
if (empty($wp_settings_errors)) return;
foreach($wp_settings_errors as $key => $value) {
foreach($wp_settings_errors as $key2 => $value2) {
if($key != $key2 && $value['message'] === $value['message']) {
unset($wp_settings_errors[$key]);
}
}
}
}
function get_screen_boards_filter(){
if ($this->screen_boards_filter) return $this->screen_boards_filter;
//default
$status = 'pending';
if (!$this->get_boards_count_pending()){
if (!$this->get_boards_count_waiting()){
$status = 'completed';
}else{
$status = 'waiting';
}
}
if (isset($_REQUEST['boards_filter'])){
$status = $_REQUEST['boards_filter'];
}
$this->screen_boards_filter = $status;
return $status;
}
function get_screen_pins_filter(){
if ($this->screen_pins_filter) return $this->screen_pins_filter;
//default
$status = 'pending';
if (!pinim_tool_page()->get_pins_count_pending()){
$status = 'processed';
}
if (isset($_REQUEST['pins_filter'])){
$status = $_REQUEST['pins_filter'];
}
$this->screen_pins_filter = $status;
return $status;
}
function save_step(){
$input = null; //form datas, etc.
if ( isset($_POST['pinim_tool']) ) {
$input = $_POST['pinim_tool'];
}
$new_input = array();
$user_id = get_current_user_id();
$action = ( isset($_REQUEST['action']) ? $_REQUEST['action'] : null);
//SYSTEM
//clear boards cache
if ( isset($input['clear_cache']) && $input['clear_cache'] ){
$this->delete_session_data();
add_settings_error('pinim', 'clear_cache', __( 'Cache Successfully cleared', 'pinim' ), 'updated');
return;
}
switch ($this->current_step){
case 2://'fetch-pins':
$pin_settings = array();
$pin_error_ids = array();
$skip_pin_import = array();
$bulk_pins = $this->get_requested_pins();
//check if a filter action is set
if ($all_pins_action = $this->get_all_pins_action()){
$action = $all_pins_action;
}
switch ($action) {
case 'pins_update_pins':
foreach((array)$bulk_pins as $key=>$pin){
//skip
if (!in_array($pin->pin_id,$this->existing_pin_ids)){
$skip_pin_import[] = $pin->pin_id;
continue;
}
//save pin
$pin_saved = $pin->save(true);
if (is_wp_error($pin_saved)){
$pins_errors[$pin->pin_id] = $pin_saved;
}
}
//errors
if (!empty($bulk_pins) && !empty($skip_pin_import)){
//remove skipped pins from bulk
foreach((array)$bulk_pins as $key=>$pin){
if (!in_array($pin->pin_id,$skip_pin_import)) continue;
unset($bulk_pins[$key]);
}
if (!$all_pins_action){
add_settings_error('pinim', 'pins_never_imported',
sprintf(
__( 'Some pins cannot be updated because they never have been imported. Choose "%1$s" if you want import pins. (Pins: %2$s)', 'pinim' ),
__('Import Pins','pinim'),
implode(',',$skip_pin_import)
)
);
}
}
if (!empty($bulk_pins)){
$bulk_count = count($bulk_pins);
$errors_count = (!empty($pins_errors)) ? count($pins_errors) : 0;
$success_count = $bulk_count-$errors_count;
if ($success_count){
add_settings_error('pinim', 'update_pins', sprintf(__( '%1$s Pins successfully updated', 'pinim' ),$success_count), 'updated');
}
if (!empty($pins_errors)){
foreach ((array)$pins_errors as $pin_id=>$pin_error){
add_settings_error('pinim', 'update_pin_'.$pin_id, $pin_error->get_error_message());
}
}
}
break;
case 'pins_import_pins':
foreach((array)$bulk_pins as $key=>$pin){
//skip
if (in_array($pin->pin_id,$this->existing_pin_ids)){
$skip_pin_import[] = $pin->pin_id;
continue;
}
//save pin
$pin_saved = $pin->save();
if (is_wp_error($pin_saved)){
$pins_errors[$pin->pin_id] = $pin_saved;
}
}
//errors
if (!empty($bulk_pins) && !empty($skip_pin_import)){
//remove skipped pins from bulk
foreach((array)$bulk_pins as $key=>$pin){
if (!in_array($pin->pin_id,$skip_pin_import)) continue;
unset($bulk_pins[$key]);
}
if (!$all_pins_action){
add_settings_error('pinim', 'pins_already_imported',
sprintf(
__( 'Some pins have been skipped because they already have been imported. Choose "%1$s" if you want update the existing pins. (Pins: %2$s)', 'pinim' ),
__('Update Pins','pinim'),
implode(',',$skip_pin_import)
)
);
}
}
if (!empty($bulk_pins)){
$bulk_count = count($bulk_pins);
$errors_count = (!empty($pins_errors)) ? count($pins_errors) : 0;
$success_count = $bulk_count-$errors_count;
if ($success_count){
add_settings_error('pinim', 'import_pins', sprintf(__( '%1$s Pins successfully imported', 'pinim' ),$success_count), 'updated');
//refresh pins list
$this->existing_pin_ids = pinim_get_meta_value_by_key('_pinterest-pin_id');
}
if (!empty($pins_errors)){
foreach ((array)$pins_errors as $pin_id=>$pin_error){
add_settings_error('pinim', 'import_pin_'.$pin_id, $pin_error->get_error_message());
}
}
}
break;
}
break;
case 1://'boards-settings':
$board_settings = array();
$board_errors = array();
if ( isset($input['boards']) ){
$board_settings = $input['boards'];
}
//bulk boards
$bulk_boards = $this->get_requested_boards();
//check if a filter action is set
if ($all_boards_action = $this->get_all_boards_action()){
$action = $all_boards_action;
}
switch ($action) {
case 'boards_update_settings':
foreach((array)$bulk_boards as $key=>$board){
$board_saved = $board->set_options($board_settings[$board->board_id]);
if (is_wp_error($board_saved)){
$board_errors[$board->board_id]=sprintf(__('Board #%1$s: ','pinim'),$board->board_id).$board_saved->get_error_message();
}
pinim()->user_boards_options=null; //force reload
}
if (!empty($bulk_boards) && empty($board_errors)){
add_settings_error('pinim', 'set_options_boards', __( 'Boards Successfully updated', 'pinim' ), 'updated');
}else{
$board_errors = array_unique($board_errors);
foreach ($board_errors as $board_id=>$error){
add_settings_error('pinim', 'set_options_board_'.$board_id, $board_errors);
}
}
break;
case 'boards_cache_pins':
foreach((array)$bulk_boards as $board){
$board_pins = $board->get_pins(true);
if (is_wp_error($board_pins)){
$board_error[$board->board_id]=sprintf(__('Board #%1$s: ','pinim'),$board->board_id).$board_pins->get_error_message();
add_settings_error('pinim', 'cache_single_board_pins_'.$board->board_id, $board_pins->get_error_message());
}
}
if ($bulk_boards && empty($board_error)){
add_settings_error('pinim', 'cache_single_board_pins', __( 'Boards Pins successfully cached', 'pinim' ), 'updated');
}else{
$board_errors = array_unique($board_errors);
foreach ($board_errors as $board_id=>$error){
add_settings_error('pinim', 'set_options_board_'.$board_id, $board_errors);
}
}
break;
case 'boards_import_pins':
$bulk_boards_ids = array();
foreach ((array)$bulk_boards as $bulk_boards){
$bulk_boards_ids[] = $bulk_boards->board_id;
}
//redirect to next step, and set selected board_ids.
$url = pinim_get_tool_page_url(array('step'=>2,'board_ids'=>implode(',',$bulk_boards_ids)));
wp_redirect( $url );
die();
break;
}
break;
default: //login
$is_form_submission = ( isset($input['login']) || isset($input['password']) );
if (!$is_form_submission) return;
//check sessions are enabled
if (!session_id()){
add_settings_error('pinim', 'no_sessions', __("It seems that your host doesn't support PHP sessions. This plugin will not work properly. We'll try to fix this soon.","pinim"));
return false;
}
if ( !$this->bridge->is_logged_in ) {
//login
if( isset( $input['login'] ) ){
$new_input['login'] = $input['login'];
}
//pwd
if( isset( $input['password'] ) ){
$new_input['password'] = $input['password'];
}
if ( !isset($new_input['login']) || !isset($new_input['password']) ){
add_settings_error( 'pinim', 'do_login', __( "Missing login and/or password", 'pinim' ) );
return;
}
//try to auth
$logged = $this->do_bridge_login($new_input['login'],$new_input['password']);
if ( is_wp_error($logged) ){
add_settings_error('pinim', 'do_login', $logged->get_error_message() );
return;
}
//store login / password
$this->set_session_data('login',$new_input['login']);
$this->set_session_data('password',$new_input['password']);
//try to get user datas
$user_datas = $this->bridge->get_user_datas();
if (is_wp_error($user_datas)){
add_settings_error('pinim', 'get_user_data', $user_datas->get_error_message() );
return;
}
//store user datas
$this->set_session_data('user_datas',$user_datas);
//try to populate boards
$boards_data = $this->get_boards_data();
if (is_wp_error($boards_data)){
add_settings_error('pinim', 'get_boards_data', sprintf(__('Error while trying to get boards data : %1$s.','pinim'),$boards_data->get_error_message()));
return;
}
//redirect to next step
$args = array(
'step'=>1
);
$url = pinim_get_tool_page_url($args);
wp_redirect( $url );
die();
}
break;
}
}
function do_bridge_login($login = null, $password = null){
if ($this->bridge->is_logged_in) return $this->bridge->is_logged_in;
if (!$login){
$login = $this->get_session_data('login');
}
if (!$password){
$password = $this->get_session_data('password');
}
//try to auth
$this->bridge->set_login($login)->set_password($password);
$logged = $this->bridge->do_login();
if ( is_wp_error($logged) ){
return new WP_Error( 'pinim',$logged->get_error_message() );
}
return $logged;
}
function init_step(){
$board_ids = array();
switch ($this->current_step){
case 2: //pins settings
//remove processed pins from request
unset($_REQUEST['pin_ids']);
if ($pins = $this->get_requested_pins()){
foreach ((array)$pins as $key=>$pin){
switch ( $this->get_screen_pins_filter() ){
case 'pending':
if (in_array($pin->pin_id,$this->existing_pin_ids)) unset($pins[$key]);
break;
case 'processed':
if (!in_array($pin->pin_id,$this->existing_pin_ids)) unset($pins[$key]);
break;
}
}
}else{
add_settings_error('pinim', 'pins-cache', __( 'No pins found. Have you cached the boards pins ?', 'pinim'));
}
$this->table_pins = new Pinim_Pins_Table($pins);
$this->table_pins->prepare_items();
break;
case 1: //boards settings
$boards = array();
if ( $boards_data = $this->get_session_data('user_boards') ){ //check cache exists
foreach((array)$boards_data as $board_data){
$boards[] = new Pinim_Board($board_data['id']);
}
}else{
$link_user_cache_args = array('step' => 0);
$link_user_cache = pinim_get_tool_page_url($link_user_cache_args);
add_settings_error('pinim', 'boards-cache', __( 'No boards found. Have you logged in ?', 'pinim'));
}
foreach ((array)$boards as $key=>$board){
$is_queue_complete = $board->is_queue_complete();
$is_fully_imported = $board->is_fully_imported();
switch ($this->get_screen_boards_filter()){
case 'pending':
if ($is_fully_imported) unset($boards[$key]);
break;
case 'waiting':
if ($is_queue_complete) unset($boards[$key]);
break;
case 'completed':
if (!$is_fully_imported) unset($boards[$key]);
break;
}
}
$this->table_board = new Pinim_Boards_Table($boards);
$this->table_board->prepare_items();
break;
}
}
function admin_menu(){
$this->options_page = add_submenu_page('tools.php', __('Pinterest Importer','pinim'), __('Pinterest Importer','pinim'), 'manage_options', 'pinim', array($this, 'importer_page'));
}
function dummy_sanitize( $input ){
/*
* Do nothing here. We use our own hooked function save_step() at init, this one is not necessary.
*/
return false;
}
function settings_page_init(){
register_setting(
'pinim', // Option group
'pinim_tool', // Option name
array( $this, 'dummy_sanitize' ) // Sanitize
);
switch($this->current_step){
case 0://authentification
add_settings_section(
'settings_general', // ID
__('Pinterest authentification','pinim'), // Title
array( $this, 'section_general_desc' ), // Callback
'pinim-user-auth' // Page
);
add_settings_field(
'login',
__('Email/Username','pinim'),
array( $this, 'login_field_callback' ),
'pinim-user-auth',
'settings_general'
);
add_settings_field(
'password',
__('Password','pinim'),
array( $this, 'password_field_callback' ),
'pinim-user-auth',
'settings_general'
);
if ( $user_datas = $this->get_session_data('user_datas') ){
add_settings_field(
'status',
__('Account','pinim'),
array( $this, 'status_field_callback' ),
'pinim-user-auth',
'settings_general'
);
}
if($this->get_session_data()){
add_settings_section(
'settings_system', // ID
__('System','pinim'), // Title
array( $this, 'section_system_desc' ), // Callback
'pinim-user-auth' // Page
);
add_settings_field(
'delete_user_boards_data',
__('Delete session','pinim'),
array( $this, 'delete_session_callback' ),
'pinim-user-auth',
'settings_system'
);
}
break;
}
}
function importer_page(){
// Set class property
?>
<div class="wrap">
<h2><?php _e('Pinterest Importer','pinim');?></h2>
<?php
$pins_count = count($this->existing_pin_ids);
if ($pins_count > 1){
$rate_link_wp = 'https://wordpress.org/support/view/plugin-reviews/pinterest-importer?rate#postform';
$rate_link = '<a href="'.$rate_link_wp.'" target="_blank" href=""><i class="fa fa-star"></i> '.__('Reviewing it','pinim').'</a>';
$donate_link = '<a href="'.pinim()->donation_url.'" target="_blank" href=""><i class="fa fa-usd"></i> '.__('make a donation','pinim').'</a>';
?>
<p class="description" id="header-links">
<?php printf(__('<i class="fa fa-pinterest-p"></i>roudly already imported %1$s pins ! Happy with it ? %2$s and %3$s would help a lot!','pinim'),'<strong>'.$pins_count.'</strong>',$rate_link,$donate_link);?>
</p>
<?php
}
?>
<?php settings_errors('pinim'); ?>
<h2 class="nav-tab-wrapper">
<?php $this->importer_page_tabs($this->current_step); ?>
</h2>
<?php
switch ($this->current_step){
case 2: //fetch pins
?>
<form id="pinim-form" method="post" action="">
<?php
$this->table_pins->views();
$this->table_pins->display();
?>
</form>
<?php
break;
case 1: //'boards-settings'
?>
<form id="pinim-form" method="post" action="">
<?php
$this->table_board->views();
$this->table_board->display();
?>
</form>
<?php
break;
default: //login
?>
<form id="pinim-form" method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'pinim' );
?>
<input type="hidden" name="step" value="<?php echo $this->current_step;?>" />
<?php
do_settings_sections( 'pinim-user-auth' );
submit_button();
?>
</form>
<?php
break;
}
?>
</div>
<?php
}
function importer_page_tabs( $active_tab = '' ) {
$tabs_html = '';
$idle_class = 'nav-tab';
$active_class = 'nav-tab nav-tab-active';
$tabs = $this->importer_page_get_tabs( $active_tab );
// Loop through tabs and build navigation
foreach ( array_values( $tabs ) as $key=>$tab_data ) {
$is_current = (bool) ( $key == $active_tab );
$tab_class = $is_current ? $active_class : $idle_class;
$tabs_html .= '<a href="' . esc_url( $tab_data['href'] ) . '" class="' . esc_attr( $tab_class ) . '">' . esc_html( $tab_data['name'] ) . '</a>';
}
echo $tabs_html;
}
/**
* Get the data for the tabs in the admin area.
*
* @param string $active_tab Name of the tab that is active. Optional.
*/
function importer_page_get_tabs( $active_tab = '' ) {
$tabs = array(
'0' => array(
'href' => pinim_get_tool_page_url(array('step'=>0)),
'name' => __( 'Authentification', 'pinim' )
)
);
$tabs[1] = array(
'href' => pinim_get_tool_page_url(array('step'=>1)),
'name' => __( 'Boards Settings', 'pinim' )
);
$tabs[2] = array(
'href' => pinim_get_tool_page_url(array('step'=>2)),
'name' => __( 'Import Pins', 'pinim' )
);
return $tabs;
}
function section_general_desc(){
$session_cache = session_cache_expire();
echo '<p class="description">'.sprintf(__('Your login, password and datas retrieved from Pinterest will be stored for %1$s minutes in a PHP session. It is not stored in the database.','pinim'),$session_cache)."</p>";
}
function login_field_callback(){
$option = $this->get_session_data('login');
$has_user_datas = (null !== $this->get_session_data('user_datas'));
$disabled = disabled($has_user_datas, true, false);
printf(
'<input type="text" name="%1$s[login]" value="%2$s"%3$s/>',
'pinim_tool',
$option,
$disabled
);
}
function password_field_callback(){
$option = $this->get_session_data('password');
$has_user_datas = (null !== $this->get_session_data('user_datas'));
$disabled = disabled($has_user_datas, true, false);
printf(
'<input type="password" name="%1$s[password]" value="%2$s"%3$s/>',
'pinim_tool',
$option,
$disabled
);
}
function status_field_callback(){
if ( $user_datas = $this->get_session_data('user_datas') ){
if (isset($user_datas['image_medium_url'])){
$image = $user_datas['image_medium_url'];
printf(
'<img src="%1$s"/>',
$image
);
}
}
//names
printf(
'<p><strong>%1$s (%2$s)</strong></p>',
$user_datas['username'],
$user_datas['full_name']
);
$list = array();
//public boards
$list[] = sprintf(
'<li>'.__('%1$s public boards','pinim').'</li>',
'<strong>'.$user_datas['board_count'].'</strong>'
);
//public boards
$list[] = sprintf(
'<li>'.__('%1$s private boards','pinim').'</li>',
'<strong>'.$user_datas['secret_board_count'].'</strong>'
);
//likes
$list[] = sprintf(
'<li>'.__('%1$s likes','pinim').'</li>',
'<strong>'.$user_datas['like_count'].'</strong>'
);
echo "<ul>".implode("\n",$list)."</ul>";
//print_r($user_datas);
}
function section_system_desc(){
?><p class="description">
<?php
_e('Delete session to reset the cache. It will clear the temporary datas fetched from Pinterest but have no effects on pins already imported.','pinim');
?>
</p>
<?php
}
function delete_session_callback(){
printf(
'<p><input type="checkbox" name="%1$s[clear_cache]" value="on"/> %2$s</p>',
'pinim_tool',
__('Logout and delete cache','pinim')
);
}
function get_boards_data(){
$user_boards = null;
if (!$user_boards = $this->get_session_data('user_boards')){ //already populated
$logged = $this->do_bridge_login();
$user_boards = $this->bridge->get_user_boards();
if (is_wp_error($user_boards)){
return $user_boards;
}
//likes board
if ( $user_datas = pinim_tool_page()->get_session_data('user_datas') ){
$likes_board = array(
'name' => __('Likes','pinim'),
'id' => 'likes',
'pin_count' => $user_datas['like_count'],
'cover_images' => array(
array(
'url' => $user_datas['image_medium_url']
)
),
'url' => '/'.$user_datas['username'].'/likes'
);
$user_boards[] = $likes_board;
}
$this->set_session_data('user_boards',$user_boards);
}
return $user_boards;
}
function get_all_pins_action(){
$action = null;
//filter buttons
if (isset($_REQUEST['all_pins_action'])){
switch ($_REQUEST['all_pins_action']){
//step 2
case $this->all_action_str['import_all_pins']: //Import All Pins
$action = 'pins_import_pins';
break;
case $this->all_action_str['update_all_pins']: //Update All Pins
$action = 'pins_update_pins';
break;
}
}
return $action;
}
function get_all_boards_action(){
$action = null;
//filter buttons
if (isset($_REQUEST['all_boards_action'])){
switch ($_REQUEST['all_boards_action']){
//step 1
case $this->all_action_str['update_all_boards']: //Update all boards settings
$action = 'boards_update_settings';
break;
case $this->all_action_str['cache_all_pins']: //Cache All Pins
$action = 'boards_cache_pins';
break;
case $this->all_action_str['import_all_pins']: //Import All Pins
$action = 'boards_import_pins';
break;
}
}
return $action;
}
function get_requested_boards_ids(){
$bulk_boards_ids = array();
$bulk_boards = array();
if (isset($_POST['pinim_tool'])){
$input = $_POST['pinim_tool'];
}
if ( isset($input['boards']) ) {
$board_settings = $input['boards'];
foreach((array)$board_settings as $board){
if ( !$this->get_all_boards_action() && !isset($board['bulk']) ) continue;
$bulk_boards_ids[] = $board['id'];
}
}elseif ( isset($_REQUEST['board_ids']) ) {
$bulk_boards_ids = explode(',',$_REQUEST['board_ids']);
}
return $bulk_boards_ids;
}
function get_requested_boards(){
$bulk_boards_ids = $this->get_requested_boards_ids();
$bulk_boards = array();
foreach ((array)$bulk_boards_ids as $bulk_board_id){
$bulk_boards[] = new Pinim_Board($bulk_board_id);
}
return $bulk_boards;
}
function get_requested_pins_ids(){
$bulk_pins_ids = array();
if (isset($_POST['pinim_tool'])){