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 | public $concatenated_style_handles = array( |
||
39 | 'jetpack-carousel', |
||
40 | 'grunion.css', |
||
41 | 'the-neverending-homepage', |
||
42 | 'jetpack_likes', |
||
43 | 'jetpack_related-posts', |
||
44 | 'sharedaddy', |
||
45 | 'jetpack-slideshow', |
||
46 | 'presentations', |
||
47 | 'jetpack-subscriptions', |
||
48 | 'jetpack-responsive-videos-style', |
||
49 | 'jetpack-social-menu', |
||
50 | 'tiled-gallery', |
||
51 | 'jetpack_display_posts_widget', |
||
52 | 'gravatar-profile-widget', |
||
53 | 'goodreads-widget', |
||
54 | 'jetpack_social_media_icons_widget', |
||
55 | 'jetpack-top-posts-widget', |
||
56 | 'jetpack_image_widget', |
||
57 | 'jetpack-my-community-widget', |
||
58 | 'wordads', |
||
59 | 'eu-cookie-law-style', |
||
60 | 'flickr-widget-style', |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * Contains all assets that have had their URL rewritten to minified versions. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | static $min_assets = array(); |
||
|
|||
69 | |||
70 | public $plugins_to_deactivate = array( |
||
71 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
72 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
73 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
74 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
75 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
76 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
77 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
78 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
79 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
80 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
81 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
82 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
83 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
84 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
85 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
86 | ); |
||
87 | |||
88 | static $capability_translations = array( |
||
89 | 'administrator' => 'manage_options', |
||
90 | 'editor' => 'edit_others_posts', |
||
91 | 'author' => 'publish_posts', |
||
92 | 'contributor' => 'edit_posts', |
||
93 | 'subscriber' => 'read', |
||
94 | ); |
||
95 | |||
96 | /** |
||
97 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
98 | * if the plugins are active. Used by filter_default_modules |
||
99 | * |
||
100 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
101 | * change `module-slug` and add this to your plugin: |
||
102 | * |
||
103 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
104 | * function my_jetpack_get_default_modules( $modules ) { |
||
105 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
106 | * } |
||
107 | * |
||
108 | * @var array |
||
109 | */ |
||
110 | private $conflicting_plugins = array( |
||
111 | 'comments' => array( |
||
112 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
113 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
114 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
115 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
116 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
117 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
118 | ), |
||
119 | 'comment-likes' => array( |
||
120 | 'Epoch' => 'epoch/plugincore.php', |
||
121 | ), |
||
122 | 'contact-form' => array( |
||
123 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
124 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
125 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
126 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
127 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
128 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
129 | ), |
||
130 | 'minileven' => array( |
||
131 | 'WPtouch' => 'wptouch/wptouch.php', |
||
132 | ), |
||
133 | 'latex' => array( |
||
134 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
135 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
136 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
137 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
138 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
139 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
140 | ), |
||
141 | 'protect' => array( |
||
142 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
143 | 'Captcha' => 'captcha/captcha.php', |
||
144 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
145 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
146 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
147 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
148 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
149 | 'Security-protection' => 'security-protection/security-protection.php', |
||
150 | 'Login Security' => 'login-security/login-security.php', |
||
151 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
152 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
153 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
154 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
155 | ), |
||
156 | 'random-redirect' => array( |
||
157 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
158 | ), |
||
159 | 'related-posts' => array( |
||
160 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
161 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
162 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
163 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
164 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
165 | 'outbrain' => 'outbrain/outbrain.php', |
||
166 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
167 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
168 | ), |
||
169 | 'sharedaddy' => array( |
||
170 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
171 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
172 | 'ShareThis' => 'share-this/sharethis.php', |
||
173 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
174 | ), |
||
175 | 'seo-tools' => array( |
||
176 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
177 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
178 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
179 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
180 | ), |
||
181 | 'verification-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 | ), |
||
187 | 'widget-visibility' => array( |
||
188 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
189 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
190 | ), |
||
191 | 'sitemaps' => array( |
||
192 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
193 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
194 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
195 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
196 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
197 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
198 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
199 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
200 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
201 | 'Sitemap' => 'sitemap/sitemap.php', |
||
202 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
203 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
204 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
205 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
206 | ), |
||
207 | 'lazy-images' => array( |
||
208 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
209 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
210 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
211 | ), |
||
212 | ); |
||
213 | |||
214 | /** |
||
215 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
216 | * |
||
217 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
218 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
219 | * |
||
220 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
221 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
222 | */ |
||
223 | private $open_graph_conflicting_plugins = array( |
||
224 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
225 | // 2 Click Social Media Buttons |
||
226 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
227 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
228 | 'autodescription/autodescription.php', // The SEO Framework |
||
229 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
230 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
231 | // Open Graph Meta Tags by Heateor |
||
232 | 'facebook/facebook.php', // Facebook (official plugin) |
||
233 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
234 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
235 | // Facebook Featured Image & OG Meta Tags |
||
236 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
237 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
238 | // Facebook Open Graph Meta Tags for WordPress |
||
239 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
240 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
241 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
242 | // Fedmich's Facebook Open Graph Meta |
||
243 | 'network-publisher/networkpub.php', // Network Publisher |
||
244 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
245 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
246 | // NextScripts SNAP |
||
247 | 'og-tags/og-tags.php', // OG Tags |
||
248 | 'opengraph/opengraph.php', // Open Graph |
||
249 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
250 | // Open Graph Protocol Framework |
||
251 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
252 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
253 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
254 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
255 | 'sharepress/sharepress.php', // SharePress |
||
256 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
257 | 'social-discussions/social-discussions.php', // Social Discussions |
||
258 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
259 | 'socialize/socialize.php', // Socialize |
||
260 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
261 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
262 | // Tweet, Like, Google +1 and Share |
||
263 | 'wordbooker/wordbooker.php', // Wordbooker |
||
264 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
265 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
266 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
267 | // WP Facebook Like Send & Open Graph Meta |
||
268 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
269 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
270 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
271 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php' // WP Facebook Like Button |
||
272 | ); |
||
273 | |||
274 | /** |
||
275 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
276 | */ |
||
277 | private $twitter_cards_conflicting_plugins = array( |
||
278 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
279 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
280 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
281 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
282 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
283 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
284 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
285 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
286 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
287 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
288 | ); |
||
289 | |||
290 | /** |
||
291 | * Message to display in admin_notice |
||
292 | * @var string |
||
293 | */ |
||
294 | public $message = ''; |
||
295 | |||
296 | /** |
||
297 | * Error to display in admin_notice |
||
298 | * @var string |
||
299 | */ |
||
300 | public $error = ''; |
||
301 | |||
302 | /** |
||
303 | * Modules that need more privacy description. |
||
304 | * @var string |
||
305 | */ |
||
306 | public $privacy_checks = ''; |
||
307 | |||
308 | /** |
||
309 | * Stats to record once the page loads |
||
310 | * |
||
311 | * @var array |
||
312 | */ |
||
313 | public $stats = array(); |
||
314 | |||
315 | /** |
||
316 | * Jetpack_Sync object |
||
317 | */ |
||
318 | public $sync; |
||
319 | |||
320 | /** |
||
321 | * Verified data for JSON authorization request |
||
322 | */ |
||
323 | public $json_api_authorization_request = array(); |
||
324 | |||
325 | /** |
||
326 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
327 | */ |
||
328 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
329 | |||
330 | /** |
||
331 | * Holds the singleton instance of this class |
||
332 | * @since 2.3.3 |
||
333 | * @var Jetpack |
||
334 | */ |
||
335 | static $instance = false; |
||
336 | |||
337 | /** |
||
338 | * Singleton |
||
339 | * @static |
||
340 | */ |
||
341 | public static function init() { |
||
342 | if ( ! self::$instance ) { |
||
343 | self::$instance = new Jetpack; |
||
344 | |||
345 | self::$instance->plugin_upgrade(); |
||
346 | } |
||
347 | |||
348 | return self::$instance; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Must never be called statically |
||
353 | */ |
||
354 | function plugin_upgrade() { |
||
355 | if ( Jetpack::is_active() ) { |
||
356 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
357 | if ( JETPACK__VERSION != $version ) { |
||
358 | // Prevent multiple upgrades at once - only a single process should trigger |
||
359 | // an upgrade to avoid stampedes |
||
360 | if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) { |
||
361 | return; |
||
362 | } |
||
363 | |||
364 | // Set a short lock to prevent multiple instances of the upgrade |
||
365 | set_transient( self::$plugin_upgrade_lock_key, 1, 10 ); |
||
366 | |||
367 | // check which active modules actually exist and remove others from active_modules list |
||
368 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
369 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
370 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
371 | Jetpack::update_active_modules( $modules ); |
||
372 | } |
||
373 | |||
374 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
375 | |||
376 | // Upgrade to 4.3.0 |
||
377 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
378 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
379 | } |
||
380 | |||
381 | // Make sure Markdown for posts gets turned back on |
||
382 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
383 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
384 | } |
||
385 | |||
386 | if ( did_action( 'wp_loaded' ) ) { |
||
387 | self::upgrade_on_load(); |
||
388 | } else { |
||
389 | add_action( |
||
390 | 'wp_loaded', |
||
391 | array( __CLASS__, 'upgrade_on_load' ) |
||
392 | ); |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Runs upgrade routines that need to have modules loaded. |
||
400 | */ |
||
401 | static function upgrade_on_load() { |
||
402 | |||
403 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
404 | // This can happen in case Jetpack has been just upgraded and is |
||
405 | // being initialized late during the page load. In this case we wait |
||
406 | // until the next proper admin page load with Jetpack active. |
||
407 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
408 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
409 | |||
410 | return; |
||
411 | } |
||
412 | |||
413 | Jetpack::maybe_set_version_option(); |
||
414 | |||
415 | if ( class_exists( 'Jetpack_Widget_Conditions' ) ) { |
||
416 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
417 | } |
||
418 | |||
419 | if ( |
||
420 | class_exists( 'Jetpack_Sitemap_Manager' ) |
||
421 | && version_compare( JETPACK__VERSION, '5.3', '>=' ) |
||
422 | ) { |
||
423 | do_action( 'jetpack_sitemaps_purge_data' ); |
||
424 | } |
||
425 | |||
426 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
427 | } |
||
428 | |||
429 | static function activate_manage( ) { |
||
430 | if ( did_action( 'init' ) || current_filter() == 'init' ) { |
||
431 | self::activate_module( 'manage', false, false ); |
||
432 | } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) { |
||
433 | add_action( 'init', array( __CLASS__, 'activate_manage' ) ); |
||
434 | } |
||
435 | } |
||
436 | |||
437 | static function update_active_modules( $modules ) { |
||
438 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
439 | |||
440 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $modules ) ); |
||
441 | |||
442 | if ( is_array( $modules ) && is_array( $current_modules ) ) { |
||
443 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
444 | foreach( $new_active_modules as $module ) { |
||
445 | /** |
||
446 | * Fires when a specific module is activated. |
||
447 | * |
||
448 | * @since 1.9.0 |
||
449 | * |
||
450 | * @param string $module Module slug. |
||
451 | * @param boolean $success whether the module was activated. @since 4.2 |
||
452 | */ |
||
453 | do_action( 'jetpack_activate_module', $module, $success ); |
||
454 | |||
455 | /** |
||
456 | * Fires when a module is activated. |
||
457 | * The dynamic part of the filter, $module, is the module slug. |
||
458 | * |
||
459 | * @since 1.9.0 |
||
460 | * |
||
461 | * @param string $module Module slug. |
||
462 | */ |
||
463 | do_action( "jetpack_activate_module_$module", $module ); |
||
464 | } |
||
465 | |||
466 | $new_deactive_modules = array_diff( $current_modules, $modules ); |
||
467 | foreach( $new_deactive_modules as $module ) { |
||
468 | /** |
||
469 | * Fired after a module has been deactivated. |
||
470 | * |
||
471 | * @since 4.2.0 |
||
472 | * |
||
473 | * @param string $module Module slug. |
||
474 | * @param boolean $success whether the module was deactivated. |
||
475 | */ |
||
476 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
477 | /** |
||
478 | * Fires when a module is deactivated. |
||
479 | * The dynamic part of the filter, $module, is the module slug. |
||
480 | * |
||
481 | * @since 1.9.0 |
||
482 | * |
||
483 | * @param string $module Module slug. |
||
484 | */ |
||
485 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
486 | } |
||
487 | } |
||
488 | |||
489 | return $success; |
||
490 | } |
||
491 | |||
492 | static function delete_active_modules() { |
||
493 | self::update_active_modules( array() ); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Constructor. Initializes WordPress hooks |
||
498 | */ |
||
499 | private function __construct() { |
||
500 | /* |
||
501 | * Check for and alert any deprecated hooks |
||
502 | */ |
||
503 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
504 | |||
505 | /* |
||
506 | * Enable enhanced handling of previewing sites in Calypso |
||
507 | */ |
||
508 | if ( Jetpack::is_active() ) { |
||
509 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php'; |
||
510 | add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 ); |
||
511 | } |
||
512 | |||
513 | /* |
||
514 | * Load things that should only be in Network Admin. |
||
515 | * |
||
516 | * For now blow away everything else until a more full |
||
517 | * understanding of what is needed at the network level is |
||
518 | * available |
||
519 | */ |
||
520 | if( is_multisite() ) { |
||
521 | Jetpack_Network::init(); |
||
522 | } |
||
523 | |||
524 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
525 | |||
526 | // Unlink user before deleting the user from .com |
||
527 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
528 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
529 | |||
530 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
531 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
532 | |||
533 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
534 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
535 | |||
536 | $this->require_jetpack_authentication(); |
||
537 | |||
538 | if ( Jetpack::is_active() ) { |
||
539 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
540 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
541 | |||
542 | $signed = $this->verify_xml_rpc_signature(); |
||
543 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
544 | // The actual API methods. |
||
545 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
546 | } else { |
||
547 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
548 | // active Jetpack connection, so that additional users can link their account. |
||
549 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
550 | } |
||
551 | } else { |
||
552 | // The bootstrap API methods. |
||
553 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
554 | } |
||
555 | |||
556 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
557 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
558 | } elseif ( |
||
559 | is_admin() && |
||
560 | isset( $_POST['action'] ) && ( |
||
561 | 'jetpack_upload_file' == $_POST['action'] || |
||
562 | 'jetpack_update_file' == $_POST['action'] |
||
563 | ) |
||
564 | ) { |
||
565 | $this->require_jetpack_authentication(); |
||
566 | $this->add_remote_request_handlers(); |
||
567 | } else { |
||
568 | if ( Jetpack::is_active() ) { |
||
569 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
570 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
571 | } |
||
572 | } |
||
573 | |||
574 | if ( Jetpack::is_active() ) { |
||
575 | Jetpack_Heartbeat::init(); |
||
576 | } |
||
577 | |||
578 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
579 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
580 | |||
581 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
582 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
583 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
584 | } |
||
585 | |||
586 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
587 | |||
588 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
589 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
590 | |||
591 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
592 | |||
593 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
594 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
595 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
596 | |||
597 | // returns HTTPS support status |
||
598 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
599 | |||
600 | // If any module option is updated before Jump Start is dismissed, hide Jump Start. |
||
601 | add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) ); |
||
602 | |||
603 | // JITM AJAX callback function |
||
604 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
605 | |||
606 | // Universal ajax callback for all tracking events triggered via js |
||
607 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
608 | |||
609 | add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) ); |
||
610 | |||
611 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
612 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
613 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
614 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
615 | |||
616 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
617 | |||
618 | /** |
||
619 | * These actions run checks to load additional files. |
||
620 | * They check for external files or plugins, so they need to run as late as possible. |
||
621 | */ |
||
622 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
623 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
624 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
625 | |||
626 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
627 | add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 ); |
||
628 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
629 | |||
630 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
631 | |||
632 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
633 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
634 | |||
635 | // A filter to control all just in time messages |
||
636 | add_filter( 'jetpack_just_in_time_msgs', '__return_true', 9 ); |
||
637 | add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9); |
||
638 | |||
639 | // If enabled, point edit post and page links to Calypso instead of WP-Admin. |
||
640 | // We should make sure to only do this for front end links. |
||
641 | if ( Jetpack_Options::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { |
||
642 | add_filter( 'get_edit_post_link', array( $this, 'point_edit_links_to_calypso' ), 1, 2 ); |
||
643 | } |
||
644 | |||
645 | // Update the Jetpack plan from API on heartbeats |
||
646 | add_action( 'jetpack_heartbeat', array( $this, 'refresh_active_plan_from_wpcom' ) ); |
||
647 | |||
648 | /** |
||
649 | * This is the hack to concatinate all css files into one. |
||
650 | * For description and reasoning see the implode_frontend_css method |
||
651 | * |
||
652 | * Super late priority so we catch all the registered styles |
||
653 | */ |
||
654 | if( !is_admin() ) { |
||
655 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
656 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * These are sync actions that we need to keep track of for jitms |
||
661 | */ |
||
662 | add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 ); |
||
663 | |||
664 | // Actually push the stats on shutdown. |
||
665 | if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) { |
||
666 | add_action( 'shutdown', array( $this, 'push_stats' ) ); |
||
667 | } |
||
668 | } |
||
669 | |||
670 | function point_edit_links_to_calypso( $default_url, $post_id ) { |
||
671 | $post = get_post( $post_id ); |
||
672 | |||
673 | if ( empty( $post ) ) { |
||
674 | return $default_url; |
||
675 | } |
||
676 | |||
677 | $post_type = $post->post_type; |
||
678 | |||
679 | // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso. |
||
680 | // https://en.support.wordpress.com/custom-post-types/ |
||
681 | $allowed_post_types = array( |
||
682 | 'post' => 'post', |
||
683 | 'page' => 'page', |
||
684 | 'jetpack-portfolio' => 'edit/jetpack-portfolio', |
||
685 | 'jetpack-testimonial' => 'edit/jetpack-testimonial', |
||
686 | ); |
||
687 | |||
688 | if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) { |
||
689 | return $default_url; |
||
690 | } |
||
691 | |||
692 | $path_prefix = $allowed_post_types[ $post_type ]; |
||
693 | |||
694 | $site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
695 | |||
696 | return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); |
||
697 | } |
||
698 | |||
699 | function jetpack_track_last_sync_callback( $params ) { |
||
700 | /** |
||
701 | * Filter to turn off jitm caching |
||
702 | * |
||
703 | * @since 5.4.0 |
||
704 | * |
||
705 | * @param bool false Whether to cache just in time messages |
||
706 | */ |
||
707 | if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
708 | return $params; |
||
709 | } |
||
710 | |||
711 | if ( is_array( $params ) && isset( $params[0] ) ) { |
||
712 | $option = $params[0]; |
||
713 | if ( 'active_plugins' === $option ) { |
||
714 | // use the cache if we can, but not terribly important if it gets evicted |
||
715 | set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS ); |
||
716 | } |
||
717 | } |
||
718 | |||
719 | return $params; |
||
720 | } |
||
721 | |||
722 | function jetpack_connection_banner_callback() { |
||
723 | check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' ); |
||
724 | |||
725 | if ( isset( $_REQUEST['dismissBanner'] ) ) { |
||
726 | Jetpack_Options::update_option( 'dismissed_connection_banner', 1 ); |
||
727 | wp_send_json_success(); |
||
728 | } |
||
729 | |||
730 | wp_die(); |
||
731 | } |
||
732 | |||
733 | function jetpack_admin_ajax_tracks_callback() { |
||
734 | // Check for nonce |
||
735 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
736 | wp_die( 'Permissions check failed.' ); |
||
737 | } |
||
738 | |||
739 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
740 | wp_die( 'No valid event name or type.' ); |
||
741 | } |
||
742 | |||
743 | $tracks_data = array(); |
||
744 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
745 | if ( is_array( $_REQUEST['tracksEventProp'] ) ) { |
||
746 | $tracks_data = $_REQUEST['tracksEventProp']; |
||
747 | } else { |
||
748 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
749 | } |
||
750 | } |
||
751 | |||
752 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
753 | wp_send_json_success(); |
||
754 | wp_die(); |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * The callback for the JITM ajax requests. |
||
759 | */ |
||
760 | function jetpack_jitm_ajax_callback() { |
||
761 | // Check for nonce |
||
762 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
763 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
764 | } |
||
765 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
766 | $module_slug = $_REQUEST['jitmModule']; |
||
767 | Jetpack::log( 'activate', $module_slug ); |
||
768 | Jetpack::activate_module( $module_slug, false, false ); |
||
769 | Jetpack::state( 'message', 'no_message' ); |
||
770 | |||
771 | //A Jetpack module is being activated through a JITM, track it |
||
772 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
773 | $this->do_stats( 'server_side' ); |
||
774 | |||
775 | wp_send_json_success(); |
||
776 | } |
||
777 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
778 | // get the hide_jitm options array |
||
779 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
780 | $module_slug = $_REQUEST['jitmModule']; |
||
781 | |||
782 | if( ! $jetpack_hide_jitm ) { |
||
783 | $jetpack_hide_jitm = array( |
||
784 | $module_slug => 'hide' |
||
785 | ); |
||
786 | } else { |
||
787 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
788 | } |
||
789 | |||
790 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
791 | |||
792 | //jitm is being dismissed forever, track it |
||
793 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
794 | $this->do_stats( 'server_side' ); |
||
795 | |||
796 | wp_send_json_success(); |
||
797 | } |
||
798 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
799 | $module_slug = $_REQUEST['jitmModule']; |
||
800 | |||
801 | // User went to WordPress.com, track this |
||
802 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
803 | $this->do_stats( 'server_side' ); |
||
804 | |||
805 | wp_send_json_success(); |
||
806 | } |
||
807 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
808 | $track = $_REQUEST['jitmModule']; |
||
809 | |||
810 | // User is viewing JITM, track it. |
||
811 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
812 | $this->do_stats( 'server_side' ); |
||
813 | |||
814 | wp_send_json_success(); |
||
815 | } |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
820 | */ |
||
821 | function push_stats() { |
||
822 | if ( ! empty( $this->stats ) ) { |
||
823 | $this->do_stats( 'server_side' ); |
||
824 | } |
||
825 | } |
||
826 | |||
827 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
828 | switch( $cap ) { |
||
829 | case 'jetpack_connect' : |
||
830 | case 'jetpack_reconnect' : |
||
831 | if ( Jetpack::is_development_mode() ) { |
||
832 | $caps = array( 'do_not_allow' ); |
||
833 | break; |
||
834 | } |
||
835 | /** |
||
836 | * Pass through. If it's not development mode, these should match disconnect. |
||
837 | * Let users disconnect if it's development mode, just in case things glitch. |
||
838 | */ |
||
839 | case 'jetpack_disconnect' : |
||
840 | /** |
||
841 | * In multisite, can individual site admins manage their own connection? |
||
842 | * |
||
843 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
844 | */ |
||
845 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
846 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
847 | /** |
||
848 | * We need to update the option name -- it's terribly unclear which |
||
849 | * direction the override goes. |
||
850 | * |
||
851 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
852 | */ |
||
853 | $caps = array( 'do_not_allow' ); |
||
854 | break; |
||
855 | } |
||
856 | } |
||
857 | |||
858 | $caps = array( 'manage_options' ); |
||
859 | break; |
||
860 | case 'jetpack_manage_modules' : |
||
861 | case 'jetpack_activate_modules' : |
||
862 | case 'jetpack_deactivate_modules' : |
||
863 | $caps = array( 'manage_options' ); |
||
864 | break; |
||
865 | case 'jetpack_configure_modules' : |
||
866 | $caps = array( 'manage_options' ); |
||
867 | break; |
||
868 | case 'jetpack_network_admin_page': |
||
869 | case 'jetpack_network_settings_page': |
||
870 | $caps = array( 'manage_network_plugins' ); |
||
871 | break; |
||
872 | case 'jetpack_network_sites_page': |
||
873 | $caps = array( 'manage_sites' ); |
||
874 | break; |
||
875 | case 'jetpack_admin_page' : |
||
876 | if ( Jetpack::is_development_mode() ) { |
||
877 | $caps = array( 'manage_options' ); |
||
878 | break; |
||
879 | } else { |
||
880 | $caps = array( 'read' ); |
||
881 | } |
||
882 | break; |
||
883 | case 'jetpack_connect_user' : |
||
884 | if ( Jetpack::is_development_mode() ) { |
||
885 | $caps = array( 'do_not_allow' ); |
||
886 | break; |
||
887 | } |
||
888 | $caps = array( 'read' ); |
||
889 | break; |
||
890 | } |
||
891 | return $caps; |
||
892 | } |
||
893 | |||
894 | function require_jetpack_authentication() { |
||
895 | // Don't let anyone authenticate |
||
896 | $_COOKIE = array(); |
||
897 | remove_all_filters( 'authenticate' ); |
||
898 | remove_all_actions( 'wp_login_failed' ); |
||
899 | |||
900 | if ( Jetpack::is_active() ) { |
||
901 | // Allow Jetpack authentication |
||
902 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
903 | } |
||
904 | } |
||
905 | |||
906 | /** |
||
907 | * Load language files |
||
908 | * @action plugins_loaded |
||
909 | */ |
||
910 | public static function plugin_textdomain() { |
||
911 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
912 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
913 | } |
||
914 | |||
915 | /** |
||
916 | * Register assets for use in various modules and the Jetpack admin page. |
||
917 | * |
||
918 | * @uses wp_script_is, wp_register_script, plugins_url |
||
919 | * @action wp_loaded |
||
920 | * @return null |
||
921 | */ |
||
922 | public function register_assets() { |
||
923 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
924 | wp_register_script( 'spin', plugins_url( '_inc/build/spin.min.js', JETPACK__PLUGIN_FILE ), false, '1.3' ); |
||
925 | } |
||
926 | |||
927 | View Code Duplication | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
|
928 | wp_register_script( 'jquery.spin', plugins_url( '_inc/build/jquery.spin.min.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' ); |
||
929 | } |
||
930 | |||
931 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
932 | wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/build/gallery-settings.min.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' ); |
||
933 | } |
||
934 | |||
935 | View Code Duplication | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
|
936 | wp_register_script( 'jetpack-twitter-timeline', plugins_url( '_inc/build/twitter-timeline.min.js', JETPACK__PLUGIN_FILE ) , array( 'jquery' ), '4.0.0', true ); |
||
937 | } |
||
938 | |||
939 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
940 | wp_register_script( 'jetpack-facebook-embed', plugins_url( '_inc/build/facebook-embed.min.js', __FILE__ ), array( 'jquery' ), null, true ); |
||
941 | |||
942 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
943 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
944 | if ( ! is_numeric( $fb_app_id ) ) { |
||
945 | $fb_app_id = ''; |
||
946 | } |
||
947 | wp_localize_script( |
||
948 | 'jetpack-facebook-embed', |
||
949 | 'jpfbembed', |
||
950 | array( |
||
951 | 'appid' => $fb_app_id, |
||
952 | 'locale' => $this->get_locale(), |
||
953 | ) |
||
954 | ); |
||
955 | } |
||
956 | |||
957 | /** |
||
958 | * As jetpack_register_genericons is by default fired off a hook, |
||
959 | * the hook may have already fired by this point. |
||
960 | * So, let's just trigger it manually. |
||
961 | */ |
||
962 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
963 | jetpack_register_genericons(); |
||
964 | |||
965 | /** |
||
966 | * Register the social logos |
||
967 | */ |
||
968 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
969 | jetpack_register_social_logos(); |
||
970 | |||
971 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
972 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
973 | } |
||
974 | |||
975 | /** |
||
976 | * Guess locale from language code. |
||
977 | * |
||
978 | * @param string $lang Language code. |
||
979 | * @return string|bool |
||
980 | */ |
||
981 | function guess_locale_from_lang( $lang ) { |
||
982 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
983 | return 'en_US'; |
||
984 | } |
||
985 | |||
986 | View Code Duplication | if ( ! class_exists( 'GP_Locales' ) ) { |
|
987 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
988 | return false; |
||
989 | } |
||
990 | |||
991 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
992 | } |
||
993 | |||
994 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
995 | // WP.com: get_locale() returns 'it' |
||
996 | $locale = GP_Locales::by_slug( $lang ); |
||
997 | } else { |
||
998 | // Jetpack: get_locale() returns 'it_IT'; |
||
999 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
1000 | } |
||
1001 | |||
1002 | if ( ! $locale ) { |
||
1003 | return false; |
||
1004 | } |
||
1005 | |||
1006 | if ( empty( $locale->facebook_locale ) ) { |
||
1007 | if ( empty( $locale->wp_locale ) ) { |
||
1008 | return false; |
||
1009 | } else { |
||
1010 | // Facebook SDK is smart enough to fall back to en_US if a |
||
1011 | // locale isn't supported. Since supported Facebook locales |
||
1012 | // can fall out of sync, we'll attempt to use the known |
||
1013 | // wp_locale value and rely on said fallback. |
||
1014 | return $locale->wp_locale; |
||
1015 | } |
||
1016 | } |
||
1017 | |||
1018 | return $locale->facebook_locale; |
||
1019 | } |
||
1020 | |||
1021 | /** |
||
1022 | * Get the locale. |
||
1023 | * |
||
1024 | * @return string|bool |
||
1025 | */ |
||
1026 | function get_locale() { |
||
1027 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
1028 | |||
1029 | if ( ! $locale ) { |
||
1030 | $locale = 'en_US'; |
||
1031 | } |
||
1032 | |||
1033 | return $locale; |
||
1034 | } |
||
1035 | |||
1036 | /** |
||
1037 | * Device Pixels support |
||
1038 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
1039 | */ |
||
1040 | function devicepx() { |
||
1041 | if ( Jetpack::is_active() ) { |
||
1042 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
1043 | } |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1048 | * @param bool $option |
||
1049 | * @return string |
||
1050 | */ |
||
1051 | public function jetpack_main_network_site_option( $option ) { |
||
1052 | return network_site_url(); |
||
1053 | } |
||
1054 | /** |
||
1055 | * Network Name. |
||
1056 | */ |
||
1057 | static function network_name( $option = null ) { |
||
1058 | global $current_site; |
||
1059 | return $current_site->site_name; |
||
1060 | } |
||
1061 | /** |
||
1062 | * Does the network allow new user and site registrations. |
||
1063 | * @return string |
||
1064 | */ |
||
1065 | static function network_allow_new_registrations( $option = null ) { |
||
1066 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
1067 | } |
||
1068 | /** |
||
1069 | * Does the network allow admins to add new users. |
||
1070 | * @return boolian |
||
1071 | */ |
||
1072 | static function network_add_new_users( $option = null ) { |
||
1073 | return (bool) get_site_option( 'add_new_users' ); |
||
1074 | } |
||
1075 | /** |
||
1076 | * File upload psace left per site in MB. |
||
1077 | * -1 means NO LIMIT. |
||
1078 | * @return number |
||
1079 | */ |
||
1080 | static function network_site_upload_space( $option = null ) { |
||
1081 | // value in MB |
||
1082 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
1083 | } |
||
1084 | |||
1085 | /** |
||
1086 | * Network allowed file types. |
||
1087 | * @return string |
||
1088 | */ |
||
1089 | static function network_upload_file_types( $option = null ) { |
||
1090 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
1091 | } |
||
1092 | |||
1093 | /** |
||
1094 | * Maximum file upload size set by the network. |
||
1095 | * @return number |
||
1096 | */ |
||
1097 | static function network_max_upload_file_size( $option = null ) { |
||
1098 | // value in KB |
||
1099 | return get_site_option( 'fileupload_maxk', 300 ); |
||
1100 | } |
||
1101 | |||
1102 | /** |
||
1103 | * Lets us know if a site allows admins to manage the network. |
||
1104 | * @return array |
||
1105 | */ |
||
1106 | static function network_enable_administration_menus( $option = null ) { |
||
1107 | return get_site_option( 'menu_items' ); |
||
1108 | } |
||
1109 | |||
1110 | /** |
||
1111 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1112 | * jetpack_other_linked_admins transient. |
||
1113 | * |
||
1114 | * @since 4.3.2 |
||
1115 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1116 | * |
||
1117 | * @param int $user_id The user ID whose role changed. |
||
1118 | * @param string $role The new role. |
||
1119 | * @param array $old_roles An array of the user's previous roles. |
||
1120 | */ |
||
1121 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1122 | if ( 'administrator' == $role |
||
1123 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
1124 | || is_null( $old_roles ) |
||
1125 | ) { |
||
1126 | delete_transient( 'jetpack_other_linked_admins' ); |
||
1127 | } |
||
1128 | } |
||
1129 | |||
1130 | /** |
||
1131 | * Checks to see if there are any other users available to become primary |
||
1132 | * Users must both: |
||
1133 | * - Be linked to wpcom |
||
1134 | * - Be an admin |
||
1135 | * |
||
1136 | * @return mixed False if no other users are linked, Int if there are. |
||
1137 | */ |
||
1138 | static function get_other_linked_admins() { |
||
1139 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
1140 | |||
1141 | if ( false === $other_linked_users ) { |
||
1142 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
1143 | if ( count( $admins ) > 1 ) { |
||
1144 | $available = array(); |
||
1145 | foreach ( $admins as $admin ) { |
||
1146 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
1147 | $available[] = $admin->ID; |
||
1148 | } |
||
1149 | } |
||
1150 | |||
1151 | $count_connected_admins = count( $available ); |
||
1152 | if ( count( $available ) > 1 ) { |
||
1153 | $other_linked_users = $count_connected_admins; |
||
1154 | } else { |
||
1155 | $other_linked_users = 0; |
||
1156 | } |
||
1157 | } else { |
||
1158 | $other_linked_users = 0; |
||
1159 | } |
||
1160 | |||
1161 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
1162 | } |
||
1163 | |||
1164 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
1165 | } |
||
1166 | |||
1167 | /** |
||
1168 | * Return whether we are dealing with a multi network setup or not. |
||
1169 | * The reason we are type casting this is because we want to avoid the situation where |
||
1170 | * the result is false since when is_main_network_option return false it cases |
||
1171 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1172 | * database which could be set to anything as opposed to what this function returns. |
||
1173 | * @param bool $option |
||
1174 | * |
||
1175 | * @return boolean |
||
1176 | */ |
||
1177 | public function is_main_network_option( $option ) { |
||
1178 | // return '1' or '' |
||
1179 | return (string) (bool) Jetpack::is_multi_network(); |
||
1180 | } |
||
1181 | |||
1182 | /** |
||
1183 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1184 | * |
||
1185 | * @param string $option |
||
1186 | * @return boolean |
||
1187 | */ |
||
1188 | public function is_multisite( $option ) { |
||
1189 | return (string) (bool) is_multisite(); |
||
1190 | } |
||
1191 | |||
1192 | /** |
||
1193 | * Implemented since there is no core is multi network function |
||
1194 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1195 | * |
||
1196 | * @since 3.3 |
||
1197 | * @return boolean |
||
1198 | */ |
||
1199 | public static function is_multi_network() { |
||
1200 | global $wpdb; |
||
1201 | |||
1202 | // if we don't have a multi site setup no need to do any more |
||
1203 | if ( ! is_multisite() ) { |
||
1204 | return false; |
||
1205 | } |
||
1206 | |||
1207 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
1208 | if ( $num_sites > 1 ) { |
||
1209 | return true; |
||
1210 | } else { |
||
1211 | return false; |
||
1212 | } |
||
1213 | } |
||
1214 | |||
1215 | /** |
||
1216 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1217 | * @return null |
||
1218 | */ |
||
1219 | function update_jetpack_main_network_site_option() { |
||
1220 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1221 | } |
||
1222 | /** |
||
1223 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1224 | * |
||
1225 | */ |
||
1226 | function update_jetpack_network_settings() { |
||
1227 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1228 | // Only sync this info for the main network site. |
||
1229 | } |
||
1230 | |||
1231 | /** |
||
1232 | * Get back if the current site is single user site. |
||
1233 | * |
||
1234 | * @return bool |
||
1235 | */ |
||
1236 | public static function is_single_user_site() { |
||
1237 | global $wpdb; |
||
1238 | |||
1239 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
1240 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
1241 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
1242 | } |
||
1243 | return 1 === (int) $some_users; |
||
1244 | } |
||
1245 | |||
1246 | /** |
||
1247 | * Returns true if the site has file write access false otherwise. |
||
1248 | * @return string ( '1' | '0' ) |
||
1249 | **/ |
||
1250 | public static function file_system_write_access() { |
||
1251 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
1252 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
1253 | } |
||
1254 | |||
1255 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
1256 | |||
1257 | $filesystem_method = get_filesystem_method(); |
||
1258 | if ( $filesystem_method === 'direct' ) { |
||
1259 | return 1; |
||
1260 | } |
||
1261 | |||
1262 | ob_start(); |
||
1263 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
1264 | ob_end_clean(); |
||
1265 | if ( $filesystem_credentials_are_stored ) { |
||
1266 | return 1; |
||
1267 | } |
||
1268 | return 0; |
||
1269 | } |
||
1270 | |||
1271 | /** |
||
1272 | * Finds out if a site is using a version control system. |
||
1273 | * @return string ( '1' | '0' ) |
||
1274 | **/ |
||
1275 | public static function is_version_controlled() { |
||
1276 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
1277 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
1278 | } |
||
1279 | |||
1280 | /** |
||
1281 | * Determines whether the current theme supports featured images or not. |
||
1282 | * @return string ( '1' | '0' ) |
||
1283 | */ |
||
1284 | public static function featured_images_enabled() { |
||
1285 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1286 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
1287 | } |
||
1288 | |||
1289 | /** |
||
1290 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1291 | * |
||
1292 | * @deprecated 4.7 use get_avatar_url instead. |
||
1293 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1294 | * @param int $size Size of the avatar image |
||
1295 | * @param string $default URL to a default image to use if no avatar is available |
||
1296 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1297 | * |
||
1298 | * @return array |
||
1299 | */ |
||
1300 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1301 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
1302 | return get_avatar_url( $id_or_email, array( |
||
1303 | 'size' => $size, |
||
1304 | 'default' => $default, |
||
1305 | 'force_default' => $force_display, |
||
1306 | ) ); |
||
1307 | } |
||
1308 | |||
1309 | /** |
||
1310 | * jetpack_updates is saved in the following schema: |
||
1311 | * |
||
1312 | * array ( |
||
1313 | * 'plugins' => (int) Number of plugin updates available. |
||
1314 | * 'themes' => (int) Number of theme updates available. |
||
1315 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1316 | * 'translations' => (int) Number of translation updates available. |
||
1317 | * 'total' => (int) Total of all available updates. |
||
1318 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1319 | * ) |
||
1320 | * @return array |
||
1321 | */ |
||
1322 | public static function get_updates() { |
||
1323 | $update_data = wp_get_update_data(); |
||
1324 | |||
1325 | // Stores the individual update counts as well as the total count. |
||
1326 | if ( isset( $update_data['counts'] ) ) { |
||
1327 | $updates = $update_data['counts']; |
||
1328 | } |
||
1329 | |||
1330 | // If we need to update WordPress core, let's find the latest version number. |
||
1331 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
1332 | $cur = get_preferred_from_update_core(); |
||
1333 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
1334 | $updates['wp_update_version'] = $cur->current; |
||
1335 | } |
||
1336 | } |
||
1337 | return isset( $updates ) ? $updates : array(); |
||
1338 | } |
||
1339 | |||
1340 | public static function get_update_details() { |
||
1341 | $update_details = array( |
||
1342 | 'update_core' => get_site_transient( 'update_core' ), |
||
1343 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
1344 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
1345 | ); |
||
1346 | return $update_details; |
||
1347 | } |
||
1348 | |||
1349 | public static function refresh_update_data() { |
||
1350 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1351 | |||
1352 | } |
||
1353 | |||
1354 | public static function refresh_theme_data() { |
||
1355 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1356 | } |
||
1357 | |||
1358 | /** |
||
1359 | * Is Jetpack active? |
||
1360 | */ |
||
1361 | public static function is_active() { |
||
1362 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1363 | } |
||
1364 | |||
1365 | /** |
||
1366 | * Make an API call to WordPress.com for plan status |
||
1367 | * |
||
1368 | * @uses Jetpack_Options::get_option() |
||
1369 | * @uses Jetpack_Client::wpcom_json_api_request_as_blog() |
||
1370 | * @uses update_option() |
||
1371 | * |
||
1372 | * @access public |
||
1373 | * @static |
||
1374 | * |
||
1375 | * @return bool True if plan is updated, false if no update |
||
1376 | */ |
||
1377 | public static function refresh_active_plan_from_wpcom() { |
||
1378 | // Make the API request |
||
1379 | $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) ); |
||
1380 | $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' ); |
||
1381 | |||
1382 | // Bail if there was an error or malformed response |
||
1383 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
||
1384 | return false; |
||
1385 | } |
||
1386 | |||
1387 | // Decode the results |
||
1388 | $results = json_decode( $response['body'], true ); |
||
1389 | |||
1390 | // Bail if there were no results or plan details returned |
||
1391 | if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) { |
||
1392 | return false; |
||
1393 | } |
||
1394 | |||
1395 | // Store the option and return true if updated |
||
1396 | return update_option( 'jetpack_active_plan', $results['plan'] ); |
||
1397 | } |
||
1398 | |||
1399 | /** |
||
1400 | * Get the plan that this Jetpack site is currently using |
||
1401 | * |
||
1402 | * @uses get_option() |
||
1403 | * |
||
1404 | * @access public |
||
1405 | * @static |
||
1406 | * |
||
1407 | * @return array Active Jetpack plan details |
||
1408 | */ |
||
1409 | public static function get_active_plan() { |
||
1410 | $plan = get_option( 'jetpack_active_plan', array() ); |
||
1411 | |||
1412 | // Set the default options |
||
1413 | if ( empty( $plan ) || ( isset( $plan['product_slug'] ) && 'jetpack_free' === $plan['product_slug'] ) ) { |
||
1414 | $plan = wp_parse_args( $plan, array( |
||
1415 | 'product_slug' => 'jetpack_free', |
||
1416 | 'supports' => array(), |
||
1417 | 'class' => 'free', |
||
1418 | ) ); |
||
1419 | } |
||
1420 | |||
1421 | // Define what paid modules are supported by personal plans |
||
1422 | $personal_plans = array( |
||
1423 | 'jetpack_personal', |
||
1424 | 'jetpack_personal_monthly', |
||
1425 | 'personal-bundle', |
||
1426 | ); |
||
1427 | |||
1428 | if ( in_array( $plan['product_slug'], $personal_plans ) ) { |
||
1429 | $plan['supports'] = array( |
||
1430 | 'akismet', |
||
1431 | ); |
||
1432 | $plan['class'] = 'personal'; |
||
1433 | } |
||
1434 | |||
1435 | // Define what paid modules are supported by premium plans |
||
1436 | $premium_plans = array( |
||
1437 | 'jetpack_premium', |
||
1438 | 'jetpack_premium_monthly', |
||
1439 | 'value_bundle', |
||
1440 | ); |
||
1441 | |||
1442 | if ( in_array( $plan['product_slug'], $premium_plans ) ) { |
||
1443 | $plan['supports'] = array( |
||
1444 | 'videopress', |
||
1445 | 'akismet', |
||
1446 | 'vaultpress', |
||
1447 | 'wordads', |
||
1448 | ); |
||
1449 | $plan['class'] = 'premium'; |
||
1450 | } |
||
1451 | |||
1452 | // Define what paid modules are supported by professional plans |
||
1453 | $business_plans = array( |
||
1454 | 'jetpack_business', |
||
1455 | 'jetpack_business_monthly', |
||
1456 | 'business-bundle', |
||
1457 | ); |
||
1458 | |||
1459 | if ( in_array( $plan['product_slug'], $business_plans ) ) { |
||
1460 | $plan['supports'] = array( |
||
1461 | 'videopress', |
||
1462 | 'akismet', |
||
1463 | 'vaultpress', |
||
1464 | 'seo-tools', |
||
1465 | 'google-analytics', |
||
1466 | 'wordads', |
||
1467 | 'search', |
||
1468 | ); |
||
1469 | $plan['class'] = 'business'; |
||
1470 | } |
||
1471 | |||
1472 | // Make sure we have an array here in the event database data is stale |
||
1473 | if ( ! isset( $plan['supports'] ) ) { |
||
1474 | $plan['supports'] = array(); |
||
1475 | } |
||
1476 | |||
1477 | return $plan; |
||
1478 | } |
||
1479 | |||
1480 | /** |
||
1481 | * Determine whether the active plan supports a particular feature |
||
1482 | * |
||
1483 | * @uses Jetpack::get_active_plan() |
||
1484 | * |
||
1485 | * @access public |
||
1486 | * @static |
||
1487 | * |
||
1488 | * @return bool True if plan supports feature, false if not |
||
1489 | */ |
||
1490 | public static function active_plan_supports( $feature ) { |
||
1491 | $plan = Jetpack::get_active_plan(); |
||
1492 | |||
1493 | if ( in_array( $feature, $plan['supports'] ) ) { |
||
1494 | return true; |
||
1495 | } |
||
1496 | |||
1497 | return false; |
||
1498 | } |
||
1499 | |||
1500 | /** |
||
1501 | * Is Jetpack in development (offline) mode? |
||
1502 | */ |
||
1503 | public static function is_development_mode() { |
||
1504 | $development_mode = false; |
||
1505 | |||
1506 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
1507 | $development_mode = JETPACK_DEV_DEBUG; |
||
1508 | } elseif ( $site_url = site_url() ) { |
||
1509 | $development_mode = false === strpos( $site_url, '.' ); |
||
1510 | } |
||
1511 | |||
1512 | /** |
||
1513 | * Filters Jetpack's development mode. |
||
1514 | * |
||
1515 | * @see https://jetpack.com/support/development-mode/ |
||
1516 | * |
||
1517 | * @since 2.2.1 |
||
1518 | * |
||
1519 | * @param bool $development_mode Is Jetpack's development mode active. |
||
1520 | */ |
||
1521 | return apply_filters( 'jetpack_development_mode', $development_mode ); |
||
1522 | } |
||
1523 | |||
1524 | /** |
||
1525 | * Get Jetpack development mode notice text and notice class. |
||
1526 | * |
||
1527 | * Mirrors the checks made in Jetpack::is_development_mode |
||
1528 | * |
||
1529 | */ |
||
1530 | public static function show_development_mode_notice() { |
||
1531 | if ( Jetpack::is_development_mode() ) { |
||
1532 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
1533 | $notice = sprintf( |
||
1534 | /* translators: %s is a URL */ |
||
1535 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ), |
||
1536 | 'https://jetpack.com/support/development-mode/' |
||
1537 | ); |
||
1538 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
1539 | $notice = sprintf( |
||
1540 | /* translators: %s is a URL */ |
||
1541 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ), |
||
1542 | 'https://jetpack.com/support/development-mode/' |
||
1543 | ); |
||
1544 | } else { |
||
1545 | $notice = sprintf( |
||
1546 | /* translators: %s is a URL */ |
||
1547 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ), |
||
1548 | 'https://jetpack.com/support/development-mode/' |
||
1549 | ); |
||
1550 | } |
||
1551 | |||
1552 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1553 | } |
||
1554 | |||
1555 | // Throw up a notice if using a development version and as for feedback. |
||
1556 | if ( Jetpack::is_development_version() ) { |
||
1557 | /* translators: %s is a URL */ |
||
1558 | $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/' ); |
||
1559 | |||
1560 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1561 | } |
||
1562 | // Throw up a notice if using staging mode |
||
1563 | if ( Jetpack::is_staging_site() ) { |
||
1564 | /* translators: %s is a URL */ |
||
1565 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
1566 | |||
1567 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1568 | } |
||
1569 | } |
||
1570 | |||
1571 | /** |
||
1572 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1573 | */ |
||
1574 | public static function is_development_version() { |
||
1575 | /** |
||
1576 | * Allows filtering whether this is a development version of Jetpack. |
||
1577 | * |
||
1578 | * This filter is especially useful for tests. |
||
1579 | * |
||
1580 | * @since 4.3.0 |
||
1581 | * |
||
1582 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
1583 | */ |
||
1584 | return (bool) apply_filters( |
||
1585 | 'jetpack_development_version', |
||
1586 | ! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
1587 | ); |
||
1588 | } |
||
1589 | |||
1590 | /** |
||
1591 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1592 | */ |
||
1593 | public static function is_user_connected( $user_id = false ) { |
||
1594 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
1595 | if ( ! $user_id ) { |
||
1596 | return false; |
||
1597 | } |
||
1598 | |||
1599 | return (bool) Jetpack_Data::get_access_token( $user_id ); |
||
1600 | } |
||
1601 | |||
1602 | /** |
||
1603 | * Get the wpcom user data of the current|specified connected user. |
||
1604 | */ |
||
1605 | public static function get_connected_user_data( $user_id = null ) { |
||
1606 | if ( ! $user_id ) { |
||
1607 | $user_id = get_current_user_id(); |
||
1608 | } |
||
1609 | |||
1610 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
1611 | |||
1612 | if ( $cached_user_data = get_transient( $transient_key ) ) { |
||
1613 | return $cached_user_data; |
||
1614 | } |
||
1615 | |||
1616 | Jetpack::load_xml_rpc_client(); |
||
1617 | $xml = new Jetpack_IXR_Client( array( |
||
1618 | 'user_id' => $user_id, |
||
1619 | ) ); |
||
1620 | $xml->query( 'wpcom.getUser' ); |
||
1621 | if ( ! $xml->isError() ) { |
||
1622 | $user_data = $xml->getResponse(); |
||
1623 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
1624 | return $user_data; |
||
1625 | } |
||
1626 | |||
1627 | return false; |
||
1628 | } |
||
1629 | |||
1630 | /** |
||
1631 | * Get the wpcom email of the current|specified connected user. |
||
1632 | */ |
||
1633 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
1634 | if ( ! $user_id ) { |
||
1635 | $user_id = get_current_user_id(); |
||
1636 | } |
||
1637 | Jetpack::load_xml_rpc_client(); |
||
1638 | $xml = new Jetpack_IXR_Client( array( |
||
1639 | 'user_id' => $user_id, |
||
1640 | ) ); |
||
1641 | $xml->query( 'wpcom.getUserEmail' ); |
||
1642 | if ( ! $xml->isError() ) { |
||
1643 | return $xml->getResponse(); |
||
1644 | } |
||
1645 | return false; |
||
1646 | } |
||
1647 | |||
1648 | /** |
||
1649 | * Get the wpcom email of the master user. |
||
1650 | */ |
||
1651 | public static function get_master_user_email() { |
||
1652 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
1653 | if ( $master_user_id ) { |
||
1654 | return self::get_connected_user_email( $master_user_id ); |
||
1655 | } |
||
1656 | return ''; |
||
1657 | } |
||
1658 | |||
1659 | function current_user_is_connection_owner() { |
||
1660 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1661 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; |
||
1662 | } |
||
1663 | |||
1664 | /** |
||
1665 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
1666 | */ |
||
1667 | function extra_oembed_providers() { |
||
1668 | // Cloudup: https://dev.cloudup.com/#oembed |
||
1669 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
1670 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
1671 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
1672 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
1673 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
1674 | wp_oembed_add_provider( '#https?://(www\.)?icloud\.com/keynote/.*#i', 'https://iwmb.icloud.com/iwmb/oembed', true ); |
||
1675 | } |
||
1676 | |||
1677 | /** |
||
1678 | * Synchronize connected user role changes |
||
1679 | */ |
||
1680 | function user_role_change( $user_id ) { |
||
1681 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()' ); |
||
1682 | Jetpack_Sync_Users::user_role_change( $user_id ); |
||
1683 | } |
||
1684 | |||
1685 | /** |
||
1686 | * Loads the currently active modules. |
||
1687 | */ |
||
1688 | public static function load_modules() { |
||
1689 | if ( |
||
1690 | ! self::is_active() |
||
1691 | && ! self::is_development_mode() |
||
1692 | && ( |
||
1693 | ! is_multisite() |
||
1694 | || ! get_site_option( 'jetpack_protect_active' ) |
||
1695 | ) |
||
1696 | ) { |
||
1697 | return; |
||
1698 | } |
||
1699 | |||
1700 | $version = Jetpack_Options::get_option( 'version' ); |
||
1701 | View Code Duplication | if ( ! $version ) { |
|
1702 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
1703 | /** This action is documented in class.jetpack.php */ |
||
1704 | do_action( 'updating_jetpack_version', $version, false ); |
||
1705 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
1706 | } |
||
1707 | list( $version ) = explode( ':', $version ); |
||
1708 | |||
1709 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
1710 | |||
1711 | $modules_data = array(); |
||
1712 | |||
1713 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
1714 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
1715 | $updated_modules = array(); |
||
1716 | foreach ( $modules as $module ) { |
||
1717 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1718 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
1719 | continue; |
||
1720 | } |
||
1721 | |||
1722 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
1723 | continue; |
||
1724 | } |
||
1725 | |||
1726 | $updated_modules[] = $module; |
||
1727 | } |
||
1728 | |||
1729 | $modules = array_diff( $modules, $updated_modules ); |
||
1730 | } |
||
1731 | |||
1732 | $is_development_mode = Jetpack::is_development_mode(); |
||
1733 | |||
1734 | foreach ( $modules as $index => $module ) { |
||
1735 | // If we're in dev mode, disable modules requiring a connection |
||
1736 | if ( $is_development_mode ) { |
||
1737 | // Prime the pump if we need to |
||
1738 | if ( empty( $modules_data[ $module ] ) ) { |
||
1739 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1740 | } |
||
1741 | // If the module requires a connection, but we're in local mode, don't include it. |
||
1742 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
1743 | continue; |
||
1744 | } |
||
1745 | } |
||
1746 | |||
1747 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
1748 | continue; |
||
1749 | } |
||
1750 | |||
1751 | if ( ! include_once( Jetpack::get_module_path( $module ) ) ) { |
||
1752 | unset( $modules[ $index ] ); |
||
1753 | self::update_active_modules( array_values( $modules ) ); |
||
1754 | continue; |
||
1755 | } |
||
1756 | |||
1757 | /** |
||
1758 | * Fires when a specific module is loaded. |
||
1759 | * The dynamic part of the hook, $module, is the module slug. |
||
1760 | * |
||
1761 | * @since 1.1.0 |
||
1762 | */ |
||
1763 | do_action( 'jetpack_module_loaded_' . $module ); |
||
1764 | } |
||
1765 | |||
1766 | /** |
||
1767 | * Fires when all the modules are loaded. |
||
1768 | * |
||
1769 | * @since 1.1.0 |
||
1770 | */ |
||
1771 | do_action( 'jetpack_modules_loaded' ); |
||
1772 | |||
1773 | // 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. |
||
1774 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
1775 | } |
||
1776 | |||
1777 | /** |
||
1778 | * Check if Jetpack's REST API compat file should be included |
||
1779 | * @action plugins_loaded |
||
1780 | * @return null |
||
1781 | */ |
||
1782 | public function check_rest_api_compat() { |
||
1783 | /** |
||
1784 | * Filters the list of REST API compat files to be included. |
||
1785 | * |
||
1786 | * @since 2.2.5 |
||
1787 | * |
||
1788 | * @param array $args Array of REST API compat files to include. |
||
1789 | */ |
||
1790 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
1791 | |||
1792 | if ( function_exists( 'bbpress' ) ) |
||
1793 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
1794 | |||
1795 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
1796 | require_once $_jetpack_rest_api_compat_include; |
||
1797 | } |
||
1798 | |||
1799 | /** |
||
1800 | * Gets all plugins currently active in values, regardless of whether they're |
||
1801 | * traditionally activated or network activated. |
||
1802 | * |
||
1803 | * @todo Store the result in core's object cache maybe? |
||
1804 | */ |
||
1805 | public static function get_active_plugins() { |
||
1806 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
1807 | |||
1808 | if ( is_multisite() ) { |
||
1809 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
1810 | // whereas active_plugins stores them in the values. |
||
1811 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
1812 | if ( $network_plugins ) { |
||
1813 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
1814 | } |
||
1815 | } |
||
1816 | |||
1817 | sort( $active_plugins ); |
||
1818 | |||
1819 | return array_unique( $active_plugins ); |
||
1820 | } |
||
1821 | |||
1822 | /** |
||
1823 | * Gets and parses additional plugin data to send with the heartbeat data |
||
1824 | * |
||
1825 | * @since 3.8.1 |
||
1826 | * |
||
1827 | * @return array Array of plugin data |
||
1828 | */ |
||
1829 | public static function get_parsed_plugin_data() { |
||
1830 | if ( ! function_exists( 'get_plugins' ) ) { |
||
1831 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
1832 | } |
||
1833 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
1834 | $all_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
1835 | $active_plugins = Jetpack::get_active_plugins(); |
||
1836 | |||
1837 | $plugins = array(); |
||
1838 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
1839 | $plugins[ $path ] = array( |
||
1840 | 'is_active' => in_array( $path, $active_plugins ), |
||
1841 | 'file' => $path, |
||
1842 | 'name' => $plugin_data['Name'], |
||
1843 | 'version' => $plugin_data['Version'], |
||
1844 | 'author' => $plugin_data['Author'], |
||
1845 | ); |
||
1846 | } |
||
1847 | |||
1848 | return $plugins; |
||
1849 | } |
||
1850 | |||
1851 | /** |
||
1852 | * Gets and parses theme data to send with the heartbeat data |
||
1853 | * |
||
1854 | * @since 3.8.1 |
||
1855 | * |
||
1856 | * @return array Array of theme data |
||
1857 | */ |
||
1858 | public static function get_parsed_theme_data() { |
||
1880 | |||
1881 | /** |
||
1882 | * Checks whether a specific plugin is active. |
||
1883 | * |
||
1884 | * We don't want to store these in a static variable, in case |
||
1885 | * there are switch_to_blog() calls involved. |
||
1886 | */ |
||
1887 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
1890 | |||
1891 | /** |
||
1892 | * Check if Jetpack's Open Graph tags should be used. |
||
1893 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
1894 | * |
||
1895 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
1896 | * @action plugins_loaded |
||
1897 | * @return null |
||
1898 | */ |
||
1899 | public function check_open_graph() { |
||
1926 | |||
1927 | /** |
||
1928 | * Check if Jetpack's Twitter tags should be used. |
||
1929 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
1930 | * |
||
1931 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
1932 | * @action plugins_loaded |
||
1933 | * @return null |
||
1934 | */ |
||
1935 | public function check_twitter_tags() { |
||
1959 | |||
1960 | /** |
||
1961 | * Allows plugins to submit security reports. |
||
1962 | * |
||
1963 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
1964 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
1965 | * @param array $args See definitions above |
||
1966 | */ |
||
1967 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
1968 | _deprecated_function( __FUNCTION__, 'jetpack-4.2', null ); |
||
1969 | } |
||
1970 | |||
1971 | /* Jetpack Options API */ |
||
1972 | |||
1973 | public static function get_option_names( $type = 'compact' ) { |
||
1974 | return Jetpack_Options::get_option_names( $type ); |
||
1975 | } |
||
1976 | |||
1977 | /** |
||
1978 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
1979 | * |
||
1980 | * @param string $name Option name |
||
1981 | * @param mixed $default (optional) |
||
1982 | */ |
||
1983 | public static function get_option( $name, $default = false ) { |
||
1984 | return Jetpack_Options::get_option( $name, $default ); |
||
1985 | } |
||
1986 | |||
1987 | /** |
||
1988 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
1989 | * |
||
1990 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
1991 | * @param string $name Option name |
||
1992 | * @param mixed $value Option value |
||
1993 | */ |
||
1994 | public static function update_option( $name, $value ) { |
||
1995 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' ); |
||
1996 | return Jetpack_Options::update_option( $name, $value ); |
||
1997 | } |
||
1998 | |||
1999 | /** |
||
2000 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
2001 | * |
||
2002 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
2003 | * @param array $array array( option name => option value, ... ) |
||
2004 | */ |
||
2005 | public static function update_options( $array ) { |
||
2006 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' ); |
||
2007 | return Jetpack_Options::update_options( $array ); |
||
2008 | } |
||
2009 | |||
2010 | /** |
||
2011 | * Deletes the given option. May be passed multiple option names as an array. |
||
2012 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
2013 | * |
||
2014 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
2015 | * @param string|array $names |
||
2016 | */ |
||
2017 | public static function delete_option( $names ) { |
||
2018 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' ); |
||
2019 | return Jetpack_Options::delete_option( $names ); |
||
2020 | } |
||
2021 | |||
2022 | /** |
||
2023 | * Enters a user token into the user_tokens option |
||
2024 | * |
||
2025 | * @param int $user_id |
||
2026 | * @param string $token |
||
2027 | * return bool |
||
2028 | */ |
||
2029 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
2043 | |||
2044 | /** |
||
2045 | * Returns an array of all PHP files in the specified absolute path. |
||
2046 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
2047 | * |
||
2048 | * @param string $absolute_path The absolute path of the directory to search. |
||
2049 | * @return array Array of absolute paths to the PHP files. |
||
2050 | */ |
||
2051 | public static function glob_php( $absolute_path ) { |
||
2080 | |||
2081 | public static function activate_new_modules( $redirect = false ) { |
||
2139 | |||
2140 | /** |
||
2141 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2142 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2143 | */ |
||
2144 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
2201 | |||
2202 | /** |
||
2203 | * Default modules loaded on activation. |
||
2204 | */ |
||
2205 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
2236 | |||
2237 | /** |
||
2238 | * Checks activated modules during auto-activation to determine |
||
2239 | * if any of those modules are being deprecated. If so, close |
||
2240 | * them out, and add any replacement modules. |
||
2241 | * |
||
2242 | * Runs at priority 99 by default. |
||
2243 | * |
||
2244 | * This is run late, so that it can still activate a module if |
||
2245 | * the new module is a replacement for another that the user |
||
2246 | * currently has active, even if something at the normal priority |
||
2247 | * would kibosh everything. |
||
2248 | * |
||
2249 | * @since 2.6 |
||
2250 | * @uses jetpack_get_default_modules filter |
||
2251 | * @param array $modules |
||
2252 | * @return array |
||
2253 | */ |
||
2254 | function handle_deprecated_modules( $modules ) { |
||
2280 | |||
2281 | /** |
||
2282 | * Checks activated plugins during auto-activation to determine |
||
2283 | * if any of those plugins are in the list with a corresponding module |
||
2284 | * that is not compatible with the plugin. The module will not be allowed |
||
2285 | * to auto-activate. |
||
2286 | * |
||
2287 | * @since 2.6 |
||
2288 | * @uses jetpack_get_default_modules filter |
||
2289 | * @param array $modules |
||
2290 | * @return array |
||
2291 | */ |
||
2292 | function filter_default_modules( $modules ) { |
||
2316 | |||
2317 | /** |
||
2318 | * Extract a module's slug from its full path. |
||
2319 | */ |
||
2320 | public static function get_module_slug( $file ) { |
||
2323 | |||
2324 | /** |
||
2325 | * Generate a module's path from its slug. |
||
2326 | */ |
||
2327 | public static function get_module_path( $slug ) { |
||
2330 | |||
2331 | /** |
||
2332 | * Load module data from module file. Headers differ from WordPress |
||
2333 | * plugin headers to avoid them being identified as standalone |
||
2334 | * plugins on the WordPress plugins page. |
||
2335 | */ |
||
2336 | public static function get_module( $module ) { |
||
2419 | |||
2420 | /** |
||
2421 | * Like core's get_file_data implementation, but caches the result. |
||
2422 | */ |
||
2423 | public static function get_file_data( $file, $headers ) { |
||
2451 | |||
2452 | |||
2453 | /** |
||
2454 | * Return translated module tag. |
||
2455 | * |
||
2456 | * @param string $tag Tag as it appears in each module heading. |
||
2457 | * |
||
2458 | * @return mixed |
||
2459 | */ |
||
2460 | public static function translate_module_tag( $tag ) { |
||
2463 | |||
2464 | /** |
||
2465 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2466 | * |
||
2467 | * @since 3.9.2 |
||
2468 | * |
||
2469 | * @param array $modules |
||
2470 | * |
||
2471 | * @return string|void |
||
2472 | */ |
||
2473 | public static function get_translated_modules( $modules ) { |
||
2486 | |||
2487 | /** |
||
2488 | * Get a list of activated modules as an array of module slugs. |
||
2489 | */ |
||
2490 | public static function get_active_modules() { |
||
2510 | |||
2511 | /** |
||
2512 | * Check whether or not a Jetpack module is active. |
||
2513 | * |
||
2514 | * @param string $module The slug of a Jetpack module. |
||
2515 | * @return bool |
||
2516 | * |
||
2517 | * @static |
||
2518 | */ |
||
2519 | public static function is_module_active( $module ) { |
||
2522 | |||
2523 | public static function is_module( $module ) { |
||
2526 | |||
2527 | /** |
||
2528 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2529 | * |
||
2530 | * @param bool $catch True to start catching, False to stop. |
||
2531 | * |
||
2532 | * @static |
||
2533 | */ |
||
2534 | public static function catch_errors( $catch ) { |
||
2547 | |||
2548 | /** |
||
2549 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2550 | */ |
||
2551 | public static function catch_errors_on_shutdown() { |
||
2554 | |||
2555 | /** |
||
2556 | * Rewrite any string to make paths easier to read. |
||
2557 | * |
||
2558 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2559 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2560 | * |
||
2561 | * @param $string |
||
2562 | * @return mixed |
||
2563 | */ |
||
2564 | public static function alias_directories( $string ) { |
||
2572 | |||
2573 | public static function activate_default_modules( |
||
2709 | |||
2710 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
2803 | |||
2804 | function activate_module_actions( $module ) { |
||
2807 | |||
2808 | public static function deactivate_module( $module ) { |
||
2835 | |||
2836 | public static function enable_module_configurable( $module ) { |
||
2840 | |||
2841 | public static function module_configuration_url( $module ) { |
||
2845 | |||
2846 | public static function module_configuration_load( $module, $method ) { |
||
2850 | |||
2851 | public static function module_configuration_head( $module, $method ) { |
||
2855 | |||
2856 | public static function module_configuration_screen( $module, $method ) { |
||
2860 | |||
2861 | public static function module_configuration_activation_screen( $module, $method ) { |
||
2865 | |||
2866 | /* Installation */ |
||
2867 | |||
2868 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
2908 | |||
2909 | /** |
||
2910 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
2911 | * @static |
||
2912 | */ |
||
2913 | public static function plugin_activation( $network_wide ) { |
||
2930 | |||
2931 | public static function get_activation_source( $referer_url ) { |
||
2980 | |||
2981 | /** |
||
2982 | * Runs before bumping version numbers up to a new version |
||
2983 | * @param string $version Version:timestamp |
||
2984 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
2985 | * @return null [description] |
||
2986 | */ |
||
2987 | public static function do_version_bump( $version, $old_version ) { |
||
2994 | |||
2995 | /** |
||
2996 | * Sets the internal version number and activation state. |
||
2997 | * @static |
||
2998 | */ |
||
2999 | public static function plugin_initialize() { |
||
3016 | |||
3017 | /** |
||
3018 | * Removes all connection options |
||
3019 | * @static |
||
3020 | */ |
||
3021 | public static function plugin_deactivation( ) { |
||
3030 | |||
3031 | /** |
||
3032 | * Disconnects from the Jetpack servers. |
||
3033 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
3034 | * @static |
||
3035 | */ |
||
3036 | public static function disconnect( $update_activated_state = true ) { |
||
3097 | |||
3098 | /** |
||
3099 | * Unlinks the current user from the linked WordPress.com user |
||
3100 | */ |
||
3101 | public static function unlink_user( $user_id = null ) { |
||
3132 | |||
3133 | /** |
||
3134 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
3135 | */ |
||
3136 | public static function try_registration() { |
||
3161 | |||
3162 | /** |
||
3163 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3164 | * |
||
3165 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3166 | */ |
||
3167 | public static function log( $code, $data = null ) { |
||
3207 | |||
3208 | /** |
||
3209 | * Get the internal event log. |
||
3210 | * |
||
3211 | * @param $event (string) - only return the specific log events |
||
3212 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3213 | * |
||
3214 | * @return array of log events || WP_Error for invalid params |
||
3215 | */ |
||
3216 | public static function get_log( $event = false, $num = false ) { |
||
3252 | |||
3253 | /** |
||
3254 | * Log modification of important settings. |
||
3255 | */ |
||
3256 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3263 | |||
3264 | /** |
||
3265 | * Return stat data for WPCOM sync |
||
3266 | */ |
||
3267 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3281 | |||
3282 | /** |
||
3283 | * Get additional stat data to sync to WPCOM |
||
3284 | */ |
||
3285 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3296 | |||
3297 | private static function get_site_user_count() { |
||
3312 | |||
3313 | /* Admin Pages */ |
||
3314 | |||
3315 | function admin_init() { |
||
3364 | |||
3365 | function admin_body_class( $admin_body_class = '' ) { |
||
3373 | |||
3374 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3377 | |||
3378 | /** |
||
3379 | * Call this function if you want the Big Jetpack Manage Notice to show up. |
||
3380 | * |
||
3381 | * @return null |
||
3382 | */ |
||
3383 | function prepare_manage_jetpack_notice() { |
||
3388 | |||
3389 | function manage_activate_screen() { |
||
3392 | /** |
||
3393 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3394 | * This function artificially throws errors for such cases (whitelisted). |
||
3395 | * |
||
3396 | * @param string $plugin The activated plugin. |
||
3397 | */ |
||
3398 | function throw_error_on_activate_plugin( $plugin ) { |
||
3422 | |||
3423 | function intercept_plugin_error_scrape_init() { |
||
3426 | |||
3427 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3438 | |||
3439 | function add_remote_request_handlers() { |
||
3443 | |||
3444 | function remote_request_handlers() { |
||
3484 | |||
3485 | /** |
||
3486 | * Uploads a file gotten from the global $_FILES. |
||
3487 | * If `$update_media_item` is true and `post_id` is defined |
||
3488 | * the attachment file of the media item (gotten through of the post_id) |
||
3489 | * will be updated instead of add a new one. |
||
3490 | * |
||
3491 | * @param boolean $update_media_item - update media attachment |
||
3492 | * @return array - An array describing the uploadind files process |
||
3493 | */ |
||
3494 | function upload_handler( $update_media_item = false ) { |
||
3616 | |||
3617 | /** |
||
3618 | * Add help to the Jetpack page |
||
3619 | * |
||
3620 | * @since Jetpack (1.2.3) |
||
3621 | * @return false if not the Jetpack page |
||
3622 | */ |
||
3623 | function admin_help() { |
||
3664 | |||
3665 | function admin_menu_css() { |
||
3668 | |||
3669 | function admin_menu_order() { |
||
3672 | |||
3673 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
3688 | |||
3689 | function admin_head() { |
||
3694 | |||
3695 | function admin_banner_styles() { |
||
3716 | |||
3717 | function plugin_action_links( $actions ) { |
||
3732 | |||
3733 | /** |
||
3734 | * This is the first banner |
||
3735 | * It should be visible only to user that can update the option |
||
3736 | * Are not connected |
||
3737 | * |
||
3738 | * @return null |
||
3739 | */ |
||
3740 | function admin_jetpack_manage_notice() { |
||
3770 | |||
3771 | /** |
||
3772 | * Returns the url that the user clicks to remove the notice for the big banner |
||
3773 | * @return string |
||
3774 | */ |
||
3775 | function opt_out_jetpack_manage_url() { |
||
3779 | /** |
||
3780 | * Returns the url that the user clicks to opt in to Jetpack Manage |
||
3781 | * @return string |
||
3782 | */ |
||
3783 | function opt_in_jetpack_manage_url() { |
||
3786 | |||
3787 | function opt_in_jetpack_manage_notice() { |
||
3797 | /** |
||
3798 | * Determines whether to show the notice of not true = display notice |
||
3799 | * @return bool |
||
3800 | */ |
||
3801 | function can_display_jetpack_manage_notice() { |
||
3823 | |||
3824 | /* |
||
3825 | * Registration flow: |
||
3826 | * 1 - ::admin_page_load() action=register |
||
3827 | * 2 - ::try_registration() |
||
3828 | * 3 - ::register() |
||
3829 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
3830 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
3831 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
3832 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
3833 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
3834 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
3835 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
3836 | * jetpack_id, jetpack_secret, jetpack_public |
||
3837 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
3838 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
3839 | * 5 - user logs in with WP.com account |
||
3840 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
3841 | * - Jetpack_Client_Server::authorize() |
||
3842 | * - Jetpack_Client_Server::get_token() |
||
3843 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
3844 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
3845 | * - which responds with access_token, token_type, scope |
||
3846 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
3847 | * - Jetpack::activate_default_modules() |
||
3848 | * - Deactivates deprecated plugins |
||
3849 | * - Activates all default modules |
||
3850 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
3851 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
3852 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
3853 | * Done! |
||
3854 | */ |
||
3855 | |||
3856 | /** |
||
3857 | * Handles the page load events for the Jetpack admin page |
||
3858 | */ |
||
3859 | function admin_page_load() { |
||
4096 | |||
4097 | function admin_notices() { |
||
4194 | |||
4195 | /** |
||
4196 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4197 | */ |
||
4198 | function stat( $group, $detail ) { |
||
4203 | |||
4204 | /** |
||
4205 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4206 | */ |
||
4207 | function do_stats( $method = '' ) { |
||
4222 | |||
4223 | /** |
||
4224 | * Runs stats code for a one-off, server-side. |
||
4225 | * |
||
4226 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4227 | * |
||
4228 | * @return bool If it worked. |
||
4229 | */ |
||
4230 | static function do_server_side_stat( $args ) { |
||
4240 | |||
4241 | /** |
||
4242 | * Builds the stats url. |
||
4243 | * |
||
4244 | * @param $args array|string The arguments to append to the URL. |
||
4245 | * |
||
4246 | * @return string The URL to be pinged. |
||
4247 | */ |
||
4248 | static function build_stats_url( $args ) { |
||
4268 | |||
4269 | static function translate_current_user_to_role() { |
||
4278 | |||
4279 | static function translate_user_to_role( $user ) { |
||
4288 | |||
4289 | static function translate_role_to_cap( $role ) { |
||
4296 | |||
4297 | static function sign_role( $role, $user_id = null ) { |
||
4313 | |||
4314 | |||
4315 | /** |
||
4316 | * Builds a URL to the Jetpack connection auth page |
||
4317 | * |
||
4318 | * @since 3.9.5 |
||
4319 | * |
||
4320 | * @param bool $raw If true, URL will not be escaped. |
||
4321 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4322 | * If string, will be a custom redirect. |
||
4323 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4324 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
4325 | * |
||
4326 | * @return string Connect URL |
||
4327 | */ |
||
4328 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
4457 | |||
4458 | public static function apply_activation_source_to_args( &$args ) { |
||
4469 | |||
4470 | function build_reconnect_url( $raw = false ) { |
||
4474 | |||
4475 | public static function admin_url( $args = null ) { |
||
4480 | |||
4481 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
4485 | |||
4486 | function dismiss_jetpack_notice() { |
||
4560 | |||
4561 | public static function admin_screen_configure_module( $module_id ) { |
||
4613 | |||
4614 | /** |
||
4615 | * Display link to activate the module to see the settings screen. |
||
4616 | * @param string $module_id |
||
4617 | * @return null |
||
4618 | */ |
||
4619 | public static function display_activate_module_link( $module_id ) { |
||
4671 | |||
4672 | public static function sort_modules( $a, $b ) { |
||
4678 | |||
4679 | function ajax_recheck_ssl() { |
||
4687 | |||
4688 | /* Client API */ |
||
4689 | |||
4690 | /** |
||
4691 | * Returns the requested Jetpack API URL |
||
4692 | * |
||
4693 | * @return string |
||
4694 | */ |
||
4695 | public static function api_url( $relative_url ) { |
||
4698 | |||
4699 | /** |
||
4700 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
4701 | */ |
||
4702 | public static function fix_url_for_bad_hosts( $url ) { |
||
4718 | |||
4719 | /** |
||
4720 | * Create a random secret for validating onboarding payload |
||
4721 | * |
||
4722 | * @return string Secret token |
||
4723 | */ |
||
4724 | public static function create_onboarding_token() { |
||
4732 | |||
4733 | /** |
||
4734 | * Remove the onboarding token |
||
4735 | * |
||
4736 | * @return bool True on success, false on failure |
||
4737 | */ |
||
4738 | public static function invalidate_onboarding_token() { |
||
4741 | |||
4742 | /** |
||
4743 | * Validate an onboarding token for a specific action |
||
4744 | * |
||
4745 | * @return boolean True if token/action pair is accepted, false if not |
||
4746 | */ |
||
4747 | public static function validate_onboarding_token_action( $token, $action ) { |
||
4765 | |||
4766 | /** |
||
4767 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
4768 | * |
||
4769 | * @since 2.3.3 |
||
4770 | * @return boolean |
||
4771 | */ |
||
4772 | public static function permit_ssl( $force_recheck = false ) { |
||
4814 | |||
4815 | /* |
||
4816 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
4817 | */ |
||
4818 | public function alert_auto_ssl_fail() { |
||
4867 | |||
4868 | /** |
||
4869 | * Returns the Jetpack XML-RPC API |
||
4870 | * |
||
4871 | * @return string |
||
4872 | */ |
||
4873 | public static function xmlrpc_api_url() { |
||
4877 | |||
4878 | /** |
||
4879 | * Creates two secret tokens and the end of life timestamp for them. |
||
4880 | * |
||
4881 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
4882 | * |
||
4883 | * @since 2.6 |
||
4884 | * @return array |
||
4885 | */ |
||
4886 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
4912 | |||
4913 | public static function get_secrets( $action, $user_id ) { |
||
4928 | |||
4929 | public static function delete_secrets( $action, $user_id ) { |
||
4937 | |||
4938 | /** |
||
4939 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
4940 | * |
||
4941 | * Based on local php max_execution_time in php.ini |
||
4942 | * |
||
4943 | * @since 2.6 |
||
4944 | * @return int |
||
4945 | * @deprecated |
||
4946 | **/ |
||
4947 | public function get_remote_query_timeout_limit() { |
||
4951 | |||
4952 | /** |
||
4953 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
4954 | * |
||
4955 | * Based on local php max_execution_time in php.ini |
||
4956 | * |
||
4957 | * @since 5.4 |
||
4958 | * @return int |
||
4959 | **/ |
||
4960 | public static function get_max_execution_time() { |
||
4969 | |||
4970 | /** |
||
4971 | * Sets a minimum request timeout, and returns the current timeout |
||
4972 | * |
||
4973 | * @since 5.4 |
||
4974 | **/ |
||
4975 | public static function set_min_time_limit( $min_timeout ) { |
||
4983 | |||
4984 | |||
4985 | /** |
||
4986 | * Takes the response from the Jetpack register new site endpoint and |
||
4987 | * verifies it worked properly. |
||
4988 | * |
||
4989 | * @since 2.6 |
||
4990 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
4991 | **/ |
||
4992 | public function validate_remote_register_response( $response ) { |
||
5032 | /** |
||
5033 | * @return bool|WP_Error |
||
5034 | */ |
||
5035 | public static function register() { |
||
5139 | |||
5140 | /** |
||
5141 | * If the db version is showing something other that what we've got now, bump it to current. |
||
5142 | * |
||
5143 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
5144 | */ |
||
5145 | public static function maybe_set_version_option() { |
||
5159 | |||
5160 | /* Client Server API */ |
||
5161 | |||
5162 | /** |
||
5163 | * Loads the Jetpack XML-RPC client |
||
5164 | */ |
||
5165 | public static function load_xml_rpc_client() { |
||
5169 | |||
5170 | /** |
||
5171 | * Resets the saved authentication state in between testing requests. |
||
5172 | */ |
||
5173 | public function reset_saved_auth_state() { |
||
5177 | |||
5178 | function verify_xml_rpc_signature() { |
||
5301 | |||
5302 | /** |
||
5303 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
5304 | */ |
||
5305 | function authenticate_jetpack( $user, $username, $password ) { |
||
5328 | |||
5329 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
5330 | // Uses the existing XMLRPC request signing implementation. |
||
5331 | function wp_rest_authenticate( $user ) { |
||
5425 | |||
5426 | /** |
||
5427 | * Report authentication status to the WP REST API. |
||
5428 | * |
||
5429 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5430 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5431 | */ |
||
5432 | public function wp_rest_authentication_errors( $value ) { |
||
5438 | |||
5439 | function add_nonce( $timestamp, $nonce ) { |
||
5477 | |||
5478 | /** |
||
5479 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5480 | * Capture it here so we can verify the signature later. |
||
5481 | */ |
||
5482 | function xmlrpc_methods( $methods ) { |
||
5486 | |||
5487 | function public_xmlrpc_methods( $methods ) { |
||
5493 | |||
5494 | function jetpack_getOptions( $args ) { |
||
5534 | |||
5535 | function xmlrpc_options( $options ) { |
||
5553 | |||
5554 | public static function clean_nonces( $all = false ) { |
||
5575 | |||
5576 | /** |
||
5577 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5578 | * SET: state( $key, $value ); |
||
5579 | * GET: $value = state( $key ); |
||
5580 | * |
||
5581 | * @param string $key |
||
5582 | * @param string $value |
||
5583 | * @param bool $restate private |
||
5584 | */ |
||
5585 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5635 | |||
5636 | public static function restate() { |
||
5639 | |||
5640 | public static function check_privacy( $file ) { |
||
5675 | |||
5676 | /** |
||
5677 | * Helper method for multicall XMLRPC. |
||
5678 | */ |
||
5679 | public static function xmlrpc_async_call() { |
||
5721 | |||
5722 | public static function staticize_subdomain( $url ) { |
||
5757 | |||
5758 | /* JSON API Authorization */ |
||
5759 | |||
5760 | /** |
||
5761 | * Handles the login action for Authorizing the JSON API |
||
5762 | */ |
||
5763 | function login_form_json_api_authorization() { |
||
5772 | |||
5773 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
5774 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
5787 | |||
5788 | // Make sure the POSTed request is handled by the same action |
||
5789 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
5793 | |||
5794 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
5795 | function store_json_api_authorization_token( $user_login, $user ) { |
||
5801 | |||
5802 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
5803 | function allow_wpcom_public_api_domain( $domains ) { |
||
5807 | |||
5808 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
5809 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
5821 | |||
5822 | |||
5823 | /** |
||
5824 | * Verifies the request by checking the signature |
||
5825 | * |
||
5826 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
5827 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
5828 | * |
||
5829 | * @param null|array $environment |
||
5830 | */ |
||
5831 | function verify_json_api_authorization_request( $environment = null ) { |
||
5924 | |||
5925 | function login_message_json_api_authorization( $message ) { |
||
5931 | |||
5932 | /** |
||
5933 | * Get $content_width, but with a <s>twist</s> filter. |
||
5934 | */ |
||
5935 | public static function get_content_width() { |
||
5946 | |||
5947 | /** |
||
5948 | * Pings the WordPress.com Mirror Site for the specified options. |
||
5949 | * |
||
5950 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
5951 | * |
||
5952 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
5953 | */ |
||
5954 | public function get_cloud_site_options( $option_names ) { |
||
5970 | |||
5971 | /** |
||
5972 | * Checks if the site is currently in an identity crisis. |
||
5973 | * |
||
5974 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
5975 | */ |
||
5976 | public static function check_identity_crisis() { |
||
5983 | |||
5984 | /** |
||
5985 | * Checks whether the home and siteurl specifically are whitelisted |
||
5986 | * Written so that we don't have re-check $key and $value params every time |
||
5987 | * we want to check if this site is whitelisted, for example in footer.php |
||
5988 | * |
||
5989 | * @since 3.8.0 |
||
5990 | * @return bool True = already whitelisted False = not whitelisted |
||
5991 | */ |
||
5992 | public static function is_staging_site() { |
||
6051 | |||
6052 | /** |
||
6053 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
6054 | * |
||
6055 | * @since 4.4.0 |
||
6056 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
6057 | * |
||
6058 | * @return bool |
||
6059 | */ |
||
6060 | public static function validate_sync_error_idc_option() { |
||
6104 | |||
6105 | /** |
||
6106 | * Normalizes a url by doing three things: |
||
6107 | * - Strips protocol |
||
6108 | * - Strips www |
||
6109 | * - Adds a trailing slash |
||
6110 | * |
||
6111 | * @since 4.4.0 |
||
6112 | * @param string $url |
||
6113 | * @return WP_Error|string |
||
6114 | */ |
||
6115 | public static function normalize_url_protocol_agnostic( $url ) { |
||
6125 | |||
6126 | /** |
||
6127 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
6128 | * |
||
6129 | * @since 4.4.0 |
||
6130 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
6131 | * |
||
6132 | * @param array $response |
||
6133 | * @return array Array of the local urls, wpcom urls, and error code |
||
6134 | */ |
||
6135 | public static function get_sync_error_idc_option( $response = array() ) { |
||
6168 | |||
6169 | /** |
||
6170 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
6171 | * If set to true, the site will be put into staging mode. |
||
6172 | * |
||
6173 | * @since 4.3.2 |
||
6174 | * @return bool |
||
6175 | */ |
||
6176 | public static function sync_idc_optin() { |
||
6194 | |||
6195 | /** |
||
6196 | * Maybe Use a .min.css stylesheet, maybe not. |
||
6197 | * |
||
6198 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
6199 | */ |
||
6200 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
6242 | |||
6243 | /** |
||
6244 | * If the asset is minified, let's flag .min as the suffix. |
||
6245 | * |
||
6246 | * Attached to `style_loader_src` filter. |
||
6247 | * |
||
6248 | * @param string $tag The tag that would link to the external asset. |
||
6249 | * @param string $handle The registered handle of the script in question. |
||
6250 | * @param string $href The url of the asset in question. |
||
6251 | */ |
||
6252 | public static function set_suffix_on_min( $src, $handle ) { |
||
6268 | |||
6269 | /** |
||
6270 | * Maybe inlines a stylesheet. |
||
6271 | * |
||
6272 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
6273 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
6274 | * |
||
6275 | * Attached to `style_loader_tag` filter. |
||
6276 | * |
||
6277 | * @param string $tag The tag that would link to the external asset. |
||
6278 | * @param string $handle The registered handle of the script in question. |
||
6279 | * |
||
6280 | * @return string |
||
6281 | */ |
||
6282 | public static function maybe_inline_style( $tag, $handle ) { |
||
6332 | |||
6333 | /** |
||
6334 | * Loads a view file from the views |
||
6335 | * |
||
6336 | * Data passed in with the $data parameter will be available in the |
||
6337 | * template file as $data['value'] |
||
6338 | * |
||
6339 | * @param string $template - Template file to load |
||
6340 | * @param array $data - Any data to pass along to the template |
||
6341 | * @return boolean - If template file was found |
||
6342 | **/ |
||
6343 | public function load_view( $template, $data = array() ) { |
||
6354 | |||
6355 | /** |
||
6356 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
6357 | */ |
||
6358 | public function deprecated_hooks() { |
||
6408 | |||
6409 | /** |
||
6410 | * Converts any url in a stylesheet, to the correct absolute url. |
||
6411 | * |
||
6412 | * Considerations: |
||
6413 | * - Normal, relative URLs `feh.png` |
||
6414 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
6415 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
6416 | * - Absolute URLs `http://domain.com/feh.png` |
||
6417 | * - Domain root relative URLs `/feh.png` |
||
6418 | * |
||
6419 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
6420 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
6421 | * |
||
6422 | * @return mixed|string |
||
6423 | */ |
||
6424 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6468 | |||
6469 | /** |
||
6470 | * This methods removes all of the registered css files on the front end |
||
6471 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6472 | * all the files into one file. |
||
6473 | * |
||
6474 | * Pros: |
||
6475 | * - Uses only ONE css asset connection instead of 15 |
||
6476 | * - Saves a minimum of 56k |
||
6477 | * - Reduces server load |
||
6478 | * - Reduces time to first painted byte |
||
6479 | * |
||
6480 | * Cons: |
||
6481 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6482 | * should not cause any issues with themes. |
||
6483 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6484 | * jetpack_implode_frontend_css filter for a workaround |
||
6485 | * |
||
6486 | * For some situations developers may wish to disable css imploding and |
||
6487 | * instead operate in legacy mode where each file loads seperately and |
||
6488 | * can be edited individually or dequeued. This can be accomplished with |
||
6489 | * the following line: |
||
6490 | * |
||
6491 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6492 | * |
||
6493 | * @since 3.2 |
||
6494 | **/ |
||
6495 | public function implode_frontend_css( $travis_test = false ) { |
||
6547 | |||
6548 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
6558 | |||
6559 | /* |
||
6560 | * Check the heartbeat data |
||
6561 | * |
||
6562 | * Organizes the heartbeat data by severity. For example, if the site |
||
6563 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
6564 | * |
||
6565 | * Data will be added to "caution" array, if it either: |
||
6566 | * - Out of date Jetpack version |
||
6567 | * - Out of date WP version |
||
6568 | * - Out of date PHP version |
||
6569 | * |
||
6570 | * $return array $filtered_data |
||
6571 | */ |
||
6572 | public static function jetpack_check_heartbeat_data() { |
||
6625 | |||
6626 | |||
6627 | /* |
||
6628 | * This method is used to organize all options that can be reset |
||
6629 | * without disconnecting Jetpack. |
||
6630 | * |
||
6631 | * It is used in class.jetpack-cli.php to reset options |
||
6632 | * |
||
6633 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
6634 | * |
||
6635 | * @return array of options to delete. |
||
6636 | */ |
||
6637 | public static function get_jetpack_options_for_reset() { |
||
6640 | |||
6641 | /** |
||
6642 | * Check if an option of a Jetpack module has been updated. |
||
6643 | * |
||
6644 | * If any module option has been updated before Jump Start has been dismissed, |
||
6645 | * update the 'jumpstart' option so we can hide Jump Start. |
||
6646 | * |
||
6647 | * @param string $option_name |
||
6648 | * |
||
6649 | * @return bool |
||
6650 | */ |
||
6651 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
6672 | |||
6673 | /* |
||
6674 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
6675 | * so we can bring them directly to their site in calypso. |
||
6676 | * |
||
6677 | * @param string | url |
||
6678 | * @return string | url without the guff |
||
6679 | */ |
||
6680 | public static function build_raw_urls( $url ) { |
||
6686 | |||
6687 | /** |
||
6688 | * Stores and prints out domains to prefetch for page speed optimization. |
||
6689 | * |
||
6690 | * @param mixed $new_urls |
||
6691 | */ |
||
6692 | public static function dns_prefetch( $new_urls = null ) { |
||
6709 | |||
6710 | public function wp_dashboard_setup() { |
||
6734 | |||
6735 | /** |
||
6736 | * @param mixed $result Value for the user's option |
||
6737 | * @return mixed |
||
6738 | */ |
||
6739 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
6764 | |||
6765 | public static function dashboard_widget() { |
||
6773 | |||
6774 | public static function dashboard_widget_footer() { |
||
6807 | |||
6808 | /** |
||
6809 | * Return string containing the Jetpack logo. |
||
6810 | * |
||
6811 | * @since 3.9.0 |
||
6812 | * |
||
6813 | * @return string |
||
6814 | */ |
||
6815 | public static function get_jp_emblem() { |
||
6818 | |||
6819 | /* |
||
6820 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
6821 | */ |
||
6822 | function jetpack_icon_user_connected( $columns ) { |
||
6826 | |||
6827 | /* |
||
6828 | * Show Jetpack icon if the user is linked. |
||
6829 | */ |
||
6830 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
6842 | |||
6843 | /* |
||
6844 | * Style the Jetpack user column |
||
6845 | */ |
||
6846 | function jetpack_user_col_style() { |
||
6863 | |||
6864 | /** |
||
6865 | * Checks if Akismet is active and working. |
||
6866 | * |
||
6867 | * @since 5.1.0 |
||
6868 | * @return bool True = Akismet available. False = Aksimet not available. |
||
6869 | */ |
||
6870 | public static function is_akismet_active() { |
||
6876 | |||
6877 | /** |
||
6878 | * Checks if one or more function names is in debug_backtrace |
||
6879 | * |
||
6880 | * @param $names Mixed string name of function or array of string names of functions |
||
6881 | * |
||
6882 | * @return bool |
||
6883 | */ |
||
6884 | public static function is_function_in_backtrace( $names ) { |
||
6907 | |||
6908 | /** |
||
6909 | * Given a minified path, and a non-minified path, will return |
||
6910 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
6911 | * |
||
6912 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
6913 | * root Jetpack directory. |
||
6914 | * |
||
6915 | * @since 5.6.0 |
||
6916 | * |
||
6917 | * @param string $min_path |
||
6918 | * @param string $non_min_path |
||
6919 | * @return string The URL to the file |
||
6920 | */ |
||
6921 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
6928 | } |
||
6929 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.