-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathfeedzy-rss-feeds-import.php
More file actions
4185 lines (3762 loc) · 140 KB
/
feedzy-rss-feeds-import.php
File metadata and controls
4185 lines (3762 loc) · 140 KB
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
/**
* The admin-specific functionality of the plugin.
*
* @link https://themeisle.com/plugins/feedzy-rss-feed/
* @since 1.0.0
*
* @package feedzy-rss-feeds
* @subpackage feedzy-rss-feeds/includes/admin
*/
/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package feedzy-rss-feeds
* @subpackage feedzy-rss-feeds/includes/admin
* @author Bogdan Preda <bogdan.preda@themeisle.com>
*/
/**
* Class Feedzy_Rss_Feeds_Import
*/
class Feedzy_Rss_Feeds_Import {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* The settings for Feedzy PRO services.
*
* @since 1.3.2
* @access public
* @var array $settings The settings for Feedzy PRO.
*/
private $settings;
/**
* The settings for Feedzy free.
*
* @access public
* @var array $settings The settings for Feedzy free.
*/
private $free_settings;
/**
* Initialize the class and set its properties.
*
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*
* @since 1.0.0
* @access public
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->settings = get_option( 'feedzy-rss-feeds-settings', array() );
$this->free_settings = get_option( 'feedzy-settings', array() );
}
/**
* Adds the class to the div that shows the upsell.
*
* @param string $css_class The class name.
* @access public
*/
public function upsell_class( $css_class ) {
if ( ! feedzy_is_pro() ) {
$css_class = 'only-pro';
}
return $css_class;
}
/**
* Adds the content to the div that shows the upsell.
*
* @param string $content The content.
* @param string $area The area identifier.
* @param string $location The location identifier.
* @access public
*/
public function upsell_content( $content, $area, $location ) {
if ( ! feedzy_is_pro() ) {
$content = '
<div class="only-pro-content">
<div class="only-pro-container">
<div class="only-pro-inner upgrade-alert">
' . __( 'This feature is available in the Pro version. Unlock more features, by', 'feedzy-rss-feeds' ) . '
<a target="_blank" href="' . esc_url( tsdk_translate_link( tsdk_utmify( FEEDZY_UPSELL_LINK, $area, $location ) ) ) . '" title="' . __( 'Buy Now', 'feedzy-rss-feeds' ) . '">' . __( 'upgrading to Feedzy Pro', 'feedzy-rss-feeds' ) . '</a>
</div>
</div>
</div>';
}
return $content;
}
/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
* @access public
*/
public function enqueue_styles() {
$screen = get_current_screen();
if (
'feedzy_imports' === $screen->post_type ||
'feedzy_categories' === $screen->post_type ||
'feedzy_page_feedzy-integration' === $screen->id ||
'feedzy_page_feedzy-settings' === $screen->id ||
'feedzy_page_feedzy-support' === $screen->id ||
'feedzy_page_feedzy-setup-wizard' === $screen->id
) {
wp_enqueue_style( $this->plugin_name, FEEDZY_ABSURL . 'css/feedzy-rss-feed-import.css', array(), $this->version, 'all' );
wp_register_style( $this->plugin_name . '_chosen', FEEDZY_ABSURL . 'includes/views/css/chosen.css', array(), $this->version, 'all' );
wp_register_script( $this->plugin_name . '_chosen_script', FEEDZY_ABSURL . 'includes/views/js/chosen.js', array( 'jquery' ), $this->version, true );
}
if ( 'feedzy_imports' === $screen->post_type ) {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
wp_enqueue_style( $this->plugin_name . '_chosen', FEEDZY_ABSURL . 'includes/views/css/chosen.css', array(), $this->version, 'all' );
wp_enqueue_style( $this->plugin_name . '_tagify', FEEDZY_ABSURL . 'includes/views/css/tagify.css', array(), $this->version, 'all' );
wp_enqueue_style( $this->plugin_name . '_metabox_edit', FEEDZY_ABSURL . 'includes/views/css/import-metabox-edit.css', array( 'wp-jquery-ui-dialog' ), $this->version, 'all' );
wp_enqueue_script( $this->plugin_name . '_chosen_script', FEEDZY_ABSURL . 'includes/views/js/chosen.js', array( 'jquery' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '_tagify_script', FEEDZY_ABSURL . 'includes/views/js/jquery.tagify.min.js', array( 'jquery' ), $this->version, true );
wp_enqueue_script(
$this->plugin_name . '_metabox_edit_script',
FEEDZY_ABSURL . 'includes/views/js/import-metabox-edit.js',
array(
'jquery',
'jquery-ui-dialog',
$this->plugin_name . '_chosen_script',
),
$this->version,
true
);
wp_localize_script(
$this->plugin_name . '_metabox_edit_script',
'feedzy',
array(
'ajax' => array(
'security' => wp_create_nonce( FEEDZY_BASEFILE ),
'url' => admin_url( 'admin-ajax.php' ),
),
'pages' => array(
'logs' => add_query_arg(
array(
'page' => 'feedzy-settings',
'tab' => 'logs',
),
admin_url( 'admin.php' )
),
),
'i10n' => array(
'importing' => __( 'Importing', 'feedzy-rss-feeds' ) . '...',
'run_now' => __( 'Run Now', 'feedzy-rss-feeds' ),
'dry_run_loading' => '<p class="hide-when-loaded">' . __( 'Processing the source and loading the items that will be imported when it runs', 'feedzy-rss-feeds' ) . '...</p>'
. '<p><b>' . __( 'Please note that if some of these items have already have been imported in previous runs with the same filters, they may be shown here but will not be imported again.', 'feedzy-rss-feeds' ) . '</b></p>'
. '<p class="loading-img hide-when-loaded"><img src="' . includes_url( 'images/wpspin-2x.gif' ) . '"></p><div></div>',
'dry_run_title' => __( 'Importable Items', 'feedzy-rss-feeds' ),
'delete_post_message' => __( 'Would you also like to delete all the imported posts for this import job?', 'feedzy-rss-feeds' ),
'media_iframe_title' => __( 'Select image', 'feedzy-rss-feeds' ),
'media_iframe_button' => __( 'Set default image', 'feedzy-rss-feeds' ),
'action_btn_text_1' => __( 'Choose image', 'feedzy-rss-feeds' ),
'action_btn_text_2' => __( 'Replace image', 'feedzy-rss-feeds' ),
'author_helper' => __( 'We display up to 100 users. If the desired username isn’t listed, type the exact existing username manually to save it.', 'feedzy-rss-feeds' ),
'clearLogButton' => __( 'Clear Log', 'feedzy-rss-feeds' ),
'goToLogsTab' => __( 'See more details', 'feedzy-rss-feeds' ),
'okButton' => __( 'Ok', 'feedzy-rss-feeds' ),
'removeErrorLogsMsg' => __( 'Removed all error logs.', 'feedzy-rss-feeds' ),
// translators: %d select images count.
'action_btn_text_3' => __( '(%d) images selected', 'feedzy-rss-feeds' ),
'importButton' => sprintf(
'<a href="#" class="page-title-action fz-export-import-btn%1$s"><span class="dashicons %2$s"></span>%3$s</a>',
! feedzy_is_pro() ? ' only-pro' : '',
feedzy_is_pro() ? 'dashicons-upload' : 'dashicons-lock',
esc_html__( 'Upload Import', 'feedzy-rss-feeds' )
),
'is_pro' => feedzy_is_pro(),
'validation_messages' => array(
'invalid_feed_url' => __( 'Invalid feed URL.', 'feedzy-rss-feeds' ),
'error_validating_feed_url' => __( 'Error validating feed URL.', 'feedzy-rss-feeds' ),
),
),
)
);
}
}
/**
* Add attributes to $item_array.
*
* @param array $item_array The item attributes array.
* @param SimplePie\Item $item The feed item.
* @param array $sc The shortcode attributes array.
* @param int $index The item number.
* @param int $item_index The real index of this item in the feed.
* @return mixed
* @since 1.0.0
* @access public
*/
public function add_data_to_item( $item_array, $item, $sc = null, $index = null, $item_index = null ) {
$item_array['item_categories'] = $this->retrieve_categories( null, $item );
// If set to true, SimplePie will return a unique MD5 hash for the item.
// If set to false, it will check <guid>, <link>, and <title> before defaulting to the hash.
$item_array['item_id'] = $item->get_id( false );
$item_array['item'] = $item;
$item_array['item_index'] = $item_index;
$item_array = $this->handle_youtube_content( $item_array, $item, $sc );
return $item_array;
}
/**
* Fetches additional information for each item.
*
* @param array<string, string > $item_array The item attributes array.
* @param SimplePie\Item $item The feed item.
* @param array<string, mixed> $sc The shortcode attributes array. This will be empty through the block editor.
*
* @return array<string, string>
*/
private function handle_youtube_content( $item_array, $item, $sc ) {
$url = '';
if ( array_key_exists( 'item_url', $item_array ) ) {
$url = $item_array['item_url'];
} elseif ( $item ) {
$url = $item->get_permalink();
}
if ( empty( $url ) ) {
return $item_array;
}
$host = wp_parse_url( $url, PHP_URL_HOST );
// Remove all dots in the hostname so that shortforms such as youtu.be can also be resolved.
$host = str_replace( array( '.', 'www' ), '', $host );
if ( ! in_array( $host, array( 'youtubecom', 'youtube' ), true ) ) {
// Not a YouTube link, return the item array as is.
return $item_array;
}
$tags = $item->get_item_tags( \SimplePie\SimplePie::NAMESPACE_MEDIARSS, 'group' );
$desc = '';
if ( $tags ) {
$desc_tag = $tags[0]['child'][ \SimplePie\SimplePie::NAMESPACE_MEDIARSS ]['description'];
if ( $desc_tag ) {
$desc = $desc_tag[0]['data'];
}
}
if ( ! empty( $desc ) ) {
if ( empty( $item_array['item_content'] ) ) {
$item_array['item_content'] = $desc;
}
if (
( empty( $sc ) || 'yes' === $sc['summary'] ) &&
empty( $item_array['item_description'] )
) {
if (
is_numeric( $sc['summarylength'] ) &&
strlen( $desc ) > $sc['summarylength']
) {
$desc = preg_replace( '/\s+?(\S+)?$/', '', substr( $desc, 0, $sc['summarylength'] ) ) . ' […]';
}
$item_array['item_description'] = $desc;
}
}
$embed_video_shortcode = '[embed]' . $url . '[/embed]';
$should_overwrite = str_contains( $item_array['item_content'], 'Post Content' );
$item_array['item_content'] = ( $should_overwrite ? '' : $item_array['item_content'] ) . $embed_video_shortcode;
return $item_array;
}
/**
* Retrieve the categories.
*
* @param string $dumb The initial categories (only a placeholder argument for the filter).
* @param SimplePie\Item $item The feed item.
*
* @return string
* @access public
*/
public function retrieve_categories( $dumb, $item ) {
$cats = array();
$categories = $item->get_categories();
if ( $categories ) {
foreach ( $categories as $category ) {
$cats[] = $category->get_label();
}
}
return apply_filters( 'feedzy_categories', implode( ', ', $cats ), $cats, $item );
}
/**
* Register a new post type for feed imports.
*
* @since 1.2.0
* @access public
*/
public function register_import_post_type() {
$labels = array(
'name' => __( 'Import Posts', 'feedzy-rss-feeds' ),
'singular_name' => __( 'Import Post', 'feedzy-rss-feeds' ),
'add_new' => __( 'New Import', 'feedzy-rss-feeds' ),
'add_new_item' => __( 'New Import', 'feedzy-rss-feeds' ),
'edit_item' => false,
'new_item' => __( 'New Import Post', 'feedzy-rss-feeds' ),
'view_item' => __( 'View Import', 'feedzy-rss-feeds' ),
'search_items' => __( 'Search Imports', 'feedzy-rss-feeds' ),
'not_found' => __( 'No imports found', 'feedzy-rss-feeds' ),
'not_found_in_trash' => __( 'No imports in the trash', 'feedzy-rss-feeds' ),
);
$supports = array(
'title',
);
$args = array(
'labels' => $labels,
'supports' => false,
'public' => true,
'exclude_from_search' => true,
'publicly_queryable' => false,
'capability_type' => 'post',
'show_in_nav_menus' => false,
'rewrite' => array(
'slug' => 'feedzy-import',
),
'show_in_menu' => 'feedzy-admin-menu',
'show_ui' => feedzy_current_user_can(),
'show_in_rest' => true,
);
$args = apply_filters( 'feedzy_imports_args', $args );
register_post_type( 'feedzy_imports', $args );
// Register user meta field.
register_meta(
'user',
'feedzy_import_tour',
array(
'type' => 'boolean',
'description' => __( 'Show tour for Feedzy.', 'feedzy-rss-feeds' ),
'show_in_rest' => true,
'single' => true,
'default' => true,
)
);
register_meta(
'user',
'feedzy_hide_action_message',
array(
'type' => 'boolean',
'description' => __( 'Show intro message for Feedzy action popup.', 'feedzy-rss-feeds' ),
'show_in_rest' => true,
'single' => true,
'default' => false,
)
);
}
/**
* Method to add a meta box to `feedzy_imports`
* custom post type.
*
* @param string $post_type The post type.
* @param \WP_Post $post The post.
*
* @return void
*
* @since 1.2.0
* @access public
*/
public function add_feedzy_import_metaboxes( $post_type, $post ) {
if ( 'feedzy_imports' !== $post_type || empty( $post ) ) {
return;
}
add_meta_box(
'feedzy_import_feeds',
__( 'Feed Import Options', 'feedzy-rss-feeds' ),
array(
$this,
'feedzy_import_feed_options',
),
'feedzy_imports',
'normal',
'high'
);
}
/**
* Method to display metabox for import post type.
*
* @return void
*
* @since 1.2.0
* @access public
*/
public function feedzy_import_feed_options() {
global $post, $pagenow;
$args = array(
'post_type' => 'feedzy_categories',
'posts_per_page' => 100,
);
$feed_categories = get_posts( $args );
$post_types = get_post_types( '', 'names' );
$post_types = array_diff( $post_types, array( 'feedzy_imports', 'feedzy_categories' ) );
$published_status = array( 'publish', 'draft' );
$authors = get_users( array( 'number' => 100 ) );
$authors_array = array();
foreach ( $authors as $author ) {
$authors_array[] = $author->user_login;
}
$import_post_type = get_post_meta( $post->ID, 'import_post_type', true );
$import_post_term = get_post_meta( $post->ID, 'import_post_term', true );
if ( metadata_exists( $import_post_type, $post->ID, 'import_post_status' ) ) {
$import_post_status = get_post_meta( $post->ID, 'import_post_status', true );
} else {
add_post_meta( $post->ID, 'import_post_status', 'publish' );
$import_post_status = get_post_meta( $post->ID, 'import_post_status', true );
}
$source = get_post_meta( $post->ID, 'source', true );
$inc_key = get_post_meta( $post->ID, 'inc_key', true );
$exc_key = get_post_meta( $post->ID, 'exc_key', true );
$inc_on = get_post_meta( $post->ID, 'inc_on', true );
$exc_on = get_post_meta( $post->ID, 'exc_on', true );
$import_title = get_post_meta( $post->ID, 'import_post_title', true );
$import_date = get_post_meta( $post->ID, 'import_post_date', true );
$post_excerpt = get_post_meta( $post->ID, 'import_post_excerpt', true );
$import_content = get_post_meta( $post->ID, 'import_post_content', true );
$import_item_img_url = get_post_meta( $post->ID, 'import_use_external_image', true );
$import_item_img_url = 'yes' === $import_item_img_url ? 'checked' : '';
$import_featured_img = get_post_meta( $post->ID, 'import_post_featured_img', true );
$import_remove_duplicates = get_post_meta( $post->ID, 'import_remove_duplicates', true );
$import_remove_duplicates = 'yes' === $import_remove_duplicates || 'post-new.php' === $pagenow ? 'checked' : '';
$import_selected_language = get_post_meta( $post->ID, 'language', true );
$from_datetime = get_post_meta( $post->ID, 'from_datetime', true );
$to_datetime = get_post_meta( $post->ID, 'to_datetime', true );
$import_auto_translation = get_post_meta( $post->ID, 'import_auto_translation', true );
$import_auto_translation = 'yes' === $import_auto_translation ? 'checked' : '';
$import_translation_lang = get_post_meta( $post->ID, 'import_auto_translation_lang', true );
$mark_duplicate_tag = get_post_meta( $post->ID, 'mark_duplicate_tag', true );
$import_post_author = get_post_meta( $post->ID, 'import_post_author', true );
$filter_conditions = get_post_meta( $post->ID, 'filter_conditions', true );
$import_remove_html = get_post_meta( $post->ID, 'import_remove_html', true );
$import_remove_html = 'yes' === $import_remove_html ? 'checked' : '';
$import_order = get_post_meta( $post->ID, 'import_order', true );
if ( empty( $filter_conditions ) ) {
$filter_conditions = apply_filters(
'feedzy_filter_conditions_migration',
array(
'keywords_inc' => $inc_key,
'keywords_exc' => $exc_key,
'keywords_inc_on' => $inc_on,
'keywords_exc_on' => $exc_on,
'from_datetime' => $from_datetime,
'to_datetime' => $to_datetime,
)
);
}
/**
* This code snippet retrieves the post author for backward compatibility for existing imports as well as for any new imports.
* It checks if the $import_post_author variable is not empty, otherwise it defaults to the current post's author.
*/
$import_post_author = ! empty( $import_post_author ) ? $import_post_author : $post->post_author;
$author = get_user_by( 'ID', $import_post_author );
if ( $author ) {
$import_post_author = $author->user_login;
if ( ! in_array( $import_post_author, $authors_array, true ) ) {
$authors_array[] = $import_post_author;
}
}
// default values so that post is not created empty.
if ( empty( $import_title ) ) {
$import_title = '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_title%22%2C%22data%22%3A%7B%7D%7D%5D"}]]';
}
if ( empty( $import_content ) ) {
$import_content = '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_content%22%2C%22data%22%3A%7B%7D%7D%5D"}]]';
}
if ( feedzy_is_pro() && empty( $import_post_term ) && 'post-new.php' === $pagenow ) {
$import_post_term = '[#auto_categories]';
}
if ( feedzy_is_pro() ) {
$custom_terms = array(
'[#item_categories]' => __( 'Item Categories', 'feedzy-rss-feeds' ),
'[#auto_categories]' => __( 'Auto Categories by keyword', 'feedzy-rss-feeds' ),
);
} elseif ( ! feedzy_is_pro() ) {
$custom_terms = array(
'[#item_categories]' => __( 'Item Categories', 'feedzy-rss-feeds' ) . sprintf( '<span class="pro-label">%s</span>', __( 'PRO', 'feedzy-rss-feeds' ) ),
'[#auto_categories]' => __( 'Auto Categories by keyword', 'feedzy-rss-feeds' ) . sprintf( '<span class="pro-label">%s</span>', __( 'PRO', 'feedzy-rss-feeds' ) ),
);
}
$custom_post_term = wp_json_encode( $custom_terms );
$import_link_author_admin = get_post_meta( $post->ID, 'import_link_author_admin', true );
$import_link_author_public = get_post_meta( $post->ID, 'import_link_author_public', true );
// Admin / Public.
$import_link_author = array( '', '' );
if ( 'yes' === $import_link_author_admin ) {
$import_link_author[0] = 'checked';
}
if ( 'yes' === $import_link_author_public ) {
$import_link_author[1] = 'checked';
}
// default values when creating a import.
if ( 'post-new.php' === $pagenow ) {
$import_date = '[#item_date]';
$import_featured_img = '[[{"value":"%5B%7B%22id%22%3A%22%22%2C%22tag%22%3A%22item_image%22%2C%22data%22%3A%7B%7D%7D%5D"}]]';
}
// maybe more options are required from pro?
$pro_options = apply_filters( 'feedzy_metabox_options', array(), $post->ID );
$import_custom_fields = get_post_meta( $post->ID, 'imports_custom_fields', true );
$custom_fields_actions = get_post_meta( $post->ID, 'imports_custom_field_actions', true );
$import_feed_limit = get_post_meta( $post->ID, 'import_feed_limit', true );
if ( empty( $import_feed_limit ) ) {
$import_feed_limit = 10;
}
$default_thumbnail_id = 0;
$inherited_thumbnail_id = ! empty( $this->free_settings['general']['default-thumbnail-id'] ) ? (int) $this->free_settings['general']['default-thumbnail-id'] : 0;
$custom_thumbnail_id = get_post_meta( $post->ID, 'default_thumbnail_id', true );
if ( is_numeric( $custom_thumbnail_id ) ) {
$default_thumbnail_id = $custom_thumbnail_id;
}
if ( feedzy_is_pro() ) {
$import_schedule = array(
'fz_cron_schedule' => ! empty( $this->free_settings['general']['fz_cron_schedule'] ) ? $this->free_settings['general']['fz_cron_schedule'] : '',
);
}
$fz_cron_schedule = get_post_meta( $post->ID, 'fz_cron_schedule', true );
if ( ! empty( $fz_cron_schedule ) ) {
$import_schedule['fz_cron_schedule'] = $fz_cron_schedule;
}
$post_status = $post->post_status;
$nonce = wp_create_nonce( FEEDZY_BASEFILE );
$invalid_source_msg = apply_filters( 'feedzy_get_source_validity_error', '', $post );
$output = '
<input type="hidden" name="feedzy_category_meta_noncename" id="feedzy_category_meta_noncename" value="' . $nonce . '" />
';
add_thickbox();
include FEEDZY_ABSPATH . '/includes/views/import-metabox-edit.php';
echo wp_kses( $output, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
}
/**
* Change number of posts imported.
*
* @param int $range The desired range.
* @param \WP_Post $post The post.
*
* @return int The range. Capped if the user is not PRO.
*/
public function items_limit( $range, $post ) {
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
if ( ! feedzy_is_pro() ) {
$range = range( 10, 10, 10 );
}
return $range;
}
/**
* Save method for custom post type
* import feeds.
*
* @param integer $post_id The post ID.
* @param object $post The post object.
*
* @return bool|integer True if it is a success.
* @since 1.2.0
* @access public
*/
public function save_feedzy_import_feed_meta( $post_id, $post ) {
if ( empty( $_POST ) ) {
return $post_id;
}
if ( ! isset( $_POST['feedzy_post_nonce'] ) ) {
return $post_id;
}
if (
get_post_type( $post_id ) !== 'feedzy_imports' ||
( ! defined( 'TI_UNIT_TESTING' ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['feedzy_post_nonce'] ) ), 'feedzy_post_nonce' ) ) ||
! current_user_can( 'edit_post', $post_id )
) {
return $post_id;
}
$data_meta = array();
if ( isset( $_POST['feedzy_meta_data'] ) && is_array( $_POST['feedzy_meta_data'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( wp_unslash( $_POST['feedzy_meta_data'] ) as $key => $val ) {
if ( is_array( $val ) ) {
foreach ( $val as $sub_key => $sub_val ) {
$data_meta[ sanitize_text_field( $key ) ][ sanitize_text_field( $sub_key ) ] = wp_kses( $sub_val, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
}
} else {
if ( 'import_post_content' === $key ) {
$val = feedzy_custom_tag_escape( $val );
} elseif ( 'default_thumbnail_id' === $key && ! empty( $val ) ) {
$val = explode( ',', $val );
$val = array_map( 'intval', $val );
} else {
$val = wp_kses( $val, apply_filters( 'feedzy_wp_kses_allowed_html', array() ) );
}
$data_meta[ sanitize_text_field( $key ) ] = $val;
}
}
}
$global_cron_schedule = ! empty( $this->free_settings['general']['fz_cron_schedule'] ) ? $this->free_settings['general']['fz_cron_schedule'] : '';
if (
empty( $data_meta['fz_cron_schedule'] ) || $global_cron_schedule === $data_meta['fz_cron_schedule']
) {
// Remove scheduled cron settings if they are equal to the global settings.
unset( $data_meta['fz_cron_schedule'] );
}
$custom_fields_keys = array();
if ( isset( $_POST['custom_vars_key'] ) && is_array( $_POST['custom_vars_key'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( wp_unslash( $_POST['custom_vars_key'] ) as $key => $val ) {
$custom_fields_keys[ sanitize_text_field( $key ) ] = esc_html( $val );
}
}
$custom_fields_values = array();
if ( isset( $_POST['custom_vars_value'] ) && is_array( $_POST['custom_vars_value'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
foreach ( wp_unslash( $_POST['custom_vars_value'] ) as $key => $val ) {
$custom_fields_values[ sanitize_text_field( $key ) ] = esc_html( $val );
}
}
$custom_fields = array();
foreach ( $custom_fields_keys as $index => $key_value ) {
$value = '';
if ( isset( $custom_fields_values[ $index ] ) ) {
$value = implode( ',', (array) $custom_fields_values[ $index ] );
}
$custom_fields[ $key_value ] = $value;
}
if ( 'revision' !== $post->post_type ) {
// delete these checkbox related fields; if checked, they will be added below.
delete_post_meta( $post_id, 'import_link_author_admin' );
delete_post_meta( $post_id, 'import_link_author_public' );
// we will activate this import only if the source has no invalid URL(s).
$source_is_valid = false;
// Check feeds remove duplicates checkbox checked OR not.
$data_meta['import_remove_duplicates'] = isset( $data_meta['import_remove_duplicates'] ) ? $data_meta['import_remove_duplicates'] : 'no';
// Check feeds automatically translation checkbox checked OR not.
$data_meta['import_auto_translation'] = isset( $data_meta['import_auto_translation'] ) ? $data_meta['import_auto_translation'] : 'no';
// Check feeds external image URL checkbox checked OR not.
$data_meta['import_use_external_image'] = isset( $data_meta['import_use_external_image'] ) ? $data_meta['import_use_external_image'] : 'no';
// Check feeds remove html checkbox checked OR not.
$data_meta['import_remove_html'] = isset( $data_meta['import_remove_html'] ) ? $data_meta['import_remove_html'] : 'no';
$data_meta['import_post_term'] = isset( $data_meta['import_post_term'] ) ? $data_meta['import_post_term'] : '';
// If it is filter_conditions we want to escape it.
if ( isset( $data_meta['filter_conditions'] ) ) {
$data_meta['filter_conditions'] = wp_slash( $data_meta['filter_conditions'] );
}
// $data_meta['feedzy_post_author'] should be the author username. We convert it to the author ID.
if ( ! empty( $data_meta['import_post_author'] ) ) {
$author = get_user_by( 'login', $data_meta['import_post_author'] );
if ( $author ) {
$data_meta['import_post_author'] = $author->ID;
} else {
$data_meta['import_post_author'] = '';
}
}
foreach ( $data_meta as $key => $value ) {
$value = is_array( $value ) ? implode( ',', $value ) : implode( ',', (array) $value );
if ( 'source' === $key ) {
// check if the source is valid.
$invalid_urls = apply_filters( 'feedzy_check_source_validity', $value, $post_id, true, false );
$source_is_valid = empty( $invalid_urls );
}
if ( 'import_post_content' === $key ) {
add_filter( 'wp_kses_allowed_html', array( $this, 'feedzy_wp_kses_allowed_html' ) );
$value = feedzy_custom_tag_escape( $value );
} else {
$value = wp_kses( $value, wp_kses_allowed_html( 'post' ) );
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value );
}
if ( ! $value ) {
if ( 'default_thumbnail_id' === $key && '0' === $value ) { // Mark the feed as having no default fallback image (including the global fallback).
continue;
}
delete_post_meta( $post_id, $key );
}
}
// Added this to activate post if publish is clicked and sometimes it does not change status.
if (
$source_is_valid && isset( $_POST['custom_post_status'] ) &&
'Publish' === sanitize_text_field( $_POST['custom_post_status'] )
) {
$activate = array(
'ID' => $post_id,
'post_status' => 'publish',
);
remove_action( 'save_post_feedzy_imports', array( $this, 'save_feedzy_import_feed_meta' ), 1, 2 );
wp_update_post( $activate );
add_action( 'save_post_feedzy_imports', array( $this, 'save_feedzy_import_feed_meta' ), 1, 2 );
}
// Clear the import job cron schedule if it exists.
Feedzy_Rss_Feeds_Util_Scheduler::clear_scheduled_hook( 'feedzy_cron', array( 100, $post_id ) );
do_action( 'feedzy_save_fields', $post_id, $post );
}
return true;
}
/**
* Redirect save post to post listing.
*
* @access public
*
* @param string $location The URL to redirect to.
* @param int $post_id The post ID.
*
* @return string The URL to redirect to. Fallback to edit.php?post_type=feedzy_imports.
*/
public function redirect_post_location( $location, $post_id ) {
$post = get_post( $post_id );
if ( 'feedzy_imports' === $post->post_type ) {
// if invalid source has been found, redirect back to edit screen where errors can be shown.
$invalid = get_post_meta( $post_id, '__transient_feedzy_invalid_source', true );
$invalid_dc_namespace = get_post_meta( $post_id, '__transient_feedzy_invalid_dc_namespace', true );
$invalid_source_errors = get_post_meta( $post_id, '__transient_feedzy_invalid_source_errors', true );
if ( empty( $invalid ) && empty( $invalid_dc_namespace ) && empty( $invalid_source_errors ) ) {
return admin_url( 'edit.php?post_type=feedzy_imports' );
}
}
return $location;
}
/**
* Method to add header columns to import feeds table.
*
* @param array<string, string> $columns The columns array.
*
* @return array<string, string> The columns.
*
* @since 1.2.0
* @access public
*/
public function feedzy_import_columns( $columns ) {
$columns['title'] = __( 'Import Title', 'feedzy-rss-feeds' );
$new_columns = $this->array_insert_before( 'date', $columns, 'feedzy-source', __( 'Source', 'feedzy-rss-feeds' ) );
if ( $new_columns ) {
$columns = $new_columns;
} else {
$columns['feedzy-source'] = __( 'Source', 'feedzy-rss-feeds' );
}
$new_columns = $this->array_insert_before( 'date', $columns, 'feedzy-status', __( 'Current Status', 'feedzy-rss-feeds' ) );
if ( $new_columns ) {
$columns = $new_columns;
} else {
$columns['feedzy-status'] = __( 'Current Status', 'feedzy-rss-feeds' );
}
$new_columns = $this->array_insert_before( 'date', $columns, 'feedzy-next_run', __( 'Next Run', 'feedzy-rss-feeds' ) );
if ( $new_columns ) {
$columns = $new_columns;
} else {
$columns['feedzy-next_run'] = __( 'Next Run', 'feedzy-rss-feeds' );
}
$new_columns = $this->array_insert_before( 'date', $columns, 'feedzy-last_run', __( 'Last Run Status', 'feedzy-rss-feeds' ) );
if ( $new_columns ) {
$columns = $new_columns;
} else {
$columns['feedzy-last_run'] = __( 'Last Run Status', 'feedzy-rss-feeds' );
}
unset( $columns['date'] );
return $columns;
}
/**
* Utility method to insert before specific key in an associative array.
*
* @param string $key The key before which to insert.
* @param array $from_array The array in which to insert the new key.
* @param string $new_key The new key name.
* @param mixed $new_value The new key value.
*
* @return array|bool The modified array. False otherwise.
*
* @since 1.2.0
* @access public
*/
protected function array_insert_before( $key, &$from_array, $new_key, $new_value ) {
if ( array_key_exists( $key, $from_array ) ) {
$new = array();
foreach ( $from_array as $k => $value ) {
if ( $k === $key ) {
$new[ $new_key ] = $new_value;
}
$new[ $k ] = $value;
}
return $new;
}
return false;
}
/**
* Method to add a columns in the import feeds table.
*
* @param string $column The current column to check.
* @param integer $post_id The post ID.
*
* @return void
*
* @since 1.2.0
* @access public
*/
public function manage_feedzy_import_columns( $column, $post_id ) {
global $post;
switch ( $column ) {
case 'feedzy-source':
$src = get_post_meta( $post_id, 'source', true );
// if the source is a category, link it.
if ( strpos( $src, 'http' ) === false && strpos( $src, 'https' ) === false ) {
if ( function_exists( 'feedzy_amazon_get_locale_hosts' ) ) {
$amazon_hosts = feedzy_amazon_get_locale_hosts();
$src_path = 'webservices.' . wp_parse_url( $src, PHP_URL_PATH );
if ( in_array( $src_path, $amazon_hosts, true ) ) {
$src = sprintf( '%s: %s%s%s', __( 'Amazon Product Advertising API', 'feedzy-rss-feeds' ), '<a>', $src, '</a>' );
} else {
$src = sprintf( '%s: %s%s%s', __( 'Feed Group', 'feedzy-rss-feeds' ), '<a href="' . admin_url( 'edit.php?post_type=feedzy_categories' ) . '" target="_blank">', $src, '</a>' );
}
} elseif ( empty( $src ) ) {
$src = __( 'No Source Configured', 'feedzy-rss-feeds' );
} else {
$src = sprintf( '%s: %s%s%s', __( 'Feed Group', 'feedzy-rss-feeds' ), '<a href="' . admin_url( 'edit.php?post_type=feedzy_categories' ) . '" target="_blank">', $src, '</a>' );
}
} else {
// else link it to the feed but shorten it if it is too long.
$too_long = 65;
$src = sprintf( '%s%s%s', '<a href="' . $src . '" target="_blank" title="' . __( 'Click to view', 'feedzy-rss-feeds' ) . '">', ( strlen( $src ) > $too_long ? substr( $src, 0, $too_long ) . '...' : $src ), '</a>' );
}
echo wp_kses_post( $src );
break;
case 'feedzy-status':
$status = $post->post_status;
if ( empty( $status ) ) {
esc_html_e( 'Undefined', 'feedzy-rss-feeds' );
} else {
if ( 'publish' === $status ) {
$checked = 'checked';
} else {
$checked = '';
}
echo wp_kses(
'<div class="switch">
<input id="feedzy-toggle_' . $post->ID . '" class="feedzy-toggle feedzy-toggle-round" type="checkbox" value="' . $post->ID . '" ' . $checked . '>
<label for="feedzy-toggle_' . $post->ID . '"></label>
<span class="feedzy-spinner spinner"></span>
</div>',
apply_filters( 'feedzy_wp_kses_allowed_html', array() )
);
}
break;
case 'feedzy-last_run':
$last = get_post_meta( $post_id, 'last_run', true );
$msg = __( 'Never Run', 'feedzy-rss-feeds' );
if ( $last ) {
$now = new DateTime();
$then = new DateTime();
$then = $then->setTimestamp( $last );
$in = $now->diff( $then );
$msg = sprintf(
// translators: %1$d: number of hours, %2$d: number of minutes.
__( 'Ran %1$d hours %2$d minutes ago', 'feedzy-rss-feeds' ),
$in->format( '%h' ),
$in->format( '%i' )
);
}
$msg .= $this->get_last_run_details( $post_id );
echo ( $msg ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if ( 'publish' === $post->post_status ) {
printf( '<p><input type="button" class="button button-primary feedzy-run-now" data-id="%d" value="%s"></p>', esc_attr( $post_id ), esc_attr__( 'Run Now', 'feedzy-rss-feeds' ) );
}
break;
case 'feedzy-next_run':
$next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron', array( 100, $post_id ) );
if ( ! $next ) {
$next = Feedzy_Rss_Feeds_Util_Scheduler::is_scheduled( 'feedzy_cron' );
}
if ( is_numeric( $next ) ) {
echo wp_kses_post( human_time_diff( $next, time() ) );
} elseif ( $next ) {
echo esc_html__( 'in-progress', 'feedzy-rss-feeds' );
}
break;
default:
break;
}
}
/**
* Generate the markup that displays the status.
*
* @param integer $post_id The post ID.
*
* @return string The HTML markup.
*
* @access private
*/
private function get_last_run_details( $post_id ) {
$msg = '';
$import_info = get_post_meta( $post_id, 'import_info', true );
$status = array(
'total' => '-',
'items' => '-',
'duplicates' => '-',
'cumulative' => '-',
);
if ( $import_info ) {
$status = array(
'total' => 0,
'items' => 0,
'duplicates' => 0,
'cumulative' => 0,
);
$status = $this->get_complete_import_status( $post_id );
}
// link to the posts listing for this job.
$job_linked_posts = add_query_arg(
array(
'feedzy_job_id' => $post_id,
'post_type' => get_post_meta(
$post_id,
'import_post_type',
true
),