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 |
||
27 | class Jetpack { |
||
28 | public $xmlrpc_server = null; |
||
29 | |||
30 | private $xmlrpc_verification = null; |
||
31 | private $rest_authentication_status = null; |
||
32 | |||
33 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
34 | |||
35 | /** |
||
36 | * @var array The handles of styles that are concatenated into jetpack.css. |
||
37 | * |
||
38 | * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js. |
||
39 | */ |
||
40 | public $concatenated_style_handles = array( |
||
41 | 'jetpack-carousel', |
||
42 | 'grunion.css', |
||
43 | 'the-neverending-homepage', |
||
44 | 'jetpack_likes', |
||
45 | 'jetpack_related-posts', |
||
46 | 'sharedaddy', |
||
47 | 'jetpack-slideshow', |
||
48 | 'presentations', |
||
49 | 'quiz', |
||
50 | 'jetpack-subscriptions', |
||
51 | 'jetpack-responsive-videos-style', |
||
52 | 'jetpack-social-menu', |
||
53 | 'tiled-gallery', |
||
54 | 'jetpack_display_posts_widget', |
||
55 | 'gravatar-profile-widget', |
||
56 | 'goodreads-widget', |
||
57 | 'jetpack_social_media_icons_widget', |
||
58 | 'jetpack-top-posts-widget', |
||
59 | 'jetpack_image_widget', |
||
60 | 'jetpack-my-community-widget', |
||
61 | 'jetpack-authors-widget', |
||
62 | 'wordads', |
||
63 | 'eu-cookie-law-style', |
||
64 | 'flickr-widget-style', |
||
65 | 'jetpack-search-widget', |
||
66 | 'jetpack-simple-payments-widget-style', |
||
67 | 'jetpack-widget-social-icons-styles', |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * Contains all assets that have had their URL rewritten to minified versions. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | static $min_assets = array(); |
||
76 | |||
77 | public $plugins_to_deactivate = array( |
||
78 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
79 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
80 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
81 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
82 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
83 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
84 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
85 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
86 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
87 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
88 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
89 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
90 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
91 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
92 | ); |
||
93 | |||
94 | static $capability_translations = array( |
||
95 | 'administrator' => 'manage_options', |
||
96 | 'editor' => 'edit_others_posts', |
||
97 | 'author' => 'publish_posts', |
||
98 | 'contributor' => 'edit_posts', |
||
99 | 'subscriber' => 'read', |
||
100 | ); |
||
101 | |||
102 | /** |
||
103 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
104 | * if the plugins are active. Used by filter_default_modules |
||
105 | * |
||
106 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
107 | * change `module-slug` and add this to your plugin: |
||
108 | * |
||
109 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
110 | * function my_jetpack_get_default_modules( $modules ) { |
||
111 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
112 | * } |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | private $conflicting_plugins = array( |
||
117 | 'comments' => array( |
||
118 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
119 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
120 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
121 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
122 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
123 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
124 | ), |
||
125 | 'comment-likes' => array( |
||
126 | 'Epoch' => 'epoch/plugincore.php', |
||
127 | ), |
||
128 | 'contact-form' => array( |
||
129 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
130 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
131 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
132 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
133 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
134 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
135 | ), |
||
136 | 'minileven' => array( |
||
137 | 'WPtouch' => 'wptouch/wptouch.php', |
||
138 | ), |
||
139 | 'latex' => array( |
||
140 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
141 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
142 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
143 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
144 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
145 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
146 | ), |
||
147 | 'protect' => array( |
||
148 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
149 | 'Captcha' => 'captcha/captcha.php', |
||
150 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
151 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
152 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
153 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
154 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
155 | 'Security-protection' => 'security-protection/security-protection.php', |
||
156 | 'Login Security' => 'login-security/login-security.php', |
||
157 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
158 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
159 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
160 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
161 | ), |
||
162 | 'random-redirect' => array( |
||
163 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
164 | ), |
||
165 | 'related-posts' => array( |
||
166 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
167 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
168 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
169 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
170 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
171 | 'outbrain' => 'outbrain/outbrain.php', |
||
172 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
173 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
174 | ), |
||
175 | 'sharedaddy' => array( |
||
176 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
177 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
178 | 'ShareThis' => 'share-this/sharethis.php', |
||
179 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
180 | ), |
||
181 | 'seo-tools' => array( |
||
182 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
183 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
184 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
185 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
186 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
187 | ), |
||
188 | 'verification-tools' => array( |
||
189 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
190 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
191 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
192 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
193 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
194 | ), |
||
195 | 'widget-visibility' => array( |
||
196 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
197 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
198 | ), |
||
199 | 'sitemaps' => array( |
||
200 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
201 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
202 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
203 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
204 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
205 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
206 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
207 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
208 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
209 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
210 | 'Sitemap' => 'sitemap/sitemap.php', |
||
211 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
212 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
213 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
214 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
215 | ), |
||
216 | 'lazy-images' => array( |
||
217 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
218 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
219 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
220 | ), |
||
221 | ); |
||
222 | |||
223 | /** |
||
224 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
225 | * |
||
226 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
227 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
228 | * |
||
229 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
230 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
231 | */ |
||
232 | private $open_graph_conflicting_plugins = array( |
||
233 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
234 | // 2 Click Social Media Buttons |
||
235 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
236 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
237 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
238 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
239 | // Open Graph Meta Tags by Heateor |
||
240 | 'facebook/facebook.php', // Facebook (official plugin) |
||
241 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
242 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
243 | // Facebook Featured Image & OG Meta Tags |
||
244 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
245 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
246 | // Facebook Open Graph Meta Tags for WordPress |
||
247 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
248 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
249 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
250 | // Fedmich's Facebook Open Graph Meta |
||
251 | 'network-publisher/networkpub.php', // Network Publisher |
||
252 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
253 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
254 | // NextScripts SNAP |
||
255 | 'og-tags/og-tags.php', // OG Tags |
||
256 | 'opengraph/opengraph.php', // Open Graph |
||
257 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
258 | // Open Graph Protocol Framework |
||
259 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
260 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
261 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
262 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
263 | 'sharepress/sharepress.php', // SharePress |
||
264 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
265 | 'social-discussions/social-discussions.php', // Social Discussions |
||
266 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
267 | 'socialize/socialize.php', // Socialize |
||
268 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
269 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
270 | // Tweet, Like, Google +1 and Share |
||
271 | 'wordbooker/wordbooker.php', // Wordbooker |
||
272 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
273 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
274 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
275 | // WP Facebook Like Send & Open Graph Meta |
||
276 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
277 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
278 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
279 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button |
||
280 | 'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox |
||
281 | ); |
||
282 | |||
283 | /** |
||
284 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
285 | */ |
||
286 | private $twitter_cards_conflicting_plugins = array( |
||
287 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
288 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
289 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
290 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
291 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
292 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
293 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
294 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
295 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
296 | 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter |
||
297 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
298 | ); |
||
299 | |||
300 | /** |
||
301 | * Message to display in admin_notice |
||
302 | * @var string |
||
303 | */ |
||
304 | public $message = ''; |
||
305 | |||
306 | /** |
||
307 | * Error to display in admin_notice |
||
308 | * @var string |
||
309 | */ |
||
310 | public $error = ''; |
||
311 | |||
312 | /** |
||
313 | * Modules that need more privacy description. |
||
314 | * @var string |
||
315 | */ |
||
316 | public $privacy_checks = ''; |
||
317 | |||
318 | /** |
||
319 | * Stats to record once the page loads |
||
320 | * |
||
321 | * @var array |
||
322 | */ |
||
323 | public $stats = array(); |
||
324 | |||
325 | /** |
||
326 | * Jetpack_Sync object |
||
327 | */ |
||
328 | public $sync; |
||
329 | |||
330 | /** |
||
331 | * Verified data for JSON authorization request |
||
332 | */ |
||
333 | public $json_api_authorization_request = array(); |
||
334 | |||
335 | /** |
||
336 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
337 | */ |
||
338 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
339 | |||
340 | /** |
||
341 | * Holds the singleton instance of this class |
||
342 | * @since 2.3.3 |
||
343 | * @var Jetpack |
||
344 | */ |
||
345 | static $instance = false; |
||
346 | |||
347 | /** |
||
348 | * Singleton |
||
349 | * @static |
||
350 | */ |
||
351 | public static function init() { |
||
352 | if ( ! self::$instance ) { |
||
353 | self::$instance = new Jetpack; |
||
354 | |||
355 | self::$instance->plugin_upgrade(); |
||
356 | } |
||
357 | |||
358 | return self::$instance; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Must never be called statically |
||
363 | */ |
||
364 | function plugin_upgrade() { |
||
365 | if ( Jetpack::is_active() ) { |
||
366 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
367 | if ( JETPACK__VERSION != $version ) { |
||
368 | // Prevent multiple upgrades at once - only a single process should trigger |
||
369 | // an upgrade to avoid stampedes |
||
370 | if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) { |
||
371 | return; |
||
372 | } |
||
373 | |||
374 | // Set a short lock to prevent multiple instances of the upgrade |
||
375 | set_transient( self::$plugin_upgrade_lock_key, 1, 10 ); |
||
376 | |||
377 | // check which active modules actually exist and remove others from active_modules list |
||
378 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
379 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
380 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
381 | Jetpack::update_active_modules( $modules ); |
||
382 | } |
||
383 | |||
384 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
385 | |||
386 | // Upgrade to 4.3.0 |
||
387 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
388 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
389 | } |
||
390 | |||
391 | // Make sure Markdown for posts gets turned back on |
||
392 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
393 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
394 | } |
||
395 | |||
396 | if ( did_action( 'wp_loaded' ) ) { |
||
397 | self::upgrade_on_load(); |
||
398 | } else { |
||
399 | add_action( |
||
400 | 'wp_loaded', |
||
401 | array( __CLASS__, 'upgrade_on_load' ) |
||
402 | ); |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Runs upgrade routines that need to have modules loaded. |
||
410 | */ |
||
411 | static function upgrade_on_load() { |
||
412 | |||
413 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
414 | // This can happen in case Jetpack has been just upgraded and is |
||
415 | // being initialized late during the page load. In this case we wait |
||
416 | // until the next proper admin page load with Jetpack active. |
||
417 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
418 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
419 | |||
420 | return; |
||
421 | } |
||
422 | |||
423 | Jetpack::maybe_set_version_option(); |
||
424 | |||
425 | if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) { |
||
426 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
427 | } |
||
428 | |||
429 | if ( |
||
430 | class_exists( 'Jetpack_Sitemap_Manager' ) |
||
431 | && version_compare( JETPACK__VERSION, '5.3', '>=' ) |
||
432 | ) { |
||
433 | do_action( 'jetpack_sitemaps_purge_data' ); |
||
434 | } |
||
435 | |||
436 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Saves all the currently active modules to options. |
||
441 | * Also fires Action hooks for each newly activated and deactived module. |
||
442 | * |
||
443 | * @param $modules Array Array of active modules to be saved in options. |
||
444 | * |
||
445 | * @return $success bool true for success, false for failure. |
||
|
|||
446 | */ |
||
447 | static function update_active_modules( $modules ) { |
||
448 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
449 | $active_modules = Jetpack::get_active_modules(); |
||
450 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
451 | $new_inactive_modules = array_diff( $active_modules, $modules ); |
||
452 | $new_current_modules = array_diff( array_merge( $current_modules, $new_active_modules ), $new_inactive_modules ); |
||
453 | $reindexed_modules = array_values( $new_current_modules ); |
||
454 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $reindexed_modules ) ); |
||
455 | |||
456 | foreach ( $new_active_modules as $module ) { |
||
457 | /** |
||
458 | * Fires when a specific module is activated. |
||
459 | * |
||
460 | * @since 1.9.0 |
||
461 | * |
||
462 | * @param string $module Module slug. |
||
463 | * @param boolean $success whether the module was activated. @since 4.2 |
||
464 | */ |
||
465 | do_action( 'jetpack_activate_module', $module, $success ); |
||
466 | /** |
||
467 | * Fires when a module is activated. |
||
468 | * The dynamic part of the filter, $module, is the module slug. |
||
469 | * |
||
470 | * @since 1.9.0 |
||
471 | * |
||
472 | * @param string $module Module slug. |
||
473 | */ |
||
474 | do_action( "jetpack_activate_module_$module", $module ); |
||
475 | } |
||
476 | |||
477 | foreach ( $new_inactive_modules as $module ) { |
||
478 | /** |
||
479 | * Fired after a module has been deactivated. |
||
480 | * |
||
481 | * @since 4.2.0 |
||
482 | * |
||
483 | * @param string $module Module slug. |
||
484 | * @param boolean $success whether the module was deactivated. |
||
485 | */ |
||
486 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
487 | /** |
||
488 | * Fires when a module is deactivated. |
||
489 | * The dynamic part of the filter, $module, is the module slug. |
||
490 | * |
||
491 | * @since 1.9.0 |
||
492 | * |
||
493 | * @param string $module Module slug. |
||
494 | */ |
||
495 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
496 | } |
||
497 | |||
498 | return $success; |
||
499 | } |
||
500 | |||
501 | static function delete_active_modules() { |
||
502 | self::update_active_modules( array() ); |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * Constructor. Initializes WordPress hooks |
||
507 | */ |
||
508 | private function __construct() { |
||
509 | /* |
||
510 | * Check for and alert any deprecated hooks |
||
511 | */ |
||
512 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
513 | |||
514 | /* |
||
515 | * Enable enhanced handling of previewing sites in Calypso |
||
516 | */ |
||
517 | if ( Jetpack::is_active() ) { |
||
518 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php'; |
||
519 | add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 ); |
||
520 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php'; |
||
521 | add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 ); |
||
522 | } |
||
523 | |||
524 | /* |
||
525 | * Load things that should only be in Network Admin. |
||
526 | * |
||
527 | * For now blow away everything else until a more full |
||
528 | * understanding of what is needed at the network level is |
||
529 | * available |
||
530 | */ |
||
531 | if ( is_multisite() ) { |
||
532 | Jetpack_Network::init(); |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Prepare Gutenberg Editor functionality |
||
537 | */ |
||
538 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php'; |
||
539 | Jetpack_Gutenberg::init(); |
||
540 | Jetpack_Gutenberg::load_independent_blocks(); |
||
541 | add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) ); |
||
542 | |||
543 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
544 | |||
545 | // Unlink user before deleting the user from .com |
||
546 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
547 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
548 | |||
549 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms |
||
550 | if ( isset( $_GET['jetpack'] ) && 'comms' == $_GET['jetpack'] && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
551 | if ( ! defined( 'XMLRPC_REQUEST' ) ) { |
||
552 | define( 'XMLRPC_REQUEST', true ); |
||
553 | } |
||
554 | |||
555 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
556 | |||
557 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
558 | } |
||
559 | |||
560 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
561 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
562 | |||
563 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
564 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
565 | |||
566 | $this->require_jetpack_authentication(); |
||
567 | |||
568 | if ( Jetpack::is_active() ) { |
||
569 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
570 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
571 | |||
572 | $signed = $this->verify_xml_rpc_signature(); |
||
573 | View Code Duplication | if ( $signed && ! is_wp_error( $signed ) ) { |
|
574 | // The actual API methods. |
||
575 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
576 | } else { |
||
577 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
578 | // active Jetpack connection, so that additional users can link their account. |
||
579 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
580 | } |
||
581 | View Code Duplication | } else { |
|
582 | // The bootstrap API methods. |
||
583 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
584 | $signed = $this->verify_xml_rpc_signature(); |
||
585 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
586 | // the jetpack Provision method is available for blog-token-signed requests |
||
587 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
588 | } |
||
589 | } |
||
590 | |||
591 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
592 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
593 | } elseif ( |
||
594 | is_admin() && |
||
595 | isset( $_POST['action'] ) && ( |
||
596 | 'jetpack_upload_file' == $_POST['action'] || |
||
597 | 'jetpack_update_file' == $_POST['action'] |
||
598 | ) |
||
599 | ) { |
||
600 | $this->require_jetpack_authentication(); |
||
601 | $this->add_remote_request_handlers(); |
||
602 | } else { |
||
603 | if ( Jetpack::is_active() ) { |
||
604 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
605 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
606 | } |
||
607 | } |
||
608 | |||
609 | if ( Jetpack::is_active() ) { |
||
610 | Jetpack_Heartbeat::init(); |
||
611 | if ( Jetpack::is_module_active( 'stats' ) && Jetpack::is_module_active( 'search' ) ) { |
||
612 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php'; |
||
613 | Jetpack_Search_Performance_Logger::init(); |
||
614 | } |
||
615 | } |
||
616 | |||
617 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
618 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
619 | |||
620 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
621 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
622 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
623 | } |
||
624 | |||
625 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
626 | |||
627 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
628 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
629 | |||
630 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
631 | |||
632 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
633 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
634 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
635 | |||
636 | // returns HTTPS support status |
||
637 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
638 | |||
639 | // JITM AJAX callback function |
||
640 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
641 | |||
642 | // Universal ajax callback for all tracking events triggered via js |
||
643 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
644 | |||
645 | add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) ); |
||
646 | |||
647 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
648 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
649 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
650 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
651 | |||
652 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
653 | |||
654 | /** |
||
655 | * These actions run checks to load additional files. |
||
656 | * They check for external files or plugins, so they need to run as late as possible. |
||
657 | */ |
||
658 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
659 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
660 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
661 | |||
662 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
663 | add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 ); |
||
664 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
665 | |||
666 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
667 | add_filter( 'profile_update', array( 'Jetpack', 'user_meta_cleanup' ) ); |
||
668 | |||
669 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
670 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
671 | |||
672 | // A filter to control all just in time messages |
||
673 | add_filter( 'jetpack_just_in_time_msgs', '__return_true', 9 ); |
||
674 | add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9); |
||
675 | |||
676 | // If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin. |
||
677 | // We should make sure to only do this for front end links. |
||
678 | if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { |
||
679 | add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 ); |
||
680 | add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 ); |
||
681 | |||
682 | //we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions |
||
683 | //so they point moderation links on emails to Calypso |
||
684 | jetpack_require_lib( 'functions.wp-notify' ); |
||
685 | } |
||
686 | |||
687 | // Update the Jetpack plan from API on heartbeats |
||
688 | add_action( 'jetpack_heartbeat', array( 'Jetpack_Plan', 'refresh_from_wpcom' ) ); |
||
689 | |||
690 | /** |
||
691 | * This is the hack to concatenate all css files into one. |
||
692 | * For description and reasoning see the implode_frontend_css method |
||
693 | * |
||
694 | * Super late priority so we catch all the registered styles |
||
695 | */ |
||
696 | if( !is_admin() ) { |
||
697 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
698 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * These are sync actions that we need to keep track of for jitms |
||
703 | */ |
||
704 | add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 ); |
||
705 | |||
706 | // Actually push the stats on shutdown. |
||
707 | if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) { |
||
708 | add_action( 'shutdown', array( $this, 'push_stats' ) ); |
||
709 | } |
||
710 | } |
||
711 | |||
712 | /** |
||
713 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
714 | * |
||
715 | * @param $domains |
||
716 | */ |
||
717 | function add_wpcom_to_allowed_redirect_hosts( $domains ) { |
||
718 | add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_domain' ) ); |
||
719 | } |
||
720 | |||
721 | /** |
||
722 | * Return $domains, with 'wordpress.com' appended. |
||
723 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
724 | * |
||
725 | * @param $domains |
||
726 | * @return array |
||
727 | */ |
||
728 | function allow_wpcom_domain( $domains ) { |
||
729 | if ( empty( $domains ) ) { |
||
730 | $domains = array(); |
||
731 | } |
||
732 | $domains[] = 'wordpress.com'; |
||
733 | return array_unique( $domains ); |
||
734 | } |
||
735 | |||
736 | function point_edit_post_links_to_calypso( $default_url, $post_id ) { |
||
737 | $post = get_post( $post_id ); |
||
738 | |||
739 | if ( empty( $post ) ) { |
||
740 | return $default_url; |
||
741 | } |
||
742 | |||
743 | $post_type = $post->post_type; |
||
744 | |||
745 | // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso. |
||
746 | // https://en.support.wordpress.com/custom-post-types/ |
||
747 | $allowed_post_types = array( |
||
748 | 'post' => 'post', |
||
749 | 'page' => 'page', |
||
750 | 'jetpack-portfolio' => 'edit/jetpack-portfolio', |
||
751 | 'jetpack-testimonial' => 'edit/jetpack-testimonial', |
||
752 | ); |
||
753 | |||
754 | if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) { |
||
755 | return $default_url; |
||
756 | } |
||
757 | |||
758 | $path_prefix = $allowed_post_types[ $post_type ]; |
||
759 | |||
760 | $site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
761 | |||
762 | return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); |
||
763 | } |
||
764 | |||
765 | function point_edit_comment_links_to_calypso( $url ) { |
||
766 | // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID. |
||
767 | wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args ); |
||
768 | return esc_url( sprintf( 'https://wordpress.com/comment/%s/%d', |
||
769 | Jetpack::build_raw_urls( get_home_url() ), |
||
770 | $query_args['amp;c'] |
||
771 | ) ); |
||
772 | } |
||
773 | |||
774 | function jetpack_track_last_sync_callback( $params ) { |
||
775 | /** |
||
776 | * Filter to turn off jitm caching |
||
777 | * |
||
778 | * @since 5.4.0 |
||
779 | * |
||
780 | * @param bool false Whether to cache just in time messages |
||
781 | */ |
||
782 | if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
783 | return $params; |
||
784 | } |
||
785 | |||
786 | if ( is_array( $params ) && isset( $params[0] ) ) { |
||
787 | $option = $params[0]; |
||
788 | if ( 'active_plugins' === $option ) { |
||
789 | // use the cache if we can, but not terribly important if it gets evicted |
||
790 | set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS ); |
||
791 | } |
||
792 | } |
||
793 | |||
794 | return $params; |
||
795 | } |
||
796 | |||
797 | function jetpack_connection_banner_callback() { |
||
798 | check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' ); |
||
799 | |||
800 | if ( isset( $_REQUEST['dismissBanner'] ) ) { |
||
801 | Jetpack_Options::update_option( 'dismissed_connection_banner', 1 ); |
||
802 | wp_send_json_success(); |
||
803 | } |
||
804 | |||
805 | wp_die(); |
||
806 | } |
||
807 | |||
808 | /** |
||
809 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
810 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
811 | * ensure that Core and other plugins' methods are not exposed. |
||
812 | * |
||
813 | * @param array $methods |
||
814 | * @return array filtered $methods |
||
815 | */ |
||
816 | function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
817 | $jetpack_methods = array(); |
||
818 | |||
819 | foreach ( $methods as $method => $callback ) { |
||
820 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
821 | $jetpack_methods[ $method ] = $callback; |
||
822 | } |
||
823 | } |
||
824 | |||
825 | return $jetpack_methods; |
||
826 | } |
||
827 | |||
828 | /** |
||
829 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
830 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
831 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
832 | * which is accessible via a different URI. Most of the below is copied directly |
||
833 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
834 | */ |
||
835 | function alternate_xmlrpc() { |
||
836 | // phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
837 | global $HTTP_RAW_POST_DATA; |
||
838 | |||
839 | // Some browser-embedded clients send cookies. We don't want them. |
||
840 | $_COOKIE = array(); |
||
841 | |||
842 | // A bug in PHP < 5.2.2 makes $HTTP_RAW_POST_DATA not set by default, |
||
843 | // but we can do it ourself. |
||
844 | if ( ! isset( $HTTP_RAW_POST_DATA ) ) { |
||
845 | $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' ); |
||
846 | } |
||
847 | |||
848 | // fix for mozBlog and other cases where '<?xml' isn't on the very first line |
||
849 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
850 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
851 | } |
||
852 | |||
853 | // phpcs:enable |
||
854 | |||
855 | include_once( ABSPATH . 'wp-admin/includes/admin.php' ); |
||
856 | include_once( ABSPATH . WPINC . '/class-IXR.php' ); |
||
857 | include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' ); |
||
858 | |||
859 | /** |
||
860 | * Filters the class used for handling XML-RPC requests. |
||
861 | * |
||
862 | * @since 3.1.0 |
||
863 | * |
||
864 | * @param string $class The name of the XML-RPC server class. |
||
865 | */ |
||
866 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
867 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class; |
||
868 | |||
869 | // Fire off the request |
||
870 | nocache_headers(); |
||
871 | $wp_xmlrpc_server->serve_request(); |
||
872 | |||
873 | exit; |
||
874 | } |
||
875 | |||
876 | function jetpack_admin_ajax_tracks_callback() { |
||
877 | // Check for nonce |
||
878 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
879 | wp_die( 'Permissions check failed.' ); |
||
880 | } |
||
881 | |||
882 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
883 | wp_die( 'No valid event name or type.' ); |
||
884 | } |
||
885 | |||
886 | $tracks_data = array(); |
||
887 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
888 | if ( is_array( $_REQUEST['tracksEventProp'] ) ) { |
||
889 | $tracks_data = $_REQUEST['tracksEventProp']; |
||
890 | } else { |
||
891 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
892 | } |
||
893 | } |
||
894 | |||
895 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
896 | wp_send_json_success(); |
||
897 | wp_die(); |
||
898 | } |
||
899 | |||
900 | /** |
||
901 | * The callback for the JITM ajax requests. |
||
902 | */ |
||
903 | function jetpack_jitm_ajax_callback() { |
||
904 | // Check for nonce |
||
905 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
906 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
907 | } |
||
908 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
909 | $module_slug = $_REQUEST['jitmModule']; |
||
910 | Jetpack::log( 'activate', $module_slug ); |
||
911 | Jetpack::activate_module( $module_slug, false, false ); |
||
912 | Jetpack::state( 'message', 'no_message' ); |
||
913 | |||
914 | //A Jetpack module is being activated through a JITM, track it |
||
915 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
916 | $this->do_stats( 'server_side' ); |
||
917 | |||
918 | wp_send_json_success(); |
||
919 | } |
||
920 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
921 | // get the hide_jitm options array |
||
922 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
923 | $module_slug = $_REQUEST['jitmModule']; |
||
924 | |||
925 | if( ! $jetpack_hide_jitm ) { |
||
926 | $jetpack_hide_jitm = array( |
||
927 | $module_slug => 'hide' |
||
928 | ); |
||
929 | } else { |
||
930 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
931 | } |
||
932 | |||
933 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
934 | |||
935 | //jitm is being dismissed forever, track it |
||
936 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
937 | $this->do_stats( 'server_side' ); |
||
938 | |||
939 | wp_send_json_success(); |
||
940 | } |
||
941 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
942 | $module_slug = $_REQUEST['jitmModule']; |
||
943 | |||
944 | // User went to WordPress.com, track this |
||
945 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
946 | $this->do_stats( 'server_side' ); |
||
947 | |||
948 | wp_send_json_success(); |
||
949 | } |
||
950 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
951 | $track = $_REQUEST['jitmModule']; |
||
952 | |||
953 | // User is viewing JITM, track it. |
||
954 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
955 | $this->do_stats( 'server_side' ); |
||
956 | |||
957 | wp_send_json_success(); |
||
958 | } |
||
959 | } |
||
960 | |||
961 | /** |
||
962 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
963 | */ |
||
964 | function push_stats() { |
||
965 | if ( ! empty( $this->stats ) ) { |
||
966 | $this->do_stats( 'server_side' ); |
||
967 | } |
||
968 | } |
||
969 | |||
970 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
971 | switch( $cap ) { |
||
972 | case 'jetpack_connect' : |
||
973 | case 'jetpack_reconnect' : |
||
974 | if ( Jetpack::is_development_mode() ) { |
||
975 | $caps = array( 'do_not_allow' ); |
||
976 | break; |
||
977 | } |
||
978 | /** |
||
979 | * Pass through. If it's not development mode, these should match disconnect. |
||
980 | * Let users disconnect if it's development mode, just in case things glitch. |
||
981 | */ |
||
982 | case 'jetpack_disconnect' : |
||
983 | /** |
||
984 | * In multisite, can individual site admins manage their own connection? |
||
985 | * |
||
986 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
987 | */ |
||
988 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
989 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
990 | /** |
||
991 | * We need to update the option name -- it's terribly unclear which |
||
992 | * direction the override goes. |
||
993 | * |
||
994 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
995 | */ |
||
996 | $caps = array( 'do_not_allow' ); |
||
997 | break; |
||
998 | } |
||
999 | } |
||
1000 | |||
1001 | $caps = array( 'manage_options' ); |
||
1002 | break; |
||
1003 | case 'jetpack_manage_modules' : |
||
1004 | case 'jetpack_activate_modules' : |
||
1005 | case 'jetpack_deactivate_modules' : |
||
1006 | $caps = array( 'manage_options' ); |
||
1007 | break; |
||
1008 | case 'jetpack_configure_modules' : |
||
1009 | $caps = array( 'manage_options' ); |
||
1010 | break; |
||
1011 | case 'jetpack_manage_autoupdates' : |
||
1012 | $caps = array( |
||
1013 | 'manage_options', |
||
1014 | 'update_plugins', |
||
1015 | ); |
||
1016 | break; |
||
1017 | case 'jetpack_network_admin_page': |
||
1018 | case 'jetpack_network_settings_page': |
||
1019 | $caps = array( 'manage_network_plugins' ); |
||
1020 | break; |
||
1021 | case 'jetpack_network_sites_page': |
||
1022 | $caps = array( 'manage_sites' ); |
||
1023 | break; |
||
1024 | case 'jetpack_admin_page' : |
||
1025 | if ( Jetpack::is_development_mode() ) { |
||
1026 | $caps = array( 'manage_options' ); |
||
1027 | break; |
||
1028 | } else { |
||
1029 | $caps = array( 'read' ); |
||
1030 | } |
||
1031 | break; |
||
1032 | case 'jetpack_connect_user' : |
||
1033 | if ( Jetpack::is_development_mode() ) { |
||
1034 | $caps = array( 'do_not_allow' ); |
||
1035 | break; |
||
1036 | } |
||
1037 | $caps = array( 'read' ); |
||
1038 | break; |
||
1039 | } |
||
1040 | return $caps; |
||
1041 | } |
||
1042 | |||
1043 | function require_jetpack_authentication() { |
||
1044 | // Don't let anyone authenticate |
||
1045 | $_COOKIE = array(); |
||
1046 | remove_all_filters( 'authenticate' ); |
||
1047 | remove_all_actions( 'wp_login_failed' ); |
||
1048 | |||
1049 | if ( Jetpack::is_active() ) { |
||
1050 | // Allow Jetpack authentication |
||
1051 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
1052 | } |
||
1053 | } |
||
1054 | |||
1055 | /** |
||
1056 | * Load language files |
||
1057 | * @action plugins_loaded |
||
1058 | */ |
||
1059 | public static function plugin_textdomain() { |
||
1060 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
1061 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
1062 | } |
||
1063 | |||
1064 | /** |
||
1065 | * Register assets for use in various modules and the Jetpack admin page. |
||
1066 | * |
||
1067 | * @uses wp_script_is, wp_register_script, plugins_url |
||
1068 | * @action wp_loaded |
||
1069 | * @return null |
||
1070 | */ |
||
1071 | public function register_assets() { |
||
1072 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
1073 | wp_register_script( |
||
1074 | 'spin', |
||
1075 | self::get_file_url_for_environment( '_inc/build/spin.min.js', '_inc/spin.js' ), |
||
1076 | false, |
||
1077 | '1.3' |
||
1078 | ); |
||
1079 | } |
||
1080 | |||
1081 | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
||
1082 | wp_register_script( |
||
1083 | 'jquery.spin', |
||
1084 | self::get_file_url_for_environment( '_inc/build/jquery.spin.min.js', '_inc/jquery.spin.js' ), |
||
1085 | array( 'jquery', 'spin' ), |
||
1086 | '1.3' |
||
1087 | ); |
||
1088 | } |
||
1089 | |||
1090 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
1091 | wp_register_script( |
||
1092 | 'jetpack-gallery-settings', |
||
1093 | self::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ), |
||
1094 | array( 'media-views' ), |
||
1095 | '20121225' |
||
1096 | ); |
||
1097 | } |
||
1098 | |||
1099 | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
||
1100 | wp_register_script( |
||
1101 | 'jetpack-twitter-timeline', |
||
1102 | self::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ), |
||
1103 | array( 'jquery' ), |
||
1104 | '4.0.0', |
||
1105 | true |
||
1106 | ); |
||
1107 | } |
||
1108 | |||
1109 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
1110 | wp_register_script( |
||
1111 | 'jetpack-facebook-embed', |
||
1112 | self::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ), |
||
1113 | array( 'jquery' ), |
||
1114 | null, |
||
1115 | true |
||
1116 | ); |
||
1117 | |||
1118 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
1119 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
1120 | if ( ! is_numeric( $fb_app_id ) ) { |
||
1121 | $fb_app_id = ''; |
||
1122 | } |
||
1123 | wp_localize_script( |
||
1124 | 'jetpack-facebook-embed', |
||
1125 | 'jpfbembed', |
||
1126 | array( |
||
1127 | 'appid' => $fb_app_id, |
||
1128 | 'locale' => $this->get_locale(), |
||
1129 | ) |
||
1130 | ); |
||
1131 | } |
||
1132 | |||
1133 | /** |
||
1134 | * As jetpack_register_genericons is by default fired off a hook, |
||
1135 | * the hook may have already fired by this point. |
||
1136 | * So, let's just trigger it manually. |
||
1137 | */ |
||
1138 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
1139 | jetpack_register_genericons(); |
||
1140 | |||
1141 | /** |
||
1142 | * Register the social logos |
||
1143 | */ |
||
1144 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
1145 | jetpack_register_social_logos(); |
||
1146 | |||
1147 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
1148 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
1149 | } |
||
1150 | |||
1151 | /** |
||
1152 | * Guess locale from language code. |
||
1153 | * |
||
1154 | * @param string $lang Language code. |
||
1155 | * @return string|bool |
||
1156 | */ |
||
1157 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
1158 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
1159 | return 'en_US'; |
||
1160 | } |
||
1161 | |||
1162 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
1163 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
1164 | return false; |
||
1165 | } |
||
1166 | |||
1167 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
1168 | } |
||
1169 | |||
1170 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
1171 | // WP.com: get_locale() returns 'it' |
||
1172 | $locale = GP_Locales::by_slug( $lang ); |
||
1173 | } else { |
||
1174 | // Jetpack: get_locale() returns 'it_IT'; |
||
1175 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
1176 | } |
||
1177 | |||
1178 | if ( ! $locale ) { |
||
1179 | return false; |
||
1180 | } |
||
1181 | |||
1182 | if ( empty( $locale->facebook_locale ) ) { |
||
1183 | if ( empty( $locale->wp_locale ) ) { |
||
1184 | return false; |
||
1185 | } else { |
||
1186 | // Facebook SDK is smart enough to fall back to en_US if a |
||
1187 | // locale isn't supported. Since supported Facebook locales |
||
1188 | // can fall out of sync, we'll attempt to use the known |
||
1189 | // wp_locale value and rely on said fallback. |
||
1190 | return $locale->wp_locale; |
||
1191 | } |
||
1192 | } |
||
1193 | |||
1194 | return $locale->facebook_locale; |
||
1195 | } |
||
1196 | |||
1197 | /** |
||
1198 | * Get the locale. |
||
1199 | * |
||
1200 | * @return string|bool |
||
1201 | */ |
||
1202 | function get_locale() { |
||
1203 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
1204 | |||
1205 | if ( ! $locale ) { |
||
1206 | $locale = 'en_US'; |
||
1207 | } |
||
1208 | |||
1209 | return $locale; |
||
1210 | } |
||
1211 | |||
1212 | /** |
||
1213 | * Device Pixels support |
||
1214 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
1215 | */ |
||
1216 | function devicepx() { |
||
1217 | if ( Jetpack::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) { |
||
1218 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
1219 | } |
||
1220 | } |
||
1221 | |||
1222 | /** |
||
1223 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1224 | * @param bool $option |
||
1225 | * @return string |
||
1226 | */ |
||
1227 | public function jetpack_main_network_site_option( $option ) { |
||
1228 | return network_site_url(); |
||
1229 | } |
||
1230 | /** |
||
1231 | * Network Name. |
||
1232 | */ |
||
1233 | static function network_name( $option = null ) { |
||
1234 | global $current_site; |
||
1235 | return $current_site->site_name; |
||
1236 | } |
||
1237 | /** |
||
1238 | * Does the network allow new user and site registrations. |
||
1239 | * @return string |
||
1240 | */ |
||
1241 | static function network_allow_new_registrations( $option = null ) { |
||
1242 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
1243 | } |
||
1244 | /** |
||
1245 | * Does the network allow admins to add new users. |
||
1246 | * @return boolian |
||
1247 | */ |
||
1248 | static function network_add_new_users( $option = null ) { |
||
1249 | return (bool) get_site_option( 'add_new_users' ); |
||
1250 | } |
||
1251 | /** |
||
1252 | * File upload psace left per site in MB. |
||
1253 | * -1 means NO LIMIT. |
||
1254 | * @return number |
||
1255 | */ |
||
1256 | static function network_site_upload_space( $option = null ) { |
||
1257 | // value in MB |
||
1258 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
1259 | } |
||
1260 | |||
1261 | /** |
||
1262 | * Network allowed file types. |
||
1263 | * @return string |
||
1264 | */ |
||
1265 | static function network_upload_file_types( $option = null ) { |
||
1266 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
1267 | } |
||
1268 | |||
1269 | /** |
||
1270 | * Maximum file upload size set by the network. |
||
1271 | * @return number |
||
1272 | */ |
||
1273 | static function network_max_upload_file_size( $option = null ) { |
||
1274 | // value in KB |
||
1275 | return get_site_option( 'fileupload_maxk', 300 ); |
||
1276 | } |
||
1277 | |||
1278 | /** |
||
1279 | * Lets us know if a site allows admins to manage the network. |
||
1280 | * @return array |
||
1281 | */ |
||
1282 | static function network_enable_administration_menus( $option = null ) { |
||
1283 | return get_site_option( 'menu_items' ); |
||
1284 | } |
||
1285 | |||
1286 | /** |
||
1287 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1288 | * jetpack_other_linked_admins transient. |
||
1289 | * |
||
1290 | * @since 4.3.2 |
||
1291 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1292 | * |
||
1293 | * @param int $user_id The user ID whose role changed. |
||
1294 | * @param string $role The new role. |
||
1295 | * @param array $old_roles An array of the user's previous roles. |
||
1296 | */ |
||
1297 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1298 | if ( 'administrator' == $role |
||
1299 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
1300 | || is_null( $old_roles ) |
||
1301 | ) { |
||
1302 | delete_transient( 'jetpack_other_linked_admins' ); |
||
1303 | } |
||
1304 | } |
||
1305 | |||
1306 | /** |
||
1307 | * Checks to see if there are any other users available to become primary |
||
1308 | * Users must both: |
||
1309 | * - Be linked to wpcom |
||
1310 | * - Be an admin |
||
1311 | * |
||
1312 | * @return mixed False if no other users are linked, Int if there are. |
||
1313 | */ |
||
1314 | static function get_other_linked_admins() { |
||
1315 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
1316 | |||
1317 | if ( false === $other_linked_users ) { |
||
1318 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
1319 | if ( count( $admins ) > 1 ) { |
||
1320 | $available = array(); |
||
1321 | foreach ( $admins as $admin ) { |
||
1322 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
1323 | $available[] = $admin->ID; |
||
1324 | } |
||
1325 | } |
||
1326 | |||
1327 | $count_connected_admins = count( $available ); |
||
1328 | if ( count( $available ) > 1 ) { |
||
1329 | $other_linked_users = $count_connected_admins; |
||
1330 | } else { |
||
1331 | $other_linked_users = 0; |
||
1332 | } |
||
1333 | } else { |
||
1334 | $other_linked_users = 0; |
||
1335 | } |
||
1336 | |||
1337 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
1338 | } |
||
1339 | |||
1340 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
1341 | } |
||
1342 | |||
1343 | /** |
||
1344 | * Return whether we are dealing with a multi network setup or not. |
||
1345 | * The reason we are type casting this is because we want to avoid the situation where |
||
1346 | * the result is false since when is_main_network_option return false it cases |
||
1347 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1348 | * database which could be set to anything as opposed to what this function returns. |
||
1349 | * @param bool $option |
||
1350 | * |
||
1351 | * @return boolean |
||
1352 | */ |
||
1353 | public function is_main_network_option( $option ) { |
||
1354 | // return '1' or '' |
||
1355 | return (string) (bool) Jetpack::is_multi_network(); |
||
1356 | } |
||
1357 | |||
1358 | /** |
||
1359 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1360 | * |
||
1361 | * @param string $option |
||
1362 | * @return boolean |
||
1363 | */ |
||
1364 | public function is_multisite( $option ) { |
||
1365 | return (string) (bool) is_multisite(); |
||
1366 | } |
||
1367 | |||
1368 | /** |
||
1369 | * Implemented since there is no core is multi network function |
||
1370 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1371 | * |
||
1372 | * @since 3.3 |
||
1373 | * @return boolean |
||
1374 | */ |
||
1375 | public static function is_multi_network() { |
||
1376 | global $wpdb; |
||
1377 | |||
1378 | // if we don't have a multi site setup no need to do any more |
||
1379 | if ( ! is_multisite() ) { |
||
1380 | return false; |
||
1381 | } |
||
1382 | |||
1383 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
1384 | if ( $num_sites > 1 ) { |
||
1385 | return true; |
||
1386 | } else { |
||
1387 | return false; |
||
1388 | } |
||
1389 | } |
||
1390 | |||
1391 | /** |
||
1392 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1393 | * @return null |
||
1394 | */ |
||
1395 | function update_jetpack_main_network_site_option() { |
||
1396 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1397 | } |
||
1398 | /** |
||
1399 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1400 | * |
||
1401 | */ |
||
1402 | function update_jetpack_network_settings() { |
||
1403 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1404 | // Only sync this info for the main network site. |
||
1405 | } |
||
1406 | |||
1407 | /** |
||
1408 | * Get back if the current site is single user site. |
||
1409 | * |
||
1410 | * @return bool |
||
1411 | */ |
||
1412 | public static function is_single_user_site() { |
||
1413 | global $wpdb; |
||
1414 | |||
1415 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
1416 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
1417 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
1418 | } |
||
1419 | return 1 === (int) $some_users; |
||
1420 | } |
||
1421 | |||
1422 | /** |
||
1423 | * Returns true if the site has file write access false otherwise. |
||
1424 | * @return string ( '1' | '0' ) |
||
1425 | **/ |
||
1426 | public static function file_system_write_access() { |
||
1427 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
1428 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
1429 | } |
||
1430 | |||
1431 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
1432 | |||
1433 | $filesystem_method = get_filesystem_method(); |
||
1434 | if ( $filesystem_method === 'direct' ) { |
||
1435 | return 1; |
||
1436 | } |
||
1437 | |||
1438 | ob_start(); |
||
1439 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
1440 | ob_end_clean(); |
||
1441 | if ( $filesystem_credentials_are_stored ) { |
||
1442 | return 1; |
||
1443 | } |
||
1444 | return 0; |
||
1445 | } |
||
1446 | |||
1447 | /** |
||
1448 | * Finds out if a site is using a version control system. |
||
1449 | * @return string ( '1' | '0' ) |
||
1450 | **/ |
||
1451 | public static function is_version_controlled() { |
||
1452 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
1453 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
1454 | } |
||
1455 | |||
1456 | /** |
||
1457 | * Determines whether the current theme supports featured images or not. |
||
1458 | * @return string ( '1' | '0' ) |
||
1459 | */ |
||
1460 | public static function featured_images_enabled() { |
||
1461 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1462 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
1463 | } |
||
1464 | |||
1465 | /** |
||
1466 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1467 | * |
||
1468 | * @deprecated 4.7 use get_avatar_url instead. |
||
1469 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1470 | * @param int $size Size of the avatar image |
||
1471 | * @param string $default URL to a default image to use if no avatar is available |
||
1472 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1473 | * |
||
1474 | * @return array |
||
1475 | */ |
||
1476 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1477 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
1478 | return get_avatar_url( $id_or_email, array( |
||
1479 | 'size' => $size, |
||
1480 | 'default' => $default, |
||
1481 | 'force_default' => $force_display, |
||
1482 | ) ); |
||
1483 | } |
||
1484 | |||
1485 | /** |
||
1486 | * jetpack_updates is saved in the following schema: |
||
1487 | * |
||
1488 | * array ( |
||
1489 | * 'plugins' => (int) Number of plugin updates available. |
||
1490 | * 'themes' => (int) Number of theme updates available. |
||
1491 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1492 | * 'translations' => (int) Number of translation updates available. |
||
1493 | * 'total' => (int) Total of all available updates. |
||
1494 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1495 | * ) |
||
1496 | * @return array |
||
1497 | */ |
||
1498 | public static function get_updates() { |
||
1499 | $update_data = wp_get_update_data(); |
||
1500 | |||
1501 | // Stores the individual update counts as well as the total count. |
||
1502 | if ( isset( $update_data['counts'] ) ) { |
||
1503 | $updates = $update_data['counts']; |
||
1504 | } |
||
1505 | |||
1506 | // If we need to update WordPress core, let's find the latest version number. |
||
1507 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
1508 | $cur = get_preferred_from_update_core(); |
||
1509 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
1510 | $updates['wp_update_version'] = $cur->current; |
||
1511 | } |
||
1512 | } |
||
1513 | return isset( $updates ) ? $updates : array(); |
||
1514 | } |
||
1515 | |||
1516 | public static function get_update_details() { |
||
1517 | $update_details = array( |
||
1518 | 'update_core' => get_site_transient( 'update_core' ), |
||
1519 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
1520 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
1521 | ); |
||
1522 | return $update_details; |
||
1523 | } |
||
1524 | |||
1525 | public static function refresh_update_data() { |
||
1526 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1527 | |||
1528 | } |
||
1529 | |||
1530 | public static function refresh_theme_data() { |
||
1531 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1532 | } |
||
1533 | |||
1534 | /** |
||
1535 | * Is Jetpack active? |
||
1536 | */ |
||
1537 | public static function is_active() { |
||
1538 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1539 | } |
||
1540 | |||
1541 | /** |
||
1542 | * Make an API call to WordPress.com for plan status |
||
1543 | * |
||
1544 | * @deprecated 7.2.0 Use Jetpack_Plan::refresh_from_wpcom. |
||
1545 | * |
||
1546 | * @return bool True if plan is updated, false if no update |
||
1547 | */ |
||
1548 | public static function refresh_active_plan_from_wpcom() { |
||
1549 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::refresh_from_wpcom' ); |
||
1550 | return Jetpack_Plan::refresh_from_wpcom(); |
||
1551 | } |
||
1552 | |||
1553 | /** |
||
1554 | * Get the plan that this Jetpack site is currently using |
||
1555 | * |
||
1556 | * @deprecated 7.2.0 Use Jetpack_Plan::get. |
||
1557 | * @return array Active Jetpack plan details. |
||
1558 | */ |
||
1559 | public static function get_active_plan() { |
||
1560 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::get' ); |
||
1561 | return Jetpack_Plan::get(); |
||
1562 | } |
||
1563 | |||
1564 | /** |
||
1565 | * Determine whether the active plan supports a particular feature |
||
1566 | * |
||
1567 | * @deprecated 7.2.0 Use Jetpack_Plan::supports. |
||
1568 | * @return bool True if plan supports feature, false if not. |
||
1569 | */ |
||
1570 | public static function active_plan_supports( $feature ) { |
||
1571 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::supports' ); |
||
1572 | return Jetpack_Plan::supports( $feature ); |
||
1573 | } |
||
1574 | |||
1575 | /** |
||
1576 | * Is Jetpack in development (offline) mode? |
||
1577 | */ |
||
1578 | public static function is_development_mode() { |
||
1579 | $development_mode = false; |
||
1580 | |||
1581 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
1582 | $development_mode = JETPACK_DEV_DEBUG; |
||
1583 | } elseif ( $site_url = site_url() ) { |
||
1584 | $development_mode = false === strpos( $site_url, '.' ); |
||
1585 | } |
||
1586 | |||
1587 | /** |
||
1588 | * Filters Jetpack's development mode. |
||
1589 | * |
||
1590 | * @see https://jetpack.com/support/development-mode/ |
||
1591 | * |
||
1592 | * @since 2.2.1 |
||
1593 | * |
||
1594 | * @param bool $development_mode Is Jetpack's development mode active. |
||
1595 | */ |
||
1596 | $development_mode = ( bool ) apply_filters( 'jetpack_development_mode', $development_mode ); |
||
1597 | return $development_mode; |
||
1598 | } |
||
1599 | |||
1600 | /** |
||
1601 | * Whether the site is currently onboarding or not. |
||
1602 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
1603 | * |
||
1604 | * @since 5.8 |
||
1605 | * |
||
1606 | * @access public |
||
1607 | * @static |
||
1608 | * |
||
1609 | * @return bool True if the site is currently onboarding, false otherwise |
||
1610 | */ |
||
1611 | public static function is_onboarding() { |
||
1612 | return Jetpack_Options::get_option( 'onboarding' ) !== false; |
||
1613 | } |
||
1614 | |||
1615 | /** |
||
1616 | * Determines reason for Jetpack development mode. |
||
1617 | */ |
||
1618 | public static function development_mode_trigger_text() { |
||
1619 | if ( ! Jetpack::is_development_mode() ) { |
||
1620 | return __( 'Jetpack is not in Development Mode.', 'jetpack' ); |
||
1621 | } |
||
1622 | |||
1623 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
1624 | $notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' ); |
||
1625 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
1626 | $notice = __( 'The site URL lacking a dot (e.g. http://localhost).', 'jetpack' ); |
||
1627 | } else { |
||
1628 | $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' ); |
||
1629 | } |
||
1630 | |||
1631 | return $notice; |
||
1632 | |||
1633 | } |
||
1634 | /** |
||
1635 | * Get Jetpack development mode notice text and notice class. |
||
1636 | * |
||
1637 | * Mirrors the checks made in Jetpack::is_development_mode |
||
1638 | * |
||
1639 | */ |
||
1640 | public static function show_development_mode_notice() { |
||
1641 | View Code Duplication | if ( Jetpack::is_development_mode() ) { |
|
1642 | $notice = sprintf( |
||
1643 | /* translators: %s is a URL */ |
||
1644 | __( 'In <a href="%s" target="_blank">Development Mode</a>:', 'jetpack' ), |
||
1645 | 'https://jetpack.com/support/development-mode/' |
||
1646 | ); |
||
1647 | |||
1648 | $notice .= ' ' . Jetpack::development_mode_trigger_text(); |
||
1649 | |||
1650 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1651 | } |
||
1652 | |||
1653 | // Throw up a notice if using a development version and as for feedback. |
||
1654 | if ( Jetpack::is_development_version() ) { |
||
1655 | /* translators: %s is a URL */ |
||
1656 | $notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), 'https://jetpack.com/contact-support/beta-group/' ); |
||
1657 | |||
1658 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1659 | } |
||
1660 | // Throw up a notice if using staging mode |
||
1661 | if ( Jetpack::is_staging_site() ) { |
||
1662 | /* translators: %s is a URL */ |
||
1663 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
1664 | |||
1665 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1666 | } |
||
1667 | } |
||
1668 | |||
1669 | /** |
||
1670 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1671 | */ |
||
1672 | public static function is_development_version() { |
||
1687 | |||
1688 | /** |
||
1689 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1690 | */ |
||
1691 | public static function is_user_connected( $user_id = false ) { |
||
1699 | |||
1700 | /** |
||
1701 | * Get the wpcom user data of the current|specified connected user. |
||
1702 | */ |
||
1703 | public static function get_connected_user_data( $user_id = null ) { |
||
1727 | |||
1728 | /** |
||
1729 | * Get the wpcom email of the current|specified connected user. |
||
1730 | */ |
||
1731 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
1745 | |||
1746 | /** |
||
1747 | * Get the wpcom email of the master user. |
||
1748 | */ |
||
1749 | public static function get_master_user_email() { |
||
1756 | |||
1757 | function current_user_is_connection_owner() { |
||
1761 | |||
1762 | /** |
||
1763 | * Gets current user IP address. |
||
1764 | * |
||
1765 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
1766 | * |
||
1767 | * @return string Current user IP address. |
||
1768 | */ |
||
1769 | public static function current_user_ip( $check_all_headers = false ) { |
||
1789 | |||
1790 | /** |
||
1791 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
1792 | */ |
||
1793 | function extra_oembed_providers() { |
||
1802 | |||
1803 | /** |
||
1804 | * Synchronize connected user role changes |
||
1805 | */ |
||
1806 | function user_role_change( $user_id ) { |
||
1810 | |||
1811 | /** |
||
1812 | * Loads the private module if it has been activated. |
||
1813 | * Else, updates the admin dashboard with the site's private status. |
||
1814 | */ |
||
1815 | public static function load_private() { |
||
1816 | if ( self::is_module_active( 'private' ) ) { |
||
1817 | self::load_modules( array( 'private' ) ); |
||
1818 | } else { |
||
1819 | add_action( 'update_right_now_text', array( __CLASS__, 'add_public_dashboard_glance_items' ) ); |
||
1820 | add_action( 'admin_enqueue_scripts', array( __CLASS__, 'wp_admin_glance_dashboard_style' ) ); |
||
1821 | add_filter( 'privacy_on_link_text', array( __CLASS__, 'private_site_privacy_on_link_text' ) ); |
||
1822 | } |
||
1823 | } |
||
1824 | |||
1825 | /** |
||
1826 | * Adds a line break for the 'Search Engines Discouraged' message |
||
1827 | * displayed in the 'At a Glance' dashboard widget. |
||
1828 | * |
||
1829 | * @param string $content Content of 'At A Glance' wp-admin dashboard widget. |
||
1830 | * @return string The modified content of the 'At a Glance' dashboard widget. |
||
1831 | */ |
||
1832 | |||
1833 | public static function private_site_privacy_on_link_text( $content ) { |
||
1834 | return '<br>' . $content; |
||
1835 | } |
||
1836 | |||
1837 | /** |
||
1838 | * Basic styling for the wp-admin 'At a Glance' dashboard widget. |
||
1839 | * This is applied when the private module is inactive. |
||
1840 | * |
||
1841 | * @param string $hook Page Hook Suffix for the current page. |
||
1842 | */ |
||
1843 | public static function wp_admin_glance_dashboard_style( $hook ) { |
||
1844 | if ( 'index.php' !== $hook ) { |
||
1845 | return; |
||
1846 | } |
||
1847 | |||
1848 | $custom_css = ' |
||
1849 | .jp-at-a-glance__site-public { |
||
1850 | color: #46B450; |
||
1851 | } |
||
1852 | '; |
||
1853 | wp_add_inline_style( 'dashboard', $custom_css ); |
||
1854 | } |
||
1855 | |||
1856 | /** |
||
1857 | * Adds a message to the 'At a Glance' dashboard widget. |
||
1858 | * |
||
1859 | * @param string $content Content of 'At A Glance' wp-admin dashboard widget. |
||
1860 | * @return string The modified content of the 'At a Glance' dashboard widget. |
||
1861 | */ |
||
1862 | View Code Duplication | public static function add_public_dashboard_glance_items( $content ) { |
|
1863 | return |
||
1864 | $content . |
||
1865 | '<br><br>' . |
||
1866 | wp_kses( |
||
1867 | sprintf( |
||
1868 | /* translators: URL for Jetpack dashboard. */ |
||
1869 | __( '<span class="%1$1s">This site is set to public.</span> <a href="%2$2s">Make private</a>.', 'jetpack' ), |
||
1870 | esc_attr( 'jp-at-a-glance__site-public' ), |
||
1871 | esc_url( admin_url( 'admin.php?page=jetpack#/security?term=private' ) ) |
||
1872 | ), |
||
1873 | array( |
||
1874 | 'a' => array( 'href' => true ), |
||
1875 | 'span' => array( 'class' => true ), |
||
1876 | ) |
||
1877 | ); |
||
1878 | } |
||
1879 | |||
1880 | /** |
||
1881 | * Loads modules from given array, otherwise all the currently active modules. |
||
1882 | * |
||
1883 | * @param array $modules Specific modules to be loaded. |
||
1884 | */ |
||
1885 | public static function load_modules( $modules = array() ) { |
||
1886 | if ( |
||
1887 | ! self::is_active() |
||
1888 | && ! self::is_development_mode() |
||
1889 | && ! self::is_onboarding() |
||
1890 | && ( |
||
1891 | ! is_multisite() |
||
1892 | || ! get_site_option( 'jetpack_protect_active' ) |
||
1893 | ) |
||
1894 | ) { |
||
1895 | return; |
||
1896 | } |
||
1897 | |||
1898 | $version = Jetpack_Options::get_option( 'version' ); |
||
1899 | View Code Duplication | if ( ! $version ) { |
|
1900 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
1901 | /** This action is documented in class.jetpack.php */ |
||
1902 | do_action( 'updating_jetpack_version', $version, false ); |
||
1903 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
1904 | } |
||
1905 | list( $version ) = explode( ':', $version ); |
||
1906 | $fetched_all_active_modules = false; |
||
1907 | |||
1908 | if ( empty( $modules ) ) { |
||
1909 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
1910 | $fetched_all_active_modules = true; |
||
1911 | } |
||
1912 | |||
1913 | $modules_data = array(); |
||
1914 | |||
1915 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
1916 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
1917 | $updated_modules = array(); |
||
1918 | foreach ( $modules as $module ) { |
||
1919 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1920 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
1921 | continue; |
||
1922 | } |
||
1923 | |||
1924 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
1925 | continue; |
||
1926 | } |
||
1927 | |||
1928 | $updated_modules[] = $module; |
||
1929 | } |
||
1930 | |||
1931 | $modules = array_diff( $modules, $updated_modules ); |
||
1932 | } |
||
1933 | |||
1934 | $is_development_mode = Jetpack::is_development_mode(); |
||
1935 | |||
1936 | foreach ( $modules as $index => $module ) { |
||
1937 | // If we're in dev mode, disable modules requiring a connection |
||
1938 | if ( $is_development_mode ) { |
||
1939 | // Prime the pump if we need to |
||
1940 | if ( empty( $modules_data[ $module ] ) ) { |
||
1941 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1942 | } |
||
1943 | // If the module requires a connection, but we're in local mode, don't include it. |
||
1944 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
1945 | continue; |
||
1946 | } |
||
1947 | } |
||
1948 | |||
1949 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
1950 | continue; |
||
1951 | } |
||
1952 | |||
1953 | if ( ! include_once( Jetpack::get_module_path( $module ) ) ) { |
||
1954 | unset( $modules[ $index ] ); |
||
1955 | self::update_active_modules( array_values( $modules ) ); |
||
1956 | continue; |
||
1957 | } |
||
1958 | |||
1959 | /** |
||
1960 | * Fires when a specific module is loaded. |
||
1961 | * The dynamic part of the hook, $module, is the module slug. |
||
1962 | * |
||
1963 | * @since 1.1.0 |
||
1964 | */ |
||
1965 | do_action( 'jetpack_module_loaded_' . $module ); |
||
1966 | } |
||
1967 | |||
1968 | if ( $fetched_all_active_modules ) { |
||
1969 | /** |
||
1970 | * Fires when all the modules are loaded. |
||
1971 | * |
||
1972 | * @since 1.1.0 |
||
1973 | */ |
||
1974 | do_action( 'jetpack_modules_loaded' ); |
||
1975 | } |
||
1976 | |||
1977 | // 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. |
||
1978 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
1979 | } |
||
1980 | |||
1981 | /** |
||
1982 | * Check if Jetpack's REST API compat file should be included |
||
1983 | * @action plugins_loaded |
||
1984 | * @return null |
||
1985 | */ |
||
1986 | public function check_rest_api_compat() { |
||
2002 | |||
2003 | /** |
||
2004 | * Gets all plugins currently active in values, regardless of whether they're |
||
2005 | * traditionally activated or network activated. |
||
2006 | * |
||
2007 | * @todo Store the result in core's object cache maybe? |
||
2008 | */ |
||
2009 | public static function get_active_plugins() { |
||
2025 | |||
2026 | /** |
||
2027 | * Gets and parses additional plugin data to send with the heartbeat data |
||
2028 | * |
||
2029 | * @since 3.8.1 |
||
2030 | * |
||
2031 | * @return array Array of plugin data |
||
2032 | */ |
||
2033 | public static function get_parsed_plugin_data() { |
||
2054 | |||
2055 | /** |
||
2056 | * Gets and parses theme data to send with the heartbeat data |
||
2057 | * |
||
2058 | * @since 3.8.1 |
||
2059 | * |
||
2060 | * @return array Array of theme data |
||
2061 | */ |
||
2062 | public static function get_parsed_theme_data() { |
||
2084 | |||
2085 | /** |
||
2086 | * Checks whether a specific plugin is active. |
||
2087 | * |
||
2088 | * We don't want to store these in a static variable, in case |
||
2089 | * there are switch_to_blog() calls involved. |
||
2090 | */ |
||
2091 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
2094 | |||
2095 | /** |
||
2096 | * Check if Jetpack's Open Graph tags should be used. |
||
2097 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
2098 | * |
||
2099 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2100 | * @action plugins_loaded |
||
2101 | * @return null |
||
2102 | */ |
||
2103 | public function check_open_graph() { |
||
2130 | |||
2131 | /** |
||
2132 | * Check if Jetpack's Twitter tags should be used. |
||
2133 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
2134 | * |
||
2135 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2136 | * @action plugins_loaded |
||
2137 | * @return null |
||
2138 | */ |
||
2139 | public function check_twitter_tags() { |
||
2163 | |||
2164 | /** |
||
2165 | * Allows plugins to submit security reports. |
||
2166 | * |
||
2167 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
2168 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
2169 | * @param array $args See definitions above |
||
2170 | */ |
||
2171 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
2174 | |||
2175 | /* Jetpack Options API */ |
||
2176 | |||
2177 | public static function get_option_names( $type = 'compact' ) { |
||
2180 | |||
2181 | /** |
||
2182 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
2183 | * |
||
2184 | * @param string $name Option name |
||
2185 | * @param mixed $default (optional) |
||
2186 | */ |
||
2187 | public static function get_option( $name, $default = false ) { |
||
2190 | |||
2191 | /** |
||
2192 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
2193 | * |
||
2194 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
2195 | * @param string $name Option name |
||
2196 | * @param mixed $value Option value |
||
2197 | */ |
||
2198 | public static function update_option( $name, $value ) { |
||
2202 | |||
2203 | /** |
||
2204 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
2205 | * |
||
2206 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
2207 | * @param array $array array( option name => option value, ... ) |
||
2208 | */ |
||
2209 | public static function update_options( $array ) { |
||
2213 | |||
2214 | /** |
||
2215 | * Deletes the given option. May be passed multiple option names as an array. |
||
2216 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
2217 | * |
||
2218 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
2219 | * @param string|array $names |
||
2220 | */ |
||
2221 | public static function delete_option( $names ) { |
||
2225 | |||
2226 | /** |
||
2227 | * Enters a user token into the user_tokens option |
||
2228 | * |
||
2229 | * @param int $user_id |
||
2230 | * @param string $token |
||
2231 | * return bool |
||
2232 | */ |
||
2233 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
2247 | |||
2248 | /** |
||
2249 | * Returns an array of all PHP files in the specified absolute path. |
||
2250 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
2251 | * |
||
2252 | * @param string $absolute_path The absolute path of the directory to search. |
||
2253 | * @return array Array of absolute paths to the PHP files. |
||
2254 | */ |
||
2255 | public static function glob_php( $absolute_path ) { |
||
2284 | |||
2285 | public static function activate_new_modules( $redirect = false ) { |
||
2343 | |||
2344 | /** |
||
2345 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2346 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2347 | */ |
||
2348 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
2405 | |||
2406 | /** |
||
2407 | * Default modules loaded on activation. |
||
2408 | */ |
||
2409 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
2440 | |||
2441 | /** |
||
2442 | * Checks activated modules during auto-activation to determine |
||
2443 | * if any of those modules are being deprecated. If so, close |
||
2444 | * them out, and add any replacement modules. |
||
2445 | * |
||
2446 | * Runs at priority 99 by default. |
||
2447 | * |
||
2448 | * This is run late, so that it can still activate a module if |
||
2449 | * the new module is a replacement for another that the user |
||
2450 | * currently has active, even if something at the normal priority |
||
2451 | * would kibosh everything. |
||
2452 | * |
||
2453 | * @since 2.6 |
||
2454 | * @uses jetpack_get_default_modules filter |
||
2455 | * @param array $modules |
||
2456 | * @return array |
||
2457 | */ |
||
2458 | function handle_deprecated_modules( $modules ) { |
||
2484 | |||
2485 | /** |
||
2486 | * Checks activated plugins during auto-activation to determine |
||
2487 | * if any of those plugins are in the list with a corresponding module |
||
2488 | * that is not compatible with the plugin. The module will not be allowed |
||
2489 | * to auto-activate. |
||
2490 | * |
||
2491 | * @since 2.6 |
||
2492 | * @uses jetpack_get_default_modules filter |
||
2493 | * @param array $modules |
||
2494 | * @return array |
||
2495 | */ |
||
2496 | function filter_default_modules( $modules ) { |
||
2520 | |||
2521 | /** |
||
2522 | * Extract a module's slug from its full path. |
||
2523 | */ |
||
2524 | public static function get_module_slug( $file ) { |
||
2527 | |||
2528 | /** |
||
2529 | * Generate a module's path from its slug. |
||
2530 | */ |
||
2531 | public static function get_module_path( $slug ) { |
||
2534 | |||
2535 | /** |
||
2536 | * Load module data from module file. Headers differ from WordPress |
||
2537 | * plugin headers to avoid them being identified as standalone |
||
2538 | * plugins on the WordPress plugins page. |
||
2539 | */ |
||
2540 | public static function get_module( $module ) { |
||
2631 | |||
2632 | /** |
||
2633 | * Like core's get_file_data implementation, but caches the result. |
||
2634 | */ |
||
2635 | public static function get_file_data( $file, $headers ) { |
||
2663 | |||
2664 | |||
2665 | /** |
||
2666 | * Return translated module tag. |
||
2667 | * |
||
2668 | * @param string $tag Tag as it appears in each module heading. |
||
2669 | * |
||
2670 | * @return mixed |
||
2671 | */ |
||
2672 | public static function translate_module_tag( $tag ) { |
||
2675 | |||
2676 | /** |
||
2677 | * Get i18n strings as a JSON-encoded string |
||
2678 | * |
||
2679 | * @return string The locale as JSON |
||
2680 | */ |
||
2681 | public static function get_i18n_data_json() { |
||
2682 | |||
2683 | // WordPress 5.0 uses md5 hashes of file paths to associate translation |
||
2684 | // JSON files with the file they should be included for. This is an md5 |
||
2685 | // of '_inc/build/admin.js'. |
||
2686 | $path_md5 = '1bac79e646a8bf4081a5011ab72d5807'; |
||
2687 | |||
2688 | $i18n_json = |
||
2689 | JETPACK__PLUGIN_DIR |
||
2690 | . 'languages/json/jetpack-' |
||
2691 | . get_user_locale() |
||
2692 | . '-' |
||
2693 | . $path_md5 |
||
2694 | . '.json'; |
||
2695 | |||
2696 | if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
||
2697 | $locale_data = @file_get_contents( $i18n_json ); |
||
2698 | if ( $locale_data ) { |
||
2699 | return $locale_data; |
||
2700 | } |
||
2701 | } |
||
2702 | |||
2703 | // Return valid empty Jed locale |
||
2704 | return '{ "locale_data": { "messages": { "": {} } } }'; |
||
2705 | } |
||
2706 | |||
2707 | /** |
||
2708 | * Add locale data setup to wp-i18n |
||
2709 | * |
||
2710 | * Any Jetpack script that depends on wp-i18n should use this method to set up the locale. |
||
2711 | * |
||
2712 | * The locale setup depends on an adding inline script. This is error-prone and could easily |
||
2713 | * result in multiple additions of the same script when exactly 0 or 1 is desireable. |
||
2714 | * |
||
2715 | * This method provides a safe way to request the setup multiple times but add the script at |
||
2716 | * most once. |
||
2717 | * |
||
2718 | * @since 6.7.0 |
||
2719 | * |
||
2720 | * @return void |
||
2721 | */ |
||
2722 | public static function setup_wp_i18n_locale_data() { |
||
2723 | static $script_added = false; |
||
2724 | if ( ! $script_added ) { |
||
2725 | $script_added = true; |
||
2726 | wp_add_inline_script( |
||
2727 | 'wp-i18n', |
||
2728 | 'wp.i18n.setLocaleData( ' . Jetpack::get_i18n_data_json() . ', \'jetpack\' );' |
||
2729 | ); |
||
2730 | } |
||
2731 | } |
||
2732 | |||
2733 | /** |
||
2734 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2735 | * |
||
2736 | * @since 3.9.2 |
||
2737 | * |
||
2738 | * @param array $modules |
||
2739 | * |
||
2740 | * @return string|void |
||
2741 | */ |
||
2742 | public static function get_translated_modules( $modules ) { |
||
2755 | |||
2756 | /** |
||
2757 | * Get a list of activated modules as an array of module slugs. |
||
2758 | */ |
||
2759 | public static function get_active_modules() { |
||
2791 | |||
2792 | /** |
||
2793 | * Check whether or not a Jetpack module is active. |
||
2794 | * |
||
2795 | * @param string $module The slug of a Jetpack module. |
||
2796 | * @return bool |
||
2797 | * |
||
2798 | * @static |
||
2799 | */ |
||
2800 | public static function is_module_active( $module ) { |
||
2803 | |||
2804 | public static function is_module( $module ) { |
||
2807 | |||
2808 | /** |
||
2809 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2810 | * |
||
2811 | * @param bool $catch True to start catching, False to stop. |
||
2812 | * |
||
2813 | * @static |
||
2814 | */ |
||
2815 | public static function catch_errors( $catch ) { |
||
2828 | |||
2829 | /** |
||
2830 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2831 | */ |
||
2832 | public static function catch_errors_on_shutdown() { |
||
2835 | |||
2836 | /** |
||
2837 | * Rewrite any string to make paths easier to read. |
||
2838 | * |
||
2839 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2840 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2841 | * |
||
2842 | * @param $string |
||
2843 | * @return mixed |
||
2844 | */ |
||
2845 | public static function alias_directories( $string ) { |
||
2853 | |||
2854 | public static function activate_default_modules( |
||
2992 | |||
2993 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
3080 | |||
3081 | function activate_module_actions( $module ) { |
||
3084 | |||
3085 | public static function deactivate_module( $module ) { |
||
3102 | |||
3103 | public static function enable_module_configurable( $module ) { |
||
3107 | |||
3108 | /** |
||
3109 | * Composes a module configure URL. It uses Jetpack settings search as default value |
||
3110 | * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter |
||
3111 | * |
||
3112 | * @param string $module Module slug |
||
3113 | * @return string $url module configuration URL |
||
3114 | */ |
||
3115 | public static function module_configuration_url( $module ) { |
||
3129 | |||
3130 | /* Installation */ |
||
3131 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
3171 | |||
3172 | /** |
||
3173 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
3174 | * @static |
||
3175 | */ |
||
3176 | public static function plugin_activation( $network_wide ) { |
||
3193 | |||
3194 | public static function get_activation_source( $referer_url ) { |
||
3243 | |||
3244 | /** |
||
3245 | * Runs before bumping version numbers up to a new version |
||
3246 | * @param string $version Version:timestamp |
||
3247 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
3248 | * @return null [description] |
||
3249 | */ |
||
3250 | public static function do_version_bump( $version, $old_version ) { |
||
3255 | |||
3256 | /** |
||
3257 | * Sets the internal version number and activation state. |
||
3258 | * @static |
||
3259 | */ |
||
3260 | public static function plugin_initialize() { |
||
3277 | |||
3278 | /** |
||
3279 | * Removes all connection options |
||
3280 | * @static |
||
3281 | */ |
||
3282 | public static function plugin_deactivation( ) { |
||
3291 | |||
3292 | /** |
||
3293 | * Disconnects from the Jetpack servers. |
||
3294 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
3295 | * @static |
||
3296 | */ |
||
3297 | public static function disconnect( $update_activated_state = true ) { |
||
3358 | |||
3359 | /** |
||
3360 | * Unlinks the current user from the linked WordPress.com user |
||
3361 | */ |
||
3362 | public static function unlink_user( $user_id = null ) { |
||
3393 | |||
3394 | /** |
||
3395 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
3396 | */ |
||
3397 | public static function try_registration() { |
||
3425 | |||
3426 | /** |
||
3427 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3428 | * |
||
3429 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3430 | */ |
||
3431 | public static function log( $code, $data = null ) { |
||
3471 | |||
3472 | /** |
||
3473 | * Get the internal event log. |
||
3474 | * |
||
3475 | * @param $event (string) - only return the specific log events |
||
3476 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3477 | * |
||
3478 | * @return array of log events || WP_Error for invalid params |
||
3479 | */ |
||
3480 | public static function get_log( $event = false, $num = false ) { |
||
3516 | |||
3517 | /** |
||
3518 | * Log modification of important settings. |
||
3519 | */ |
||
3520 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3527 | |||
3528 | /** |
||
3529 | * Return stat data for WPCOM sync |
||
3530 | */ |
||
3531 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3545 | |||
3546 | /** |
||
3547 | * Get additional stat data to sync to WPCOM |
||
3548 | */ |
||
3549 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3560 | |||
3561 | private static function get_site_user_count() { |
||
3576 | |||
3577 | /* Admin Pages */ |
||
3578 | |||
3579 | function admin_init() { |
||
3622 | |||
3623 | function admin_body_class( $admin_body_class = '' ) { |
||
3631 | |||
3632 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3635 | |||
3636 | /** |
||
3637 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3638 | * This function artificially throws errors for such cases (whitelisted). |
||
3639 | * |
||
3640 | * @param string $plugin The activated plugin. |
||
3641 | */ |
||
3642 | function throw_error_on_activate_plugin( $plugin ) { |
||
3666 | |||
3667 | function intercept_plugin_error_scrape_init() { |
||
3670 | |||
3671 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3682 | |||
3683 | function add_remote_request_handlers() { |
||
3687 | |||
3688 | function remote_request_handlers() { |
||
3728 | |||
3729 | /** |
||
3730 | * Uploads a file gotten from the global $_FILES. |
||
3731 | * If `$update_media_item` is true and `post_id` is defined |
||
3732 | * the attachment file of the media item (gotten through of the post_id) |
||
3733 | * will be updated instead of add a new one. |
||
3734 | * |
||
3735 | * @param boolean $update_media_item - update media attachment |
||
3736 | * @return array - An array describing the uploadind files process |
||
3737 | */ |
||
3738 | function upload_handler( $update_media_item = false ) { |
||
3860 | |||
3861 | /** |
||
3862 | * Add help to the Jetpack page |
||
3863 | * |
||
3864 | * @since Jetpack (1.2.3) |
||
3865 | * @return false if not the Jetpack page |
||
3866 | */ |
||
3867 | function admin_help() { |
||
3908 | |||
3909 | function admin_menu_css() { |
||
3912 | |||
3913 | function admin_menu_order() { |
||
3916 | |||
3917 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
3932 | |||
3933 | function admin_banner_styles() { |
||
3954 | |||
3955 | function plugin_action_links( $actions ) { |
||
3970 | |||
3971 | /* |
||
3972 | * Registration flow: |
||
3973 | * 1 - ::admin_page_load() action=register |
||
3974 | * 2 - ::try_registration() |
||
3975 | * 3 - ::register() |
||
3976 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
3977 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
3978 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
3979 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
3980 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
3981 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
3982 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
3983 | * jetpack_id, jetpack_secret, jetpack_public |
||
3984 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
3985 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
3986 | * 5 - user logs in with WP.com account |
||
3987 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
3988 | * - Jetpack_Client_Server::authorize() |
||
3989 | * - Jetpack_Client_Server::get_token() |
||
3990 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
3991 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
3992 | * - which responds with access_token, token_type, scope |
||
3993 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
3994 | * - Jetpack::activate_default_modules() |
||
3995 | * - Deactivates deprecated plugins |
||
3996 | * - Activates all default modules |
||
3997 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
3998 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
3999 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
4000 | * Done! |
||
4001 | */ |
||
4002 | |||
4003 | /** |
||
4004 | * Handles the page load events for the Jetpack admin page |
||
4005 | */ |
||
4006 | function admin_page_load() { |
||
4272 | |||
4273 | function admin_notices() { |
||
4365 | |||
4366 | /** |
||
4367 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4368 | */ |
||
4369 | function stat( $group, $detail ) { |
||
4374 | |||
4375 | /** |
||
4376 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4377 | */ |
||
4378 | function do_stats( $method = '' ) { |
||
4393 | |||
4394 | /** |
||
4395 | * Runs stats code for a one-off, server-side. |
||
4396 | * |
||
4397 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4398 | * |
||
4399 | * @return bool If it worked. |
||
4400 | */ |
||
4401 | static function do_server_side_stat( $args ) { |
||
4411 | |||
4412 | /** |
||
4413 | * Builds the stats url. |
||
4414 | * |
||
4415 | * @param $args array|string The arguments to append to the URL. |
||
4416 | * |
||
4417 | * @return string The URL to be pinged. |
||
4418 | */ |
||
4419 | static function build_stats_url( $args ) { |
||
4439 | |||
4440 | static function translate_current_user_to_role() { |
||
4449 | |||
4450 | static function translate_user_to_role( $user ) { |
||
4459 | |||
4460 | static function translate_role_to_cap( $role ) { |
||
4467 | |||
4468 | static function sign_role( $role, $user_id = null ) { |
||
4484 | |||
4485 | |||
4486 | /** |
||
4487 | * Builds a URL to the Jetpack connection auth page |
||
4488 | * |
||
4489 | * @since 3.9.5 |
||
4490 | * |
||
4491 | * @param bool $raw If true, URL will not be escaped. |
||
4492 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4493 | * If string, will be a custom redirect. |
||
4494 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4495 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
4496 | * |
||
4497 | * @return string Connect URL |
||
4498 | */ |
||
4499 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
4625 | |||
4626 | /** |
||
4627 | * Get our assumed site creation date. |
||
4628 | * Calculated based on the earlier date of either: |
||
4629 | * - Earliest admin user registration date. |
||
4630 | * - Earliest date of post of any post type. |
||
4631 | * |
||
4632 | * @since 7.2.0 |
||
4633 | * |
||
4634 | * @return string Assumed site creation date and time. |
||
4635 | */ |
||
4636 | public static function get_assumed_site_creation_date() { |
||
4663 | |||
4664 | public static function apply_activation_source_to_args( &$args ) { |
||
4675 | |||
4676 | function build_reconnect_url( $raw = false ) { |
||
4680 | |||
4681 | public static function admin_url( $args = null ) { |
||
4686 | |||
4687 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
4691 | |||
4692 | function dismiss_jetpack_notice() { |
||
4724 | |||
4725 | public static function sort_modules( $a, $b ) { |
||
4731 | |||
4732 | function ajax_recheck_ssl() { |
||
4740 | |||
4741 | /* Client API */ |
||
4742 | |||
4743 | /** |
||
4744 | * Returns the requested Jetpack API URL |
||
4745 | * |
||
4746 | * @return string |
||
4747 | */ |
||
4748 | public static function api_url( $relative_url ) { |
||
4751 | |||
4752 | /** |
||
4753 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
4754 | */ |
||
4755 | public static function fix_url_for_bad_hosts( $url ) { |
||
4771 | |||
4772 | /** |
||
4773 | * Create a random secret for validating onboarding payload |
||
4774 | * |
||
4775 | * @return string Secret token |
||
4776 | */ |
||
4777 | public static function create_onboarding_token() { |
||
4785 | |||
4786 | /** |
||
4787 | * Remove the onboarding token |
||
4788 | * |
||
4789 | * @return bool True on success, false on failure |
||
4790 | */ |
||
4791 | public static function invalidate_onboarding_token() { |
||
4794 | |||
4795 | /** |
||
4796 | * Validate an onboarding token for a specific action |
||
4797 | * |
||
4798 | * @return boolean True if token/action pair is accepted, false if not |
||
4799 | */ |
||
4800 | public static function validate_onboarding_token_action( $token, $action ) { |
||
4818 | |||
4819 | /** |
||
4820 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
4821 | * |
||
4822 | * @since 2.3.3 |
||
4823 | * @return boolean |
||
4824 | */ |
||
4825 | public static function permit_ssl( $force_recheck = false ) { |
||
4867 | |||
4868 | /* |
||
4869 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
4870 | */ |
||
4871 | public function alert_auto_ssl_fail() { |
||
4920 | |||
4921 | /** |
||
4922 | * Returns the Jetpack XML-RPC API |
||
4923 | * |
||
4924 | * @return string |
||
4925 | */ |
||
4926 | public static function xmlrpc_api_url() { |
||
4930 | |||
4931 | /** |
||
4932 | * Creates two secret tokens and the end of life timestamp for them. |
||
4933 | * |
||
4934 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
4935 | * |
||
4936 | * @since 2.6 |
||
4937 | * @return array |
||
4938 | */ |
||
4939 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
4965 | |||
4966 | public static function get_secrets( $action, $user_id ) { |
||
4981 | |||
4982 | public static function delete_secrets( $action, $user_id ) { |
||
4990 | |||
4991 | /** |
||
4992 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
4993 | * |
||
4994 | * Based on local php max_execution_time in php.ini |
||
4995 | * |
||
4996 | * @since 2.6 |
||
4997 | * @return int |
||
4998 | * @deprecated |
||
4999 | **/ |
||
5000 | public function get_remote_query_timeout_limit() { |
||
5004 | |||
5005 | /** |
||
5006 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5007 | * |
||
5008 | * Based on local php max_execution_time in php.ini |
||
5009 | * |
||
5010 | * @since 5.4 |
||
5011 | * @return int |
||
5012 | **/ |
||
5013 | public static function get_max_execution_time() { |
||
5022 | |||
5023 | /** |
||
5024 | * Sets a minimum request timeout, and returns the current timeout |
||
5025 | * |
||
5026 | * @since 5.4 |
||
5027 | **/ |
||
5028 | public static function set_min_time_limit( $min_timeout ) { |
||
5036 | |||
5037 | |||
5038 | /** |
||
5039 | * Takes the response from the Jetpack register new site endpoint and |
||
5040 | * verifies it worked properly. |
||
5041 | * |
||
5042 | * @since 2.6 |
||
5043 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
5044 | **/ |
||
5045 | public function validate_remote_register_response( $response ) { |
||
5085 | /** |
||
5086 | * @return bool|WP_Error |
||
5087 | */ |
||
5088 | public static function register() { |
||
5193 | |||
5194 | /** |
||
5195 | * If the db version is showing something other that what we've got now, bump it to current. |
||
5196 | * |
||
5197 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
5198 | */ |
||
5199 | public static function maybe_set_version_option() { |
||
5213 | |||
5214 | /* Client Server API */ |
||
5215 | |||
5216 | /** |
||
5217 | * Loads the Jetpack XML-RPC client |
||
5218 | */ |
||
5219 | public static function load_xml_rpc_client() { |
||
5223 | |||
5224 | /** |
||
5225 | * Resets the saved authentication state in between testing requests. |
||
5226 | */ |
||
5227 | public function reset_saved_auth_state() { |
||
5231 | |||
5232 | function verify_xml_rpc_signature() { |
||
5362 | |||
5363 | /** |
||
5364 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
5365 | */ |
||
5366 | function authenticate_jetpack( $user, $username, $password ) { |
||
5389 | |||
5390 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
5391 | // Uses the existing XMLRPC request signing implementation. |
||
5392 | function wp_rest_authenticate( $user ) { |
||
5467 | |||
5468 | /** |
||
5469 | * Report authentication status to the WP REST API. |
||
5470 | * |
||
5471 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5472 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5473 | */ |
||
5474 | public function wp_rest_authentication_errors( $value ) { |
||
5480 | |||
5481 | function add_nonce( $timestamp, $nonce ) { |
||
5519 | |||
5520 | /** |
||
5521 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5522 | * Capture it here so we can verify the signature later. |
||
5523 | */ |
||
5524 | function xmlrpc_methods( $methods ) { |
||
5528 | |||
5529 | function public_xmlrpc_methods( $methods ) { |
||
5535 | |||
5536 | function jetpack_getOptions( $args ) { |
||
5576 | |||
5577 | function xmlrpc_options( $options ) { |
||
5595 | |||
5596 | public static function clean_nonces( $all = false ) { |
||
5617 | |||
5618 | /** |
||
5619 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5620 | * SET: state( $key, $value ); |
||
5621 | * GET: $value = state( $key ); |
||
5622 | * |
||
5623 | * @param string $key |
||
5624 | * @param string $value |
||
5625 | * @param bool $restate private |
||
5626 | */ |
||
5627 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5677 | |||
5678 | public static function restate() { |
||
5681 | |||
5682 | public static function check_privacy( $file ) { |
||
5717 | |||
5718 | /** |
||
5719 | * Helper method for multicall XMLRPC. |
||
5720 | */ |
||
5721 | public static function xmlrpc_async_call() { |
||
5763 | |||
5764 | public static function staticize_subdomain( $url ) { |
||
5799 | |||
5800 | /* JSON API Authorization */ |
||
5801 | |||
5802 | /** |
||
5803 | * Handles the login action for Authorizing the JSON API |
||
5804 | */ |
||
5805 | function login_form_json_api_authorization() { |
||
5814 | |||
5815 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
5816 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
5829 | |||
5830 | // Make sure the POSTed request is handled by the same action |
||
5831 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
5835 | |||
5836 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
5837 | function store_json_api_authorization_token( $user_login, $user ) { |
||
5843 | |||
5844 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
5845 | function allow_wpcom_public_api_domain( $domains ) { |
||
5849 | |||
5850 | static function is_redirect_encoded( $redirect_url ) { |
||
5853 | |||
5854 | // Add all wordpress.com environments to the safe redirect whitelist |
||
5855 | function allow_wpcom_environments( $domains ) { |
||
5862 | |||
5863 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
5864 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
5876 | |||
5877 | |||
5878 | /** |
||
5879 | * Verifies the request by checking the signature |
||
5880 | * |
||
5881 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
5882 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
5883 | * |
||
5884 | * @param null|array $environment |
||
5885 | */ |
||
5886 | function verify_json_api_authorization_request( $environment = null ) { |
||
5990 | |||
5991 | function login_message_json_api_authorization( $message ) { |
||
5997 | |||
5998 | /** |
||
5999 | * Get $content_width, but with a <s>twist</s> filter. |
||
6000 | */ |
||
6001 | public static function get_content_width() { |
||
6012 | |||
6013 | /** |
||
6014 | * Pings the WordPress.com Mirror Site for the specified options. |
||
6015 | * |
||
6016 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
6017 | * |
||
6018 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
6019 | */ |
||
6020 | public function get_cloud_site_options( $option_names ) { |
||
6036 | |||
6037 | /** |
||
6038 | * Checks if the site is currently in an identity crisis. |
||
6039 | * |
||
6040 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
6041 | */ |
||
6042 | public static function check_identity_crisis() { |
||
6049 | |||
6050 | /** |
||
6051 | * Checks whether the home and siteurl specifically are whitelisted |
||
6052 | * Written so that we don't have re-check $key and $value params every time |
||
6053 | * we want to check if this site is whitelisted, for example in footer.php |
||
6054 | * |
||
6055 | * @since 3.8.0 |
||
6056 | * @return bool True = already whitelisted False = not whitelisted |
||
6057 | */ |
||
6058 | public static function is_staging_site() { |
||
6117 | |||
6118 | /** |
||
6119 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
6120 | * |
||
6121 | * @since 4.4.0 |
||
6122 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
6123 | * |
||
6124 | * @return bool |
||
6125 | */ |
||
6126 | public static function validate_sync_error_idc_option() { |
||
6170 | |||
6171 | /** |
||
6172 | * Normalizes a url by doing three things: |
||
6173 | * - Strips protocol |
||
6174 | * - Strips www |
||
6175 | * - Adds a trailing slash |
||
6176 | * |
||
6177 | * @since 4.4.0 |
||
6178 | * @param string $url |
||
6179 | * @return WP_Error|string |
||
6180 | */ |
||
6181 | public static function normalize_url_protocol_agnostic( $url ) { |
||
6191 | |||
6192 | /** |
||
6193 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
6194 | * |
||
6195 | * @since 4.4.0 |
||
6196 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
6197 | * |
||
6198 | * @param array $response |
||
6199 | * @return array Array of the local urls, wpcom urls, and error code |
||
6200 | */ |
||
6201 | public static function get_sync_error_idc_option( $response = array() ) { |
||
6234 | |||
6235 | /** |
||
6236 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
6237 | * If set to true, the site will be put into staging mode. |
||
6238 | * |
||
6239 | * @since 4.3.2 |
||
6240 | * @return bool |
||
6241 | */ |
||
6242 | public static function sync_idc_optin() { |
||
6260 | |||
6261 | /** |
||
6262 | * Maybe Use a .min.css stylesheet, maybe not. |
||
6263 | * |
||
6264 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
6265 | */ |
||
6266 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
6308 | |||
6309 | /** |
||
6310 | * If the asset is minified, let's flag .min as the suffix. |
||
6311 | * |
||
6312 | * Attached to `style_loader_src` filter. |
||
6313 | * |
||
6314 | * @param string $tag The tag that would link to the external asset. |
||
6315 | * @param string $handle The registered handle of the script in question. |
||
6316 | * @param string $href The url of the asset in question. |
||
6317 | */ |
||
6318 | public static function set_suffix_on_min( $src, $handle ) { |
||
6334 | |||
6335 | /** |
||
6336 | * Maybe inlines a stylesheet. |
||
6337 | * |
||
6338 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
6339 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
6340 | * |
||
6341 | * Attached to `style_loader_tag` filter. |
||
6342 | * |
||
6343 | * @param string $tag The tag that would link to the external asset. |
||
6344 | * @param string $handle The registered handle of the script in question. |
||
6345 | * |
||
6346 | * @return string |
||
6347 | */ |
||
6348 | public static function maybe_inline_style( $tag, $handle ) { |
||
6398 | |||
6399 | /** |
||
6400 | * Loads a view file from the views |
||
6401 | * |
||
6402 | * Data passed in with the $data parameter will be available in the |
||
6403 | * template file as $data['value'] |
||
6404 | * |
||
6405 | * @param string $template - Template file to load |
||
6406 | * @param array $data - Any data to pass along to the template |
||
6407 | * @return boolean - If template file was found |
||
6408 | **/ |
||
6409 | public function load_view( $template, $data = array() ) { |
||
6420 | |||
6421 | /** |
||
6422 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
6423 | */ |
||
6424 | public function deprecated_hooks() { |
||
6492 | |||
6493 | /** |
||
6494 | * Converts any url in a stylesheet, to the correct absolute url. |
||
6495 | * |
||
6496 | * Considerations: |
||
6497 | * - Normal, relative URLs `feh.png` |
||
6498 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
6499 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
6500 | * - Absolute URLs `http://domain.com/feh.png` |
||
6501 | * - Domain root relative URLs `/feh.png` |
||
6502 | * |
||
6503 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
6504 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
6505 | * |
||
6506 | * @return mixed|string |
||
6507 | */ |
||
6508 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6552 | |||
6553 | /** |
||
6554 | * This methods removes all of the registered css files on the front end |
||
6555 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6556 | * all the files into one file. |
||
6557 | * |
||
6558 | * Pros: |
||
6559 | * - Uses only ONE css asset connection instead of 15 |
||
6560 | * - Saves a minimum of 56k |
||
6561 | * - Reduces server load |
||
6562 | * - Reduces time to first painted byte |
||
6563 | * |
||
6564 | * Cons: |
||
6565 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6566 | * should not cause any issues with themes. |
||
6567 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6568 | * jetpack_implode_frontend_css filter for a workaround |
||
6569 | * |
||
6570 | * For some situations developers may wish to disable css imploding and |
||
6571 | * instead operate in legacy mode where each file loads seperately and |
||
6572 | * can be edited individually or dequeued. This can be accomplished with |
||
6573 | * the following line: |
||
6574 | * |
||
6575 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6576 | * |
||
6577 | * @since 3.2 |
||
6578 | **/ |
||
6579 | public function implode_frontend_css( $travis_test = false ) { |
||
6636 | |||
6637 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
6647 | |||
6648 | /* |
||
6649 | * Check the heartbeat data |
||
6650 | * |
||
6651 | * Organizes the heartbeat data by severity. For example, if the site |
||
6652 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
6653 | * |
||
6654 | * Data will be added to "caution" array, if it either: |
||
6655 | * - Out of date Jetpack version |
||
6656 | * - Out of date WP version |
||
6657 | * - Out of date PHP version |
||
6658 | * |
||
6659 | * $return array $filtered_data |
||
6660 | */ |
||
6661 | public static function jetpack_check_heartbeat_data() { |
||
6714 | |||
6715 | |||
6716 | /* |
||
6717 | * This method is used to organize all options that can be reset |
||
6718 | * without disconnecting Jetpack. |
||
6719 | * |
||
6720 | * It is used in class.jetpack-cli.php to reset options |
||
6721 | * |
||
6722 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
6723 | * |
||
6724 | * @return array of options to delete. |
||
6725 | */ |
||
6726 | public static function get_jetpack_options_for_reset() { |
||
6729 | |||
6730 | /* |
||
6731 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
6732 | * so we can bring them directly to their site in calypso. |
||
6733 | * |
||
6734 | * @param string | url |
||
6735 | * @return string | url without the guff |
||
6736 | */ |
||
6737 | public static function build_raw_urls( $url ) { |
||
6743 | |||
6744 | /** |
||
6745 | * Stores and prints out domains to prefetch for page speed optimization. |
||
6746 | * |
||
6747 | * @param mixed $new_urls |
||
6748 | */ |
||
6749 | public static function dns_prefetch( $new_urls = null ) { |
||
6766 | |||
6767 | public function wp_dashboard_setup() { |
||
6800 | |||
6801 | /** |
||
6802 | * @param mixed $result Value for the user's option |
||
6803 | * @return mixed |
||
6804 | */ |
||
6805 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
6830 | |||
6831 | public static function dashboard_widget() { |
||
6839 | |||
6840 | public static function dashboard_widget_footer() { |
||
6873 | |||
6874 | /** |
||
6875 | * Return string containing the Jetpack logo. |
||
6876 | * |
||
6877 | * @since 3.9.0 |
||
6878 | * |
||
6879 | * @param bool $logotype Should we use the full logotype (logo + text). Default to false. |
||
6880 | * |
||
6881 | * @return string |
||
6882 | */ |
||
6883 | public static function get_jp_emblem( $logotype = false ) { |
||
6901 | |||
6902 | /* |
||
6903 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
6904 | */ |
||
6905 | function jetpack_icon_user_connected( $columns ) { |
||
6909 | |||
6910 | /* |
||
6911 | * Show Jetpack icon if the user is linked. |
||
6912 | */ |
||
6913 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
6925 | |||
6926 | /* |
||
6927 | * Style the Jetpack user column |
||
6928 | */ |
||
6929 | function jetpack_user_col_style() { |
||
6946 | |||
6947 | /** |
||
6948 | * Checks if Akismet is active and working. |
||
6949 | * |
||
6950 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
6951 | * that implied usage of methods present since more recent version. |
||
6952 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
6953 | * |
||
6954 | * @since 5.1.0 |
||
6955 | * |
||
6956 | * @return bool True = Akismet available. False = Aksimet not available. |
||
6957 | */ |
||
6958 | public static function is_akismet_active() { |
||
6993 | |||
6994 | /** |
||
6995 | * Checks if one or more function names is in debug_backtrace |
||
6996 | * |
||
6997 | * @param $names Mixed string name of function or array of string names of functions |
||
6998 | * |
||
6999 | * @return bool |
||
7000 | */ |
||
7001 | public static function is_function_in_backtrace( $names ) { |
||
7024 | |||
7025 | /** |
||
7026 | * Given a minified path, and a non-minified path, will return |
||
7027 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
7028 | * |
||
7029 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
7030 | * root Jetpack directory. |
||
7031 | * |
||
7032 | * @since 5.6.0 |
||
7033 | * |
||
7034 | * @param string $min_path |
||
7035 | * @param string $non_min_path |
||
7036 | * @return string The URL to the file |
||
7037 | */ |
||
7038 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
7045 | |||
7046 | /** |
||
7047 | * Checks for whether Jetpack Backup & Scan is enabled. |
||
7048 | * Will return true if the state of Backup & Scan is anything except "unavailable". |
||
7049 | * @return bool|int|mixed |
||
7050 | */ |
||
7051 | public static function is_rewind_enabled() { |
||
7070 | |||
7071 | /** |
||
7072 | * Return Calypso environment value; used for developing Jetpack and pairing |
||
7073 | * it with different Calypso enrionments, such as localhost. |
||
7074 | * |
||
7075 | * @since 7.4.0 |
||
7076 | * |
||
7077 | * @return string Calypso environment |
||
7078 | */ |
||
7079 | public static function get_calypso_env() { |
||
7094 | |||
7095 | /** |
||
7096 | * Checks whether or not TOS has been agreed upon. |
||
7097 | * Will return true if a user has clicked to register, or is already connected. |
||
7098 | */ |
||
7099 | public static function jetpack_tos_agreed() { |
||
7102 | |||
7103 | /** |
||
7104 | * Handles activating default modules as well general cleanup for the new connection. |
||
7105 | * |
||
7106 | * @param boolean $activate_sso Whether to activate the SSO module when activating default modules. |
||
7107 | * @param boolean $redirect_on_activation_error Whether to redirect on activation error. |
||
7108 | * @param boolean $send_state_messages Whether to send state messages. |
||
7109 | * @return void |
||
7110 | */ |
||
7111 | public static function handle_post_authorization_actions( |
||
7140 | |||
7141 | /** |
||
7142 | * Returns a boolean for whether backups UI should be displayed or not. |
||
7143 | * |
||
7144 | * @return bool Should backups UI be displayed? |
||
7145 | */ |
||
7146 | public static function show_backups_ui() { |
||
7156 | |||
7157 | /* |
||
7158 | * Deprecated manage functions |
||
7159 | */ |
||
7160 | function prepare_manage_jetpack_notice() { |
||
7181 | |||
7182 | /** |
||
7183 | * Clean leftoveruser meta. |
||
7184 | * |
||
7185 | * Delete Jetpack-related user meta when it is no longer needed. |
||
7186 | * |
||
7187 | * @since 7.3.0 |
||
7188 | * |
||
7189 | * @param int $user_id User ID being updated. |
||
7190 | */ |
||
7191 | public static function user_meta_cleanup( $user_id ) { |
||
7206 | } |
||
7207 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.