Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Jetpack { |
||
28 | public $xmlrpc_server = null; |
||
29 | |||
30 | private $xmlrpc_verification = null; |
||
31 | private $rest_authentication_status = null; |
||
32 | |||
33 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
34 | |||
35 | /** |
||
36 | * @var array The handles of styles that are concatenated into jetpack.css. |
||
37 | * |
||
38 | * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js. |
||
39 | */ |
||
40 | public $concatenated_style_handles = array( |
||
41 | 'jetpack-carousel', |
||
42 | 'grunion.css', |
||
43 | 'the-neverending-homepage', |
||
44 | 'jetpack_likes', |
||
45 | 'jetpack_related-posts', |
||
46 | 'sharedaddy', |
||
47 | 'jetpack-slideshow', |
||
48 | 'presentations', |
||
49 | 'quiz', |
||
50 | 'jetpack-subscriptions', |
||
51 | 'jetpack-responsive-videos-style', |
||
52 | 'jetpack-social-menu', |
||
53 | 'tiled-gallery', |
||
54 | 'jetpack_display_posts_widget', |
||
55 | 'gravatar-profile-widget', |
||
56 | 'goodreads-widget', |
||
57 | 'jetpack_social_media_icons_widget', |
||
58 | 'jetpack-top-posts-widget', |
||
59 | 'jetpack_image_widget', |
||
60 | 'jetpack-my-community-widget', |
||
61 | 'jetpack-authors-widget', |
||
62 | 'wordads', |
||
63 | 'eu-cookie-law-style', |
||
64 | 'flickr-widget-style', |
||
65 | 'jetpack-search-widget', |
||
66 | 'jetpack-simple-payments-widget-style', |
||
67 | 'jetpack-widget-social-icons-styles', |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * Contains all assets that have had their URL rewritten to minified versions. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | static $min_assets = array(); |
||
76 | |||
77 | public $plugins_to_deactivate = array( |
||
78 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
79 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
80 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
81 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
82 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
83 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
84 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
85 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
86 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
87 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
88 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
89 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
90 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
91 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
92 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
93 | ); |
||
94 | |||
95 | static $capability_translations = array( |
||
96 | 'administrator' => 'manage_options', |
||
97 | 'editor' => 'edit_others_posts', |
||
98 | 'author' => 'publish_posts', |
||
99 | 'contributor' => 'edit_posts', |
||
100 | 'subscriber' => 'read', |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
105 | * if the plugins are active. Used by filter_default_modules |
||
106 | * |
||
107 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
108 | * change `module-slug` and add this to your plugin: |
||
109 | * |
||
110 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
111 | * function my_jetpack_get_default_modules( $modules ) { |
||
112 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
113 | * } |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | private $conflicting_plugins = array( |
||
118 | 'comments' => array( |
||
119 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
120 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
121 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
122 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
123 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
124 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
125 | ), |
||
126 | 'comment-likes' => array( |
||
127 | 'Epoch' => 'epoch/plugincore.php', |
||
128 | ), |
||
129 | 'contact-form' => array( |
||
130 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
131 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
132 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
133 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
134 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
135 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
136 | ), |
||
137 | 'minileven' => array( |
||
138 | 'WPtouch' => 'wptouch/wptouch.php', |
||
139 | ), |
||
140 | 'latex' => array( |
||
141 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
142 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
143 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
144 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
145 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
146 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
147 | ), |
||
148 | 'protect' => array( |
||
149 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
150 | 'Captcha' => 'captcha/captcha.php', |
||
151 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
152 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
153 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
154 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
155 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
156 | 'Security-protection' => 'security-protection/security-protection.php', |
||
157 | 'Login Security' => 'login-security/login-security.php', |
||
158 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
159 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
160 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
161 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
162 | ), |
||
163 | 'random-redirect' => array( |
||
164 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
165 | ), |
||
166 | 'related-posts' => array( |
||
167 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
168 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
169 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
170 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
171 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
172 | 'outbrain' => 'outbrain/outbrain.php', |
||
173 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
174 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
175 | ), |
||
176 | 'sharedaddy' => array( |
||
177 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
178 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
179 | 'ShareThis' => 'share-this/sharethis.php', |
||
180 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
181 | ), |
||
182 | 'seo-tools' => array( |
||
183 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
184 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
185 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
186 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
187 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
188 | ), |
||
189 | 'verification-tools' => array( |
||
190 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
191 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
192 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
193 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
194 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
195 | ), |
||
196 | 'widget-visibility' => array( |
||
197 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
198 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
199 | ), |
||
200 | 'sitemaps' => array( |
||
201 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
202 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
203 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
204 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
205 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
206 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
207 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
208 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
209 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
210 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
211 | 'Sitemap' => 'sitemap/sitemap.php', |
||
212 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
213 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
214 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
215 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
216 | ), |
||
217 | 'lazy-images' => array( |
||
218 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
219 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
220 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
221 | ), |
||
222 | ); |
||
223 | |||
224 | /** |
||
225 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
226 | * |
||
227 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
228 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
229 | * |
||
230 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
231 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
232 | */ |
||
233 | private $open_graph_conflicting_plugins = array( |
||
234 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
235 | // 2 Click Social Media Buttons |
||
236 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
237 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
238 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
239 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
240 | // Open Graph Meta Tags by Heateor |
||
241 | 'facebook/facebook.php', // Facebook (official plugin) |
||
242 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
243 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
244 | // Facebook Featured Image & OG Meta Tags |
||
245 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
246 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
247 | // Facebook Open Graph Meta Tags for WordPress |
||
248 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
249 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
250 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
251 | // Fedmich's Facebook Open Graph Meta |
||
252 | 'network-publisher/networkpub.php', // Network Publisher |
||
253 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
254 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
255 | // NextScripts SNAP |
||
256 | 'og-tags/og-tags.php', // OG Tags |
||
257 | 'opengraph/opengraph.php', // Open Graph |
||
258 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
259 | // Open Graph Protocol Framework |
||
260 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
261 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
262 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
263 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
264 | 'sharepress/sharepress.php', // SharePress |
||
265 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
266 | 'social-discussions/social-discussions.php', // Social Discussions |
||
267 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
268 | 'socialize/socialize.php', // Socialize |
||
269 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
270 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
271 | // Tweet, Like, Google +1 and Share |
||
272 | 'wordbooker/wordbooker.php', // Wordbooker |
||
273 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
274 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
275 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
276 | // WP Facebook Like Send & Open Graph Meta |
||
277 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
278 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
279 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
280 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button |
||
281 | 'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox |
||
282 | ); |
||
283 | |||
284 | /** |
||
285 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
286 | */ |
||
287 | private $twitter_cards_conflicting_plugins = array( |
||
288 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
289 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
290 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
291 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
292 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
293 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
294 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
295 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
296 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
297 | 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter |
||
298 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
299 | ); |
||
300 | |||
301 | /** |
||
302 | * Message to display in admin_notice |
||
303 | * @var string |
||
304 | */ |
||
305 | public $message = ''; |
||
306 | |||
307 | /** |
||
308 | * Error to display in admin_notice |
||
309 | * @var string |
||
310 | */ |
||
311 | public $error = ''; |
||
312 | |||
313 | /** |
||
314 | * Modules that need more privacy description. |
||
315 | * @var string |
||
316 | */ |
||
317 | public $privacy_checks = ''; |
||
318 | |||
319 | /** |
||
320 | * Stats to record once the page loads |
||
321 | * |
||
322 | * @var array |
||
323 | */ |
||
324 | public $stats = array(); |
||
325 | |||
326 | /** |
||
327 | * Jetpack_Sync object |
||
328 | */ |
||
329 | public $sync; |
||
330 | |||
331 | /** |
||
332 | * Verified data for JSON authorization request |
||
333 | */ |
||
334 | public $json_api_authorization_request = array(); |
||
335 | |||
336 | /** |
||
337 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
338 | */ |
||
339 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
340 | |||
341 | /** |
||
342 | * Holds the singleton instance of this class |
||
343 | * @since 2.3.3 |
||
344 | * @var Jetpack |
||
345 | */ |
||
346 | static $instance = false; |
||
347 | |||
348 | /** |
||
349 | * Singleton |
||
350 | * @static |
||
351 | */ |
||
352 | public static function init() { |
||
353 | if ( ! self::$instance ) { |
||
354 | self::$instance = new Jetpack; |
||
355 | |||
356 | self::$instance->plugin_upgrade(); |
||
357 | } |
||
358 | |||
359 | return self::$instance; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Must never be called statically |
||
364 | */ |
||
365 | function plugin_upgrade() { |
||
366 | if ( Jetpack::is_active() ) { |
||
367 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
368 | if ( JETPACK__VERSION != $version ) { |
||
369 | // Prevent multiple upgrades at once - only a single process should trigger |
||
370 | // an upgrade to avoid stampedes |
||
371 | if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) { |
||
372 | return; |
||
373 | } |
||
374 | |||
375 | // Set a short lock to prevent multiple instances of the upgrade |
||
376 | set_transient( self::$plugin_upgrade_lock_key, 1, 10 ); |
||
377 | |||
378 | // check which active modules actually exist and remove others from active_modules list |
||
379 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
380 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
381 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
382 | Jetpack::update_active_modules( $modules ); |
||
383 | } |
||
384 | |||
385 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
386 | |||
387 | // Upgrade to 4.3.0 |
||
388 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
389 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
390 | } |
||
391 | |||
392 | // Make sure Markdown for posts gets turned back on |
||
393 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
394 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
395 | } |
||
396 | |||
397 | if ( did_action( 'wp_loaded' ) ) { |
||
398 | self::upgrade_on_load(); |
||
399 | } else { |
||
400 | add_action( |
||
401 | 'wp_loaded', |
||
402 | array( __CLASS__, 'upgrade_on_load' ) |
||
403 | ); |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Runs upgrade routines that need to have modules loaded. |
||
411 | */ |
||
412 | static function upgrade_on_load() { |
||
413 | |||
414 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
415 | // This can happen in case Jetpack has been just upgraded and is |
||
416 | // being initialized late during the page load. In this case we wait |
||
417 | // until the next proper admin page load with Jetpack active. |
||
418 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
419 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
420 | |||
421 | return; |
||
422 | } |
||
423 | |||
424 | Jetpack::maybe_set_version_option(); |
||
425 | |||
426 | if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) { |
||
427 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
428 | } |
||
429 | |||
430 | if ( |
||
431 | class_exists( 'Jetpack_Sitemap_Manager' ) |
||
432 | && version_compare( JETPACK__VERSION, '5.3', '>=' ) |
||
433 | ) { |
||
434 | do_action( 'jetpack_sitemaps_purge_data' ); |
||
435 | } |
||
436 | |||
437 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
438 | } |
||
439 | |||
440 | static function update_active_modules( $modules ) { |
||
494 | |||
495 | static function delete_active_modules() { |
||
498 | |||
499 | /** |
||
500 | * Constructor. Initializes WordPress hooks |
||
501 | */ |
||
502 | private function __construct() { |
||
503 | /* |
||
504 | * Check for and alert any deprecated hooks |
||
505 | */ |
||
506 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
507 | |||
508 | /* |
||
509 | * Enable enhanced handling of previewing sites in Calypso |
||
510 | */ |
||
511 | if ( Jetpack::is_active() ) { |
||
512 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php'; |
||
513 | add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 ); |
||
514 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php'; |
||
515 | add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 ); |
||
516 | } |
||
517 | |||
518 | /* |
||
519 | * Load things that should only be in Network Admin. |
||
520 | * |
||
521 | * For now blow away everything else until a more full |
||
522 | * understanding of what is needed at the network level is |
||
523 | * available |
||
524 | */ |
||
525 | if ( is_multisite() ) { |
||
526 | Jetpack_Network::init(); |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Prepare Gutenberg Editor functionality |
||
531 | */ |
||
532 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php'; |
||
533 | Jetpack_Gutenberg::init(); |
||
534 | Jetpack_Gutenberg::load_independent_blocks(); |
||
535 | add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) ); |
||
536 | |||
537 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
538 | |||
539 | // Unlink user before deleting the user from .com |
||
540 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
541 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
542 | |||
543 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms |
||
544 | if ( isset( $_GET['jetpack'] ) && 'comms' == $_GET['jetpack'] && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
545 | if ( ! defined( 'XMLRPC_REQUEST' ) ) { |
||
546 | define( 'XMLRPC_REQUEST', true ); |
||
547 | } |
||
548 | |||
549 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
550 | |||
551 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
552 | } |
||
553 | |||
554 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
555 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
|
|||
556 | |||
557 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
558 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
559 | |||
560 | $this->require_jetpack_authentication(); |
||
561 | |||
562 | if ( Jetpack::is_active() ) { |
||
563 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
564 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
565 | |||
566 | $signed = $this->verify_xml_rpc_signature(); |
||
567 | View Code Duplication | if ( $signed && ! is_wp_error( $signed ) ) { |
|
568 | // The actual API methods. |
||
569 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
570 | } else { |
||
571 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
572 | // active Jetpack connection, so that additional users can link their account. |
||
573 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
574 | } |
||
575 | View Code Duplication | } else { |
|
576 | // The bootstrap API methods. |
||
577 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
578 | $signed = $this->verify_xml_rpc_signature(); |
||
579 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
580 | // the jetpack Provision method is available for blog-token-signed requests |
||
581 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
582 | } |
||
583 | } |
||
584 | |||
585 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
586 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
587 | } elseif ( |
||
588 | is_admin() && |
||
589 | isset( $_POST['action'] ) && ( |
||
590 | 'jetpack_upload_file' == $_POST['action'] || |
||
591 | 'jetpack_update_file' == $_POST['action'] |
||
592 | ) |
||
593 | ) { |
||
594 | $this->require_jetpack_authentication(); |
||
595 | $this->add_remote_request_handlers(); |
||
596 | } else { |
||
597 | if ( Jetpack::is_active() ) { |
||
598 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
599 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
600 | } |
||
601 | } |
||
602 | |||
603 | if ( Jetpack::is_active() ) { |
||
604 | Jetpack_Heartbeat::init(); |
||
605 | if ( Jetpack::is_module_active( 'stats' ) && Jetpack::is_module_active( 'search' ) ) { |
||
606 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php'; |
||
607 | Jetpack_Search_Performance_Logger::init(); |
||
608 | } |
||
609 | } |
||
610 | |||
611 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
612 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
613 | |||
614 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
615 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
616 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
617 | } |
||
618 | |||
619 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
620 | |||
621 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
622 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
623 | |||
624 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
625 | |||
626 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
627 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
628 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
629 | |||
630 | // returns HTTPS support status |
||
631 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
632 | |||
633 | // If any module option is updated before Jump Start is dismissed, hide Jump Start. |
||
634 | add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) ); |
||
635 | |||
636 | // JITM AJAX callback function |
||
637 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
638 | |||
639 | // Universal ajax callback for all tracking events triggered via js |
||
640 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
641 | |||
642 | add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) ); |
||
643 | |||
644 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
645 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
646 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
647 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
648 | |||
649 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
650 | |||
651 | /** |
||
652 | * These actions run checks to load additional files. |
||
653 | * They check for external files or plugins, so they need to run as late as possible. |
||
654 | */ |
||
655 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
656 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
657 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
658 | |||
659 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
660 | add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 ); |
||
661 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
662 | |||
663 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
664 | |||
665 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
666 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
667 | |||
668 | // A filter to control all just in time messages |
||
669 | add_filter( 'jetpack_just_in_time_msgs', '__return_true', 9 ); |
||
670 | add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9); |
||
671 | |||
672 | // If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin. |
||
673 | // We should make sure to only do this for front end links. |
||
674 | if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { |
||
675 | add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 ); |
||
676 | add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 ); |
||
677 | |||
678 | //we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions |
||
679 | //so they point moderation links on emails to Calypso |
||
680 | jetpack_require_lib( 'functions.wp-notify' ); |
||
681 | } |
||
682 | |||
683 | // Update the Jetpack plan from API on heartbeats |
||
684 | add_action( 'jetpack_heartbeat', array( 'Jetpack_Plan', 'refresh_from_wpcom' ) ); |
||
685 | |||
686 | /** |
||
687 | * This is the hack to concatenate all css files into one. |
||
688 | * For description and reasoning see the implode_frontend_css method |
||
689 | * |
||
690 | * Super late priority so we catch all the registered styles |
||
691 | */ |
||
692 | if( !is_admin() ) { |
||
693 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
694 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * These are sync actions that we need to keep track of for jitms |
||
699 | */ |
||
700 | add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 ); |
||
701 | |||
702 | // Actually push the stats on shutdown. |
||
703 | if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) { |
||
704 | add_action( 'shutdown', array( $this, 'push_stats' ) ); |
||
705 | } |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
710 | * |
||
711 | * @param $domains |
||
712 | */ |
||
713 | function add_wpcom_to_allowed_redirect_hosts( $domains ) { |
||
714 | add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_domain' ) ); |
||
715 | } |
||
716 | |||
717 | /** |
||
718 | * Return $domains, with 'wordpress.com' appended. |
||
719 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
720 | * |
||
721 | * @param $domains |
||
722 | * @return array |
||
723 | */ |
||
724 | function allow_wpcom_domain( $domains ) { |
||
725 | if ( empty( $domains ) ) { |
||
726 | $domains = array(); |
||
727 | } |
||
728 | $domains[] = 'wordpress.com'; |
||
729 | return array_unique( $domains ); |
||
730 | } |
||
731 | |||
732 | function point_edit_post_links_to_calypso( $default_url, $post_id ) { |
||
733 | $post = get_post( $post_id ); |
||
734 | |||
735 | if ( empty( $post ) ) { |
||
736 | return $default_url; |
||
737 | } |
||
738 | |||
739 | $post_type = $post->post_type; |
||
740 | |||
741 | // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso. |
||
742 | // https://en.support.wordpress.com/custom-post-types/ |
||
743 | $allowed_post_types = array( |
||
744 | 'post' => 'post', |
||
745 | 'page' => 'page', |
||
746 | 'jetpack-portfolio' => 'edit/jetpack-portfolio', |
||
747 | 'jetpack-testimonial' => 'edit/jetpack-testimonial', |
||
748 | ); |
||
749 | |||
750 | if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) { |
||
751 | return $default_url; |
||
752 | } |
||
753 | |||
754 | $path_prefix = $allowed_post_types[ $post_type ]; |
||
755 | |||
756 | $site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
757 | |||
758 | return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); |
||
759 | } |
||
760 | |||
761 | function point_edit_comment_links_to_calypso( $url ) { |
||
762 | // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID. |
||
763 | wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args ); |
||
764 | return esc_url( sprintf( 'https://wordpress.com/comment/%s/%d', |
||
765 | Jetpack::build_raw_urls( get_home_url() ), |
||
766 | $query_args['amp;c'] |
||
767 | ) ); |
||
768 | } |
||
769 | |||
770 | function jetpack_track_last_sync_callback( $params ) { |
||
771 | /** |
||
772 | * Filter to turn off jitm caching |
||
773 | * |
||
774 | * @since 5.4.0 |
||
775 | * |
||
776 | * @param bool false Whether to cache just in time messages |
||
777 | */ |
||
778 | if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
779 | return $params; |
||
780 | } |
||
781 | |||
782 | if ( is_array( $params ) && isset( $params[0] ) ) { |
||
783 | $option = $params[0]; |
||
784 | if ( 'active_plugins' === $option ) { |
||
785 | // use the cache if we can, but not terribly important if it gets evicted |
||
786 | set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS ); |
||
787 | } |
||
788 | } |
||
789 | |||
790 | return $params; |
||
791 | } |
||
792 | |||
793 | function jetpack_connection_banner_callback() { |
||
794 | check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' ); |
||
795 | |||
796 | if ( isset( $_REQUEST['dismissBanner'] ) ) { |
||
797 | Jetpack_Options::update_option( 'dismissed_connection_banner', 1 ); |
||
798 | wp_send_json_success(); |
||
799 | } |
||
800 | |||
801 | wp_die(); |
||
802 | } |
||
803 | |||
804 | /** |
||
805 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
806 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
807 | * ensure that Core and other plugins' methods are not exposed. |
||
808 | * |
||
809 | * @param array $methods |
||
810 | * @return array filtered $methods |
||
811 | */ |
||
812 | function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
813 | $jetpack_methods = array(); |
||
814 | |||
815 | foreach ( $methods as $method => $callback ) { |
||
816 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
817 | $jetpack_methods[ $method ] = $callback; |
||
818 | } |
||
819 | } |
||
820 | |||
821 | return $jetpack_methods; |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
826 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
827 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
828 | * which is accessible via a different URI. Most of the below is copied directly |
||
829 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
830 | */ |
||
831 | function alternate_xmlrpc() { |
||
832 | // phpcs:disable PHPCompatibility.PHP.RemovedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
833 | global $HTTP_RAW_POST_DATA; |
||
834 | |||
835 | // Some browser-embedded clients send cookies. We don't want them. |
||
836 | $_COOKIE = array(); |
||
837 | |||
838 | // A bug in PHP < 5.2.2 makes $HTTP_RAW_POST_DATA not set by default, |
||
839 | // but we can do it ourself. |
||
840 | if ( ! isset( $HTTP_RAW_POST_DATA ) ) { |
||
841 | $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' ); |
||
842 | } |
||
843 | |||
844 | // fix for mozBlog and other cases where '<?xml' isn't on the very first line |
||
845 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
846 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
847 | } |
||
848 | |||
849 | // phpcs:enable |
||
850 | |||
851 | include_once( ABSPATH . 'wp-admin/includes/admin.php' ); |
||
852 | include_once( ABSPATH . WPINC . '/class-IXR.php' ); |
||
853 | include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' ); |
||
854 | |||
855 | /** |
||
856 | * Filters the class used for handling XML-RPC requests. |
||
857 | * |
||
858 | * @since 3.1.0 |
||
859 | * |
||
860 | * @param string $class The name of the XML-RPC server class. |
||
861 | */ |
||
862 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
863 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class; |
||
864 | |||
865 | // Fire off the request |
||
866 | nocache_headers(); |
||
867 | $wp_xmlrpc_server->serve_request(); |
||
868 | |||
869 | exit; |
||
870 | } |
||
871 | |||
872 | function jetpack_admin_ajax_tracks_callback() { |
||
873 | // Check for nonce |
||
874 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
875 | wp_die( 'Permissions check failed.' ); |
||
876 | } |
||
877 | |||
878 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
879 | wp_die( 'No valid event name or type.' ); |
||
880 | } |
||
881 | |||
882 | $tracks_data = array(); |
||
883 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
884 | if ( is_array( $_REQUEST['tracksEventProp'] ) ) { |
||
885 | $tracks_data = $_REQUEST['tracksEventProp']; |
||
886 | } else { |
||
887 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
888 | } |
||
889 | } |
||
890 | |||
891 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
892 | wp_send_json_success(); |
||
893 | wp_die(); |
||
894 | } |
||
895 | |||
896 | /** |
||
897 | * The callback for the JITM ajax requests. |
||
898 | */ |
||
899 | function jetpack_jitm_ajax_callback() { |
||
900 | // Check for nonce |
||
901 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
902 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
903 | } |
||
904 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
905 | $module_slug = $_REQUEST['jitmModule']; |
||
906 | Jetpack::log( 'activate', $module_slug ); |
||
907 | Jetpack::activate_module( $module_slug, false, false ); |
||
908 | Jetpack::state( 'message', 'no_message' ); |
||
909 | |||
910 | //A Jetpack module is being activated through a JITM, track it |
||
911 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
912 | $this->do_stats( 'server_side' ); |
||
913 | |||
914 | wp_send_json_success(); |
||
915 | } |
||
916 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
917 | // get the hide_jitm options array |
||
918 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
919 | $module_slug = $_REQUEST['jitmModule']; |
||
920 | |||
921 | if( ! $jetpack_hide_jitm ) { |
||
922 | $jetpack_hide_jitm = array( |
||
923 | $module_slug => 'hide' |
||
924 | ); |
||
925 | } else { |
||
926 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
927 | } |
||
928 | |||
929 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
930 | |||
931 | //jitm is being dismissed forever, track it |
||
932 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
933 | $this->do_stats( 'server_side' ); |
||
934 | |||
935 | wp_send_json_success(); |
||
936 | } |
||
937 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
938 | $module_slug = $_REQUEST['jitmModule']; |
||
939 | |||
940 | // User went to WordPress.com, track this |
||
941 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
942 | $this->do_stats( 'server_side' ); |
||
943 | |||
944 | wp_send_json_success(); |
||
945 | } |
||
946 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
947 | $track = $_REQUEST['jitmModule']; |
||
948 | |||
949 | // User is viewing JITM, track it. |
||
950 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
951 | $this->do_stats( 'server_side' ); |
||
952 | |||
953 | wp_send_json_success(); |
||
954 | } |
||
955 | } |
||
956 | |||
957 | /** |
||
958 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
959 | */ |
||
960 | function push_stats() { |
||
961 | if ( ! empty( $this->stats ) ) { |
||
962 | $this->do_stats( 'server_side' ); |
||
963 | } |
||
964 | } |
||
965 | |||
966 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
967 | switch( $cap ) { |
||
968 | case 'jetpack_connect' : |
||
969 | case 'jetpack_reconnect' : |
||
970 | if ( Jetpack::is_development_mode() ) { |
||
971 | $caps = array( 'do_not_allow' ); |
||
972 | break; |
||
973 | } |
||
974 | /** |
||
975 | * Pass through. If it's not development mode, these should match disconnect. |
||
976 | * Let users disconnect if it's development mode, just in case things glitch. |
||
977 | */ |
||
978 | case 'jetpack_disconnect' : |
||
979 | /** |
||
980 | * In multisite, can individual site admins manage their own connection? |
||
981 | * |
||
982 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
983 | */ |
||
984 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
985 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
986 | /** |
||
987 | * We need to update the option name -- it's terribly unclear which |
||
988 | * direction the override goes. |
||
989 | * |
||
990 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
991 | */ |
||
992 | $caps = array( 'do_not_allow' ); |
||
993 | break; |
||
994 | } |
||
995 | } |
||
996 | |||
997 | $caps = array( 'manage_options' ); |
||
998 | break; |
||
999 | case 'jetpack_manage_modules' : |
||
1000 | case 'jetpack_activate_modules' : |
||
1001 | case 'jetpack_deactivate_modules' : |
||
1002 | $caps = array( 'manage_options' ); |
||
1003 | break; |
||
1004 | case 'jetpack_configure_modules' : |
||
1005 | $caps = array( 'manage_options' ); |
||
1006 | break; |
||
1007 | case 'jetpack_manage_autoupdates' : |
||
1008 | $caps = array( |
||
1009 | 'manage_options', |
||
1010 | 'update_plugins', |
||
1011 | ); |
||
1012 | break; |
||
1013 | case 'jetpack_network_admin_page': |
||
1014 | case 'jetpack_network_settings_page': |
||
1015 | $caps = array( 'manage_network_plugins' ); |
||
1016 | break; |
||
1017 | case 'jetpack_network_sites_page': |
||
1018 | $caps = array( 'manage_sites' ); |
||
1019 | break; |
||
1020 | case 'jetpack_admin_page' : |
||
1021 | if ( Jetpack::is_development_mode() ) { |
||
1022 | $caps = array( 'manage_options' ); |
||
1023 | break; |
||
1024 | } else { |
||
1025 | $caps = array( 'read' ); |
||
1026 | } |
||
1027 | break; |
||
1028 | case 'jetpack_connect_user' : |
||
1029 | if ( Jetpack::is_development_mode() ) { |
||
1030 | $caps = array( 'do_not_allow' ); |
||
1031 | break; |
||
1032 | } |
||
1033 | $caps = array( 'read' ); |
||
1034 | break; |
||
1035 | } |
||
1036 | return $caps; |
||
1037 | } |
||
1038 | |||
1039 | function require_jetpack_authentication() { |
||
1040 | // Don't let anyone authenticate |
||
1041 | $_COOKIE = array(); |
||
1042 | remove_all_filters( 'authenticate' ); |
||
1043 | remove_all_actions( 'wp_login_failed' ); |
||
1044 | |||
1045 | if ( Jetpack::is_active() ) { |
||
1046 | // Allow Jetpack authentication |
||
1047 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
1048 | } |
||
1049 | } |
||
1050 | |||
1051 | /** |
||
1052 | * Load language files |
||
1053 | * @action plugins_loaded |
||
1054 | */ |
||
1055 | public static function plugin_textdomain() { |
||
1056 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
1057 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
1058 | } |
||
1059 | |||
1060 | /** |
||
1061 | * Register assets for use in various modules and the Jetpack admin page. |
||
1062 | * |
||
1063 | * @uses wp_script_is, wp_register_script, plugins_url |
||
1064 | * @action wp_loaded |
||
1065 | * @return null |
||
1066 | */ |
||
1067 | public function register_assets() { |
||
1068 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
1069 | wp_register_script( |
||
1070 | 'spin', |
||
1071 | self::get_file_url_for_environment( '_inc/build/spin.min.js', '_inc/spin.js' ), |
||
1072 | false, |
||
1073 | '1.3' |
||
1074 | ); |
||
1075 | } |
||
1076 | |||
1077 | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
||
1078 | wp_register_script( |
||
1079 | 'jquery.spin', |
||
1080 | self::get_file_url_for_environment( '_inc/build/jquery.spin.min.js', '_inc/jquery.spin.js' ), |
||
1081 | array( 'jquery', 'spin' ), |
||
1082 | '1.3' |
||
1083 | ); |
||
1084 | } |
||
1085 | |||
1086 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
1087 | wp_register_script( |
||
1088 | 'jetpack-gallery-settings', |
||
1089 | self::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ), |
||
1090 | array( 'media-views' ), |
||
1091 | '20121225' |
||
1092 | ); |
||
1093 | } |
||
1094 | |||
1095 | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
||
1096 | wp_register_script( |
||
1097 | 'jetpack-twitter-timeline', |
||
1098 | self::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ), |
||
1099 | array( 'jquery' ), |
||
1100 | '4.0.0', |
||
1101 | true |
||
1102 | ); |
||
1103 | } |
||
1104 | |||
1105 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
1106 | wp_register_script( |
||
1107 | 'jetpack-facebook-embed', |
||
1108 | self::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ), |
||
1109 | array( 'jquery' ), |
||
1110 | null, |
||
1111 | true |
||
1112 | ); |
||
1113 | |||
1114 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
1115 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
1116 | if ( ! is_numeric( $fb_app_id ) ) { |
||
1117 | $fb_app_id = ''; |
||
1118 | } |
||
1119 | wp_localize_script( |
||
1120 | 'jetpack-facebook-embed', |
||
1121 | 'jpfbembed', |
||
1122 | array( |
||
1123 | 'appid' => $fb_app_id, |
||
1124 | 'locale' => $this->get_locale(), |
||
1125 | ) |
||
1126 | ); |
||
1127 | } |
||
1128 | |||
1129 | /** |
||
1130 | * As jetpack_register_genericons is by default fired off a hook, |
||
1131 | * the hook may have already fired by this point. |
||
1132 | * So, let's just trigger it manually. |
||
1133 | */ |
||
1134 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
1135 | jetpack_register_genericons(); |
||
1136 | |||
1137 | /** |
||
1138 | * Register the social logos |
||
1139 | */ |
||
1140 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
1141 | jetpack_register_social_logos(); |
||
1142 | |||
1143 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
1144 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
1145 | } |
||
1146 | |||
1147 | /** |
||
1148 | * Guess locale from language code. |
||
1149 | * |
||
1150 | * @param string $lang Language code. |
||
1151 | * @return string|bool |
||
1152 | */ |
||
1153 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
1154 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
1155 | return 'en_US'; |
||
1156 | } |
||
1157 | |||
1158 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
1159 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
1160 | return false; |
||
1161 | } |
||
1162 | |||
1163 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
1164 | } |
||
1165 | |||
1166 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
1167 | // WP.com: get_locale() returns 'it' |
||
1168 | $locale = GP_Locales::by_slug( $lang ); |
||
1169 | } else { |
||
1170 | // Jetpack: get_locale() returns 'it_IT'; |
||
1171 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
1172 | } |
||
1173 | |||
1174 | if ( ! $locale ) { |
||
1175 | return false; |
||
1176 | } |
||
1177 | |||
1178 | if ( empty( $locale->facebook_locale ) ) { |
||
1179 | if ( empty( $locale->wp_locale ) ) { |
||
1180 | return false; |
||
1181 | } else { |
||
1182 | // Facebook SDK is smart enough to fall back to en_US if a |
||
1183 | // locale isn't supported. Since supported Facebook locales |
||
1184 | // can fall out of sync, we'll attempt to use the known |
||
1185 | // wp_locale value and rely on said fallback. |
||
1186 | return $locale->wp_locale; |
||
1187 | } |
||
1188 | } |
||
1189 | |||
1190 | return $locale->facebook_locale; |
||
1191 | } |
||
1192 | |||
1193 | /** |
||
1194 | * Get the locale. |
||
1195 | * |
||
1196 | * @return string|bool |
||
1197 | */ |
||
1198 | function get_locale() { |
||
1199 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
1200 | |||
1201 | if ( ! $locale ) { |
||
1202 | $locale = 'en_US'; |
||
1203 | } |
||
1204 | |||
1205 | return $locale; |
||
1206 | } |
||
1207 | |||
1208 | /** |
||
1209 | * Device Pixels support |
||
1210 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
1211 | */ |
||
1212 | function devicepx() { |
||
1213 | if ( Jetpack::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) { |
||
1214 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
1215 | } |
||
1216 | } |
||
1217 | |||
1218 | /** |
||
1219 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1220 | * @param bool $option |
||
1221 | * @return string |
||
1222 | */ |
||
1223 | public function jetpack_main_network_site_option( $option ) { |
||
1224 | return network_site_url(); |
||
1225 | } |
||
1226 | /** |
||
1227 | * Network Name. |
||
1228 | */ |
||
1229 | static function network_name( $option = null ) { |
||
1230 | global $current_site; |
||
1231 | return $current_site->site_name; |
||
1232 | } |
||
1233 | /** |
||
1234 | * Does the network allow new user and site registrations. |
||
1235 | * @return string |
||
1236 | */ |
||
1237 | static function network_allow_new_registrations( $option = null ) { |
||
1238 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
1239 | } |
||
1240 | /** |
||
1241 | * Does the network allow admins to add new users. |
||
1242 | * @return boolian |
||
1243 | */ |
||
1244 | static function network_add_new_users( $option = null ) { |
||
1245 | return (bool) get_site_option( 'add_new_users' ); |
||
1246 | } |
||
1247 | /** |
||
1248 | * File upload psace left per site in MB. |
||
1249 | * -1 means NO LIMIT. |
||
1250 | * @return number |
||
1251 | */ |
||
1252 | static function network_site_upload_space( $option = null ) { |
||
1253 | // value in MB |
||
1254 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
1255 | } |
||
1256 | |||
1257 | /** |
||
1258 | * Network allowed file types. |
||
1259 | * @return string |
||
1260 | */ |
||
1261 | static function network_upload_file_types( $option = null ) { |
||
1262 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
1263 | } |
||
1264 | |||
1265 | /** |
||
1266 | * Maximum file upload size set by the network. |
||
1267 | * @return number |
||
1268 | */ |
||
1269 | static function network_max_upload_file_size( $option = null ) { |
||
1270 | // value in KB |
||
1271 | return get_site_option( 'fileupload_maxk', 300 ); |
||
1272 | } |
||
1273 | |||
1274 | /** |
||
1275 | * Lets us know if a site allows admins to manage the network. |
||
1276 | * @return array |
||
1277 | */ |
||
1278 | static function network_enable_administration_menus( $option = null ) { |
||
1279 | return get_site_option( 'menu_items' ); |
||
1280 | } |
||
1281 | |||
1282 | /** |
||
1283 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1284 | * jetpack_other_linked_admins transient. |
||
1285 | * |
||
1286 | * @since 4.3.2 |
||
1287 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1288 | * |
||
1289 | * @param int $user_id The user ID whose role changed. |
||
1290 | * @param string $role The new role. |
||
1291 | * @param array $old_roles An array of the user's previous roles. |
||
1292 | */ |
||
1293 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1294 | if ( 'administrator' == $role |
||
1295 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
1296 | || is_null( $old_roles ) |
||
1297 | ) { |
||
1298 | delete_transient( 'jetpack_other_linked_admins' ); |
||
1299 | } |
||
1300 | } |
||
1301 | |||
1302 | /** |
||
1303 | * Checks to see if there are any other users available to become primary |
||
1304 | * Users must both: |
||
1305 | * - Be linked to wpcom |
||
1306 | * - Be an admin |
||
1307 | * |
||
1308 | * @return mixed False if no other users are linked, Int if there are. |
||
1309 | */ |
||
1310 | static function get_other_linked_admins() { |
||
1311 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
1312 | |||
1313 | if ( false === $other_linked_users ) { |
||
1314 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
1315 | if ( count( $admins ) > 1 ) { |
||
1316 | $available = array(); |
||
1317 | foreach ( $admins as $admin ) { |
||
1318 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
1319 | $available[] = $admin->ID; |
||
1320 | } |
||
1321 | } |
||
1322 | |||
1323 | $count_connected_admins = count( $available ); |
||
1324 | if ( count( $available ) > 1 ) { |
||
1325 | $other_linked_users = $count_connected_admins; |
||
1326 | } else { |
||
1327 | $other_linked_users = 0; |
||
1328 | } |
||
1329 | } else { |
||
1330 | $other_linked_users = 0; |
||
1331 | } |
||
1332 | |||
1333 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
1334 | } |
||
1335 | |||
1336 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
1337 | } |
||
1338 | |||
1339 | /** |
||
1340 | * Return whether we are dealing with a multi network setup or not. |
||
1341 | * The reason we are type casting this is because we want to avoid the situation where |
||
1342 | * the result is false since when is_main_network_option return false it cases |
||
1343 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1344 | * database which could be set to anything as opposed to what this function returns. |
||
1345 | * @param bool $option |
||
1346 | * |
||
1347 | * @return boolean |
||
1348 | */ |
||
1349 | public function is_main_network_option( $option ) { |
||
1350 | // return '1' or '' |
||
1351 | return (string) (bool) Jetpack::is_multi_network(); |
||
1352 | } |
||
1353 | |||
1354 | /** |
||
1355 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1356 | * |
||
1357 | * @param string $option |
||
1358 | * @return boolean |
||
1359 | */ |
||
1360 | public function is_multisite( $option ) { |
||
1361 | return (string) (bool) is_multisite(); |
||
1362 | } |
||
1363 | |||
1364 | /** |
||
1365 | * Implemented since there is no core is multi network function |
||
1366 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1367 | * |
||
1368 | * @since 3.3 |
||
1369 | * @return boolean |
||
1370 | */ |
||
1371 | public static function is_multi_network() { |
||
1372 | global $wpdb; |
||
1373 | |||
1374 | // if we don't have a multi site setup no need to do any more |
||
1375 | if ( ! is_multisite() ) { |
||
1376 | return false; |
||
1377 | } |
||
1378 | |||
1379 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
1380 | if ( $num_sites > 1 ) { |
||
1381 | return true; |
||
1382 | } else { |
||
1383 | return false; |
||
1384 | } |
||
1385 | } |
||
1386 | |||
1387 | /** |
||
1388 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1389 | * @return null |
||
1390 | */ |
||
1391 | function update_jetpack_main_network_site_option() { |
||
1392 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1393 | } |
||
1394 | /** |
||
1395 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1396 | * |
||
1397 | */ |
||
1398 | function update_jetpack_network_settings() { |
||
1399 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1400 | // Only sync this info for the main network site. |
||
1401 | } |
||
1402 | |||
1403 | /** |
||
1404 | * Get back if the current site is single user site. |
||
1405 | * |
||
1406 | * @return bool |
||
1407 | */ |
||
1408 | public static function is_single_user_site() { |
||
1409 | global $wpdb; |
||
1410 | |||
1411 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
1412 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
1413 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
1414 | } |
||
1415 | return 1 === (int) $some_users; |
||
1416 | } |
||
1417 | |||
1418 | /** |
||
1419 | * Returns true if the site has file write access false otherwise. |
||
1420 | * @return string ( '1' | '0' ) |
||
1421 | **/ |
||
1422 | public static function file_system_write_access() { |
||
1423 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
1424 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
1425 | } |
||
1426 | |||
1427 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
1428 | |||
1429 | $filesystem_method = get_filesystem_method(); |
||
1430 | if ( $filesystem_method === 'direct' ) { |
||
1431 | return 1; |
||
1432 | } |
||
1433 | |||
1434 | ob_start(); |
||
1435 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
1436 | ob_end_clean(); |
||
1437 | if ( $filesystem_credentials_are_stored ) { |
||
1438 | return 1; |
||
1439 | } |
||
1440 | return 0; |
||
1441 | } |
||
1442 | |||
1443 | /** |
||
1444 | * Finds out if a site is using a version control system. |
||
1445 | * @return string ( '1' | '0' ) |
||
1446 | **/ |
||
1447 | public static function is_version_controlled() { |
||
1448 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
1449 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
1450 | } |
||
1451 | |||
1452 | /** |
||
1453 | * Determines whether the current theme supports featured images or not. |
||
1454 | * @return string ( '1' | '0' ) |
||
1455 | */ |
||
1456 | public static function featured_images_enabled() { |
||
1457 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1458 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
1459 | } |
||
1460 | |||
1461 | /** |
||
1462 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1463 | * |
||
1464 | * @deprecated 4.7 use get_avatar_url instead. |
||
1465 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1466 | * @param int $size Size of the avatar image |
||
1467 | * @param string $default URL to a default image to use if no avatar is available |
||
1468 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1469 | * |
||
1470 | * @return array |
||
1471 | */ |
||
1472 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1473 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
1474 | return get_avatar_url( $id_or_email, array( |
||
1475 | 'size' => $size, |
||
1476 | 'default' => $default, |
||
1477 | 'force_default' => $force_display, |
||
1478 | ) ); |
||
1479 | } |
||
1480 | |||
1481 | /** |
||
1482 | * jetpack_updates is saved in the following schema: |
||
1483 | * |
||
1484 | * array ( |
||
1485 | * 'plugins' => (int) Number of plugin updates available. |
||
1486 | * 'themes' => (int) Number of theme updates available. |
||
1487 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1488 | * 'translations' => (int) Number of translation updates available. |
||
1489 | * 'total' => (int) Total of all available updates. |
||
1490 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1491 | * ) |
||
1492 | * @return array |
||
1493 | */ |
||
1494 | public static function get_updates() { |
||
1495 | $update_data = wp_get_update_data(); |
||
1496 | |||
1497 | // Stores the individual update counts as well as the total count. |
||
1498 | if ( isset( $update_data['counts'] ) ) { |
||
1499 | $updates = $update_data['counts']; |
||
1500 | } |
||
1501 | |||
1502 | // If we need to update WordPress core, let's find the latest version number. |
||
1503 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
1504 | $cur = get_preferred_from_update_core(); |
||
1505 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
1506 | $updates['wp_update_version'] = $cur->current; |
||
1507 | } |
||
1508 | } |
||
1509 | return isset( $updates ) ? $updates : array(); |
||
1510 | } |
||
1511 | |||
1512 | public static function get_update_details() { |
||
1513 | $update_details = array( |
||
1514 | 'update_core' => get_site_transient( 'update_core' ), |
||
1515 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
1516 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
1517 | ); |
||
1518 | return $update_details; |
||
1519 | } |
||
1520 | |||
1521 | public static function refresh_update_data() { |
||
1522 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1523 | |||
1524 | } |
||
1525 | |||
1526 | public static function refresh_theme_data() { |
||
1527 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1528 | } |
||
1529 | |||
1530 | /** |
||
1531 | * Is Jetpack active? |
||
1532 | */ |
||
1533 | public static function is_active() { |
||
1534 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1535 | } |
||
1536 | |||
1537 | /** |
||
1538 | * Make an API call to WordPress.com for plan status |
||
1539 | * |
||
1540 | * @deprecated 7.2.0 Use Jetpack_Plan::refresh_from_wpcom. |
||
1541 | * |
||
1542 | * @return bool True if plan is updated, false if no update |
||
1543 | */ |
||
1544 | public static function refresh_active_plan_from_wpcom() { |
||
1545 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::refresh_from_wpcom' ); |
||
1546 | return Jetpack_Plan::refresh_from_wpcom(); |
||
1547 | } |
||
1548 | |||
1549 | /** |
||
1550 | * Get the plan that this Jetpack site is currently using |
||
1551 | * |
||
1552 | * @deprecated 7.2.0 Use Jetpack_Plan::get. |
||
1553 | * @return array Active Jetpack plan details. |
||
1554 | */ |
||
1555 | public static function get_active_plan() { |
||
1556 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::get' ); |
||
1557 | return Jetpack_Plan::get(); |
||
1558 | } |
||
1559 | |||
1560 | /** |
||
1561 | * Determine whether the active plan supports a particular feature |
||
1562 | * |
||
1563 | * @deprecated 7.2.0 Use Jetpack_Plan::supports. |
||
1564 | * @return bool True if plan supports feature, false if not. |
||
1565 | */ |
||
1566 | public static function active_plan_supports( $feature ) { |
||
1567 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::supports' ); |
||
1568 | return Jetpack_Plan::supports( $feature ); |
||
1569 | } |
||
1570 | |||
1571 | /** |
||
1572 | * Is Jetpack in development (offline) mode? |
||
1573 | */ |
||
1574 | public static function is_development_mode() { |
||
1575 | $development_mode = false; |
||
1576 | |||
1577 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
1578 | $development_mode = JETPACK_DEV_DEBUG; |
||
1579 | } elseif ( $site_url = site_url() ) { |
||
1580 | $development_mode = false === strpos( $site_url, '.' ); |
||
1581 | } |
||
1582 | |||
1583 | /** |
||
1584 | * Filters Jetpack's development mode. |
||
1585 | * |
||
1586 | * @see https://jetpack.com/support/development-mode/ |
||
1587 | * |
||
1588 | * @since 2.2.1 |
||
1589 | * |
||
1590 | * @param bool $development_mode Is Jetpack's development mode active. |
||
1591 | */ |
||
1592 | $development_mode = ( bool ) apply_filters( 'jetpack_development_mode', $development_mode ); |
||
1593 | return $development_mode; |
||
1594 | } |
||
1595 | |||
1596 | /** |
||
1597 | * Whether the site is currently onboarding or not. |
||
1598 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
1599 | * |
||
1600 | * @since 5.8 |
||
1601 | * |
||
1602 | * @access public |
||
1603 | * @static |
||
1604 | * |
||
1605 | * @return bool True if the site is currently onboarding, false otherwise |
||
1606 | */ |
||
1607 | public static function is_onboarding() { |
||
1608 | return Jetpack_Options::get_option( 'onboarding' ) !== false; |
||
1609 | } |
||
1610 | |||
1611 | /** |
||
1612 | * Determines reason for Jetpack development mode. |
||
1613 | */ |
||
1614 | public static function development_mode_trigger_text() { |
||
1615 | if ( ! Jetpack::is_development_mode() ) { |
||
1616 | return __( 'Jetpack is not in Development Mode.', 'jetpack' ); |
||
1617 | } |
||
1618 | |||
1619 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
1620 | $notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' ); |
||
1621 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
1622 | $notice = __( 'The site URL lacking a dot (e.g. http://localhost).', 'jetpack' ); |
||
1623 | } else { |
||
1624 | $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' ); |
||
1625 | } |
||
1626 | |||
1627 | return $notice; |
||
1628 | |||
1629 | } |
||
1630 | /** |
||
1631 | * Get Jetpack development mode notice text and notice class. |
||
1632 | * |
||
1633 | * Mirrors the checks made in Jetpack::is_development_mode |
||
1634 | * |
||
1635 | */ |
||
1636 | public static function show_development_mode_notice() { |
||
1637 | View Code Duplication | if ( Jetpack::is_development_mode() ) { |
|
1638 | $notice = sprintf( |
||
1639 | /* translators: %s is a URL */ |
||
1640 | __( 'In <a href="%s" target="_blank">Development Mode</a>:', 'jetpack' ), |
||
1641 | 'https://jetpack.com/support/development-mode/' |
||
1642 | ); |
||
1643 | |||
1644 | $notice .= ' ' . Jetpack::development_mode_trigger_text(); |
||
1645 | |||
1646 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1647 | } |
||
1648 | |||
1649 | // Throw up a notice if using a development version and as for feedback. |
||
1650 | if ( Jetpack::is_development_version() ) { |
||
1651 | /* translators: %s is a URL */ |
||
1652 | $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/' ); |
||
1653 | |||
1654 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1655 | } |
||
1656 | // Throw up a notice if using staging mode |
||
1657 | if ( Jetpack::is_staging_site() ) { |
||
1658 | /* translators: %s is a URL */ |
||
1659 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
1660 | |||
1661 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1662 | } |
||
1663 | } |
||
1664 | |||
1665 | /** |
||
1666 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1667 | */ |
||
1668 | public static function is_development_version() { |
||
1669 | /** |
||
1670 | * Allows filtering whether this is a development version of Jetpack. |
||
1671 | * |
||
1672 | * This filter is especially useful for tests. |
||
1673 | * |
||
1674 | * @since 4.3.0 |
||
1675 | * |
||
1676 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
1677 | */ |
||
1678 | return (bool) apply_filters( |
||
1679 | 'jetpack_development_version', |
||
1680 | ! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
1681 | ); |
||
1682 | } |
||
1683 | |||
1684 | /** |
||
1685 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1686 | */ |
||
1687 | public static function is_user_connected( $user_id = false ) { |
||
1688 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
1689 | if ( ! $user_id ) { |
||
1690 | return false; |
||
1691 | } |
||
1692 | |||
1693 | return (bool) Jetpack_Data::get_access_token( $user_id ); |
||
1694 | } |
||
1695 | |||
1696 | /** |
||
1697 | * Get the wpcom user data of the current|specified connected user. |
||
1698 | */ |
||
1699 | public static function get_connected_user_data( $user_id = null ) { |
||
1700 | if ( ! $user_id ) { |
||
1701 | $user_id = get_current_user_id(); |
||
1702 | } |
||
1703 | |||
1704 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
1705 | |||
1706 | if ( $cached_user_data = get_transient( $transient_key ) ) { |
||
1707 | return $cached_user_data; |
||
1708 | } |
||
1709 | |||
1710 | Jetpack::load_xml_rpc_client(); |
||
1711 | $xml = new Jetpack_IXR_Client( array( |
||
1712 | 'user_id' => $user_id, |
||
1713 | ) ); |
||
1714 | $xml->query( 'wpcom.getUser' ); |
||
1715 | if ( ! $xml->isError() ) { |
||
1716 | $user_data = $xml->getResponse(); |
||
1717 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
1718 | return $user_data; |
||
1719 | } |
||
1720 | |||
1721 | return false; |
||
1722 | } |
||
1723 | |||
1724 | /** |
||
1725 | * Get the wpcom email of the current|specified connected user. |
||
1726 | */ |
||
1727 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
1728 | if ( ! $user_id ) { |
||
1729 | $user_id = get_current_user_id(); |
||
1730 | } |
||
1731 | Jetpack::load_xml_rpc_client(); |
||
1732 | $xml = new Jetpack_IXR_Client( array( |
||
1733 | 'user_id' => $user_id, |
||
1734 | ) ); |
||
1735 | $xml->query( 'wpcom.getUserEmail' ); |
||
1736 | if ( ! $xml->isError() ) { |
||
1737 | return $xml->getResponse(); |
||
1738 | } |
||
1739 | return false; |
||
1740 | } |
||
1741 | |||
1742 | /** |
||
1743 | * Get the wpcom email of the master user. |
||
1744 | */ |
||
1745 | public static function get_master_user_email() { |
||
1746 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
1747 | if ( $master_user_id ) { |
||
1748 | return self::get_connected_user_email( $master_user_id ); |
||
1749 | } |
||
1750 | return ''; |
||
1751 | } |
||
1752 | |||
1753 | function current_user_is_connection_owner() { |
||
1754 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1755 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; |
||
1756 | } |
||
1757 | |||
1758 | /** |
||
1759 | * Gets current user IP address. |
||
1760 | * |
||
1761 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
1762 | * |
||
1763 | * @return string Current user IP address. |
||
1764 | */ |
||
1765 | public static function current_user_ip( $check_all_headers = false ) { |
||
1766 | if ( $check_all_headers ) { |
||
1767 | foreach ( array( |
||
1768 | 'HTTP_CF_CONNECTING_IP', |
||
1769 | 'HTTP_CLIENT_IP', |
||
1770 | 'HTTP_X_FORWARDED_FOR', |
||
1771 | 'HTTP_X_FORWARDED', |
||
1772 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
1773 | 'HTTP_FORWARDED_FOR', |
||
1774 | 'HTTP_FORWARDED', |
||
1775 | 'HTTP_VIA', |
||
1776 | ) as $key ) { |
||
1777 | if ( ! empty( $_SERVER[ $key ] ) ) { |
||
1778 | return $_SERVER[ $key ]; |
||
1779 | } |
||
1780 | } |
||
1781 | } |
||
1782 | |||
1783 | return ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
||
1784 | } |
||
1785 | |||
1786 | /** |
||
1787 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
1788 | */ |
||
1789 | function extra_oembed_providers() { |
||
1790 | // Cloudup: https://dev.cloudup.com/#oembed |
||
1791 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
1792 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
1793 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
1794 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
1795 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
1796 | wp_oembed_add_provider( '#https?://(www\.)?icloud\.com/keynote/.*#i', 'https://iwmb.icloud.com/iwmb/oembed', true ); |
||
1797 | } |
||
1798 | |||
1799 | /** |
||
1800 | * Synchronize connected user role changes |
||
1801 | */ |
||
1802 | function user_role_change( $user_id ) { |
||
1803 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()' ); |
||
1804 | Jetpack_Sync_Users::user_role_change( $user_id ); |
||
1805 | } |
||
1806 | |||
1807 | /** |
||
1808 | * Loads the currently active modules. |
||
1809 | */ |
||
1810 | public static function load_modules() { |
||
1811 | if ( |
||
1812 | ! self::is_active() |
||
1813 | && ! self::is_development_mode() |
||
1814 | && ! self::is_onboarding() |
||
1815 | && ( |
||
1816 | ! is_multisite() |
||
1817 | || ! get_site_option( 'jetpack_protect_active' ) |
||
1818 | ) |
||
1819 | ) { |
||
1820 | return; |
||
1821 | } |
||
1822 | |||
1823 | $version = Jetpack_Options::get_option( 'version' ); |
||
1824 | View Code Duplication | if ( ! $version ) { |
|
1825 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
1826 | /** This action is documented in class.jetpack.php */ |
||
1827 | do_action( 'updating_jetpack_version', $version, false ); |
||
1828 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
1829 | } |
||
1830 | list( $version ) = explode( ':', $version ); |
||
1831 | |||
1832 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
1833 | |||
1834 | $modules_data = array(); |
||
1835 | |||
1836 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
1837 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
1838 | $updated_modules = array(); |
||
1839 | foreach ( $modules as $module ) { |
||
1840 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1841 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
1842 | continue; |
||
1843 | } |
||
1844 | |||
1845 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
1846 | continue; |
||
1847 | } |
||
1848 | |||
1849 | $updated_modules[] = $module; |
||
1850 | } |
||
1851 | |||
1852 | $modules = array_diff( $modules, $updated_modules ); |
||
1853 | } |
||
1854 | |||
1855 | $is_development_mode = Jetpack::is_development_mode(); |
||
1856 | |||
1857 | foreach ( $modules as $index => $module ) { |
||
1858 | // If we're in dev mode, disable modules requiring a connection |
||
1859 | if ( $is_development_mode ) { |
||
1860 | // Prime the pump if we need to |
||
1861 | if ( empty( $modules_data[ $module ] ) ) { |
||
1862 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1863 | } |
||
1864 | // If the module requires a connection, but we're in local mode, don't include it. |
||
1865 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
1866 | continue; |
||
1867 | } |
||
1868 | } |
||
1869 | |||
1870 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
1871 | continue; |
||
1872 | } |
||
1873 | |||
1874 | if ( ! include_once( Jetpack::get_module_path( $module ) ) ) { |
||
1875 | unset( $modules[ $index ] ); |
||
1876 | self::update_active_modules( array_values( $modules ) ); |
||
1877 | continue; |
||
1878 | } |
||
1879 | |||
1880 | /** |
||
1881 | * Fires when a specific module is loaded. |
||
1882 | * The dynamic part of the hook, $module, is the module slug. |
||
1883 | * |
||
1884 | * @since 1.1.0 |
||
1885 | */ |
||
1886 | do_action( 'jetpack_module_loaded_' . $module ); |
||
1887 | } |
||
1888 | |||
1889 | /** |
||
1890 | * Fires when all the modules are loaded. |
||
1891 | * |
||
1892 | * @since 1.1.0 |
||
1893 | */ |
||
1894 | do_action( 'jetpack_modules_loaded' ); |
||
1895 | |||
1896 | // 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. |
||
1897 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
1898 | } |
||
1899 | |||
1900 | /** |
||
1901 | * Check if Jetpack's REST API compat file should be included |
||
1902 | * @action plugins_loaded |
||
1903 | * @return null |
||
1904 | */ |
||
1905 | public function check_rest_api_compat() { |
||
1906 | /** |
||
1907 | * Filters the list of REST API compat files to be included. |
||
1908 | * |
||
1909 | * @since 2.2.5 |
||
1910 | * |
||
1911 | * @param array $args Array of REST API compat files to include. |
||
1912 | */ |
||
1913 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
1914 | |||
1915 | if ( function_exists( 'bbpress' ) ) |
||
1916 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
1917 | |||
1918 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
1919 | require_once $_jetpack_rest_api_compat_include; |
||
1920 | } |
||
1921 | |||
1922 | /** |
||
1923 | * Gets all plugins currently active in values, regardless of whether they're |
||
1924 | * traditionally activated or network activated. |
||
1925 | * |
||
1926 | * @todo Store the result in core's object cache maybe? |
||
1927 | */ |
||
1928 | public static function get_active_plugins() { |
||
1929 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
1930 | |||
1931 | if ( is_multisite() ) { |
||
1932 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
1933 | // whereas active_plugins stores them in the values. |
||
1934 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
1935 | if ( $network_plugins ) { |
||
1936 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
1937 | } |
||
1938 | } |
||
1939 | |||
1940 | sort( $active_plugins ); |
||
1941 | |||
1942 | return array_unique( $active_plugins ); |
||
1943 | } |
||
1944 | |||
1945 | /** |
||
1946 | * Gets and parses additional plugin data to send with the heartbeat data |
||
1947 | * |
||
1948 | * @since 3.8.1 |
||
1949 | * |
||
1950 | * @return array Array of plugin data |
||
1951 | */ |
||
1952 | public static function get_parsed_plugin_data() { |
||
1953 | if ( ! function_exists( 'get_plugins' ) ) { |
||
1954 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
1955 | } |
||
1956 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
1957 | $all_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
1958 | $active_plugins = Jetpack::get_active_plugins(); |
||
1959 | |||
1960 | $plugins = array(); |
||
1961 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
1962 | $plugins[ $path ] = array( |
||
1963 | 'is_active' => in_array( $path, $active_plugins ), |
||
1964 | 'file' => $path, |
||
1965 | 'name' => $plugin_data['Name'], |
||
1966 | 'version' => $plugin_data['Version'], |
||
1967 | 'author' => $plugin_data['Author'], |
||
1968 | ); |
||
1969 | } |
||
1970 | |||
1971 | return $plugins; |
||
1972 | } |
||
1973 | |||
1974 | /** |
||
1975 | * Gets and parses theme data to send with the heartbeat data |
||
1976 | * |
||
1977 | * @since 3.8.1 |
||
1978 | * |
||
1979 | * @return array Array of theme data |
||
1980 | */ |
||
1981 | public static function get_parsed_theme_data() { |
||
1982 | $all_themes = wp_get_themes( array( 'allowed' => true ) ); |
||
1983 | $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); |
||
1984 | |||
1985 | $themes = array(); |
||
1986 | foreach ( $all_themes as $slug => $theme_data ) { |
||
1987 | $theme_headers = array(); |
||
1988 | foreach ( $header_keys as $header_key ) { |
||
1989 | $theme_headers[ $header_key ] = $theme_data->get( $header_key ); |
||
1990 | } |
||
1991 | |||
1992 | $themes[ $slug ] = array( |
||
1993 | 'is_active_theme' => $slug == wp_get_theme()->get_template(), |
||
1994 | 'slug' => $slug, |
||
1995 | 'theme_root' => $theme_data->get_theme_root_uri(), |
||
1996 | 'parent' => $theme_data->parent(), |
||
1997 | 'headers' => $theme_headers |
||
1998 | ); |
||
1999 | } |
||
2000 | |||
2001 | return $themes; |
||
2002 | } |
||
2003 | |||
2004 | /** |
||
2005 | * Checks whether a specific plugin is active. |
||
2006 | * |
||
2007 | * We don't want to store these in a static variable, in case |
||
2008 | * there are switch_to_blog() calls involved. |
||
2009 | */ |
||
2010 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
2011 | return in_array( $plugin, self::get_active_plugins() ); |
||
2012 | } |
||
2013 | |||
2014 | /** |
||
2015 | * Check if Jetpack's Open Graph tags should be used. |
||
2016 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
2017 | * |
||
2018 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2019 | * @action plugins_loaded |
||
2020 | * @return null |
||
2021 | */ |
||
2022 | public function check_open_graph() { |
||
2023 | if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { |
||
2024 | add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); |
||
2025 | } |
||
2026 | |||
2027 | $active_plugins = self::get_active_plugins(); |
||
2028 | |||
2029 | if ( ! empty( $active_plugins ) ) { |
||
2030 | foreach ( $this->open_graph_conflicting_plugins as $plugin ) { |
||
2031 | if ( in_array( $plugin, $active_plugins ) ) { |
||
2032 | add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); |
||
2033 | break; |
||
2034 | } |
||
2035 | } |
||
2036 | } |
||
2037 | |||
2038 | /** |
||
2039 | * Allow the addition of Open Graph Meta Tags to all pages. |
||
2040 | * |
||
2041 | * @since 2.0.3 |
||
2042 | * |
||
2043 | * @param bool false Should Open Graph Meta tags be added. Default to false. |
||
2044 | */ |
||
2045 | if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { |
||
2046 | require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; |
||
2047 | } |
||
2048 | } |
||
2049 | |||
2050 | /** |
||
2051 | * Check if Jetpack's Twitter tags should be used. |
||
2052 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
2053 | * |
||
2054 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2055 | * @action plugins_loaded |
||
2056 | * @return null |
||
2057 | */ |
||
2058 | public function check_twitter_tags() { |
||
2059 | |||
2060 | $active_plugins = self::get_active_plugins(); |
||
2061 | |||
2062 | if ( ! empty( $active_plugins ) ) { |
||
2063 | foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { |
||
2064 | if ( in_array( $plugin, $active_plugins ) ) { |
||
2065 | add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); |
||
2066 | break; |
||
2067 | } |
||
2068 | } |
||
2069 | } |
||
2070 | |||
2071 | /** |
||
2072 | * Allow Twitter Card Meta tags to be disabled. |
||
2073 | * |
||
2074 | * @since 2.6.0 |
||
2075 | * |
||
2076 | * @param bool true Should Twitter Card Meta tags be disabled. Default to true. |
||
2077 | */ |
||
2078 | if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) { |
||
2079 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; |
||
2080 | } |
||
2081 | } |
||
2082 | |||
2083 | /** |
||
2084 | * Allows plugins to submit security reports. |
||
2085 | * |
||
2086 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
2087 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
2088 | * @param array $args See definitions above |
||
2089 | */ |
||
2090 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
2091 | _deprecated_function( __FUNCTION__, 'jetpack-4.2', null ); |
||
2092 | } |
||
2093 | |||
2094 | /* Jetpack Options API */ |
||
2095 | |||
2096 | public static function get_option_names( $type = 'compact' ) { |
||
2097 | return Jetpack_Options::get_option_names( $type ); |
||
2098 | } |
||
2099 | |||
2100 | /** |
||
2101 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
2102 | * |
||
2103 | * @param string $name Option name |
||
2104 | * @param mixed $default (optional) |
||
2105 | */ |
||
2106 | public static function get_option( $name, $default = false ) { |
||
2107 | return Jetpack_Options::get_option( $name, $default ); |
||
2108 | } |
||
2109 | |||
2110 | /** |
||
2111 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
2112 | * |
||
2113 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
2114 | * @param string $name Option name |
||
2115 | * @param mixed $value Option value |
||
2116 | */ |
||
2117 | public static function update_option( $name, $value ) { |
||
2118 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' ); |
||
2119 | return Jetpack_Options::update_option( $name, $value ); |
||
2120 | } |
||
2121 | |||
2122 | /** |
||
2123 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
2124 | * |
||
2125 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
2126 | * @param array $array array( option name => option value, ... ) |
||
2127 | */ |
||
2128 | public static function update_options( $array ) { |
||
2129 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' ); |
||
2130 | return Jetpack_Options::update_options( $array ); |
||
2131 | } |
||
2132 | |||
2133 | /** |
||
2134 | * Deletes the given option. May be passed multiple option names as an array. |
||
2135 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
2136 | * |
||
2137 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
2138 | * @param string|array $names |
||
2139 | */ |
||
2140 | public static function delete_option( $names ) { |
||
2141 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' ); |
||
2142 | return Jetpack_Options::delete_option( $names ); |
||
2143 | } |
||
2144 | |||
2145 | /** |
||
2146 | * Enters a user token into the user_tokens option |
||
2147 | * |
||
2148 | * @param int $user_id |
||
2149 | * @param string $token |
||
2150 | * return bool |
||
2151 | */ |
||
2152 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
2153 | // not designed for concurrent updates |
||
2154 | $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); |
||
2155 | if ( ! is_array( $user_tokens ) ) |
||
2156 | $user_tokens = array(); |
||
2157 | $user_tokens[$user_id] = $token; |
||
2158 | if ( $is_master_user ) { |
||
2159 | $master_user = $user_id; |
||
2160 | $options = compact( 'user_tokens', 'master_user' ); |
||
2161 | } else { |
||
2162 | $options = compact( 'user_tokens' ); |
||
2163 | } |
||
2164 | return Jetpack_Options::update_options( $options ); |
||
2165 | } |
||
2166 | |||
2167 | /** |
||
2168 | * Returns an array of all PHP files in the specified absolute path. |
||
2169 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
2170 | * |
||
2171 | * @param string $absolute_path The absolute path of the directory to search. |
||
2172 | * @return array Array of absolute paths to the PHP files. |
||
2173 | */ |
||
2174 | public static function glob_php( $absolute_path ) { |
||
2175 | if ( function_exists( 'glob' ) ) { |
||
2176 | return glob( "$absolute_path/*.php" ); |
||
2177 | } |
||
2178 | |||
2179 | $absolute_path = untrailingslashit( $absolute_path ); |
||
2180 | $files = array(); |
||
2181 | if ( ! $dir = @opendir( $absolute_path ) ) { |
||
2182 | return $files; |
||
2183 | } |
||
2184 | |||
2185 | while ( false !== $file = readdir( $dir ) ) { |
||
2186 | if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) { |
||
2187 | continue; |
||
2188 | } |
||
2189 | |||
2190 | $file = "$absolute_path/$file"; |
||
2191 | |||
2192 | if ( ! is_file( $file ) ) { |
||
2193 | continue; |
||
2194 | } |
||
2195 | |||
2196 | $files[] = $file; |
||
2197 | } |
||
2198 | |||
2199 | closedir( $dir ); |
||
2200 | |||
2201 | return $files; |
||
2202 | } |
||
2203 | |||
2204 | public static function activate_new_modules( $redirect = false ) { |
||
2205 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
2206 | return; |
||
2207 | } |
||
2208 | |||
2209 | $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic] |
||
2210 | View Code Duplication | if ( ! $jetpack_old_version ) { |
|
2211 | $jetpack_old_version = $version = $old_version = '1.1:' . time(); |
||
2212 | /** This action is documented in class.jetpack.php */ |
||
2213 | do_action( 'updating_jetpack_version', $version, false ); |
||
2214 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
2215 | } |
||
2216 | |||
2217 | list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic] |
||
2218 | |||
2219 | if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) { |
||
2220 | return; |
||
2221 | } |
||
2222 | |||
2223 | $active_modules = Jetpack::get_active_modules(); |
||
2224 | $reactivate_modules = array(); |
||
2225 | foreach ( $active_modules as $active_module ) { |
||
2226 | $module = Jetpack::get_module( $active_module ); |
||
2227 | if ( ! isset( $module['changed'] ) ) { |
||
2228 | continue; |
||
2229 | } |
||
2230 | |||
2231 | if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) { |
||
2232 | continue; |
||
2233 | } |
||
2234 | |||
2235 | $reactivate_modules[] = $active_module; |
||
2236 | Jetpack::deactivate_module( $active_module ); |
||
2237 | } |
||
2238 | |||
2239 | $new_version = JETPACK__VERSION . ':' . time(); |
||
2240 | /** This action is documented in class.jetpack.php */ |
||
2241 | do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version ); |
||
2242 | Jetpack_Options::update_options( |
||
2243 | array( |
||
2244 | 'version' => $new_version, |
||
2245 | 'old_version' => $jetpack_old_version, |
||
2246 | ) |
||
2247 | ); |
||
2248 | |||
2249 | Jetpack::state( 'message', 'modules_activated' ); |
||
2250 | Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules ); |
||
2251 | |||
2252 | if ( $redirect ) { |
||
2253 | $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page |
||
2254 | if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) { |
||
2255 | $page = $_GET['page']; |
||
2256 | } |
||
2257 | |||
2258 | wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) ); |
||
2259 | exit; |
||
2260 | } |
||
2261 | } |
||
2262 | |||
2263 | /** |
||
2264 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2265 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2266 | */ |
||
2267 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
2268 | static $modules = null; |
||
2269 | |||
2270 | if ( ! isset( $modules ) ) { |
||
2271 | $available_modules_option = Jetpack_Options::get_option( 'available_modules', array() ); |
||
2272 | // Use the cache if we're on the front-end and it's available... |
||
2273 | if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) { |
||
2274 | $modules = $available_modules_option[ JETPACK__VERSION ]; |
||
2275 | } else { |
||
2276 | $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' ); |
||
2277 | |||
2278 | $modules = array(); |
||
2279 | |||
2280 | foreach ( $files as $file ) { |
||
2281 | if ( ! $headers = Jetpack::get_module( $file ) ) { |
||
2282 | continue; |
||
2283 | } |
||
2284 | |||
2285 | $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced']; |
||
2286 | } |
||
2287 | |||
2288 | Jetpack_Options::update_option( 'available_modules', array( |
||
2289 | JETPACK__VERSION => $modules, |
||
2290 | ) ); |
||
2291 | } |
||
2292 | } |
||
2293 | |||
2294 | /** |
||
2295 | * Filters the array of modules available to be activated. |
||
2296 | * |
||
2297 | * @since 2.4.0 |
||
2298 | * |
||
2299 | * @param array $modules Array of available modules. |
||
2300 | * @param string $min_version Minimum version number required to use modules. |
||
2301 | * @param string $max_version Maximum version number required to use modules. |
||
2302 | */ |
||
2303 | $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version ); |
||
2304 | |||
2305 | if ( ! $min_version && ! $max_version ) { |
||
2306 | return array_keys( $mods ); |
||
2307 | } |
||
2308 | |||
2309 | $r = array(); |
||
2310 | foreach ( $mods as $slug => $introduced ) { |
||
2311 | if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) { |
||
2312 | continue; |
||
2313 | } |
||
2314 | |||
2315 | if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) { |
||
2316 | continue; |
||
2317 | } |
||
2318 | |||
2319 | $r[] = $slug; |
||
2320 | } |
||
2321 | |||
2322 | return $r; |
||
2323 | } |
||
2324 | |||
2325 | /** |
||
2326 | * Default modules loaded on activation. |
||
2327 | */ |
||
2328 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
2329 | $return = array(); |
||
2330 | |||
2331 | foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) { |
||
2332 | $module_data = Jetpack::get_module( $module ); |
||
2333 | |||
2334 | switch ( strtolower( $module_data['auto_activate'] ) ) { |
||
2335 | case 'yes' : |
||
2336 | $return[] = $module; |
||
2337 | break; |
||
2338 | case 'public' : |
||
2339 | if ( Jetpack_Options::get_option( 'public' ) ) { |
||
2340 | $return[] = $module; |
||
2341 | } |
||
2342 | break; |
||
2343 | case 'no' : |
||
2344 | default : |
||
2345 | break; |
||
2346 | } |
||
2347 | } |
||
2348 | /** |
||
2349 | * Filters the array of default modules. |
||
2350 | * |
||
2351 | * @since 2.5.0 |
||
2352 | * |
||
2353 | * @param array $return Array of default modules. |
||
2354 | * @param string $min_version Minimum version number required to use modules. |
||
2355 | * @param string $max_version Maximum version number required to use modules. |
||
2356 | */ |
||
2357 | return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version ); |
||
2358 | } |
||
2359 | |||
2360 | /** |
||
2361 | * Checks activated modules during auto-activation to determine |
||
2362 | * if any of those modules are being deprecated. If so, close |
||
2363 | * them out, and add any replacement modules. |
||
2364 | * |
||
2365 | * Runs at priority 99 by default. |
||
2366 | * |
||
2367 | * This is run late, so that it can still activate a module if |
||
2368 | * the new module is a replacement for another that the user |
||
2369 | * currently has active, even if something at the normal priority |
||
2370 | * would kibosh everything. |
||
2371 | * |
||
2372 | * @since 2.6 |
||
2373 | * @uses jetpack_get_default_modules filter |
||
2374 | * @param array $modules |
||
2375 | * @return array |
||
2376 | */ |
||
2377 | function handle_deprecated_modules( $modules ) { |
||
2378 | $deprecated_modules = array( |
||
2379 | 'debug' => null, // Closed out and moved to the debugger library. |
||
2380 | 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality. |
||
2381 | 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support. |
||
2382 | ); |
||
2383 | |||
2384 | // Don't activate SSO if they never completed activating WPCC. |
||
2385 | if ( Jetpack::is_module_active( 'wpcc' ) ) { |
||
2386 | $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' ); |
||
2387 | if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) { |
||
2388 | $deprecated_modules['wpcc'] = null; |
||
2389 | } |
||
2390 | } |
||
2391 | |||
2392 | foreach ( $deprecated_modules as $module => $replacement ) { |
||
2393 | if ( Jetpack::is_module_active( $module ) ) { |
||
2394 | self::deactivate_module( $module ); |
||
2395 | if ( $replacement ) { |
||
2396 | $modules[] = $replacement; |
||
2397 | } |
||
2398 | } |
||
2399 | } |
||
2400 | |||
2401 | return array_unique( $modules ); |
||
2402 | } |
||
2403 | |||
2404 | /** |
||
2405 | * Checks activated plugins during auto-activation to determine |
||
2406 | * if any of those plugins are in the list with a corresponding module |
||
2407 | * that is not compatible with the plugin. The module will not be allowed |
||
2408 | * to auto-activate. |
||
2409 | * |
||
2410 | * @since 2.6 |
||
2411 | * @uses jetpack_get_default_modules filter |
||
2412 | * @param array $modules |
||
2413 | * @return array |
||
2414 | */ |
||
2415 | function filter_default_modules( $modules ) { |
||
2416 | |||
2417 | $active_plugins = self::get_active_plugins(); |
||
2418 | |||
2419 | if ( ! empty( $active_plugins ) ) { |
||
2420 | |||
2421 | // For each module we'd like to auto-activate... |
||
2422 | foreach ( $modules as $key => $module ) { |
||
2423 | // If there are potential conflicts for it... |
||
2424 | if ( ! empty( $this->conflicting_plugins[ $module ] ) ) { |
||
2425 | // For each potential conflict... |
||
2426 | foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) { |
||
2427 | // If that conflicting plugin is active... |
||
2428 | if ( in_array( $plugin, $active_plugins ) ) { |
||
2429 | // Remove that item from being auto-activated. |
||
2430 | unset( $modules[ $key ] ); |
||
2431 | } |
||
2432 | } |
||
2433 | } |
||
2434 | } |
||
2435 | } |
||
2436 | |||
2437 | return $modules; |
||
2438 | } |
||
2439 | |||
2440 | /** |
||
2441 | * Extract a module's slug from its full path. |
||
2442 | */ |
||
2443 | public static function get_module_slug( $file ) { |
||
2444 | return str_replace( '.php', '', basename( $file ) ); |
||
2445 | } |
||
2446 | |||
2447 | /** |
||
2448 | * Generate a module's path from its slug. |
||
2449 | */ |
||
2450 | public static function get_module_path( $slug ) { |
||
2451 | return JETPACK__PLUGIN_DIR . "modules/$slug.php"; |
||
2452 | } |
||
2453 | |||
2454 | /** |
||
2455 | * Load module data from module file. Headers differ from WordPress |
||
2456 | * plugin headers to avoid them being identified as standalone |
||
2457 | * plugins on the WordPress plugins page. |
||
2458 | */ |
||
2459 | public static function get_module( $module ) { |
||
2460 | $headers = array( |
||
2461 | 'name' => 'Module Name', |
||
2462 | 'description' => 'Module Description', |
||
2463 | 'jumpstart_desc' => 'Jumpstart Description', |
||
2464 | 'sort' => 'Sort Order', |
||
2465 | 'recommendation_order' => 'Recommendation Order', |
||
2466 | 'introduced' => 'First Introduced', |
||
2467 | 'changed' => 'Major Changes In', |
||
2468 | 'deactivate' => 'Deactivate', |
||
2469 | 'free' => 'Free', |
||
2470 | 'requires_connection' => 'Requires Connection', |
||
2471 | 'auto_activate' => 'Auto Activate', |
||
2472 | 'module_tags' => 'Module Tags', |
||
2473 | 'feature' => 'Feature', |
||
2474 | 'additional_search_queries' => 'Additional Search Queries', |
||
2475 | 'plan_classes' => 'Plans', |
||
2476 | ); |
||
2477 | |||
2478 | $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) ); |
||
2479 | |||
2480 | $mod = Jetpack::get_file_data( $file, $headers ); |
||
2481 | if ( empty( $mod['name'] ) ) { |
||
2482 | return false; |
||
2483 | } |
||
2484 | |||
2485 | $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; |
||
2486 | $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; |
||
2487 | $mod['deactivate'] = empty( $mod['deactivate'] ); |
||
2488 | $mod['free'] = empty( $mod['free'] ); |
||
2489 | $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true; |
||
2490 | |||
2491 | if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) { |
||
2492 | $mod['auto_activate'] = 'No'; |
||
2493 | } else { |
||
2494 | $mod['auto_activate'] = (string) $mod['auto_activate']; |
||
2495 | } |
||
2496 | |||
2497 | if ( $mod['module_tags'] ) { |
||
2498 | $mod['module_tags'] = explode( ',', $mod['module_tags'] ); |
||
2499 | $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] ); |
||
2500 | $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] ); |
||
2501 | } else { |
||
2502 | $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) ); |
||
2503 | } |
||
2504 | |||
2505 | View Code Duplication | if ( $mod['plan_classes'] ) { |
|
2506 | $mod['plan_classes'] = explode( ',', $mod['plan_classes'] ); |
||
2507 | $mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) ); |
||
2508 | } else { |
||
2509 | $mod['plan_classes'] = array( 'free' ); |
||
2510 | } |
||
2511 | |||
2512 | View Code Duplication | if ( $mod['feature'] ) { |
|
2513 | $mod['feature'] = explode( ',', $mod['feature'] ); |
||
2514 | $mod['feature'] = array_map( 'trim', $mod['feature'] ); |
||
2515 | } else { |
||
2516 | $mod['feature'] = array( self::translate_module_tag( 'Other' ) ); |
||
2517 | } |
||
2518 | |||
2519 | /** |
||
2520 | * Filters the feature array on a module. |
||
2521 | * |
||
2522 | * This filter allows you to control where each module is filtered: Recommended, |
||
2523 | * Jumpstart, and the default "Other" listing. |
||
2524 | * |
||
2525 | * @since 3.5.0 |
||
2526 | * |
||
2527 | * @param array $mod['feature'] The areas to feature this module: |
||
2528 | * 'Jumpstart' adds to the "Jumpstart" option to activate many modules at once. |
||
2529 | * 'Recommended' shows on the main Jetpack admin screen. |
||
2530 | * 'Other' should be the default if no other value is in the array. |
||
2531 | * @param string $module The slug of the module, e.g. sharedaddy. |
||
2532 | * @param array $mod All the currently assembled module data. |
||
2533 | */ |
||
2534 | $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod ); |
||
2535 | |||
2536 | /** |
||
2537 | * Filter the returned data about a module. |
||
2538 | * |
||
2539 | * This filter allows overriding any info about Jetpack modules. It is dangerous, |
||
2540 | * so please be careful. |
||
2541 | * |
||
2542 | * @since 3.6.0 |
||
2543 | * |
||
2544 | * @param array $mod The details of the requested module. |
||
2545 | * @param string $module The slug of the module, e.g. sharedaddy |
||
2546 | * @param string $file The path to the module source file. |
||
2547 | */ |
||
2548 | return apply_filters( 'jetpack_get_module', $mod, $module, $file ); |
||
2549 | } |
||
2550 | |||
2551 | /** |
||
2552 | * Like core's get_file_data implementation, but caches the result. |
||
2553 | */ |
||
2554 | public static function get_file_data( $file, $headers ) { |
||
2555 | //Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated |
||
2556 | $file_name = basename( $file ); |
||
2557 | |||
2558 | $cache_key = 'jetpack_file_data_' . JETPACK__VERSION; |
||
2559 | |||
2560 | $file_data_option = get_transient( $cache_key ); |
||
2561 | |||
2562 | if ( false === $file_data_option ) { |
||
2563 | $file_data_option = array(); |
||
2564 | } |
||
2565 | |||
2566 | $key = md5( $file_name . serialize( $headers ) ); |
||
2567 | $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 ); |
||
2568 | |||
2569 | // If we don't need to refresh the cache, and already have the value, short-circuit! |
||
2570 | if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) { |
||
2571 | return $file_data_option[ $key ]; |
||
2572 | } |
||
2573 | |||
2574 | $data = get_file_data( $file, $headers ); |
||
2575 | |||
2576 | $file_data_option[ $key ] = $data; |
||
2577 | |||
2578 | set_transient( $cache_key, $file_data_option, 29 * DAY_IN_SECONDS ); |
||
2579 | |||
2580 | return $data; |
||
2581 | } |
||
2582 | |||
2583 | |||
2584 | /** |
||
2585 | * Return translated module tag. |
||
2586 | * |
||
2587 | * @param string $tag Tag as it appears in each module heading. |
||
2588 | * |
||
2589 | * @return mixed |
||
2590 | */ |
||
2591 | public static function translate_module_tag( $tag ) { |
||
2592 | return jetpack_get_module_i18n_tag( $tag ); |
||
2593 | } |
||
2594 | |||
2595 | /** |
||
2596 | * Get i18n strings as a JSON-encoded string |
||
2597 | * |
||
2598 | * @return string The locale as JSON |
||
2599 | */ |
||
2600 | public static function get_i18n_data_json() { |
||
2601 | |||
2602 | // WordPress 5.0 uses md5 hashes of file paths to associate translation |
||
2603 | // JSON files with the file they should be included for. This is an md5 |
||
2604 | // of '_inc/build/admin.js'. |
||
2605 | $path_md5 = '1bac79e646a8bf4081a5011ab72d5807'; |
||
2606 | |||
2607 | $i18n_json = |
||
2608 | JETPACK__PLUGIN_DIR |
||
2609 | . 'languages/json/jetpack-' |
||
2610 | . get_user_locale() |
||
2611 | . '-' |
||
2612 | . $path_md5 |
||
2613 | . '.json'; |
||
2614 | |||
2615 | if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
||
2616 | $locale_data = @file_get_contents( $i18n_json ); |
||
2617 | if ( $locale_data ) { |
||
2618 | return $locale_data; |
||
2619 | } |
||
2620 | } |
||
2621 | |||
2622 | // Return valid empty Jed locale |
||
2623 | return '{ "locale_data": { "messages": { "": {} } } }'; |
||
2624 | } |
||
2625 | |||
2626 | /** |
||
2627 | * Add locale data setup to wp-i18n |
||
2628 | * |
||
2629 | * Any Jetpack script that depends on wp-i18n should use this method to set up the locale. |
||
2630 | * |
||
2631 | * The locale setup depends on an adding inline script. This is error-prone and could easily |
||
2632 | * result in multiple additions of the same script when exactly 0 or 1 is desireable. |
||
2633 | * |
||
2634 | * This method provides a safe way to request the setup multiple times but add the script at |
||
2635 | * most once. |
||
2636 | * |
||
2637 | * @since 6.7.0 |
||
2638 | * |
||
2639 | * @return void |
||
2640 | */ |
||
2641 | public static function setup_wp_i18n_locale_data() { |
||
2642 | static $script_added = false; |
||
2643 | if ( ! $script_added ) { |
||
2644 | $script_added = true; |
||
2645 | wp_add_inline_script( |
||
2646 | 'wp-i18n', |
||
2647 | 'wp.i18n.setLocaleData( ' . Jetpack::get_i18n_data_json() . ', \'jetpack\' );' |
||
2648 | ); |
||
2649 | } |
||
2650 | } |
||
2651 | |||
2652 | /** |
||
2653 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2654 | * |
||
2655 | * @since 3.9.2 |
||
2656 | * |
||
2657 | * @param array $modules |
||
2658 | * |
||
2659 | * @return string|void |
||
2660 | */ |
||
2661 | public static function get_translated_modules( $modules ) { |
||
2662 | foreach ( $modules as $index => $module ) { |
||
2663 | $i18n_module = jetpack_get_module_i18n( $module['module'] ); |
||
2664 | if ( isset( $module['name'] ) ) { |
||
2665 | $modules[ $index ]['name'] = $i18n_module['name']; |
||
2666 | } |
||
2667 | if ( isset( $module['description'] ) ) { |
||
2668 | $modules[ $index ]['description'] = $i18n_module['description']; |
||
2669 | $modules[ $index ]['short_description'] = $i18n_module['description']; |
||
2670 | } |
||
2671 | } |
||
2672 | return $modules; |
||
2673 | } |
||
2674 | |||
2675 | /** |
||
2676 | * Get a list of activated modules as an array of module slugs. |
||
2677 | */ |
||
2678 | public static function get_active_modules() { |
||
2679 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
2680 | |||
2681 | if ( ! is_array( $active ) ) { |
||
2682 | $active = array(); |
||
2683 | } |
||
2684 | |||
2685 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
2686 | $active[] = 'vaultpress'; |
||
2687 | } else { |
||
2688 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
2689 | } |
||
2690 | |||
2691 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
2692 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
2693 | $active[] = 'protect'; |
||
2694 | } |
||
2695 | |||
2696 | /** |
||
2697 | * Allow filtering of the active modules. |
||
2698 | * |
||
2699 | * Gives theme and plugin developers the power to alter the modules that |
||
2700 | * are activated on the fly. |
||
2701 | * |
||
2702 | * @since 5.8.0 |
||
2703 | * |
||
2704 | * @param array $active Array of active module slugs. |
||
2705 | */ |
||
2706 | $active = apply_filters( 'jetpack_active_modules', $active ); |
||
2707 | |||
2708 | return array_unique( $active ); |
||
2709 | } |
||
2710 | |||
2711 | /** |
||
2712 | * Check whether or not a Jetpack module is active. |
||
2713 | * |
||
2714 | * @param string $module The slug of a Jetpack module. |
||
2715 | * @return bool |
||
2716 | * |
||
2717 | * @static |
||
2718 | */ |
||
2719 | public static function is_module_active( $module ) { |
||
2720 | return in_array( $module, self::get_active_modules() ); |
||
2721 | } |
||
2722 | |||
2723 | public static function is_module( $module ) { |
||
2724 | return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() ); |
||
2725 | } |
||
2726 | |||
2727 | /** |
||
2728 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2729 | * |
||
2730 | * @param bool $catch True to start catching, False to stop. |
||
2731 | * |
||
2732 | * @static |
||
2733 | */ |
||
2734 | public static function catch_errors( $catch ) { |
||
2735 | static $display_errors, $error_reporting; |
||
2736 | |||
2737 | if ( $catch ) { |
||
2738 | $display_errors = @ini_set( 'display_errors', 1 ); |
||
2739 | $error_reporting = @error_reporting( E_ALL ); |
||
2740 | add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
2741 | } else { |
||
2742 | @ini_set( 'display_errors', $display_errors ); |
||
2743 | @error_reporting( $error_reporting ); |
||
2744 | remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
2745 | } |
||
2746 | } |
||
2747 | |||
2748 | /** |
||
2749 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2750 | */ |
||
2751 | public static function catch_errors_on_shutdown() { |
||
2752 | Jetpack::state( 'php_errors', self::alias_directories( ob_get_clean() ) ); |
||
2753 | } |
||
2754 | |||
2755 | /** |
||
2756 | * Rewrite any string to make paths easier to read. |
||
2757 | * |
||
2758 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2759 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2760 | * |
||
2761 | * @param $string |
||
2762 | * @return mixed |
||
2763 | */ |
||
2764 | public static function alias_directories( $string ) { |
||
2765 | // ABSPATH has a trailing slash. |
||
2766 | $string = str_replace( ABSPATH, 'ABSPATH/', $string ); |
||
2767 | // WP_CONTENT_DIR does not have a trailing slash. |
||
2768 | $string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string ); |
||
2769 | |||
2770 | return $string; |
||
2771 | } |
||
2772 | |||
2773 | public static function activate_default_modules( |
||
2774 | $min_version = false, |
||
2775 | $max_version = false, |
||
2776 | $other_modules = array(), |
||
2777 | $redirect = true, |
||
2778 | $send_state_messages = true |
||
2779 | ) { |
||
2780 | $jetpack = Jetpack::init(); |
||
2781 | |||
2782 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
2783 | $modules = array_merge( $other_modules, $modules ); |
||
2784 | |||
2785 | // Look for standalone plugins and disable if active. |
||
2786 | |||
2787 | $to_deactivate = array(); |
||
2788 | foreach ( $modules as $module ) { |
||
2789 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
2790 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
2791 | } |
||
2792 | } |
||
2793 | |||
2794 | $deactivated = array(); |
||
2795 | foreach ( $to_deactivate as $module => $deactivate_me ) { |
||
2796 | list( $probable_file, $probable_title ) = $deactivate_me; |
||
2797 | if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { |
||
2798 | $deactivated[] = $module; |
||
2799 | } |
||
2800 | } |
||
2801 | |||
2802 | if ( $deactivated && $redirect ) { |
||
2803 | Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) ); |
||
2804 | |||
2805 | $url = add_query_arg( |
||
2806 | array( |
||
2807 | 'action' => 'activate_default_modules', |
||
2808 | '_wpnonce' => wp_create_nonce( 'activate_default_modules' ), |
||
2809 | ), |
||
2810 | add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) |
||
2811 | ); |
||
2812 | wp_safe_redirect( $url ); |
||
2813 | exit; |
||
2814 | } |
||
2815 | |||
2816 | /** |
||
2817 | * Fires before default modules are activated. |
||
2818 | * |
||
2819 | * @since 1.9.0 |
||
2820 | * |
||
2821 | * @param string $min_version Minimum version number required to use modules. |
||
2822 | * @param string $max_version Maximum version number required to use modules. |
||
2823 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
2824 | */ |
||
2825 | do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
2826 | |||
2827 | // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating |
||
2828 | if ( $send_state_messages ) { |
||
2829 | Jetpack::restate(); |
||
2830 | Jetpack::catch_errors( true ); |
||
2831 | } |
||
2832 | |||
2833 | $active = Jetpack::get_active_modules(); |
||
2834 | |||
2835 | foreach ( $modules as $module ) { |
||
2836 | if ( did_action( "jetpack_module_loaded_$module" ) ) { |
||
2837 | $active[] = $module; |
||
2838 | self::update_active_modules( $active ); |
||
2839 | continue; |
||
2840 | } |
||
2841 | |||
2842 | if ( $send_state_messages && in_array( $module, $active ) ) { |
||
2843 | $module_info = Jetpack::get_module( $module ); |
||
2844 | View Code Duplication | if ( ! $module_info['deactivate'] ) { |
|
2845 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
2846 | if ( $active_state = Jetpack::state( $state ) ) { |
||
2847 | $active_state = explode( ',', $active_state ); |
||
2848 | } else { |
||
2849 | $active_state = array(); |
||
2850 | } |
||
2851 | $active_state[] = $module; |
||
2852 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
2853 | } |
||
2854 | continue; |
||
2855 | } |
||
2856 | |||
2857 | $file = Jetpack::get_module_path( $module ); |
||
2858 | if ( ! file_exists( $file ) ) { |
||
2859 | continue; |
||
2860 | } |
||
2861 | |||
2862 | // we'll override this later if the plugin can be included without fatal error |
||
2863 | if ( $redirect ) { |
||
2864 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
2865 | } |
||
2866 | |||
2867 | if ( $send_state_messages ) { |
||
2868 | Jetpack::state( 'error', 'module_activation_failed' ); |
||
2869 | Jetpack::state( 'module', $module ); |
||
2870 | } |
||
2871 | |||
2872 | ob_start(); |
||
2873 | require_once $file; |
||
2874 | |||
2875 | $active[] = $module; |
||
2876 | |||
2877 | View Code Duplication | if ( $send_state_messages ) { |
|
2878 | |||
2879 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
2880 | if ( $active_state = Jetpack::state( $state ) ) { |
||
2881 | $active_state = explode( ',', $active_state ); |
||
2882 | } else { |
||
2883 | $active_state = array(); |
||
2884 | } |
||
2885 | $active_state[] = $module; |
||
2886 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
2887 | } |
||
2888 | |||
2889 | Jetpack::update_active_modules( $active ); |
||
2890 | |||
2891 | ob_end_clean(); |
||
2892 | } |
||
2893 | |||
2894 | if ( $send_state_messages ) { |
||
2895 | Jetpack::state( 'error', false ); |
||
2896 | Jetpack::state( 'module', false ); |
||
2897 | } |
||
2898 | |||
2899 | Jetpack::catch_errors( false ); |
||
2900 | /** |
||
2901 | * Fires when default modules are activated. |
||
2902 | * |
||
2903 | * @since 1.9.0 |
||
2904 | * |
||
2905 | * @param string $min_version Minimum version number required to use modules. |
||
2906 | * @param string $max_version Maximum version number required to use modules. |
||
2907 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
2908 | */ |
||
2909 | do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
2910 | } |
||
2911 | |||
2912 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
2913 | /** |
||
2914 | * Fires before a module is activated. |
||
2915 | * |
||
2916 | * @since 2.6.0 |
||
2917 | * |
||
2918 | * @param string $module Module slug. |
||
2919 | * @param bool $exit Should we exit after the module has been activated. Default to true. |
||
2920 | * @param bool $redirect Should the user be redirected after module activation? Default to true. |
||
2921 | */ |
||
2922 | do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); |
||
2923 | |||
2924 | $jetpack = Jetpack::init(); |
||
2925 | |||
2926 | if ( ! strlen( $module ) ) |
||
2927 | return false; |
||
2928 | |||
2929 | if ( ! Jetpack::is_module( $module ) ) |
||
2930 | return false; |
||
2931 | |||
2932 | // If it's already active, then don't do it again |
||
2933 | $active = Jetpack::get_active_modules(); |
||
2934 | foreach ( $active as $act ) { |
||
2935 | if ( $act == $module ) |
||
2936 | return true; |
||
2937 | } |
||
2938 | |||
2939 | $module_data = Jetpack::get_module( $module ); |
||
2940 | |||
2941 | if ( ! Jetpack::is_active() ) { |
||
2942 | if ( ! Jetpack::is_development_mode() && ! Jetpack::is_onboarding() ) |
||
2943 | return false; |
||
2944 | |||
2945 | // If we're not connected but in development mode, make sure the module doesn't require a connection |
||
2946 | if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) |
||
2947 | return false; |
||
2948 | } |
||
2949 | |||
2950 | // Check and see if the old plugin is active |
||
2951 | if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { |
||
2952 | // Deactivate the old plugin |
||
2953 | if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { |
||
2954 | // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module |
||
2955 | // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. |
||
2956 | Jetpack::state( 'deactivated_plugins', $module ); |
||
2957 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
||
2958 | exit; |
||
2959 | } |
||
2960 | } |
||
2961 | |||
2962 | // Protect won't work with mis-configured IPs |
||
2963 | if ( 'protect' === $module ) { |
||
2964 | include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; |
||
2965 | if ( ! jetpack_protect_get_ip() ) { |
||
2966 | Jetpack::state( 'message', 'protect_misconfigured_ip' ); |
||
2967 | return false; |
||
2968 | } |
||
2969 | } |
||
2970 | |||
2971 | if ( ! Jetpack_Plan::supports( $module ) ) { |
||
2972 | return false; |
||
2973 | } |
||
2974 | |||
2975 | // Check the file for fatal errors, a la wp-admin/plugins.php::activate |
||
2976 | Jetpack::state( 'module', $module ); |
||
2977 | Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error |
||
2978 | |||
2979 | Jetpack::catch_errors( true ); |
||
2980 | ob_start(); |
||
2981 | require Jetpack::get_module_path( $module ); |
||
2982 | /** This action is documented in class.jetpack.php */ |
||
2983 | do_action( 'jetpack_activate_module', $module ); |
||
2984 | $active[] = $module; |
||
2985 | Jetpack::update_active_modules( $active ); |
||
2986 | |||
2987 | Jetpack::state( 'error', false ); // the override |
||
2988 | ob_end_clean(); |
||
2989 | Jetpack::catch_errors( false ); |
||
2990 | |||
2991 | // A flag for Jump Start so it's not shown again. Only set if it hasn't been yet. |
||
2992 | View Code Duplication | if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { |
|
2993 | Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); |
||
2994 | |||
2995 | //Jump start is being dismissed send data to MC Stats |
||
2996 | $jetpack->stat( 'jumpstart', 'manual,'.$module ); |
||
2997 | |||
2998 | $jetpack->do_stats( 'server_side' ); |
||
2999 | } |
||
3000 | |||
3001 | if ( $redirect ) { |
||
3002 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
3003 | } |
||
3004 | if ( $exit ) { |
||
3005 | exit; |
||
3006 | } |
||
3007 | return true; |
||
3008 | } |
||
3009 | |||
3010 | function activate_module_actions( $module ) { |
||
3011 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
3012 | } |
||
3013 | |||
3014 | public static function deactivate_module( $module ) { |
||
3015 | /** |
||
3016 | * Fires when a module is deactivated. |
||
3017 | * |
||
3018 | * @since 1.9.0 |
||
3019 | * |
||
3020 | * @param string $module Module slug. |
||
3021 | */ |
||
3022 | do_action( 'jetpack_pre_deactivate_module', $module ); |
||
3023 | |||
3024 | $jetpack = Jetpack::init(); |
||
3025 | |||
3026 | $active = Jetpack::get_active_modules(); |
||
3027 | $new = array_filter( array_diff( $active, (array) $module ) ); |
||
3028 | |||
3029 | // A flag for Jump Start so it's not shown again. |
||
3030 | View Code Duplication | if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { |
|
3031 | Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); |
||
3032 | |||
3033 | //Jump start is being dismissed send data to MC Stats |
||
3034 | $jetpack->stat( 'jumpstart', 'manual,deactivated-'.$module ); |
||
3035 | |||
3036 | $jetpack->do_stats( 'server_side' ); |
||
3037 | } |
||
3038 | |||
3039 | return self::update_active_modules( $new ); |
||
3040 | } |
||
3041 | |||
3042 | public static function enable_module_configurable( $module ) { |
||
3043 | $module = Jetpack::get_module_slug( $module ); |
||
3044 | add_filter( 'jetpack_module_configurable_' . $module, '__return_true' ); |
||
3045 | } |
||
3046 | |||
3047 | /** |
||
3048 | * Composes a module configure URL. It uses Jetpack settings search as default value |
||
3049 | * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter |
||
3050 | * |
||
3051 | * @param string $module Module slug |
||
3052 | * @return string $url module configuration URL |
||
3053 | */ |
||
3054 | public static function module_configuration_url( $module ) { |
||
3055 | $module = Jetpack::get_module_slug( $module ); |
||
3056 | $default_url = Jetpack::admin_url() . "#/settings?term=$module"; |
||
3057 | /** |
||
3058 | * Allows to modify configure_url of specific module to be able to redirect to some custom location. |
||
3059 | * |
||
3060 | * @since 6.9.0 |
||
3061 | * |
||
3062 | * @param string $default_url Default url, which redirects to jetpack settings page. |
||
3063 | */ |
||
3064 | $url = apply_filters( 'jetpack_module_configuration_url_' . $module, $default_url ); |
||
3065 | |||
3066 | return $url; |
||
3067 | } |
||
3068 | |||
3069 | public static function module_configuration_load( $module, $method ) { |
||
3070 | $module = Jetpack::get_module_slug( $module ); |
||
3071 | add_action( 'jetpack_module_configuration_load_' . $module, $method ); |
||
3072 | } |
||
3073 | |||
3074 | public static function module_configuration_head( $module, $method ) { |
||
3075 | $module = Jetpack::get_module_slug( $module ); |
||
3076 | add_action( 'jetpack_module_configuration_head_' . $module, $method ); |
||
3077 | } |
||
3078 | |||
3079 | public static function module_configuration_screen( $module, $method ) { |
||
3080 | $module = Jetpack::get_module_slug( $module ); |
||
3081 | add_action( 'jetpack_module_configuration_screen_' . $module, $method ); |
||
3082 | } |
||
3083 | |||
3084 | public static function module_configuration_activation_screen( $module, $method ) { |
||
3085 | $module = Jetpack::get_module_slug( $module ); |
||
3086 | add_action( 'display_activate_module_setting_' . $module, $method ); |
||
3087 | } |
||
3088 | |||
3089 | /* Installation */ |
||
3090 | |||
3091 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
3092 | ?> |
||
3093 | <!doctype html> |
||
3094 | <html> |
||
3095 | <head> |
||
3096 | <meta charset="<?php bloginfo( 'charset' ); ?>"> |
||
3097 | <style> |
||
3098 | * { |
||
3099 | text-align: center; |
||
3100 | margin: 0; |
||
3101 | padding: 0; |
||
3102 | font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; |
||
3103 | } |
||
3104 | p { |
||
3105 | margin-top: 1em; |
||
3106 | font-size: 18px; |
||
3107 | } |
||
3108 | </style> |
||
3109 | <body> |
||
3110 | <p><?php echo esc_html( $message ); ?></p> |
||
3111 | </body> |
||
3112 | </html> |
||
3113 | <?php |
||
3114 | if ( $deactivate ) { |
||
3115 | $plugins = get_option( 'active_plugins' ); |
||
3116 | $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ); |
||
3117 | $update = false; |
||
3118 | foreach ( $plugins as $i => $plugin ) { |
||
3119 | if ( $plugin === $jetpack ) { |
||
3120 | $plugins[$i] = false; |
||
3121 | $update = true; |
||
3122 | } |
||
3123 | } |
||
3124 | |||
3125 | if ( $update ) { |
||
3126 | update_option( 'active_plugins', array_filter( $plugins ) ); |
||
3127 | } |
||
3128 | } |
||
3129 | exit; |
||
3130 | } |
||
3131 | |||
3132 | /** |
||
3133 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
3134 | * @static |
||
3135 | */ |
||
3136 | public static function plugin_activation( $network_wide ) { |
||
3137 | Jetpack_Options::update_option( 'activated', 1 ); |
||
3138 | |||
3139 | if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) { |
||
3140 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) ); |
||
3141 | } |
||
3142 | |||
3143 | if ( $network_wide ) |
||
3144 | Jetpack::state( 'network_nag', true ); |
||
3145 | |||
3146 | // For firing one-off events (notices) immediately after activation |
||
3147 | set_transient( 'activated_jetpack', true, .1 * MINUTE_IN_SECONDS ); |
||
3148 | |||
3149 | update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) ); |
||
3150 | |||
3151 | Jetpack::plugin_initialize(); |
||
3152 | } |
||
3153 | |||
3154 | public static function get_activation_source( $referer_url ) { |
||
3155 | |||
3156 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
3157 | return array( 'wp-cli', null ); |
||
3158 | } |
||
3159 | |||
3160 | $referer = parse_url( $referer_url ); |
||
3161 | |||
3162 | $source_type = 'unknown'; |
||
3163 | $source_query = null; |
||
3164 | |||
3165 | if ( ! is_array( $referer ) ) { |
||
3166 | return array( $source_type, $source_query ); |
||
3167 | } |
||
3168 | |||
3169 | $plugins_path = parse_url( admin_url( 'plugins.php' ), PHP_URL_PATH ); |
||
3170 | $plugins_install_path = parse_url( admin_url( 'plugin-install.php' ), PHP_URL_PATH );// /wp-admin/plugin-install.php |
||
3171 | |||
3172 | if ( isset( $referer['query'] ) ) { |
||
3173 | parse_str( $referer['query'], $query_parts ); |
||
3174 | } else { |
||
3175 | $query_parts = array(); |
||
3176 | } |
||
3177 | |||
3178 | if ( $plugins_path === $referer['path'] ) { |
||
3179 | $source_type = 'list'; |
||
3180 | } elseif ( $plugins_install_path === $referer['path'] ) { |
||
3181 | $tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured'; |
||
3182 | switch( $tab ) { |
||
3183 | case 'popular': |
||
3184 | $source_type = 'popular'; |
||
3185 | break; |
||
3186 | case 'recommended': |
||
3187 | $source_type = 'recommended'; |
||
3188 | break; |
||
3189 | case 'favorites': |
||
3190 | $source_type = 'favorites'; |
||
3191 | break; |
||
3192 | case 'search': |
||
3193 | $source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' ); |
||
3194 | $source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null; |
||
3195 | break; |
||
3196 | default: |
||
3197 | $source_type = 'featured'; |
||
3198 | } |
||
3199 | } |
||
3200 | |||
3201 | return array( $source_type, $source_query ); |
||
3202 | } |
||
3203 | |||
3204 | /** |
||
3205 | * Runs before bumping version numbers up to a new version |
||
3206 | * @param string $version Version:timestamp |
||
3207 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
3208 | * @return null [description] |
||
3209 | */ |
||
3210 | public static function do_version_bump( $version, $old_version ) { |
||
3211 | if ( ! $old_version ) { // For new sites |
||
3212 | // There used to be stuff here, but this seems like it might be useful to someone in the future... |
||
3213 | } |
||
3214 | } |
||
3215 | |||
3216 | /** |
||
3217 | * Sets the internal version number and activation state. |
||
3218 | * @static |
||
3219 | */ |
||
3220 | public static function plugin_initialize() { |
||
3221 | if ( ! Jetpack_Options::get_option( 'activated' ) ) { |
||
3222 | Jetpack_Options::update_option( 'activated', 2 ); |
||
3223 | } |
||
3224 | |||
3225 | View Code Duplication | if ( ! Jetpack_Options::get_option( 'version' ) ) { |
|
3226 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
3227 | /** This action is documented in class.jetpack.php */ |
||
3228 | do_action( 'updating_jetpack_version', $version, false ); |
||
3229 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
3230 | } |
||
3231 | |||
3232 | Jetpack::load_modules(); |
||
3233 | |||
3234 | Jetpack_Options::delete_option( 'do_activate' ); |
||
3235 | Jetpack_Options::delete_option( 'dismissed_connection_banner' ); |
||
3236 | } |
||
3237 | |||
3238 | /** |
||
3239 | * Removes all connection options |
||
3240 | * @static |
||
3241 | */ |
||
3242 | public static function plugin_deactivation( ) { |
||
3243 | require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
||
3244 | if( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
3245 | Jetpack_Network::init()->deactivate(); |
||
3246 | } else { |
||
3247 | Jetpack::disconnect( false ); |
||
3248 | //Jetpack_Heartbeat::init()->deactivate(); |
||
3249 | } |
||
3250 | } |
||
3251 | |||
3252 | /** |
||
3253 | * Disconnects from the Jetpack servers. |
||
3254 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
3255 | * @static |
||
3256 | */ |
||
3257 | public static function disconnect( $update_activated_state = true ) { |
||
3258 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
3259 | Jetpack::clean_nonces( true ); |
||
3260 | |||
3261 | // If the site is in an IDC because sync is not allowed, |
||
3262 | // let's make sure to not disconnect the production site. |
||
3263 | if ( ! self::validate_sync_error_idc_option() ) { |
||
3264 | JetpackTracking::record_user_event( 'disconnect_site', array() ); |
||
3265 | Jetpack::load_xml_rpc_client(); |
||
3266 | $xml = new Jetpack_IXR_Client(); |
||
3267 | $xml->query( 'jetpack.deregister' ); |
||
3268 | } |
||
3269 | |||
3270 | Jetpack_Options::delete_option( |
||
3271 | array( |
||
3272 | 'blog_token', |
||
3273 | 'user_token', |
||
3274 | 'user_tokens', |
||
3275 | 'master_user', |
||
3276 | 'time_diff', |
||
3277 | 'fallback_no_verify_ssl_certs', |
||
3278 | ) |
||
3279 | ); |
||
3280 | |||
3281 | Jetpack_IDC::clear_all_idc_options(); |
||
3282 | Jetpack_Options::delete_raw_option( 'jetpack_secrets' ); |
||
3283 | |||
3284 | if ( $update_activated_state ) { |
||
3285 | Jetpack_Options::update_option( 'activated', 4 ); |
||
3286 | } |
||
3287 | |||
3288 | if ( $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ) ) { |
||
3289 | // Check then record unique disconnection if site has never been disconnected previously |
||
3290 | if ( - 1 == $jetpack_unique_connection['disconnected'] ) { |
||
3291 | $jetpack_unique_connection['disconnected'] = 1; |
||
3292 | } else { |
||
3293 | if ( 0 == $jetpack_unique_connection['disconnected'] ) { |
||
3294 | //track unique disconnect |
||
3295 | $jetpack = Jetpack::init(); |
||
3296 | |||
3297 | $jetpack->stat( 'connections', 'unique-disconnect' ); |
||
3298 | $jetpack->do_stats( 'server_side' ); |
||
3299 | } |
||
3300 | // increment number of times disconnected |
||
3301 | $jetpack_unique_connection['disconnected'] += 1; |
||
3302 | } |
||
3303 | |||
3304 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
3305 | } |
||
3306 | |||
3307 | // Delete cached connected user data |
||
3308 | $transient_key = "jetpack_connected_user_data_" . get_current_user_id(); |
||
3309 | delete_transient( $transient_key ); |
||
3310 | |||
3311 | // Delete all the sync related data. Since it could be taking up space. |
||
3312 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
||
3313 | Jetpack_Sync_Sender::get_instance()->uninstall(); |
||
3314 | |||
3315 | // Disable the Heartbeat cron |
||
3316 | Jetpack_Heartbeat::init()->deactivate(); |
||
3317 | } |
||
3318 | |||
3319 | /** |
||
3320 | * Unlinks the current user from the linked WordPress.com user |
||
3321 | */ |
||
3322 | public static function unlink_user( $user_id = null ) { |
||
3323 | if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) ) |
||
3324 | return false; |
||
3325 | |||
3326 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
3327 | |||
3328 | if ( Jetpack_Options::get_option( 'master_user' ) == $user_id ) |
||
3329 | return false; |
||
3330 | |||
3331 | if ( ! isset( $tokens[ $user_id ] ) ) |
||
3332 | return false; |
||
3333 | |||
3334 | Jetpack::load_xml_rpc_client(); |
||
3335 | $xml = new Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
3336 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
3337 | |||
3338 | unset( $tokens[ $user_id ] ); |
||
3339 | |||
3340 | Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
3341 | |||
3342 | /** |
||
3343 | * Fires after the current user has been unlinked from WordPress.com. |
||
3344 | * |
||
3345 | * @since 4.1.0 |
||
3346 | * |
||
3347 | * @param int $user_id The current user's ID. |
||
3348 | */ |
||
3349 | do_action( 'jetpack_unlinked_user', $user_id ); |
||
3350 | |||
3351 | return true; |
||
3352 | } |
||
3353 | |||
3354 | /** |
||
3355 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
3356 | */ |
||
3357 | public static function try_registration() { |
||
3358 | // The user has agreed to the TOS at some point by now. |
||
3359 | Jetpack_Options::update_option( 'tos_agreed', true ); |
||
3360 | |||
3361 | // Let's get some testing in beta versions and such. |
||
3362 | if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) { |
||
3363 | // Before attempting to connect, let's make sure that the domains are viable. |
||
3364 | $domains_to_check = array_unique( array( |
||
3365 | 'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ), |
||
3366 | 'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ), |
||
3367 | ) ); |
||
3368 | foreach ( $domains_to_check as $domain ) { |
||
3369 | $result = Jetpack_Data::is_usable_domain( $domain ); |
||
3370 | if ( is_wp_error( $result ) ) { |
||
3371 | return $result; |
||
3372 | } |
||
3373 | } |
||
3374 | } |
||
3375 | |||
3376 | $result = Jetpack::register(); |
||
3377 | |||
3378 | // If there was an error with registration and the site was not registered, record this so we can show a message. |
||
3379 | if ( ! $result || is_wp_error( $result ) ) { |
||
3380 | return $result; |
||
3381 | } else { |
||
3382 | return true; |
||
3383 | } |
||
3384 | } |
||
3385 | |||
3386 | /** |
||
3387 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3388 | * |
||
3389 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3390 | */ |
||
3391 | public static function log( $code, $data = null ) { |
||
3392 | // only grab the latest 200 entries |
||
3393 | $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 ); |
||
3394 | |||
3395 | // Append our event to the log |
||
3396 | $log_entry = array( |
||
3397 | 'time' => time(), |
||
3398 | 'user_id' => get_current_user_id(), |
||
3399 | 'blog_id' => Jetpack_Options::get_option( 'id' ), |
||
3400 | 'code' => $code, |
||
3401 | ); |
||
3402 | // Don't bother storing it unless we've got some. |
||
3403 | if ( ! is_null( $data ) ) { |
||
3404 | $log_entry['data'] = $data; |
||
3405 | } |
||
3406 | $log[] = $log_entry; |
||
3407 | |||
3408 | // Try add_option first, to make sure it's not autoloaded. |
||
3409 | // @todo: Add an add_option method to Jetpack_Options |
||
3410 | if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) { |
||
3411 | Jetpack_Options::update_option( 'log', $log ); |
||
3412 | } |
||
3413 | |||
3414 | /** |
||
3415 | * Fires when Jetpack logs an internal event. |
||
3416 | * |
||
3417 | * @since 3.0.0 |
||
3418 | * |
||
3419 | * @param array $log_entry { |
||
3420 | * Array of details about the log entry. |
||
3421 | * |
||
3422 | * @param string time Time of the event. |
||
3423 | * @param int user_id ID of the user who trigerred the event. |
||
3424 | * @param int blog_id Jetpack Blog ID. |
||
3425 | * @param string code Unique name for the event. |
||
3426 | * @param string data Data about the event. |
||
3427 | * } |
||
3428 | */ |
||
3429 | do_action( 'jetpack_log_entry', $log_entry ); |
||
3430 | } |
||
3431 | |||
3432 | /** |
||
3433 | * Get the internal event log. |
||
3434 | * |
||
3435 | * @param $event (string) - only return the specific log events |
||
3436 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3437 | * |
||
3438 | * @return array of log events || WP_Error for invalid params |
||
3439 | */ |
||
3440 | public static function get_log( $event = false, $num = false ) { |
||
3441 | if ( $event && ! is_string( $event ) ) { |
||
3442 | return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) ); |
||
3443 | } |
||
3444 | |||
3445 | if ( $num && ! is_numeric( $num ) ) { |
||
3446 | return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) ); |
||
3447 | } |
||
3448 | |||
3449 | $entire_log = Jetpack_Options::get_option( 'log', array() ); |
||
3450 | |||
3451 | // If nothing set - act as it did before, otherwise let's start customizing the output |
||
3452 | if ( ! $num && ! $event ) { |
||
3453 | return $entire_log; |
||
3454 | } else { |
||
3455 | $entire_log = array_reverse( $entire_log ); |
||
3456 | } |
||
3457 | |||
3458 | $custom_log_output = array(); |
||
3459 | |||
3460 | if ( $event ) { |
||
3461 | foreach ( $entire_log as $log_event ) { |
||
3462 | if ( $event == $log_event[ 'code' ] ) { |
||
3463 | $custom_log_output[] = $log_event; |
||
3464 | } |
||
3465 | } |
||
3466 | } else { |
||
3467 | $custom_log_output = $entire_log; |
||
3468 | } |
||
3469 | |||
3470 | if ( $num ) { |
||
3471 | $custom_log_output = array_slice( $custom_log_output, 0, $num ); |
||
3472 | } |
||
3473 | |||
3474 | return $custom_log_output; |
||
3475 | } |
||
3476 | |||
3477 | /** |
||
3478 | * Log modification of important settings. |
||
3479 | */ |
||
3480 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3481 | switch( $option ) { |
||
3482 | case 'jetpack_sync_non_public_post_stati': |
||
3483 | self::log( $option, $value ); |
||
3484 | break; |
||
3485 | } |
||
3486 | } |
||
3487 | |||
3488 | /** |
||
3489 | * Return stat data for WPCOM sync |
||
3490 | */ |
||
3491 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3492 | $data = Jetpack_Heartbeat::generate_stats_array(); |
||
3493 | |||
3494 | if ( $extended ) { |
||
3495 | $additional_data = self::get_additional_stat_data(); |
||
3496 | $data = array_merge( $data, $additional_data ); |
||
3497 | } |
||
3498 | |||
3499 | if ( $encode ) { |
||
3500 | return json_encode( $data ); |
||
3501 | } |
||
3502 | |||
3503 | return $data; |
||
3504 | } |
||
3505 | |||
3506 | /** |
||
3507 | * Get additional stat data to sync to WPCOM |
||
3508 | */ |
||
3509 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3510 | $return["{$prefix}themes"] = Jetpack::get_parsed_theme_data(); |
||
3511 | $return["{$prefix}plugins-extra"] = Jetpack::get_parsed_plugin_data(); |
||
3512 | $return["{$prefix}users"] = (int) Jetpack::get_site_user_count(); |
||
3513 | $return["{$prefix}site-count"] = 0; |
||
3514 | |||
3515 | if ( function_exists( 'get_blog_count' ) ) { |
||
3516 | $return["{$prefix}site-count"] = get_blog_count(); |
||
3517 | } |
||
3518 | return $return; |
||
3519 | } |
||
3520 | |||
3521 | private static function get_site_user_count() { |
||
3522 | global $wpdb; |
||
3523 | |||
3524 | if ( function_exists( 'wp_is_large_network' ) ) { |
||
3525 | if ( wp_is_large_network( 'users' ) ) { |
||
3526 | return -1; // Not a real value but should tell us that we are dealing with a large network. |
||
3527 | } |
||
3528 | } |
||
3529 | View Code Duplication | if ( false === ( $user_count = get_transient( 'jetpack_site_user_count' ) ) ) { |
|
3530 | // It wasn't there, so regenerate the data and save the transient |
||
3531 | $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities'" ); |
||
3532 | set_transient( 'jetpack_site_user_count', $user_count, DAY_IN_SECONDS ); |
||
3533 | } |
||
3534 | return $user_count; |
||
3535 | } |
||
3536 | |||
3537 | /* Admin Pages */ |
||
3538 | |||
3539 | function admin_init() { |
||
3540 | // If the plugin is not connected, display a connect message. |
||
3541 | if ( |
||
3542 | // the plugin was auto-activated and needs its candy |
||
3543 | Jetpack_Options::get_option_and_ensure_autoload( 'do_activate', '0' ) |
||
3544 | || |
||
3545 | // the plugin is active, but was never activated. Probably came from a site-wide network activation |
||
3546 | ! Jetpack_Options::get_option( 'activated' ) |
||
3547 | ) { |
||
3548 | Jetpack::plugin_initialize(); |
||
3549 | } |
||
3550 | |||
3551 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
3552 | Jetpack_Connection_Banner::init(); |
||
3553 | } elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) { |
||
3554 | // Upgrade: 1.1 -> 1.1.1 |
||
3555 | // Check and see if host can verify the Jetpack servers' SSL certificate |
||
3556 | $args = array(); |
||
3557 | Jetpack_Client::_wp_remote_request( |
||
3558 | Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ) ), |
||
3559 | $args, |
||
3560 | true |
||
3561 | ); |
||
3562 | } |
||
3563 | |||
3564 | if ( current_user_can( 'manage_options' ) && 'AUTO' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) { |
||
3565 | add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) ); |
||
3566 | } |
||
3567 | |||
3568 | add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) ); |
||
3569 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
||
3570 | add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) ); |
||
3571 | |||
3572 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) { |
||
3573 | // Artificially throw errors in certain whitelisted cases during plugin activation |
||
3574 | add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) ); |
||
3575 | } |
||
3576 | |||
3577 | // Add custom column in wp-admin/users.php to show whether user is linked. |
||
3578 | add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) ); |
||
3579 | add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 ); |
||
3580 | add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) ); |
||
3581 | } |
||
3582 | |||
3583 | function admin_body_class( $admin_body_class = '' ) { |
||
3584 | $classes = explode( ' ', trim( $admin_body_class ) ); |
||
3585 | |||
3586 | $classes[] = self::is_active() ? 'jetpack-connected' : 'jetpack-disconnected'; |
||
3587 | |||
3588 | $admin_body_class = implode( ' ', array_unique( $classes ) ); |
||
3589 | return " $admin_body_class "; |
||
3590 | } |
||
3591 | |||
3592 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3593 | return $admin_body_class . ' jetpack-pagestyles '; |
||
3594 | } |
||
3595 | |||
3596 | /** |
||
3597 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3598 | * This function artificially throws errors for such cases (whitelisted). |
||
3599 | * |
||
3600 | * @param string $plugin The activated plugin. |
||
3601 | */ |
||
3602 | function throw_error_on_activate_plugin( $plugin ) { |
||
3603 | $active_modules = Jetpack::get_active_modules(); |
||
3604 | |||
3605 | // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks. |
||
3606 | if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) { |
||
3607 | $throw = false; |
||
3608 | |||
3609 | // Try and make sure it really was the stats plugin |
||
3610 | if ( ! class_exists( 'ReflectionFunction' ) ) { |
||
3611 | if ( 'stats.php' == basename( $plugin ) ) { |
||
3612 | $throw = true; |
||
3613 | } |
||
3614 | } else { |
||
3615 | $reflection = new ReflectionFunction( 'stats_get_api_key' ); |
||
3616 | if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) { |
||
3617 | $throw = true; |
||
3618 | } |
||
3619 | } |
||
3620 | |||
3621 | if ( $throw ) { |
||
3622 | trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR ); |
||
3623 | } |
||
3624 | } |
||
3625 | } |
||
3626 | |||
3627 | function intercept_plugin_error_scrape_init() { |
||
3628 | add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 ); |
||
3629 | } |
||
3630 | |||
3631 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3632 | if ( ! $result ) { |
||
3633 | return; |
||
3634 | } |
||
3635 | |||
3636 | foreach ( $this->plugins_to_deactivate as $deactivate_me ) { |
||
3637 | if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) { |
||
3638 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), $deactivate_me[1] ), false ); |
||
3639 | } |
||
3640 | } |
||
3641 | } |
||
3642 | |||
3643 | function add_remote_request_handlers() { |
||
3644 | add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) ); |
||
3645 | add_action( 'wp_ajax_nopriv_jetpack_update_file', array( $this, 'remote_request_handlers' ) ); |
||
3646 | } |
||
3647 | |||
3648 | function remote_request_handlers() { |
||
3649 | $action = current_filter(); |
||
3650 | |||
3651 | switch ( current_filter() ) { |
||
3652 | case 'wp_ajax_nopriv_jetpack_upload_file' : |
||
3653 | $response = $this->upload_handler(); |
||
3654 | break; |
||
3655 | |||
3656 | case 'wp_ajax_nopriv_jetpack_update_file' : |
||
3657 | $response = $this->upload_handler( true ); |
||
3658 | break; |
||
3659 | default : |
||
3660 | $response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 ); |
||
3661 | break; |
||
3662 | } |
||
3663 | |||
3664 | if ( ! $response ) { |
||
3665 | $response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 ); |
||
3666 | } |
||
3667 | |||
3668 | if ( is_wp_error( $response ) ) { |
||
3669 | $status_code = $response->get_error_data(); |
||
3670 | $error = $response->get_error_code(); |
||
3671 | $error_description = $response->get_error_message(); |
||
3672 | |||
3673 | if ( ! is_int( $status_code ) ) { |
||
3674 | $status_code = 400; |
||
3675 | } |
||
3676 | |||
3677 | status_header( $status_code ); |
||
3678 | die( json_encode( (object) compact( 'error', 'error_description' ) ) ); |
||
3679 | } |
||
3680 | |||
3681 | status_header( 200 ); |
||
3682 | if ( true === $response ) { |
||
3683 | exit; |
||
3684 | } |
||
3685 | |||
3686 | die( json_encode( (object) $response ) ); |
||
3687 | } |
||
3688 | |||
3689 | /** |
||
3690 | * Uploads a file gotten from the global $_FILES. |
||
3691 | * If `$update_media_item` is true and `post_id` is defined |
||
3692 | * the attachment file of the media item (gotten through of the post_id) |
||
3693 | * will be updated instead of add a new one. |
||
3694 | * |
||
3695 | * @param boolean $update_media_item - update media attachment |
||
3696 | * @return array - An array describing the uploadind files process |
||
3697 | */ |
||
3698 | function upload_handler( $update_media_item = false ) { |
||
3699 | if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { |
||
3700 | return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 ); |
||
3701 | } |
||
3702 | |||
3703 | $user = wp_authenticate( '', '' ); |
||
3704 | if ( ! $user || is_wp_error( $user ) ) { |
||
3705 | return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 ); |
||
3706 | } |
||
3707 | |||
3708 | wp_set_current_user( $user->ID ); |
||
3709 | |||
3710 | if ( ! current_user_can( 'upload_files' ) ) { |
||
3711 | return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 ); |
||
3712 | } |
||
3713 | |||
3714 | if ( empty( $_FILES ) ) { |
||
3715 | return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 ); |
||
3716 | } |
||
3717 | |||
3718 | foreach ( array_keys( $_FILES ) as $files_key ) { |
||
3719 | if ( ! isset( $_POST["_jetpack_file_hmac_{$files_key}"] ) ) { |
||
3720 | return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 ); |
||
3721 | } |
||
3722 | } |
||
3723 | |||
3724 | $media_keys = array_keys( $_FILES['media'] ); |
||
3725 | |||
3726 | $token = Jetpack_Data::get_access_token( get_current_user_id() ); |
||
3727 | if ( ! $token || is_wp_error( $token ) ) { |
||
3728 | return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 ); |
||
3729 | } |
||
3730 | |||
3731 | $uploaded_files = array(); |
||
3732 | $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; |
||
3733 | unset( $GLOBALS['post'] ); |
||
3734 | foreach ( $_FILES['media']['name'] as $index => $name ) { |
||
3735 | $file = array(); |
||
3736 | foreach ( $media_keys as $media_key ) { |
||
3737 | $file[$media_key] = $_FILES['media'][$media_key][$index]; |
||
3738 | } |
||
3739 | |||
3740 | list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][$index] ); |
||
3741 | |||
3742 | $hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret ); |
||
3743 | if ( $hmac_provided !== $hmac_file ) { |
||
3744 | $uploaded_files[$index] = (object) array( 'error' => 'invalid_hmac', 'error_description' => 'The corresponding HMAC for this file does not match' ); |
||
3745 | continue; |
||
3746 | } |
||
3747 | |||
3748 | $_FILES['.jetpack.upload.'] = $file; |
||
3749 | $post_id = isset( $_POST['post_id'][$index] ) ? absint( $_POST['post_id'][$index] ) : 0; |
||
3750 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
||
3751 | $post_id = 0; |
||
3752 | } |
||
3753 | |||
3754 | if ( $update_media_item ) { |
||
3755 | if ( ! isset( $post_id ) || $post_id === 0 ) { |
||
3756 | return new Jetpack_Error( 'invalid_input', 'Media ID must be defined.', 400 ); |
||
3757 | } |
||
3758 | |||
3759 | $media_array = $_FILES['media']; |
||
3760 | |||
3761 | $file_array['name'] = $media_array['name'][0]; |
||
3762 | $file_array['type'] = $media_array['type'][0]; |
||
3763 | $file_array['tmp_name'] = $media_array['tmp_name'][0]; |
||
3764 | $file_array['error'] = $media_array['error'][0]; |
||
3765 | $file_array['size'] = $media_array['size'][0]; |
||
3766 | |||
3767 | $edited_media_item = Jetpack_Media::edit_media_file( $post_id, $file_array ); |
||
3768 | |||
3769 | if ( is_wp_error( $edited_media_item ) ) { |
||
3770 | return $edited_media_item; |
||
3771 | } |
||
3772 | |||
3773 | $response = (object) array( |
||
3774 | 'id' => (string) $post_id, |
||
3775 | 'file' => (string) $edited_media_item->post_title, |
||
3776 | 'url' => (string) wp_get_attachment_url( $post_id ), |
||
3777 | 'type' => (string) $edited_media_item->post_mime_type, |
||
3778 | 'meta' => (array) wp_get_attachment_metadata( $post_id ), |
||
3779 | ); |
||
3780 | |||
3781 | return (array) array( $response ); |
||
3782 | } |
||
3783 | |||
3784 | $attachment_id = media_handle_upload( |
||
3785 | '.jetpack.upload.', |
||
3786 | $post_id, |
||
3787 | array(), |
||
3788 | array( |
||
3789 | 'action' => 'jetpack_upload_file', |
||
3790 | ) |
||
3791 | ); |
||
3792 | |||
3793 | if ( ! $attachment_id ) { |
||
3794 | $uploaded_files[$index] = (object) array( 'error' => 'unknown', 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site' ); |
||
3795 | } elseif ( is_wp_error( $attachment_id ) ) { |
||
3796 | $uploaded_files[$index] = (object) array( 'error' => 'attachment_' . $attachment_id->get_error_code(), 'error_description' => $attachment_id->get_error_message() ); |
||
3797 | } else { |
||
3798 | $attachment = get_post( $attachment_id ); |
||
3799 | $uploaded_files[$index] = (object) array( |
||
3800 | 'id' => (string) $attachment_id, |
||
3801 | 'file' => $attachment->post_title, |
||
3802 | 'url' => wp_get_attachment_url( $attachment_id ), |
||
3803 | 'type' => $attachment->post_mime_type, |
||
3804 | 'meta' => wp_get_attachment_metadata( $attachment_id ), |
||
3805 | ); |
||
3806 | // Zip files uploads are not supported unless they are done for installation purposed |
||
3807 | // lets delete them in case something goes wrong in this whole process |
||
3808 | if ( 'application/zip' === $attachment->post_mime_type ) { |
||
3809 | // Schedule a cleanup for 2 hours from now in case of failed install. |
||
3810 | wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $attachment_id ) ); |
||
3811 | } |
||
3812 | } |
||
3813 | } |
||
3814 | if ( ! is_null( $global_post ) ) { |
||
3815 | $GLOBALS['post'] = $global_post; |
||
3816 | } |
||
3817 | |||
3818 | return $uploaded_files; |
||
3819 | } |
||
3820 | |||
3821 | /** |
||
3822 | * Add help to the Jetpack page |
||
3823 | * |
||
3824 | * @since Jetpack (1.2.3) |
||
3825 | * @return false if not the Jetpack page |
||
3826 | */ |
||
3827 | function admin_help() { |
||
3828 | $current_screen = get_current_screen(); |
||
3829 | |||
3830 | // Overview |
||
3831 | $current_screen->add_help_tab( |
||
3832 | array( |
||
3833 | 'id' => 'home', |
||
3834 | 'title' => __( 'Home', 'jetpack' ), |
||
3835 | 'content' => |
||
3836 | '<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' . |
||
3837 | '<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' . |
||
3838 | '<p>' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '</p>', |
||
3839 | ) |
||
3840 | ); |
||
3841 | |||
3842 | // Screen Content |
||
3843 | if ( current_user_can( 'manage_options' ) ) { |
||
3844 | $current_screen->add_help_tab( |
||
3845 | array( |
||
3846 | 'id' => 'settings', |
||
3847 | 'title' => __( 'Settings', 'jetpack' ), |
||
3848 | 'content' => |
||
3849 | '<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' . |
||
3850 | '<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' . |
||
3851 | '<ol>' . |
||
3852 | '<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' . |
||
3853 | '<li>' . __( 'Using the checkboxes next to each module, you can select multiple modules to toggle via the Bulk Actions menu at the top of the list.', 'jetpack' ) . '</li>' . |
||
3854 | '</ol>' . |
||
3855 | '<p>' . __( 'Using the tools on the right, you can search for specific modules, filter by module categories or which are active, or change the sorting order.', 'jetpack' ) . '</p>' |
||
3856 | ) |
||
3857 | ); |
||
3858 | } |
||
3859 | |||
3860 | // Help Sidebar |
||
3861 | $current_screen->set_help_sidebar( |
||
3862 | '<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' . |
||
3863 | '<p><a href="https://jetpack.com/faq/" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' . |
||
3864 | '<p><a href="https://jetpack.com/support/" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' . |
||
3865 | '<p><a href="' . Jetpack::admin_url( array( 'page' => 'jetpack-debugger' ) ) .'">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>' |
||
3866 | ); |
||
3867 | } |
||
3868 | |||
3869 | function admin_menu_css() { |
||
3870 | wp_enqueue_style( 'jetpack-icons' ); |
||
3871 | } |
||
3872 | |||
3873 | function admin_menu_order() { |
||
3874 | return true; |
||
3875 | } |
||
3876 | |||
3877 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
3892 | |||
3893 | function admin_head() { |
||
3898 | |||
3899 | function admin_banner_styles() { |
||
3920 | |||
3921 | function plugin_action_links( $actions ) { |
||
3936 | |||
3937 | /* |
||
3938 | * Registration flow: |
||
3939 | * 1 - ::admin_page_load() action=register |
||
3940 | * 2 - ::try_registration() |
||
3941 | * 3 - ::register() |
||
3942 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
3943 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
3944 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
3945 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
3946 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
3947 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
3948 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
3949 | * jetpack_id, jetpack_secret, jetpack_public |
||
3950 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
3951 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
3952 | * 5 - user logs in with WP.com account |
||
3953 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
3954 | * - Jetpack_Client_Server::authorize() |
||
3955 | * - Jetpack_Client_Server::get_token() |
||
3956 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
3957 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
3958 | * - which responds with access_token, token_type, scope |
||
3959 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
3960 | * - Jetpack::activate_default_modules() |
||
3961 | * - Deactivates deprecated plugins |
||
3962 | * - Activates all default modules |
||
3963 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
3964 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
3965 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
3966 | * Done! |
||
3967 | */ |
||
3968 | |||
3969 | /** |
||
3970 | * Handles the page load events for the Jetpack admin page |
||
3971 | */ |
||
3972 | function admin_page_load() { |
||
3973 | $error = false; |
||
3974 | |||
3975 | // Make sure we have the right body class to hook stylings for subpages off of. |
||
3976 | add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ) ); |
||
3977 | |||
3978 | if ( ! empty( $_GET['jetpack_restate'] ) ) { |
||
3979 | // Should only be used in intermediate redirects to preserve state across redirects |
||
3980 | Jetpack::restate(); |
||
3981 | } |
||
3982 | |||
3983 | if ( isset( $_GET['connect_url_redirect'] ) ) { |
||
3984 | // User clicked in the iframe to link their accounts |
||
3985 | if ( ! Jetpack::is_user_connected() ) { |
||
3986 | $from = ! empty( $_GET['from'] ) ? $_GET['from'] : 'iframe'; |
||
3987 | $redirect = ! empty( $_GET['redirect_after_auth'] ) ? $_GET['redirect_after_auth'] : false; |
||
3988 | |||
3989 | add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) ); |
||
3990 | $connect_url = $this->build_connect_url( true, $redirect, $from ); |
||
3991 | remove_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) ); |
||
3992 | |||
3993 | if ( isset( $_GET['notes_iframe'] ) ) |
||
3994 | $connect_url .= '¬es_iframe'; |
||
3995 | wp_redirect( $connect_url ); |
||
3996 | exit; |
||
3997 | } else { |
||
3998 | if ( ! isset( $_GET['calypso_env'] ) ) { |
||
3999 | Jetpack::state( 'message', 'already_authorized' ); |
||
4000 | wp_safe_redirect( Jetpack::admin_url() ); |
||
4001 | exit; |
||
4002 | } else { |
||
4003 | $connect_url = $this->build_connect_url( true, false, 'iframe' ); |
||
4004 | $connect_url .= '&already_authorized=true'; |
||
4005 | wp_redirect( $connect_url ); |
||
4006 | exit; |
||
4007 | } |
||
4008 | } |
||
4009 | } |
||
4010 | |||
4011 | |||
4012 | if ( isset( $_GET['action'] ) ) { |
||
4013 | switch ( $_GET['action'] ) { |
||
4014 | case 'authorize': |
||
4015 | if ( Jetpack::is_active() && Jetpack::is_user_connected() ) { |
||
4016 | Jetpack::state( 'message', 'already_authorized' ); |
||
4017 | wp_safe_redirect( Jetpack::admin_url() ); |
||
4018 | exit; |
||
4019 | } |
||
4020 | Jetpack::log( 'authorize' ); |
||
4021 | $client_server = new Jetpack_Client_Server; |
||
4022 | $client_server->client_authorize(); |
||
4023 | exit; |
||
4024 | case 'register' : |
||
4025 | if ( ! current_user_can( 'jetpack_connect' ) ) { |
||
4026 | $error = 'cheatin'; |
||
4027 | break; |
||
4028 | } |
||
4029 | check_admin_referer( 'jetpack-register' ); |
||
4030 | Jetpack::log( 'register' ); |
||
4031 | Jetpack::maybe_set_version_option(); |
||
4032 | $registered = Jetpack::try_registration(); |
||
4033 | if ( is_wp_error( $registered ) ) { |
||
4034 | $error = $registered->get_error_code(); |
||
4035 | Jetpack::state( 'error', $error ); |
||
4036 | Jetpack::state( 'error', $registered->get_error_message() ); |
||
4037 | JetpackTracking::record_user_event( 'jpc_register_fail', array( |
||
4038 | 'error_code' => $error, |
||
4039 | 'error_message' => $registered->get_error_message() |
||
4040 | ) ); |
||
4041 | break; |
||
4042 | } |
||
4043 | |||
4044 | $from = isset( $_GET['from'] ) ? $_GET['from'] : false; |
||
4045 | $redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : false; |
||
4046 | |||
4047 | JetpackTracking::record_user_event( 'jpc_register_success', array( |
||
4048 | 'from' => $from |
||
4049 | ) ); |
||
4050 | |||
4051 | $url = $this->build_connect_url( true, $redirect, $from ); |
||
4052 | |||
4053 | if ( ! empty( $_GET['onboarding'] ) ) { |
||
4054 | $url = add_query_arg( 'onboarding', $_GET['onboarding'], $url ); |
||
4055 | } |
||
4056 | |||
4057 | if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) { |
||
4058 | $url = add_query_arg( 'auth_approved', 'true', $url ); |
||
4059 | } |
||
4060 | |||
4061 | wp_redirect( $url ); |
||
4062 | exit; |
||
4063 | case 'activate' : |
||
4064 | if ( ! current_user_can( 'jetpack_activate_modules' ) ) { |
||
4065 | $error = 'cheatin'; |
||
4066 | break; |
||
4067 | } |
||
4068 | |||
4069 | $module = stripslashes( $_GET['module'] ); |
||
4070 | check_admin_referer( "jetpack_activate-$module" ); |
||
4071 | Jetpack::log( 'activate', $module ); |
||
4247 | |||
4248 | function admin_notices() { |
||
4340 | |||
4341 | /** |
||
4342 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4343 | */ |
||
4344 | function stat( $group, $detail ) { |
||
4349 | |||
4350 | /** |
||
4351 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4352 | */ |
||
4353 | function do_stats( $method = '' ) { |
||
4368 | |||
4369 | /** |
||
4370 | * Runs stats code for a one-off, server-side. |
||
4371 | * |
||
4372 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4373 | * |
||
4374 | * @return bool If it worked. |
||
4375 | */ |
||
4376 | static function do_server_side_stat( $args ) { |
||
4386 | |||
4387 | /** |
||
4388 | * Builds the stats url. |
||
4389 | * |
||
4390 | * @param $args array|string The arguments to append to the URL. |
||
4391 | * |
||
4392 | * @return string The URL to be pinged. |
||
4393 | */ |
||
4394 | static function build_stats_url( $args ) { |
||
4414 | |||
4415 | static function translate_current_user_to_role() { |
||
4424 | |||
4425 | static function translate_user_to_role( $user ) { |
||
4434 | |||
4435 | static function translate_role_to_cap( $role ) { |
||
4442 | |||
4443 | static function sign_role( $role, $user_id = null ) { |
||
4459 | |||
4460 | |||
4461 | /** |
||
4462 | * Builds a URL to the Jetpack connection auth page |
||
4463 | * |
||
4464 | * @since 3.9.5 |
||
4465 | * |
||
4466 | * @param bool $raw If true, URL will not be escaped. |
||
4467 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4468 | * If string, will be a custom redirect. |
||
4469 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4470 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
4471 | * |
||
4472 | * @return string Connect URL |
||
4473 | */ |
||
4474 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
4598 | |||
4599 | /** |
||
4600 | * Get our assumed site creation date. |
||
4601 | * Calculated based on the earlier date of either: |
||
4602 | * - Earliest admin user registration date. |
||
4603 | * - Earliest date of post of any post type. |
||
4604 | * |
||
4605 | * @since 7.2.0 |
||
4606 | * |
||
4607 | * @return string Assumed site creation date and time. |
||
4608 | */ |
||
4609 | public static function get_assumed_site_creation_date() { |
||
4636 | |||
4637 | public static function apply_activation_source_to_args( &$args ) { |
||
4648 | |||
4649 | function build_reconnect_url( $raw = false ) { |
||
4653 | |||
4654 | public static function admin_url( $args = null ) { |
||
4659 | |||
4660 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
4664 | |||
4665 | function dismiss_jetpack_notice() { |
||
4697 | |||
4698 | public static function admin_screen_configure_module( $module_id ) { |
||
4749 | |||
4750 | /** |
||
4751 | * Display link to activate the module to see the settings screen. |
||
4752 | * @param string $module_id |
||
4753 | * @return null |
||
4754 | */ |
||
4755 | public static function display_activate_module_link( $module_id ) { |
||
4805 | |||
4806 | public static function sort_modules( $a, $b ) { |
||
4812 | |||
4813 | function ajax_recheck_ssl() { |
||
4821 | |||
4822 | /* Client API */ |
||
4823 | |||
4824 | /** |
||
4825 | * Returns the requested Jetpack API URL |
||
4826 | * |
||
4827 | * @return string |
||
4828 | */ |
||
4829 | public static function api_url( $relative_url ) { |
||
4832 | |||
4833 | /** |
||
4834 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
4835 | */ |
||
4836 | public static function fix_url_for_bad_hosts( $url ) { |
||
4852 | |||
4853 | /** |
||
4854 | * Create a random secret for validating onboarding payload |
||
4855 | * |
||
4856 | * @return string Secret token |
||
4857 | */ |
||
4858 | public static function create_onboarding_token() { |
||
4866 | |||
4867 | /** |
||
4868 | * Remove the onboarding token |
||
4869 | * |
||
4870 | * @return bool True on success, false on failure |
||
4871 | */ |
||
4872 | public static function invalidate_onboarding_token() { |
||
4875 | |||
4876 | /** |
||
4877 | * Validate an onboarding token for a specific action |
||
4878 | * |
||
4879 | * @return boolean True if token/action pair is accepted, false if not |
||
4880 | */ |
||
4881 | public static function validate_onboarding_token_action( $token, $action ) { |
||
4899 | |||
4900 | /** |
||
4901 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
4902 | * |
||
4903 | * @since 2.3.3 |
||
4904 | * @return boolean |
||
4905 | */ |
||
4906 | public static function permit_ssl( $force_recheck = false ) { |
||
4948 | |||
4949 | /* |
||
4950 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
4951 | */ |
||
4952 | public function alert_auto_ssl_fail() { |
||
5001 | |||
5002 | /** |
||
5003 | * Returns the Jetpack XML-RPC API |
||
5004 | * |
||
5005 | * @return string |
||
5006 | */ |
||
5007 | public static function xmlrpc_api_url() { |
||
5011 | |||
5012 | /** |
||
5013 | * Creates two secret tokens and the end of life timestamp for them. |
||
5014 | * |
||
5015 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
5016 | * |
||
5017 | * @since 2.6 |
||
5018 | * @return array |
||
5019 | */ |
||
5020 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
5046 | |||
5047 | public static function get_secrets( $action, $user_id ) { |
||
5062 | |||
5063 | public static function delete_secrets( $action, $user_id ) { |
||
5071 | |||
5072 | /** |
||
5073 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5074 | * |
||
5075 | * Based on local php max_execution_time in php.ini |
||
5076 | * |
||
5077 | * @since 2.6 |
||
5078 | * @return int |
||
5079 | * @deprecated |
||
5080 | **/ |
||
5081 | public function get_remote_query_timeout_limit() { |
||
5085 | |||
5086 | /** |
||
5087 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5088 | * |
||
5089 | * Based on local php max_execution_time in php.ini |
||
5090 | * |
||
5091 | * @since 5.4 |
||
5092 | * @return int |
||
5093 | **/ |
||
5094 | public static function get_max_execution_time() { |
||
5103 | |||
5104 | /** |
||
5105 | * Sets a minimum request timeout, and returns the current timeout |
||
5106 | * |
||
5107 | * @since 5.4 |
||
5108 | **/ |
||
5109 | public static function set_min_time_limit( $min_timeout ) { |
||
5117 | |||
5118 | |||
5119 | /** |
||
5120 | * Takes the response from the Jetpack register new site endpoint and |
||
5121 | * verifies it worked properly. |
||
5122 | * |
||
5123 | * @since 2.6 |
||
5124 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
5125 | **/ |
||
5126 | public function validate_remote_register_response( $response ) { |
||
5166 | /** |
||
5167 | * @return bool|WP_Error |
||
5168 | */ |
||
5169 | public static function register() { |
||
5274 | |||
5275 | /** |
||
5276 | * If the db version is showing something other that what we've got now, bump it to current. |
||
5277 | * |
||
5278 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
5279 | */ |
||
5280 | public static function maybe_set_version_option() { |
||
5294 | |||
5295 | /* Client Server API */ |
||
5296 | |||
5297 | /** |
||
5298 | * Loads the Jetpack XML-RPC client |
||
5299 | */ |
||
5300 | public static function load_xml_rpc_client() { |
||
5304 | |||
5305 | /** |
||
5306 | * Resets the saved authentication state in between testing requests. |
||
5307 | */ |
||
5308 | public function reset_saved_auth_state() { |
||
5312 | |||
5313 | function verify_xml_rpc_signature() { |
||
5443 | |||
5444 | /** |
||
5445 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
5446 | */ |
||
5447 | function authenticate_jetpack( $user, $username, $password ) { |
||
5470 | |||
5471 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
5472 | // Uses the existing XMLRPC request signing implementation. |
||
5473 | function wp_rest_authenticate( $user ) { |
||
5548 | |||
5549 | /** |
||
5550 | * Report authentication status to the WP REST API. |
||
5551 | * |
||
5552 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5553 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5554 | */ |
||
5555 | public function wp_rest_authentication_errors( $value ) { |
||
5561 | |||
5562 | function add_nonce( $timestamp, $nonce ) { |
||
5600 | |||
5601 | /** |
||
5602 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5603 | * Capture it here so we can verify the signature later. |
||
5604 | */ |
||
5605 | function xmlrpc_methods( $methods ) { |
||
5609 | |||
5610 | function public_xmlrpc_methods( $methods ) { |
||
5616 | |||
5617 | function jetpack_getOptions( $args ) { |
||
5657 | |||
5658 | function xmlrpc_options( $options ) { |
||
5676 | |||
5677 | public static function clean_nonces( $all = false ) { |
||
5698 | |||
5699 | /** |
||
5700 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5701 | * SET: state( $key, $value ); |
||
5702 | * GET: $value = state( $key ); |
||
5703 | * |
||
5704 | * @param string $key |
||
5705 | * @param string $value |
||
5706 | * @param bool $restate private |
||
5707 | */ |
||
5708 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5758 | |||
5759 | public static function restate() { |
||
5762 | |||
5763 | public static function check_privacy( $file ) { |
||
5798 | |||
5799 | /** |
||
5800 | * Helper method for multicall XMLRPC. |
||
5801 | */ |
||
5802 | public static function xmlrpc_async_call() { |
||
5844 | |||
5845 | public static function staticize_subdomain( $url ) { |
||
5880 | |||
5881 | /* JSON API Authorization */ |
||
5882 | |||
5883 | /** |
||
5884 | * Handles the login action for Authorizing the JSON API |
||
5885 | */ |
||
5886 | function login_form_json_api_authorization() { |
||
5895 | |||
5896 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
5897 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
5910 | |||
5911 | // Make sure the POSTed request is handled by the same action |
||
5912 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
5916 | |||
5917 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
5918 | function store_json_api_authorization_token( $user_login, $user ) { |
||
5924 | |||
5925 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
5926 | function allow_wpcom_public_api_domain( $domains ) { |
||
5930 | |||
5931 | static function is_redirect_encoded( $redirect_url ) { |
||
5934 | |||
5935 | // Add all wordpress.com environments to the safe redirect whitelist |
||
5936 | function allow_wpcom_environments( $domains ) { |
||
5943 | |||
5944 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
5945 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
5957 | |||
5958 | |||
5959 | /** |
||
5960 | * Verifies the request by checking the signature |
||
5961 | * |
||
5962 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
5963 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
5964 | * |
||
5965 | * @param null|array $environment |
||
5966 | */ |
||
5967 | function verify_json_api_authorization_request( $environment = null ) { |
||
6071 | |||
6072 | function login_message_json_api_authorization( $message ) { |
||
6078 | |||
6079 | /** |
||
6080 | * Get $content_width, but with a <s>twist</s> filter. |
||
6081 | */ |
||
6082 | public static function get_content_width() { |
||
6093 | |||
6094 | /** |
||
6095 | * Pings the WordPress.com Mirror Site for the specified options. |
||
6096 | * |
||
6097 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
6098 | * |
||
6099 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
6100 | */ |
||
6101 | public function get_cloud_site_options( $option_names ) { |
||
6117 | |||
6118 | /** |
||
6119 | * Checks if the site is currently in an identity crisis. |
||
6120 | * |
||
6121 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
6122 | */ |
||
6123 | public static function check_identity_crisis() { |
||
6130 | |||
6131 | /** |
||
6132 | * Checks whether the home and siteurl specifically are whitelisted |
||
6133 | * Written so that we don't have re-check $key and $value params every time |
||
6134 | * we want to check if this site is whitelisted, for example in footer.php |
||
6135 | * |
||
6136 | * @since 3.8.0 |
||
6137 | * @return bool True = already whitelisted False = not whitelisted |
||
6138 | */ |
||
6139 | public static function is_staging_site() { |
||
6198 | |||
6199 | /** |
||
6200 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
6201 | * |
||
6202 | * @since 4.4.0 |
||
6203 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
6204 | * |
||
6205 | * @return bool |
||
6206 | */ |
||
6207 | public static function validate_sync_error_idc_option() { |
||
6251 | |||
6252 | /** |
||
6253 | * Normalizes a url by doing three things: |
||
6254 | * - Strips protocol |
||
6255 | * - Strips www |
||
6256 | * - Adds a trailing slash |
||
6257 | * |
||
6258 | * @since 4.4.0 |
||
6259 | * @param string $url |
||
6260 | * @return WP_Error|string |
||
6261 | */ |
||
6262 | public static function normalize_url_protocol_agnostic( $url ) { |
||
6272 | |||
6273 | /** |
||
6274 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
6275 | * |
||
6276 | * @since 4.4.0 |
||
6277 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
6278 | * |
||
6279 | * @param array $response |
||
6280 | * @return array Array of the local urls, wpcom urls, and error code |
||
6281 | */ |
||
6282 | public static function get_sync_error_idc_option( $response = array() ) { |
||
6315 | |||
6316 | /** |
||
6317 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
6318 | * If set to true, the site will be put into staging mode. |
||
6319 | * |
||
6320 | * @since 4.3.2 |
||
6321 | * @return bool |
||
6322 | */ |
||
6323 | public static function sync_idc_optin() { |
||
6341 | |||
6342 | /** |
||
6343 | * Maybe Use a .min.css stylesheet, maybe not. |
||
6344 | * |
||
6345 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
6346 | */ |
||
6347 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
6389 | |||
6390 | /** |
||
6391 | * If the asset is minified, let's flag .min as the suffix. |
||
6392 | * |
||
6393 | * Attached to `style_loader_src` filter. |
||
6394 | * |
||
6395 | * @param string $tag The tag that would link to the external asset. |
||
6396 | * @param string $handle The registered handle of the script in question. |
||
6397 | * @param string $href The url of the asset in question. |
||
6398 | */ |
||
6399 | public static function set_suffix_on_min( $src, $handle ) { |
||
6415 | |||
6416 | /** |
||
6417 | * Maybe inlines a stylesheet. |
||
6418 | * |
||
6419 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
6420 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
6421 | * |
||
6422 | * Attached to `style_loader_tag` filter. |
||
6423 | * |
||
6424 | * @param string $tag The tag that would link to the external asset. |
||
6425 | * @param string $handle The registered handle of the script in question. |
||
6426 | * |
||
6427 | * @return string |
||
6428 | */ |
||
6429 | public static function maybe_inline_style( $tag, $handle ) { |
||
6479 | |||
6480 | /** |
||
6481 | * Loads a view file from the views |
||
6482 | * |
||
6483 | * Data passed in with the $data parameter will be available in the |
||
6484 | * template file as $data['value'] |
||
6485 | * |
||
6486 | * @param string $template - Template file to load |
||
6487 | * @param array $data - Any data to pass along to the template |
||
6488 | * @return boolean - If template file was found |
||
6489 | **/ |
||
6490 | public function load_view( $template, $data = array() ) { |
||
6501 | |||
6502 | /** |
||
6503 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
6504 | */ |
||
6505 | public function deprecated_hooks() { |
||
6569 | |||
6570 | /** |
||
6571 | * Converts any url in a stylesheet, to the correct absolute url. |
||
6572 | * |
||
6573 | * Considerations: |
||
6574 | * - Normal, relative URLs `feh.png` |
||
6575 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
6576 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
6577 | * - Absolute URLs `http://domain.com/feh.png` |
||
6578 | * - Domain root relative URLs `/feh.png` |
||
6579 | * |
||
6580 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
6581 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
6582 | * |
||
6583 | * @return mixed|string |
||
6584 | */ |
||
6585 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6629 | |||
6630 | /** |
||
6631 | * This methods removes all of the registered css files on the front end |
||
6632 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6633 | * all the files into one file. |
||
6634 | * |
||
6635 | * Pros: |
||
6636 | * - Uses only ONE css asset connection instead of 15 |
||
6637 | * - Saves a minimum of 56k |
||
6638 | * - Reduces server load |
||
6639 | * - Reduces time to first painted byte |
||
6640 | * |
||
6641 | * Cons: |
||
6642 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6643 | * should not cause any issues with themes. |
||
6644 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6645 | * jetpack_implode_frontend_css filter for a workaround |
||
6646 | * |
||
6647 | * For some situations developers may wish to disable css imploding and |
||
6648 | * instead operate in legacy mode where each file loads seperately and |
||
6649 | * can be edited individually or dequeued. This can be accomplished with |
||
6650 | * the following line: |
||
6651 | * |
||
6652 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6653 | * |
||
6654 | * @since 3.2 |
||
6655 | **/ |
||
6656 | public function implode_frontend_css( $travis_test = false ) { |
||
6713 | |||
6714 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
6724 | |||
6725 | /* |
||
6726 | * Check the heartbeat data |
||
6727 | * |
||
6728 | * Organizes the heartbeat data by severity. For example, if the site |
||
6729 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
6730 | * |
||
6731 | * Data will be added to "caution" array, if it either: |
||
6732 | * - Out of date Jetpack version |
||
6733 | * - Out of date WP version |
||
6734 | * - Out of date PHP version |
||
6735 | * |
||
6736 | * $return array $filtered_data |
||
6737 | */ |
||
6738 | public static function jetpack_check_heartbeat_data() { |
||
6791 | |||
6792 | |||
6793 | /* |
||
6794 | * This method is used to organize all options that can be reset |
||
6795 | * without disconnecting Jetpack. |
||
6796 | * |
||
6797 | * It is used in class.jetpack-cli.php to reset options |
||
6798 | * |
||
6799 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
6800 | * |
||
6801 | * @return array of options to delete. |
||
6802 | */ |
||
6803 | public static function get_jetpack_options_for_reset() { |
||
6806 | |||
6807 | /** |
||
6808 | * Check if an option of a Jetpack module has been updated. |
||
6809 | * |
||
6810 | * If any module option has been updated before Jump Start has been dismissed, |
||
6811 | * update the 'jumpstart' option so we can hide Jump Start. |
||
6812 | * |
||
6813 | * @param string $option_name |
||
6814 | * |
||
6815 | * @return bool |
||
6816 | */ |
||
6817 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
6838 | |||
6839 | /* |
||
6840 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
6841 | * so we can bring them directly to their site in calypso. |
||
6842 | * |
||
6843 | * @param string | url |
||
6844 | * @return string | url without the guff |
||
6845 | */ |
||
6846 | public static function build_raw_urls( $url ) { |
||
6852 | |||
6853 | /** |
||
6854 | * Stores and prints out domains to prefetch for page speed optimization. |
||
6855 | * |
||
6856 | * @param mixed $new_urls |
||
6857 | */ |
||
6858 | public static function dns_prefetch( $new_urls = null ) { |
||
6875 | |||
6876 | public function wp_dashboard_setup() { |
||
6909 | |||
6910 | /** |
||
6911 | * @param mixed $result Value for the user's option |
||
6912 | * @return mixed |
||
6913 | */ |
||
6914 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
6939 | |||
6940 | public static function dashboard_widget() { |
||
6948 | |||
6949 | public static function dashboard_widget_footer() { |
||
6982 | |||
6983 | /** |
||
6984 | * Return string containing the Jetpack logo. |
||
6985 | * |
||
6986 | * @since 3.9.0 |
||
6987 | * |
||
6988 | * @param bool $logotype Should we use the full logotype (logo + text). Default to false. |
||
6989 | * |
||
6990 | * @return string |
||
6991 | */ |
||
6992 | public static function get_jp_emblem( $logotype = false ) { |
||
7010 | |||
7011 | /* |
||
7012 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
7013 | */ |
||
7014 | function jetpack_icon_user_connected( $columns ) { |
||
7018 | |||
7019 | /* |
||
7020 | * Show Jetpack icon if the user is linked. |
||
7021 | */ |
||
7022 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
7034 | |||
7035 | /* |
||
7036 | * Style the Jetpack user column |
||
7037 | */ |
||
7038 | function jetpack_user_col_style() { |
||
7055 | |||
7056 | /** |
||
7057 | * Checks if Akismet is active and working. |
||
7058 | * |
||
7059 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
7060 | * that implied usage of methods present since more recent version. |
||
7061 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
7062 | * |
||
7063 | * @since 5.1.0 |
||
7064 | * |
||
7065 | * @return bool True = Akismet available. False = Aksimet not available. |
||
7066 | */ |
||
7067 | public static function is_akismet_active() { |
||
7102 | |||
7103 | /** |
||
7104 | * Checks if one or more function names is in debug_backtrace |
||
7105 | * |
||
7106 | * @param $names Mixed string name of function or array of string names of functions |
||
7107 | * |
||
7108 | * @return bool |
||
7109 | */ |
||
7110 | public static function is_function_in_backtrace( $names ) { |
||
7133 | |||
7134 | /** |
||
7135 | * Given a minified path, and a non-minified path, will return |
||
7136 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
7137 | * |
||
7138 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
7139 | * root Jetpack directory. |
||
7140 | * |
||
7141 | * @since 5.6.0 |
||
7142 | * |
||
7143 | * @param string $min_path |
||
7144 | * @param string $non_min_path |
||
7145 | * @return string The URL to the file |
||
7146 | */ |
||
7147 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
7154 | |||
7155 | /** |
||
7156 | * Checks for whether Jetpack Rewind is enabled. |
||
7157 | * Will return true if the state of Rewind is anything except "unavailable". |
||
7158 | * @return bool|int|mixed |
||
7159 | */ |
||
7160 | public static function is_rewind_enabled() { |
||
7179 | |||
7180 | /** |
||
7181 | * Checks whether or not TOS has been agreed upon. |
||
7182 | * Will return true if a user has clicked to register, or is already connected. |
||
7183 | */ |
||
7184 | public static function jetpack_tos_agreed() { |
||
7187 | |||
7188 | /** |
||
7189 | * Handles activating default modules as well general cleanup for the new connection. |
||
7190 | * |
||
7191 | * @param boolean $activate_sso Whether to activate the SSO module when activating default modules. |
||
7192 | * @param boolean $redirect_on_activation_error Whether to redirect on activation error. |
||
7193 | * @param boolean $send_state_messages Whether to send state messages. |
||
7194 | * @return void |
||
7195 | */ |
||
7196 | public static function handle_post_authorization_actions( |
||
7225 | |||
7226 | /** |
||
7227 | * Returns a boolean for whether backups UI should be displayed or not. |
||
7228 | * |
||
7229 | * @return bool Should backups UI be displayed? |
||
7230 | */ |
||
7231 | public static function show_backups_ui() { |
||
7241 | |||
7242 | /* |
||
7243 | * Deprecated manage functions |
||
7244 | */ |
||
7245 | function prepare_manage_jetpack_notice() { |
||
7266 | } |
||
7267 |
If you suppress an error, we recommend checking for the error condition explicitly: