-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass-contextly.php
1614 lines (1408 loc) · 44.5 KB
/
class-contextly.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
/**
* Main class.
*
* @package Contextly Related Links
* @link https://contextly.com
*/
/**
* Class Contextly
*/
class Contextly {
const API_SETTINGS_KEY = 'contextly_options_api';
const ADVANCED_SETTINGS_KEY = 'contextly_options_advanced';
const DEFAULT_PLACEMENT_CLASS = 'ctx_default_placement';
const WIDGET_PLACEMENT_CLASS = 'ctx_widget_placement';
const SHORTCODE_PLACEMENT_CLASS = 'ctx_shortcode_placement';
const WIDGET_SIDEBAR_CLASS = 'ctx-sidebar-container';
const WIDGET_SIDEBAR_PREFIX = 'ctx-sidebar-container--';
const WIDGET_AUTO_SIDEBAR_CLASS = 'ctx-autosidebar-container';
const WIDGET_AUTO_SIDEBAR_PREFIX = 'ctx-autosidebar-container--';
const WIDGET_AUTO_SIDEBAR_CODE = '[contextly_auto_sidebar]';
const CLEARFIX_CLASS = 'ctx-clearfix';
const WIDGET_SNIPPET_CLASS = 'ctx-module-container';
const WIDGET_STORYLINE_CLASS = 'ctx-subscribe-container';
const WIDGET_PERSONALIZATION_CLASS = 'ctx-personalization-container';
const WIDGET_CHANNEL_CLASS = 'ctx-channel-container';
const WIDGET_SIDERAIL_CLASS = 'ctx-siderail-container';
const WIDGET_AUTHOR_CLASS = 'ctx-author-container';
const MAIN_MODULE_SHORT_CODE = 'contextly_main_module';
const SL_MODULE_SHORT_CODE = 'contextly_sl_button';
const PERSONALIZATION_MODULE_SHORT_CODE = 'contextly_personalization_button';
const CHANNEL_MODULE_SHORT_CODE = 'contextly_channel_button';
const ALL_BUTTONS_SHORT_CODE = 'contextly_all_buttons';
const SIDERAIL_MODULE_SHORT_CODE = 'contextly_siderail';
const AUTHOR_MODULE_SHORT_CODE = 'contextly_author';
/**
* APi instance.
*
* @var ContextlyKitApi
*/
protected $api;
/**
* Additional inline JS.
*
* @var string $inline_js
*/
protected $inline_js = '';
/**
* Gte API instance.
*
* @return ContextlyKitApi API instance.
*/
protected function api() {
if ( ! isset( $this->api ) ) {
$this->api = ContextlyWpKit::getInstance()->newApi();
}
return $this->api;
}
/**
* Main init method.
*/
public function init() {
if ( is_admin() ) {
add_action( 'admin_enqueue_scripts', array( $this, 'init_admin' ), 1 );
add_action( 'save_post', array( $this, 'publish_box_control_save_post_hook' ) );
add_filter( 'default_content', array( $this, 'add_autosidebar_code_filter' ), 10, 2 );
add_action( 'admin_head', array( $this, 'insert_metatags' ), 0 );
add_action( 'admin_head', array( $this, 'insert_head_scripts' ), 1 );
add_action( 'admin_footer', array( $this, 'insert_footer_scripts' ), 0 );
register_activation_hook( CONTEXTLY_PLUGIN_FILE, array( $this, 'add_activation_hook' ) );
// Register overlay dialog page.
ContextlyWpKit::getInstance()
->newWpOverlayPage()
->addMenuAction();
} else {
add_action( 'the_content', array( $this, 'add_snippet_widget_to_content' ) );
add_action( 'wp_head', array( $this, 'insert_metatags' ), 0 );
$head_action = CONTEXTLY_HEAD_SCRIPT_ACTION;
if ( ! empty( $head_action ) ) {
add_action( $head_action, array( $this, 'insert_head_scripts' ), CONTEXTLY_HEAD_SCRIPT_WEIGHT );
}
$footer_action = CONTEXTLY_FOOTER_SCRIPT_ACTION;
if ( ! empty( $footer_action ) ) {
add_action( $footer_action, array( $this, 'insert_footer_scripts' ), CONTEXTLY_FOOTER_SCRIPT_WEIGHT );
}
// Add auto-placement anchor with priority a bit higher than default 10 to run after
// wpautop() that causes the anchor to end up inside a P tag.
add_action( 'the_content', array( $this, 'add_article_root_anchor_to_content' ), 11 );
}
add_action( 'init', array( $this, 'init_default' ), 1 );
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
add_action( 'publish_post', array( $this, 'publish_post' ), 10, 2 );
add_action( 'save_post', array( $this, 'publish_post' ), 10, 2 );
add_action('enqueue_block_editor_assets', array( $this, 'register_block_assets' ) );
add_action('enqueue_block_assets', array( $this, 'register_block_assets' ) );
$this->attach_ajax_actions();
$this->expose_public_actions();
}
/**
* Register blocks.
*/
public function register_block_assets() {
if ( $this->is_load_widget() && function_exists( 'register_block_type' ) ) {
$this->register_sidebar_block();
$this->register_auto_sidebar_block();
}
}
/**
* Override some actions.
*/
public function expose_public_actions() {
add_action( 'contextly_print_metatags', array( $this, 'print_post_metatags' ), 10, 2 );
add_action( 'contextly_print_init_script', array( $this, 'print_init_script' ) );
add_action( 'contextly_print_launch_script', array( $this, 'print_launch_script' ), 10, 2 );
add_action( 'contextly_print_removal_script', array( $this, 'print_removal_script' ), 10, 2 );
add_filter( 'contextly_post_metadata', array( $this, 'fill_post_metadata' ), 10, 2 );
add_filter( 'contextly_post_js_data', array( $this, 'fill_post_js_data' ), 10, 2 );
}
/**
* Attach AJAX actions.
*/
private function attach_ajax_actions() {
add_action( 'wp_ajax_nopriv_contextly_publish_post', array( $this, 'ajax_publish_post_callback' ) );
add_action( 'wp_ajax_contextly_publish_post', array( $this, 'ajax_publish_post_callback' ) );
add_action( 'wp_ajax_contextly_get_auth_token', array( $this, 'ajax_get_auth_token_callback' ) );
ContextlyWpKit::getInstance()
->newWidgetsEditor()
->addAjaxActions();
}
/**
* Check if this is admin page.
*
* @return bool true if this is admin edit page.
*/
private function is_admin_edit_page() {
global $pagenow;
if ( ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) && is_admin() ) {
return true;
}
return false;
}
/**
* Check widget display type.
*
* @param null|object $post_param WP post object.
* @return bool true if post type is allowed.
*/
public function check_widget_display_type( $post_param = null ) {
global $post;
$post_object = null;
if ( null !== $post_param ) {
$post_object = $post_param;
} elseif ( isset( $post ) ) {
$post_object = $post;
}
if ( null !== $post_object && isset( $post_object->post_type ) ) {
if ( $this->is_post_type_display_allowed( $post_object->post_type ) ) {
return true;
}
}
return false;
}
/**
* Check if some post type allowed for display modules.
*
* @param $post_type
* @return bool
*/
public function is_post_type_display_allowed( $post_type ) {
$contextly_settings = new ContextlySettings();
$display_types = $contextly_settings->get_widget_display_type();
return in_array( $post_type, array_values( $display_types ) );
}
/**
* Get API options.
*
* @return array array of options.
*/
public static function get_api_client_options() {
$client_options = array(
'appID' => '',
'appSecret' => '',
);
$api_options = get_option( self::API_SETTINGS_KEY );
if ( is_array( $api_options ) && isset( $api_options['api_key'] ) ) {
$api_key = explode( '-', trim( $api_options['api_key'] ) );
if ( count( $api_key ) === 2 ) {
$client_options['appID'] = $api_key[0];
$client_options['appSecret'] = $api_key[1];
}
}
return $client_options;
}
/**
* Get author full name.
*
* @param object $post WP post object.
* @return string author full name.
*/
private function get_author_full_name( $post ) {
if ( get_the_author_meta( 'first_name', $post->post_author ) || get_the_author_meta( 'last_name', $post->post_author ) ) {
$name = get_the_author_meta( 'first_name', $post->post_author ) . ' ' . get_the_author_meta( 'last_name', $post->post_author );
return trim( $name );
}
return null;
}
/**
* Get post author display name.
*
* @param object $post WP post object.
* @return string author display name.
*/
private function get_author_display_name( $post ) {
$display_name = get_the_author_meta( 'display_name', $post->post_author );
$nickname = get_the_author_meta( 'nickname', $post->post_author );
$name = $display_name ? $display_name : $nickname;
return $name;
}
/**
* Admin init method.
*/
public function init_admin() {
if ( $this->check_widget_display_type() ) {
$contextly_settings = new ContextlySettings();
$display_types = $contextly_settings->get_widget_display_type();
foreach ( $display_types as $display_type ) {
$this->add_admin_metabox_for_page( $display_type );
if ( $this->is_load_widget() ) {
$this->add_admin_publish_metabox_for_page( $display_type );
}
}
global $post;
if ( ! $contextly_settings->is_page_display_disabled( $post->ID ) ) {
$this->add_editor_buttons();
}
}
}
/**
* Hook for post publish action.
*
* @param int $post_id post ID.
* @return bool true if publish box available.
*/
public function publish_box_control_save_post_hook( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return false;
}
if ( empty( $post_id ) ) {
return false;
}
if ( isset( $_POST['contextly_display_widgets'] ) ) { // WPCS: CSRF ok.
$display_widget_flag = sanitize_text_field( wp_unslash( $_POST['contextly_display_widgets'] ) ); // WPCS: CSRF ok. Input var okay.
$contextly_settings = new ContextlySettings();
$contextly_settings->change_page_display( $post_id, $display_widget_flag );
}
return true;
}
/**
* Add admin metabox page.
*/
private function add_admin_publish_metabox_for_page() {
add_action( 'post_submitbox_misc_actions', array( $this, 'echo_admin_publish_metabox_for_page' ) );
}
/**
* Echo admin metabox for page.
*/
public function echo_admin_publish_metabox_for_page() {
echo '<div class="misc-pub-section misc-pub-section-last" style="border-top: 1px solid #eee; margin-bottom: 5px;">';
echo 'Contextly: <a class="button action button-primary ctx_snippets_editor_btn" disabled="disabled" style="float: right;">Loading...</a>';
echo '</div>';
}
/**
* Main init method.
*/
public function init_default() {
add_shortcode( self::MAIN_MODULE_SHORT_CODE, array( $this, 'prepare_main_module_short_code' ) );
add_shortcode( 'contextly_sidebar', array( $this, 'prepare_sidebar' ) );
add_shortcode( 'contextly_auto_sidebar', array( $this, 'prepare_auto_sidebar' ) );
add_shortcode( self::SL_MODULE_SHORT_CODE, array( $this, 'prepare_fu_button_short_code' ) );
add_shortcode( self::PERSONALIZATION_MODULE_SHORT_CODE, array( $this, 'prepare_personalization_button_short_code' ) );
add_shortcode( self::CHANNEL_MODULE_SHORT_CODE, array( $this, 'prepare_channel_button_short_code' ) );
add_shortcode( self::ALL_BUTTONS_SHORT_CODE, array( $this, 'prepare_all_buttons_short_code' ) );
add_shortcode( self::SIDERAIL_MODULE_SHORT_CODE, array( $this, 'prepare_siderail_short_code' ) );
add_shortcode( self::AUTHOR_MODULE_SHORT_CODE, array( $this, 'prepare_author_short_code' ) );
}
public function register_sidebar_block() {
if (is_admin()) {
$block_js = 'js/contextly-sidebar-block.js';
wp_enqueue_script(
'contextly-sidebar-block',
plugins_url($block_js, __FILE__),
array('wp-blocks', 'wp-element', 'wp-components', 'wp-editor')
);
}
register_block_type( 'contextly-related-links-block/contextly-sidebar', array(
'editor_script' => 'contextly-sidebar-block',
'render_callback' => array( $this, 'prepare_sidebar' ),
) );
}
public function register_auto_sidebar_block() {
if (is_admin()) {
$block_js = 'js/contextly-auto-sidebar-block.js';
wp_enqueue_script(
'contextly-auto-sidebar-block',
plugins_url($block_js, __FILE__),
array('wp-blocks', 'wp-element', 'wp-components', 'wp-editor')
);
}
register_block_type( 'contextly-related-links-block/contextly-auto-sidebar', array(
'editor_script' => 'contextly-auto-sidebar-block',
'render_callback' => array( $this, 'prepare_auto_sidebar' ),
) );
}
/**
* Add MCE editor buttons.
*/
private function add_editor_buttons() {
// Don't bother doing this stuff if the current user lacks permissions.
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
// Add only in Rich Editor mode.
if ( get_user_option( 'rich_editing' ) === 'true' ) {
add_filter( 'mce_external_plugins', array( $this, 'add_mce_buttons' ) );
add_filter( 'mce_buttons', array( $this, 'register_mce_buttons' ) );
}
}
/**
* Add admin metabox for Contextly.
*
* @param string $page_type page type.
*/
private function add_admin_metabox_for_page( $page_type ) {
add_meta_box(
'contextly_linker_sectionid',
__( 'Contextly Related Links', 'contextly_linker_textdomain' ),
array( $this, 'echo_admin_metabox_content' ),
$page_type,
'normal',
'high'
);
}
/**
* Echo admin metabox content.
*/
public function echo_admin_metabox_content() {
echo $this->get_snippet_widget(); // WPCS: XSS ok.
}
/**
* Add snippet shortcode to page content.
*
* @param string $content page content.
* @return string page content.
*/
public function add_snippet_widget_to_content( $content ) {
return $content . $this->get_snippet_widget();
}
/**
* Add article anchor to post content.
*
* @param string $content post content.
* @return string content.
*/
public function add_article_root_anchor_to_content( $content ) {
return $content . $this->get_article_root_anchor();
}
/**
* Get article root anchor.
*
* @return string root anchor HTML.
*/
public function get_article_root_anchor() {
return '<span class="ctx-article-root"><!-- --></span>';
}
/**
* Registed Contextly tiny MCE buttons.
*
* @param array $buttons tiny mce buttons.
* @return array array of buttons.
*/
public function register_mce_buttons( $buttons ) {
$options = get_option( self::ADVANCED_SETTINGS_KEY );
// Now we need to add contextly link btn at position of native link button.
if ( is_array( $buttons ) && count( $buttons ) > 0 ) {
foreach ( $buttons as $btn_idx => $button ) {
if ( 'link' === $button ) {
if ( 'override' === $options['link_type'] ) {
$buttons[ $btn_idx ] = 'contextlylink';
} else {
array_splice( $buttons, $btn_idx, 0, 'contextlylink' );
}
}
}
} else {
if ( ! $options['link_type'] ) {
array_push( $buttons, 'separator', 'contextlylink' );
}
}
array_push( $buttons, 'separator', 'contextly' );
array_push( $buttons, 'separator', 'contextlysidebar' );
return $buttons;
}
/**
* Add MCE editor buttons.
*
* @param array $plugin_array MCE plugins array.
* @return array array of all MCE plugins.
*/
public function add_mce_buttons( $plugin_array ) {
$plugin_array['contextly'] = plugins_url( 'js/contextly-tinymce.js?v=' . CONTEXTLY_PLUGIN_VERSION, __FILE__ );
return $plugin_array;
}
/**
* Get Contextly enable/disable control HTML.
*
* @return string control HTML.
*/
public function get_additional_show_hide_control() {
global $post;
$html = '';
if ( isset( $post ) && $post->ID ) {
$contextly_settings = new ContextlySettings();
$flag = $contextly_settings->is_page_display_disabled( $post->ID );
if ( ! $flag ) {
$html .= '<a class="button action ctx_snippets_editor_btn" disabled="disabled">Loading...</a>';
}
$html .= '<div style="border-top: 1px solid #DFDFDF; margin-top: 8px; padding-top: 8px;"><span id="timestamp">';
$html .= '<label>Don\'t display Contextly content on this ' . esc_html( $post->post_type ) . ': ';
$html .= "<input type='hidden' name='contextly_display_widgets' value='' />";
$html .= "<input type='checkbox' name='contextly_display_widgets' " . ( $flag ? "checked='checked'" : '' ) . " onchange=\"jQuery('#post').submit();\" /></label>";
$html .= '</span></div>';
// Wrap with div, so post editor could render button here.
$html = '<div class="ctx_preview_admin_controls">' . $html . '</div>'; // WPCS: XSS ok.
}
return $html;
}
/**
* Get main module HTML.
*
* @return string main module HTML.
*/
public function get_snippet_widget() {
global $post;
$prefix = '';
$default_html_code = '';
$additional_html_controls = '';
if ( is_admin() ) {
$contextly_settings = new ContextlySettings();
$display_post_settings = $contextly_settings->is_page_display_disabled( $post->ID );
$display_global_settings = $this->check_widget_display_type();
if ( $display_global_settings && ! $display_post_settings ) {
$default_html_code = "Loading data from <a target='_blank' href='http://contextly.com'>contextly.com</a>, please wait...";
if ( ! isset( $post ) || ! $post->ID ) {
$default_html_code = 'Please save a Draft first.';
}
} else {
if ( $display_post_settings && $display_global_settings ) {
$default_html_code = 'Contextly content is turned off for this post/page. You can change this via the checkbox below.';
} else {
$default_html_code = 'Contextly content is turned off for this page. You can change this in <a href="admin.php?page=contextly_options&tab=contextly_options_advanced">Contextly settings page</a> in WordPress, under advanced.';
}
}
if ( $display_global_settings ) {
$additional_html_controls = $this->get_additional_show_hide_control();
}
}
$classes = array(
self::WIDGET_SNIPPET_CLASS,
self::DEFAULT_PLACEMENT_CLASS,
self::CLEARFIX_CLASS,
);
return $prefix . "<div class='" . esc_attr( $this->join_classes( $classes ) ) . "'>" . $default_html_code . '</div>' . $additional_html_controls; // WPCS: XSS ok.
}
/**
* Get plugin JS file URL.
*
* @param string $script_name script name.
* @return string plugin JS url.
*/
public function get_plugin_js( $script_name ) {
if ( CONTEXTLY_MODE === Urls::MODE_LIVE ) {
return Urls::get_plugin_cdn_url( $script_name, 'js' );
} else {
return plugins_url( 'js/' . $script_name, __FILE__ );
}
}
/**
* Get WP ajax url.
*
* @return string url.
*/
public static function get_ajax_url() {
return admin_url( 'admin-ajax.php' );
}
/**
* Get editor overlay url.
*
* @return string url.
*/
public static function get_overlay_editor_url() {
return admin_url( 'admin.php?page=contextly_overlay_dialog&noheader' );
}
/**
* Build Contextly SJ object.
*
* @param array $additional_params additional parameters if needed.
* @return array|null updated Contextly object.
*/
public static function get_contextly_js_object( $additional_params = null ) {
global $post;
$options = array(
'ajax_url' => self::get_ajax_url(),
);
if ( is_admin() ) {
$options += array(
'editor_url' => self::get_overlay_editor_url(),
'settings' => ContextlySettings::get_plugin_options(),
);
if ( isset( $post->ID ) ) {
$options['editor_post_id'] = $post->ID;
}
}
if ( null !== $additional_params ) {
$options = $options + $additional_params;
}
return $options;
}
/**
* Add needed params in post data.
*
* @param array $data parameters.
* @param object $post WP post object.
* @return array array of changed parameters.
*/
public static function fill_post_js_data( $data, $post ) {
if ( ! empty( $post->ID ) ) {
$data += array(
'ajax_nonce' => wp_create_nonce( "contextly-post-{$post->ID}" ),
);
}
return $data;
}
/**
* Override some Kit settings.
*
* @return array array of Kit settings.
*/
public static function get_kit_settings_overrides() {
$api_options = self::get_api_client_options();
$overrides = array(
'appId' => $api_options['appID'],
'https' => CONTEXTLY_HTTPS,
'client' => array(
'client' => 'wp',
'version' => CONTEXTLY_PLUGIN_VERSION,
),
);
if ( CONTEXTLY_MODE !== Urls::MODE_LIVE ) {
$overrides += array(
'mode' => CONTEXTLY_MODE,
'assetUrl' => plugin_dir_url( __FILE__ ) . 'kit/client/src',
);
}
if ( (boolean) is_admin() ) {
$overrides += array(
'admin' => true,
'branding' => false,
);
}
return $overrides;
}
/**
* Is widget need to be loaded.
*
* @return bool if widget nee to be loaded.
*/
public function is_load_widget() {
global $post;
$contextly_settings = new ContextlySettings();
// check regular WP pages
if ( $this->check_widget_display_type() && ! $contextly_settings->is_page_display_disabled( $post->ID ) ) {
if (is_page() || is_single() || $this->is_admin_edit_page()) {
return true;
}
}
// check non posts pages
$page_type = $contextly_settings->get_wp_page_type();
$enabled_non_article_pages = $contextly_settings->get_enable_non_article_page_display();
if ( in_array( $page_type, $enabled_non_article_pages) ) {
return true;
}
return false;
}
/**
* Load needed libraries.
*/
public function load_scripts() {
if ( ! $this->is_load_widget() ) {
return;
}
if ($this->is_admin_edit_page()) {
wp_enqueue_script( 'jquery' );
}
}
/**
* Load needed Contextly scripts.
*
* @param array $params input parameters.
*/
public function insert_kit_scripts( $params = array() ) {
$kit = ContextlyWpKit::getInstance();
$params += array(
'preload' => '',
'libraries' => array(),
'foreign' => array(),
'overrides' => true,
'wpdata' => true,
'loader' => $kit->getLoaderName(),
);
static $known_packages = array(
'wp/widgets' => array(
'include' => array(
'widgets' => true,
),
'js' => array(
'contextly-wordpress.js' => true,
),
),
'wp/editor' => array(
'include' => array(
'wp/widgets' => true,
'overlay-dialogs/overlay' => true,
),
'js' => array(
'contextly-post-editor.js' => true,
'contextly-quicktags.js' => true,
),
),
);
$foreign_packages = array_intersect_key( $known_packages, $params['foreign'] );
foreach ( $foreign_packages as $name => $contents ) {
$contents += array(
'js' => array(),
'include' => array(),
);
$js = array();
foreach ( array_keys( $contents['js'] ) as $script_name ) {
$js[ $this->get_plugin_js( $script_name ) ] = true;
}
$contents['js'] = $js;
$foreign_packages[ $name ] = $kit->newAssetsPackageForeign()
->addAssets( $contents )
->addIncluded( $contents['include'] )
->toExposed();
}
$ready = array();
if ( ! empty( $foreign_packages ) ) {
$ready[] = array( 'expose', $foreign_packages );
}
if ( ! empty( $params['libraries'] ) ) {
$ready[] = array( 'libraries', $params['libraries'] );
}
if ( ! empty( $params['preload'] ) ) {
$ready[] = array( 'load', $params['preload'] );
}
$manager = $kit->newAssetsManager();
$packages = $manager->getPackageWithDependencies( $params['loader'] );
$exposed_tree = $manager->buildExposedTree( array_keys( $packages ) );
$code = '';
if ( ! empty( $params['overrides'] ) ) {
$code .= $kit->newOverridesManager( Contextly::get_kit_settings_overrides() )
->compile( true );
}
if ( ! empty( $params['wpdata'] ) ) {
$code .= $kit->newJsExporter( $this->get_contextly_js_object() )
->export( 'wpdata', true );
}
echo $kit->newAssetsAsyncRenderer( $packages, $exposed_tree )
->renderAll(
array(
'ready' => $ready,
'code' => $code,
)
); // WPCS: XSS ok.
}
/**
* Inserts async loader into the page head.
*/
public function insert_head_scripts() {
$params = array(
// Give some context, to let filters know who initiated the call.
'source' => 'contextly-linker',
'enabled' => $this->is_load_widget(),
'preload' => true,
'editor' => $this->is_admin_edit_page(),
);
do_action( 'contextly_print_init_script', $params );
}
/**
* Prints initialization script.
*
* Important! This function must be called AFTER other scripts are
* inserted into the page DOM, because otherwise it causes race condition.
* According to the tests, the only code always executed before the loader
* is the synchronous JS inside the same <script> tag.
*
* @param array $params input parameters.
*/
public function print_init_script( $params = array() ) {
$params += array(
'preload' => true,
'enabled' => true,
'editor' => false,
);
$params = apply_filters( 'contextly_init_script_options', $params );
if ( empty( $params['enabled'] ) ) {
return;
}
$package_name = 'wp/widgets';
$packages = array(
$package_name => true,
);
if ( ! empty( $params['editor'] ) ) {
$package_name = 'wp/editor';
$packages[ $package_name ] = true;
}
$this->insert_kit_scripts(
array(
'foreign' => $packages,
'preload' => ! empty( $params['preload'] ) ? $package_name : null,
'overrides' => true,
'libraries' => array(
'jquery' => false,
),
)
);
}
/**
* Add footer scripts.
*/
public function insert_footer_scripts() {
global $post;
$params = array(
// Give some context, to let filters know who initiated the call.
'source' => 'contextly-linker',
'enabled' => $this->is_load_widget(),
'editor' => $this->is_admin_edit_page(),
'metadata' => false,
'context' => null,
);
do_action( 'contextly_print_launch_script', $post, $params );
}
/**
* Launch script action.
*
* @param object $post WP post object.
* @param array $params input params.
*/
public function print_launch_script( $post, $params = array() ) {
$params += array(
'enabled' => true,
'editor' => false,
'context' => null,
'metadata' => false,
);
$params = apply_filters( 'contextly_launch_script_options', $params, $post );
if ( empty( $params['enabled'] ) ) {
return;
}
$widgets_options = array();
if ( ! empty( $params['metadata'] ) ) {
$widgets_options['metadata'] = apply_filters( 'contextly_post_metadata', array(), $post );
}
if ( isset( $params['context'] ) ) {
$widgets_options['context'] = $params['context'];
}
$widgets_args = array( 'widgets' );
if ( ! empty( $widgets_options ) ) {
$widgets_args[] = $widgets_options;
}
$post_data_args = array();
if ( ! empty( $post ) ) {
$post_data = apply_filters( 'contextly_post_js_data', array(), $post );
if ( ! empty( $post_data ) ) {
$post_data_args = array( $post->ID, $post_data );
}
}
$widgets_code = '';
if ( ! empty( $post_data_args ) ) {
$widgets_code .= 'Contextly.WPSettings.setPostData(' . $this->encode_args_for_script( $post_data_args ) . ');';
}
$widgets_code .= 'Contextly.ready(' . $this->encode_args_for_script( $widgets_args ) . ');';
$load = array(
'wp/widgets' => $widgets_code,
);
if ( ! empty( $params['editor'] ) ) {
$load['wp/editor'] = 'Contextly.PostEditor.loadData();';
}
$this->render(
'launch-script', array(
'load' => $load,
)
);
}
/**
* Adjust removal script action.
*
* @param object $post WP post.
* @param array $params input params.
*/
public function print_removal_script( $post, $params = array() ) {
$params += array(
'enabled' => true,
'editor' => false,
'context' => null,
);
$params = apply_filters( 'contextly_removal_script_options', $params, $post );
if ( empty( $params['enabled'] ) ) {
return;
}
$options = array();
if ( isset( $params['context'] ) ) {
$options['context'] = $params['context'];
}
$this->render(
'removal-script', array(
'package_name' => 'wp/widgets',
'options' => $options,
)
);
}
/**
* Encodes passed data as JSON safe for SCRIPT tag.
*
* PHP 5.3 is required.
*
* @param array $data input params.
*
* @return mixed|string JSON text.
*
* @throws Exception In case of very old php version.
*/
public static function encode_json_for_script( $data ) {
static $php_checked = false;
if ( ! $php_checked ) {
if ( ! version_compare( PHP_VERSION, '5.3', '>=' ) ) {
throw new Exception( 'PHP 5.3 is required to output inline metadata' );
}
$php_checked = true;
}
return wp_json_encode( $data, JSON_HEX_TAG & JSON_HEX_AMP & JSON_HEX_APOS & JSON_HEX_QUOT );
}
/**
* JSON-encodes passed array so that it may be printed as JS function arguments in SCRIPT tag.
*
* @param array $args arguments.
*
* @return string javascript expression.
*/
public static function encode_args_for_script( $args ) {
$args = array_map( array( 'Contextly', 'encode_json_for_script' ), $args );
return implode( ',', $args );
}
/**
* Render plugin views.