-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-post-collection.php
More file actions
4192 lines (3671 loc) · 128 KB
/
Copy pathclass-post-collection.php
File metadata and controls
4192 lines (3671 loc) · 128 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
// phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.NonceVerification.Missing,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Request reads are validated by action handlers and migration/status reads intentionally use prepared direct queries.
/**
* Post Collection
*
* This contains the post collection functions.
*
* @package Post_Collection
*/
namespace PostCollection;
defined( 'ABSPATH' ) || exit;
use Friends\Friends;
use WP_HTML_Tag_Processor;
/**
* This is the class for the downloading and storing posts.
*
* @since 0.1
*
* @package Post_Collection
* @author Alex Kirk
*/
class Post_Collection {
const CPT = 'collected_post';
const COLLECTION_TAXONOMY = 'post_collection';
/**
* Nonce action for the frontend admin-ajax endpoints.
*/
const AJAX_NONCE = 'post-collection-ajax';
/**
* Whether to cache the retrieved users
*
* @var boolean
*/
public static $cache = true;
/**
* Contains a reference to the Friends class.
*
* @var Friends
*/
private $friends;
/**
* Contains the site configs
*
* @var array
*/
private $site_configs = array();
/**
* Contains the article notes instance
*
* @var Article_Notes
*/
private $article_notes;
/**
* Contains the abilities integration instance.
*
* @var Post_Collection_Abilities|null
*/
private $abilities;
/**
* Constructor
*
* @param Friends|null $friends A reference to the Friends object (optional).
*/
public function __construct( ?Friends $friends = null ) {
$this->friends = $friends;
$this->article_notes = new Article_Notes( $this );
if ( class_exists( __NAMESPACE__ . '\Post_Collection_Abilities' ) ) {
$this->abilities = new Post_Collection_Abilities( $this );
}
$this->register_hooks();
if ( did_action( 'init' ) ) {
$this->boot_app();
} else {
add_action( 'init', array( $this, 'boot_app' ), 0 );
}
}
/**
* Initialize the standalone frontend app.
*/
public function boot_app() {
if ( class_exists( __NAMESPACE__ . '\Post_Collection_App' ) ) {
Post_Collection_App::boot( $this );
}
}
/**
* Get the tag taxonomy to use for post collections.
*
* @return string The taxonomy name.
*/
public function get_tag_taxonomy() {
if ( $this->friends && defined( 'Friends\Friends::TAG_TAXONOMY' ) ) {
return Friends::TAG_TAXONOMY;
}
return 'friend_tag';
}
/**
* Get the required role capability for Friends functionality.
*
* @return string The capability name.
*/
public function get_required_role() {
if ( $this->friends && defined( 'Friends\Friends::REQUIRED_ROLE' ) ) {
return Friends::REQUIRED_ROLE;
}
return 'edit_private_posts';
}
/**
* Get the Friends version or fallback.
*
* @return string The version string.
*/
public function get_friends_version() {
if ( $this->friends && defined( 'Friends\Friends::VERSION' ) ) {
return Friends::VERSION;
}
return '1.0';
}
/**
* Check if we're on the Friends frontend.
*
* @return bool True if on Friends frontend.
*/
public function is_on_friends_frontend() {
if ( $this->friends ) {
return Friends::on_frontend();
}
return false;
}
/**
* Check if a URL is valid using Friends method if available.
*
* @param string $url The URL to check.
* @return bool True if URL is valid.
*/
public function check_url( $url ) {
if ( $this->friends ) {
return Friends::check_url( $url );
}
return filter_var( $url, FILTER_VALIDATE_URL ) !== false;
}
/**
* Parse a shared text payload into a URL and optional title.
*
* @param string $text The shared text.
* @return array Parsed URL and title.
*/
public function parse_shared_url_payload( $text ) {
$text = trim( (string) $text );
if ( $this->check_url( $text ) ) {
return array(
'url' => $text,
'title' => '',
);
}
if ( preg_match( '~https?://[^\s<>"\']+~i', $text, $matches, PREG_OFFSET_CAPTURE ) ) {
$url = rtrim( $matches[0][0], '.,;:!?)]}' );
$title = trim( substr( $text, 0, $matches[0][1] ) );
$title = preg_replace( '/\s+/', ' ', $title );
return array(
'url' => $url,
'title' => $title && strlen( $title ) <= 160 ? $title : '',
);
}
return array(
'url' => $text,
'title' => '',
);
}
/**
* Truncate URL using Friends method if available.
*
* @param string $url The URL to truncate.
* @return string The truncated URL.
*/
public function url_truncate( $url ) {
if ( $this->friends ) {
return Friends::url_truncate( $url );
}
// Simple fallback truncation
if ( strlen( $url ) > 50 ) {
return substr( $url, 0, 47 ) . '...';
}
return $url;
}
/**
* Find post ID by URL.
*
* @param string $url The URL to find.
* @param int $user_id The post author ID.
* @return int|null The post ID if found, null otherwise.
*/
public function url_to_postid( $url, $user_id ) {
if ( $this->friends ) {
return $this->friends->feed->url_to_postid( $url, $user_id );
}
// Fallback: query by guid field.
global $wpdb;
$post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid = %s AND post_author = %d AND post_type = %s LIMIT 1",
$url,
$user_id,
self::CPT
)
);
return $post_id ? intval( $post_id ) : null;
}
/**
* Get the article notes instance.
*
* @return Article_Notes The article notes instance.
*/
public function get_article_notes() {
return $this->article_notes;
}
/**
* Get the post author name.
*
* @param \WP_Post $post The post object.
* @return string The author display name.
*/
public function get_post_author_name( \WP_Post $post ) {
if ( $this->friends && class_exists( '\Friends\User' ) ) {
$author = \Friends\User::get_post_author( $post );
return $author->display_name;
}
return get_the_author_meta( 'display_name', $post->post_author );
}
/**
* Register the WordPress hooks
*/
private function register_hooks() {
if ( did_action( 'init' ) ) {
$this->register_custom_post_type();
$this->add_revision_support();
} else {
add_action( 'init', array( $this, 'register_custom_post_type' ) );
add_action( 'init', array( $this, 'add_revision_support' ) );
}
add_filter( 'friends_author_post_type', array( $this, 'filter_author_post_type' ), 10, 2 );
add_filter( 'friends_frontend_post_types', array( $this, 'add_frontend_post_types' ) );
add_action( 'pre_get_posts', array( $this, 'filter_collection_friend_posts_query' ), 20 );
add_action( 'tool_box', array( $this, 'toolbox_bookmarklet' ) );
add_filter( 'user_row_actions', array( $this, 'user_row_actions' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 50 );
add_action( 'admin_bar_menu', array( $this, 'admin_bar_new_content' ), 72 );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99999 );
add_action( 'wp_loaded', array( $this, 'save_url_endpoint' ), 100 );
add_filter( 'get_edit_user_link', array( $this, 'edit_post_collection_link' ), 10, 2 );
add_action( 'friend_post_edit_link', array( $this, 'allow_post_editing' ), 10, 2 );
add_action( 'friends_show_author_edit', array( $this, 'friends_show_author_edit' ), 10, 2 );
add_action( 'friends_entry_dropdown_menu', array( $this, 'entry_dropdown_menu' ), 10, 2 );
add_action( 'friends_friend_feed_viewable', array( $this, 'friends_friend_feed_viewable' ), 10, 2 );
add_filter( 'friends_friend_posts_query_viewable', array( $this, 'collection_friend_posts_query_viewable' ), 10, 2 );
add_action( 'friend_user_role_name', array( $this, 'friend_user_role_name' ), 10, 2 );
add_filter( 'friends_plugin_roles', array( $this, 'associate_friend_user_role' ) );
add_action( 'friends_override_author_name', array( $this, 'friends_override_author_name' ), 15, 3 );
add_filter( 'send_to_e_reader_ebook_author', array( $this, 'filter_ebook_author' ), 10, 2 );
add_action( 'friends_widget_friend_list_after', array( $this, 'friends_widget_friend_list_after' ), 10, 2 );
add_action( 'friends_author_header', array( $this, 'friends_author_header' ) );
add_action( 'friends_author_header', array( $this, 'enter_url_field' ) );
add_action( 'friends_post_footer_first', array( $this, 'share_button' ) );
add_action( 'friends_feed_table_header', array( $this, 'feed_table_header' ) );
add_action( 'friends_feed_table_row', array( $this, 'feed_table_row' ), 10, 2 );
add_action( 'friends_feed_list_item', array( $this, 'feed_list_item' ), 10, 2 );
add_action( 'friends_process_feed_item_submit', array( $this, 'feed_item_submit' ), 10, 2 );
add_action( 'friends_modify_feed_item', array( $this, 'modify_feed_item' ), 10, 4 );
add_filter( 'friends_can_update_modified_feed_posts', array( $this, 'can_update_modified_feed_posts' ), 10, 5 );
add_action( 'friends_after_register_feed_taxonomy', array( $this, 'after_register_feed_taxonomy' ) );
add_filter( 'my_apps_plugins', array( $this, 'my_apps_plugins' ) );
add_action( 'wp_ajax_post-collection-mark-publish', array( $this, 'wp_ajax_mark_publish' ) );
add_action( 'wp_ajax_post-collection-mark-private', array( $this, 'wp_ajax_mark_private' ) );
add_action( 'wp_ajax_post-collection-change-author', array( $this, 'wp_ajax_change_author' ) );
add_action( 'wp_ajax_post-collection-save-to-collection', array( $this, 'wp_ajax_save_to_collection' ) );
add_action( 'wp_ajax_post-collection-fetch-full-content', array( $this, 'wp_ajax_fetch_full_content' ) );
add_action( 'wp_ajax_post-collection-download-images', array( $this, 'wp_ajax_download_images' ) );
add_action( 'wp_ajax_post-collection-re-extract', array( $this, 'wp_ajax_re_extract' ) );
add_filter( 'friends_search_autocomplete', array( $this, 'friends_search_autocomplete' ), 20, 2 );
add_filter( 'friends_browser_extension_actions', array( $this, 'friends_browser_extension_actions' ), 10, 3 );
add_filter( 'friends_browser_extension_action_post_collection_save', array( $this, 'friends_browser_extension_action_save' ), 10, 4 );
}
public function register_site_config( SiteConfig\SiteConfig $config ) {
$this->site_configs[] = $config;
}
/**
* Register the custom post type for collected posts.
*/
public function register_custom_post_type() {
$this->register_collection_taxonomy();
$labels = array(
'name' => __( 'Collected Posts', 'post-collection' ),
'singular_name' => __( 'Collected Post', 'post-collection' ),
'add_new' => _x( 'Add New', 'collected post', 'post-collection' ),
'add_new_item' => __( 'Add New Collected Post', 'post-collection' ),
'edit_item' => __( 'Edit Collected Post', 'post-collection' ),
'new_item' => __( 'New Collected Post', 'post-collection' ),
'all_items' => __( 'All Collected Posts', 'post-collection' ),
'view_item' => __( 'View Collected Post', 'post-collection' ),
'search_items' => __( 'Search Collected Posts', 'post-collection' ),
'not_found' => __( 'No Collected Posts found', 'post-collection' ),
'not_found_in_trash' => __( 'No Collected Posts found in the Trash', 'post-collection' ),
'parent_item_colon' => '',
'menu_name' => __( 'Collected Posts', 'post-collection' ),
);
$args = array(
'labels' => $labels,
'description' => __( 'A collected post from the web', 'post-collection' ),
'publicly_queryable' => current_user_can( $this->get_required_role() ),
'show_ui' => true,
'show_in_menu' => apply_filters( 'post_collection_show_in_menu', true ),
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'show_in_rest' => is_user_logged_in(),
'exclude_from_search' => true,
'public' => false,
'delete_with_user' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-book',
'supports' => array( 'title', 'editor', 'author', 'revisions', 'thumbnail', 'excerpt', 'comments', 'post-formats' ),
'taxonomies' => array( self::COLLECTION_TAXONOMY, $this->get_tag_taxonomy(), 'post_format' ),
'has_archive' => true,
'rewrite' => false,
);
register_post_type( self::CPT, $args );
}
/**
* Register collections as a taxonomy for collected posts.
*/
public function register_collection_taxonomy() {
$labels = array(
'name' => __( 'Post Collections', 'post-collection' ),
'singular_name' => __( 'Post Collection', 'post-collection' ),
'search_items' => __( 'Search Post Collections', 'post-collection' ),
'all_items' => __( 'All Post Collections', 'post-collection' ),
'edit_item' => __( 'Edit Post Collection', 'post-collection' ),
'update_item' => __( 'Update Post Collection', 'post-collection' ),
'add_new_item' => __( 'Add New Post Collection', 'post-collection' ),
'new_item_name' => __( 'New Post Collection Name', 'post-collection' ),
'menu_name' => __( 'Post Collections', 'post-collection' ),
);
register_taxonomy(
self::COLLECTION_TAXONOMY,
self::CPT,
array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => is_user_logged_in(),
'hierarchical' => false,
'rewrite' => false,
)
);
}
public function add_revision_support() {
add_post_type_support( self::CPT, 'revisions' );
}
/**
* Filter the post type when querying by a post_collection author.
*
* @param string|array $post_type The current post type(s).
* @param User $user The author being queried.
* @return string|array The filtered post type(s).
*/
public function filter_author_post_type( $post_type, $user ) {
if ( $user->has_cap( 'post_collection' ) ) {
return self::CPT;
}
return $post_type;
}
/**
* Get the collection term addressed by a /friends/{collection}/ query.
*
* @param \WP_Query|null $query Optional query object.
* @return \WP_Term|null Collection term, if the query targets one.
*/
private function get_collection_term_from_friends_query( $query = null ) {
if ( $query instanceof \WP_Query ) {
$pagename = isset( $query->query['pagename'] ) ? $query->query['pagename'] : $query->get( 'pagename' );
if ( ! $pagename ) {
$pagename = isset( $query->query['category_name'] ) ? $query->query['category_name'] : $query->get( 'category_name' );
}
if ( ! $pagename ) {
$pagename = isset( $query->query['name'] ) ? $query->query['name'] : $query->get( 'name' );
}
} else {
$pagename = get_query_var( 'pagename' );
if ( ! $pagename ) {
$pagename = get_query_var( 'category_name' );
}
if ( ! $pagename ) {
$pagename = get_query_var( 'name' );
}
}
$pagename_parts = explode( '/', trim( (string) $pagename, '/' ) );
if ( 'friends' !== array_shift( $pagename_parts ) || empty( $pagename_parts[0] ) ) {
return null;
}
$collection = get_term_by( 'slug', sanitize_title( $pagename_parts[0] ), self::COLLECTION_TAXONOMY );
if ( ! $collection || is_wp_error( $collection ) ) {
return null;
}
return $collection;
}
/**
* Query migrated collection URLs by collection term instead of old author users.
*
* @param \WP_Query $query The main query.
*/
public function filter_collection_friend_posts_query( \WP_Query $query ) {
global $wp_query;
if ( $wp_query !== $query || $query->is_admin() || $query->is_home() ) {
return;
}
$collection = $this->get_collection_term_from_friends_query( $query );
if ( ! $collection ) {
return;
}
$tax_query = $query->get( 'tax_query' );
if ( ! is_array( $tax_query ) ) {
$tax_query = array();
}
if ( $tax_query ) {
$tax_query['relation'] = 'AND';
}
$tax_query[] = array(
'taxonomy' => self::COLLECTION_TAXONOMY,
'field' => 'term_id',
'terms' => array( (int) $collection->term_id ),
);
$query->set( 'author', '' );
$query->set( 'author_name', '' );
$query->set( 'post_type', self::CPT );
$query->set( 'tax_query', $tax_query );
$query->is_author = true;
}
/**
* Add post_collection CPT to the Friends frontend post types.
*
* This is for backward compatibility with older versions of the Friends plugin.
*
* @param array $post_types The incoming post types.
* @return array The frontend post types.
*/
public function add_frontend_post_types( $post_types ) {
return array_merge( array( self::CPT ), $post_types );
}
/**
* Get the Friends\Template_Loader singleton
*
* @return Friends\Template_Loader A class instance.
*/
public static function template_loader() {
static $template_loader;
if ( ! isset( $template_loader ) ) {
require_once __DIR__ . '/class-post-collection-template-loader.php';
$template_loader = new Post_Collection_Template_Loader();
}
return $template_loader;
}
public function allow_post_editing( $link, $original ) {
if ( $this->is_post_collection_user( get_the_author_meta( 'ID' ) ) ) {
return $original;
}
return $link;
}
public function friends_show_author_edit( $show, $friend_user ) {
static $cache = array();
if ( isset( $cache[ $friend_user->ID ] ) ) {
if ( $cache[ $friend_user->ID ] ) {
return $show;
}
return false;
}
if ( $friend_user->has_cap( 'post_collection' ) ) {
$cache[ $friend_user->ID ] = false;
return false;
}
$cache[ $friend_user->ID ] = true;
return $show;
}
public function entry_dropdown_menu( $post = null, $friend_user = null ) {
$divider = '<li class="divider" data-content="' . esc_attr__( 'Post Collection', 'post-collection' ) . '"></li>';
$list_tags = array(
'li' => array(
'class' => true,
'data-content' => true,
),
);
$post = get_post( $post );
$author = $friend_user instanceof \WP_User ? $friend_user : $this->get_feed_item_author( $post );
$user_id = $author ? $author->ID : get_the_author_meta( 'ID' );
$user_id = (int) $user_id;
$author_attr = $user_id;
$is_post_collection_user = $this->is_post_collection_user( $user_id );
$is_collected_post = $post && self::CPT === $post->post_type;
if ( $is_collected_post && $is_post_collection_user ) {
echo wp_kses( $divider, $list_tags );
$divider = '';
?>
<li class="menu-item"><a href="<?php echo esc_url( get_edit_user_link( $user_id ) ); ?>"><?php esc_html_e( 'Edit Post Collection', 'post-collection' ); ?></a></li>
<?php
if ( 'private' === get_post_status() ) {
?>
<li class="menu-item"><a href="#" data-id="<?php echo esc_attr( get_the_ID() ); ?>" class="post-collection-mark-publish"><?php esc_html_e( 'Show post in the feed', 'post-collection' ); ?></a></li>
<?php
} elseif ( 'publish' === get_post_status() ) {
?>
<li class="menu-item"><a href="#" data-id="<?php echo esc_attr( get_the_ID() ); ?>" class="post-collection-mark-private"><?php esc_html_e( 'Hide post from the feed', 'post-collection' ); ?></a></li>
<?php
}
}
foreach ( $this->get_post_collection_terms() as $collection ) {
if ( $is_collected_post && has_term( (int) $collection->term_id, self::COLLECTION_TAXONOMY, $post ) ) {
continue;
}
if ( get_term_meta( $collection->term_id, 'friends_post_collection_inactive', true ) ) {
continue;
}
echo wp_kses( $divider, $list_tags );
$divider = '';
?>
<li class="menu-item"><a href="#" data-id="<?php echo esc_attr( get_the_ID() ); ?>" data-collection="<?php echo esc_attr( $collection->term_id ); ?>" class="post-collection-save-to-collection has-icon-right<?php echo esc_attr( get_term_meta( $collection->term_id, 'friends_post_collection_copy_mode', true ) ? ' copy-mode' : '' ); ?>">
<?php
if ( get_term_meta( $collection->term_id, 'friends_post_collection_copy_mode', true ) ) {
echo esc_html(
sprintf(
// translators: %s is the name of a post collection.
_x( 'Copy to %s', 'post-collection', 'post-collection' ),
$collection->name
)
);
} else {
echo esc_html(
sprintf(
// translators: %s is the name of a post collection.
_x( 'Move to %s', 'post-collection', 'post-collection' ),
$collection->name
)
);
}
?>
<i class="form-icon"></i></a>
</li>
<?php
}
$already_fetched = get_post_meta( get_the_ID(), 'full-content-fetched', true );
$i_classes = 'form-icon';
if ( $already_fetched ) {
$i_classes = 'dashicons dashicons-saved';
}
$already_downloaded = get_post_meta( get_the_ID(), 'images-downloaded', true );
$i_classes = 'form-icon';
if ( $already_downloaded ) {
$i_classes = 'dashicons dashicons-saved';
}
?>
<li class="menu-item"><a href="#" data-id="<?php echo esc_attr( get_the_ID() ); ?>" data-author="<?php echo esc_attr( $author_attr ); ?>" class="post-collection-fetch-full-content has-icon-right">
<?php
esc_html_e( 'Fetch full content', 'post-collection' );
?>
<i class="<?php echo esc_attr( $i_classes ); ?>"></i></a>
</li>
<li class="menu-item"><a href="#" data-id="<?php echo esc_attr( get_the_ID() ); ?>" data-author="<?php echo esc_attr( $author_attr ); ?>" class="post-collection-download-images has-icon-right">
<?php
esc_html_e( 'Download external images', 'post-collection' );
?>
<i class="<?php echo esc_attr( $i_classes ); ?>"></i></a>
</li>
<?php
$revisions = wp_get_post_revisions( get_the_ID(), array( 'posts_per_page' => 1 ) );
if ( ! empty( $revisions ) ) :
?>
<li class="menu-item"><a href="#" data-id="<?php echo esc_attr( get_the_ID() ); ?>" class="post-collection-re-extract has-icon-right">
<?php
esc_html_e( 'Re-extract from original HTML', 'post-collection' );
?>
<i class="form-icon"></i></a>
</li>
<?php
endif;
}
/**
* Get the author for the current Friends feed item.
*
* Friends can store feed ownership in taxonomy relationships, so template
* author globals and post_author are not always the right source.
*
* @param \WP_Post|null $post The feed item post.
* @return \WP_User|false The resolved feed item author.
*/
private function get_feed_item_author( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( class_exists( '\Friends\User' ) && method_exists( '\Friends\User', 'get_post_author' ) ) {
$author = \Friends\User::get_post_author( $post );
if ( $author && ! is_wp_error( $author ) ) {
return $author;
}
}
return User::get_post_author( $post );
}
public function edit_post_collection_link( $link, $user_id ) {
$user = new \WP_User( $user_id );
if ( is_multisite() && is_super_admin( $user->ID ) ) {
return $link;
}
if (
! $user->has_cap( 'post_collection' )
) {
return $link;
}
return self_admin_url( 'admin.php?page=edit-post-collection&user=' . $user_id );
}
public function is_post_collection_user( $user_id ) {
static $cache = array();
if ( ! isset( $cache[ $user_id ] ) ) {
$user = new User( $user_id );
$cache[ $user_id ] = $user->has_cap( 'post_collection' );
}
return $cache[ $user_id ];
}
/**
* Process access for the Friends Edit User page
*/
private function check_edit_post_collection() {
if ( ! current_user_can( $this->get_required_role() ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to edit this user.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
if ( ! isset( $_GET['user'] ) || ! is_numeric( $_GET['user'] ) ) {
wp_die( esc_html__( 'Invalid user ID.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
$user = new User( intval( $_GET['user'] ) );
if ( ! $user || is_wp_error( $user ) ) {
wp_die( esc_html__( 'Invalid user ID.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
if ( is_multisite() && is_super_admin( intval( $_GET['user'] ) ) ) {
wp_die( esc_html__( 'Invalid user ID.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
if (
! $user->has_cap( 'post_collection' )
) {
wp_die( esc_html__( 'This is not a user related to this plugin.', 'post-collection' ) );
}
return $user;
}
/**
* Process the Friends Edit Post Collection page
*/
public function process_edit_post_collection() {
$user = $this->check_edit_post_collection();
$arg = 'updated';
$arg_value = 1;
if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'edit-post-collection-' . $user->ID ) ) {
$display_name = isset( $_POST['display_name'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['display_name'] ) ) ) : '';
if ( $display_name ) {
$user->display_name = $display_name;
}
$user->description = isset( $_POST['description'] ) ? trim( sanitize_textarea_field( wp_unslash( $_POST['description'] ) ) ) : '';
wp_update_user( $user );
if ( ! empty( $_POST['publish_post_collection'] ) ) {
update_user_option( $user->ID, 'friends_publish_post_collection', true );
} else {
delete_user_option( $user->ID, 'friends_publish_post_collection' );
}
$frontend_mode = isset( $_POST['frontend_mode'] ) ? sanitize_key( wp_unslash( $_POST['frontend_mode'] ) ) : 'posts';
if ( in_array( $frontend_mode, array( 'bookmarks', 'posts' ), true ) ) {
update_user_option( $user->ID, 'post_collection_frontend_mode', $frontend_mode );
}
$frontend_view = isset( $_POST['frontend_view'] ) ? sanitize_key( wp_unslash( $_POST['frontend_view'] ) ) : 'reader';
if ( in_array( $frontend_view, array( 'board', 'links', 'reader' ), true ) ) {
update_user_option( $user->ID, 'post_collection_frontend_view', $frontend_view );
}
if ( ! empty( $_POST['hide_from_home'] ) ) {
update_user_option( $user->ID, 'post_collection_hide_from_home', true );
} else {
delete_user_option( $user->ID, 'post_collection_hide_from_home' );
}
if ( isset( $_POST['dropdown'] ) ) {
switch ( sanitize_key( wp_unslash( $_POST['dropdown'] ) ) ) {
case 'inactive':
update_user_option( $user->ID, 'friends_post_collection_inactive', true );
break;
case 'move':
delete_user_option( $user->ID, 'friends_post_collection_inactive' );
delete_user_option( $user->ID, 'friends_post_collection_copy_mode' );
break;
case 'copy':
delete_user_option( $user->ID, 'friends_post_collection_inactive' );
update_user_option( $user->ID, 'friends_post_collection_copy_mode', true );
break;
}
}
} else {
return;
}
if ( isset( $_GET['wp_http_referer'] ) ) {
wp_safe_redirect( esc_url_raw( wp_unslash( $_GET['wp_http_referer'] ) ) );
} else {
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
wp_safe_redirect( add_query_arg( $arg, $arg_value, remove_query_arg( array( 'wp_http_referer', '_wpnonce' ), $request_uri ) ) );
}
exit;
}
public function render_edit_post_collection() {
$user = $this->check_edit_post_collection();
$args = array(
'user' => $user,
'inactive' => get_user_option( 'friends_post_collection_inactive', $user->ID ),
'copy_mode' => get_user_option( 'friends_post_collection_copy_mode', $user->ID ),
'posts' => new \WP_Query(
array(
'post_type' => self::CPT,
'post_status' => array( 'publish', 'private' ),
'author' => $user->ID,
)
),
'post_collection_url' => home_url( '/?user=' . $user->ID ),
'bookmarklet_js' => $this->get_bookmarklet_js(),
'frontend_mode' => get_user_option( 'post_collection_frontend_mode', $user->ID ),
'frontend_view' => get_user_option( 'post_collection_frontend_view', $user->ID ),
'hide_from_home' => get_user_option( 'post_collection_hide_from_home', $user->ID ),
'frontend_url' => class_exists( __NAMESPACE__ . '\Post_Collection_App' ) && Post_Collection_App::instance() ? Post_Collection_App::instance()->get_collection_url( $user ) : '',
);
?>
<h1><?php echo esc_html( $user->user_login ); ?></h1>
<?php
if ( isset( $_GET['updated'] ) ) {
?>
<div id="message" class="updated notice is-dismissible"><p><?php esc_html_e( 'User was updated.', 'post-collection' ); ?></p></div>
<?php
} elseif ( isset( $_GET['error'] ) ) {
?>
<div id="message" class="updated error is-dismissible"><p><?php esc_html_e( 'An error occurred.', 'post-collection' ); ?></p></div>
<?php
}
$this->template_loader()->get_template_part( 'admin/edit-post-collection', null, $args );
}
/**
* Process access for the Friends create User page
*/
private function check_create_post_collection() {
if ( ! current_user_can( $this->get_required_role() ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to create this user.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
$user = (object) array(
'user_login' => null,
'display_name' => null,
);
if ( isset( $_POST['display_name'] ) ) {
$user->display_name = sanitize_text_field( wp_unslash( $_POST['display_name'] ) );
}
if ( isset( $_POST['user_login'] ) ) {
$user->user_login = sanitize_user( wp_unslash( $_POST['user_login'] ) );
if ( ! $user->user_login && $user->display_name ) {
$user->user_login = User::sanitize_username( $user->display_name );
}
}
return $user;
}
/**
* Process the Friends Create Post Collection page
*/
public function process_create_post_collection() {
$errors = new \WP_Error();
$user = $this->check_create_post_collection();
if ( ! $user->user_login ) {
$errors->add( 'user_login', __( '<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
} elseif ( username_exists( $user->user_login ) ) {
$errors->add( 'user_login', __( '<strong>Error</strong>: This username is already registered. Please choose another one.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
} elseif ( ! $user->display_name ) {
$errors->add( 'user_login', __( '<strong>Error</strong>: Please enter a valid display name.' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
if ( ! $errors->has_errors() ) {
$userdata = array(
'user_login' => $user->user_login,
'display_name' => $user->display_name,
'user_pass' => wp_generate_password( 256 ),
'role' => 'post_collection',
);
$user_id = wp_insert_user( $userdata );
if ( is_wp_error( $user_id ) ) {
return $user_id;
}
wp_safe_redirect( self_admin_url( 'admin.php?page=edit-post-collection&user=' . $user_id ) );
exit;
}
return $errors;
}
public function render_create_post_collection() {
$response = null;
$user = $this->check_create_post_collection();
if ( ! empty( $_POST ) ) {
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'create-post-collection' ) ) {
$response = new \WP_Error( 'invalid-nonce', __( 'For security reasons, please verify the URL and click next if you want to proceed.', 'post-collection' ) );
} else {
$response = $this->process_create_post_collection();
}
}
?>
<h1><?php esc_html_e( 'Create Post Collection', 'post-collection' ); ?></h1>
<?php
if ( is_wp_error( $response ) ) {
?>
<div id="message" class="updated notice is-dismissible"><p>
<?php
echo wp_kses(
$response->get_error_message(),
array(
'strong' => array(),
'a' => array(
'href' => array(),
'rel' => array(),
'target' => array(),
),
)
);
?>
</p>
</div>
<?php
}
$args = array(
'user_login' => $user->user_login,
'display_name' => $user->display_name,
);
$this->template_loader()->get_template_part( 'admin/create-post-collection', null, $args );
}
public function enqueue_scripts() {
if ( ! is_user_logged_in() ) {
return;
}
if ( $this->is_on_friends_frontend() ) {
wp_enqueue_script( 'post-collection', plugins_url( 'post-collection.js', __FILE__ ), array( 'friends' ), filemtime( __DIR__ . '/post-collection.js' ), true );
wp_localize_script(
'post-collection',
'postCollection',
array(
'nonce' => wp_create_nonce( self::AJAX_NONCE ),
)
);
}
}
/**
* Enqueue block editor assets for collected posts.
*/
public function enqueue_block_editor_assets() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || self::CPT !== $screen->post_type ) {
return;
}
$post = get_post();
if ( ! $post || self::CPT !== $post->post_type ) {
return;
}
$source_url = $this->get_post_source_url( $post );
if ( ! $source_url ) {
return;
}
$handle = 'post-collection-editor';
wp_enqueue_script(
$handle,
plugins_url( 'post-collection-editor.js', __FILE__ ),
array( 'wp-components', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins' ),
filemtime( __DIR__ . '/post-collection-editor.js' ),
true
);
wp_localize_script(
$handle,
'postCollectionEditor',
array(
'sourceUrl' => $source_url,
'i18n' => array(
'panelTitle' => __( 'Post Archives', 'post-collection' ),
'waybackSnapshots' => __( 'Wayback Snapshots', 'post-collection' ),
'archiveIs' => __( 'Archive.is', 'post-collection' ),
'archiveLinksLabel' => __( 'Find archived copies of the original article URL.', 'post-collection' ),
),
)
);
}
/**
* Get the original source URL for a collected post.
*
* @param \WP_Post $post The collected post.
* @return string
*/
private function get_post_source_url( \WP_Post $post ) {
$url = get_post_meta( $post->ID, 'url', true );
if ( is_string( $url ) && $this->check_url( $url ) ) {
return $url;
}
if ( $this->check_url( $post->guid ) ) {