forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.jetpack.php
7401 lines (6381 loc) · 242 KB
/
class.jetpack.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
/*
Options:
jetpack_options (array)
An array of options.
@see Jetpack_Options::get_option_names()
jetpack_register (string)
Temporary verification secrets.
jetpack_activated (int)
1: the plugin was activated normally
2: the plugin was activated on this site because of a network-wide activation
3: the plugin was auto-installed
4: the plugin was manually disconnected (but is still installed)
jetpack_active_modules (array)
Array of active module slugs.
jetpack_do_activate (bool)
Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
*/
require_once( JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php' );
class Jetpack {
public $xmlrpc_server = null;
private $xmlrpc_verification = null;
private $rest_authentication_status = null;
public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA']
/**
* @var array The handles of styles that are concatenated into jetpack.css.
*
* When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js.
*/
public $concatenated_style_handles = array(
'jetpack-carousel',
'grunion.css',
'the-neverending-homepage',
'jetpack_likes',
'jetpack_related-posts',
'sharedaddy',
'jetpack-slideshow',
'presentations',
'quiz',
'jetpack-subscriptions',
'jetpack-responsive-videos-style',
'jetpack-social-menu',
'tiled-gallery',
'jetpack_display_posts_widget',
'gravatar-profile-widget',
'goodreads-widget',
'jetpack_social_media_icons_widget',
'jetpack-top-posts-widget',
'jetpack_image_widget',
'jetpack-my-community-widget',
'jetpack-authors-widget',
'wordads',
'eu-cookie-law-style',
'flickr-widget-style',
'jetpack-search-widget',
'jetpack-simple-payments-widget-style',
'jetpack-widget-social-icons-styles',
);
/**
* Contains all assets that have had their URL rewritten to minified versions.
*
* @var array
*/
static $min_assets = array();
public $plugins_to_deactivate = array(
'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ),
'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ),
'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ),
'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ),
'videopress' => array( 'video/video.php', 'VideoPress' ),
'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),
'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),
'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),
'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' )
);
static $capability_translations = array(
'administrator' => 'manage_options',
'editor' => 'edit_others_posts',
'author' => 'publish_posts',
'contributor' => 'edit_posts',
'subscriber' => 'read',
);
/**
* Map of modules that have conflicts with plugins and should not be auto-activated
* if the plugins are active. Used by filter_default_modules
*
* Plugin Authors: If you'd like to prevent a single module from auto-activating,
* change `module-slug` and add this to your plugin:
*
* add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' );
* function my_jetpack_get_default_modules( $modules ) {
* return array_diff( $modules, array( 'module-slug' ) );
* }
*
* @var array
*/
private $conflicting_plugins = array(
'comments' => array(
'Intense Debate' => 'intensedebate/intensedebate.php',
'Disqus' => 'disqus-comment-system/disqus.php',
'Livefyre' => 'livefyre-comments/livefyre.php',
'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php',
'Google+ Comments' => 'google-plus-comments/google-plus-comments.php',
'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php',
),
'comment-likes' => array(
'Epoch' => 'epoch/plugincore.php',
),
'contact-form' => array(
'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php',
'Gravity Forms' => 'gravityforms/gravityforms.php',
'Contact Form Plugin' => 'contact-form-plugin/contact_form.php',
'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php',
'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php',
'Ninja Forms' => 'ninja-forms/ninja-forms.php',
),
'minileven' => array(
'WPtouch' => 'wptouch/wptouch.php',
),
'latex' => array(
'LaTeX for WordPress' => 'latex/latex.php',
'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php',
'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php',
'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php',
'Enable Latex' => 'enable-latex/enable-latex.php',
'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php',
),
'protect' => array(
'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php',
'Captcha' => 'captcha/captcha.php',
'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php',
'Login Security Solution' => 'login-security-solution/login-security-solution.php',
'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php',
'BulletProof Security' => 'bulletproof-security/bulletproof-security.php',
'SiteGuard WP Plugin' => 'siteguard/siteguard.php',
'Security-protection' => 'security-protection/security-protection.php',
'Login Security' => 'login-security/login-security.php',
'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php',
'Wordfence Security' => 'wordfence/wordfence.php',
'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php',
'iThemes Security' => 'better-wp-security/better-wp-security.php',
),
'random-redirect' => array(
'Random Redirect 2' => 'random-redirect-2/random-redirect.php',
),
'related-posts' => array(
'YARPP' => 'yet-another-related-posts-plugin/yarpp.php',
'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php',
'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php',
'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php',
'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php',
'outbrain' => 'outbrain/outbrain.php',
'Shareaholic' => 'shareaholic/shareaholic.php',
'Sexybookmarks' => 'sexybookmarks/shareaholic.php',
),
'sharedaddy' => array(
'AddThis' => 'addthis/addthis_social_widget.php',
'Add To Any' => 'add-to-any/add-to-any.php',
'ShareThis' => 'share-this/sharethis.php',
'Shareaholic' => 'shareaholic/shareaholic.php',
),
'seo-tools' => array(
'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
'The SEO Framework' => 'autodescription/autodescription.php',
),
'verification-tools' => array(
'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
'The SEO Framework' => 'autodescription/autodescription.php',
),
'widget-visibility' => array(
'Widget Logic' => 'widget-logic/widget_logic.php',
'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php',
),
'sitemaps' => array(
'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php',
'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php',
'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php',
'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php',
'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php',
'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
'The SEO Framework' => 'autodescription/autodescription.php',
'Sitemap' => 'sitemap/sitemap.php',
'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php',
'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php',
'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php',
'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php',
),
'lazy-images' => array(
'Lazy Load' => 'lazy-load/lazy-load.php',
'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php',
'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php',
),
);
/**
* Plugins for which we turn off our Facebook OG Tags implementation.
*
* Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate
* Jetpack's Open Graph tags via filter when their Social Meta modules are active.
*
* Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter:
* add_filter( 'jetpack_enable_open_graph', '__return_false' );
*/
private $open_graph_conflicting_plugins = array(
'2-click-socialmedia-buttons/2-click-socialmedia-buttons.php',
// 2 Click Social Media Buttons
'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook
'add-meta-tags/add-meta-tags.php', // Add Meta Tags
'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail
'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php',
// Open Graph Meta Tags by Heateor
'facebook/facebook.php', // Facebook (official plugin)
'facebook-awd/AWD_facebook.php', // Facebook AWD All in one
'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php',
// Facebook Featured Image & OG Meta Tags
'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags
'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php',
// Facebook Open Graph Meta Tags for WordPress
'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag
'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer
'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php',
// Fedmich's Facebook Open Graph Meta
'network-publisher/networkpub.php', // Network Publisher
'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG
'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php',
// NextScripts SNAP
'og-tags/og-tags.php', // OG Tags
'opengraph/opengraph.php', // Open Graph
'open-graph-protocol-framework/open-graph-protocol-framework.php',
// Open Graph Protocol Framework
'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments
'seo-ultimate/seo-ultimate.php', // SEO Ultimate
'sexybookmarks/sexy-bookmarks.php', // Shareaholic
'shareaholic/sexy-bookmarks.php', // Shareaholic
'sharepress/sharepress.php', // SharePress
'simple-facebook-connect/sfc.php', // Simple Facebook Connect
'social-discussions/social-discussions.php', // Social Discussions
'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit
'socialize/socialize.php', // Socialize
'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™
'only-tweet-like-share-and-google-1/tweet-like-plusone.php',
// Tweet, Like, Google +1 and Share
'wordbooker/wordbooker.php', // Wordbooker
'wpsso/wpsso.php', // WordPress Social Sharing Optimization
'wp-caregiver/wp-caregiver.php', // WP Caregiver
'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php',
// WP Facebook Like Send & Open Graph Meta
'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol
'wp-ogp/wp-ogp.php', // WP-OGP
'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin
'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button
'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox
);
/**
* Plugins for which we turn off our Twitter Cards Tags implementation.
*/
private $twitter_cards_conflicting_plugins = array(
// 'twitter/twitter.php', // The official one handles this on its own.
// // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php
'eewee-twitter-card/index.php', // Eewee Twitter Card
'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards
'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards
'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php',
// Pure Web Brilliant's Social Graph Twitter Cards Extension
'twitter-cards/twitter-cards.php', // Twitter Cards
'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta
'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards
);
/**
* Message to display in admin_notice
* @var string
*/
public $message = '';
/**
* Error to display in admin_notice
* @var string
*/
public $error = '';
/**
* Modules that need more privacy description.
* @var string
*/
public $privacy_checks = '';
/**
* Stats to record once the page loads
*
* @var array
*/
public $stats = array();
/**
* Jetpack_Sync object
*/
public $sync;
/**
* Verified data for JSON authorization request
*/
public $json_api_authorization_request = array();
/**
* @var string Transient key used to prevent multiple simultaneous plugin upgrades
*/
public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock';
/**
* Holds the singleton instance of this class
* @since 2.3.3
* @var Jetpack
*/
static $instance = false;
/**
* Singleton
* @static
*/
public static function init() {
if ( ! self::$instance ) {
self::$instance = new Jetpack;
self::$instance->plugin_upgrade();
}
return self::$instance;
}
/**
* Must never be called statically
*/
function plugin_upgrade() {
if ( Jetpack::is_active() ) {
list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
if ( JETPACK__VERSION != $version ) {
// Prevent multiple upgrades at once - only a single process should trigger
// an upgrade to avoid stampedes
if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) {
return;
}
// Set a short lock to prevent multiple instances of the upgrade
set_transient( self::$plugin_upgrade_lock_key, 1, 10 );
// check which active modules actually exist and remove others from active_modules list
$unfiltered_modules = Jetpack::get_active_modules();
$modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) );
if ( array_diff( $unfiltered_modules, $modules ) ) {
Jetpack::update_active_modules( $modules );
}
add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
// Upgrade to 4.3.0
if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) {
Jetpack_Options::delete_option( 'identity_crisis_whitelist' );
}
// Make sure Markdown for posts gets turned back on
if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) {
update_option( 'wpcom_publish_posts_with_markdown', true );
}
if ( did_action( 'wp_loaded' ) ) {
self::upgrade_on_load();
} else {
add_action(
'wp_loaded',
array( __CLASS__, 'upgrade_on_load' )
);
}
}
}
}
/**
* Runs upgrade routines that need to have modules loaded.
*/
static function upgrade_on_load() {
// Not attempting any upgrades if jetpack_modules_loaded did not fire.
// This can happen in case Jetpack has been just upgraded and is
// being initialized late during the page load. In this case we wait
// until the next proper admin page load with Jetpack active.
if ( ! did_action( 'jetpack_modules_loaded' ) ) {
delete_transient( self::$plugin_upgrade_lock_key );
return;
}
Jetpack::maybe_set_version_option();
if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) {
Jetpack_Widget_Conditions::migrate_post_type_rules();
}
if (
class_exists( 'Jetpack_Sitemap_Manager' )
&& version_compare( JETPACK__VERSION, '5.3', '>=' )
) {
do_action( 'jetpack_sitemaps_purge_data' );
}
delete_transient( self::$plugin_upgrade_lock_key );
}
static function activate_manage( ) {
if ( did_action( 'init' ) || current_filter() == 'init' ) {
self::activate_module( 'manage', false, false );
} else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) {
add_action( 'init', array( __CLASS__, 'activate_manage' ) );
}
}
static function update_active_modules( $modules ) {
$current_modules = Jetpack_Options::get_option( 'active_modules', array() );
$success = Jetpack_Options::update_option( 'active_modules', array_unique( $modules ) );
if ( is_array( $modules ) && is_array( $current_modules ) ) {
$new_active_modules = array_diff( $modules, $current_modules );
foreach( $new_active_modules as $module ) {
/**
* Fires when a specific module is activated.
*
* @since 1.9.0
*
* @param string $module Module slug.
* @param boolean $success whether the module was activated. @since 4.2
*/
do_action( 'jetpack_activate_module', $module, $success );
/**
* Fires when a module is activated.
* The dynamic part of the filter, $module, is the module slug.
*
* @since 1.9.0
*
* @param string $module Module slug.
*/
do_action( "jetpack_activate_module_$module", $module );
}
$new_deactive_modules = array_diff( $current_modules, $modules );
foreach( $new_deactive_modules as $module ) {
/**
* Fired after a module has been deactivated.
*
* @since 4.2.0
*
* @param string $module Module slug.
* @param boolean $success whether the module was deactivated.
*/
do_action( 'jetpack_deactivate_module', $module, $success );
/**
* Fires when a module is deactivated.
* The dynamic part of the filter, $module, is the module slug.
*
* @since 1.9.0
*
* @param string $module Module slug.
*/
do_action( "jetpack_deactivate_module_$module", $module );
}
}
return $success;
}
static function delete_active_modules() {
self::update_active_modules( array() );
}
/**
* Constructor. Initializes WordPress hooks
*/
private function __construct() {
/*
* Check for and alert any deprecated hooks
*/
add_action( 'init', array( $this, 'deprecated_hooks' ) );
/*
* Enable enhanced handling of previewing sites in Calypso
*/
if ( Jetpack::is_active() ) {
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php';
add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 );
}
/*
* Load things that should only be in Network Admin.
*
* For now blow away everything else until a more full
* understanding of what is needed at the network level is
* available
*/
if ( is_multisite() ) {
Jetpack_Network::init();
}
/**
* Prepare Gutenberg Editor functionality
*/
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
add_action( 'init', array( 'Jetpack_Gutenberg', 'load_blocks' ) ); // Registers all the Jetpack blocks .
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
add_filter( 'jetpack_set_available_blocks', array( 'Jetpack_Gutenberg', 'jetpack_set_available_blocks' ) );
add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );
// Unlink user before deleting the user from .com
add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 );
add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 );
// Alternate XML-RPC, via ?for=jetpack&jetpack=comms
if ( isset( $_GET['jetpack'] ) && 'comms' == $_GET['jetpack'] && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
if ( ! defined( 'XMLRPC_REQUEST' ) ) {
define( 'XMLRPC_REQUEST', true );
}
add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) );
add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 );
}
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
@ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed.
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
$this->xmlrpc_server = new Jetpack_XMLRPC_Server();
$this->require_jetpack_authentication();
if ( Jetpack::is_active() ) {
// Hack to preserve $HTTP_RAW_POST_DATA
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
$signed = $this->verify_xml_rpc_signature();
if ( $signed && ! is_wp_error( $signed ) ) {
// The actual API methods.
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) );
} else {
// The jetpack.authorize method should be available for unauthenticated users on a site with an
// active Jetpack connection, so that additional users can link their account.
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) );
}
} else {
// The bootstrap API methods.
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) );
$signed = $this->verify_xml_rpc_signature();
if ( $signed && ! is_wp_error( $signed ) ) {
// the jetpack Provision method is available for blog-token-signed requests
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) );
}
}
// Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on.
add_filter( 'pre_option_enable_xmlrpc', '__return_true' );
} elseif (
is_admin() &&
isset( $_POST['action'] ) && (
'jetpack_upload_file' == $_POST['action'] ||
'jetpack_update_file' == $_POST['action']
)
) {
$this->require_jetpack_authentication();
$this->add_remote_request_handlers();
} else {
if ( Jetpack::is_active() ) {
add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) );
add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) );
}
}
if ( Jetpack::is_active() ) {
Jetpack_Heartbeat::init();
if ( Jetpack::is_module_active( 'stats' ) && Jetpack::is_module_active( 'search' ) ) {
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
Jetpack_Search_Performance_Logger::init();
}
}
add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) );
add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) );
add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) );
if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) {
wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
}
add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
// Filter the dashboard meta box order to swap the new one in in place of the old one.
add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) );
// returns HTTPS support status
add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) );
// If any module option is updated before Jump Start is dismissed, hide Jump Start.
add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) );
// JITM AJAX callback function
add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) );
// Universal ajax callback for all tracking events triggered via js
add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) );
add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) );
add_action( 'wp_loaded', array( $this, 'register_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 );
/**
* These actions run checks to load additional files.
* They check for external files or plugins, so they need to run as late as possible.
*/
add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 );
add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 );
add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 );
add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 );
add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 );
add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 );
add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
// A filter to control all just in time messages
add_filter( 'jetpack_just_in_time_msgs', '__return_true', 9 );
add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9);
// If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin.
// We should make sure to only do this for front end links.
if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) {
add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 );
add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 );
//we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions
//so they point moderation links on emails to Calypso
jetpack_require_lib( 'functions.wp-notify' );
}
// Update the Jetpack plan from API on heartbeats
add_action( 'jetpack_heartbeat', array( $this, 'refresh_active_plan_from_wpcom' ) );
/**
* This is the hack to concatenate all css files into one.
* For description and reasoning see the implode_frontend_css method
*
* Super late priority so we catch all the registered styles
*/
if( !is_admin() ) {
add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first
add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles`
}
/**
* These are sync actions that we need to keep track of for jitms
*/
add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 );
// Actually push the stats on shutdown.
if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
add_action( 'shutdown', array( $this, 'push_stats' ) );
}
}
function point_edit_post_links_to_calypso( $default_url, $post_id ) {
$post = get_post( $post_id );
if ( empty( $post ) ) {
return $default_url;
}
$post_type = $post->post_type;
// Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso.
// https://en.support.wordpress.com/custom-post-types/
$allowed_post_types = array(
'post' => 'post',
'page' => 'page',
'jetpack-portfolio' => 'edit/jetpack-portfolio',
'jetpack-testimonial' => 'edit/jetpack-testimonial',
);
if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) {
return $default_url;
}
$path_prefix = $allowed_post_types[ $post_type ];
$site_slug = Jetpack::build_raw_urls( get_home_url() );
return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) );
}
function point_edit_comment_links_to_calypso( $url ) {
// Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID.
wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args );
return esc_url( sprintf( 'https://wordpress.com/comment/%s/%d',
Jetpack::build_raw_urls( get_home_url() ),
$query_args['amp;c']
) );
}
function jetpack_track_last_sync_callback( $params ) {
/**
* Filter to turn off jitm caching
*
* @since 5.4.0
*
* @param bool false Whether to cache just in time messages
*/
if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) {
return $params;
}
if ( is_array( $params ) && isset( $params[0] ) ) {
$option = $params[0];
if ( 'active_plugins' === $option ) {
// use the cache if we can, but not terribly important if it gets evicted
set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS );
}
}
return $params;
}
function jetpack_connection_banner_callback() {
check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' );
if ( isset( $_REQUEST['dismissBanner'] ) ) {
Jetpack_Options::update_option( 'dismissed_connection_banner', 1 );
wp_send_json_success();
}
wp_die();
}
/**
* Removes all XML-RPC methods that are not `jetpack.*`.
* Only used in our alternate XML-RPC endpoint, where we want to
* ensure that Core and other plugins' methods are not exposed.
*
* @param array $methods
* @return array filtered $methods
*/
function remove_non_jetpack_xmlrpc_methods( $methods ) {
$jetpack_methods = array();
foreach ( $methods as $method => $callback ) {
if ( 0 === strpos( $method, 'jetpack.' ) ) {
$jetpack_methods[ $method ] = $callback;
}
}
return $jetpack_methods;
}
/**
* Since a lot of hosts use a hammer approach to "protecting" WordPress sites,
* and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive
* security/firewall policies, we provide our own alternate XML RPC API endpoint
* which is accessible via a different URI. Most of the below is copied directly
* from /xmlrpc.php so that we're replicating it as closely as possible.
*/
function alternate_xmlrpc() {
// phpcs:disable PHPCompatibility.PHP.RemovedGlobalVariables.http_raw_post_dataDeprecatedRemoved
global $HTTP_RAW_POST_DATA;
// Some browser-embedded clients send cookies. We don't want them.
$_COOKIE = array();
// A bug in PHP < 5.2.2 makes $HTTP_RAW_POST_DATA not set by default,
// but we can do it ourself.
if ( ! isset( $HTTP_RAW_POST_DATA ) ) {
$HTTP_RAW_POST_DATA = file_get_contents( 'php://input' );
}
// fix for mozBlog and other cases where '<?xml' isn't on the very first line
if ( isset( $HTTP_RAW_POST_DATA ) ) {
$HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA );
}
// phpcs:enable
include_once( ABSPATH . 'wp-admin/includes/admin.php' );
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' );
/**
* Filters the class used for handling XML-RPC requests.
*
* @since 3.1.0
*
* @param string $class The name of the XML-RPC server class.
*/
$wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' );
$wp_xmlrpc_server = new $wp_xmlrpc_server_class;
// Fire off the request
nocache_headers();
$wp_xmlrpc_server->serve_request();
exit;
}
function jetpack_admin_ajax_tracks_callback() {
// Check for nonce
if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) {
wp_die( 'Permissions check failed.' );
}
if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) {
wp_die( 'No valid event name or type.' );
}
$tracks_data = array();
if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) {
if ( is_array( $_REQUEST['tracksEventProp'] ) ) {
$tracks_data = $_REQUEST['tracksEventProp'];
} else {
$tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] );
}
}
JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data );
wp_send_json_success();
wp_die();
}
/**
* The callback for the JITM ajax requests.
*/
function jetpack_jitm_ajax_callback() {
// Check for nonce
if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) {
wp_die( 'Module activation failed due to lack of appropriate permissions' );
}
if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) {
$module_slug = $_REQUEST['jitmModule'];
Jetpack::log( 'activate', $module_slug );
Jetpack::activate_module( $module_slug, false, false );
Jetpack::state( 'message', 'no_message' );
//A Jetpack module is being activated through a JITM, track it
$this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION );
$this->do_stats( 'server_side' );
wp_send_json_success();
}
if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) {
// get the hide_jitm options array
$jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
$module_slug = $_REQUEST['jitmModule'];
if( ! $jetpack_hide_jitm ) {
$jetpack_hide_jitm = array(
$module_slug => 'hide'
);
} else {
$jetpack_hide_jitm[$module_slug] = 'hide';
}
Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm );
//jitm is being dismissed forever, track it
$this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION );
$this->do_stats( 'server_side' );
wp_send_json_success();
}
if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) {
$module_slug = $_REQUEST['jitmModule'];
// User went to WordPress.com, track this
$this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION );
$this->do_stats( 'server_side' );
wp_send_json_success();
}
if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) {
$track = $_REQUEST['jitmModule'];
// User is viewing JITM, track it.
$this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION );
$this->do_stats( 'server_side' );
wp_send_json_success();
}
}
/**
* If there are any stats that need to be pushed, but haven't been, push them now.
*/
function push_stats() {
if ( ! empty( $this->stats ) ) {
$this->do_stats( 'server_side' );
}
}
function jetpack_custom_caps( $caps, $cap, $user_id, $args ) {
switch( $cap ) {
case 'jetpack_connect' :
case 'jetpack_reconnect' :
if ( Jetpack::is_development_mode() ) {
$caps = array( 'do_not_allow' );
break;
}
/**
* Pass through. If it's not development mode, these should match disconnect.
* Let users disconnect if it's development mode, just in case things glitch.
*/
case 'jetpack_disconnect' :
/**
* In multisite, can individual site admins manage their own connection?
*
* Ideally, this should be extracted out to a separate filter in the Jetpack_Network class.
*/
if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) {
/**
* We need to update the option name -- it's terribly unclear which
* direction the override goes.
*
* @todo: Update the option name to `sub-sites-can-manage-own-connections`
*/
$caps = array( 'do_not_allow' );
break;
}
}
$caps = array( 'manage_options' );
break;
case 'jetpack_manage_modules' :
case 'jetpack_activate_modules' :
case 'jetpack_deactivate_modules' :
$caps = array( 'manage_options' );
break;
case 'jetpack_configure_modules' :
$caps = array( 'manage_options' );
break;
case 'jetpack_manage_autoupdates' :
$caps = array(
'manage_options',
'update_plugins',
);
break;
case 'jetpack_network_admin_page':
case 'jetpack_network_settings_page':
$caps = array( 'manage_network_plugins' );
break;
case 'jetpack_network_sites_page':