-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-commenting-block-admin.php
2356 lines (2011 loc) · 83.8 KB
/
class-commenting-block-admin.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
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* The admin-specific functionality of the plugin.
*
* @link #
* @since 1.0.0
*
* @package content-collaboration-inline-commenting
*/
class Commenting_block_Admin extends Commenting_block_Functions {
/**
* 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;
/**
* Initiate Email Class Object.
*/
private $email_class;
/**
* @var string Comment Activities.
*/
public $cf_activities;
public $cf_activities_object;
private static $allowed_attribute_tags = array( 'content', 'citation', 'caption', 'value', 'values', 'fileName', 'text', 'downloadButtonText' );
private static $cf_permission_options = array( 'cf_permission_add_comment', 'cf_permission_resolved_comment', 'cf_permission_add_suggestion', 'cf_permission_resolved_suggestion' );
/**
* Initiate basename .
*/
static $basename = null;
/**
* 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
*/
public function __construct( $plugin_name, $version ) {
global $pagenow;
$this->plugin_name = $plugin_name;
$this->version = $version;
// Publish Comments on status change.
add_action( 'post_updated', array( $this, 'cf_post_status_changes' ), 10, 3 );
// Update caps for authors and contributors.
add_filter( 'admin_init', array( $this, 'cf_custom_caps' ) );
// Allow caps for Multisite environment.
add_filter( 'map_meta_cap', array( $this, 'cf_add_unfiltered_html_capability_to_users' ), 1, 3 );
// Action to add setting page.
add_action( 'admin_menu', array( $this, 'cf_add_setting_page' ) );
// Adding new column to the posts list.
add_filter( 'manage_posts_columns', array( $this, 'cf_columns_head' ) );
add_filter( 'manage_pages_columns', array( $this, 'cf_columns_head' ) );
// Adding content in a column of posts list.
add_action( 'manage_posts_custom_column', array( $this, 'cf_columns_content' ), 10, 2 );
add_action( 'manage_pages_custom_column', array( $this, 'cf_columns_content' ), 10, 2 );
// Make custom comments columns sortable.
add_filter( 'manage_edit-post_sortable_columns', array( $this, 'cf_sortable_comments_column' ) );
add_filter( 'manage_edit-page_sortable_columns', array( $this, 'cf_sortable_comments_column' ) );
// Set query to sort.
add_action( 'pre_get_posts', array( $this, 'cf_sort_custom_column_query' ) );
// Remove the mdspan tage from the front content.
add_filter( 'the_content', array( $this, 'cf_removeMdspan' ) );
// Add untitled when page/post title blank
add_filter( 'the_title', array( $this, 'cf_post_title' ) );
// Add user role to WordPress users api
add_action( 'rest_api_init', array( $this, 'create_api_user_meta_field_for_userrole' ) );
// clear output buffer on init hook.
add_action( 'init', array( $this, 'cf_app_output_buffer' ) );
add_action( 'admin_footer', array( $this, 'cf_free_admin_edit_post_feedback_form' ) );
add_action( 'wp_ajax_cf_free_plugin_wizard_submit', array( $this, 'cf_free_plugin_wizard_submit' ) );
add_action( 'cf_free_plugin_usage_data', array( $this, 'cf_free_plugin_usage_data_callback_function' ) );
add_filter( 'admin_body_class', array( $this, 'cf_admin_classes' ) );
$allow_pages = array( 'edit.php', 'post-new.php', 'post.php' );
if ( in_array( $pagenow, $allow_pages, true ) ) {
add_action( 'admin_notices', array( $this, 'cf_display_promotional_banner_page_post' ) );
}
add_filter( 'cron_schedules', array( $this, 'cf_free_cron_job_recurrence' ) );
// User authentication function while redirect guest.
add_filter( 'authenticate', array( $this, 'cf_guest_auto_login' ), 10, 3 );
add_filter( 'plugin_action_links_' . COMMENTING_BLOCK_BASE, array( $this, 'cf_custom_plugin_action_links' ), 10, 4 );
add_filter( 'block_type_metadata', array( $this, 'filter_block_type_metadata' ), 10, 1 );
}
/**
* Adds a custom cron schedule for every month.
*
* @param array $schedules An array of non-default cron schedules.
* @return array Filtered array of non-default cron schedules.
*/
function cf_free_cron_job_recurrence( $schedules ) {
$schedules['cf_free_monthly'] = array(
'display' => __( 'Once monthly', 'content-collaboration-inline-commenting' ),
'interval' => 2635200,
);
return $schedules;
}
/**
* Display promotional banner on post/page.
*
* @author Nirav Soni
*/
public function cf_display_promotional_banner_page_post() {
$promotional_banner = cf_dpb_promotional_banner();
if ( ! empty( $promotional_banner ) ) {
echo $promotional_banner; // phpcs:ignore WordPress.Security.EscapeOutput
}
}
/**
* Added extra links to default meta row.
*
* @author: Himanshu shekhar
* @version 4.1
*
* @param array $links To add custom link.
* @param string $plugin_file The path to the main plugin file.
*/
public function cf_custom_plugin_action_links( $links, $plugin_file ) {
// Add your custom links.
$custom_links = array(
'<a href="https://www.multicollab.com/upgrade-to-premium/" style="color:#4abe17" target="_blank">Upgrade to Pro</a>',
'<a href="https://docs.multicollab.com/" target="_blank">Documentation</a>',
);
// Merge the custom links with the existing action links.
$links = array_merge( $custom_links, $links );
return $links;
}
/**
* Update realtime collobrators ajax function.
*
* @return void
*/
public function realtime_collaborators_update_ajax_function() {
$current_post_id = filter_input( INPUT_POST, 'postID', FILTER_SANITIZE_SPECIAL_CHARS );
$activeUsers = filter_input( INPUT_POST, 'activeUsers', FILTER_UNSAFE_RAW ); //phpcs:ignore
$active_users = json_decode( stripslashes( $activeUsers ), true );
$realtime_collobrator = get_post_meta( $current_post_id, '_realtime_collaborators', true );
$realtime_collobrator = (array) json_decode( $realtime_collobrator, true );
if( isset( $realtime_collobrator ) && !empty( $realtime_collobrator ) ) {
foreach( $active_users as $active_users_data ) {
array_push( $realtime_collobrator, $active_users_data );
}
} else {
$realtime_collobrator = $active_users;
}
$realtime_collobrators_unique_array = [];
foreach($realtime_collobrator as $element) {
$hash = $element['userId'];
$realtime_collobrators_unique_array[$hash] = $element;
$user_data = get_user_by( 'ID', $element['userId'] );
$realtime_user_roles = array_values( $user_data->roles );
$realtime_user_role = array_shift( $realtime_user_roles );
$realtime_collobrators_unique_array[$hash]['role'] = $realtime_user_role;
$realtime_collobrators_unique_array[$hash]['email'] = $user_data->user_email;
$realtime_collobrators_unique_array[$hash]['userAvatar'] = get_avatar_url( $element['userId'] );
}
update_post_meta( $current_post_id, '_realtime_collaborators', wp_json_encode( $realtime_collobrators_unique_array ) );
echo wp_json_encode( $realtime_collobrators_unique_array );
wp_die();
}
/**
* Update realtime collobrators activity ajax function.
*
* @return void
*/
public function realtime_collaborators_activity_update_ajax_function() {
$current_post_id = filter_input( INPUT_POST, 'postID', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$activeUsers = filter_input( INPUT_POST, 'currentUser', FILTER_UNSAFE_RAW ); //phpcs:ignore
$active_users = json_decode( stripslashes( $activeUsers ), true );
$realtime_collobrator = get_post_meta( $current_post_id, '_realtime_collaborators_activity', true );
$realtime_collobrator = (array) json_decode( $realtime_collobrator, true );
if( isset( $realtime_collobrator ) && !empty( $realtime_collobrator ) ) {
$userIdExists = false;
$joined_count = 0;
$removed_count = 0;
foreach($realtime_collobrator as $collaborator) {
if($collaborator['userId'] === $active_users[0]['userId']){
if ( 'Joined' === $collaborator['type'] ) {
$joined_count++;
}
if ( 'Removed' === $collaborator['type'] ) {
$removed_count++;
}
$userIdExists = true;
}
}
// If userId is unique, OR IF user joined back after being removed merge it into realtime_collobrator array
if (!$userIdExists || ($joined_count === $removed_count)) {
$realtime_collobrator[] = $active_users[0];
$meta_key_sufix = $active_users[0]['userId'].'_'.$active_users[0]['timestamp'];
if( 'Joined' === $active_users[0]['type'] ){
update_post_meta( $current_post_id, 'th_rc_joined_'.$meta_key_sufix, $active_users[0]['timestamp'] );
}
}
} else {
$realtime_collobrator = $active_users;
$meta_key_sufix = $active_users[0]['userId'].'_'.$active_users[0]['timestamp'];
if( 'Joined' === $active_users[0]['type'] ){
update_post_meta( $current_post_id, 'th_rc_joined_'.$meta_key_sufix, $active_users[0]['timestamp'] );
}
}
update_post_meta( $current_post_id, '_realtime_collaborators_activity', wp_json_encode( $realtime_collobrator ) );
echo wp_json_encode( $realtime_collobrator );
wp_die();
}
/**
* Add body class for wizard screen.
*
* @return void
*/
public function cf_admin_classes( $classes ) {
$page_type = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_SPECIAL_CHARS );
if ( 'multicollab_setup_wizard' === $page_type ) {
$classes .= ' cf_fullscreen';
}
$cf_edd = new CF_EDD();
if ( $cf_edd->is__premium_only() ) {
$plan_name = esc_html( $cf_edd->get_plan_name() );
} else {
$plan_name = esc_html( 'FREE' );
}
$classes .= ' multicollab_plan_' . strtolower( $plan_name );
// Added parent class to body for adding css.
$classes .= ' multicollab_body_class ';
return $classes;
}
/**
* Send Wizard Opt-in details to sendinblue.
*
* @return void
*/
public function cf_free_plugin_wizard_submit() {
global $wp_version;
$current_user = wp_get_current_user();
$subscribe_email = filter_input( INPUT_GET, 'subscribe_email', FILTER_SANITIZE_SPECIAL_CHARS );
$subscribe_email = ! empty( $subscribe_email ) ? $subscribe_email : '';
$opt_in = filter_input( INPUT_GET, 'opt_in', FILTER_SANITIZE_SPECIAL_CHARS );
$broser_name = filter_input( INPUT_GET, 'broser_name', FILTER_SANITIZE_SPECIAL_CHARS );
$country = filter_input( INPUT_GET, 'country', FILTER_SANITIZE_SPECIAL_CHARS );
$user_email = $current_user->user_email;
// Get Gutenberg Version.
$plugins = get_option( 'active_plugins' );
if ( in_array( 'gutenberg/gutenberg.php', $plugins, true ) ) {
$get_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' );
$gutenberg_version = esc_html( $get_plugin_data['Version'] );
} else {
$gutenberg_version = esc_html( 'Default', 'content-collaboration-inline-commenting' );
}
update_option( 'cf_opt_in', $opt_in );
// Get multicollab plan details.
$multicollab_plan = esc_html( 'FREE', 'content-collaboration-inline-commenting' );
// Get Webserver Name and Version.
$server_software = filter_input( INPUT_SERVER, 'SERVER_SOFTWARE', FILTER_SANITIZE_SPECIAL_CHARS );
$server_software = isset( $server_software ) ? $server_software : '';
$webservername = explode( '/', $server_software )[0];
$webserver_name_version = esc_html( $webservername ) . ' ' . esc_html( explode( '/', $server_software )[1] );
// Get users count.
$user_count = count_users();
// get WP version.
$my_theme = wp_get_theme();
$plugin_usage_data = array(
'Admin Email' => $user_email,
'Website URL' => esc_url( get_home_url() ),
'WordPress Version' => $wp_version,
'Gutenberg Version' => $gutenberg_version,
'PHP Version' => phpversion(),
'Multicollab Plugin status' => 'active',
'Multicollab Version' => COMMENTING_BLOCK_VERSION,
'Multicollab Plan' => $multicollab_plan,
'Date & Time' => gmdate( 'F j, Y g:i a' ),
'Language' => get_bloginfo( 'language' ),
'Theme' => $my_theme->get( 'Name' ),
'Browser Name and Version' => $broser_name,
'Webserver Name and Version' => $webserver_name_version,
'Operating System' => sprintf( '%s %s %s', php_uname( 's' ), php_uname( 'r' ), php_uname( 'm' ) ),
'Country' => $country,
);
$data_insert_array = array(
'user_name' => $current_user->display_name,
'user_email' => $user_email,
'news_letter_email' => $subscribe_email,
'opt_in' => $opt_in,
'plan_name' => $multicollab_plan,
'PLUGIN_USAGE_DATA' => wp_json_encode( $plugin_usage_data ),
'website_url' => esc_url( get_home_url() ),
);
$feedback_api_url = CF_STORE_URL . '/wp-json/edd-add-free-user-contact/v2/edd-add-free-user-contact';
$query_url = $feedback_api_url . '?' . http_build_query( $data_insert_array );
$response = wp_remote_get( $query_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
if ( ! wp_next_scheduled( 'cf_free_plugin_usage_data' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'cf_free_monthly', 'cf_free_plugin_usage_data' );
}
if ( ! wp_next_scheduled( 'cf_daily_license_checker' ) ) {
wp_schedule_event( time(), 'daily', 'cf_daily_license_checker' );
}
$args = array(
'title' => 'Getting Started with Multicollab',
'post_type' => 'post',
'post_status' => 'draft',
);
$posts_array = get_posts( $args );
if( empty( $posts_array ) ){
$wizard_redirect_link = home_url() . '/wp-admin/admin.php?page=editorial-comments&view=web-activity';
} else {
$wizard_redirect_link = $url = get_edit_post_link( $posts_array[0]->ID );;
}
echo esc_html( $wizard_redirect_link );
wp_die();
}
/**
* Send Wizard Opt-in details to sendinblue every monthly.
*
* @return void
*/
public function cf_free_plugin_usage_data_callback_function() {
global $wp_version;
// Get Gutenberg Version.
$plugins = get_option( 'active_plugins' );
if ( in_array( 'gutenberg/gutenberg.php', $plugins, true ) ) {
$get_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/gutenberg/gutenberg.php' );
$gutenberg_version = esc_html( $get_plugin_data['Version'] );
} else {
$gutenberg_version = esc_html( 'Default', 'content-collaboration-inline-commenting' );
}
// Get multicollab plan details.
$multicollab_plan = esc_html( 'FREE', 'content-collaboration-inline-commenting' );
// get WP version.
$my_theme = wp_get_theme();
$user_count = count_users();
$cf_custom_posts_args = array(
'public' => true,
'_builtin' => false,
);
$cf_custom_posts_output = 'names';
$cf_custom_posts_operator = 'and';
$cf_custom_post_types = get_post_types( $cf_custom_posts_args, $cf_custom_posts_output, $cf_custom_posts_operator );
$cf_default_post_types = array(
'post' => 'post',
'page' => 'page',
);
$cf_all_post_types = array_merge( $cf_default_post_types, $cf_custom_post_types );
$all_posts_id = get_posts(
array(
'post_type' => $cf_all_post_types,
'post_status' => 'any',
'numberposts' => -1,
'fields' => 'ids',
)
);
$posts_counts = count( $all_posts_id );
$comments_counts = 0;
if ( $all_posts_id ) {
foreach ( $all_posts_id as $id ) {
$comments_count_data = $this->cf_get_comment_counts( $id );
$comments_counts += $comments_count_data['total_counts'];
}
}
// average post count
$avg_post_count = ( $comments_counts / $posts_counts );
// number of users - exclude subscriber user
$total_user = $user_count['total_users'];
if ( isset( $user_count['avail_roles']['subscriber'] ) ) {
$subscribers = $user_count['avail_roles']['subscriber'];
} else {
$subscribers = 0;
}
$no_of_users = ( $total_user - $subscribers );
// commennt of current month
$comments_of_month = gmdate( 'F' );
// commennt of current year
$comments_of_year = gmdate( 'Y' );
$general_setting = $this->cf_get_general_settings();
$plugin_advance_data = array(
'general_setting' => $general_setting,
'comment_count' => $comments_counts,
'post_count' => $posts_counts,
'avg_post_count' => $avg_post_count,
'no_of_users' => $no_of_users,
'comments_of_month' => $comments_of_month,
'comments_of_year' => $comments_of_year,
);
$cf_opt_in = get_option( 'cf_opt_in' );
$plugin_basic_data = array(
'wordpress_version' => $wp_version,
'gutenberg_version' => $gutenberg_version,
'php_version' => phpversion(),
'multicollab_version' => COMMENTING_BLOCK_VERSION,
'multicollab_plan' => $multicollab_plan,
'language' => get_bloginfo( 'language' ),
'theme' => $my_theme->get( 'Name' ),
);
$data_insert_array = array(
'website_url' => esc_url( get_home_url() ),
'plugin_basic_data' => wp_json_encode( $plugin_basic_data ),
'plugin_advance_data' => wp_json_encode( $plugin_advance_data ),
'opt_in' => $cf_opt_in,
);
$feedback_api_url = CF_STORE_URL . '/wp-json/edd-add-free-user-contact/v2/edd-add-free-user-contact?' . wp_rand();
$query_url = $feedback_api_url . '&' . http_build_query( $data_insert_array );
$response = wp_remote_get( $query_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
wp_die();
}
/**
* Add function to add HTML to admin footer.
*
* @return void
*/
public function cf_free_admin_edit_post_feedback_form() {
global $pagenow;
if ( 'plugins.php' === $pagenow ) {
require_once COMMENTING_BLOCK_DIR . 'admin/partials/commenting-block-admin-deactive-free.php'; // phpcs:ignore
}
}
/**
* Clear output buffer on init function.
*
* @return void
*/
public function cf_app_output_buffer() {
ob_start();
}
/**
* Add Untitled when post title is blank.
*
* @param array $title title of post.
*
* @return mixed Updated title.
*/
function cf_post_title( $title ) {
return '' === $title ? esc_html_x( 'Untitled', 'Added to posts and pages that are missing titles', 'content-collaboration-inline-commenting' ) : $title;
}
/**
* Remove custom tag "mdspan" from the content.
*
* @param array $content content of post.
*
* @return mixed Updated content.
*/
public function cf_removeMdspan( $content ) {
if ( ( is_singular() ) && ( is_main_query() ) ) {
$regex = '#<mdspan(.*?)>#';
$replacement = '';
$content = preg_replace( $regex, $replacement, $content );
}
return $content;
}
/**
* Make custom comments columns sortable.
*
* @param array $columns List of columns.
*
* @return mixed Updated list of columns.
*/
public function cf_sortable_comments_column( $columns ) {
$columns['cb_comments_status'] = 'sort_by_cf_comments';
return $columns;
}
/**
* Set query to sort.
*
* @param object $query Query object.
*/
public function cf_sort_custom_column_query( $query ) {
$orderby = $query->get( 'orderby' );
if ( 'sort_by_cf_comments' === $orderby && $query->is_main_query() ) {
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'open_cf_count',
'compare' => 'NOT EXISTS', // see note above
),
array(
'key' => 'open_cf_count',
),
);
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', 'meta_value' );
return;
}
}
/**
* Update columns of the posts list.
*
* @param array $defaults List of default columns.
*
* @return array mixed Updated list of default columns.
*/
public function cf_columns_head( $defaults ) {
$all_post_type = get_post_types_by_support( array( 'editor' ) );
$post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_SPECIAL_CHARS );
$type = get_post_type();
$cf_hide_editorial_column = get_option( 'cf_hide_editorial_column' );
if ( empty( $cf_hide_editorial_column ) ) {
if ( ( in_array( trim( $post_type ), $all_post_type, true ) ) || ( in_array( trim( $type ), $all_post_type, true ) ) ) {
if ( ( isset( $post_type ) || isset( $type ) ) && ( $post_type !== 'product' || $type !== 'product' ) ) {
$defaults['cb_comments_status'] = '<img id="cf-column-img" src="' . esc_url( COMMENTING_BLOCK_URL . '/admin/assets/images/commenting-logo.svg' ) . '" width=17/>' . __( 'Multicollab', 'content-collaboration-inline-commenting' );
}
}
}
return $defaults;
}
/**
* Add content in a new column of the posts list.
*
* @param string $column_name Column name.
* @param int $post_ID Post ID.
*/
public function cf_columns_content( $column_name, $post_ID ) {
if ( 'cb_comments_status' === $column_name ) {
$comment_counts = $this->cf_get_comment_counts( $post_ID );
$suggestion_counts = $this->cf_get_suggestion_counts( $post_ID );
$open_counts = intval( $comment_counts['open_counts'] ) + intval( $suggestion_counts['open_counts'] );
$total_counts = intval( $comment_counts['total_counts'] ) + intval( $suggestion_counts['total_counts'] );
$resolved_total = $comment_counts['resolved_counts'] + $suggestion_counts['accepted_counts'] + $suggestion_counts['rejected_counts'];
$autodraft_total = $total_counts - ( $open_counts + $resolved_total );
$open_counts = $total_counts - ( $autodraft_total + $resolved_total );
if ( 0 !== $total_counts ) {
echo '<a href="' . esc_url( get_edit_post_link( $post_ID ) ) . '">' . esc_html( $open_counts . '/' . $total_counts ) . '</a>';
} else {
echo '-';
}
}
}
/**
* Add Setting Page.
*/
public function cf_add_setting_page() {
$settings_title = 'Multicollab';
// Adding a new admin page for MYS
add_menu_page(
__( esc_html( $settings_title ), 'content-collaboration-inline-commenting' ),
__( esc_html( $settings_title ), 'content-collaboration-inline-commenting' ),
'manage_options',
'editorial-comments',
array( $this, 'cf_settings_callback' ),
COMMENTING_BLOCK_URL . '/admin/assets/images/menu-icon.svg'
);
add_submenu_page(
'',
__( 'Free Wizard', 'textdomain' ),
'Multicolab Wizard',
'manage_options',
'multicollab_setup_wizard',
array( $this, 'multicollab_setup_wizard_function' )
);
}
/**
* Callback function for free user setup wizard.
*
* @return void
*/
public function multicollab_setup_wizard_function() {
include COMMENTING_BLOCK_DIR . 'admin/partials/free-user-plugin-wizard.php';
}
/**
* Plugin setting page callback function.
*/
public function cf_settings_callback() {
// Add setting page file.
require_once COMMENTING_BLOCK_DIR . 'admin/partials/commenting-block-settings-page.php'; // Removed phpcs:ignore by Rishi Shah.
}
/**
* Get the latest comment activities.
*/
public function cf_get_activities() {
require_once COMMENTING_BLOCK_DIR . 'admin/classes/class-commenting-block-activities.php'; // Removed phpcs:ignore by Rishi Shah.
$this->cf_activities_object = new Commenting_Block_Activities();
$this->cf_activities = $this->cf_activities_object->cf_get_activities();
}
/**
* Get activities code.
*/
public function cf_get_activity_details() {
require_once COMMENTING_BLOCK_DIR . 'admin/classes/class-commenting-block-activities.php'; // Removed phpcs:ignore by Rishi Shah.
$this->cf_activities_object = new Commenting_Block_Activities();
$this->cf_activities = $this->cf_activities_object->cf_get_detailed_activity();
}
/**
* Get activities code.
*/
public function cf_migrate_to_pro() {
require_once COMMENTING_BLOCK_DIR . 'admin/classes/class-commenting-block-activities.php'; // Removed phpcs:ignore by Rishi Shah.
$this->cf_activities_object = new Commenting_Block_Activities();
$this->cf_activities = $this->cf_activities_object->cf_migrate_to_pro_now();
}
/**
* Allowed Administrator, editor, author and contributor user to enter unfiltered html.
*
* @param array $caps All caps.
* @param string $cap Cap in a loop.
* @param int $user_id User ID.
*
* @return array Caps.
*/
public function cf_add_unfiltered_html_capability_to_users( $caps, $cap, $user_id ) {
if ( 'unfiltered_html' === $cap && ( user_can( $user_id, 'administrator' ) || user_can( $user_id, 'editor' ) || user_can( $user_id, 'author' ) || user_can( $user_id, 'contributor' ) ) ) {
$caps = array( 'unfiltered_html' );
}
return $caps;
}
/**
* Add capabilities to user roles to make 'mdspan' tag unfiltered.
*
* @return bool True always.
*/
public function cf_custom_caps() {
$roles = array( 'author', 'contributor' );
foreach ( $roles as $role ) {
$role = get_role( $role );
if ( $role ) {
// Add custom capabilities.
$role->add_cap( 'unfiltered_html' );
}
}
return true;
}
/**
* Get User Details using AJAX.
*/
public function cf_get_user() {
$curr_user = wp_get_current_user();
$userID = $curr_user->ID;
$userName = $curr_user->display_name;
$userURL = get_avatar_url( $userID );
$userRole = get_userdata( $userID )->roles[0];
echo wp_json_encode(
array(
'id' => $userID,
'name' => $userName,
'role' => $userRole,
'url' => $userURL,
)
);
wp_die();
}
/**
* @param int $post_ID Post ID.
* @param object/string $post Post Content.
* @param string $update Status of the update.
*/
public function cf_post_status_changes( $post_ID, $post ) {
$metas = get_post_meta( $post_ID );
$p_content = is_object( $post ) ? $post->post_content : $post;
$p_link = get_edit_post_link( $post_ID );
$p_title = get_the_title( $post_ID ); // Removed phpcs:ignore by Rishi Shah.
$site_title = get_bloginfo( 'name' ); // Removed phpcs:ignore by Rishi Shah.
$html = '';
// Get current user details.
$curr_user = wp_get_current_user();
$user_id = $curr_user->ID;
$current_user_email = $curr_user->user_email;
$current_user_display_name = $curr_user->display_name;
// Publish drafts from the '_current_drafts' stack.
$current_drafts = isset( $metas['_current_drafts'][0] ) ? $metas['_current_drafts'][0] : array();
$current_drafts = maybe_unserialize( $current_drafts );
$current_timestamp = current_time( 'timestamp' );
// Initiate Email Class Object.
$this->cf_initiate_email_class();
// Publish Deleted Comments. (i.e. finally delete them.)
if ( isset( $current_drafts['deleted'] ) && 0 !== count( $current_drafts['deleted'] ) ) {
$deleted_drafts = $current_drafts['deleted'];
foreach ( $deleted_drafts as $el => $timestamps ) {
$prev_state = $metas[ $el ][0];
$prev_state = maybe_unserialize( $prev_state );
foreach ( $timestamps as $key => $t ) {
$local_time = current_datetime();
$deleted_timestamp = $local_time->getTimestamp() + $local_time->getOffset() + $key;
// Update the timestamp of deleted comment.
$previous_comment = $prev_state['comments'][ $t ];
if ( ! empty( $previous_comment ) ) {
$prev_state['comments'][ $deleted_timestamp ] = $previous_comment;
$prev_state['comments'][ $deleted_timestamp ]['status'] = 'deleted';
$prev_state['comments'][ $deleted_timestamp ]['created_at'] = $t;
}
}
$prev_state['updated_at'] = $current_timestamp;
// add th meta
update_post_meta( $post_ID, $el, $prev_state );
update_post_meta( $post_ID, 'th' . $el, $deleted_timestamp );
$metas[ $el ][0] = maybe_serialize( $prev_state );
}
// add mc_updated
}
// Publish New Comments.
if ( isset( $current_drafts['comments'] ) && 0 !== count( $current_drafts['comments'] ) ) {
$new_drafts = $current_drafts['comments'];
foreach ( $new_drafts as $el => $drafts ) {
/*
* Make publish only if its tag available in the content.
* Doing this to handle the CTRL-Z action.
* Sometimes CTRL-Z does not removes the tag completely
* but only removes its attributes, so we cant find 'datatext' attribute,
* So skipping those mdspan tags which has no 'datatext' attribute.
* This is also skipping the recent resolved drafts.
*/
$elid = str_replace( '_', '', $el );
if ( strpos( $p_content, $elid ) !== false ) {
$prev_state = $metas[ $el ][0];
$prev_state = maybe_unserialize( $prev_state );
$new_comments = array();
foreach ( $drafts as $d ) {
$prev_state['comments'][ $d ]['status'] = 'publish';
$new_comments[] = $d;
}
$prev_state['updated_at'] = $current_timestamp;
update_post_meta( $post_ID, $el, $prev_state );
$metas[ $el ][0] = maybe_serialize( $prev_state );
}
}
}
// Publish Edited Comments.
if ( isset( $current_drafts['edited'] ) && 0 !== count( $current_drafts['edited'] ) ) {
$edited_drafts = $current_drafts['edited'];
foreach ( $edited_drafts as $el => $timestamps ) {
$prev_state = $metas[ $el ][0];
$prev_state = maybe_unserialize( $prev_state );
foreach ( $timestamps as $t ) {
$edited_draft = $prev_state['comments'][ $t ]['draft_edits']['thread'];
$edited_attachment = $prev_state['comments'][ $t ]['draft_edits']['attachmentText'];
if ( ! empty( $edited_draft ) ) {
$prev_state['comments'][ $t ]['thread'] = $edited_draft;
}
if ( ! empty( $edited_attachment ) ) {
$prev_state['comments'][ $t ]['attachmentText'] = $edited_attachment;
} else {
$prev_state['comments'][ $t ]['attachmentText'] = '';
}
// Change status to publish.
$prev_state['comments'][ $t ]['status'] = 'publish';
// Remove comment from edited_draft.
unset( $prev_state['comments'][ $t ]['draft_edits']['thread'] );
unset( $prev_state['comments'][ $t ]['draft_edits']['attachmentText'] );
}
$prev_state['updated_at'] = $current_timestamp;
update_post_meta( $post_ID, $el, $prev_state );
update_post_meta( $post_ID, 'th' . $el, $current_timestamp );
$metas[ $el ][0] = maybe_serialize( $prev_state );
}
}
if ( isset( $current_drafts ) && ! empty( $current_drafts ) ) {
// create and update the mc_uodated meta
update_post_meta( $post_ID, 'mc_updated', $current_timestamp );
}
// Flush Current Drafts Stack.
update_post_meta( $post_ID, '_current_drafts', '' );
// Update open comments count.
$comment_counts = $this->cf_get_comment_counts( $post_ID, $p_content, $metas );
update_post_meta( $post_ID, 'open_cf_count', $comment_counts['open_counts'] );
// Create and Update the last user for summary tab in activity center.
update_post_meta( $post_ID, 'last_user_edited', $current_user_display_name );
// Deleteing comments if users delete comments at the same moment.
if ( ! empty( $current_drafts['deleted'] ) ) {
foreach ( $current_drafts['deleted'] as $key => $value ) {
$comment = get_post_meta( $post_ID, $key, true );
foreach ( $value as $delete_key ) {
unset( $comment['comments'][ $delete_key ] );
}
update_post_meta( $post_ID, $key, $comment );
}
}
// Sending Emails to newly mentioned users.
if ( isset( $current_drafts['comments'] ) && 0 !== count( $current_drafts['comments'] ) ) {
$new_drafts = $current_drafts['comments'];
foreach ( $new_drafts as $el => $drafts ) {
$comments = get_post_meta( $post_ID, $el, true );
$commented_on_text = $comments['commentedOnText'];
$assigned_to = isset( $comments['assigned_to'] ) ? $comments['assigned_to'] : '';
$list_of_comments = isset( $comments['comments'] ) ? $comments['comments'] : '';
$blockType = isset( $comments['blockType'] ) ? $comments['blockType'] : '';
$link = $p_link . '¤t_url=' . $elid;
$prev_state = $metas[ $el ][0];
$prev_state = maybe_unserialize( $prev_state );
$new_comments = array();
foreach ( $drafts as $d ) {
$prev_state['comments'][ $d ]['status'] = 'publish';
$new_comments[] = $d;
}
// Send email to the commented recipients.
$this->email_class->cf_email_new_comments(
array(
'post_ID' => $post_ID,
'elid' => $elid,
'post_title' => $p_title,
'post_edit_link' => $link,
'site_title' => $site_title,
'commented_on_text' => $commented_on_text,
'list_of_comments' => $list_of_comments,
'current_user_email' => $current_user_email,
'current_user_display_name' => $current_user_display_name,
'new_comments' => $new_comments,
'assign_to' => $assigned_to,
'block_type' => $blockType,
)
);
}
}
}
/**
* Include the Email template class and initiate the object.
*/
private function cf_initiate_email_class() {
require_once COMMENTING_BLOCK_DIR . 'admin/classes/class-commenting-block-email-templates.php'; // Removed phpcs:ignore by Rishi Shah.
$this->email_class = new Commenting_Block_Email_Templates();
}
/**
* @param string $string The string to be limited.
* @param int $limit The total number of characters allowed.
*
* @return string The limited string with '...' appended.
*/
public function cf_limit_characters( $string, $limit = 100 ) {
return strlen( $string ) > $limit ? substr( $string, 0, $limit ) . '...' : $string;
}