Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class Jetpack { |
||
55 | public $xmlrpc_server = null; |
||
56 | |||
57 | /** |
||
58 | * @var array The handles of styles that are concatenated into jetpack.css. |
||
59 | * |
||
60 | * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js. |
||
61 | */ |
||
62 | public $concatenated_style_handles = array( |
||
63 | 'jetpack-carousel', |
||
64 | 'grunion.css', |
||
65 | 'the-neverending-homepage', |
||
66 | 'jetpack_likes', |
||
67 | 'jetpack_related-posts', |
||
68 | 'sharedaddy', |
||
69 | 'jetpack-slideshow', |
||
70 | 'presentations', |
||
71 | 'quiz', |
||
72 | 'jetpack-subscriptions', |
||
73 | 'jetpack-responsive-videos-style', |
||
74 | 'jetpack-social-menu', |
||
75 | 'tiled-gallery', |
||
76 | 'jetpack_display_posts_widget', |
||
77 | 'gravatar-profile-widget', |
||
78 | 'goodreads-widget', |
||
79 | 'jetpack_social_media_icons_widget', |
||
80 | 'jetpack-top-posts-widget', |
||
81 | 'jetpack_image_widget', |
||
82 | 'jetpack-my-community-widget', |
||
83 | 'jetpack-authors-widget', |
||
84 | 'wordads', |
||
85 | 'eu-cookie-law-style', |
||
86 | 'flickr-widget-style', |
||
87 | 'jetpack-search-widget', |
||
88 | 'jetpack-simple-payments-widget-style', |
||
89 | 'jetpack-widget-social-icons-styles', |
||
90 | 'wpcom_instagram_widget', |
||
91 | ); |
||
92 | |||
93 | /** |
||
94 | * Contains all assets that have had their URL rewritten to minified versions. |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | static $min_assets = array(); |
||
99 | |||
100 | public $plugins_to_deactivate = array( |
||
101 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
102 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
103 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
104 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
105 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
106 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
107 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
108 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
109 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
110 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
111 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
112 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
113 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
114 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ), |
||
115 | ); |
||
116 | |||
117 | /** |
||
118 | * Map of roles we care about, and their corresponding minimum capabilities. |
||
119 | * |
||
120 | * @deprecated 7.6 Use Automattic\Jetpack\Roles::$capability_translations instead. |
||
121 | * |
||
122 | * @access public |
||
123 | * @static |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | public static $capability_translations = array( |
||
128 | 'administrator' => 'manage_options', |
||
129 | 'editor' => 'edit_others_posts', |
||
130 | 'author' => 'publish_posts', |
||
131 | 'contributor' => 'edit_posts', |
||
132 | 'subscriber' => 'read', |
||
133 | ); |
||
134 | |||
135 | /** |
||
136 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
137 | * if the plugins are active. Used by filter_default_modules |
||
138 | * |
||
139 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
140 | * change `module-slug` and add this to your plugin: |
||
141 | * |
||
142 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
143 | * function my_jetpack_get_default_modules( $modules ) { |
||
144 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
145 | * } |
||
146 | * |
||
147 | * @var array |
||
148 | */ |
||
149 | private $conflicting_plugins = array( |
||
150 | 'comments' => array( |
||
151 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
152 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
153 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
154 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
155 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
156 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
157 | ), |
||
158 | 'comment-likes' => array( |
||
159 | 'Epoch' => 'epoch/plugincore.php', |
||
160 | ), |
||
161 | 'contact-form' => array( |
||
162 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
163 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
164 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
165 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
166 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
167 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
168 | ), |
||
169 | 'latex' => array( |
||
170 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
171 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
172 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
173 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
174 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
175 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
176 | ), |
||
177 | 'protect' => array( |
||
178 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
179 | 'Captcha' => 'captcha/captcha.php', |
||
180 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
181 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
182 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
183 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
184 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
185 | 'Security-protection' => 'security-protection/security-protection.php', |
||
186 | 'Login Security' => 'login-security/login-security.php', |
||
187 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
188 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
189 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
190 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
191 | ), |
||
192 | 'random-redirect' => array( |
||
193 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
194 | ), |
||
195 | 'related-posts' => array( |
||
196 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
197 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
198 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
199 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
200 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
201 | 'outbrain' => 'outbrain/outbrain.php', |
||
202 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
203 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
204 | ), |
||
205 | 'sharedaddy' => array( |
||
206 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
207 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
208 | 'ShareThis' => 'share-this/sharethis.php', |
||
209 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
210 | ), |
||
211 | 'seo-tools' => array( |
||
212 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
213 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
214 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
215 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
216 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
217 | 'Rank Math' => 'seo-by-rank-math/rank-math.php', |
||
218 | 'Slim SEO' => 'slim-seo/slim-seo.php', |
||
219 | ), |
||
220 | 'verification-tools' => array( |
||
221 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
222 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
223 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
224 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
225 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
226 | 'Rank Math' => 'seo-by-rank-math/rank-math.php', |
||
227 | 'Slim SEO' => 'slim-seo/slim-seo.php', |
||
228 | ), |
||
229 | 'widget-visibility' => array( |
||
230 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
231 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
232 | ), |
||
233 | 'sitemaps' => array( |
||
234 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
235 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
236 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
237 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
238 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
239 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
240 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
241 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
242 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
243 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
244 | 'Sitemap' => 'sitemap/sitemap.php', |
||
245 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
246 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
247 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
248 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
249 | 'Rank Math' => 'seo-by-rank-math/rank-math.php', |
||
250 | 'Slim SEO' => 'slim-seo/slim-seo.php', |
||
251 | ), |
||
252 | 'lazy-images' => array( |
||
253 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
254 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
255 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
256 | ), |
||
257 | ); |
||
258 | |||
259 | /** |
||
260 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
261 | * |
||
262 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
263 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
264 | * |
||
265 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
266 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
267 | */ |
||
268 | private $open_graph_conflicting_plugins = array( |
||
269 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
270 | // 2 Click Social Media Buttons |
||
271 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
272 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
273 | 'complete-open-graph/complete-open-graph.php', // Complete Open Graph |
||
274 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
275 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
276 | // Open Graph Meta Tags by Heateor |
||
277 | 'facebook/facebook.php', // Facebook (official plugin) |
||
278 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
279 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
280 | // Facebook Featured Image & OG Meta Tags |
||
281 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
282 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
283 | // Facebook Open Graph Meta Tags for WordPress |
||
284 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
285 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
286 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
287 | // Fedmich's Facebook Open Graph Meta |
||
288 | 'network-publisher/networkpub.php', // Network Publisher |
||
289 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
290 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
291 | // NextScripts SNAP |
||
292 | 'og-tags/og-tags.php', // OG Tags |
||
293 | 'opengraph/opengraph.php', // Open Graph |
||
294 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
295 | // Open Graph Protocol Framework |
||
296 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
297 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
298 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
299 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
300 | 'sharepress/sharepress.php', // SharePress |
||
301 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
302 | 'social-discussions/social-discussions.php', // Social Discussions |
||
303 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
304 | 'socialize/socialize.php', // Socialize |
||
305 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
306 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
307 | // Tweet, Like, Google +1 and Share |
||
308 | 'wordbooker/wordbooker.php', // Wordbooker |
||
309 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
310 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
311 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
312 | // WP Facebook Like Send & Open Graph Meta |
||
313 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
314 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
315 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
316 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button |
||
317 | 'open-graph-metabox/open-graph-metabox.php', // Open Graph Metabox |
||
318 | 'seo-by-rank-math/rank-math.php', // Rank Math. |
||
319 | 'slim-seo/slim-seo.php', // Slim SEO |
||
320 | ); |
||
321 | |||
322 | /** |
||
323 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
324 | */ |
||
325 | private $twitter_cards_conflicting_plugins = array( |
||
326 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
327 | // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
328 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
329 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
330 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
331 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
332 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
333 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
334 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
335 | 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter |
||
336 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
337 | 'seo-by-rank-math/rank-math.php', // Rank Math. |
||
338 | 'slim-seo/slim-seo.php', // Slim SEO |
||
339 | ); |
||
340 | |||
341 | /** |
||
342 | * Message to display in admin_notice |
||
343 | * |
||
344 | * @var string |
||
345 | */ |
||
346 | public $message = ''; |
||
347 | |||
348 | /** |
||
349 | * Error to display in admin_notice |
||
350 | * |
||
351 | * @var string |
||
352 | */ |
||
353 | public $error = ''; |
||
354 | |||
355 | /** |
||
356 | * Modules that need more privacy description. |
||
357 | * |
||
358 | * @var string |
||
359 | */ |
||
360 | public $privacy_checks = ''; |
||
361 | |||
362 | /** |
||
363 | * Stats to record once the page loads |
||
364 | * |
||
365 | * @var array |
||
366 | */ |
||
367 | public $stats = array(); |
||
368 | |||
369 | /** |
||
370 | * Jetpack_Sync object |
||
371 | */ |
||
372 | public $sync; |
||
373 | |||
374 | /** |
||
375 | * Verified data for JSON authorization request |
||
376 | */ |
||
377 | public $json_api_authorization_request = array(); |
||
378 | |||
379 | /** |
||
380 | * @var Automattic\Jetpack\Connection\Manager |
||
381 | */ |
||
382 | protected $connection_manager; |
||
383 | |||
384 | /** |
||
385 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
386 | */ |
||
387 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
388 | |||
389 | /** |
||
390 | * Holds an instance of Automattic\Jetpack\A8c_Mc_Stats |
||
391 | * |
||
392 | * @var Automattic\Jetpack\A8c_Mc_Stats |
||
393 | */ |
||
394 | public $a8c_mc_stats_instance; |
||
395 | |||
396 | /** |
||
397 | * Constant for login redirect key. |
||
398 | * |
||
399 | * @var string |
||
400 | * @since 8.4.0 |
||
401 | */ |
||
402 | public static $jetpack_redirect_login = 'jetpack_connect_login_redirect'; |
||
403 | |||
404 | /** |
||
405 | * Holds the singleton instance of this class |
||
406 | * |
||
407 | * @since 2.3.3 |
||
408 | * @var Jetpack |
||
409 | */ |
||
410 | static $instance = false; |
||
411 | |||
412 | /** |
||
413 | * Singleton |
||
414 | * |
||
415 | * @static |
||
416 | */ |
||
417 | public static function init() { |
||
418 | if ( ! self::$instance ) { |
||
419 | self::$instance = new Jetpack(); |
||
420 | add_action( 'plugins_loaded', array( self::$instance, 'plugin_upgrade' ) ); |
||
421 | } |
||
422 | |||
423 | return self::$instance; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Must never be called statically |
||
428 | */ |
||
429 | function plugin_upgrade() { |
||
430 | if ( self::is_connection_ready() ) { |
||
431 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
432 | if ( JETPACK__VERSION != $version ) { |
||
433 | // Prevent multiple upgrades at once - only a single process should trigger |
||
434 | // an upgrade to avoid stampedes |
||
435 | if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) { |
||
436 | return; |
||
437 | } |
||
438 | |||
439 | // Set a short lock to prevent multiple instances of the upgrade |
||
440 | set_transient( self::$plugin_upgrade_lock_key, 1, 10 ); |
||
441 | |||
442 | // check which active modules actually exist and remove others from active_modules list |
||
443 | $unfiltered_modules = self::get_active_modules(); |
||
444 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
445 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
446 | self::update_active_modules( $modules ); |
||
447 | } |
||
448 | |||
449 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
450 | |||
451 | // Upgrade to 4.3.0 |
||
452 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
453 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
454 | } |
||
455 | |||
456 | // Make sure Markdown for posts gets turned back on |
||
457 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
458 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
459 | } |
||
460 | |||
461 | /* |
||
462 | * Minileven deprecation. 8.3.0. |
||
463 | * Only delete options if not using |
||
464 | * the replacement standalone Minileven plugin. |
||
465 | */ |
||
466 | if ( |
||
467 | ! self::is_plugin_active( 'minileven-master/minileven.php' ) |
||
468 | && ! self::is_plugin_active( 'minileven/minileven.php' ) |
||
469 | ) { |
||
470 | if ( get_option( 'wp_mobile_custom_css' ) ) { |
||
471 | delete_option( 'wp_mobile_custom_css' ); |
||
472 | } |
||
473 | if ( get_option( 'wp_mobile_excerpt' ) ) { |
||
474 | delete_option( 'wp_mobile_excerpt' ); |
||
475 | } |
||
476 | if ( get_option( 'wp_mobile_featured_images' ) ) { |
||
477 | delete_option( 'wp_mobile_featured_images' ); |
||
478 | } |
||
479 | if ( get_option( 'wp_mobile_app_promos' ) ) { |
||
480 | delete_option( 'wp_mobile_app_promos' ); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | // Upgrade to 8.4.0. |
||
485 | if ( Jetpack_Options::get_option( 'ab_connect_banner_green_bar' ) ) { |
||
486 | Jetpack_Options::delete_option( 'ab_connect_banner_green_bar' ); |
||
487 | } |
||
488 | |||
489 | // Update to 8.8.x (WordPress 5.5 Compatibility). |
||
490 | if ( Jetpack_Options::get_option( 'autoupdate_plugins' ) ) { |
||
491 | $updated = update_site_option( |
||
492 | 'auto_update_plugins', |
||
493 | array_unique( |
||
494 | array_merge( |
||
495 | (array) Jetpack_Options::get_option( 'autoupdate_plugins', array() ), |
||
496 | (array) get_site_option( 'auto_update_plugins', array() ) |
||
497 | ) |
||
498 | ) |
||
499 | ); |
||
500 | |||
501 | if ( $updated ) { |
||
502 | Jetpack_Options::delete_option( 'autoupdate_plugins' ); |
||
503 | } // Should we have some type of fallback if something fails here? |
||
504 | } |
||
505 | |||
506 | if ( did_action( 'wp_loaded' ) ) { |
||
507 | self::upgrade_on_load(); |
||
508 | } else { |
||
509 | add_action( |
||
510 | 'wp_loaded', |
||
511 | array( __CLASS__, 'upgrade_on_load' ) |
||
512 | ); |
||
513 | } |
||
514 | } |
||
515 | } |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Runs upgrade routines that need to have modules loaded. |
||
520 | */ |
||
521 | static function upgrade_on_load() { |
||
522 | |||
523 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
524 | // This can happen in case Jetpack has been just upgraded and is |
||
525 | // being initialized late during the page load. In this case we wait |
||
526 | // until the next proper admin page load with Jetpack active. |
||
527 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
528 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
529 | |||
530 | return; |
||
531 | } |
||
532 | |||
533 | self::maybe_set_version_option(); |
||
534 | |||
535 | if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) { |
||
536 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
537 | } |
||
538 | |||
539 | if ( |
||
540 | class_exists( 'Jetpack_Sitemap_Manager' ) |
||
541 | && version_compare( JETPACK__VERSION, '5.3', '>=' ) |
||
542 | ) { |
||
543 | do_action( 'jetpack_sitemaps_purge_data' ); |
||
544 | } |
||
545 | |||
546 | // Delete old stats cache |
||
547 | delete_option( 'jetpack_restapi_stats_cache' ); |
||
548 | |||
549 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Saves all the currently active modules to options. |
||
554 | * Also fires Action hooks for each newly activated and deactivated module. |
||
555 | * |
||
556 | * @param $modules Array Array of active modules to be saved in options. |
||
557 | * |
||
558 | * @return $success bool true for success, false for failure. |
||
559 | */ |
||
560 | static function update_active_modules( $modules ) { |
||
561 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
562 | $active_modules = self::get_active_modules(); |
||
563 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
564 | $new_inactive_modules = array_diff( $active_modules, $modules ); |
||
565 | $new_current_modules = array_diff( array_merge( $current_modules, $new_active_modules ), $new_inactive_modules ); |
||
566 | $reindexed_modules = array_values( $new_current_modules ); |
||
567 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $reindexed_modules ) ); |
||
568 | |||
569 | foreach ( $new_active_modules as $module ) { |
||
570 | /** |
||
571 | * Fires when a specific module is activated. |
||
572 | * |
||
573 | * @since 1.9.0 |
||
574 | * |
||
575 | * @param string $module Module slug. |
||
576 | * @param boolean $success whether the module was activated. @since 4.2 |
||
577 | */ |
||
578 | do_action( 'jetpack_activate_module', $module, $success ); |
||
579 | /** |
||
580 | * Fires when a module is activated. |
||
581 | * The dynamic part of the filter, $module, is the module slug. |
||
582 | * |
||
583 | * @since 1.9.0 |
||
584 | * |
||
585 | * @param string $module Module slug. |
||
586 | */ |
||
587 | do_action( "jetpack_activate_module_$module", $module ); |
||
588 | } |
||
589 | |||
590 | foreach ( $new_inactive_modules as $module ) { |
||
591 | /** |
||
592 | * Fired after a module has been deactivated. |
||
593 | * |
||
594 | * @since 4.2.0 |
||
595 | * |
||
596 | * @param string $module Module slug. |
||
597 | * @param boolean $success whether the module was deactivated. |
||
598 | */ |
||
599 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
600 | /** |
||
601 | * Fires when a module is deactivated. |
||
602 | * The dynamic part of the filter, $module, is the module slug. |
||
603 | * |
||
604 | * @since 1.9.0 |
||
605 | * |
||
606 | * @param string $module Module slug. |
||
607 | */ |
||
608 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
609 | } |
||
610 | |||
611 | return $success; |
||
612 | } |
||
613 | |||
614 | static function delete_active_modules() { |
||
615 | self::update_active_modules( array() ); |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * Adds a hook to plugins_loaded at a priority that's currently the earliest |
||
620 | * available. |
||
621 | */ |
||
622 | public function add_configure_hook() { |
||
623 | global $wp_filter; |
||
624 | |||
625 | $current_priority = has_filter( 'plugins_loaded', array( $this, 'configure' ) ); |
||
626 | if ( false !== $current_priority ) { |
||
627 | remove_action( 'plugins_loaded', array( $this, 'configure' ), $current_priority ); |
||
628 | } |
||
629 | |||
630 | $taken_priorities = array_map( 'intval', array_keys( $wp_filter['plugins_loaded']->callbacks ) ); |
||
631 | sort( $taken_priorities ); |
||
632 | |||
633 | $first_priority = array_shift( $taken_priorities ); |
||
634 | |||
635 | if ( defined( 'PHP_INT_MAX' ) && $first_priority <= - PHP_INT_MAX ) { |
||
636 | $new_priority = - PHP_INT_MAX; |
||
637 | } else { |
||
638 | $new_priority = $first_priority - 1; |
||
639 | } |
||
640 | |||
641 | add_action( 'plugins_loaded', array( $this, 'configure' ), $new_priority ); |
||
642 | } |
||
643 | |||
644 | /** |
||
645 | * Constructor. Initializes WordPress hooks |
||
646 | */ |
||
647 | private function __construct() { |
||
648 | /* |
||
649 | * Check for and alert any deprecated hooks |
||
650 | */ |
||
651 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
652 | |||
653 | // Note how this runs at an earlier plugin_loaded hook intentionally to accomodate for other plugins. |
||
654 | add_action( 'plugin_loaded', array( $this, 'add_configure_hook' ), 90 ); |
||
655 | add_action( 'network_plugin_loaded', array( $this, 'add_configure_hook' ), 90 ); |
||
656 | add_action( 'mu_plugin_loaded', array( $this, 'add_configure_hook' ), 90 ); |
||
657 | add_action( 'plugins_loaded', array( $this, 'late_initialization' ), 90 ); |
||
658 | |||
659 | add_action( 'jetpack_verify_signature_error', array( $this, 'track_xmlrpc_error' ) ); |
||
660 | |||
661 | add_filter( |
||
662 | 'jetpack_signature_check_token', |
||
663 | array( __CLASS__, 'verify_onboarding_token' ), |
||
664 | 10, |
||
665 | 3 |
||
666 | ); |
||
667 | |||
668 | /** |
||
669 | * Prepare Gutenberg Editor functionality |
||
670 | */ |
||
671 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php'; |
||
672 | add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'init' ) ); |
||
673 | add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'load_independent_blocks' ) ); |
||
674 | add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'load_extended_blocks' ), 9 ); |
||
675 | add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) ); |
||
676 | |||
677 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
678 | |||
679 | // Unlink user before deleting the user from WP.com. |
||
680 | add_action( 'deleted_user', array( $this, 'disconnect_user' ), 10, 1 ); |
||
681 | add_action( 'remove_user_from_blog', array( $this, 'disconnect_user' ), 10, 1 ); |
||
682 | |||
683 | add_action( 'jetpack_event_log', array( 'Jetpack', 'log' ), 10, 2 ); |
||
684 | |||
685 | add_filter( 'login_url', array( $this, 'login_url' ), 10, 2 ); |
||
686 | add_action( 'login_init', array( $this, 'login_init' ) ); |
||
687 | |||
688 | // Set up the REST authentication hooks. |
||
689 | Connection_Rest_Authentication::init(); |
||
690 | |||
691 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
692 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
693 | |||
694 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ), 20 ); |
||
695 | |||
696 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
697 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
698 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
699 | |||
700 | // returns HTTPS support status |
||
701 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
702 | |||
703 | add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) ); |
||
704 | |||
705 | add_action( 'wp_ajax_jetpack_recommendations_banner', array( 'Jetpack_Recommendations_Banner', 'ajax_callback' ) ); |
||
706 | |||
707 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
708 | |||
709 | /** |
||
710 | * These actions run checks to load additional files. |
||
711 | * They check for external files or plugins, so they need to run as late as possible. |
||
712 | */ |
||
713 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
714 | add_action( 'web_stories_story_head', array( $this, 'check_open_graph' ), 1 ); |
||
715 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
716 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
717 | |||
718 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
719 | add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 ); |
||
720 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
721 | |||
722 | add_filter( 'profile_update', array( 'Jetpack', 'user_meta_cleanup' ) ); |
||
723 | |||
724 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
725 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
726 | |||
727 | require_once JETPACK__PLUGIN_DIR . 'class-jetpack-pre-connection-jitms.php'; |
||
728 | $jetpack_jitm_messages = ( new Jetpack_Pre_Connection_JITMs() ); |
||
729 | add_filter( 'jetpack_pre_connection_jitms', array( $jetpack_jitm_messages, 'add_pre_connection_jitms' ) ); |
||
730 | |||
731 | /* |
||
732 | * If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin. |
||
733 | * We should make sure to only do this for front end links. |
||
734 | */ |
||
735 | if ( self::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { |
||
736 | add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 ); |
||
737 | add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 ); |
||
738 | |||
739 | /* |
||
740 | * We'll shortcircuit wp_notify_postauthor and wp_notify_moderator pluggable functions |
||
741 | * so they point moderation links on emails to Calypso. |
||
742 | */ |
||
743 | jetpack_require_lib( 'functions.wp-notify' ); |
||
744 | add_filter( 'comment_notification_recipients', 'jetpack_notify_postauthor', 1, 2 ); |
||
745 | add_filter( 'notify_moderator', 'jetpack_notify_moderator', 1, 2 ); |
||
746 | } |
||
747 | |||
748 | add_action( |
||
749 | 'plugins_loaded', |
||
750 | function () { |
||
751 | if ( User_Agent_Info::is_mobile_app() ) { |
||
752 | add_filter( 'get_edit_post_link', '__return_empty_string' ); |
||
753 | } |
||
754 | } |
||
755 | ); |
||
756 | |||
757 | // Update the site's Jetpack plan and products from API on heartbeats. |
||
758 | add_action( 'jetpack_heartbeat', array( 'Jetpack_Plan', 'refresh_from_wpcom' ) ); |
||
759 | |||
760 | /** |
||
761 | * This is the hack to concatenate all css files into one. |
||
762 | * For description and reasoning see the implode_frontend_css method. |
||
763 | * |
||
764 | * Super late priority so we catch all the registered styles. |
||
765 | */ |
||
766 | if ( ! is_admin() ) { |
||
767 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
768 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
769 | } |
||
770 | |||
771 | // Actually push the stats on shutdown. |
||
772 | if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) { |
||
773 | add_action( 'shutdown', array( $this, 'push_stats' ) ); |
||
774 | } |
||
775 | |||
776 | // After a successful connection. |
||
777 | add_action( 'jetpack_site_registered', array( $this, 'activate_default_modules_on_site_register' ) ); |
||
778 | add_action( 'jetpack_site_registered', array( $this, 'handle_unique_registrations_stats' ) ); |
||
779 | |||
780 | // Actions for Manager::authorize(). |
||
781 | add_action( 'jetpack_authorize_starting', array( $this, 'authorize_starting' ) ); |
||
782 | add_action( 'jetpack_authorize_ending_linked', array( $this, 'authorize_ending_linked' ) ); |
||
783 | add_action( 'jetpack_authorize_ending_authorized', array( $this, 'authorize_ending_authorized' ) ); |
||
784 | |||
785 | add_action( 'jetpack_client_authorize_error', array( Jetpack_Client_Server::class, 'client_authorize_error' ) ); |
||
786 | add_filter( 'jetpack_client_authorize_already_authorized_url', array( Jetpack_Client_Server::class, 'client_authorize_already_authorized_url' ) ); |
||
787 | add_action( 'jetpack_client_authorize_processing', array( Jetpack_Client_Server::class, 'client_authorize_processing' ) ); |
||
788 | add_filter( 'jetpack_client_authorize_fallback_url', array( Jetpack_Client_Server::class, 'client_authorize_fallback_url' ) ); |
||
789 | |||
790 | // Filters for the Manager::get_token() urls and request body. |
||
791 | add_filter( 'jetpack_token_redirect_url', array( __CLASS__, 'filter_connect_redirect_url' ) ); |
||
792 | add_filter( 'jetpack_token_request_body', array( __CLASS__, 'filter_token_request_body' ) ); |
||
793 | |||
794 | // Actions for successful reconnect. |
||
795 | add_action( 'jetpack_reconnection_completed', array( $this, 'reconnection_completed' ) ); |
||
796 | |||
797 | // Actions for licensing. |
||
798 | Licensing::instance()->initialize(); |
||
799 | |||
800 | // Filters for Sync Callables. |
||
801 | add_filter( 'jetpack_sync_callable_whitelist', array( $this, 'filter_sync_callable_whitelist' ), 10, 1 ); |
||
802 | add_filter( 'jetpack_sync_multisite_callable_whitelist', array( $this, 'filter_sync_multisite_callable_whitelist' ), 10, 1 ); |
||
803 | |||
804 | // Make resources use static domain when possible. |
||
805 | add_filter( 'jetpack_static_url', array( 'Automattic\\Jetpack\\Assets', 'staticize_subdomain' ) ); |
||
806 | |||
807 | // Validate the domain names in Jetpack development versions. |
||
808 | add_action( 'jetpack_pre_register', array( get_called_class(), 'registration_check_domains' ) ); |
||
809 | } |
||
810 | |||
811 | /** |
||
812 | * Before everything else starts getting initalized, we need to initialize Jetpack using the |
||
813 | * Config object. |
||
814 | */ |
||
815 | public function configure() { |
||
816 | $config = new Config(); |
||
817 | |||
818 | foreach ( |
||
819 | array( |
||
820 | 'sync', |
||
821 | 'jitm', |
||
822 | ) |
||
823 | as $feature |
||
824 | ) { |
||
825 | $config->ensure( $feature ); |
||
826 | } |
||
827 | |||
828 | $config->ensure( |
||
829 | 'connection', |
||
830 | array( |
||
831 | 'slug' => 'jetpack', |
||
832 | 'name' => 'Jetpack', |
||
833 | ) |
||
834 | ); |
||
835 | |||
836 | if ( ! $this->connection_manager ) { |
||
837 | $this->connection_manager = new Connection_Manager( 'jetpack' ); |
||
838 | |||
839 | /** |
||
840 | * Filter to activate Jetpack Connection UI. |
||
841 | * INTERNAL USE ONLY. |
||
842 | * |
||
843 | * @since 9.5.0 |
||
844 | * |
||
845 | * @param bool false Whether to activate the Connection UI. |
||
846 | */ |
||
847 | if ( apply_filters( 'jetpack_connection_ui_active', false ) ) { |
||
848 | Automattic\Jetpack\ConnectionUI\Admin::init(); |
||
849 | } |
||
850 | } |
||
851 | |||
852 | /* |
||
853 | * Load things that should only be in Network Admin. |
||
854 | * |
||
855 | * For now blow away everything else until a more full |
||
856 | * understanding of what is needed at the network level is |
||
857 | * available |
||
858 | */ |
||
859 | if ( is_multisite() ) { |
||
860 | $network = Jetpack_Network::init(); |
||
861 | $network->set_connection( $this->connection_manager ); |
||
862 | } |
||
863 | |||
864 | if ( self::is_connection_ready() ) { |
||
865 | add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) ); |
||
866 | |||
867 | Jetpack_Heartbeat::init(); |
||
868 | if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) { |
||
869 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php'; |
||
870 | Jetpack_Search_Performance_Logger::init(); |
||
871 | } |
||
872 | } |
||
873 | |||
874 | // Initialize remote file upload request handlers. |
||
875 | $this->add_remote_request_handlers(); |
||
876 | |||
877 | /* |
||
878 | * Enable enhanced handling of previewing sites in Calypso |
||
879 | */ |
||
880 | if ( self::is_connection_ready() ) { |
||
881 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php'; |
||
882 | add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 ); |
||
883 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php'; |
||
884 | add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 ); |
||
885 | } |
||
886 | |||
887 | if ( ( new Tracking( $this->connection_manager ) )->should_enable_tracking( new Terms_Of_Service(), new Status() ) ) { |
||
888 | add_action( 'init', array( new Plugin_Tracking(), 'init' ) ); |
||
889 | } else { |
||
890 | /** |
||
891 | * Initialize tracking right after the user agrees to the terms of service. |
||
892 | */ |
||
893 | add_action( 'jetpack_agreed_to_terms_of_service', array( new Plugin_Tracking(), 'init' ) ); |
||
894 | } |
||
895 | } |
||
896 | |||
897 | /** |
||
898 | * Runs on plugins_loaded. Use this to add code that needs to be executed later than other |
||
899 | * initialization code. |
||
900 | * |
||
901 | * @action plugins_loaded |
||
902 | */ |
||
903 | public function late_initialization() { |
||
920 | |||
921 | /** |
||
922 | * Sets up the XMLRPC request handlers. |
||
923 | * |
||
924 | * @deprecated since 7.7.0 |
||
925 | * @see Automattic\Jetpack\Connection\Manager::setup_xmlrpc_handlers() |
||
926 | * |
||
927 | * @param array $request_params Incoming request parameters. |
||
928 | * @param Boolean $is_active Whether the connection is currently active. |
||
929 | * @param Boolean $is_signed Whether the signature check has been successful. |
||
930 | * @param Jetpack_XMLRPC_Server $xmlrpc_server (optional) An instance of the server to use instead of instantiating a new one. |
||
931 | */ |
||
932 | View Code Duplication | public function setup_xmlrpc_handlers( |
|
951 | |||
952 | /** |
||
953 | * Initialize REST API registration connector. |
||
954 | * |
||
955 | * @deprecated since 7.7.0 |
||
956 | * @see Automattic\Jetpack\Connection\Manager::initialize_rest_api_registration_connector() |
||
957 | */ |
||
958 | View Code Duplication | public function initialize_rest_api_registration_connector() { |
|
967 | |||
968 | /** |
||
969 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
970 | * |
||
971 | * @param $domains |
||
972 | */ |
||
973 | function add_wpcom_to_allowed_redirect_hosts( $domains ) { |
||
976 | |||
977 | /** |
||
978 | * Return $domains, with 'wordpress.com' appended. |
||
979 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
980 | * |
||
981 | * @param $domains |
||
982 | * @return array |
||
983 | */ |
||
984 | function allow_wpcom_domain( $domains ) { |
||
991 | |||
992 | function point_edit_post_links_to_calypso( $default_url, $post_id ) { |
||
1021 | |||
1022 | function point_edit_comment_links_to_calypso( $url ) { |
||
1034 | |||
1035 | /** |
||
1036 | * Extend Sync callables with Jetpack Plugin functions. |
||
1037 | * |
||
1038 | * @param array $callables list of callables. |
||
1039 | * |
||
1040 | * @return array list of callables. |
||
1041 | */ |
||
1042 | public function filter_sync_callable_whitelist( $callables ) { |
||
1067 | |||
1068 | /** |
||
1069 | * Extend Sync multisite callables with Jetpack Plugin functions. |
||
1070 | * |
||
1071 | * @param array $callables list of callables. |
||
1072 | * |
||
1073 | * @return array list of callables. |
||
1074 | */ |
||
1075 | public function filter_sync_multisite_callable_whitelist( $callables ) { |
||
1090 | |||
1091 | /** |
||
1092 | * Deprecated |
||
1093 | * Please use Automattic\Jetpack\JITMS\JITM::jetpack_track_last_sync_callback instead. |
||
1094 | * |
||
1095 | * @param array $params The action parameters. |
||
1096 | * |
||
1097 | * @deprecated since 9.8. |
||
1098 | */ |
||
1099 | function jetpack_track_last_sync_callback( $params ) { |
||
1100 | _deprecated_function( __METHOD__, 'jetpack-9.8', '\Automattic\Jetpack\JITMS\JITM->jetpack_track_last_sync_callback' ); |
||
1101 | return Automattic\Jetpack\JITMS\JITM::get_instance()->jetpack_track_last_sync_callback( $params ); |
||
1102 | } |
||
1103 | |||
1104 | function jetpack_connection_banner_callback() { |
||
1105 | check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' ); |
||
1106 | |||
1107 | // Disable the banner dismiss functionality if the pre-connection prompt helpers filter is set. |
||
1108 | if ( |
||
1109 | isset( $_REQUEST['dismissBanner'] ) && |
||
1110 | ! Jetpack_Connection_Banner::force_display() |
||
1111 | ) { |
||
1112 | Jetpack_Options::update_option( 'dismissed_connection_banner', 1 ); |
||
1113 | wp_send_json_success(); |
||
1114 | } |
||
1115 | |||
1116 | wp_die(); |
||
1117 | } |
||
1118 | |||
1119 | /** |
||
1120 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
1121 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
1122 | * ensure that Core and other plugins' methods are not exposed. |
||
1123 | * |
||
1124 | * @deprecated since 7.7.0 |
||
1125 | * @see Automattic\Jetpack\Connection\Manager::remove_non_jetpack_xmlrpc_methods() |
||
1126 | * |
||
1127 | * @param array $methods A list of registered WordPress XMLRPC methods. |
||
1128 | * @return array Filtered $methods |
||
1129 | */ |
||
1130 | View Code Duplication | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
|
1131 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::remove_non_jetpack_xmlrpc_methods' ); |
||
1132 | |||
1133 | if ( ! $this->connection_manager ) { |
||
1134 | $this->connection_manager = new Connection_Manager(); |
||
1135 | } |
||
1136 | |||
1137 | return $this->connection_manager->remove_non_jetpack_xmlrpc_methods( $methods ); |
||
1138 | } |
||
1139 | |||
1140 | /** |
||
1141 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
1142 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
1143 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
1144 | * which is accessible via a different URI. Most of the below is copied directly |
||
1145 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
1146 | * |
||
1147 | * @deprecated since 7.7.0 |
||
1148 | * @see Automattic\Jetpack\Connection\Manager::alternate_xmlrpc() |
||
1149 | */ |
||
1150 | View Code Duplication | public function alternate_xmlrpc() { |
|
1151 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::alternate_xmlrpc' ); |
||
1152 | |||
1153 | if ( ! $this->connection_manager ) { |
||
1154 | $this->connection_manager = new Connection_Manager(); |
||
1155 | } |
||
1156 | |||
1157 | $this->connection_manager->alternate_xmlrpc(); |
||
1158 | } |
||
1159 | |||
1160 | /** |
||
1161 | * The callback for the JITM ajax requests. |
||
1162 | * |
||
1163 | * @deprecated since 7.9.0 |
||
1164 | */ |
||
1165 | function jetpack_jitm_ajax_callback() { |
||
1166 | _deprecated_function( __METHOD__, 'jetpack-7.9' ); |
||
1167 | } |
||
1168 | |||
1169 | /** |
||
1170 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
1171 | */ |
||
1172 | function push_stats() { |
||
1173 | if ( ! empty( $this->stats ) ) { |
||
1174 | $this->do_stats( 'server_side' ); |
||
1175 | } |
||
1176 | } |
||
1177 | |||
1178 | /** |
||
1179 | * Sets the Jetpack custom capabilities. |
||
1180 | * |
||
1181 | * @param string[] $caps Array of the user's capabilities. |
||
1182 | * @param string $cap Capability name. |
||
1183 | * @param int $user_id The user ID. |
||
1184 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
1185 | */ |
||
1186 | public function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
1187 | switch ( $cap ) { |
||
1188 | case 'jetpack_manage_modules': |
||
1189 | case 'jetpack_activate_modules': |
||
1190 | case 'jetpack_deactivate_modules': |
||
1191 | $caps = array( 'manage_options' ); |
||
1192 | break; |
||
1193 | case 'jetpack_configure_modules': |
||
1194 | $caps = array( 'manage_options' ); |
||
1195 | break; |
||
1196 | case 'jetpack_manage_autoupdates': |
||
1197 | $caps = array( |
||
1198 | 'manage_options', |
||
1199 | 'update_plugins', |
||
1200 | ); |
||
1201 | break; |
||
1202 | case 'jetpack_network_admin_page': |
||
1203 | case 'jetpack_network_settings_page': |
||
1204 | $caps = array( 'manage_network_plugins' ); |
||
1205 | break; |
||
1206 | case 'jetpack_network_sites_page': |
||
1207 | $caps = array( 'manage_sites' ); |
||
1208 | break; |
||
1209 | View Code Duplication | case 'jetpack_admin_page': |
|
1210 | $is_offline_mode = ( new Status() )->is_offline_mode(); |
||
1211 | if ( $is_offline_mode ) { |
||
1212 | $caps = array( 'manage_options' ); |
||
1213 | break; |
||
1214 | } else { |
||
1215 | $caps = array( 'read' ); |
||
1216 | } |
||
1217 | break; |
||
1218 | } |
||
1219 | return $caps; |
||
1220 | } |
||
1221 | |||
1222 | /** |
||
1223 | * Require a Jetpack authentication. |
||
1224 | * |
||
1225 | * @deprecated since 7.7.0 |
||
1226 | * @see Automattic\Jetpack\Connection\Manager::require_jetpack_authentication() |
||
1227 | */ |
||
1228 | View Code Duplication | public function require_jetpack_authentication() { |
|
1229 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::require_jetpack_authentication' ); |
||
1230 | |||
1231 | if ( ! $this->connection_manager ) { |
||
1232 | $this->connection_manager = new Connection_Manager(); |
||
1233 | } |
||
1234 | |||
1235 | $this->connection_manager->require_jetpack_authentication(); |
||
1236 | } |
||
1237 | |||
1238 | /** |
||
1239 | * Register assets for use in various modules and the Jetpack admin page. |
||
1240 | * |
||
1241 | * @uses wp_script_is, wp_register_script, plugins_url |
||
1242 | * @action wp_loaded |
||
1243 | * @return null |
||
1244 | */ |
||
1245 | public function register_assets() { |
||
1246 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
1247 | wp_register_script( |
||
1248 | 'jetpack-gallery-settings', |
||
1249 | Assets::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ), |
||
1250 | array( 'media-views' ), |
||
1251 | '20121225' |
||
1252 | ); |
||
1253 | } |
||
1254 | |||
1255 | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
||
1256 | wp_register_script( |
||
1257 | 'jetpack-twitter-timeline', |
||
1258 | Assets::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ), |
||
1259 | array( 'jquery' ), |
||
1260 | '4.0.0', |
||
1261 | true |
||
1262 | ); |
||
1263 | } |
||
1264 | |||
1265 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
1266 | wp_register_script( |
||
1267 | 'jetpack-facebook-embed', |
||
1268 | Assets::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ), |
||
1269 | array(), |
||
1270 | null, |
||
1271 | true |
||
1272 | ); |
||
1273 | |||
1274 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
1275 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
1276 | if ( ! is_numeric( $fb_app_id ) ) { |
||
1277 | $fb_app_id = ''; |
||
1278 | } |
||
1279 | wp_localize_script( |
||
1280 | 'jetpack-facebook-embed', |
||
1281 | 'jpfbembed', |
||
1282 | array( |
||
1283 | 'appid' => $fb_app_id, |
||
1284 | 'locale' => $this->get_locale(), |
||
1285 | ) |
||
1286 | ); |
||
1287 | } |
||
1288 | |||
1289 | /** |
||
1290 | * As jetpack_register_genericons is by default fired off a hook, |
||
1291 | * the hook may have already fired by this point. |
||
1292 | * So, let's just trigger it manually. |
||
1293 | */ |
||
1294 | require_once JETPACK__PLUGIN_DIR . '_inc/genericons.php'; |
||
1295 | jetpack_register_genericons(); |
||
1296 | |||
1297 | /** |
||
1298 | * Register the social logos |
||
1299 | */ |
||
1300 | require_once JETPACK__PLUGIN_DIR . '_inc/social-logos.php'; |
||
1301 | jetpack_register_social_logos(); |
||
1302 | |||
1303 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) { |
|
1304 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
1305 | } |
||
1306 | } |
||
1307 | |||
1308 | /** |
||
1309 | * Guess locale from language code. |
||
1310 | * |
||
1311 | * @param string $lang Language code. |
||
1312 | * @return string|bool |
||
1313 | */ |
||
1314 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
1315 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
1316 | return 'en_US'; |
||
1317 | } |
||
1318 | |||
1319 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
1320 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
1321 | return false; |
||
1322 | } |
||
1323 | |||
1324 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
1325 | } |
||
1326 | |||
1327 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
1328 | // WP.com: get_locale() returns 'it' |
||
1329 | $locale = GP_Locales::by_slug( $lang ); |
||
1330 | } else { |
||
1331 | // Jetpack: get_locale() returns 'it_IT'; |
||
1332 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
1333 | } |
||
1334 | |||
1335 | if ( ! $locale ) { |
||
1336 | return false; |
||
1337 | } |
||
1338 | |||
1339 | if ( empty( $locale->facebook_locale ) ) { |
||
1340 | if ( empty( $locale->wp_locale ) ) { |
||
1341 | return false; |
||
1342 | } else { |
||
1343 | // Facebook SDK is smart enough to fall back to en_US if a |
||
1344 | // locale isn't supported. Since supported Facebook locales |
||
1345 | // can fall out of sync, we'll attempt to use the known |
||
1346 | // wp_locale value and rely on said fallback. |
||
1347 | return $locale->wp_locale; |
||
1348 | } |
||
1349 | } |
||
1350 | |||
1351 | return $locale->facebook_locale; |
||
1352 | } |
||
1353 | |||
1354 | /** |
||
1355 | * Get the locale. |
||
1356 | * |
||
1357 | * @return string|bool |
||
1358 | */ |
||
1359 | function get_locale() { |
||
1360 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
1361 | |||
1362 | if ( ! $locale ) { |
||
1363 | $locale = 'en_US'; |
||
1364 | } |
||
1365 | |||
1366 | return $locale; |
||
1367 | } |
||
1368 | |||
1369 | /** |
||
1370 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1371 | * |
||
1372 | * @param bool $option |
||
1373 | * @return string |
||
1374 | */ |
||
1375 | public function jetpack_main_network_site_option( $option ) { |
||
1376 | return network_site_url(); |
||
1377 | } |
||
1378 | /** |
||
1379 | * Network Name. |
||
1380 | */ |
||
1381 | static function network_name( $option = null ) { |
||
1382 | global $current_site; |
||
1383 | return $current_site->site_name; |
||
1384 | } |
||
1385 | /** |
||
1386 | * Does the network allow new user and site registrations. |
||
1387 | * |
||
1388 | * @return string |
||
1389 | */ |
||
1390 | static function network_allow_new_registrations( $option = null ) { |
||
1391 | return ( in_array( get_site_option( 'registration' ), array( 'none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration' ) : 'none' ); |
||
1392 | } |
||
1393 | /** |
||
1394 | * Does the network allow admins to add new users. |
||
1395 | * |
||
1396 | * @return boolian |
||
1397 | */ |
||
1398 | static function network_add_new_users( $option = null ) { |
||
1399 | return (bool) get_site_option( 'add_new_users' ); |
||
1400 | } |
||
1401 | /** |
||
1402 | * File upload psace left per site in MB. |
||
1403 | * -1 means NO LIMIT. |
||
1404 | * |
||
1405 | * @return number |
||
1406 | */ |
||
1407 | static function network_site_upload_space( $option = null ) { |
||
1408 | // value in MB |
||
1409 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
1410 | } |
||
1411 | |||
1412 | /** |
||
1413 | * Network allowed file types. |
||
1414 | * |
||
1415 | * @return string |
||
1416 | */ |
||
1417 | static function network_upload_file_types( $option = null ) { |
||
1418 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
1419 | } |
||
1420 | |||
1421 | /** |
||
1422 | * Maximum file upload size set by the network. |
||
1423 | * |
||
1424 | * @return number |
||
1425 | */ |
||
1426 | static function network_max_upload_file_size( $option = null ) { |
||
1427 | // value in KB |
||
1428 | return get_site_option( 'fileupload_maxk', 300 ); |
||
1429 | } |
||
1430 | |||
1431 | /** |
||
1432 | * Lets us know if a site allows admins to manage the network. |
||
1433 | * |
||
1434 | * @return array |
||
1435 | */ |
||
1436 | static function network_enable_administration_menus( $option = null ) { |
||
1437 | return get_site_option( 'menu_items' ); |
||
1438 | } |
||
1439 | |||
1440 | /** |
||
1441 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1442 | * jetpack_other_linked_admins transient. |
||
1443 | * |
||
1444 | * @since 4.3.2 |
||
1445 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1446 | * |
||
1447 | * @param int $user_id The user ID whose role changed. |
||
1448 | * @param string $role The new role. |
||
1449 | * @param array $old_roles An array of the user's previous roles. |
||
1450 | */ |
||
1451 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1452 | if ( 'administrator' == $role |
||
1453 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
1454 | || is_null( $old_roles ) |
||
1455 | ) { |
||
1456 | delete_transient( 'jetpack_other_linked_admins' ); |
||
1457 | } |
||
1458 | } |
||
1459 | |||
1460 | /** |
||
1461 | * Checks to see if there are any other users available to become primary |
||
1462 | * Users must both: |
||
1463 | * - Be linked to wpcom |
||
1464 | * - Be an admin |
||
1465 | * |
||
1466 | * @return mixed False if no other users are linked, Int if there are. |
||
1467 | */ |
||
1468 | static function get_other_linked_admins() { |
||
1469 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
1470 | |||
1471 | if ( false === $other_linked_users ) { |
||
1472 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
1473 | if ( count( $admins ) > 1 ) { |
||
1474 | $available = array(); |
||
1475 | foreach ( $admins as $admin ) { |
||
1476 | if ( self::connection()->is_user_connected( $admin->ID ) ) { |
||
1477 | $available[] = $admin->ID; |
||
1478 | } |
||
1479 | } |
||
1480 | |||
1481 | $count_connected_admins = count( $available ); |
||
1482 | if ( count( $available ) > 1 ) { |
||
1483 | $other_linked_users = $count_connected_admins; |
||
1484 | } else { |
||
1485 | $other_linked_users = 0; |
||
1486 | } |
||
1487 | } else { |
||
1488 | $other_linked_users = 0; |
||
1489 | } |
||
1490 | |||
1491 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
1492 | } |
||
1493 | |||
1494 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
1495 | } |
||
1496 | |||
1497 | /** |
||
1498 | * Return whether we are dealing with a multi network setup or not. |
||
1499 | * The reason we are type casting this is because we want to avoid the situation where |
||
1500 | * the result is false since when is_main_network_option return false it cases |
||
1501 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1502 | * database which could be set to anything as opposed to what this function returns. |
||
1503 | * |
||
1504 | * @param bool $option |
||
1505 | * |
||
1506 | * @return boolean |
||
1507 | */ |
||
1508 | public function is_main_network_option( $option ) { |
||
1509 | // return '1' or '' |
||
1510 | return (string) (bool) self::is_multi_network(); |
||
1511 | } |
||
1512 | |||
1513 | /** |
||
1514 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1515 | * |
||
1516 | * @param string $option |
||
1517 | * @return boolean |
||
1518 | */ |
||
1519 | public function is_multisite( $option ) { |
||
1520 | return (string) (bool) is_multisite(); |
||
1521 | } |
||
1522 | |||
1523 | /** |
||
1524 | * Implemented since there is no core is multi network function |
||
1525 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1526 | * |
||
1527 | * @since 3.3 |
||
1528 | * @return boolean |
||
1529 | */ |
||
1530 | View Code Duplication | public static function is_multi_network() { |
|
1531 | global $wpdb; |
||
1532 | |||
1533 | // if we don't have a multi site setup no need to do any more |
||
1534 | if ( ! is_multisite() ) { |
||
1535 | return false; |
||
1536 | } |
||
1537 | |||
1538 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
1539 | if ( $num_sites > 1 ) { |
||
1540 | return true; |
||
1541 | } else { |
||
1542 | return false; |
||
1543 | } |
||
1544 | } |
||
1545 | |||
1546 | /** |
||
1547 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1548 | * |
||
1549 | * @return null |
||
1550 | */ |
||
1551 | function update_jetpack_main_network_site_option() { |
||
1552 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1553 | } |
||
1554 | /** |
||
1555 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1556 | */ |
||
1557 | function update_jetpack_network_settings() { |
||
1558 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1559 | // Only sync this info for the main network site. |
||
1560 | } |
||
1561 | |||
1562 | /** |
||
1563 | * Get back if the current site is single user site. |
||
1564 | * |
||
1565 | * @return bool |
||
1566 | */ |
||
1567 | View Code Duplication | public static function is_single_user_site() { |
|
1568 | global $wpdb; |
||
1569 | |||
1570 | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
||
1571 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
1572 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
1573 | } |
||
1574 | return 1 === (int) $some_users; |
||
1575 | } |
||
1576 | |||
1577 | /** |
||
1578 | * Returns true if the site has file write access false otherwise. |
||
1579 | * |
||
1580 | * @return string ( '1' | '0' ) |
||
1581 | **/ |
||
1582 | public static function file_system_write_access() { |
||
1583 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
1584 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
1585 | } |
||
1586 | |||
1587 | require_once ABSPATH . 'wp-admin/includes/template.php'; |
||
1588 | |||
1589 | $filesystem_method = get_filesystem_method(); |
||
1590 | if ( $filesystem_method === 'direct' ) { |
||
1591 | return 1; |
||
1592 | } |
||
1593 | |||
1594 | ob_start(); |
||
1595 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
1596 | ob_end_clean(); |
||
1597 | if ( $filesystem_credentials_are_stored ) { |
||
1598 | return 1; |
||
1599 | } |
||
1600 | return 0; |
||
1601 | } |
||
1602 | |||
1603 | /** |
||
1604 | * Finds out if a site is using a version control system. |
||
1605 | * |
||
1606 | * @return string ( '1' | '0' ) |
||
1607 | **/ |
||
1608 | public static function is_version_controlled() { |
||
1609 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Functions::is_version_controlled' ); |
||
1610 | return (string) (int) Functions::is_version_controlled(); |
||
1611 | } |
||
1612 | |||
1613 | /** |
||
1614 | * Determines whether the current theme supports featured images or not. |
||
1615 | * |
||
1616 | * @return string ( '1' | '0' ) |
||
1617 | */ |
||
1618 | public static function featured_images_enabled() { |
||
1619 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1620 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
1621 | } |
||
1622 | |||
1623 | /** |
||
1624 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1625 | * |
||
1626 | * @deprecated 4.7 use get_avatar_url instead. |
||
1627 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1628 | * @param int $size Size of the avatar image |
||
1629 | * @param string $default URL to a default image to use if no avatar is available |
||
1630 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1631 | * |
||
1632 | * @return array |
||
1633 | */ |
||
1634 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1635 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
1636 | return get_avatar_url( |
||
1637 | $id_or_email, |
||
1638 | array( |
||
1639 | 'size' => $size, |
||
1640 | 'default' => $default, |
||
1641 | 'force_default' => $force_display, |
||
1642 | ) |
||
1643 | ); |
||
1644 | } |
||
1645 | // phpcs:disable WordPress.WP.CapitalPDangit.Misspelled |
||
1646 | /** |
||
1647 | * jetpack_updates is saved in the following schema: |
||
1648 | * |
||
1649 | * array ( |
||
1650 | * 'plugins' => (int) Number of plugin updates available. |
||
1651 | * 'themes' => (int) Number of theme updates available. |
||
1652 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1653 | * 'translations' => (int) Number of translation updates available. |
||
1654 | * 'total' => (int) Total of all available updates. |
||
1655 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1656 | * ) |
||
1657 | * |
||
1658 | * @return array |
||
1659 | */ |
||
1660 | public static function get_updates() { |
||
1661 | $update_data = wp_get_update_data(); |
||
1662 | |||
1663 | // Stores the individual update counts as well as the total count. |
||
1664 | if ( isset( $update_data['counts'] ) ) { |
||
1665 | $updates = $update_data['counts']; |
||
1666 | } |
||
1667 | |||
1668 | // If we need to update WordPress core, let's find the latest version number. |
||
1669 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
1670 | $cur = get_preferred_from_update_core(); |
||
1671 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
1672 | $updates['wp_update_version'] = $cur->current; |
||
1673 | } |
||
1674 | } |
||
1675 | return isset( $updates ) ? $updates : array(); |
||
1676 | } |
||
1677 | // phpcs:enable |
||
1678 | |||
1679 | public static function get_update_details() { |
||
1680 | $update_details = array( |
||
1681 | 'update_core' => get_site_transient( 'update_core' ), |
||
1682 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
1683 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
1684 | ); |
||
1685 | return $update_details; |
||
1686 | } |
||
1687 | |||
1688 | public static function refresh_update_data() { |
||
1689 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1690 | |||
1691 | } |
||
1692 | |||
1693 | public static function refresh_theme_data() { |
||
1694 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1695 | } |
||
1696 | |||
1697 | /** |
||
1698 | * Is Jetpack active? |
||
1699 | * The method only checks if there's an existing token for the master user. It doesn't validate the token. |
||
1700 | * |
||
1701 | * This method is deprecated since 9.6.0. Please use one of the methods provided by the Manager class in the Connection package, |
||
1702 | * or Jetpack::is_connection_ready if you want to know when the Jetpack plugin starts considering the connection ready to be used. |
||
1703 | * |
||
1704 | * Since this method has a wide spread use, we decided not to throw any deprecation warnings for now. |
||
1705 | * |
||
1706 | * @deprecated 9.6.0 |
||
1707 | * |
||
1708 | * @return bool |
||
1709 | */ |
||
1710 | public static function is_active() { |
||
1711 | return self::connection()->is_active(); |
||
1712 | } |
||
1713 | |||
1714 | /** |
||
1715 | * Returns true if the current site is connected to WordPress.com and has the minimum requirements to enable Jetpack UI |
||
1716 | * |
||
1717 | * This method was introduced just before the release of the possibility to use Jetpack without a user connection, while |
||
1718 | * it was available only when no_user_testing_mode was enabled. In the near future, this will return is_connected for all |
||
1719 | * users and this option will be available by default for everybody. |
||
1720 | * |
||
1721 | * @since 9.6.0 |
||
1722 | * @since 9.7.0 returns is_connected in all cases and adds filter to the returned value |
||
1723 | * |
||
1724 | * @return bool is the site connection ready to be used? |
||
1725 | */ |
||
1726 | public static function is_connection_ready() { |
||
1727 | /** |
||
1728 | * Allows filtering whether the connection is ready to be used. If true, this will enable the Jetpack UI and modules |
||
1729 | * |
||
1730 | * Modules will be enabled depending on the connection status and if the module requires a connection or user connection. |
||
1731 | * |
||
1732 | * @since 9.7.0 |
||
1733 | * |
||
1734 | * @param bool $is_connection_ready Is the connection ready? |
||
1735 | * @param Automattic\Jetpack\Connection\Manager $connection_manager Instance of the Manager class, can be used to check the connection status. |
||
1736 | */ |
||
1737 | return apply_filters( 'jetpack_is_connection_ready', self::connection()->is_connected(), self::connection() ); |
||
1738 | } |
||
1739 | |||
1740 | /** |
||
1741 | * Make an API call to WordPress.com for plan status |
||
1742 | * |
||
1743 | * @deprecated 7.2.0 Use Jetpack_Plan::refresh_from_wpcom. |
||
1744 | * |
||
1745 | * @return bool True if plan is updated, false if no update |
||
1746 | */ |
||
1747 | public static function refresh_active_plan_from_wpcom() { |
||
1748 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::refresh_from_wpcom' ); |
||
1749 | return Jetpack_Plan::refresh_from_wpcom(); |
||
1750 | } |
||
1751 | |||
1752 | /** |
||
1753 | * Get the plan that this Jetpack site is currently using |
||
1754 | * |
||
1755 | * @deprecated 7.2.0 Use Jetpack_Plan::get. |
||
1756 | * @return array Active Jetpack plan details. |
||
1757 | */ |
||
1758 | public static function get_active_plan() { |
||
1759 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::get' ); |
||
1760 | return Jetpack_Plan::get(); |
||
1761 | } |
||
1762 | |||
1763 | /** |
||
1764 | * Determine whether the active plan supports a particular feature |
||
1765 | * |
||
1766 | * @deprecated 7.2.0 Use Jetpack_Plan::supports. |
||
1767 | * @return bool True if plan supports feature, false if not. |
||
1768 | */ |
||
1769 | public static function active_plan_supports( $feature ) { |
||
1770 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::supports' ); |
||
1771 | return Jetpack_Plan::supports( $feature ); |
||
1772 | } |
||
1773 | |||
1774 | /** |
||
1775 | * Deprecated: Is Jetpack in development (offline) mode? |
||
1776 | * |
||
1777 | * This static method is being left here intentionally without the use of _deprecated_function(), as other plugins |
||
1778 | * and themes still use it, and we do not want to flood them with notices. |
||
1779 | * |
||
1780 | * Please use Automattic\Jetpack\Status()->is_offline_mode() instead. |
||
1781 | * |
||
1782 | * @deprecated since 8.0. |
||
1783 | */ |
||
1784 | public static function is_development_mode() { |
||
1785 | _deprecated_function( __METHOD__, 'jetpack-8.0', '\Automattic\Jetpack\Status->is_offline_mode' ); |
||
1786 | return ( new Status() )->is_offline_mode(); |
||
1787 | } |
||
1788 | |||
1789 | /** |
||
1790 | * Whether the site is currently onboarding or not. |
||
1791 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
1792 | * |
||
1793 | * @since 5.8 |
||
1794 | * |
||
1795 | * @access public |
||
1796 | * @static |
||
1797 | * |
||
1798 | * @return bool True if the site is currently onboarding, false otherwise |
||
1799 | */ |
||
1800 | public static function is_onboarding() { |
||
1801 | return Jetpack_Options::get_option( 'onboarding' ) !== false; |
||
1802 | } |
||
1803 | |||
1804 | /** |
||
1805 | * Determines reason for Jetpack offline mode. |
||
1806 | */ |
||
1807 | public static function development_mode_trigger_text() { |
||
1808 | $status = new Status(); |
||
1809 | |||
1810 | if ( ! $status->is_offline_mode() ) { |
||
1811 | return __( 'Jetpack is not in Offline Mode.', 'jetpack' ); |
||
1812 | } |
||
1813 | |||
1814 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
1815 | $notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' ); |
||
1816 | } elseif ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) { |
||
1817 | $notice = __( 'The WP_LOCAL_DEV constant is defined in wp-config.php or elsewhere.', 'jetpack' ); |
||
1818 | } elseif ( $status->is_local_site() ) { |
||
1819 | $notice = __( 'The site URL is a known local development environment URL (e.g. http://localhost).', 'jetpack' ); |
||
1820 | /** This filter is documented in packages/status/src/class-status.php */ |
||
1821 | } elseif ( has_filter( 'jetpack_development_mode' ) && apply_filters( 'jetpack_development_mode', false ) ) { // This is a deprecated filter name. |
||
1822 | $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' ); |
||
1823 | } else { |
||
1824 | $notice = __( 'The jetpack_offline_mode filter is set to true.', 'jetpack' ); |
||
1825 | } |
||
1826 | |||
1827 | return $notice; |
||
1828 | |||
1829 | } |
||
1830 | /** |
||
1831 | * Get Jetpack offline mode notice text and notice class. |
||
1832 | * |
||
1833 | * Mirrors the checks made in Automattic\Jetpack\Status->is_offline_mode |
||
1834 | */ |
||
1835 | public static function show_development_mode_notice() { |
||
1836 | View Code Duplication | if ( ( new Status() )->is_offline_mode() ) { |
|
1837 | $notice = sprintf( |
||
1838 | /* translators: %s is a URL */ |
||
1839 | __( 'In <a href="%s" target="_blank">Offline Mode</a>:', 'jetpack' ), |
||
1840 | Redirect::get_url( 'jetpack-support-development-mode' ) |
||
1841 | ); |
||
1842 | |||
1843 | $notice .= ' ' . self::development_mode_trigger_text(); |
||
1844 | |||
1845 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1846 | } |
||
1847 | |||
1848 | // Throw up a notice if using a development version and as for feedback. |
||
1849 | if ( self::is_development_version() ) { |
||
1850 | /* translators: %s is a URL */ |
||
1851 | $notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), Redirect::get_url( 'jetpack-contact-support-beta-group' ) ); |
||
1852 | |||
1853 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1854 | } |
||
1855 | // Throw up a notice if using staging mode |
||
1856 | View Code Duplication | if ( ( new Status() )->is_staging_site() ) { |
|
1857 | /* translators: %s is a URL */ |
||
1858 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), Redirect::get_url( 'jetpack-support-staging-sites' ) ); |
||
1859 | |||
1860 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1861 | } |
||
1862 | } |
||
1863 | |||
1864 | /** |
||
1865 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1866 | */ |
||
1867 | public static function is_development_version() { |
||
1868 | /** |
||
1869 | * Allows filtering whether this is a development version of Jetpack. |
||
1870 | * |
||
1871 | * This filter is especially useful for tests. |
||
1872 | * |
||
1873 | * @since 4.3.0 |
||
1874 | * |
||
1875 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
1876 | */ |
||
1877 | return (bool) apply_filters( |
||
1878 | 'jetpack_development_version', |
||
1879 | ! preg_match( '/^\d+(\.\d+)+$/', Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
1880 | ); |
||
1881 | } |
||
1882 | |||
1883 | /** |
||
1884 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1885 | */ |
||
1886 | public static function is_user_connected( $user_id = false ) { |
||
1887 | _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Manager\\is_user_connected' ); |
||
1888 | return self::connection()->is_user_connected( $user_id ); |
||
1889 | } |
||
1890 | |||
1891 | /** |
||
1892 | * Get the wpcom user data of the current|specified connected user. |
||
1893 | */ |
||
1894 | public static function get_connected_user_data( $user_id = null ) { |
||
1895 | _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Manager\\get_connected_user_data' ); |
||
1896 | return self::connection()->get_connected_user_data( $user_id ); |
||
1897 | } |
||
1898 | |||
1899 | /** |
||
1900 | * Get the wpcom email of the current|specified connected user. |
||
1901 | */ |
||
1902 | public static function get_connected_user_email( $user_id = null ) { |
||
1903 | if ( ! $user_id ) { |
||
1904 | $user_id = get_current_user_id(); |
||
1905 | } |
||
1906 | |||
1907 | $xml = new Jetpack_IXR_Client( |
||
1908 | array( |
||
1909 | 'user_id' => $user_id, |
||
1910 | ) |
||
1911 | ); |
||
1912 | $xml->query( 'wpcom.getUserEmail' ); |
||
1913 | if ( ! $xml->isError() ) { |
||
1914 | return $xml->getResponse(); |
||
1915 | } |
||
1916 | return false; |
||
1917 | } |
||
1918 | |||
1919 | /** |
||
1920 | * Get the wpcom email of the master user. |
||
1921 | */ |
||
1922 | public static function get_master_user_email() { |
||
1923 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
1924 | if ( $master_user_id ) { |
||
1925 | return self::get_connected_user_email( $master_user_id ); |
||
1926 | } |
||
1927 | return ''; |
||
1928 | } |
||
1929 | |||
1930 | /** |
||
1931 | * Whether the current user is the connection owner. |
||
1932 | * |
||
1933 | * @deprecated since 7.7 |
||
1934 | * |
||
1935 | * @return bool Whether the current user is the connection owner. |
||
1936 | */ |
||
1937 | public function current_user_is_connection_owner() { |
||
1938 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::is_connection_owner' ); |
||
1939 | return self::connection()->is_connection_owner(); |
||
1940 | } |
||
1941 | |||
1942 | /** |
||
1943 | * Gets current user IP address. |
||
1944 | * |
||
1945 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
1946 | * |
||
1947 | * @return string Current user IP address. |
||
1948 | */ |
||
1949 | public static function current_user_ip( $check_all_headers = false ) { |
||
1950 | if ( $check_all_headers ) { |
||
1951 | foreach ( array( |
||
1952 | 'HTTP_CF_CONNECTING_IP', |
||
1953 | 'HTTP_CLIENT_IP', |
||
1954 | 'HTTP_X_FORWARDED_FOR', |
||
1955 | 'HTTP_X_FORWARDED', |
||
1956 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
1957 | 'HTTP_FORWARDED_FOR', |
||
1958 | 'HTTP_FORWARDED', |
||
1959 | 'HTTP_VIA', |
||
1960 | ) as $key ) { |
||
1961 | if ( ! empty( $_SERVER[ $key ] ) ) { |
||
1962 | return $_SERVER[ $key ]; |
||
1963 | } |
||
1964 | } |
||
1965 | } |
||
1966 | |||
1967 | return ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
||
1968 | } |
||
1969 | |||
1970 | /** |
||
1971 | * Synchronize connected user role changes |
||
1972 | */ |
||
1973 | function user_role_change( $user_id ) { |
||
1974 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Users::user_role_change()' ); |
||
1975 | Users::user_role_change( $user_id ); |
||
1976 | } |
||
1977 | |||
1978 | /** |
||
1979 | * Loads the currently active modules. |
||
1980 | */ |
||
1981 | public static function load_modules() { |
||
1982 | $is_offline_mode = ( new Status() )->is_offline_mode(); |
||
1983 | if ( |
||
1984 | ! self::is_connection_ready() |
||
1985 | && ! $is_offline_mode |
||
1986 | && ! self::is_onboarding() |
||
1987 | && ( |
||
1988 | ! is_multisite() |
||
1989 | || ! get_site_option( 'jetpack_protect_active' ) |
||
1990 | ) |
||
1991 | ) { |
||
1992 | return; |
||
1993 | } |
||
1994 | |||
1995 | $version = Jetpack_Options::get_option( 'version' ); |
||
1996 | View Code Duplication | if ( ! $version ) { |
|
1997 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
1998 | /** This action is documented in class.jetpack.php */ |
||
1999 | do_action( 'updating_jetpack_version', $version, false ); |
||
2000 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
2001 | } |
||
2002 | list( $version ) = explode( ':', $version ); |
||
2003 | |||
2004 | $modules = array_filter( self::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
2005 | |||
2006 | $modules_data = array(); |
||
2007 | |||
2008 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
2009 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
2010 | $updated_modules = array(); |
||
2011 | foreach ( $modules as $module ) { |
||
2012 | $modules_data[ $module ] = self::get_module( $module ); |
||
2013 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
2014 | continue; |
||
2015 | } |
||
2016 | |||
2017 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
2018 | continue; |
||
2019 | } |
||
2020 | |||
2021 | $updated_modules[] = $module; |
||
2022 | } |
||
2023 | |||
2024 | $modules = array_diff( $modules, $updated_modules ); |
||
2025 | } |
||
2026 | |||
2027 | $is_site_connection = self::connection()->is_site_connection(); |
||
2028 | |||
2029 | foreach ( $modules as $index => $module ) { |
||
2030 | // If we're in offline/site-connection mode, disable modules requiring a connection/user connection. |
||
2031 | if ( $is_offline_mode || $is_site_connection ) { |
||
2032 | // Prime the pump if we need to |
||
2033 | if ( empty( $modules_data[ $module ] ) ) { |
||
2034 | $modules_data[ $module ] = self::get_module( $module ); |
||
2035 | } |
||
2036 | // If the module requires a connection, but we're in local mode, don't include it. |
||
2037 | if ( $is_offline_mode && $modules_data[ $module ]['requires_connection'] ) { |
||
2038 | continue; |
||
2039 | } |
||
2040 | |||
2041 | if ( $is_site_connection && $modules_data[ $module ]['requires_user_connection'] ) { |
||
2042 | continue; |
||
2043 | } |
||
2044 | } |
||
2045 | |||
2046 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
2047 | continue; |
||
2048 | } |
||
2049 | |||
2050 | if ( ! include_once self::get_module_path( $module ) ) { |
||
2051 | unset( $modules[ $index ] ); |
||
2052 | self::update_active_modules( array_values( $modules ) ); |
||
2053 | continue; |
||
2054 | } |
||
2055 | |||
2056 | /** |
||
2057 | * Fires when a specific module is loaded. |
||
2058 | * The dynamic part of the hook, $module, is the module slug. |
||
2059 | * |
||
2060 | * @since 1.1.0 |
||
2061 | */ |
||
2062 | do_action( 'jetpack_module_loaded_' . $module ); |
||
2063 | } |
||
2064 | |||
2065 | /** |
||
2066 | * Fires when all the modules are loaded. |
||
2067 | * |
||
2068 | * @since 1.1.0 |
||
2069 | */ |
||
2070 | do_action( 'jetpack_modules_loaded' ); |
||
2071 | |||
2072 | // Load module-specific code that is needed even when a module isn't active. Loaded here because code contained therein may need actions such as setup_theme. |
||
2073 | require_once JETPACK__PLUGIN_DIR . 'modules/module-extras.php'; |
||
2074 | } |
||
2075 | |||
2076 | /** |
||
2077 | * Check if Jetpack's REST API compat file should be included |
||
2078 | * |
||
2079 | * @action plugins_loaded |
||
2080 | * @return null |
||
2081 | */ |
||
2082 | public function check_rest_api_compat() { |
||
2096 | |||
2097 | /** |
||
2098 | * Gets all plugins currently active in values, regardless of whether they're |
||
2099 | * traditionally activated or network activated. |
||
2100 | * |
||
2101 | * @todo Store the result in core's object cache maybe? |
||
2102 | */ |
||
2103 | public static function get_active_plugins() { |
||
2119 | |||
2120 | /** |
||
2121 | * Gets and parses additional plugin data to send with the heartbeat data |
||
2122 | * |
||
2123 | * @since 3.8.1 |
||
2124 | * |
||
2125 | * @return array Array of plugin data |
||
2126 | */ |
||
2127 | public static function get_parsed_plugin_data() { |
||
2148 | |||
2149 | /** |
||
2150 | * Gets and parses theme data to send with the heartbeat data |
||
2151 | * |
||
2152 | * @since 3.8.1 |
||
2153 | * |
||
2154 | * @return array Array of theme data |
||
2155 | */ |
||
2156 | public static function get_parsed_theme_data() { |
||
2178 | |||
2179 | /** |
||
2180 | * Checks whether a specific plugin is active. |
||
2181 | * |
||
2182 | * We don't want to store these in a static variable, in case |
||
2183 | * there are switch_to_blog() calls involved. |
||
2184 | */ |
||
2185 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
2188 | |||
2189 | /** |
||
2190 | * Check if Jetpack's Open Graph tags should be used. |
||
2191 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
2192 | * |
||
2193 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2194 | * @action plugins_loaded |
||
2195 | * @return null |
||
2196 | */ |
||
2197 | public function check_open_graph() { |
||
2224 | |||
2225 | /** |
||
2226 | * Check if Jetpack's Twitter tags should be used. |
||
2227 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
2228 | * |
||
2229 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2230 | * @action plugins_loaded |
||
2231 | * @return null |
||
2232 | */ |
||
2233 | public function check_twitter_tags() { |
||
2257 | |||
2258 | /** |
||
2259 | * Allows plugins to submit security reports. |
||
2260 | * |
||
2261 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
2262 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
2263 | * @param array $args See definitions above |
||
2264 | */ |
||
2265 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
2268 | |||
2269 | /* Jetpack Options API */ |
||
2270 | |||
2271 | public static function get_option_names( $type = 'compact' ) { |
||
2274 | |||
2275 | /** |
||
2276 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
2277 | * |
||
2278 | * @param string $name Option name |
||
2279 | * @param mixed $default (optional) |
||
2280 | */ |
||
2281 | public static function get_option( $name, $default = false ) { |
||
2284 | |||
2285 | /** |
||
2286 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
2287 | * |
||
2288 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
2289 | * @param string $name Option name |
||
2290 | * @param mixed $value Option value |
||
2291 | */ |
||
2292 | public static function update_option( $name, $value ) { |
||
2296 | |||
2297 | /** |
||
2298 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
2299 | * |
||
2300 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
2301 | * @param array $array array( option name => option value, ... ) |
||
2302 | */ |
||
2303 | public static function update_options( $array ) { |
||
2307 | |||
2308 | /** |
||
2309 | * Deletes the given option. May be passed multiple option names as an array. |
||
2310 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
2311 | * |
||
2312 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
2313 | * @param string|array $names |
||
2314 | */ |
||
2315 | public static function delete_option( $names ) { |
||
2319 | |||
2320 | /** |
||
2321 | * Enters a user token into the user_tokens option |
||
2322 | * |
||
2323 | * @deprecated 8.0 Use Automattic\Jetpack\Connection\Tokens->update_user_token() instead. |
||
2324 | * |
||
2325 | * @param int $user_id The user id. |
||
2326 | * @param string $token The user token. |
||
2327 | * @param bool $is_master_user Whether the user is the master user. |
||
2328 | * @return bool |
||
2329 | */ |
||
2330 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
2334 | |||
2335 | /** |
||
2336 | * Returns an array of all PHP files in the specified absolute path. |
||
2337 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
2338 | * |
||
2339 | * @param string $absolute_path The absolute path of the directory to search. |
||
2340 | * @return array Array of absolute paths to the PHP files. |
||
2341 | */ |
||
2342 | public static function glob_php( $absolute_path ) { |
||
2371 | |||
2372 | public static function activate_new_modules( $redirect = false ) { |
||
2431 | |||
2432 | /** |
||
2433 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2434 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2435 | * |
||
2436 | * @param bool|string $min_version Only return modules introduced in this version or later. Default is false, do not filter. |
||
2437 | * @param bool|string $max_version Only return modules introduced before this version. Default is false, do not filter. |
||
2438 | * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection. |
||
2439 | * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection. |
||
2440 | * |
||
2441 | * @return array $modules Array of module slugs |
||
2442 | */ |
||
2443 | public static function get_available_modules( $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) { |
||
2515 | |||
2516 | /** |
||
2517 | * Get default modules loaded on activation. |
||
2518 | * |
||
2519 | * @param bool|string $min_version Onlu return modules introduced in this version or later. Default is false, do not filter. |
||
2520 | * @param bool|string $max_version Only return modules introduced before this version. Default is false, do not filter. |
||
2521 | * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection. |
||
2522 | * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection. |
||
2523 | * |
||
2524 | * @return array $modules Array of module slugs |
||
2525 | */ |
||
2526 | public static function get_default_modules( $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) { |
||
2559 | |||
2560 | /** |
||
2561 | * Checks activated modules during auto-activation to determine |
||
2562 | * if any of those modules are being deprecated. If so, close |
||
2563 | * them out, and add any replacement modules. |
||
2564 | * |
||
2565 | * Runs at priority 99 by default. |
||
2566 | * |
||
2567 | * This is run late, so that it can still activate a module if |
||
2568 | * the new module is a replacement for another that the user |
||
2569 | * currently has active, even if something at the normal priority |
||
2570 | * would kibosh everything. |
||
2571 | * |
||
2572 | * @since 2.6 |
||
2573 | * @uses jetpack_get_default_modules filter |
||
2574 | * @param array $modules |
||
2575 | * @return array |
||
2576 | */ |
||
2577 | function handle_deprecated_modules( $modules ) { |
||
2604 | |||
2605 | /** |
||
2606 | * Checks activated plugins during auto-activation to determine |
||
2607 | * if any of those plugins are in the list with a corresponding module |
||
2608 | * that is not compatible with the plugin. The module will not be allowed |
||
2609 | * to auto-activate. |
||
2610 | * |
||
2611 | * @since 2.6 |
||
2612 | * @uses jetpack_get_default_modules filter |
||
2613 | * @param array $modules |
||
2614 | * @return array |
||
2615 | */ |
||
2616 | function filter_default_modules( $modules ) { |
||
2640 | |||
2641 | /** |
||
2642 | * Extract a module's slug from its full path. |
||
2643 | */ |
||
2644 | public static function get_module_slug( $file ) { |
||
2647 | |||
2648 | /** |
||
2649 | * Generate a module's path from its slug. |
||
2650 | */ |
||
2651 | public static function get_module_path( $slug ) { |
||
2662 | |||
2663 | /** |
||
2664 | * Load module data from module file. Headers differ from WordPress |
||
2665 | * plugin headers to avoid them being identified as standalone |
||
2666 | * plugins on the WordPress plugins page. |
||
2667 | */ |
||
2668 | public static function get_module( $module ) { |
||
2768 | |||
2769 | /** |
||
2770 | * Like core's get_file_data implementation, but caches the result. |
||
2771 | */ |
||
2772 | public static function get_file_data( $file, $headers ) { |
||
2805 | |||
2806 | /** |
||
2807 | * Return translated module tag. |
||
2808 | * |
||
2809 | * @param string $tag Tag as it appears in each module heading. |
||
2810 | * |
||
2811 | * @return mixed |
||
2812 | */ |
||
2813 | public static function translate_module_tag( $tag ) { |
||
2816 | |||
2817 | /** |
||
2818 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2819 | * |
||
2820 | * @since 3.9.2 |
||
2821 | * |
||
2822 | * @param array $modules |
||
2823 | * |
||
2824 | * @return string|void |
||
2825 | */ |
||
2826 | public static function get_translated_modules( $modules ) { |
||
2839 | |||
2840 | /** |
||
2841 | * Get a list of activated modules as an array of module slugs. |
||
2842 | */ |
||
2843 | public static function get_active_modules() { |
||
2875 | |||
2876 | /** |
||
2877 | * Check whether or not a Jetpack module is active. |
||
2878 | * |
||
2879 | * @param string $module The slug of a Jetpack module. |
||
2880 | * @return bool |
||
2881 | * |
||
2882 | * @static |
||
2883 | */ |
||
2884 | public static function is_module_active( $module ) { |
||
2887 | |||
2888 | public static function is_module( $module ) { |
||
2891 | |||
2892 | /** |
||
2893 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2894 | * |
||
2895 | * @param bool $catch True to start catching, False to stop. |
||
2896 | * |
||
2897 | * @static |
||
2898 | */ |
||
2899 | public static function catch_errors( $catch ) { |
||
2912 | |||
2913 | /** |
||
2914 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2915 | */ |
||
2916 | public static function catch_errors_on_shutdown() { |
||
2919 | |||
2920 | /** |
||
2921 | * Rewrite any string to make paths easier to read. |
||
2922 | * |
||
2923 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2924 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2925 | * |
||
2926 | * @param $string |
||
2927 | * @return mixed |
||
2928 | */ |
||
2929 | public static function alias_directories( $string ) { |
||
2937 | |||
2938 | public static function activate_default_modules( |
||
3110 | |||
3111 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
3204 | |||
3205 | function activate_module_actions( $module ) { |
||
3208 | |||
3209 | public static function deactivate_module( $module ) { |
||
3226 | |||
3227 | public static function enable_module_configurable( $module ) { |
||
3231 | |||
3232 | /** |
||
3233 | * Composes a module configure URL. It uses Jetpack settings search as default value |
||
3234 | * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter |
||
3235 | * |
||
3236 | * @param string $module Module slug |
||
3237 | * @return string $url module configuration URL |
||
3238 | */ |
||
3239 | public static function module_configuration_url( $module ) { |
||
3253 | |||
3254 | /* Installation */ |
||
3255 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
3295 | |||
3296 | /** |
||
3297 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
3298 | * |
||
3299 | * @static |
||
3300 | */ |
||
3301 | public static function plugin_activation( $network_wide ) { |
||
3321 | |||
3322 | public static function get_activation_source( $referer_url ) { |
||
3371 | |||
3372 | /** |
||
3373 | * Runs before bumping version numbers up to a new version |
||
3374 | * |
||
3375 | * @param string $version Version:timestamp. |
||
3376 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
3377 | */ |
||
3378 | public static function do_version_bump( $version, $old_version ) { |
||
3379 | if ( $old_version ) { // For existing Jetpack installations. |
||
3380 | add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' ); |
||
3381 | |||
3382 | // If a front end page is visited after the update, the 'wp' action will fire. |
||
3383 | add_action( 'wp', 'Jetpack::set_update_modal_display' ); |
||
3384 | |||
3385 | // If an admin page is visited after the update, the 'current_screen' action will fire. |
||
3386 | add_action( 'current_screen', 'Jetpack::set_update_modal_display' ); |
||
3387 | } |
||
3388 | } |
||
3389 | |||
3390 | /** |
||
3391 | * Sets the display_update_modal state. |
||
3392 | */ |
||
3393 | public static function set_update_modal_display() { |
||
3394 | self::state( 'display_update_modal', true ); |
||
3395 | |||
3396 | } |
||
3397 | |||
3398 | /** |
||
3399 | * Enqueues the block library styles. |
||
3400 | * |
||
3401 | * @param string $hook The current admin page. |
||
3402 | */ |
||
3403 | public static function enqueue_block_style( $hook ) { |
||
3404 | if ( 'toplevel_page_jetpack' === $hook ) { |
||
3405 | wp_enqueue_style( 'wp-block-library' ); |
||
3406 | } |
||
3407 | } |
||
3408 | |||
3409 | /** |
||
3410 | * Sets the internal version number and activation state. |
||
3411 | * |
||
3412 | * @static |
||
3413 | */ |
||
3414 | public static function plugin_initialize() { |
||
3415 | if ( ! Jetpack_Options::get_option( 'activated' ) ) { |
||
3416 | Jetpack_Options::update_option( 'activated', 2 ); |
||
3417 | } |
||
3418 | |||
3419 | View Code Duplication | if ( ! Jetpack_Options::get_option( 'version' ) ) { |
|
3420 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
3421 | /** This action is documented in class.jetpack.php */ |
||
3422 | do_action( 'updating_jetpack_version', $version, false ); |
||
3423 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
3424 | } |
||
3425 | |||
3426 | self::load_modules(); |
||
3427 | |||
3428 | Jetpack_Options::delete_option( 'do_activate' ); |
||
3429 | Jetpack_Options::delete_option( 'dismissed_connection_banner' ); |
||
3430 | } |
||
3431 | |||
3432 | /** |
||
3433 | * Removes all connection options |
||
3434 | * |
||
3435 | * @static |
||
3436 | */ |
||
3437 | public static function plugin_deactivation() { |
||
3438 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
||
3439 | $tracking = new Tracking(); |
||
3440 | $tracking->record_user_event( 'deactivate_plugin', array() ); |
||
3441 | if ( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
3442 | Jetpack_Network::init()->deactivate(); |
||
3443 | } else { |
||
3444 | self::disconnect( false ); |
||
3445 | // Jetpack_Heartbeat::init()->deactivate(); |
||
3446 | } |
||
3447 | } |
||
3448 | |||
3449 | /** |
||
3450 | * Disconnects from the Jetpack servers. |
||
3451 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
3452 | * |
||
3453 | * @static |
||
3454 | */ |
||
3455 | public static function disconnect( $update_activated_state = true ) { |
||
3456 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
3457 | |||
3458 | $connection = self::connection(); |
||
3459 | |||
3460 | ( new Nonce_Handler() )->clean_all(); |
||
3461 | |||
3462 | // If the site is in an IDC because sync is not allowed, |
||
3463 | // let's make sure to not disconnect the production site. |
||
3464 | if ( ! Identity_Crisis::validate_sync_error_idc_option() ) { |
||
3465 | $tracking = new Tracking(); |
||
3466 | $tracking->record_user_event( 'disconnect_site', array() ); |
||
3467 | |||
3468 | $connection->disconnect_site_wpcom( true ); |
||
3469 | } |
||
3470 | |||
3471 | $connection->delete_all_connection_tokens( true ); |
||
3472 | Identity_Crisis::clear_all_idc_options(); |
||
3473 | |||
3474 | if ( $update_activated_state ) { |
||
3475 | Jetpack_Options::update_option( 'activated', 4 ); |
||
3476 | } |
||
3477 | |||
3478 | if ( $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ) ) { |
||
3479 | // Check then record unique disconnection if site has never been disconnected previously |
||
3480 | if ( - 1 == $jetpack_unique_connection['disconnected'] ) { |
||
3481 | $jetpack_unique_connection['disconnected'] = 1; |
||
3482 | } else { |
||
3483 | if ( 0 == $jetpack_unique_connection['disconnected'] ) { |
||
3484 | // track unique disconnect |
||
3485 | $jetpack = self::init(); |
||
3486 | |||
3487 | $jetpack->stat( 'connections', 'unique-disconnect' ); |
||
3488 | $jetpack->do_stats( 'server_side' ); |
||
3489 | } |
||
3490 | // increment number of times disconnected |
||
3491 | $jetpack_unique_connection['disconnected'] += 1; |
||
3492 | } |
||
3493 | |||
3494 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
3495 | } |
||
3496 | |||
3497 | // Delete all the sync related data. Since it could be taking up space. |
||
3498 | Sender::get_instance()->uninstall(); |
||
3499 | |||
3500 | } |
||
3501 | |||
3502 | /** |
||
3503 | * Disconnects the user |
||
3504 | * |
||
3505 | * @param int $user_id The user ID to disconnect. |
||
3506 | */ |
||
3507 | public function disconnect_user( $user_id ) { |
||
3508 | $this->connection_manager->disconnect_user( $user_id ); |
||
3509 | } |
||
3510 | |||
3511 | /** |
||
3512 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
3513 | * |
||
3514 | * @deprecated since Jetpack 9.7.0 |
||
3515 | * @see Automattic\Jetpack\Connection\Manager::try_registration() |
||
3516 | * |
||
3517 | * @return bool|WP_Error |
||
3518 | */ |
||
3519 | public static function try_registration() { |
||
3520 | _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Manager::try_registration' ); |
||
3521 | return static::connection()->try_registration(); |
||
3522 | } |
||
3523 | |||
3524 | /** |
||
3525 | * Checking the domain names in beta versions. |
||
3526 | * If this is a development version, before attempting to connect, let's make sure that the domains are viable. |
||
3527 | * |
||
3528 | * @param null|\WP_Error $error The domain validation error, or `null` if everything's fine. |
||
3529 | * |
||
3530 | * @return null|\WP_Error The domain validation error, or `null` if everything's fine. |
||
3531 | */ |
||
3532 | public static function registration_check_domains( $error ) { |
||
3533 | if ( static::is_development_version() && defined( 'PHP_URL_HOST' ) ) { |
||
3534 | $domains_to_check = array_unique( |
||
3535 | array( |
||
3536 | 'siteurl' => wp_parse_url( get_site_url(), PHP_URL_HOST ), |
||
3537 | 'homeurl' => wp_parse_url( get_home_url(), PHP_URL_HOST ), |
||
3538 | ) |
||
3539 | ); |
||
3540 | foreach ( $domains_to_check as $domain ) { |
||
3541 | $result = static::connection()->is_usable_domain( $domain ); |
||
3542 | if ( is_wp_error( $result ) ) { |
||
3543 | return $result; |
||
3544 | } |
||
3545 | } |
||
3546 | } |
||
3547 | |||
3548 | return $error; |
||
3549 | } |
||
3550 | |||
3551 | /** |
||
3552 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3553 | * |
||
3554 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3555 | */ |
||
3556 | public static function log( $code, $data = null ) { |
||
3557 | // only grab the latest 200 entries |
||
3558 | $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 ); |
||
3559 | |||
3560 | // Append our event to the log |
||
3561 | $log_entry = array( |
||
3562 | 'time' => time(), |
||
3563 | 'user_id' => get_current_user_id(), |
||
3564 | 'blog_id' => Jetpack_Options::get_option( 'id' ), |
||
3565 | 'code' => $code, |
||
3566 | ); |
||
3567 | // Don't bother storing it unless we've got some. |
||
3568 | if ( ! is_null( $data ) ) { |
||
3569 | $log_entry['data'] = $data; |
||
3570 | } |
||
3571 | $log[] = $log_entry; |
||
3572 | |||
3573 | // Try add_option first, to make sure it's not autoloaded. |
||
3574 | // @todo: Add an add_option method to Jetpack_Options |
||
3575 | if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) { |
||
3576 | Jetpack_Options::update_option( 'log', $log ); |
||
3577 | } |
||
3578 | |||
3579 | /** |
||
3580 | * Fires when Jetpack logs an internal event. |
||
3581 | * |
||
3582 | * @since 3.0.0 |
||
3583 | * |
||
3584 | * @param array $log_entry { |
||
3585 | * Array of details about the log entry. |
||
3586 | * |
||
3587 | * @param string time Time of the event. |
||
3588 | * @param int user_id ID of the user who trigerred the event. |
||
3589 | * @param int blog_id Jetpack Blog ID. |
||
3590 | * @param string code Unique name for the event. |
||
3591 | * @param string data Data about the event. |
||
3592 | * } |
||
3593 | */ |
||
3594 | do_action( 'jetpack_log_entry', $log_entry ); |
||
3595 | } |
||
3596 | |||
3597 | /** |
||
3598 | * Get the internal event log. |
||
3599 | * |
||
3600 | * @param $event (string) - only return the specific log events |
||
3601 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3602 | * |
||
3603 | * @return array of log events || WP_Error for invalid params |
||
3604 | */ |
||
3605 | public static function get_log( $event = false, $num = false ) { |
||
3606 | if ( $event && ! is_string( $event ) ) { |
||
3607 | return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) ); |
||
3608 | } |
||
3609 | |||
3610 | if ( $num && ! is_numeric( $num ) ) { |
||
3611 | return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) ); |
||
3612 | } |
||
3613 | |||
3614 | $entire_log = Jetpack_Options::get_option( 'log', array() ); |
||
3615 | |||
3616 | // If nothing set - act as it did before, otherwise let's start customizing the output |
||
3617 | if ( ! $num && ! $event ) { |
||
3618 | return $entire_log; |
||
3619 | } else { |
||
3620 | $entire_log = array_reverse( $entire_log ); |
||
3621 | } |
||
3622 | |||
3623 | $custom_log_output = array(); |
||
3624 | |||
3625 | if ( $event ) { |
||
3626 | foreach ( $entire_log as $log_event ) { |
||
3627 | if ( $event == $log_event['code'] ) { |
||
3628 | $custom_log_output[] = $log_event; |
||
3629 | } |
||
3630 | } |
||
3631 | } else { |
||
3632 | $custom_log_output = $entire_log; |
||
3633 | } |
||
3634 | |||
3635 | if ( $num ) { |
||
3636 | $custom_log_output = array_slice( $custom_log_output, 0, $num ); |
||
3637 | } |
||
3638 | |||
3639 | return $custom_log_output; |
||
3640 | } |
||
3641 | |||
3642 | /** |
||
3643 | * Log modification of important settings. |
||
3644 | */ |
||
3645 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3646 | switch ( $option ) { |
||
3647 | case 'jetpack_sync_non_public_post_stati': |
||
3648 | self::log( $option, $value ); |
||
3649 | break; |
||
3650 | } |
||
3651 | } |
||
3652 | |||
3653 | /** |
||
3654 | * Return stat data for WPCOM sync |
||
3655 | */ |
||
3656 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3657 | $data = Jetpack_Heartbeat::generate_stats_array(); |
||
3658 | |||
3659 | if ( $extended ) { |
||
3660 | $additional_data = self::get_additional_stat_data(); |
||
3661 | $data = array_merge( $data, $additional_data ); |
||
3662 | } |
||
3663 | |||
3664 | if ( $encode ) { |
||
3665 | return json_encode( $data ); |
||
3666 | } |
||
3667 | |||
3668 | return $data; |
||
3669 | } |
||
3670 | |||
3671 | /** |
||
3672 | * Get additional stat data to sync to WPCOM |
||
3673 | */ |
||
3674 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3675 | $return[ "{$prefix}themes" ] = self::get_parsed_theme_data(); |
||
3676 | $return[ "{$prefix}plugins-extra" ] = self::get_parsed_plugin_data(); |
||
3677 | $return[ "{$prefix}users" ] = (int) self::get_site_user_count(); |
||
3678 | $return[ "{$prefix}site-count" ] = 0; |
||
3679 | |||
3680 | if ( function_exists( 'get_blog_count' ) ) { |
||
3681 | $return[ "{$prefix}site-count" ] = get_blog_count(); |
||
3682 | } |
||
3683 | return $return; |
||
3684 | } |
||
3685 | |||
3686 | private static function get_site_user_count() { |
||
3687 | global $wpdb; |
||
3688 | |||
3689 | if ( function_exists( 'wp_is_large_network' ) ) { |
||
3690 | if ( wp_is_large_network( 'users' ) ) { |
||
3691 | return -1; // Not a real value but should tell us that we are dealing with a large network. |
||
3692 | } |
||
3693 | } |
||
3694 | if ( false === ( $user_count = get_transient( 'jetpack_site_user_count' ) ) ) { |
||
3695 | // It wasn't there, so regenerate the data and save the transient |
||
3696 | $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities'" ); |
||
3697 | set_transient( 'jetpack_site_user_count', $user_count, DAY_IN_SECONDS ); |
||
3698 | } |
||
3699 | return $user_count; |
||
3700 | } |
||
3701 | |||
3702 | /* Admin Pages */ |
||
3703 | |||
3704 | function admin_init() { |
||
3705 | // If the plugin is not connected, display a connect message. |
||
3706 | if ( |
||
3707 | // the plugin was auto-activated and needs its candy |
||
3708 | Jetpack_Options::get_option_and_ensure_autoload( 'do_activate', '0' ) |
||
3709 | || |
||
3710 | // the plugin is active, but was never activated. Probably came from a site-wide network activation |
||
3711 | ! Jetpack_Options::get_option( 'activated' ) |
||
3712 | ) { |
||
3713 | self::plugin_initialize(); |
||
3714 | } |
||
3715 | |||
3716 | $is_offline_mode = ( new Status() )->is_offline_mode(); |
||
3717 | $fallback_no_verify_ssl_certs = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ); |
||
3718 | /** Already documented in automattic/jetpack-connection::src/class-client.php */ |
||
3719 | $client_verify_ssl_certs = apply_filters( 'jetpack_client_verify_ssl_certs', false ); |
||
3720 | |||
3721 | if ( ! $is_offline_mode ) { |
||
3722 | Jetpack_Connection_Banner::init(); |
||
3723 | } |
||
3724 | |||
3725 | if ( ( self::is_connection_ready() || $is_offline_mode ) && false === $fallback_no_verify_ssl_certs && ! $client_verify_ssl_certs ) { |
||
3726 | // Upgrade: 1.1 -> 1.1.1 |
||
3727 | // Check and see if host can verify the Jetpack servers' SSL certificate |
||
3728 | $args = array(); |
||
3729 | Client::_wp_remote_request( self::connection()->api_url( 'test' ), $args, true ); |
||
3730 | } |
||
3731 | |||
3732 | Jetpack_Recommendations_Banner::init(); |
||
3733 | |||
3734 | if ( current_user_can( 'manage_options' ) && ! self::permit_ssl() ) { |
||
3735 | add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) ); |
||
3736 | } |
||
3737 | |||
3738 | add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) ); |
||
3739 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
||
3740 | add_action( 'admin_enqueue_scripts', array( $this, 'deactivate_dialog' ) ); |
||
3741 | |||
3742 | if ( isset( $_COOKIE['jetpackState']['display_update_modal'] ) ) { |
||
3743 | add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' ); |
||
3744 | } |
||
3745 | |||
3746 | add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) ); |
||
3747 | |||
3748 | if ( self::is_connection_ready() || $is_offline_mode ) { |
||
3749 | // Artificially throw errors in certain specific cases during plugin activation. |
||
3750 | add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) ); |
||
3751 | } |
||
3752 | |||
3753 | // Add custom column in wp-admin/users.php to show whether user is linked. |
||
3754 | add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) ); |
||
3755 | add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 ); |
||
3756 | add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) ); |
||
3757 | } |
||
3758 | |||
3759 | function admin_body_class( $admin_body_class = '' ) { |
||
3760 | $classes = explode( ' ', trim( $admin_body_class ) ); |
||
3761 | |||
3762 | $classes[] = self::is_connection_ready() ? 'jetpack-connected' : 'jetpack-disconnected'; |
||
3763 | |||
3764 | $admin_body_class = implode( ' ', array_unique( $classes ) ); |
||
3765 | return " $admin_body_class "; |
||
3766 | } |
||
3767 | |||
3768 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3769 | return $admin_body_class . ' jetpack-pagestyles '; |
||
3770 | } |
||
3771 | |||
3772 | /** |
||
3773 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3774 | * This function artificially throws errors for such cases (per a specific list). |
||
3775 | * |
||
3776 | * @param string $plugin The activated plugin. |
||
3777 | */ |
||
3778 | function throw_error_on_activate_plugin( $plugin ) { |
||
3779 | $active_modules = self::get_active_modules(); |
||
3780 | |||
3781 | // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks. |
||
3782 | if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) { |
||
3783 | $throw = false; |
||
3784 | |||
3785 | // Try and make sure it really was the stats plugin |
||
3786 | if ( ! class_exists( 'ReflectionFunction' ) ) { |
||
3787 | if ( 'stats.php' == basename( $plugin ) ) { |
||
3788 | $throw = true; |
||
3789 | } |
||
3790 | } else { |
||
3791 | $reflection = new ReflectionFunction( 'stats_get_api_key' ); |
||
3792 | if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) { |
||
3793 | $throw = true; |
||
3794 | } |
||
3795 | } |
||
3796 | |||
3797 | if ( $throw ) { |
||
3798 | trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR ); |
||
3799 | } |
||
3800 | } |
||
3801 | } |
||
3802 | |||
3803 | function intercept_plugin_error_scrape_init() { |
||
3804 | add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 ); |
||
3805 | } |
||
3806 | |||
3807 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3808 | if ( ! $result ) { |
||
3809 | return; |
||
3810 | } |
||
3811 | |||
3812 | foreach ( $this->plugins_to_deactivate as $deactivate_me ) { |
||
3813 | if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) { |
||
3814 | self::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), $deactivate_me[1] ), false ); |
||
3815 | } |
||
3816 | } |
||
3817 | } |
||
3818 | |||
3819 | /** |
||
3820 | * Register the remote file upload request handlers, if needed. |
||
3821 | * |
||
3822 | * @access public |
||
3823 | */ |
||
3824 | public function add_remote_request_handlers() { |
||
3825 | // Remote file uploads are allowed only via AJAX requests. |
||
3826 | if ( ! is_admin() || ! Constants::get_constant( 'DOING_AJAX' ) ) { |
||
3827 | return; |
||
3828 | } |
||
3829 | |||
3830 | // Remote file uploads are allowed only for a set of specific AJAX actions. |
||
3831 | $remote_request_actions = array( |
||
3832 | 'jetpack_upload_file', |
||
3833 | 'jetpack_update_file', |
||
3834 | ); |
||
3835 | |||
3836 | // phpcs:ignore WordPress.Security.NonceVerification |
||
3837 | if ( ! isset( $_POST['action'] ) || ! in_array( $_POST['action'], $remote_request_actions, true ) ) { |
||
3838 | return; |
||
3839 | } |
||
3840 | |||
3841 | // Require Jetpack authentication for the remote file upload AJAX requests. |
||
3842 | if ( ! $this->connection_manager ) { |
||
3843 | $this->connection_manager = new Connection_Manager(); |
||
3844 | } |
||
3845 | |||
3846 | $this->connection_manager->require_jetpack_authentication(); |
||
3847 | |||
3848 | // Register the remote file upload AJAX handlers. |
||
3849 | foreach ( $remote_request_actions as $action ) { |
||
3850 | add_action( "wp_ajax_nopriv_{$action}", array( $this, 'remote_request_handlers' ) ); |
||
3851 | } |
||
3852 | } |
||
3853 | |||
3854 | /** |
||
3855 | * Handler for Jetpack remote file uploads. |
||
3856 | * |
||
3857 | * @access public |
||
3858 | */ |
||
3859 | public function remote_request_handlers() { |
||
3860 | $action = current_filter(); |
||
3861 | |||
3862 | switch ( current_filter() ) { |
||
3863 | case 'wp_ajax_nopriv_jetpack_upload_file': |
||
3864 | $response = $this->upload_handler(); |
||
3865 | break; |
||
3866 | |||
3867 | case 'wp_ajax_nopriv_jetpack_update_file': |
||
3868 | $response = $this->upload_handler( true ); |
||
3869 | break; |
||
3870 | default: |
||
3871 | $response = new WP_Error( 'unknown_handler', 'Unknown Handler', 400 ); |
||
3872 | break; |
||
3873 | } |
||
3874 | |||
3875 | if ( ! $response ) { |
||
3876 | $response = new WP_Error( 'unknown_error', 'Unknown Error', 400 ); |
||
3877 | } |
||
3878 | |||
3879 | if ( is_wp_error( $response ) ) { |
||
3880 | $status_code = $response->get_error_data(); |
||
3881 | $error = $response->get_error_code(); |
||
3882 | $error_description = $response->get_error_message(); |
||
3883 | |||
3884 | if ( ! is_int( $status_code ) ) { |
||
3885 | $status_code = 400; |
||
3886 | } |
||
3887 | |||
3888 | status_header( $status_code ); |
||
3889 | die( json_encode( (object) compact( 'error', 'error_description' ) ) ); |
||
3890 | } |
||
3891 | |||
3892 | status_header( 200 ); |
||
3893 | if ( true === $response ) { |
||
3894 | exit; |
||
3895 | } |
||
3896 | |||
3897 | die( json_encode( (object) $response ) ); |
||
3898 | } |
||
3899 | |||
3900 | /** |
||
3901 | * Uploads a file gotten from the global $_FILES. |
||
3902 | * If `$update_media_item` is true and `post_id` is defined |
||
3903 | * the attachment file of the media item (gotten through of the post_id) |
||
3904 | * will be updated instead of add a new one. |
||
3905 | * |
||
3906 | * @param boolean $update_media_item - update media attachment |
||
3907 | * @return array - An array describing the uploadind files process |
||
3908 | */ |
||
3909 | function upload_handler( $update_media_item = false ) { |
||
3910 | if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { |
||
3911 | return new WP_Error( 405, get_status_header_desc( 405 ), 405 ); |
||
3912 | } |
||
3913 | |||
3914 | $user = wp_authenticate( '', '' ); |
||
3915 | if ( ! $user || is_wp_error( $user ) ) { |
||
3916 | return new WP_Error( 403, get_status_header_desc( 403 ), 403 ); |
||
3917 | } |
||
3918 | |||
3919 | wp_set_current_user( $user->ID ); |
||
3920 | |||
3921 | if ( ! current_user_can( 'upload_files' ) ) { |
||
3922 | return new WP_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 ); |
||
3923 | } |
||
3924 | |||
3925 | if ( empty( $_FILES ) ) { |
||
3926 | return new WP_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 ); |
||
3927 | } |
||
3928 | |||
3929 | foreach ( array_keys( $_FILES ) as $files_key ) { |
||
3930 | if ( ! isset( $_POST[ "_jetpack_file_hmac_{$files_key}" ] ) ) { |
||
3931 | return new WP_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 ); |
||
3932 | } |
||
3933 | } |
||
3934 | |||
3935 | $media_keys = array_keys( $_FILES['media'] ); |
||
3936 | |||
3937 | $token = ( new Tokens() )->get_access_token( get_current_user_id() ); |
||
3938 | if ( ! $token || is_wp_error( $token ) ) { |
||
3939 | return new WP_Error( 'unknown_token', 'Unknown Jetpack token', 403 ); |
||
3940 | } |
||
3941 | |||
3942 | $uploaded_files = array(); |
||
3943 | $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; |
||
3944 | unset( $GLOBALS['post'] ); |
||
3945 | foreach ( $_FILES['media']['name'] as $index => $name ) { |
||
3946 | $file = array(); |
||
3947 | foreach ( $media_keys as $media_key ) { |
||
3948 | $file[ $media_key ] = $_FILES['media'][ $media_key ][ $index ]; |
||
3949 | } |
||
3950 | |||
3951 | list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][ $index ] ); |
||
3952 | |||
3953 | $hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret ); |
||
3954 | if ( $hmac_provided !== $hmac_file ) { |
||
3955 | $uploaded_files[ $index ] = (object) array( |
||
3956 | 'error' => 'invalid_hmac', |
||
3957 | 'error_description' => 'The corresponding HMAC for this file does not match', |
||
3958 | ); |
||
3959 | continue; |
||
3960 | } |
||
3961 | |||
3962 | $_FILES['.jetpack.upload.'] = $file; |
||
3963 | $post_id = isset( $_POST['post_id'][ $index ] ) ? absint( $_POST['post_id'][ $index ] ) : 0; |
||
3964 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
||
3965 | $post_id = 0; |
||
3966 | } |
||
3967 | |||
3968 | if ( $update_media_item ) { |
||
3969 | if ( ! isset( $post_id ) || $post_id === 0 ) { |
||
3970 | return new WP_Error( 'invalid_input', 'Media ID must be defined.', 400 ); |
||
3971 | } |
||
3972 | |||
3973 | $media_array = $_FILES['media']; |
||
3974 | |||
3975 | $file_array['name'] = $media_array['name'][0]; |
||
3976 | $file_array['type'] = $media_array['type'][0]; |
||
3977 | $file_array['tmp_name'] = $media_array['tmp_name'][0]; |
||
3978 | $file_array['error'] = $media_array['error'][0]; |
||
3979 | $file_array['size'] = $media_array['size'][0]; |
||
3980 | |||
3981 | $edited_media_item = Jetpack_Media::edit_media_file( $post_id, $file_array ); |
||
3982 | |||
3983 | if ( is_wp_error( $edited_media_item ) ) { |
||
3984 | return $edited_media_item; |
||
3985 | } |
||
3986 | |||
3987 | $response = (object) array( |
||
3988 | 'id' => (string) $post_id, |
||
3989 | 'file' => (string) $edited_media_item->post_title, |
||
3990 | 'url' => (string) wp_get_attachment_url( $post_id ), |
||
3991 | 'type' => (string) $edited_media_item->post_mime_type, |
||
3992 | 'meta' => (array) wp_get_attachment_metadata( $post_id ), |
||
3993 | ); |
||
3994 | |||
3995 | return (array) array( $response ); |
||
3996 | } |
||
3997 | |||
3998 | $attachment_id = media_handle_upload( |
||
3999 | '.jetpack.upload.', |
||
4000 | $post_id, |
||
4001 | array(), |
||
4002 | array( |
||
4003 | 'action' => 'jetpack_upload_file', |
||
4004 | ) |
||
4005 | ); |
||
4006 | |||
4007 | if ( ! $attachment_id ) { |
||
4008 | $uploaded_files[ $index ] = (object) array( |
||
4009 | 'error' => 'unknown', |
||
4010 | 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site', |
||
4011 | ); |
||
4012 | } elseif ( is_wp_error( $attachment_id ) ) { |
||
4013 | $uploaded_files[ $index ] = (object) array( |
||
4014 | 'error' => 'attachment_' . $attachment_id->get_error_code(), |
||
4015 | 'error_description' => $attachment_id->get_error_message(), |
||
4016 | ); |
||
4017 | } else { |
||
4018 | $attachment = get_post( $attachment_id ); |
||
4019 | $uploaded_files[ $index ] = (object) array( |
||
4020 | 'id' => (string) $attachment_id, |
||
4021 | 'file' => $attachment->post_title, |
||
4022 | 'url' => wp_get_attachment_url( $attachment_id ), |
||
4023 | 'type' => $attachment->post_mime_type, |
||
4024 | 'meta' => wp_get_attachment_metadata( $attachment_id ), |
||
4025 | ); |
||
4026 | // Zip files uploads are not supported unless they are done for installation purposed |
||
4027 | // lets delete them in case something goes wrong in this whole process |
||
4028 | if ( 'application/zip' === $attachment->post_mime_type ) { |
||
4029 | // Schedule a cleanup for 2 hours from now in case of failed install. |
||
4030 | wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $attachment_id ) ); |
||
4031 | } |
||
4032 | } |
||
4033 | } |
||
4034 | if ( ! is_null( $global_post ) ) { |
||
4035 | $GLOBALS['post'] = $global_post; |
||
4036 | } |
||
4037 | |||
4038 | return $uploaded_files; |
||
4039 | } |
||
4040 | |||
4041 | /** |
||
4042 | * Add help to the Jetpack page |
||
4043 | * |
||
4044 | * @since Jetpack (1.2.3) |
||
4045 | * @return false if not the Jetpack page |
||
4046 | */ |
||
4047 | function admin_help() { |
||
4048 | $current_screen = get_current_screen(); |
||
4049 | |||
4050 | // Overview |
||
4051 | $current_screen->add_help_tab( |
||
4052 | array( |
||
4053 | 'id' => 'home', |
||
4054 | 'title' => __( 'Home', 'jetpack' ), |
||
4055 | 'content' => |
||
4056 | '<p><strong>' . __( 'Jetpack', 'jetpack' ) . '</strong></p>' . |
||
4057 | '<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' . |
||
4058 | '<p>' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '</p>', |
||
4059 | ) |
||
4060 | ); |
||
4061 | |||
4062 | // Screen Content |
||
4063 | if ( current_user_can( 'manage_options' ) ) { |
||
4064 | $current_screen->add_help_tab( |
||
4065 | array( |
||
4066 | 'id' => 'settings', |
||
4067 | 'title' => __( 'Settings', 'jetpack' ), |
||
4068 | 'content' => |
||
4069 | '<p><strong>' . __( 'Jetpack', 'jetpack' ) . '</strong></p>' . |
||
4070 | '<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' . |
||
4071 | '<ol>' . |
||
4072 | '<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' . |
||
4073 | '<li>' . __( 'Using the checkboxes next to each module, you can select multiple modules to toggle via the Bulk Actions menu at the top of the list.', 'jetpack' ) . '</li>' . |
||
4074 | '</ol>' . |
||
4075 | '<p>' . __( 'Using the tools on the right, you can search for specific modules, filter by module categories or which are active, or change the sorting order.', 'jetpack' ) . '</p>', |
||
4076 | ) |
||
4077 | ); |
||
4078 | } |
||
4079 | |||
4080 | // Help Sidebar |
||
4081 | $support_url = Redirect::get_url( 'jetpack-support' ); |
||
4082 | $faq_url = Redirect::get_url( 'jetpack-faq' ); |
||
4083 | $current_screen->set_help_sidebar( |
||
4084 | '<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' . |
||
4085 | '<p><a href="' . $faq_url . '" rel="noopener noreferrer" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' . |
||
4086 | '<p><a href="' . $support_url . '" rel="noopener noreferrer" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' . |
||
4087 | '<p><a href="' . self::admin_url( array( 'page' => 'jetpack-debugger' ) ) . '">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>' |
||
4088 | ); |
||
4089 | } |
||
4090 | |||
4091 | function admin_menu_css() { |
||
4092 | wp_enqueue_style( 'jetpack-icons' ); |
||
4093 | } |
||
4094 | |||
4095 | function admin_menu_order() { |
||
4096 | return true; |
||
4097 | } |
||
4098 | |||
4099 | function jetpack_menu_order( $menu_order ) { |
||
4100 | $jp_menu_order = array(); |
||
4101 | |||
4102 | foreach ( $menu_order as $index => $item ) { |
||
4103 | if ( $item != 'jetpack' ) { |
||
4104 | $jp_menu_order[] = $item; |
||
4105 | } |
||
4106 | |||
4107 | if ( $index == 0 ) { |
||
4108 | $jp_menu_order[] = 'jetpack'; |
||
4109 | } |
||
4110 | } |
||
4111 | |||
4112 | return $jp_menu_order; |
||
4113 | } |
||
4114 | |||
4115 | function admin_banner_styles() { |
||
4116 | $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
4117 | |||
4118 | View Code Duplication | if ( ! wp_style_is( 'jetpack-dops-style' ) ) { |
|
4119 | wp_register_style( |
||
4120 | 'jetpack-dops-style', |
||
4121 | plugins_url( '_inc/build/admin.css', JETPACK__PLUGIN_FILE ), |
||
4122 | array(), |
||
4123 | JETPACK__VERSION |
||
4124 | ); |
||
4125 | } |
||
4126 | |||
4127 | wp_enqueue_style( |
||
4128 | 'jetpack', |
||
4129 | plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ), |
||
4130 | array( 'jetpack-dops-style' ), |
||
4131 | JETPACK__VERSION . '-20121016' |
||
4132 | ); |
||
4133 | wp_style_add_data( 'jetpack', 'rtl', 'replace' ); |
||
4134 | wp_style_add_data( 'jetpack', 'suffix', $min ); |
||
4135 | } |
||
4136 | |||
4137 | function plugin_action_links( $actions ) { |
||
4138 | |||
4139 | $jetpack_home = array( 'jetpack-home' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack' ), __( 'My Jetpack', 'jetpack' ) ) ); |
||
4140 | |||
4141 | if ( current_user_can( 'jetpack_manage_modules' ) && ( self::is_connection_ready() || ( new Status() )->is_offline_mode() ) ) { |
||
4142 | return array_merge( |
||
4143 | $jetpack_home, |
||
4144 | array( 'settings' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack#/settings' ), __( 'Settings', 'jetpack' ) ) ), |
||
4145 | array( 'support' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack-debugger ' ), __( 'Support', 'jetpack' ) ) ), |
||
4146 | $actions |
||
4147 | ); |
||
4148 | } |
||
4149 | |||
4150 | return array_merge( $jetpack_home, $actions ); |
||
4151 | } |
||
4152 | |||
4153 | /** |
||
4154 | * Adds the deactivation warning modal if there are other active plugins using the connection |
||
4155 | * |
||
4156 | * @param string $hook The current admin page. |
||
4157 | * |
||
4158 | * @return void |
||
4159 | */ |
||
4160 | public function deactivate_dialog( $hook ) { |
||
4161 | if ( |
||
4162 | 'plugins.php' === $hook |
||
4163 | && self::is_connection_ready() |
||
4164 | ) { |
||
4165 | |||
4166 | $active_plugins_using_connection = Connection_Plugin_Storage::get_all(); |
||
4167 | |||
4168 | if ( count( $active_plugins_using_connection ) > 1 ) { |
||
4169 | |||
4170 | add_thickbox(); |
||
4171 | |||
4172 | // Register jp-tracks-functions dependency. |
||
4173 | Tracking::register_tracks_functions_scripts(); |
||
4174 | |||
4175 | wp_enqueue_script( |
||
4176 | 'jetpack-deactivate-dialog-js', |
||
4177 | Assets::get_file_url_for_environment( |
||
4178 | '_inc/build/jetpack-deactivate-dialog.min.js', |
||
4179 | '_inc/jetpack-deactivate-dialog.js' |
||
4180 | ), |
||
4181 | array( 'jquery', 'jp-tracks-functions' ), |
||
4182 | JETPACK__VERSION, |
||
4183 | true |
||
4184 | ); |
||
4185 | |||
4186 | wp_localize_script( |
||
4187 | 'jetpack-deactivate-dialog-js', |
||
4188 | 'deactivate_dialog', |
||
4189 | array( |
||
4190 | 'title' => __( 'Deactivate Jetpack', 'jetpack' ), |
||
4191 | 'deactivate_label' => __( 'Disconnect and Deactivate', 'jetpack' ), |
||
4192 | 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(), |
||
4193 | ) |
||
4194 | ); |
||
4195 | |||
4196 | add_action( 'admin_footer', array( $this, 'deactivate_dialog_content' ) ); |
||
4197 | |||
4198 | wp_enqueue_style( 'jetpack-deactivate-dialog', plugins_url( 'css/jetpack-deactivate-dialog.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); |
||
4199 | } |
||
4200 | } |
||
4201 | } |
||
4202 | |||
4203 | /** |
||
4204 | * Outputs the content of the deactivation modal |
||
4205 | * |
||
4206 | * @return void |
||
4207 | */ |
||
4208 | public function deactivate_dialog_content() { |
||
4213 | |||
4214 | /** |
||
4215 | * Filters the login URL to include the registration flow in case the user isn't logged in. |
||
4216 | * |
||
4217 | * @param string $login_url The wp-login URL. |
||
4218 | * @param string $redirect URL to redirect users after logging in. |
||
4219 | * @since Jetpack 8.4 |
||
4220 | * @return string |
||
4221 | */ |
||
4222 | public function login_url( $login_url, $redirect ) { |
||
4229 | |||
4230 | /** |
||
4231 | * Redirects non-authenticated users to authenticate with Calypso if redirect flag is set. |
||
4232 | * |
||
4233 | * @since Jetpack 8.4 |
||
4234 | */ |
||
4235 | public function login_init() { |
||
4252 | |||
4253 | /* |
||
4254 | * Registration flow: |
||
4255 | * 1 - ::admin_page_load() action=register |
||
4256 | * 2 - ::try_registration() |
||
4257 | * 3 - ::register() |
||
4258 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
4259 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
4260 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
4261 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
4262 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
4263 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
4264 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
4265 | * jetpack_id, jetpack_secret, jetpack_public |
||
4266 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
4267 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
4268 | * 5 - user logs in with WP.com account |
||
4269 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
4270 | * - Manager::authorize() |
||
4271 | * - Manager::get_token() |
||
4272 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
4273 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
4274 | * - which responds with access_token, token_type, scope |
||
4275 | * - Manager::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
4276 | * - Jetpack::activate_default_modules() |
||
4277 | * - Deactivates deprecated plugins |
||
4278 | * - Activates all default modules |
||
4279 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
4280 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
4281 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
4282 | * Done! |
||
4283 | */ |
||
4284 | |||
4285 | /** |
||
4286 | * Handles the page load events for the Jetpack admin page |
||
4287 | */ |
||
4288 | function admin_page_load() { |
||
4289 | $error = false; |
||
4290 | |||
4291 | // Make sure we have the right body class to hook stylings for subpages off of. |
||
4292 | add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ), 20 ); |
||
4293 | |||
4294 | if ( ! empty( $_GET['jetpack_restate'] ) ) { |
||
4295 | // Should only be used in intermediate redirects to preserve state across redirects |
||
4296 | self::restate(); |
||
4297 | } |
||
4298 | |||
4299 | if ( isset( $_GET['connect_url_redirect'] ) ) { |
||
4300 | // @todo: Add validation against a known allowed list. |
||
4301 | $from = ! empty( $_GET['from'] ) ? $_GET['from'] : 'iframe'; |
||
4302 | // User clicked in the iframe to link their accounts |
||
4303 | if ( ! self::connection()->is_user_connected() ) { |
||
4304 | $redirect = ! empty( $_GET['redirect_after_auth'] ) ? $_GET['redirect_after_auth'] : false; |
||
4305 | |||
4306 | add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) ); |
||
4307 | $connect_url = $this->build_connect_url( true, $redirect, $from ); |
||
4308 | remove_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) ); |
||
4309 | |||
4310 | if ( isset( $_GET['notes_iframe'] ) ) { |
||
4311 | $connect_url .= '¬es_iframe'; |
||
4312 | } |
||
4313 | wp_redirect( $connect_url ); |
||
4314 | exit; |
||
4315 | } else { |
||
4316 | if ( ! isset( $_GET['calypso_env'] ) ) { |
||
4317 | self::state( 'message', 'already_authorized' ); |
||
4318 | wp_safe_redirect( self::admin_url() ); |
||
4319 | exit; |
||
4320 | } else { |
||
4321 | $connect_url = $this->build_connect_url( true, false, $from ); |
||
4322 | $connect_url .= '&already_authorized=true'; |
||
4323 | wp_redirect( $connect_url ); |
||
4324 | exit; |
||
4325 | } |
||
4326 | } |
||
4327 | } |
||
4328 | |||
4329 | if ( isset( $_GET['action'] ) ) { |
||
4330 | switch ( $_GET['action'] ) { |
||
4331 | case 'authorize_redirect': |
||
4332 | self::log( 'authorize_redirect' ); |
||
4333 | |||
4334 | add_filter( |
||
4335 | 'allowed_redirect_hosts', |
||
4336 | function ( $domains ) { |
||
4337 | $domains[] = 'jetpack.com'; |
||
4338 | $domains[] = 'jetpack.wordpress.com'; |
||
4339 | $domains[] = 'wordpress.com'; |
||
4340 | $domains[] = wp_parse_url( static::get_calypso_host(), PHP_URL_HOST ); // May differ from `wordpress.com`. |
||
4341 | return array_unique( $domains ); |
||
4342 | } |
||
4343 | ); |
||
4344 | |||
4345 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
4346 | $dest_url = empty( $_GET['dest_url'] ) ? null : $_GET['dest_url']; |
||
4347 | |||
4348 | if ( ! $dest_url || ( 0 === stripos( $dest_url, 'https://jetpack.com/' ) && 0 === stripos( $dest_url, 'https://wordpress.com/' ) ) ) { |
||
4349 | // The destination URL is missing or invalid, nothing to do here. |
||
4350 | exit; |
||
4351 | } |
||
4352 | |||
4353 | if ( static::connection()->is_connected() && static::connection()->is_user_connected() ) { |
||
4354 | // The user is either already connected, or finished the connection process. |
||
4355 | wp_safe_redirect( $dest_url ); |
||
4356 | exit; |
||
4357 | } elseif ( ! empty( $_GET['done'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
||
4358 | // The user decided not to proceed with setting up the connection. |
||
4359 | wp_safe_redirect( self::admin_url( 'page=jetpack' ) ); |
||
4360 | exit; |
||
4361 | } |
||
4362 | |||
4363 | $redirect_args = array( |
||
4364 | 'page' => 'jetpack', |
||
4365 | 'action' => 'authorize_redirect', |
||
4366 | 'dest_url' => rawurlencode( $dest_url ), |
||
4367 | 'done' => '1', |
||
4368 | ); |
||
4369 | |||
4370 | if ( ! empty( $_GET['from'] ) && 'jetpack_site_only_checkout' === $_GET['from'] ) { |
||
4371 | $redirect_args['from'] = 'jetpack_site_only_checkout'; |
||
4372 | } |
||
4373 | |||
4374 | wp_safe_redirect( static::build_authorize_url( self::admin_url( $redirect_args ) ) ); |
||
4375 | exit; |
||
4376 | case 'authorize': |
||
4377 | _doing_it_wrong( __METHOD__, 'The `page=jetpack&action=authorize` webhook is deprecated. Use `handler=jetpack-connection-webhooks&action=authorize` instead', 'Jetpack 9.5.0' ); |
||
4378 | ( new Connection_Webhooks( $this->connection_manager ) )->handle_authorize(); |
||
4379 | exit; |
||
4380 | case 'register': |
||
4381 | if ( ! current_user_can( 'jetpack_connect' ) ) { |
||
4382 | $error = 'cheatin'; |
||
4383 | break; |
||
4384 | } |
||
4385 | check_admin_referer( 'jetpack-register' ); |
||
4386 | self::log( 'register' ); |
||
4387 | self::maybe_set_version_option(); |
||
4388 | $from = isset( $_GET['from'] ) ? $_GET['from'] : false; |
||
4389 | if ( $from ) { |
||
4390 | static::connection()->add_register_request_param( 'from', (string) $from ); |
||
4391 | } |
||
4392 | $registered = static::connection()->try_registration(); |
||
4393 | if ( is_wp_error( $registered ) ) { |
||
4394 | $error = $registered->get_error_code(); |
||
4395 | self::state( 'error', $error ); |
||
4396 | self::state( 'error', $registered->get_error_message() ); |
||
4397 | |||
4398 | /** |
||
4399 | * Jetpack registration Error. |
||
4400 | * |
||
4401 | * @since 7.5.0 |
||
4402 | * |
||
4403 | * @param string|int $error The error code. |
||
4404 | * @param \WP_Error $registered The error object. |
||
4405 | */ |
||
4406 | do_action( 'jetpack_connection_register_fail', $error, $registered ); |
||
4407 | break; |
||
4408 | } |
||
4409 | |||
4410 | $redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : false; |
||
4411 | |||
4412 | /** |
||
4413 | * Jetpack registration Success. |
||
4414 | * |
||
4415 | * @since 7.5.0 |
||
4416 | * |
||
4417 | * @param string $from 'from' GET parameter; |
||
4418 | */ |
||
4419 | do_action( 'jetpack_connection_register_success', $from ); |
||
4420 | |||
4421 | $url = $this->build_connect_url( true, $redirect, $from ); |
||
4422 | |||
4423 | if ( ! empty( $_GET['onboarding'] ) ) { |
||
4424 | $url = add_query_arg( 'onboarding', $_GET['onboarding'], $url ); |
||
4425 | } |
||
4426 | |||
4427 | if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) { |
||
4428 | $url = add_query_arg( 'auth_approved', 'true', $url ); |
||
4429 | } |
||
4430 | |||
4431 | wp_redirect( $url ); |
||
4432 | exit; |
||
4433 | case 'activate': |
||
4434 | if ( ! current_user_can( 'jetpack_activate_modules' ) ) { |
||
4435 | $error = 'cheatin'; |
||
4436 | break; |
||
4437 | } |
||
4438 | |||
4439 | $module = stripslashes( $_GET['module'] ); |
||
4440 | check_admin_referer( "jetpack_activate-$module" ); |
||
4441 | self::log( 'activate', $module ); |
||
4442 | if ( ! self::activate_module( $module ) ) { |
||
4443 | self::state( 'error', sprintf( __( 'Could not activate %s', 'jetpack' ), $module ) ); |
||
4444 | } |
||
4445 | // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. |
||
4446 | wp_safe_redirect( self::admin_url( 'page=jetpack' ) ); |
||
4447 | exit; |
||
4448 | case 'activate_default_modules': |
||
4449 | check_admin_referer( 'activate_default_modules' ); |
||
4450 | self::log( 'activate_default_modules' ); |
||
4451 | self::restate(); |
||
4452 | $min_version = isset( $_GET['min_version'] ) ? $_GET['min_version'] : false; |
||
4453 | $max_version = isset( $_GET['max_version'] ) ? $_GET['max_version'] : false; |
||
4454 | $other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? $_GET['other_modules'] : array(); |
||
4455 | self::activate_default_modules( $min_version, $max_version, $other_modules ); |
||
4456 | wp_safe_redirect( self::admin_url( 'page=jetpack' ) ); |
||
4457 | exit; |
||
4458 | View Code Duplication | case 'disconnect': |
|
4459 | if ( ! current_user_can( 'jetpack_disconnect' ) ) { |
||
4460 | $error = 'cheatin'; |
||
4461 | break; |
||
4462 | } |
||
4463 | |||
4464 | check_admin_referer( 'jetpack-disconnect' ); |
||
4465 | self::log( 'disconnect' ); |
||
4466 | self::disconnect(); |
||
4467 | wp_safe_redirect( self::admin_url( 'disconnected=true' ) ); |
||
4468 | exit; |
||
4469 | View Code Duplication | case 'reconnect': |
|
4470 | if ( ! current_user_can( 'jetpack_reconnect' ) ) { |
||
4471 | $error = 'cheatin'; |
||
4472 | break; |
||
4473 | } |
||
4474 | |||
4475 | check_admin_referer( 'jetpack-reconnect' ); |
||
4476 | self::log( 'reconnect' ); |
||
4477 | self::disconnect(); |
||
4478 | wp_redirect( $this->build_connect_url( true, false, 'reconnect' ) ); |
||
4479 | exit; |
||
4480 | View Code Duplication | case 'deactivate': |
|
4481 | if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { |
||
4482 | $error = 'cheatin'; |
||
4483 | break; |
||
4484 | } |
||
4485 | |||
4486 | $modules = stripslashes( $_GET['module'] ); |
||
4487 | check_admin_referer( "jetpack_deactivate-$modules" ); |
||
4488 | foreach ( explode( ',', $modules ) as $module ) { |
||
4489 | self::log( 'deactivate', $module ); |
||
4490 | self::deactivate_module( $module ); |
||
4491 | self::state( 'message', 'module_deactivated' ); |
||
4492 | } |
||
4493 | self::state( 'module', $modules ); |
||
4494 | wp_safe_redirect( self::admin_url( 'page=jetpack' ) ); |
||
4495 | exit; |
||
4496 | case 'unlink': |
||
4497 | $redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : ''; |
||
4498 | check_admin_referer( 'jetpack-unlink' ); |
||
4499 | self::log( 'unlink' ); |
||
4500 | $this->connection_manager->disconnect_user(); |
||
4501 | self::state( 'message', 'unlinked' ); |
||
4502 | if ( 'sub-unlink' == $redirect ) { |
||
4503 | wp_safe_redirect( admin_url() ); |
||
4504 | } else { |
||
4505 | wp_safe_redirect( self::admin_url( array( 'page' => $redirect ) ) ); |
||
4506 | } |
||
4507 | exit; |
||
4508 | case 'onboard': |
||
4509 | if ( ! current_user_can( 'manage_options' ) ) { |
||
4510 | wp_safe_redirect( self::admin_url( 'page=jetpack' ) ); |
||
4511 | } else { |
||
4512 | self::create_onboarding_token(); |
||
4513 | $url = $this->build_connect_url( true ); |
||
4514 | |||
4515 | if ( false !== ( $token = Jetpack_Options::get_option( 'onboarding' ) ) ) { |
||
4516 | $url = add_query_arg( 'onboarding', $token, $url ); |
||
4517 | } |
||
4518 | |||
4519 | $calypso_env = $this->get_calypso_env(); |
||
4520 | if ( ! empty( $calypso_env ) ) { |
||
4521 | $url = add_query_arg( 'calypso_env', $calypso_env, $url ); |
||
4522 | } |
||
4523 | |||
4524 | wp_redirect( $url ); |
||
4525 | exit; |
||
4526 | } |
||
4527 | exit; |
||
4528 | default: |
||
4529 | /** |
||
4530 | * Fires when a Jetpack admin page is loaded with an unrecognized parameter. |
||
4531 | * |
||
4532 | * @since 2.6.0 |
||
4533 | * |
||
4534 | * @param string sanitize_key( $_GET['action'] ) Unrecognized URL parameter. |
||
4535 | */ |
||
4536 | do_action( 'jetpack_unrecognized_action', sanitize_key( $_GET['action'] ) ); |
||
4537 | } |
||
4538 | } |
||
4539 | |||
4540 | if ( ! $error = $error ? $error : self::state( 'error' ) ) { |
||
4541 | self::activate_new_modules( true ); |
||
4542 | } |
||
4543 | |||
4544 | $message_code = self::state( 'message' ); |
||
4545 | if ( self::state( 'optin-manage' ) ) { |
||
4546 | $activated_manage = $message_code; |
||
4547 | $message_code = 'jetpack-manage'; |
||
4548 | } |
||
4549 | |||
4550 | switch ( $message_code ) { |
||
4551 | case 'jetpack-manage': |
||
4552 | $sites_url = esc_url( Redirect::get_url( 'calypso-sites' ) ); |
||
4553 | // translators: %s is the URL to the "Sites" panel on wordpress.com. |
||
4554 | $this->message = '<strong>' . sprintf( __( 'You are all set! Your site can now be managed from <a href="%s" target="_blank">wordpress.com/sites</a>.', 'jetpack' ), $sites_url ) . '</strong>'; |
||
4555 | if ( $activated_manage ) { |
||
4556 | $this->message .= '<br /><strong>' . __( 'Manage has been activated for you!', 'jetpack' ) . '</strong>'; |
||
4557 | } |
||
4558 | break; |
||
4559 | |||
4560 | } |
||
4561 | |||
4562 | $deactivated_plugins = self::state( 'deactivated_plugins' ); |
||
4563 | |||
4564 | if ( ! empty( $deactivated_plugins ) ) { |
||
4565 | $deactivated_plugins = explode( ',', $deactivated_plugins ); |
||
4566 | $deactivated_titles = array(); |
||
4567 | foreach ( $deactivated_plugins as $deactivated_plugin ) { |
||
4568 | if ( ! isset( $this->plugins_to_deactivate[ $deactivated_plugin ] ) ) { |
||
4569 | continue; |
||
4570 | } |
||
4571 | |||
4572 | $deactivated_titles[] = '<strong>' . str_replace( ' ', ' ', $this->plugins_to_deactivate[ $deactivated_plugin ][1] ) . '</strong>'; |
||
4573 | } |
||
4574 | |||
4575 | if ( $deactivated_titles ) { |
||
4576 | if ( $this->message ) { |
||
4577 | $this->message .= "<br /><br />\n"; |
||
4578 | } |
||
4579 | |||
4580 | $this->message .= wp_sprintf( |
||
4581 | _n( |
||
4582 | 'Jetpack contains the most recent version of the old %l plugin.', |
||
4583 | 'Jetpack contains the most recent versions of the old %l plugins.', |
||
4584 | count( $deactivated_titles ), |
||
4585 | 'jetpack' |
||
4586 | ), |
||
4587 | $deactivated_titles |
||
4588 | ); |
||
4589 | |||
4590 | $this->message .= "<br />\n"; |
||
4591 | |||
4592 | $this->message .= _n( |
||
4593 | 'The old version has been deactivated and can be removed from your site.', |
||
4594 | 'The old versions have been deactivated and can be removed from your site.', |
||
4595 | count( $deactivated_titles ), |
||
4596 | 'jetpack' |
||
4597 | ); |
||
4598 | } |
||
4599 | } |
||
4600 | |||
4601 | $this->privacy_checks = self::state( 'privacy_checks' ); |
||
4602 | |||
4603 | if ( $this->message || $this->error || $this->privacy_checks ) { |
||
4604 | add_action( 'jetpack_notices', array( $this, 'admin_notices' ) ); |
||
4605 | } |
||
4606 | |||
4607 | add_filter( 'jetpack_short_module_description', 'wptexturize' ); |
||
4608 | } |
||
4609 | |||
4610 | function admin_notices() { |
||
4735 | |||
4736 | /** |
||
4737 | * We can't always respond to a signed XML-RPC request with a |
||
4738 | * helpful error message. In some circumstances, doing so could |
||
4739 | * leak information. |
||
4740 | * |
||
4741 | * Instead, track that the error occurred via a Jetpack_Option, |
||
4742 | * and send that data back in the heartbeat. |
||
4743 | * All this does is increment a number, but it's enough to find |
||
4744 | * trends. |
||
4745 | * |
||
4746 | * @param WP_Error $xmlrpc_error The error produced during |
||
4747 | * signature validation. |
||
4748 | */ |
||
4749 | function track_xmlrpc_error( $xmlrpc_error ) { |
||
4764 | |||
4765 | /** |
||
4766 | * Initialize the jetpack stats instance only when needed |
||
4767 | * |
||
4768 | * @return void |
||
4769 | */ |
||
4770 | private function initialize_stats() { |
||
4775 | |||
4776 | /** |
||
4777 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4778 | */ |
||
4779 | function stat( $group, $detail ) { |
||
4786 | |||
4787 | /** |
||
4788 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4789 | */ |
||
4790 | function do_stats( $method = '' ) { |
||
4801 | |||
4802 | /** |
||
4803 | * Runs stats code for a one-off, server-side. |
||
4804 | * |
||
4805 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4806 | * |
||
4807 | * @return bool If it worked. |
||
4808 | */ |
||
4809 | static function do_server_side_stat( $args ) { |
||
4814 | |||
4815 | /** |
||
4816 | * Builds the stats url. |
||
4817 | * |
||
4818 | * @param $args array|string The arguments to append to the URL. |
||
4819 | * |
||
4820 | * @return string The URL to be pinged. |
||
4821 | */ |
||
4822 | static function build_stats_url( $args ) { |
||
4828 | |||
4829 | /** |
||
4830 | * Builds a URL to the Jetpack connection auth page |
||
4831 | * |
||
4832 | * @since 3.9.5 |
||
4833 | * |
||
4834 | * @param bool $raw If true, URL will not be escaped. |
||
4835 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4836 | * If string, will be a custom redirect. |
||
4837 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4838 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
4839 | * |
||
4840 | * @return string Connect URL |
||
4841 | */ |
||
4842 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
4904 | |||
4905 | public static function build_authorize_url( $redirect = false, $iframe = false ) { |
||
4933 | |||
4934 | /** |
||
4935 | * Filters the connection URL parameter array. |
||
4936 | * |
||
4937 | * @param array $args default URL parameters used by the package. |
||
4938 | * @return array the modified URL arguments array. |
||
4939 | */ |
||
4940 | public static function filter_connect_request_body( $args ) { |
||
4970 | |||
4971 | /** |
||
4972 | * Filters the URL that will process the connection data. It can be different from the URL |
||
4973 | * that we send the user to after everything is done. |
||
4974 | * |
||
4975 | * @param String $processing_url the default redirect URL used by the package. |
||
4976 | * @return String the modified URL. |
||
4977 | * |
||
4978 | * @deprecated since Jetpack 9.5.0 |
||
4979 | */ |
||
4980 | public static function filter_connect_processing_url( $processing_url ) { |
||
4986 | |||
4987 | /** |
||
4988 | * Filters the redirection URL that is used for connect requests. The redirect |
||
4989 | * URL should return the user back to the Jetpack console. |
||
4990 | * |
||
4991 | * @param String $redirect the default redirect URL used by the package. |
||
4992 | * @return String the modified URL. |
||
4993 | */ |
||
4994 | public static function filter_connect_redirect_url( $redirect ) { |
||
5006 | |||
5007 | /** |
||
5008 | * This action fires at the beginning of the Manager::authorize method. |
||
5009 | */ |
||
5010 | public static function authorize_starting() { |
||
5034 | |||
5035 | /** |
||
5036 | * This action fires when the site is registered (connected at a site level). |
||
5037 | */ |
||
5038 | public function handle_unique_registrations_stats() { |
||
5053 | |||
5054 | /** |
||
5055 | * This action fires at the end of the Manager::authorize method when a secondary user is |
||
5056 | * linked. |
||
5057 | */ |
||
5058 | public static function authorize_ending_linked() { |
||
5062 | |||
5063 | /** |
||
5064 | * This action fires at the end of the Manager::authorize method when the master user is |
||
5065 | * authorized. |
||
5066 | * |
||
5067 | * @param array $data The request data. |
||
5068 | */ |
||
5069 | public static function authorize_ending_authorized( $data ) { |
||
5089 | |||
5090 | /** |
||
5091 | * Fires on the jetpack_site_registered hook and acitvates default modules |
||
5092 | */ |
||
5093 | public static function activate_default_modules_on_site_register() { |
||
5108 | |||
5109 | /** |
||
5110 | * This action fires at the end of the REST_Connector connection_reconnect method when the |
||
5111 | * reconnect process is completed. |
||
5112 | * Note that this currently only happens when we don't need the user to re-authorize |
||
5113 | * their WP.com account, eg in cases where we are restoring a connection with |
||
5114 | * unhealthy blog token. |
||
5115 | */ |
||
5116 | public static function reconnection_completed() { |
||
5119 | |||
5120 | /** |
||
5121 | * Get our assumed site creation date. |
||
5122 | * Calculated based on the earlier date of either: |
||
5123 | * - Earliest admin user registration date. |
||
5124 | * - Earliest date of post of any post type. |
||
5125 | * |
||
5126 | * @since 7.2.0 |
||
5127 | * @deprecated since 7.8.0 |
||
5128 | * |
||
5129 | * @return string Assumed site creation date and time. |
||
5130 | */ |
||
5131 | public static function get_assumed_site_creation_date() { |
||
5135 | |||
5136 | View Code Duplication | public static function apply_activation_source_to_args( &$args ) { |
|
5147 | |||
5148 | function build_reconnect_url( $raw = false ) { |
||
5152 | |||
5153 | public static function admin_url( $args = null ) { |
||
5158 | |||
5159 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
5163 | |||
5164 | function dismiss_jetpack_notice() { |
||
5181 | |||
5182 | public static function sort_modules( $a, $b ) { |
||
5189 | |||
5190 | function ajax_recheck_ssl() { |
||
5200 | |||
5201 | /* Client API */ |
||
5202 | |||
5203 | /** |
||
5204 | * Returns the requested Jetpack API URL |
||
5205 | * |
||
5206 | * @deprecated since 7.7 |
||
5207 | * @return string |
||
5208 | */ |
||
5209 | public static function api_url( $relative_url ) { |
||
5214 | |||
5215 | /** |
||
5216 | * @deprecated 8.0 |
||
5217 | * |
||
5218 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requests. |
||
5219 | * But we no longer fix "bad hosts" anyway, outbound HTTPS is required for Jetpack to function. |
||
5220 | */ |
||
5221 | public static function fix_url_for_bad_hosts( $url ) { |
||
5225 | |||
5226 | public static function verify_onboarding_token( $token_data, $token, $request_data ) { |
||
5271 | |||
5272 | /** |
||
5273 | * Create a random secret for validating onboarding payload |
||
5274 | * |
||
5275 | * @return string Secret token |
||
5276 | */ |
||
5277 | public static function create_onboarding_token() { |
||
5285 | |||
5286 | /** |
||
5287 | * Remove the onboarding token |
||
5288 | * |
||
5289 | * @return bool True on success, false on failure |
||
5290 | */ |
||
5291 | public static function invalidate_onboarding_token() { |
||
5294 | |||
5295 | /** |
||
5296 | * Validate an onboarding token for a specific action |
||
5297 | * |
||
5298 | * @return boolean True if token/action pair is accepted, false if not |
||
5299 | */ |
||
5300 | public static function validate_onboarding_token_action( $token, $action ) { |
||
5318 | |||
5319 | /** |
||
5320 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
5321 | * |
||
5322 | * @since 2.3.3 |
||
5323 | * @return boolean |
||
5324 | */ |
||
5325 | public static function permit_ssl( $force_recheck = false ) { |
||
5354 | |||
5355 | /* |
||
5356 | * Displays an admin_notice, alerting the user that outbound SSL isn't working. |
||
5357 | */ |
||
5358 | public function alert_auto_ssl_fail() { |
||
5412 | |||
5413 | /** |
||
5414 | * Returns the Jetpack XML-RPC API |
||
5415 | * |
||
5416 | * @deprecated 8.0 Use Connection_Manager instead. |
||
5417 | * @return string |
||
5418 | */ |
||
5419 | public static function xmlrpc_api_url() { |
||
5423 | |||
5424 | /** |
||
5425 | * Returns the connection manager object. |
||
5426 | * |
||
5427 | * @return Automattic\Jetpack\Connection\Manager |
||
5428 | */ |
||
5429 | public static function connection() { |
||
5439 | |||
5440 | /** |
||
5441 | * Creates two secret tokens and the end of life timestamp for them. |
||
5442 | * |
||
5443 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
5444 | * |
||
5445 | * @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->generate() instead. |
||
5446 | * |
||
5447 | * @since 2.6 |
||
5448 | * @param String $action The action name. |
||
5449 | * @param Integer $user_id The user identifier. |
||
5450 | * @param Integer $exp Expiration time in seconds. |
||
5451 | * @return array |
||
5452 | */ |
||
5453 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
5457 | |||
5458 | public static function get_secrets( $action, $user_id ) { |
||
5471 | |||
5472 | /** |
||
5473 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5474 | * |
||
5475 | * Based on local php max_execution_time in php.ini |
||
5476 | * |
||
5477 | * @since 2.6 |
||
5478 | * @return int |
||
5479 | * @deprecated |
||
5480 | **/ |
||
5481 | public function get_remote_query_timeout_limit() { |
||
5485 | |||
5486 | /** |
||
5487 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5488 | * |
||
5489 | * Based on local php max_execution_time in php.ini |
||
5490 | * |
||
5491 | * @since 5.4 |
||
5492 | * @return int |
||
5493 | **/ |
||
5494 | public static function get_max_execution_time() { |
||
5503 | |||
5504 | /** |
||
5505 | * Sets a minimum request timeout, and returns the current timeout |
||
5506 | * |
||
5507 | * @since 5.4 |
||
5508 | **/ |
||
5509 | View Code Duplication | public static function set_min_time_limit( $min_timeout ) { |
|
5517 | |||
5518 | /** |
||
5519 | * Takes the response from the Jetpack register new site endpoint and |
||
5520 | * verifies it worked properly. |
||
5521 | * |
||
5522 | * @since 2.6 |
||
5523 | * @deprecated since 7.7.0 |
||
5524 | * @see Automattic\Jetpack\Connection\Manager::validate_remote_register_response() |
||
5525 | **/ |
||
5526 | public function validate_remote_register_response() { |
||
5529 | |||
5530 | /** |
||
5531 | * @deprecated since Jetpack 9.7.0 |
||
5532 | * @see Automattic\Jetpack\Connection\Manager::try_registration() |
||
5533 | * |
||
5534 | * @return bool|WP_Error |
||
5535 | */ |
||
5536 | public static function register() { |
||
5540 | |||
5541 | /** |
||
5542 | * Filters the registration request body to include tracking properties. |
||
5543 | * |
||
5544 | * @deprecated since Jetpack 9.7.0 |
||
5545 | * @see Automattic\Jetpack\Connection\Utils::filter_register_request_body() |
||
5546 | * |
||
5547 | * @param array $properties |
||
5548 | * @return array amended properties. |
||
5549 | */ |
||
5550 | public static function filter_register_request_body( $properties ) { |
||
5554 | |||
5555 | /** |
||
5556 | * Filters the token request body to include tracking properties. |
||
5557 | * |
||
5558 | * @param array $properties |
||
5559 | * @return array amended properties. |
||
5560 | */ |
||
5561 | View Code Duplication | public static function filter_token_request_body( $properties ) { |
|
5573 | |||
5574 | /** |
||
5575 | * If the db version is showing something other that what we've got now, bump it to current. |
||
5576 | * |
||
5577 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
5578 | */ |
||
5579 | public static function maybe_set_version_option() { |
||
5593 | |||
5594 | /* Client Server API */ |
||
5595 | |||
5596 | /** |
||
5597 | * Loads the Jetpack XML-RPC client. |
||
5598 | * No longer necessary, as the XML-RPC client will be automagically loaded. |
||
5599 | * |
||
5600 | * @deprecated since 7.7.0 |
||
5601 | */ |
||
5602 | public static function load_xml_rpc_client() { |
||
5605 | |||
5606 | /** |
||
5607 | * Resets the saved authentication state in between testing requests. |
||
5608 | * |
||
5609 | * @deprecated since 8.9.0 |
||
5610 | * @see Automattic\Jetpack\Connection\Rest_Authentication::reset_saved_auth_state() |
||
5611 | */ |
||
5612 | public function reset_saved_auth_state() { |
||
5616 | |||
5617 | /** |
||
5618 | * Verifies the signature of the current request. |
||
5619 | * |
||
5620 | * @deprecated since 7.7.0 |
||
5621 | * @see Automattic\Jetpack\Connection\Manager::verify_xml_rpc_signature() |
||
5622 | * |
||
5623 | * @return false|array |
||
5624 | */ |
||
5625 | public function verify_xml_rpc_signature() { |
||
5629 | |||
5630 | /** |
||
5631 | * Verifies the signature of the current request. |
||
5632 | * |
||
5633 | * This function has side effects and should not be used. Instead, |
||
5634 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
5635 | * |
||
5636 | * @deprecated since 7.7.0 |
||
5637 | * @see Automattic\Jetpack\Connection\Manager::internal_verify_xml_rpc_signature() |
||
5638 | * @internal |
||
5639 | */ |
||
5640 | private function internal_verify_xml_rpc_signature() { |
||
5643 | |||
5644 | /** |
||
5645 | * Authenticates XML-RPC and other requests from the Jetpack Server. |
||
5646 | * |
||
5647 | * @deprecated since 7.7.0 |
||
5648 | * @see Automattic\Jetpack\Connection\Manager::authenticate_jetpack() |
||
5649 | * |
||
5650 | * @param \WP_User|mixed $user User object if authenticated. |
||
5651 | * @param string $username Username. |
||
5652 | * @param string $password Password string. |
||
5653 | * @return \WP_User|mixed Authenticated user or error. |
||
5654 | */ |
||
5655 | View Code Duplication | public function authenticate_jetpack( $user, $username, $password ) { |
|
5664 | |||
5665 | /** |
||
5666 | * Authenticates requests from Jetpack server to WP REST API endpoints. |
||
5667 | * Uses the existing XMLRPC request signing implementation. |
||
5668 | * |
||
5669 | * @deprecated since 8.9.0 |
||
5670 | * @see Automattic\Jetpack\Connection\Rest_Authentication::wp_rest_authenticate() |
||
5671 | */ |
||
5672 | function wp_rest_authenticate( $user ) { |
||
5676 | |||
5677 | /** |
||
5678 | * Report authentication status to the WP REST API. |
||
5679 | * |
||
5680 | * @deprecated since 8.9.0 |
||
5681 | * @see Automattic\Jetpack\Connection\Rest_Authentication::wp_rest_authentication_errors() |
||
5682 | * |
||
5683 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5684 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5685 | */ |
||
5686 | public function wp_rest_authentication_errors( $value ) { |
||
5690 | |||
5691 | /** |
||
5692 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5693 | * Capture it here so we can verify the signature later. |
||
5694 | * |
||
5695 | * @deprecated since 7.7.0 |
||
5696 | * @see Automattic\Jetpack\Connection\Manager::xmlrpc_methods() |
||
5697 | * |
||
5698 | * @param array $methods XMLRPC methods. |
||
5699 | * @return array XMLRPC methods, with the $HTTP_RAW_POST_DATA one. |
||
5700 | */ |
||
5701 | View Code Duplication | public function xmlrpc_methods( $methods ) { |
|
5710 | |||
5711 | /** |
||
5712 | * Register additional public XMLRPC methods. |
||
5713 | * |
||
5714 | * @deprecated since 7.7.0 |
||
5715 | * @see Automattic\Jetpack\Connection\Manager::public_xmlrpc_methods() |
||
5716 | * |
||
5717 | * @param array $methods Public XMLRPC methods. |
||
5718 | * @return array Public XMLRPC methods, with the getOptions one. |
||
5719 | */ |
||
5720 | View Code Duplication | public function public_xmlrpc_methods( $methods ) { |
|
5729 | |||
5730 | /** |
||
5731 | * Handles a getOptions XMLRPC method call. |
||
5732 | * |
||
5733 | * @deprecated since 7.7.0 |
||
5734 | * @see Automattic\Jetpack\Connection\Manager::jetpack_getOptions() |
||
5735 | * |
||
5736 | * @param array $args method call arguments. |
||
5737 | * @return array an amended XMLRPC server options array. |
||
5738 | */ |
||
5739 | View Code Duplication | public function jetpack_getOptions( $args ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
|
5748 | |||
5749 | /** |
||
5750 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
5751 | * |
||
5752 | * @deprecated since 7.7.0 |
||
5753 | * @see Automattic\Jetpack\Connection\Manager::xmlrpc_options() |
||
5754 | * |
||
5755 | * @param array $options Standard Core options. |
||
5756 | * @return array Amended options. |
||
5757 | */ |
||
5758 | View Code Duplication | public function xmlrpc_options( $options ) { |
|
5767 | |||
5768 | /** |
||
5769 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5770 | * SET: state( $key, $value ); |
||
5771 | * GET: $value = state( $key ); |
||
5772 | * |
||
5773 | * @param string $key |
||
5774 | * @param string $value |
||
5775 | * @param bool $restate private |
||
5776 | */ |
||
5777 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5833 | |||
5834 | public static function restate() { |
||
5837 | |||
5838 | /** |
||
5839 | * Determines whether the jetpackState[$key] value should be added to the |
||
5840 | * cookie. |
||
5841 | * |
||
5842 | * @param string $key The state key. |
||
5843 | * |
||
5844 | * @return boolean Whether the value should be added to the cookie. |
||
5845 | */ |
||
5846 | public static function should_set_cookie( $key ) { |
||
5856 | |||
5857 | public static function check_privacy( $file ) { |
||
5891 | |||
5892 | /** |
||
5893 | * Helper method for multicall XMLRPC. |
||
5894 | * |
||
5895 | * @deprecated since 8.9.0 |
||
5896 | * @see Automattic\\Jetpack\\Connection\\Xmlrpc_Async_Call::add_call() |
||
5897 | * |
||
5898 | * @param ...$args Args for the async_call. |
||
5899 | */ |
||
5900 | public static function xmlrpc_async_call( ...$args ) { |
||
5942 | |||
5943 | /** |
||
5944 | * Serve a WordPress.com static resource via a randomized wp.com subdomain. |
||
5945 | * |
||
5946 | * @deprecated 9.3.0 Use Assets::staticize_subdomain. |
||
5947 | * |
||
5948 | * @param string $url WordPress.com static resource URL. |
||
5949 | */ |
||
5950 | public static function staticize_subdomain( $url ) { |
||
5954 | |||
5955 | /* JSON API Authorization */ |
||
5956 | |||
5957 | /** |
||
5958 | * Handles the login action for Authorizing the JSON API |
||
5959 | */ |
||
5960 | function login_form_json_api_authorization() { |
||
5969 | |||
5970 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
5971 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
5985 | |||
5986 | // Make sure the POSTed request is handled by the same action |
||
5987 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
5991 | |||
5992 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
5993 | function store_json_api_authorization_token( $user_login, $user ) { |
||
5999 | |||
6000 | // Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access. |
||
6001 | function allow_wpcom_public_api_domain( $domains ) { |
||
6005 | |||
6006 | static function is_redirect_encoded( $redirect_url ) { |
||
6009 | |||
6010 | // Add all wordpress.com environments to the safe redirect allowed list. |
||
6011 | function allow_wpcom_environments( $domains ) { |
||
6018 | |||
6019 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
6020 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
6032 | |||
6033 | /** |
||
6034 | * Verifies the request by checking the signature |
||
6035 | * |
||
6036 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
6037 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
6038 | * |
||
6039 | * @param null|array $environment |
||
6040 | */ |
||
6041 | function verify_json_api_authorization_request( $environment = null ) { |
||
6162 | |||
6163 | function login_message_json_api_authorization( $message ) { |
||
6169 | |||
6170 | /** |
||
6171 | * Get $content_width, but with a <s>twist</s> filter. |
||
6172 | */ |
||
6173 | public static function get_content_width() { |
||
6186 | |||
6187 | /** |
||
6188 | * Pings the WordPress.com Mirror Site for the specified options. |
||
6189 | * |
||
6190 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
6191 | * |
||
6192 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
6193 | */ |
||
6194 | public function get_cloud_site_options( $option_names ) { |
||
6209 | |||
6210 | /** |
||
6211 | * Checks if the site is currently in an identity crisis. |
||
6212 | * |
||
6213 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
6214 | */ |
||
6215 | public static function check_identity_crisis() { |
||
6222 | |||
6223 | /** |
||
6224 | * Checks whether the home and siteurl specifically are allowed. |
||
6225 | * Written so that we don't have re-check $key and $value params every time |
||
6226 | * we want to check if this site is allowed, for example in footer.php |
||
6227 | * |
||
6228 | * @since 3.8.0 |
||
6229 | * @return bool True = already allowed False = not on the allowed list. |
||
6230 | */ |
||
6231 | public static function is_staging_site() { |
||
6235 | |||
6236 | /** |
||
6237 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
6238 | * |
||
6239 | * @since 4.4.0 |
||
6240 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
6241 | * |
||
6242 | * @return bool |
||
6243 | */ |
||
6244 | public static function validate_sync_error_idc_option() { |
||
6248 | |||
6249 | /** |
||
6250 | * Normalizes a url by doing three things: |
||
6251 | * - Strips protocol |
||
6252 | * - Strips www |
||
6253 | * - Adds a trailing slash |
||
6254 | * |
||
6255 | * @since 4.4.0 |
||
6256 | * @param string $url |
||
6257 | * @return WP_Error|string |
||
6258 | */ |
||
6259 | View Code Duplication | public static function normalize_url_protocol_agnostic( $url ) { |
|
6269 | |||
6270 | /** |
||
6271 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
6272 | * |
||
6273 | * @since 4.4.0 |
||
6274 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
6275 | * |
||
6276 | * @param array $response |
||
6277 | * @return array Array of the local urls, wpcom urls, and error code |
||
6278 | */ |
||
6279 | public static function get_sync_error_idc_option( $response = array() ) { |
||
6283 | |||
6284 | /** |
||
6285 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
6286 | * If set to true, the site will be put into staging mode. |
||
6287 | * |
||
6288 | * @since 4.3.2 |
||
6289 | * @return bool |
||
6290 | */ |
||
6291 | public static function sync_idc_optin() { |
||
6295 | |||
6296 | /** |
||
6297 | * Maybe Use a .min.css stylesheet, maybe not. |
||
6298 | * |
||
6299 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
6300 | */ |
||
6301 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
6343 | |||
6344 | /** |
||
6345 | * If the asset is minified, let's flag .min as the suffix. |
||
6346 | * |
||
6347 | * Attached to `style_loader_src` filter. |
||
6348 | * |
||
6349 | * @param string $tag The tag that would link to the external asset. |
||
6350 | * @param string $handle The registered handle of the script in question. |
||
6351 | * @param string $href The url of the asset in question. |
||
6352 | */ |
||
6353 | public static function set_suffix_on_min( $src, $handle ) { |
||
6369 | |||
6370 | /** |
||
6371 | * Maybe inlines a stylesheet. |
||
6372 | * |
||
6373 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
6374 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
6375 | * |
||
6376 | * Attached to `style_loader_tag` filter. |
||
6377 | * |
||
6378 | * @param string $tag The tag that would link to the external asset. |
||
6379 | * @param string $handle The registered handle of the script in question. |
||
6380 | * |
||
6381 | * @return string |
||
6382 | */ |
||
6383 | public static function maybe_inline_style( $tag, $handle ) { |
||
6433 | |||
6434 | /** |
||
6435 | * Loads a view file from the views |
||
6436 | * |
||
6437 | * Data passed in with the $data parameter will be available in the |
||
6438 | * template file as $data['value'] |
||
6439 | * |
||
6440 | * @param string $template - Template file to load |
||
6441 | * @param array $data - Any data to pass along to the template |
||
6442 | * @return boolean - If template file was found |
||
6443 | **/ |
||
6444 | public function load_view( $template, $data = array() ) { |
||
6455 | |||
6456 | /** |
||
6457 | * Throws warnings for deprecated hooks to be removed from Jetpack that cannot remain in the original place in the code. |
||
6458 | */ |
||
6459 | public function deprecated_hooks() { |
||
6716 | |||
6717 | /** |
||
6718 | * Converts any url in a stylesheet, to the correct absolute url. |
||
6719 | * |
||
6720 | * Considerations: |
||
6721 | * - Normal, relative URLs `feh.png` |
||
6722 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
6723 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
6724 | * - Absolute URLs `http://domain.com/feh.png` |
||
6725 | * - Domain root relative URLs `/feh.png` |
||
6726 | * |
||
6727 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
6728 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
6729 | * |
||
6730 | * @return mixed|string |
||
6731 | */ |
||
6732 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6776 | |||
6777 | /** |
||
6778 | * This methods removes all of the registered css files on the front end |
||
6779 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6780 | * all the files into one file. |
||
6781 | * |
||
6782 | * Pros: |
||
6783 | * - Uses only ONE css asset connection instead of 15 |
||
6784 | * - Saves a minimum of 56k |
||
6785 | * - Reduces server load |
||
6786 | * - Reduces time to first painted byte |
||
6787 | * |
||
6788 | * Cons: |
||
6789 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6790 | * should not cause any issues with themes. |
||
6791 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6792 | * jetpack_implode_frontend_css filter for a workaround |
||
6793 | * |
||
6794 | * For some situations developers may wish to disable css imploding and |
||
6795 | * instead operate in legacy mode where each file loads seperately and |
||
6796 | * can be edited individually or dequeued. This can be accomplished with |
||
6797 | * the following line: |
||
6798 | * |
||
6799 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6800 | * |
||
6801 | * @since 3.2 |
||
6802 | **/ |
||
6803 | public function implode_frontend_css( $travis_test = false ) { |
||
6860 | |||
6861 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
6871 | |||
6872 | /** |
||
6873 | * @deprecated |
||
6874 | * @see Automattic\Jetpack\Assets\add_aync_script |
||
6875 | */ |
||
6876 | public function script_add_async( $tag, $handle, $src ) { |
||
6879 | |||
6880 | /* |
||
6881 | * Check the heartbeat data |
||
6882 | * |
||
6883 | * Organizes the heartbeat data by severity. For example, if the site |
||
6884 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
6885 | * |
||
6886 | * Data will be added to "caution" array, if it either: |
||
6887 | * - Out of date Jetpack version |
||
6888 | * - Out of date WP version |
||
6889 | * - Out of date PHP version |
||
6890 | * |
||
6891 | * $return array $filtered_data |
||
6892 | */ |
||
6893 | public static function jetpack_check_heartbeat_data() { |
||
6946 | |||
6947 | /* |
||
6948 | * This method is used to organize all options that can be reset |
||
6949 | * without disconnecting Jetpack. |
||
6950 | * |
||
6951 | * It is used in class.jetpack-cli.php to reset options |
||
6952 | * |
||
6953 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
6954 | * |
||
6955 | * @return array of options to delete. |
||
6956 | */ |
||
6957 | public static function get_jetpack_options_for_reset() { |
||
6960 | |||
6961 | /* |
||
6962 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
6963 | * so we can bring them directly to their site in calypso. |
||
6964 | * |
||
6965 | * @deprecated 9.2.0 Use Automattic\Jetpack\Status::get_site_suffix |
||
6966 | * |
||
6967 | * @param string | url |
||
6968 | * @return string | url without the guff |
||
6969 | */ |
||
6970 | public static function build_raw_urls( $url ) { |
||
6975 | |||
6976 | /** |
||
6977 | * Stores and prints out domains to prefetch for page speed optimization. |
||
6978 | * |
||
6979 | * @deprecated 8.8.0 Use Jetpack::add_resource_hints. |
||
6980 | * |
||
6981 | * @param string|array $urls URLs to hint. |
||
6982 | */ |
||
6983 | public static function dns_prefetch( $urls = null ) { |
||
6989 | |||
6990 | public function wp_dashboard_setup() { |
||
7028 | |||
7029 | /** |
||
7030 | * @param mixed $result Value for the user's option |
||
7031 | * @return mixed |
||
7032 | */ |
||
7033 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
7058 | |||
7059 | public static function dashboard_widget() { |
||
7067 | |||
7068 | public static function dashboard_widget_footer() { |
||
7136 | |||
7137 | /* |
||
7138 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
7139 | */ |
||
7140 | function jetpack_icon_user_connected( $columns ) { |
||
7144 | |||
7145 | /* |
||
7146 | * Show Jetpack icon if the user is linked. |
||
7147 | */ |
||
7148 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
7161 | |||
7162 | /* |
||
7163 | * Style the Jetpack user column |
||
7164 | */ |
||
7165 | function jetpack_user_col_style() { |
||
7184 | |||
7185 | /** |
||
7186 | * Checks if Akismet is active and working. |
||
7187 | * |
||
7188 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
7189 | * that implied usage of methods present since more recent version. |
||
7190 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
7191 | * |
||
7192 | * @since 5.1.0 |
||
7193 | * |
||
7194 | * @return bool True = Akismet available. False = Aksimet not available. |
||
7195 | */ |
||
7196 | public static function is_akismet_active() { |
||
7231 | |||
7232 | /** |
||
7233 | * @deprecated |
||
7234 | * |
||
7235 | * @see Automattic\Jetpack\Sync\Modules\Users::is_function_in_backtrace |
||
7236 | */ |
||
7237 | public static function is_function_in_backtrace() { |
||
7240 | |||
7241 | /** |
||
7242 | * Given a minified path, and a non-minified path, will return |
||
7243 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
7244 | * |
||
7245 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
7246 | * root Jetpack directory. |
||
7247 | * |
||
7248 | * @since 5.6.0 |
||
7249 | * |
||
7250 | * @param string $min_path |
||
7251 | * @param string $non_min_path |
||
7252 | * @return string The URL to the file |
||
7253 | */ |
||
7254 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
7257 | |||
7258 | /** |
||
7259 | * Checks for whether Jetpack Backup is enabled. |
||
7260 | * Will return true if the state of Backup is anything except "unavailable". |
||
7261 | * |
||
7262 | * @return bool|int|mixed |
||
7263 | */ |
||
7264 | public static function is_rewind_enabled() { |
||
7284 | |||
7285 | /** |
||
7286 | * Return Calypso environment value; used for developing Jetpack and pairing |
||
7287 | * it with different Calypso enrionments, such as localhost. |
||
7288 | * |
||
7289 | * @since 7.4.0 |
||
7290 | * |
||
7291 | * @return string Calypso environment |
||
7292 | */ |
||
7293 | public static function get_calypso_env() { |
||
7308 | |||
7309 | /** |
||
7310 | * Returns the hostname with protocol for Calypso. |
||
7311 | * Used for developing Jetpack with Calypso. |
||
7312 | * |
||
7313 | * @since 8.4.0 |
||
7314 | * |
||
7315 | * @return string Calypso host. |
||
7316 | */ |
||
7317 | public static function get_calypso_host() { |
||
7330 | |||
7331 | /** |
||
7332 | * Handles activating default modules as well general cleanup for the new connection. |
||
7333 | * |
||
7334 | * @param boolean $activate_sso Whether to activate the SSO module when activating default modules. |
||
7335 | * @param boolean $redirect_on_activation_error Whether to redirect on activation error. |
||
7336 | * @param boolean $send_state_messages Whether to send state messages. |
||
7337 | * @return void |
||
7338 | */ |
||
7339 | public static function handle_post_authorization_actions( |
||
7367 | |||
7368 | /** |
||
7369 | * Returns a boolean for whether backups UI should be displayed or not. |
||
7370 | * |
||
7371 | * @return bool Should backups UI be displayed? |
||
7372 | */ |
||
7373 | public static function show_backups_ui() { |
||
7383 | |||
7384 | /* |
||
7385 | * Deprecated manage functions |
||
7386 | */ |
||
7387 | function prepare_manage_jetpack_notice() { |
||
7408 | |||
7409 | /** |
||
7410 | * Clean leftoveruser meta. |
||
7411 | * |
||
7412 | * Delete Jetpack-related user meta when it is no longer needed. |
||
7413 | * |
||
7414 | * @since 7.3.0 |
||
7415 | * |
||
7416 | * @param int $user_id User ID being updated. |
||
7417 | */ |
||
7418 | public static function user_meta_cleanup( $user_id ) { |
||
7433 | |||
7434 | /** |
||
7435 | * Checks if a Jetpack site is both active and not in offline mode. |
||
7436 | * |
||
7437 | * This is a DRY function to avoid repeating `Jetpack::is_active && ! Automattic\Jetpack\Status->is_offline_mode`. |
||
7438 | * |
||
7439 | * @deprecated 8.8.0 |
||
7440 | * |
||
7441 | * @return bool True if Jetpack is active and not in offline mode. |
||
7442 | */ |
||
7443 | public static function is_active_and_not_development_mode() { |
||
7450 | |||
7451 | /** |
||
7452 | * Checks if a Jetpack site is both active and not in offline mode. |
||
7453 | * |
||
7454 | * This is a DRY function to avoid repeating `Jetpack::is_connection_ready && ! Automattic\Jetpack\Status->is_offline_mode`. |
||
7455 | * |
||
7456 | * @since 8.8.0 |
||
7457 | * |
||
7458 | * @return bool True if Jetpack is active and not in offline mode. |
||
7459 | */ |
||
7460 | public static function is_active_and_not_offline_mode() { |
||
7466 | |||
7467 | /** |
||
7468 | * Returns the list of products that we have available for purchase. |
||
7469 | */ |
||
7470 | public static function get_products_for_purchase() { |
||
7564 | |||
7565 | /** |
||
7566 | * Determine if the current user is allowed to make Jetpack purchases without |
||
7567 | * a WordPress.com account |
||
7568 | * |
||
7569 | * @return boolean True if the user can make purchases, false if not |
||
7570 | */ |
||
7571 | public static function current_user_can_purchase() { |
||
7585 | |||
7586 | } |
||
7587 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: