Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Jetpack { |
||
28 | public $xmlrpc_server = null; |
||
29 | |||
30 | private $xmlrpc_verification = null; |
||
31 | private $rest_authentication_status = null; |
||
32 | |||
33 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
34 | |||
35 | /** |
||
36 | * @var array The handles of styles that are concatenated into jetpack.css. |
||
37 | * |
||
38 | * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js. |
||
39 | */ |
||
40 | public $concatenated_style_handles = array( |
||
41 | 'jetpack-carousel', |
||
42 | 'grunion.css', |
||
43 | 'the-neverending-homepage', |
||
44 | 'jetpack_likes', |
||
45 | 'jetpack_related-posts', |
||
46 | 'sharedaddy', |
||
47 | 'jetpack-slideshow', |
||
48 | 'presentations', |
||
49 | 'quiz', |
||
50 | 'jetpack-subscriptions', |
||
51 | 'jetpack-responsive-videos-style', |
||
52 | 'jetpack-social-menu', |
||
53 | 'tiled-gallery', |
||
54 | 'jetpack_display_posts_widget', |
||
55 | 'gravatar-profile-widget', |
||
56 | 'goodreads-widget', |
||
57 | 'jetpack_social_media_icons_widget', |
||
58 | 'jetpack-top-posts-widget', |
||
59 | 'jetpack_image_widget', |
||
60 | 'jetpack-my-community-widget', |
||
61 | 'jetpack-authors-widget', |
||
62 | 'wordads', |
||
63 | 'eu-cookie-law-style', |
||
64 | 'flickr-widget-style', |
||
65 | 'jetpack-search-widget', |
||
66 | 'jetpack-simple-payments-widget-style', |
||
67 | 'jetpack-widget-social-icons-styles', |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * Contains all assets that have had their URL rewritten to minified versions. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | static $min_assets = array(); |
||
76 | |||
77 | public $plugins_to_deactivate = array( |
||
78 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
79 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
80 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
81 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
82 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
83 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
84 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
85 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
86 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
87 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
88 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
89 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
90 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
91 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
92 | ); |
||
93 | |||
94 | static $capability_translations = array( |
||
95 | 'administrator' => 'manage_options', |
||
96 | 'editor' => 'edit_others_posts', |
||
97 | 'author' => 'publish_posts', |
||
98 | 'contributor' => 'edit_posts', |
||
99 | 'subscriber' => 'read', |
||
100 | ); |
||
101 | |||
102 | /** |
||
103 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
104 | * if the plugins are active. Used by filter_default_modules |
||
105 | * |
||
106 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
107 | * change `module-slug` and add this to your plugin: |
||
108 | * |
||
109 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
110 | * function my_jetpack_get_default_modules( $modules ) { |
||
111 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
112 | * } |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | private $conflicting_plugins = array( |
||
117 | 'comments' => array( |
||
118 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
119 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
120 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
121 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
122 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
123 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
124 | ), |
||
125 | 'comment-likes' => array( |
||
126 | 'Epoch' => 'epoch/plugincore.php', |
||
127 | ), |
||
128 | 'contact-form' => array( |
||
129 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
130 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
131 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
132 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
133 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
134 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
135 | ), |
||
136 | 'minileven' => array( |
||
137 | 'WPtouch' => 'wptouch/wptouch.php', |
||
138 | ), |
||
139 | 'latex' => array( |
||
140 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
141 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
142 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
143 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
144 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
145 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
146 | ), |
||
147 | 'protect' => array( |
||
148 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
149 | 'Captcha' => 'captcha/captcha.php', |
||
150 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
151 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
152 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
153 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
154 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
155 | 'Security-protection' => 'security-protection/security-protection.php', |
||
156 | 'Login Security' => 'login-security/login-security.php', |
||
157 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
158 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
159 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
160 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
161 | ), |
||
162 | 'random-redirect' => array( |
||
163 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
164 | ), |
||
165 | 'related-posts' => array( |
||
166 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
167 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
168 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
169 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
170 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
171 | 'outbrain' => 'outbrain/outbrain.php', |
||
172 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
173 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
174 | ), |
||
175 | 'sharedaddy' => array( |
||
176 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
177 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
178 | 'ShareThis' => 'share-this/sharethis.php', |
||
179 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
180 | ), |
||
181 | 'seo-tools' => array( |
||
182 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
183 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
184 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
185 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
186 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
187 | ), |
||
188 | 'verification-tools' => array( |
||
189 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
190 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
191 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
192 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
193 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
194 | ), |
||
195 | 'widget-visibility' => array( |
||
196 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
197 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
198 | ), |
||
199 | 'sitemaps' => array( |
||
200 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
201 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
202 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
203 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
204 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
205 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
206 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
207 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
208 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
209 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
210 | 'Sitemap' => 'sitemap/sitemap.php', |
||
211 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
212 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
213 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
214 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
215 | ), |
||
216 | 'lazy-images' => array( |
||
217 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
218 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
219 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
220 | ), |
||
221 | ); |
||
222 | |||
223 | /** |
||
224 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
225 | * |
||
226 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
227 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
228 | * |
||
229 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
230 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
231 | */ |
||
232 | private $open_graph_conflicting_plugins = array( |
||
233 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
234 | // 2 Click Social Media Buttons |
||
235 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
236 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
237 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
238 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
239 | // Open Graph Meta Tags by Heateor |
||
240 | 'facebook/facebook.php', // Facebook (official plugin) |
||
241 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
242 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
243 | // Facebook Featured Image & OG Meta Tags |
||
244 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
245 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
246 | // Facebook Open Graph Meta Tags for WordPress |
||
247 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
248 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
249 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
250 | // Fedmich's Facebook Open Graph Meta |
||
251 | 'network-publisher/networkpub.php', // Network Publisher |
||
252 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
253 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
254 | // NextScripts SNAP |
||
255 | 'og-tags/og-tags.php', // OG Tags |
||
256 | 'opengraph/opengraph.php', // Open Graph |
||
257 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
258 | // Open Graph Protocol Framework |
||
259 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
260 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
261 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
262 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
263 | 'sharepress/sharepress.php', // SharePress |
||
264 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
265 | 'social-discussions/social-discussions.php', // Social Discussions |
||
266 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
267 | 'socialize/socialize.php', // Socialize |
||
268 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
269 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
270 | // Tweet, Like, Google +1 and Share |
||
271 | 'wordbooker/wordbooker.php', // Wordbooker |
||
272 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
273 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
274 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
275 | // WP Facebook Like Send & Open Graph Meta |
||
276 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
277 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
278 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
279 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button |
||
280 | 'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox |
||
281 | ); |
||
282 | |||
283 | /** |
||
284 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
285 | */ |
||
286 | private $twitter_cards_conflicting_plugins = array( |
||
287 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
288 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
289 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
290 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
291 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
292 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
293 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
294 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
295 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
296 | 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter |
||
297 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
298 | ); |
||
299 | |||
300 | /** |
||
301 | * Message to display in admin_notice |
||
302 | * @var string |
||
303 | */ |
||
304 | public $message = ''; |
||
305 | |||
306 | /** |
||
307 | * Error to display in admin_notice |
||
308 | * @var string |
||
309 | */ |
||
310 | public $error = ''; |
||
311 | |||
312 | /** |
||
313 | * Modules that need more privacy description. |
||
314 | * @var string |
||
315 | */ |
||
316 | public $privacy_checks = ''; |
||
317 | |||
318 | /** |
||
319 | * Stats to record once the page loads |
||
320 | * |
||
321 | * @var array |
||
322 | */ |
||
323 | public $stats = array(); |
||
324 | |||
325 | /** |
||
326 | * Jetpack_Sync object |
||
327 | */ |
||
328 | public $sync; |
||
329 | |||
330 | /** |
||
331 | * Verified data for JSON authorization request |
||
332 | */ |
||
333 | public $json_api_authorization_request = array(); |
||
334 | |||
335 | /** |
||
336 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
337 | */ |
||
338 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
339 | |||
340 | /** |
||
341 | * Holds the singleton instance of this class |
||
342 | * @since 2.3.3 |
||
343 | * @var Jetpack |
||
344 | */ |
||
345 | static $instance = false; |
||
346 | |||
347 | /** |
||
348 | * Singleton |
||
349 | * @static |
||
350 | */ |
||
351 | public static function init() { |
||
352 | if ( ! self::$instance ) { |
||
353 | self::$instance = new Jetpack; |
||
354 | |||
355 | self::$instance->plugin_upgrade(); |
||
356 | } |
||
357 | |||
358 | return self::$instance; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Must never be called statically |
||
363 | */ |
||
364 | function plugin_upgrade() { |
||
365 | if ( Jetpack::is_active() ) { |
||
366 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
367 | if ( JETPACK__VERSION != $version ) { |
||
368 | // Prevent multiple upgrades at once - only a single process should trigger |
||
369 | // an upgrade to avoid stampedes |
||
370 | if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) { |
||
371 | return; |
||
372 | } |
||
373 | |||
374 | // Set a short lock to prevent multiple instances of the upgrade |
||
375 | set_transient( self::$plugin_upgrade_lock_key, 1, 10 ); |
||
376 | |||
377 | // check which active modules actually exist and remove others from active_modules list |
||
378 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
379 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
380 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
381 | Jetpack::update_active_modules( $modules ); |
||
382 | } |
||
383 | |||
384 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
385 | |||
386 | // Upgrade to 4.3.0 |
||
387 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
388 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
389 | } |
||
390 | |||
391 | // Make sure Markdown for posts gets turned back on |
||
392 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
393 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
394 | } |
||
395 | |||
396 | if ( did_action( 'wp_loaded' ) ) { |
||
397 | self::upgrade_on_load(); |
||
398 | } else { |
||
399 | add_action( |
||
400 | 'wp_loaded', |
||
401 | array( __CLASS__, 'upgrade_on_load' ) |
||
402 | ); |
||
403 | } |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Runs upgrade routines that need to have modules loaded. |
||
410 | */ |
||
411 | static function upgrade_on_load() { |
||
412 | |||
413 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
414 | // This can happen in case Jetpack has been just upgraded and is |
||
415 | // being initialized late during the page load. In this case we wait |
||
416 | // until the next proper admin page load with Jetpack active. |
||
417 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
418 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
419 | |||
420 | return; |
||
421 | } |
||
422 | |||
423 | Jetpack::maybe_set_version_option(); |
||
424 | |||
425 | if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) { |
||
426 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
427 | } |
||
428 | |||
429 | if ( |
||
430 | class_exists( 'Jetpack_Sitemap_Manager' ) |
||
431 | && version_compare( JETPACK__VERSION, '5.3', '>=' ) |
||
432 | ) { |
||
433 | do_action( 'jetpack_sitemaps_purge_data' ); |
||
434 | } |
||
435 | |||
436 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
437 | } |
||
438 | |||
439 | function isArray( $arr ) { |
||
440 | if ( ! is_array( $arr ) ) { |
||
441 | return array(); |
||
442 | } |
||
443 | return $arr; |
||
444 | } |
||
445 | |||
446 | static function update_active_modules( $modules ) { |
||
447 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
448 | $active_modules = Jetpack::get_active_modules(); |
||
449 | $new_active_modules = array(); |
||
|
|||
450 | $new_deactive_modules = array(); |
||
451 | |||
452 | list( $modules, $current_modules, $active_modules ) = array_map( 'isArray', array( $modules, $current_modules, $active_modules ) ); |
||
453 | |||
454 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
455 | $new_deactive_modules = array_diff( $active_modules, $modules ); |
||
456 | |||
457 | |||
458 | $new_current_modules = array_diff( array_merge( $current_modules, $new_active_modules ), $new_deactive_modules ); |
||
459 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $new_current_modules ) ); |
||
460 | |||
461 | foreach( $new_active_modules as $module ) { |
||
462 | /** |
||
463 | * Fires when a specific module is activated. |
||
464 | * |
||
465 | * @since 1.9.0 |
||
466 | * |
||
467 | * @param string $module Module slug. |
||
468 | * @param boolean $success whether the module was activated. @since 4.2 |
||
469 | */ |
||
470 | do_action( 'jetpack_activate_module', $module, $success ); |
||
471 | /** |
||
472 | * Fires when a module is activated. |
||
473 | * The dynamic part of the filter, $module, is the module slug. |
||
474 | * |
||
475 | * @since 1.9.0 |
||
476 | * |
||
477 | * @param string $module Module slug. |
||
478 | */ |
||
479 | do_action( "jetpack_activate_module_$module", $module ); |
||
480 | } |
||
481 | |||
482 | foreach( $new_deactive_modules as $module ) { |
||
483 | /** |
||
484 | * Fired after a module has been deactivated. |
||
485 | * |
||
486 | * @since 4.2.0 |
||
487 | * |
||
488 | * @param string $module Module slug. |
||
489 | * @param boolean $success whether the module was deactivated. |
||
490 | */ |
||
491 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
492 | /** |
||
493 | * Fires when a module is deactivated. |
||
494 | * The dynamic part of the filter, $module, is the module slug. |
||
495 | * |
||
496 | * @since 1.9.0 |
||
497 | * |
||
498 | * @param string $module Module slug. |
||
499 | */ |
||
500 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
501 | } |
||
502 | |||
503 | return $success; |
||
504 | } |
||
505 | |||
506 | static function delete_active_modules() { |
||
507 | self::update_active_modules( array() ); |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Constructor. Initializes WordPress hooks |
||
512 | */ |
||
513 | private function __construct() { |
||
514 | /* |
||
515 | * Check for and alert any deprecated hooks |
||
516 | */ |
||
517 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
518 | |||
519 | /* |
||
520 | * Enable enhanced handling of previewing sites in Calypso |
||
521 | */ |
||
522 | if ( Jetpack::is_active() ) { |
||
523 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php'; |
||
524 | add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 ); |
||
525 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php'; |
||
526 | add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 ); |
||
527 | } |
||
528 | |||
529 | /* |
||
530 | * Load things that should only be in Network Admin. |
||
531 | * |
||
532 | * For now blow away everything else until a more full |
||
533 | * understanding of what is needed at the network level is |
||
534 | * available |
||
535 | */ |
||
536 | if ( is_multisite() ) { |
||
537 | Jetpack_Network::init(); |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Prepare Gutenberg Editor functionality |
||
542 | */ |
||
543 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php'; |
||
544 | Jetpack_Gutenberg::init(); |
||
545 | Jetpack_Gutenberg::load_independent_blocks(); |
||
546 | add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) ); |
||
547 | |||
548 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
549 | |||
550 | // Unlink user before deleting the user from .com |
||
551 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
552 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
553 | |||
554 | // Alternate XML-RPC, via ?for=jetpack&jetpack=comms |
||
555 | if ( isset( $_GET['jetpack'] ) && 'comms' == $_GET['jetpack'] && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
556 | if ( ! defined( 'XMLRPC_REQUEST' ) ) { |
||
557 | define( 'XMLRPC_REQUEST', true ); |
||
558 | } |
||
559 | |||
560 | add_action( 'template_redirect', array( $this, 'alternate_xmlrpc' ) ); |
||
561 | |||
562 | add_filter( 'xmlrpc_methods', array( $this, 'remove_non_jetpack_xmlrpc_methods' ), 1000 ); |
||
563 | } |
||
564 | |||
565 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
566 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
567 | |||
568 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
569 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
570 | |||
571 | $this->require_jetpack_authentication(); |
||
572 | |||
573 | if ( Jetpack::is_active() ) { |
||
574 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
575 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
576 | |||
577 | $signed = $this->verify_xml_rpc_signature(); |
||
578 | View Code Duplication | if ( $signed && ! is_wp_error( $signed ) ) { |
|
579 | // The actual API methods. |
||
580 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
581 | } else { |
||
582 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
583 | // active Jetpack connection, so that additional users can link their account. |
||
584 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
585 | } |
||
586 | View Code Duplication | } else { |
|
587 | // The bootstrap API methods. |
||
588 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
589 | $signed = $this->verify_xml_rpc_signature(); |
||
590 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
591 | // the jetpack Provision method is available for blog-token-signed requests |
||
592 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) ); |
||
593 | } |
||
594 | } |
||
595 | |||
596 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
597 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
598 | } elseif ( |
||
599 | is_admin() && |
||
600 | isset( $_POST['action'] ) && ( |
||
601 | 'jetpack_upload_file' == $_POST['action'] || |
||
602 | 'jetpack_update_file' == $_POST['action'] |
||
603 | ) |
||
604 | ) { |
||
605 | $this->require_jetpack_authentication(); |
||
606 | $this->add_remote_request_handlers(); |
||
607 | } else { |
||
608 | if ( Jetpack::is_active() ) { |
||
609 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
610 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
611 | } |
||
612 | } |
||
613 | |||
614 | if ( Jetpack::is_active() ) { |
||
615 | Jetpack_Heartbeat::init(); |
||
616 | if ( Jetpack::is_module_active( 'stats' ) && Jetpack::is_module_active( 'search' ) ) { |
||
617 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php'; |
||
618 | Jetpack_Search_Performance_Logger::init(); |
||
619 | } |
||
620 | } |
||
621 | |||
622 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
623 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
624 | |||
625 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
626 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
627 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
628 | } |
||
629 | |||
630 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
631 | |||
632 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
633 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
634 | |||
635 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
636 | |||
637 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
638 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
639 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
640 | |||
641 | // returns HTTPS support status |
||
642 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
643 | |||
644 | // JITM AJAX callback function |
||
645 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
646 | |||
647 | // Universal ajax callback for all tracking events triggered via js |
||
648 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
649 | |||
650 | add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) ); |
||
651 | |||
652 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
653 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
654 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
655 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
656 | |||
657 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
658 | |||
659 | /** |
||
660 | * These actions run checks to load additional files. |
||
661 | * They check for external files or plugins, so they need to run as late as possible. |
||
662 | */ |
||
663 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
664 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
665 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
666 | |||
667 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
668 | add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 ); |
||
669 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
670 | |||
671 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
672 | add_filter( 'profile_update', array( 'Jetpack', 'user_meta_cleanup' ) ); |
||
673 | |||
674 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
675 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
676 | |||
677 | // A filter to control all just in time messages |
||
678 | add_filter( 'jetpack_just_in_time_msgs', '__return_true', 9 ); |
||
679 | add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9); |
||
680 | |||
681 | // If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin. |
||
682 | // We should make sure to only do this for front end links. |
||
683 | if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { |
||
684 | add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 ); |
||
685 | add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 ); |
||
686 | |||
687 | //we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions |
||
688 | //so they point moderation links on emails to Calypso |
||
689 | jetpack_require_lib( 'functions.wp-notify' ); |
||
690 | } |
||
691 | |||
692 | // Update the Jetpack plan from API on heartbeats |
||
693 | add_action( 'jetpack_heartbeat', array( 'Jetpack_Plan', 'refresh_from_wpcom' ) ); |
||
694 | |||
695 | /** |
||
696 | * This is the hack to concatenate all css files into one. |
||
697 | * For description and reasoning see the implode_frontend_css method |
||
698 | * |
||
699 | * Super late priority so we catch all the registered styles |
||
700 | */ |
||
701 | if( !is_admin() ) { |
||
702 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
703 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
704 | } |
||
705 | |||
706 | /** |
||
707 | * These are sync actions that we need to keep track of for jitms |
||
708 | */ |
||
709 | add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 ); |
||
710 | |||
711 | // Actually push the stats on shutdown. |
||
712 | if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) { |
||
713 | add_action( 'shutdown', array( $this, 'push_stats' ) ); |
||
714 | } |
||
715 | } |
||
716 | |||
717 | /** |
||
718 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
719 | * |
||
720 | * @param $domains |
||
721 | */ |
||
722 | function add_wpcom_to_allowed_redirect_hosts( $domains ) { |
||
723 | add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_domain' ) ); |
||
724 | } |
||
725 | |||
726 | /** |
||
727 | * Return $domains, with 'wordpress.com' appended. |
||
728 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
729 | * |
||
730 | * @param $domains |
||
731 | * @return array |
||
732 | */ |
||
733 | function allow_wpcom_domain( $domains ) { |
||
734 | if ( empty( $domains ) ) { |
||
735 | $domains = array(); |
||
736 | } |
||
737 | $domains[] = 'wordpress.com'; |
||
738 | return array_unique( $domains ); |
||
739 | } |
||
740 | |||
741 | function point_edit_post_links_to_calypso( $default_url, $post_id ) { |
||
742 | $post = get_post( $post_id ); |
||
743 | |||
744 | if ( empty( $post ) ) { |
||
745 | return $default_url; |
||
746 | } |
||
747 | |||
748 | $post_type = $post->post_type; |
||
749 | |||
750 | // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso. |
||
751 | // https://en.support.wordpress.com/custom-post-types/ |
||
752 | $allowed_post_types = array( |
||
753 | 'post' => 'post', |
||
754 | 'page' => 'page', |
||
755 | 'jetpack-portfolio' => 'edit/jetpack-portfolio', |
||
756 | 'jetpack-testimonial' => 'edit/jetpack-testimonial', |
||
757 | ); |
||
758 | |||
759 | if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) { |
||
760 | return $default_url; |
||
761 | } |
||
762 | |||
763 | $path_prefix = $allowed_post_types[ $post_type ]; |
||
764 | |||
765 | $site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
766 | |||
767 | return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); |
||
768 | } |
||
769 | |||
770 | function point_edit_comment_links_to_calypso( $url ) { |
||
771 | // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID. |
||
772 | wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args ); |
||
773 | return esc_url( sprintf( 'https://wordpress.com/comment/%s/%d', |
||
774 | Jetpack::build_raw_urls( get_home_url() ), |
||
775 | $query_args['amp;c'] |
||
776 | ) ); |
||
777 | } |
||
778 | |||
779 | function jetpack_track_last_sync_callback( $params ) { |
||
780 | /** |
||
781 | * Filter to turn off jitm caching |
||
782 | * |
||
783 | * @since 5.4.0 |
||
784 | * |
||
785 | * @param bool false Whether to cache just in time messages |
||
786 | */ |
||
787 | if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
788 | return $params; |
||
789 | } |
||
790 | |||
791 | if ( is_array( $params ) && isset( $params[0] ) ) { |
||
792 | $option = $params[0]; |
||
793 | if ( 'active_plugins' === $option ) { |
||
794 | // use the cache if we can, but not terribly important if it gets evicted |
||
795 | set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS ); |
||
796 | } |
||
797 | } |
||
798 | |||
799 | return $params; |
||
800 | } |
||
801 | |||
802 | function jetpack_connection_banner_callback() { |
||
803 | check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' ); |
||
804 | |||
805 | if ( isset( $_REQUEST['dismissBanner'] ) ) { |
||
806 | Jetpack_Options::update_option( 'dismissed_connection_banner', 1 ); |
||
807 | wp_send_json_success(); |
||
808 | } |
||
809 | |||
810 | wp_die(); |
||
811 | } |
||
812 | |||
813 | /** |
||
814 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
815 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
816 | * ensure that Core and other plugins' methods are not exposed. |
||
817 | * |
||
818 | * @param array $methods |
||
819 | * @return array filtered $methods |
||
820 | */ |
||
821 | function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
822 | $jetpack_methods = array(); |
||
823 | |||
824 | foreach ( $methods as $method => $callback ) { |
||
825 | if ( 0 === strpos( $method, 'jetpack.' ) ) { |
||
826 | $jetpack_methods[ $method ] = $callback; |
||
827 | } |
||
828 | } |
||
829 | |||
830 | return $jetpack_methods; |
||
831 | } |
||
832 | |||
833 | /** |
||
834 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
835 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
836 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
837 | * which is accessible via a different URI. Most of the below is copied directly |
||
838 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
839 | */ |
||
840 | function alternate_xmlrpc() { |
||
841 | // phpcs:disable PHPCompatibility.Variables.RemovedPredefinedGlobalVariables.http_raw_post_dataDeprecatedRemoved |
||
842 | global $HTTP_RAW_POST_DATA; |
||
843 | |||
844 | // Some browser-embedded clients send cookies. We don't want them. |
||
845 | $_COOKIE = array(); |
||
846 | |||
847 | // A bug in PHP < 5.2.2 makes $HTTP_RAW_POST_DATA not set by default, |
||
848 | // but we can do it ourself. |
||
849 | if ( ! isset( $HTTP_RAW_POST_DATA ) ) { |
||
850 | $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' ); |
||
851 | } |
||
852 | |||
853 | // fix for mozBlog and other cases where '<?xml' isn't on the very first line |
||
854 | if ( isset( $HTTP_RAW_POST_DATA ) ) { |
||
855 | $HTTP_RAW_POST_DATA = trim( $HTTP_RAW_POST_DATA ); |
||
856 | } |
||
857 | |||
858 | // phpcs:enable |
||
859 | |||
860 | include_once( ABSPATH . 'wp-admin/includes/admin.php' ); |
||
861 | include_once( ABSPATH . WPINC . '/class-IXR.php' ); |
||
862 | include_once( ABSPATH . WPINC . '/class-wp-xmlrpc-server.php' ); |
||
863 | |||
864 | /** |
||
865 | * Filters the class used for handling XML-RPC requests. |
||
866 | * |
||
867 | * @since 3.1.0 |
||
868 | * |
||
869 | * @param string $class The name of the XML-RPC server class. |
||
870 | */ |
||
871 | $wp_xmlrpc_server_class = apply_filters( 'wp_xmlrpc_server_class', 'wp_xmlrpc_server' ); |
||
872 | $wp_xmlrpc_server = new $wp_xmlrpc_server_class; |
||
873 | |||
874 | // Fire off the request |
||
875 | nocache_headers(); |
||
876 | $wp_xmlrpc_server->serve_request(); |
||
877 | |||
878 | exit; |
||
879 | } |
||
880 | |||
881 | function jetpack_admin_ajax_tracks_callback() { |
||
882 | // Check for nonce |
||
883 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
884 | wp_die( 'Permissions check failed.' ); |
||
885 | } |
||
886 | |||
887 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
888 | wp_die( 'No valid event name or type.' ); |
||
889 | } |
||
890 | |||
891 | $tracks_data = array(); |
||
892 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
893 | if ( is_array( $_REQUEST['tracksEventProp'] ) ) { |
||
894 | $tracks_data = $_REQUEST['tracksEventProp']; |
||
895 | } else { |
||
896 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
897 | } |
||
898 | } |
||
899 | |||
900 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
901 | wp_send_json_success(); |
||
902 | wp_die(); |
||
903 | } |
||
904 | |||
905 | /** |
||
906 | * The callback for the JITM ajax requests. |
||
907 | */ |
||
908 | function jetpack_jitm_ajax_callback() { |
||
909 | // Check for nonce |
||
910 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
911 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
912 | } |
||
913 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
914 | $module_slug = $_REQUEST['jitmModule']; |
||
915 | Jetpack::log( 'activate', $module_slug ); |
||
916 | Jetpack::activate_module( $module_slug, false, false ); |
||
917 | Jetpack::state( 'message', 'no_message' ); |
||
918 | |||
919 | //A Jetpack module is being activated through a JITM, track it |
||
920 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
921 | $this->do_stats( 'server_side' ); |
||
922 | |||
923 | wp_send_json_success(); |
||
924 | } |
||
925 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
926 | // get the hide_jitm options array |
||
927 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
928 | $module_slug = $_REQUEST['jitmModule']; |
||
929 | |||
930 | if( ! $jetpack_hide_jitm ) { |
||
931 | $jetpack_hide_jitm = array( |
||
932 | $module_slug => 'hide' |
||
933 | ); |
||
934 | } else { |
||
935 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
936 | } |
||
937 | |||
938 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
939 | |||
940 | //jitm is being dismissed forever, track it |
||
941 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
942 | $this->do_stats( 'server_side' ); |
||
943 | |||
944 | wp_send_json_success(); |
||
945 | } |
||
946 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
947 | $module_slug = $_REQUEST['jitmModule']; |
||
948 | |||
949 | // User went to WordPress.com, track this |
||
950 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
951 | $this->do_stats( 'server_side' ); |
||
952 | |||
953 | wp_send_json_success(); |
||
954 | } |
||
955 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
956 | $track = $_REQUEST['jitmModule']; |
||
957 | |||
958 | // User is viewing JITM, track it. |
||
959 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
960 | $this->do_stats( 'server_side' ); |
||
961 | |||
962 | wp_send_json_success(); |
||
963 | } |
||
964 | } |
||
965 | |||
966 | /** |
||
967 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
968 | */ |
||
969 | function push_stats() { |
||
970 | if ( ! empty( $this->stats ) ) { |
||
971 | $this->do_stats( 'server_side' ); |
||
972 | } |
||
973 | } |
||
974 | |||
975 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
976 | switch( $cap ) { |
||
977 | case 'jetpack_connect' : |
||
978 | case 'jetpack_reconnect' : |
||
979 | if ( Jetpack::is_development_mode() ) { |
||
980 | $caps = array( 'do_not_allow' ); |
||
981 | break; |
||
982 | } |
||
983 | /** |
||
984 | * Pass through. If it's not development mode, these should match disconnect. |
||
985 | * Let users disconnect if it's development mode, just in case things glitch. |
||
986 | */ |
||
987 | case 'jetpack_disconnect' : |
||
988 | /** |
||
989 | * In multisite, can individual site admins manage their own connection? |
||
990 | * |
||
991 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
992 | */ |
||
993 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
994 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
995 | /** |
||
996 | * We need to update the option name -- it's terribly unclear which |
||
997 | * direction the override goes. |
||
998 | * |
||
999 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
1000 | */ |
||
1001 | $caps = array( 'do_not_allow' ); |
||
1002 | break; |
||
1003 | } |
||
1004 | } |
||
1005 | |||
1006 | $caps = array( 'manage_options' ); |
||
1007 | break; |
||
1008 | case 'jetpack_manage_modules' : |
||
1009 | case 'jetpack_activate_modules' : |
||
1010 | case 'jetpack_deactivate_modules' : |
||
1011 | $caps = array( 'manage_options' ); |
||
1012 | break; |
||
1013 | case 'jetpack_configure_modules' : |
||
1014 | $caps = array( 'manage_options' ); |
||
1015 | break; |
||
1016 | case 'jetpack_manage_autoupdates' : |
||
1017 | $caps = array( |
||
1018 | 'manage_options', |
||
1019 | 'update_plugins', |
||
1020 | ); |
||
1021 | break; |
||
1022 | case 'jetpack_network_admin_page': |
||
1023 | case 'jetpack_network_settings_page': |
||
1024 | $caps = array( 'manage_network_plugins' ); |
||
1025 | break; |
||
1026 | case 'jetpack_network_sites_page': |
||
1027 | $caps = array( 'manage_sites' ); |
||
1028 | break; |
||
1029 | case 'jetpack_admin_page' : |
||
1030 | if ( Jetpack::is_development_mode() ) { |
||
1031 | $caps = array( 'manage_options' ); |
||
1032 | break; |
||
1033 | } else { |
||
1034 | $caps = array( 'read' ); |
||
1035 | } |
||
1036 | break; |
||
1037 | case 'jetpack_connect_user' : |
||
1038 | if ( Jetpack::is_development_mode() ) { |
||
1039 | $caps = array( 'do_not_allow' ); |
||
1040 | break; |
||
1041 | } |
||
1042 | $caps = array( 'read' ); |
||
1043 | break; |
||
1044 | } |
||
1045 | return $caps; |
||
1046 | } |
||
1047 | |||
1048 | function require_jetpack_authentication() { |
||
1049 | // Don't let anyone authenticate |
||
1050 | $_COOKIE = array(); |
||
1051 | remove_all_filters( 'authenticate' ); |
||
1052 | remove_all_actions( 'wp_login_failed' ); |
||
1053 | |||
1054 | if ( Jetpack::is_active() ) { |
||
1055 | // Allow Jetpack authentication |
||
1056 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
1057 | } |
||
1058 | } |
||
1059 | |||
1060 | /** |
||
1061 | * Load language files |
||
1062 | * @action plugins_loaded |
||
1063 | */ |
||
1064 | public static function plugin_textdomain() { |
||
1065 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
1066 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
1067 | } |
||
1068 | |||
1069 | /** |
||
1070 | * Register assets for use in various modules and the Jetpack admin page. |
||
1071 | * |
||
1072 | * @uses wp_script_is, wp_register_script, plugins_url |
||
1073 | * @action wp_loaded |
||
1074 | * @return null |
||
1075 | */ |
||
1076 | public function register_assets() { |
||
1077 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
1078 | wp_register_script( |
||
1079 | 'spin', |
||
1080 | self::get_file_url_for_environment( '_inc/build/spin.min.js', '_inc/spin.js' ), |
||
1081 | false, |
||
1082 | '1.3' |
||
1083 | ); |
||
1084 | } |
||
1085 | |||
1086 | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
||
1087 | wp_register_script( |
||
1088 | 'jquery.spin', |
||
1089 | self::get_file_url_for_environment( '_inc/build/jquery.spin.min.js', '_inc/jquery.spin.js' ), |
||
1090 | array( 'jquery', 'spin' ), |
||
1091 | '1.3' |
||
1092 | ); |
||
1093 | } |
||
1094 | |||
1095 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
1096 | wp_register_script( |
||
1097 | 'jetpack-gallery-settings', |
||
1098 | self::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ), |
||
1099 | array( 'media-views' ), |
||
1100 | '20121225' |
||
1101 | ); |
||
1102 | } |
||
1103 | |||
1104 | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
||
1105 | wp_register_script( |
||
1106 | 'jetpack-twitter-timeline', |
||
1107 | self::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ), |
||
1108 | array( 'jquery' ), |
||
1109 | '4.0.0', |
||
1110 | true |
||
1111 | ); |
||
1112 | } |
||
1113 | |||
1114 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
1115 | wp_register_script( |
||
1116 | 'jetpack-facebook-embed', |
||
1117 | self::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ), |
||
1118 | array( 'jquery' ), |
||
1119 | null, |
||
1120 | true |
||
1121 | ); |
||
1122 | |||
1123 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
1124 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
1125 | if ( ! is_numeric( $fb_app_id ) ) { |
||
1126 | $fb_app_id = ''; |
||
1127 | } |
||
1128 | wp_localize_script( |
||
1129 | 'jetpack-facebook-embed', |
||
1130 | 'jpfbembed', |
||
1131 | array( |
||
1132 | 'appid' => $fb_app_id, |
||
1133 | 'locale' => $this->get_locale(), |
||
1134 | ) |
||
1135 | ); |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * As jetpack_register_genericons is by default fired off a hook, |
||
1140 | * the hook may have already fired by this point. |
||
1141 | * So, let's just trigger it manually. |
||
1142 | */ |
||
1143 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
1144 | jetpack_register_genericons(); |
||
1145 | |||
1146 | /** |
||
1147 | * Register the social logos |
||
1148 | */ |
||
1149 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
1150 | jetpack_register_social_logos(); |
||
1151 | |||
1152 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
1153 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
1154 | } |
||
1155 | |||
1156 | /** |
||
1157 | * Guess locale from language code. |
||
1158 | * |
||
1159 | * @param string $lang Language code. |
||
1160 | * @return string|bool |
||
1161 | */ |
||
1162 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
1163 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
1164 | return 'en_US'; |
||
1165 | } |
||
1166 | |||
1167 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
1168 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
1169 | return false; |
||
1170 | } |
||
1171 | |||
1172 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
1173 | } |
||
1174 | |||
1175 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
1176 | // WP.com: get_locale() returns 'it' |
||
1177 | $locale = GP_Locales::by_slug( $lang ); |
||
1178 | } else { |
||
1179 | // Jetpack: get_locale() returns 'it_IT'; |
||
1180 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
1181 | } |
||
1182 | |||
1183 | if ( ! $locale ) { |
||
1184 | return false; |
||
1185 | } |
||
1186 | |||
1187 | if ( empty( $locale->facebook_locale ) ) { |
||
1188 | if ( empty( $locale->wp_locale ) ) { |
||
1189 | return false; |
||
1190 | } else { |
||
1191 | // Facebook SDK is smart enough to fall back to en_US if a |
||
1192 | // locale isn't supported. Since supported Facebook locales |
||
1193 | // can fall out of sync, we'll attempt to use the known |
||
1194 | // wp_locale value and rely on said fallback. |
||
1195 | return $locale->wp_locale; |
||
1196 | } |
||
1197 | } |
||
1198 | |||
1199 | return $locale->facebook_locale; |
||
1200 | } |
||
1201 | |||
1202 | /** |
||
1203 | * Get the locale. |
||
1204 | * |
||
1205 | * @return string|bool |
||
1206 | */ |
||
1207 | function get_locale() { |
||
1208 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
1209 | |||
1210 | if ( ! $locale ) { |
||
1211 | $locale = 'en_US'; |
||
1212 | } |
||
1213 | |||
1214 | return $locale; |
||
1215 | } |
||
1216 | |||
1217 | /** |
||
1218 | * Device Pixels support |
||
1219 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
1220 | */ |
||
1221 | function devicepx() { |
||
1222 | if ( Jetpack::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) { |
||
1223 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
1224 | } |
||
1225 | } |
||
1226 | |||
1227 | /** |
||
1228 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1229 | * @param bool $option |
||
1230 | * @return string |
||
1231 | */ |
||
1232 | public function jetpack_main_network_site_option( $option ) { |
||
1233 | return network_site_url(); |
||
1234 | } |
||
1235 | /** |
||
1236 | * Network Name. |
||
1237 | */ |
||
1238 | static function network_name( $option = null ) { |
||
1239 | global $current_site; |
||
1240 | return $current_site->site_name; |
||
1241 | } |
||
1242 | /** |
||
1243 | * Does the network allow new user and site registrations. |
||
1244 | * @return string |
||
1245 | */ |
||
1246 | static function network_allow_new_registrations( $option = null ) { |
||
1247 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
1248 | } |
||
1249 | /** |
||
1250 | * Does the network allow admins to add new users. |
||
1251 | * @return boolian |
||
1252 | */ |
||
1253 | static function network_add_new_users( $option = null ) { |
||
1254 | return (bool) get_site_option( 'add_new_users' ); |
||
1255 | } |
||
1256 | /** |
||
1257 | * File upload psace left per site in MB. |
||
1258 | * -1 means NO LIMIT. |
||
1259 | * @return number |
||
1260 | */ |
||
1261 | static function network_site_upload_space( $option = null ) { |
||
1262 | // value in MB |
||
1263 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
1264 | } |
||
1265 | |||
1266 | /** |
||
1267 | * Network allowed file types. |
||
1268 | * @return string |
||
1269 | */ |
||
1270 | static function network_upload_file_types( $option = null ) { |
||
1271 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
1272 | } |
||
1273 | |||
1274 | /** |
||
1275 | * Maximum file upload size set by the network. |
||
1276 | * @return number |
||
1277 | */ |
||
1278 | static function network_max_upload_file_size( $option = null ) { |
||
1279 | // value in KB |
||
1280 | return get_site_option( 'fileupload_maxk', 300 ); |
||
1281 | } |
||
1282 | |||
1283 | /** |
||
1284 | * Lets us know if a site allows admins to manage the network. |
||
1285 | * @return array |
||
1286 | */ |
||
1287 | static function network_enable_administration_menus( $option = null ) { |
||
1288 | return get_site_option( 'menu_items' ); |
||
1289 | } |
||
1290 | |||
1291 | /** |
||
1292 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1293 | * jetpack_other_linked_admins transient. |
||
1294 | * |
||
1295 | * @since 4.3.2 |
||
1296 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1297 | * |
||
1298 | * @param int $user_id The user ID whose role changed. |
||
1299 | * @param string $role The new role. |
||
1300 | * @param array $old_roles An array of the user's previous roles. |
||
1301 | */ |
||
1302 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1303 | if ( 'administrator' == $role |
||
1304 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
1305 | || is_null( $old_roles ) |
||
1306 | ) { |
||
1307 | delete_transient( 'jetpack_other_linked_admins' ); |
||
1308 | } |
||
1309 | } |
||
1310 | |||
1311 | /** |
||
1312 | * Checks to see if there are any other users available to become primary |
||
1313 | * Users must both: |
||
1314 | * - Be linked to wpcom |
||
1315 | * - Be an admin |
||
1316 | * |
||
1317 | * @return mixed False if no other users are linked, Int if there are. |
||
1318 | */ |
||
1319 | static function get_other_linked_admins() { |
||
1320 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
1321 | |||
1322 | if ( false === $other_linked_users ) { |
||
1323 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
1324 | if ( count( $admins ) > 1 ) { |
||
1325 | $available = array(); |
||
1326 | foreach ( $admins as $admin ) { |
||
1327 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
1328 | $available[] = $admin->ID; |
||
1329 | } |
||
1330 | } |
||
1331 | |||
1332 | $count_connected_admins = count( $available ); |
||
1333 | if ( count( $available ) > 1 ) { |
||
1334 | $other_linked_users = $count_connected_admins; |
||
1335 | } else { |
||
1336 | $other_linked_users = 0; |
||
1337 | } |
||
1338 | } else { |
||
1339 | $other_linked_users = 0; |
||
1340 | } |
||
1341 | |||
1342 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
1343 | } |
||
1344 | |||
1345 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
1346 | } |
||
1347 | |||
1348 | /** |
||
1349 | * Return whether we are dealing with a multi network setup or not. |
||
1350 | * The reason we are type casting this is because we want to avoid the situation where |
||
1351 | * the result is false since when is_main_network_option return false it cases |
||
1352 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1353 | * database which could be set to anything as opposed to what this function returns. |
||
1354 | * @param bool $option |
||
1355 | * |
||
1356 | * @return boolean |
||
1357 | */ |
||
1358 | public function is_main_network_option( $option ) { |
||
1359 | // return '1' or '' |
||
1360 | return (string) (bool) Jetpack::is_multi_network(); |
||
1361 | } |
||
1362 | |||
1363 | /** |
||
1364 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1365 | * |
||
1366 | * @param string $option |
||
1367 | * @return boolean |
||
1368 | */ |
||
1369 | public function is_multisite( $option ) { |
||
1370 | return (string) (bool) is_multisite(); |
||
1371 | } |
||
1372 | |||
1373 | /** |
||
1374 | * Implemented since there is no core is multi network function |
||
1375 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1376 | * |
||
1377 | * @since 3.3 |
||
1378 | * @return boolean |
||
1379 | */ |
||
1380 | public static function is_multi_network() { |
||
1381 | global $wpdb; |
||
1382 | |||
1383 | // if we don't have a multi site setup no need to do any more |
||
1384 | if ( ! is_multisite() ) { |
||
1385 | return false; |
||
1386 | } |
||
1387 | |||
1388 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
1389 | if ( $num_sites > 1 ) { |
||
1390 | return true; |
||
1391 | } else { |
||
1392 | return false; |
||
1393 | } |
||
1394 | } |
||
1395 | |||
1396 | /** |
||
1397 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1398 | * @return null |
||
1399 | */ |
||
1400 | function update_jetpack_main_network_site_option() { |
||
1401 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1402 | } |
||
1403 | /** |
||
1404 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1405 | * |
||
1406 | */ |
||
1407 | function update_jetpack_network_settings() { |
||
1408 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1409 | // Only sync this info for the main network site. |
||
1410 | } |
||
1411 | |||
1412 | /** |
||
1413 | * Get back if the current site is single user site. |
||
1414 | * |
||
1415 | * @return bool |
||
1416 | */ |
||
1417 | public static function is_single_user_site() { |
||
1418 | global $wpdb; |
||
1419 | |||
1420 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
1421 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
1422 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
1423 | } |
||
1424 | return 1 === (int) $some_users; |
||
1425 | } |
||
1426 | |||
1427 | /** |
||
1428 | * Returns true if the site has file write access false otherwise. |
||
1429 | * @return string ( '1' | '0' ) |
||
1430 | **/ |
||
1431 | public static function file_system_write_access() { |
||
1432 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
1433 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
1434 | } |
||
1435 | |||
1436 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
1437 | |||
1438 | $filesystem_method = get_filesystem_method(); |
||
1439 | if ( $filesystem_method === 'direct' ) { |
||
1440 | return 1; |
||
1441 | } |
||
1442 | |||
1443 | ob_start(); |
||
1444 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
1445 | ob_end_clean(); |
||
1446 | if ( $filesystem_credentials_are_stored ) { |
||
1447 | return 1; |
||
1448 | } |
||
1449 | return 0; |
||
1450 | } |
||
1451 | |||
1452 | /** |
||
1453 | * Finds out if a site is using a version control system. |
||
1454 | * @return string ( '1' | '0' ) |
||
1455 | **/ |
||
1456 | public static function is_version_controlled() { |
||
1457 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
1458 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
1459 | } |
||
1460 | |||
1461 | /** |
||
1462 | * Determines whether the current theme supports featured images or not. |
||
1463 | * @return string ( '1' | '0' ) |
||
1464 | */ |
||
1465 | public static function featured_images_enabled() { |
||
1466 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1467 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
1468 | } |
||
1469 | |||
1470 | /** |
||
1471 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1472 | * |
||
1473 | * @deprecated 4.7 use get_avatar_url instead. |
||
1474 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1475 | * @param int $size Size of the avatar image |
||
1476 | * @param string $default URL to a default image to use if no avatar is available |
||
1477 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1478 | * |
||
1479 | * @return array |
||
1480 | */ |
||
1481 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1482 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
1483 | return get_avatar_url( $id_or_email, array( |
||
1484 | 'size' => $size, |
||
1485 | 'default' => $default, |
||
1486 | 'force_default' => $force_display, |
||
1487 | ) ); |
||
1488 | } |
||
1489 | |||
1490 | /** |
||
1491 | * jetpack_updates is saved in the following schema: |
||
1492 | * |
||
1493 | * array ( |
||
1494 | * 'plugins' => (int) Number of plugin updates available. |
||
1495 | * 'themes' => (int) Number of theme updates available. |
||
1496 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1497 | * 'translations' => (int) Number of translation updates available. |
||
1498 | * 'total' => (int) Total of all available updates. |
||
1499 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1500 | * ) |
||
1501 | * @return array |
||
1502 | */ |
||
1503 | public static function get_updates() { |
||
1504 | $update_data = wp_get_update_data(); |
||
1505 | |||
1506 | // Stores the individual update counts as well as the total count. |
||
1507 | if ( isset( $update_data['counts'] ) ) { |
||
1508 | $updates = $update_data['counts']; |
||
1509 | } |
||
1510 | |||
1511 | // If we need to update WordPress core, let's find the latest version number. |
||
1512 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
1513 | $cur = get_preferred_from_update_core(); |
||
1514 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
1515 | $updates['wp_update_version'] = $cur->current; |
||
1516 | } |
||
1517 | } |
||
1518 | return isset( $updates ) ? $updates : array(); |
||
1519 | } |
||
1520 | |||
1521 | public static function get_update_details() { |
||
1522 | $update_details = array( |
||
1523 | 'update_core' => get_site_transient( 'update_core' ), |
||
1524 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
1525 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
1526 | ); |
||
1527 | return $update_details; |
||
1528 | } |
||
1529 | |||
1530 | public static function refresh_update_data() { |
||
1531 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1532 | |||
1533 | } |
||
1534 | |||
1535 | public static function refresh_theme_data() { |
||
1536 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1537 | } |
||
1538 | |||
1539 | /** |
||
1540 | * Is Jetpack active? |
||
1541 | */ |
||
1542 | public static function is_active() { |
||
1543 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1544 | } |
||
1545 | |||
1546 | /** |
||
1547 | * Make an API call to WordPress.com for plan status |
||
1548 | * |
||
1549 | * @deprecated 7.2.0 Use Jetpack_Plan::refresh_from_wpcom. |
||
1550 | * |
||
1551 | * @return bool True if plan is updated, false if no update |
||
1552 | */ |
||
1553 | public static function refresh_active_plan_from_wpcom() { |
||
1554 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::refresh_from_wpcom' ); |
||
1555 | return Jetpack_Plan::refresh_from_wpcom(); |
||
1556 | } |
||
1557 | |||
1558 | /** |
||
1559 | * Get the plan that this Jetpack site is currently using |
||
1560 | * |
||
1561 | * @deprecated 7.2.0 Use Jetpack_Plan::get. |
||
1562 | * @return array Active Jetpack plan details. |
||
1563 | */ |
||
1564 | public static function get_active_plan() { |
||
1565 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::get' ); |
||
1566 | return Jetpack_Plan::get(); |
||
1567 | } |
||
1568 | |||
1569 | /** |
||
1570 | * Determine whether the active plan supports a particular feature |
||
1571 | * |
||
1572 | * @deprecated 7.2.0 Use Jetpack_Plan::supports. |
||
1573 | * @return bool True if plan supports feature, false if not. |
||
1574 | */ |
||
1575 | public static function active_plan_supports( $feature ) { |
||
1576 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::supports' ); |
||
1577 | return Jetpack_Plan::supports( $feature ); |
||
1578 | } |
||
1579 | |||
1580 | /** |
||
1581 | * Is Jetpack in development (offline) mode? |
||
1582 | */ |
||
1583 | public static function is_development_mode() { |
||
1604 | |||
1605 | /** |
||
1606 | * Whether the site is currently onboarding or not. |
||
1607 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
1608 | * |
||
1609 | * @since 5.8 |
||
1610 | * |
||
1611 | * @access public |
||
1612 | * @static |
||
1613 | * |
||
1614 | * @return bool True if the site is currently onboarding, false otherwise |
||
1615 | */ |
||
1616 | public static function is_onboarding() { |
||
1619 | |||
1620 | /** |
||
1621 | * Determines reason for Jetpack development mode. |
||
1622 | */ |
||
1623 | public static function development_mode_trigger_text() { |
||
1624 | if ( ! Jetpack::is_development_mode() ) { |
||
1625 | return __( 'Jetpack is not in Development Mode.', 'jetpack' ); |
||
1626 | } |
||
1627 | |||
1628 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
1629 | $notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' ); |
||
1630 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
1631 | $notice = __( 'The site URL lacking a dot (e.g. http://localhost).', 'jetpack' ); |
||
1632 | } else { |
||
1633 | $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' ); |
||
1634 | } |
||
1635 | |||
1636 | return $notice; |
||
1637 | |||
1638 | } |
||
1639 | /** |
||
1640 | * Get Jetpack development mode notice text and notice class. |
||
1641 | * |
||
1642 | * Mirrors the checks made in Jetpack::is_development_mode |
||
1643 | * |
||
1644 | */ |
||
1645 | public static function show_development_mode_notice() { |
||
1646 | View Code Duplication | if ( Jetpack::is_development_mode() ) { |
|
1647 | $notice = sprintf( |
||
1648 | /* translators: %s is a URL */ |
||
1649 | __( 'In <a href="%s" target="_blank">Development Mode</a>:', 'jetpack' ), |
||
1650 | 'https://jetpack.com/support/development-mode/' |
||
1651 | ); |
||
1652 | |||
1653 | $notice .= ' ' . Jetpack::development_mode_trigger_text(); |
||
1654 | |||
1655 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1656 | } |
||
1657 | |||
1658 | // Throw up a notice if using a development version and as for feedback. |
||
1659 | if ( Jetpack::is_development_version() ) { |
||
1660 | /* translators: %s is a URL */ |
||
1661 | $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/' ); |
||
1662 | |||
1663 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1664 | } |
||
1665 | // Throw up a notice if using staging mode |
||
1666 | if ( Jetpack::is_staging_site() ) { |
||
1667 | /* translators: %s is a URL */ |
||
1668 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
1669 | |||
1670 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1671 | } |
||
1672 | } |
||
1673 | |||
1674 | /** |
||
1675 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1676 | */ |
||
1677 | public static function is_development_version() { |
||
1692 | |||
1693 | /** |
||
1694 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1695 | */ |
||
1696 | public static function is_user_connected( $user_id = false ) { |
||
1704 | |||
1705 | /** |
||
1706 | * Get the wpcom user data of the current|specified connected user. |
||
1707 | */ |
||
1708 | public static function get_connected_user_data( $user_id = null ) { |
||
1732 | |||
1733 | /** |
||
1734 | * Get the wpcom email of the current|specified connected user. |
||
1735 | */ |
||
1736 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
1750 | |||
1751 | /** |
||
1752 | * Get the wpcom email of the master user. |
||
1753 | */ |
||
1754 | public static function get_master_user_email() { |
||
1761 | |||
1762 | function current_user_is_connection_owner() { |
||
1766 | |||
1767 | /** |
||
1768 | * Gets current user IP address. |
||
1769 | * |
||
1770 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
1771 | * |
||
1772 | * @return string Current user IP address. |
||
1773 | */ |
||
1774 | public static function current_user_ip( $check_all_headers = false ) { |
||
1794 | |||
1795 | /** |
||
1796 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
1797 | */ |
||
1798 | function extra_oembed_providers() { |
||
1807 | |||
1808 | /** |
||
1809 | * Synchronize connected user role changes |
||
1810 | */ |
||
1811 | function user_role_change( $user_id ) { |
||
1815 | |||
1816 | /** |
||
1817 | * Loads the currently active modules. |
||
1818 | */ |
||
1819 | public static function load_modules() { |
||
1908 | |||
1909 | /** |
||
1910 | * Check if Jetpack's REST API compat file should be included |
||
1911 | * @action plugins_loaded |
||
1912 | * @return null |
||
1913 | */ |
||
1914 | public function check_rest_api_compat() { |
||
1930 | |||
1931 | /** |
||
1932 | * Gets all plugins currently active in values, regardless of whether they're |
||
1933 | * traditionally activated or network activated. |
||
1934 | * |
||
1935 | * @todo Store the result in core's object cache maybe? |
||
1936 | */ |
||
1937 | public static function get_active_plugins() { |
||
1953 | |||
1954 | /** |
||
1955 | * Gets and parses additional plugin data to send with the heartbeat data |
||
1956 | * |
||
1957 | * @since 3.8.1 |
||
1958 | * |
||
1959 | * @return array Array of plugin data |
||
1960 | */ |
||
1961 | public static function get_parsed_plugin_data() { |
||
1982 | |||
1983 | /** |
||
1984 | * Gets and parses theme data to send with the heartbeat data |
||
1985 | * |
||
1986 | * @since 3.8.1 |
||
1987 | * |
||
1988 | * @return array Array of theme data |
||
1989 | */ |
||
1990 | public static function get_parsed_theme_data() { |
||
2012 | |||
2013 | /** |
||
2014 | * Checks whether a specific plugin is active. |
||
2015 | * |
||
2016 | * We don't want to store these in a static variable, in case |
||
2017 | * there are switch_to_blog() calls involved. |
||
2018 | */ |
||
2019 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
2022 | |||
2023 | /** |
||
2024 | * Check if Jetpack's Open Graph tags should be used. |
||
2025 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
2026 | * |
||
2027 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2028 | * @action plugins_loaded |
||
2029 | * @return null |
||
2030 | */ |
||
2031 | public function check_open_graph() { |
||
2058 | |||
2059 | /** |
||
2060 | * Check if Jetpack's Twitter tags should be used. |
||
2061 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
2062 | * |
||
2063 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2064 | * @action plugins_loaded |
||
2065 | * @return null |
||
2066 | */ |
||
2067 | public function check_twitter_tags() { |
||
2091 | |||
2092 | /** |
||
2093 | * Allows plugins to submit security reports. |
||
2094 | * |
||
2095 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
2096 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
2097 | * @param array $args See definitions above |
||
2098 | */ |
||
2099 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
2102 | |||
2103 | /* Jetpack Options API */ |
||
2104 | |||
2105 | public static function get_option_names( $type = 'compact' ) { |
||
2108 | |||
2109 | /** |
||
2110 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
2111 | * |
||
2112 | * @param string $name Option name |
||
2113 | * @param mixed $default (optional) |
||
2114 | */ |
||
2115 | public static function get_option( $name, $default = false ) { |
||
2118 | |||
2119 | /** |
||
2120 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
2121 | * |
||
2122 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
2123 | * @param string $name Option name |
||
2124 | * @param mixed $value Option value |
||
2125 | */ |
||
2126 | public static function update_option( $name, $value ) { |
||
2130 | |||
2131 | /** |
||
2132 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
2133 | * |
||
2134 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
2135 | * @param array $array array( option name => option value, ... ) |
||
2136 | */ |
||
2137 | public static function update_options( $array ) { |
||
2141 | |||
2142 | /** |
||
2143 | * Deletes the given option. May be passed multiple option names as an array. |
||
2144 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
2145 | * |
||
2146 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
2147 | * @param string|array $names |
||
2148 | */ |
||
2149 | public static function delete_option( $names ) { |
||
2153 | |||
2154 | /** |
||
2155 | * Enters a user token into the user_tokens option |
||
2156 | * |
||
2157 | * @param int $user_id |
||
2158 | * @param string $token |
||
2159 | * return bool |
||
2160 | */ |
||
2161 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
2175 | |||
2176 | /** |
||
2177 | * Returns an array of all PHP files in the specified absolute path. |
||
2178 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
2179 | * |
||
2180 | * @param string $absolute_path The absolute path of the directory to search. |
||
2181 | * @return array Array of absolute paths to the PHP files. |
||
2182 | */ |
||
2183 | public static function glob_php( $absolute_path ) { |
||
2212 | |||
2213 | public static function activate_new_modules( $redirect = false ) { |
||
2271 | |||
2272 | /** |
||
2273 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2274 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2275 | */ |
||
2276 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
2333 | |||
2334 | /** |
||
2335 | * Default modules loaded on activation. |
||
2336 | */ |
||
2337 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
2368 | |||
2369 | /** |
||
2370 | * Checks activated modules during auto-activation to determine |
||
2371 | * if any of those modules are being deprecated. If so, close |
||
2372 | * them out, and add any replacement modules. |
||
2373 | * |
||
2374 | * Runs at priority 99 by default. |
||
2375 | * |
||
2376 | * This is run late, so that it can still activate a module if |
||
2377 | * the new module is a replacement for another that the user |
||
2378 | * currently has active, even if something at the normal priority |
||
2379 | * would kibosh everything. |
||
2380 | * |
||
2381 | * @since 2.6 |
||
2382 | * @uses jetpack_get_default_modules filter |
||
2383 | * @param array $modules |
||
2384 | * @return array |
||
2385 | */ |
||
2386 | function handle_deprecated_modules( $modules ) { |
||
2412 | |||
2413 | /** |
||
2414 | * Checks activated plugins during auto-activation to determine |
||
2415 | * if any of those plugins are in the list with a corresponding module |
||
2416 | * that is not compatible with the plugin. The module will not be allowed |
||
2417 | * to auto-activate. |
||
2418 | * |
||
2419 | * @since 2.6 |
||
2420 | * @uses jetpack_get_default_modules filter |
||
2421 | * @param array $modules |
||
2422 | * @return array |
||
2423 | */ |
||
2424 | function filter_default_modules( $modules ) { |
||
2448 | |||
2449 | /** |
||
2450 | * Extract a module's slug from its full path. |
||
2451 | */ |
||
2452 | public static function get_module_slug( $file ) { |
||
2455 | |||
2456 | /** |
||
2457 | * Generate a module's path from its slug. |
||
2458 | */ |
||
2459 | public static function get_module_path( $slug ) { |
||
2462 | |||
2463 | /** |
||
2464 | * Load module data from module file. Headers differ from WordPress |
||
2465 | * plugin headers to avoid them being identified as standalone |
||
2466 | * plugins on the WordPress plugins page. |
||
2467 | */ |
||
2468 | public static function get_module( $module ) { |
||
2559 | |||
2560 | /** |
||
2561 | * Like core's get_file_data implementation, but caches the result. |
||
2562 | */ |
||
2563 | public static function get_file_data( $file, $headers ) { |
||
2591 | |||
2592 | |||
2593 | /** |
||
2594 | * Return translated module tag. |
||
2595 | * |
||
2596 | * @param string $tag Tag as it appears in each module heading. |
||
2597 | * |
||
2598 | * @return mixed |
||
2599 | */ |
||
2600 | public static function translate_module_tag( $tag ) { |
||
2603 | |||
2604 | /** |
||
2605 | * Get i18n strings as a JSON-encoded string |
||
2606 | * |
||
2607 | * @return string The locale as JSON |
||
2608 | */ |
||
2609 | public static function get_i18n_data_json() { |
||
2610 | |||
2611 | // WordPress 5.0 uses md5 hashes of file paths to associate translation |
||
2612 | // JSON files with the file they should be included for. This is an md5 |
||
2613 | // of '_inc/build/admin.js'. |
||
2614 | $path_md5 = '1bac79e646a8bf4081a5011ab72d5807'; |
||
2615 | |||
2616 | $i18n_json = |
||
2617 | JETPACK__PLUGIN_DIR |
||
2618 | . 'languages/json/jetpack-' |
||
2619 | . get_user_locale() |
||
2620 | . '-' |
||
2621 | . $path_md5 |
||
2622 | . '.json'; |
||
2623 | |||
2624 | if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
||
2625 | $locale_data = @file_get_contents( $i18n_json ); |
||
2626 | if ( $locale_data ) { |
||
2627 | return $locale_data; |
||
2628 | } |
||
2629 | } |
||
2630 | |||
2631 | // Return valid empty Jed locale |
||
2632 | return '{ "locale_data": { "messages": { "": {} } } }'; |
||
2633 | } |
||
2634 | |||
2635 | /** |
||
2636 | * Add locale data setup to wp-i18n |
||
2637 | * |
||
2638 | * Any Jetpack script that depends on wp-i18n should use this method to set up the locale. |
||
2639 | * |
||
2640 | * The locale setup depends on an adding inline script. This is error-prone and could easily |
||
2641 | * result in multiple additions of the same script when exactly 0 or 1 is desireable. |
||
2642 | * |
||
2643 | * This method provides a safe way to request the setup multiple times but add the script at |
||
2644 | * most once. |
||
2645 | * |
||
2646 | * @since 6.7.0 |
||
2647 | * |
||
2648 | * @return void |
||
2649 | */ |
||
2650 | public static function setup_wp_i18n_locale_data() { |
||
2651 | static $script_added = false; |
||
2652 | if ( ! $script_added ) { |
||
2653 | $script_added = true; |
||
2654 | wp_add_inline_script( |
||
2655 | 'wp-i18n', |
||
2656 | 'wp.i18n.setLocaleData( ' . Jetpack::get_i18n_data_json() . ', \'jetpack\' );' |
||
2657 | ); |
||
2658 | } |
||
2659 | } |
||
2660 | |||
2661 | /** |
||
2662 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2663 | * |
||
2664 | * @since 3.9.2 |
||
2665 | * |
||
2666 | * @param array $modules |
||
2667 | * |
||
2668 | * @return string|void |
||
2669 | */ |
||
2670 | public static function get_translated_modules( $modules ) { |
||
2671 | foreach ( $modules as $index => $module ) { |
||
2672 | $i18n_module = jetpack_get_module_i18n( $module['module'] ); |
||
2673 | if ( isset( $module['name'] ) ) { |
||
2674 | $modules[ $index ]['name'] = $i18n_module['name']; |
||
2675 | } |
||
2676 | if ( isset( $module['description'] ) ) { |
||
2677 | $modules[ $index ]['description'] = $i18n_module['description']; |
||
2678 | $modules[ $index ]['short_description'] = $i18n_module['description']; |
||
2679 | } |
||
2680 | } |
||
2681 | return $modules; |
||
2682 | } |
||
2683 | |||
2684 | /** |
||
2685 | * Get a list of activated modules as an array of module slugs. |
||
2686 | */ |
||
2687 | public static function get_active_modules() { |
||
2688 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
2689 | |||
2690 | if ( ! is_array( $active ) ) { |
||
2691 | $active = array(); |
||
2692 | } |
||
2693 | |||
2694 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
2695 | $active[] = 'vaultpress'; |
||
2696 | } else { |
||
2697 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
2698 | } |
||
2699 | |||
2700 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
2701 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
2702 | $active[] = 'protect'; |
||
2703 | } |
||
2704 | |||
2705 | /** |
||
2706 | * Allow filtering of the active modules. |
||
2707 | * |
||
2708 | * Gives theme and plugin developers the power to alter the modules that |
||
2709 | * are activated on the fly. |
||
2710 | * |
||
2711 | * @since 5.8.0 |
||
2712 | * |
||
2713 | * @param array $active Array of active module slugs. |
||
2714 | */ |
||
2715 | $active = apply_filters( 'jetpack_active_modules', $active ); |
||
2716 | |||
2717 | return array_unique( $active ); |
||
2718 | } |
||
2719 | |||
2720 | /** |
||
2721 | * Check whether or not a Jetpack module is active. |
||
2722 | * |
||
2723 | * @param string $module The slug of a Jetpack module. |
||
2724 | * @return bool |
||
2725 | * |
||
2726 | * @static |
||
2727 | */ |
||
2728 | public static function is_module_active( $module ) { |
||
2729 | return in_array( $module, self::get_active_modules() ); |
||
2730 | } |
||
2731 | |||
2732 | public static function is_module( $module ) { |
||
2733 | return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() ); |
||
2734 | } |
||
2735 | |||
2736 | /** |
||
2737 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2738 | * |
||
2739 | * @param bool $catch True to start catching, False to stop. |
||
2740 | * |
||
2741 | * @static |
||
2742 | */ |
||
2743 | public static function catch_errors( $catch ) { |
||
2744 | static $display_errors, $error_reporting; |
||
2745 | |||
2746 | if ( $catch ) { |
||
2747 | $display_errors = @ini_set( 'display_errors', 1 ); |
||
2748 | $error_reporting = @error_reporting( E_ALL ); |
||
2749 | add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
2750 | } else { |
||
2751 | @ini_set( 'display_errors', $display_errors ); |
||
2752 | @error_reporting( $error_reporting ); |
||
2753 | remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
2754 | } |
||
2755 | } |
||
2756 | |||
2757 | /** |
||
2758 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2759 | */ |
||
2760 | public static function catch_errors_on_shutdown() { |
||
2761 | Jetpack::state( 'php_errors', self::alias_directories( ob_get_clean() ) ); |
||
2762 | } |
||
2763 | |||
2764 | /** |
||
2765 | * Rewrite any string to make paths easier to read. |
||
2766 | * |
||
2767 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2768 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2769 | * |
||
2770 | * @param $string |
||
2771 | * @return mixed |
||
2772 | */ |
||
2773 | public static function alias_directories( $string ) { |
||
2774 | // ABSPATH has a trailing slash. |
||
2775 | $string = str_replace( ABSPATH, 'ABSPATH/', $string ); |
||
2776 | // WP_CONTENT_DIR does not have a trailing slash. |
||
2777 | $string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string ); |
||
2778 | |||
2779 | return $string; |
||
2780 | } |
||
2781 | |||
2782 | public static function activate_default_modules( |
||
2783 | $min_version = false, |
||
2784 | $max_version = false, |
||
2785 | $other_modules = array(), |
||
2786 | $redirect = true, |
||
2787 | $send_state_messages = true |
||
2788 | ) { |
||
2789 | $jetpack = Jetpack::init(); |
||
2790 | |||
2791 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
2792 | $modules = array_merge( $other_modules, $modules ); |
||
2793 | |||
2794 | // Look for standalone plugins and disable if active. |
||
2795 | |||
2796 | $to_deactivate = array(); |
||
2797 | foreach ( $modules as $module ) { |
||
2798 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
2799 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
2800 | } |
||
2801 | } |
||
2802 | |||
2803 | $deactivated = array(); |
||
2804 | foreach ( $to_deactivate as $module => $deactivate_me ) { |
||
2805 | list( $probable_file, $probable_title ) = $deactivate_me; |
||
2806 | if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { |
||
2807 | $deactivated[] = $module; |
||
2808 | } |
||
2809 | } |
||
2810 | |||
2811 | if ( $deactivated && $redirect ) { |
||
2812 | Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) ); |
||
2813 | |||
2814 | $url = add_query_arg( |
||
2815 | array( |
||
2816 | 'action' => 'activate_default_modules', |
||
2817 | '_wpnonce' => wp_create_nonce( 'activate_default_modules' ), |
||
2818 | ), |
||
2819 | add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) |
||
2820 | ); |
||
2821 | wp_safe_redirect( $url ); |
||
2822 | exit; |
||
2823 | } |
||
2824 | |||
2825 | /** |
||
2826 | * Fires before default modules are activated. |
||
2827 | * |
||
2828 | * @since 1.9.0 |
||
2829 | * |
||
2830 | * @param string $min_version Minimum version number required to use modules. |
||
2831 | * @param string $max_version Maximum version number required to use modules. |
||
2832 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
2833 | */ |
||
2834 | do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
2835 | |||
2836 | // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating |
||
2837 | if ( $send_state_messages ) { |
||
2838 | Jetpack::restate(); |
||
2839 | Jetpack::catch_errors( true ); |
||
2840 | } |
||
2841 | |||
2842 | $active = Jetpack::get_active_modules(); |
||
2843 | |||
2844 | foreach ( $modules as $module ) { |
||
2845 | if ( did_action( "jetpack_module_loaded_$module" ) ) { |
||
2846 | $active[] = $module; |
||
2847 | self::update_active_modules( $active ); |
||
2848 | continue; |
||
2849 | } |
||
2850 | |||
2851 | if ( $send_state_messages && in_array( $module, $active ) ) { |
||
2852 | $module_info = Jetpack::get_module( $module ); |
||
2853 | View Code Duplication | if ( ! $module_info['deactivate'] ) { |
|
2854 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
2855 | if ( $active_state = Jetpack::state( $state ) ) { |
||
2856 | $active_state = explode( ',', $active_state ); |
||
2857 | } else { |
||
2858 | $active_state = array(); |
||
2859 | } |
||
2860 | $active_state[] = $module; |
||
2861 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
2862 | } |
||
2863 | continue; |
||
2864 | } |
||
2865 | |||
2866 | $file = Jetpack::get_module_path( $module ); |
||
2867 | if ( ! file_exists( $file ) ) { |
||
2868 | continue; |
||
2869 | } |
||
2870 | |||
2871 | // we'll override this later if the plugin can be included without fatal error |
||
2872 | if ( $redirect ) { |
||
2873 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
2874 | } |
||
2875 | |||
2876 | if ( $send_state_messages ) { |
||
2877 | Jetpack::state( 'error', 'module_activation_failed' ); |
||
2878 | Jetpack::state( 'module', $module ); |
||
2879 | } |
||
2880 | |||
2881 | ob_start(); |
||
2882 | require_once $file; |
||
2883 | |||
2884 | $active[] = $module; |
||
2885 | |||
2886 | View Code Duplication | if ( $send_state_messages ) { |
|
2887 | |||
2888 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
2889 | if ( $active_state = Jetpack::state( $state ) ) { |
||
2890 | $active_state = explode( ',', $active_state ); |
||
2891 | } else { |
||
2892 | $active_state = array(); |
||
2893 | } |
||
2894 | $active_state[] = $module; |
||
2895 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
2896 | } |
||
2897 | |||
2898 | Jetpack::update_active_modules( $active ); |
||
2899 | |||
2900 | ob_end_clean(); |
||
2901 | } |
||
2902 | |||
2903 | if ( $send_state_messages ) { |
||
2904 | Jetpack::state( 'error', false ); |
||
2905 | Jetpack::state( 'module', false ); |
||
2906 | } |
||
2907 | |||
2908 | Jetpack::catch_errors( false ); |
||
2909 | /** |
||
2910 | * Fires when default modules are activated. |
||
2911 | * |
||
2912 | * @since 1.9.0 |
||
2913 | * |
||
2914 | * @param string $min_version Minimum version number required to use modules. |
||
2915 | * @param string $max_version Maximum version number required to use modules. |
||
2916 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
2917 | */ |
||
2918 | do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
2919 | } |
||
2920 | |||
2921 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
2922 | /** |
||
2923 | * Fires before a module is activated. |
||
2924 | * |
||
2925 | * @since 2.6.0 |
||
2926 | * |
||
2927 | * @param string $module Module slug. |
||
2928 | * @param bool $exit Should we exit after the module has been activated. Default to true. |
||
2929 | * @param bool $redirect Should the user be redirected after module activation? Default to true. |
||
2930 | */ |
||
2931 | do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); |
||
2932 | |||
2933 | $jetpack = Jetpack::init(); |
||
2934 | |||
2935 | if ( ! strlen( $module ) ) |
||
2936 | return false; |
||
2937 | |||
2938 | if ( ! Jetpack::is_module( $module ) ) |
||
2939 | return false; |
||
2940 | |||
2941 | // If it's already active, then don't do it again |
||
2942 | $active = Jetpack::get_active_modules(); |
||
2943 | foreach ( $active as $act ) { |
||
2944 | if ( $act == $module ) |
||
2945 | return true; |
||
2946 | } |
||
2947 | |||
2948 | $module_data = Jetpack::get_module( $module ); |
||
2949 | |||
2950 | if ( ! Jetpack::is_active() ) { |
||
2951 | if ( ! Jetpack::is_development_mode() && ! Jetpack::is_onboarding() ) |
||
2952 | return false; |
||
2953 | |||
2954 | // If we're not connected but in development mode, make sure the module doesn't require a connection |
||
2955 | if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) |
||
2956 | return false; |
||
2957 | } |
||
2958 | |||
2959 | // Check and see if the old plugin is active |
||
2960 | if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { |
||
2961 | // Deactivate the old plugin |
||
2962 | if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { |
||
2963 | // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module |
||
2964 | // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. |
||
2965 | Jetpack::state( 'deactivated_plugins', $module ); |
||
2966 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
||
2967 | exit; |
||
2968 | } |
||
2969 | } |
||
2970 | |||
2971 | // Protect won't work with mis-configured IPs |
||
2972 | if ( 'protect' === $module ) { |
||
2973 | include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; |
||
2974 | if ( ! jetpack_protect_get_ip() ) { |
||
2975 | Jetpack::state( 'message', 'protect_misconfigured_ip' ); |
||
2976 | return false; |
||
2977 | } |
||
2978 | } |
||
2979 | |||
2980 | if ( ! Jetpack_Plan::supports( $module ) ) { |
||
2981 | return false; |
||
2982 | } |
||
2983 | |||
2984 | // Check the file for fatal errors, a la wp-admin/plugins.php::activate |
||
2985 | Jetpack::state( 'module', $module ); |
||
2986 | Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error |
||
2987 | |||
2988 | Jetpack::catch_errors( true ); |
||
2989 | ob_start(); |
||
2990 | require Jetpack::get_module_path( $module ); |
||
2991 | /** This action is documented in class.jetpack.php */ |
||
2992 | do_action( 'jetpack_activate_module', $module ); |
||
2993 | $active[] = $module; |
||
2994 | Jetpack::update_active_modules( $active ); |
||
2995 | |||
2996 | Jetpack::state( 'error', false ); // the override |
||
2997 | ob_end_clean(); |
||
2998 | Jetpack::catch_errors( false ); |
||
2999 | |||
3000 | if ( $redirect ) { |
||
3001 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
3002 | } |
||
3003 | if ( $exit ) { |
||
3004 | exit; |
||
3005 | } |
||
3006 | return true; |
||
3007 | } |
||
3008 | |||
3009 | function activate_module_actions( $module ) { |
||
3010 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
3011 | } |
||
3012 | |||
3013 | public static function deactivate_module( $module ) { |
||
3014 | /** |
||
3015 | * Fires when a module is deactivated. |
||
3016 | * |
||
3017 | * @since 1.9.0 |
||
3018 | * |
||
3019 | * @param string $module Module slug. |
||
3020 | */ |
||
3021 | do_action( 'jetpack_pre_deactivate_module', $module ); |
||
3022 | |||
3023 | $jetpack = Jetpack::init(); |
||
3024 | |||
3025 | $active = Jetpack_Options::get_option( 'active_modules', array() ); |
||
3026 | $new = array_filter( array_diff( $active, (array) $module ) ); |
||
3027 | |||
3028 | return self::update_active_modules( $new ); |
||
3029 | } |
||
3030 | |||
3031 | public static function enable_module_configurable( $module ) { |
||
3032 | $module = Jetpack::get_module_slug( $module ); |
||
3033 | add_filter( 'jetpack_module_configurable_' . $module, '__return_true' ); |
||
3034 | } |
||
3035 | |||
3036 | /** |
||
3037 | * Composes a module configure URL. It uses Jetpack settings search as default value |
||
3038 | * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter |
||
3039 | * |
||
3040 | * @param string $module Module slug |
||
3041 | * @return string $url module configuration URL |
||
3042 | */ |
||
3043 | public static function module_configuration_url( $module ) { |
||
3044 | $module = Jetpack::get_module_slug( $module ); |
||
3045 | $default_url = Jetpack::admin_url() . "#/settings?term=$module"; |
||
3046 | /** |
||
3047 | * Allows to modify configure_url of specific module to be able to redirect to some custom location. |
||
3048 | * |
||
3049 | * @since 6.9.0 |
||
3050 | * |
||
3051 | * @param string $default_url Default url, which redirects to jetpack settings page. |
||
3052 | */ |
||
3053 | $url = apply_filters( 'jetpack_module_configuration_url_' . $module, $default_url ); |
||
3054 | |||
3055 | return $url; |
||
3056 | } |
||
3057 | |||
3058 | /* Installation */ |
||
3059 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
3060 | ?> |
||
3061 | <!doctype html> |
||
3062 | <html> |
||
3063 | <head> |
||
3064 | <meta charset="<?php bloginfo( 'charset' ); ?>"> |
||
3065 | <style> |
||
3066 | * { |
||
3067 | text-align: center; |
||
3068 | margin: 0; |
||
3069 | padding: 0; |
||
3070 | font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; |
||
3071 | } |
||
3072 | p { |
||
3073 | margin-top: 1em; |
||
3074 | font-size: 18px; |
||
3075 | } |
||
3076 | </style> |
||
3077 | <body> |
||
3078 | <p><?php echo esc_html( $message ); ?></p> |
||
3079 | </body> |
||
3080 | </html> |
||
3081 | <?php |
||
3082 | if ( $deactivate ) { |
||
3083 | $plugins = get_option( 'active_plugins' ); |
||
3084 | $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ); |
||
3085 | $update = false; |
||
3086 | foreach ( $plugins as $i => $plugin ) { |
||
3087 | if ( $plugin === $jetpack ) { |
||
3088 | $plugins[$i] = false; |
||
3089 | $update = true; |
||
3090 | } |
||
3091 | } |
||
3092 | |||
3093 | if ( $update ) { |
||
3094 | update_option( 'active_plugins', array_filter( $plugins ) ); |
||
3095 | } |
||
3096 | } |
||
3097 | exit; |
||
3098 | } |
||
3099 | |||
3100 | /** |
||
3101 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
3102 | * @static |
||
3103 | */ |
||
3104 | public static function plugin_activation( $network_wide ) { |
||
3105 | Jetpack_Options::update_option( 'activated', 1 ); |
||
3106 | |||
3107 | if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) { |
||
3108 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) ); |
||
3109 | } |
||
3110 | |||
3111 | if ( $network_wide ) |
||
3112 | Jetpack::state( 'network_nag', true ); |
||
3113 | |||
3114 | // For firing one-off events (notices) immediately after activation |
||
3115 | set_transient( 'activated_jetpack', true, .1 * MINUTE_IN_SECONDS ); |
||
3116 | |||
3117 | update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) ); |
||
3118 | |||
3119 | Jetpack::plugin_initialize(); |
||
3120 | } |
||
3121 | |||
3122 | public static function get_activation_source( $referer_url ) { |
||
3123 | |||
3124 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
3125 | return array( 'wp-cli', null ); |
||
3126 | } |
||
3127 | |||
3128 | $referer = parse_url( $referer_url ); |
||
3129 | |||
3130 | $source_type = 'unknown'; |
||
3131 | $source_query = null; |
||
3132 | |||
3133 | if ( ! is_array( $referer ) ) { |
||
3134 | return array( $source_type, $source_query ); |
||
3135 | } |
||
3136 | |||
3137 | $plugins_path = parse_url( admin_url( 'plugins.php' ), PHP_URL_PATH ); |
||
3138 | $plugins_install_path = parse_url( admin_url( 'plugin-install.php' ), PHP_URL_PATH );// /wp-admin/plugin-install.php |
||
3139 | |||
3140 | if ( isset( $referer['query'] ) ) { |
||
3141 | parse_str( $referer['query'], $query_parts ); |
||
3142 | } else { |
||
3143 | $query_parts = array(); |
||
3144 | } |
||
3145 | |||
3146 | if ( $plugins_path === $referer['path'] ) { |
||
3147 | $source_type = 'list'; |
||
3148 | } elseif ( $plugins_install_path === $referer['path'] ) { |
||
3149 | $tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured'; |
||
3150 | switch( $tab ) { |
||
3151 | case 'popular': |
||
3152 | $source_type = 'popular'; |
||
3153 | break; |
||
3154 | case 'recommended': |
||
3155 | $source_type = 'recommended'; |
||
3156 | break; |
||
3157 | case 'favorites': |
||
3158 | $source_type = 'favorites'; |
||
3159 | break; |
||
3160 | case 'search': |
||
3161 | $source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' ); |
||
3162 | $source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null; |
||
3163 | break; |
||
3164 | default: |
||
3165 | $source_type = 'featured'; |
||
3166 | } |
||
3167 | } |
||
3168 | |||
3169 | return array( $source_type, $source_query ); |
||
3170 | } |
||
3171 | |||
3172 | /** |
||
3173 | * Runs before bumping version numbers up to a new version |
||
3174 | * @param string $version Version:timestamp |
||
3175 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
3176 | * @return null [description] |
||
3177 | */ |
||
3178 | public static function do_version_bump( $version, $old_version ) { |
||
3179 | if ( ! $old_version ) { // For new sites |
||
3180 | // There used to be stuff here, but this seems like it might be useful to someone in the future... |
||
3181 | } |
||
3182 | } |
||
3183 | |||
3184 | /** |
||
3185 | * Sets the internal version number and activation state. |
||
3186 | * @static |
||
3187 | */ |
||
3188 | public static function plugin_initialize() { |
||
3189 | if ( ! Jetpack_Options::get_option( 'activated' ) ) { |
||
3190 | Jetpack_Options::update_option( 'activated', 2 ); |
||
3191 | } |
||
3192 | |||
3193 | View Code Duplication | if ( ! Jetpack_Options::get_option( 'version' ) ) { |
|
3194 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
3195 | /** This action is documented in class.jetpack.php */ |
||
3196 | do_action( 'updating_jetpack_version', $version, false ); |
||
3197 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
3198 | } |
||
3199 | |||
3200 | Jetpack::load_modules(); |
||
3201 | |||
3202 | Jetpack_Options::delete_option( 'do_activate' ); |
||
3203 | Jetpack_Options::delete_option( 'dismissed_connection_banner' ); |
||
3204 | } |
||
3205 | |||
3206 | /** |
||
3207 | * Removes all connection options |
||
3208 | * @static |
||
3209 | */ |
||
3210 | public static function plugin_deactivation( ) { |
||
3211 | require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
||
3212 | if( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
3213 | Jetpack_Network::init()->deactivate(); |
||
3214 | } else { |
||
3215 | Jetpack::disconnect( false ); |
||
3216 | //Jetpack_Heartbeat::init()->deactivate(); |
||
3217 | } |
||
3218 | } |
||
3219 | |||
3220 | /** |
||
3221 | * Disconnects from the Jetpack servers. |
||
3222 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
3223 | * @static |
||
3224 | */ |
||
3225 | public static function disconnect( $update_activated_state = true ) { |
||
3226 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
3227 | Jetpack::clean_nonces( true ); |
||
3228 | |||
3229 | // If the site is in an IDC because sync is not allowed, |
||
3230 | // let's make sure to not disconnect the production site. |
||
3231 | if ( ! self::validate_sync_error_idc_option() ) { |
||
3232 | JetpackTracking::record_user_event( 'disconnect_site', array() ); |
||
3233 | Jetpack::load_xml_rpc_client(); |
||
3234 | $xml = new Jetpack_IXR_Client(); |
||
3235 | $xml->query( 'jetpack.deregister' ); |
||
3236 | } |
||
3237 | |||
3238 | Jetpack_Options::delete_option( |
||
3239 | array( |
||
3240 | 'blog_token', |
||
3241 | 'user_token', |
||
3242 | 'user_tokens', |
||
3243 | 'master_user', |
||
3244 | 'time_diff', |
||
3245 | 'fallback_no_verify_ssl_certs', |
||
3246 | ) |
||
3247 | ); |
||
3248 | |||
3249 | Jetpack_IDC::clear_all_idc_options(); |
||
3250 | Jetpack_Options::delete_raw_option( 'jetpack_secrets' ); |
||
3251 | |||
3252 | if ( $update_activated_state ) { |
||
3253 | Jetpack_Options::update_option( 'activated', 4 ); |
||
3254 | } |
||
3255 | |||
3256 | if ( $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ) ) { |
||
3257 | // Check then record unique disconnection if site has never been disconnected previously |
||
3258 | if ( - 1 == $jetpack_unique_connection['disconnected'] ) { |
||
3259 | $jetpack_unique_connection['disconnected'] = 1; |
||
3260 | } else { |
||
3261 | if ( 0 == $jetpack_unique_connection['disconnected'] ) { |
||
3262 | //track unique disconnect |
||
3263 | $jetpack = Jetpack::init(); |
||
3264 | |||
3265 | $jetpack->stat( 'connections', 'unique-disconnect' ); |
||
3266 | $jetpack->do_stats( 'server_side' ); |
||
3267 | } |
||
3268 | // increment number of times disconnected |
||
3269 | $jetpack_unique_connection['disconnected'] += 1; |
||
3270 | } |
||
3271 | |||
3272 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
3273 | } |
||
3274 | |||
3275 | // Delete cached connected user data |
||
3276 | $transient_key = "jetpack_connected_user_data_" . get_current_user_id(); |
||
3277 | delete_transient( $transient_key ); |
||
3278 | |||
3279 | // Delete all the sync related data. Since it could be taking up space. |
||
3280 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
||
3281 | Jetpack_Sync_Sender::get_instance()->uninstall(); |
||
3282 | |||
3283 | // Disable the Heartbeat cron |
||
3284 | Jetpack_Heartbeat::init()->deactivate(); |
||
3285 | } |
||
3286 | |||
3287 | /** |
||
3288 | * Unlinks the current user from the linked WordPress.com user |
||
3289 | */ |
||
3290 | public static function unlink_user( $user_id = null ) { |
||
3291 | if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) ) |
||
3292 | return false; |
||
3293 | |||
3294 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
3295 | |||
3296 | if ( Jetpack_Options::get_option( 'master_user' ) == $user_id ) |
||
3297 | return false; |
||
3298 | |||
3299 | if ( ! isset( $tokens[ $user_id ] ) ) |
||
3300 | return false; |
||
3301 | |||
3302 | Jetpack::load_xml_rpc_client(); |
||
3303 | $xml = new Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
3304 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
3305 | |||
3306 | unset( $tokens[ $user_id ] ); |
||
3307 | |||
3308 | Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
3309 | |||
3310 | /** |
||
3311 | * Fires after the current user has been unlinked from WordPress.com. |
||
3312 | * |
||
3313 | * @since 4.1.0 |
||
3314 | * |
||
3315 | * @param int $user_id The current user's ID. |
||
3316 | */ |
||
3317 | do_action( 'jetpack_unlinked_user', $user_id ); |
||
3318 | |||
3319 | return true; |
||
3320 | } |
||
3321 | |||
3322 | /** |
||
3323 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
3324 | */ |
||
3325 | public static function try_registration() { |
||
3326 | // The user has agreed to the TOS at some point by now. |
||
3327 | Jetpack_Options::update_option( 'tos_agreed', true ); |
||
3328 | |||
3329 | // Let's get some testing in beta versions and such. |
||
3330 | if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) { |
||
3331 | // Before attempting to connect, let's make sure that the domains are viable. |
||
3332 | $domains_to_check = array_unique( array( |
||
3333 | 'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ), |
||
3334 | 'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ), |
||
3335 | ) ); |
||
3336 | foreach ( $domains_to_check as $domain ) { |
||
3337 | $result = Jetpack_Data::is_usable_domain( $domain ); |
||
3338 | if ( is_wp_error( $result ) ) { |
||
3339 | return $result; |
||
3340 | } |
||
3341 | } |
||
3342 | } |
||
3343 | |||
3344 | $result = Jetpack::register(); |
||
3345 | |||
3346 | // If there was an error with registration and the site was not registered, record this so we can show a message. |
||
3347 | if ( ! $result || is_wp_error( $result ) ) { |
||
3348 | return $result; |
||
3349 | } else { |
||
3350 | return true; |
||
3351 | } |
||
3352 | } |
||
3353 | |||
3354 | /** |
||
3355 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3356 | * |
||
3357 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3358 | */ |
||
3359 | public static function log( $code, $data = null ) { |
||
3360 | // only grab the latest 200 entries |
||
3361 | $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 ); |
||
3362 | |||
3363 | // Append our event to the log |
||
3364 | $log_entry = array( |
||
3365 | 'time' => time(), |
||
3366 | 'user_id' => get_current_user_id(), |
||
3367 | 'blog_id' => Jetpack_Options::get_option( 'id' ), |
||
3368 | 'code' => $code, |
||
3369 | ); |
||
3370 | // Don't bother storing it unless we've got some. |
||
3371 | if ( ! is_null( $data ) ) { |
||
3372 | $log_entry['data'] = $data; |
||
3373 | } |
||
3374 | $log[] = $log_entry; |
||
3375 | |||
3376 | // Try add_option first, to make sure it's not autoloaded. |
||
3377 | // @todo: Add an add_option method to Jetpack_Options |
||
3378 | if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) { |
||
3379 | Jetpack_Options::update_option( 'log', $log ); |
||
3380 | } |
||
3381 | |||
3382 | /** |
||
3383 | * Fires when Jetpack logs an internal event. |
||
3384 | * |
||
3385 | * @since 3.0.0 |
||
3386 | * |
||
3387 | * @param array $log_entry { |
||
3388 | * Array of details about the log entry. |
||
3389 | * |
||
3390 | * @param string time Time of the event. |
||
3391 | * @param int user_id ID of the user who trigerred the event. |
||
3392 | * @param int blog_id Jetpack Blog ID. |
||
3393 | * @param string code Unique name for the event. |
||
3394 | * @param string data Data about the event. |
||
3395 | * } |
||
3396 | */ |
||
3397 | do_action( 'jetpack_log_entry', $log_entry ); |
||
3398 | } |
||
3399 | |||
3400 | /** |
||
3401 | * Get the internal event log. |
||
3402 | * |
||
3403 | * @param $event (string) - only return the specific log events |
||
3404 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3405 | * |
||
3406 | * @return array of log events || WP_Error for invalid params |
||
3407 | */ |
||
3408 | public static function get_log( $event = false, $num = false ) { |
||
3409 | if ( $event && ! is_string( $event ) ) { |
||
3410 | return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) ); |
||
3411 | } |
||
3412 | |||
3413 | if ( $num && ! is_numeric( $num ) ) { |
||
3414 | return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) ); |
||
3415 | } |
||
3416 | |||
3417 | $entire_log = Jetpack_Options::get_option( 'log', array() ); |
||
3418 | |||
3419 | // If nothing set - act as it did before, otherwise let's start customizing the output |
||
3420 | if ( ! $num && ! $event ) { |
||
3421 | return $entire_log; |
||
3422 | } else { |
||
3423 | $entire_log = array_reverse( $entire_log ); |
||
3424 | } |
||
3425 | |||
3426 | $custom_log_output = array(); |
||
3427 | |||
3428 | if ( $event ) { |
||
3429 | foreach ( $entire_log as $log_event ) { |
||
3430 | if ( $event == $log_event[ 'code' ] ) { |
||
3431 | $custom_log_output[] = $log_event; |
||
3432 | } |
||
3433 | } |
||
3434 | } else { |
||
3435 | $custom_log_output = $entire_log; |
||
3436 | } |
||
3437 | |||
3438 | if ( $num ) { |
||
3439 | $custom_log_output = array_slice( $custom_log_output, 0, $num ); |
||
3440 | } |
||
3441 | |||
3442 | return $custom_log_output; |
||
3443 | } |
||
3444 | |||
3445 | /** |
||
3446 | * Log modification of important settings. |
||
3447 | */ |
||
3448 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3449 | switch( $option ) { |
||
3450 | case 'jetpack_sync_non_public_post_stati': |
||
3451 | self::log( $option, $value ); |
||
3452 | break; |
||
3453 | } |
||
3454 | } |
||
3455 | |||
3456 | /** |
||
3457 | * Return stat data for WPCOM sync |
||
3458 | */ |
||
3459 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3460 | $data = Jetpack_Heartbeat::generate_stats_array(); |
||
3461 | |||
3462 | if ( $extended ) { |
||
3463 | $additional_data = self::get_additional_stat_data(); |
||
3464 | $data = array_merge( $data, $additional_data ); |
||
3465 | } |
||
3466 | |||
3467 | if ( $encode ) { |
||
3468 | return json_encode( $data ); |
||
3469 | } |
||
3470 | |||
3471 | return $data; |
||
3472 | } |
||
3473 | |||
3474 | /** |
||
3475 | * Get additional stat data to sync to WPCOM |
||
3476 | */ |
||
3477 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3478 | $return["{$prefix}themes"] = Jetpack::get_parsed_theme_data(); |
||
3479 | $return["{$prefix}plugins-extra"] = Jetpack::get_parsed_plugin_data(); |
||
3480 | $return["{$prefix}users"] = (int) Jetpack::get_site_user_count(); |
||
3481 | $return["{$prefix}site-count"] = 0; |
||
3482 | |||
3483 | if ( function_exists( 'get_blog_count' ) ) { |
||
3484 | $return["{$prefix}site-count"] = get_blog_count(); |
||
3485 | } |
||
3486 | return $return; |
||
3487 | } |
||
3488 | |||
3489 | private static function get_site_user_count() { |
||
3490 | global $wpdb; |
||
3491 | |||
3492 | if ( function_exists( 'wp_is_large_network' ) ) { |
||
3493 | if ( wp_is_large_network( 'users' ) ) { |
||
3494 | return -1; // Not a real value but should tell us that we are dealing with a large network. |
||
3495 | } |
||
3496 | } |
||
3497 | View Code Duplication | if ( false === ( $user_count = get_transient( 'jetpack_site_user_count' ) ) ) { |
|
3498 | // It wasn't there, so regenerate the data and save the transient |
||
3499 | $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities'" ); |
||
3500 | set_transient( 'jetpack_site_user_count', $user_count, DAY_IN_SECONDS ); |
||
3501 | } |
||
3502 | return $user_count; |
||
3503 | } |
||
3504 | |||
3505 | /* Admin Pages */ |
||
3506 | |||
3507 | function admin_init() { |
||
3508 | // If the plugin is not connected, display a connect message. |
||
3509 | if ( |
||
3510 | // the plugin was auto-activated and needs its candy |
||
3511 | Jetpack_Options::get_option_and_ensure_autoload( 'do_activate', '0' ) |
||
3512 | || |
||
3513 | // the plugin is active, but was never activated. Probably came from a site-wide network activation |
||
3514 | ! Jetpack_Options::get_option( 'activated' ) |
||
3515 | ) { |
||
3516 | Jetpack::plugin_initialize(); |
||
3517 | } |
||
3518 | |||
3519 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
3520 | Jetpack_Connection_Banner::init(); |
||
3521 | } elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) { |
||
3522 | // Upgrade: 1.1 -> 1.1.1 |
||
3523 | // Check and see if host can verify the Jetpack servers' SSL certificate |
||
3524 | $args = array(); |
||
3525 | Jetpack_Client::_wp_remote_request( |
||
3526 | Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ) ), |
||
3527 | $args, |
||
3528 | true |
||
3529 | ); |
||
3530 | } |
||
3531 | |||
3532 | if ( current_user_can( 'manage_options' ) && 'AUTO' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) { |
||
3533 | add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) ); |
||
3534 | } |
||
3535 | |||
3536 | add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) ); |
||
3537 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
||
3538 | add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) ); |
||
3539 | |||
3540 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) { |
||
3541 | // Artificially throw errors in certain whitelisted cases during plugin activation |
||
3542 | add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) ); |
||
3543 | } |
||
3544 | |||
3545 | // Add custom column in wp-admin/users.php to show whether user is linked. |
||
3546 | add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) ); |
||
3547 | add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 ); |
||
3548 | add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) ); |
||
3549 | } |
||
3550 | |||
3551 | function admin_body_class( $admin_body_class = '' ) { |
||
3559 | |||
3560 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3563 | |||
3564 | /** |
||
3565 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3566 | * This function artificially throws errors for such cases (whitelisted). |
||
3567 | * |
||
3568 | * @param string $plugin The activated plugin. |
||
3569 | */ |
||
3570 | function throw_error_on_activate_plugin( $plugin ) { |
||
3571 | $active_modules = Jetpack::get_active_modules(); |
||
3572 | |||
3573 | // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks. |
||
3574 | if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) { |
||
3575 | $throw = false; |
||
3576 | |||
3577 | // Try and make sure it really was the stats plugin |
||
3578 | if ( ! class_exists( 'ReflectionFunction' ) ) { |
||
3579 | if ( 'stats.php' == basename( $plugin ) ) { |
||
3580 | $throw = true; |
||
3581 | } |
||
3582 | } else { |
||
3583 | $reflection = new ReflectionFunction( 'stats_get_api_key' ); |
||
3584 | if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) { |
||
3585 | $throw = true; |
||
3586 | } |
||
3587 | } |
||
3588 | |||
3589 | if ( $throw ) { |
||
3590 | trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR ); |
||
3591 | } |
||
3592 | } |
||
3593 | } |
||
3594 | |||
3595 | function intercept_plugin_error_scrape_init() { |
||
3596 | add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 ); |
||
3597 | } |
||
3598 | |||
3599 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3600 | if ( ! $result ) { |
||
3601 | return; |
||
3602 | } |
||
3603 | |||
3604 | foreach ( $this->plugins_to_deactivate as $deactivate_me ) { |
||
3605 | if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) { |
||
3606 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), $deactivate_me[1] ), false ); |
||
3607 | } |
||
3608 | } |
||
3609 | } |
||
3610 | |||
3611 | function add_remote_request_handlers() { |
||
3612 | add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) ); |
||
3613 | add_action( 'wp_ajax_nopriv_jetpack_update_file', array( $this, 'remote_request_handlers' ) ); |
||
3614 | } |
||
3615 | |||
3616 | function remote_request_handlers() { |
||
3617 | $action = current_filter(); |
||
3618 | |||
3619 | switch ( current_filter() ) { |
||
3620 | case 'wp_ajax_nopriv_jetpack_upload_file' : |
||
3621 | $response = $this->upload_handler(); |
||
3622 | break; |
||
3623 | |||
3624 | case 'wp_ajax_nopriv_jetpack_update_file' : |
||
3625 | $response = $this->upload_handler( true ); |
||
3626 | break; |
||
3627 | default : |
||
3628 | $response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 ); |
||
3629 | break; |
||
3630 | } |
||
3631 | |||
3632 | if ( ! $response ) { |
||
3633 | $response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 ); |
||
3634 | } |
||
3635 | |||
3636 | if ( is_wp_error( $response ) ) { |
||
3637 | $status_code = $response->get_error_data(); |
||
3638 | $error = $response->get_error_code(); |
||
3639 | $error_description = $response->get_error_message(); |
||
3640 | |||
3641 | if ( ! is_int( $status_code ) ) { |
||
3642 | $status_code = 400; |
||
3643 | } |
||
3644 | |||
3645 | status_header( $status_code ); |
||
3646 | die( json_encode( (object) compact( 'error', 'error_description' ) ) ); |
||
3647 | } |
||
3648 | |||
3649 | status_header( 200 ); |
||
3650 | if ( true === $response ) { |
||
3651 | exit; |
||
3652 | } |
||
3653 | |||
3656 | |||
3657 | /** |
||
3658 | * Uploads a file gotten from the global $_FILES. |
||
3659 | * If `$update_media_item` is true and `post_id` is defined |
||
3660 | * the attachment file of the media item (gotten through of the post_id) |
||
3661 | * will be updated instead of add a new one. |
||
3662 | * |
||
3663 | * @param boolean $update_media_item - update media attachment |
||
3664 | * @return array - An array describing the uploadind files process |
||
3665 | */ |
||
3666 | function upload_handler( $update_media_item = false ) { |
||
3788 | |||
3789 | /** |
||
3790 | * Add help to the Jetpack page |
||
3791 | * |
||
3792 | * @since Jetpack (1.2.3) |
||
3793 | * @return false if not the Jetpack page |
||
3794 | */ |
||
3795 | function admin_help() { |
||
3836 | |||
3837 | function admin_menu_css() { |
||
3840 | |||
3841 | function admin_menu_order() { |
||
3844 | |||
3845 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
3860 | |||
3861 | function admin_banner_styles() { |
||
3882 | |||
3883 | function plugin_action_links( $actions ) { |
||
3898 | |||
3899 | /* |
||
3900 | * Registration flow: |
||
3901 | * 1 - ::admin_page_load() action=register |
||
3902 | * 2 - ::try_registration() |
||
3903 | * 3 - ::register() |
||
3904 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
3905 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
3906 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
3907 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
3908 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
3909 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
3910 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
3911 | * jetpack_id, jetpack_secret, jetpack_public |
||
3912 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
3913 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
3914 | * 5 - user logs in with WP.com account |
||
3915 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
3916 | * - Jetpack_Client_Server::authorize() |
||
3917 | * - Jetpack_Client_Server::get_token() |
||
3918 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
3919 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
3920 | * - which responds with access_token, token_type, scope |
||
3921 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
3922 | * - Jetpack::activate_default_modules() |
||
3923 | * - Deactivates deprecated plugins |
||
3924 | * - Activates all default modules |
||
3925 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
3926 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
3927 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
3928 | * Done! |
||
3929 | */ |
||
3930 | |||
3931 | /** |
||
3932 | * Handles the page load events for the Jetpack admin page |
||
3933 | */ |
||
3934 | function admin_page_load() { |
||
4199 | |||
4200 | function admin_notices() { |
||
4292 | |||
4293 | /** |
||
4294 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4295 | */ |
||
4296 | function stat( $group, $detail ) { |
||
4301 | |||
4302 | /** |
||
4303 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4304 | */ |
||
4305 | function do_stats( $method = '' ) { |
||
4320 | |||
4321 | /** |
||
4322 | * Runs stats code for a one-off, server-side. |
||
4323 | * |
||
4324 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4325 | * |
||
4326 | * @return bool If it worked. |
||
4327 | */ |
||
4328 | static function do_server_side_stat( $args ) { |
||
4338 | |||
4339 | /** |
||
4340 | * Builds the stats url. |
||
4341 | * |
||
4342 | * @param $args array|string The arguments to append to the URL. |
||
4343 | * |
||
4344 | * @return string The URL to be pinged. |
||
4345 | */ |
||
4346 | static function build_stats_url( $args ) { |
||
4366 | |||
4367 | static function translate_current_user_to_role() { |
||
4376 | |||
4377 | static function translate_user_to_role( $user ) { |
||
4386 | |||
4387 | static function translate_role_to_cap( $role ) { |
||
4394 | |||
4395 | static function sign_role( $role, $user_id = null ) { |
||
4411 | |||
4412 | |||
4413 | /** |
||
4414 | * Builds a URL to the Jetpack connection auth page |
||
4415 | * |
||
4416 | * @since 3.9.5 |
||
4417 | * |
||
4418 | * @param bool $raw If true, URL will not be escaped. |
||
4419 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4420 | * If string, will be a custom redirect. |
||
4421 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4422 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
4423 | * |
||
4424 | * @return string Connect URL |
||
4425 | */ |
||
4426 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
4550 | |||
4551 | /** |
||
4552 | * Get our assumed site creation date. |
||
4553 | * Calculated based on the earlier date of either: |
||
4554 | * - Earliest admin user registration date. |
||
4555 | * - Earliest date of post of any post type. |
||
4556 | * |
||
4557 | * @since 7.2.0 |
||
4558 | * |
||
4559 | * @return string Assumed site creation date and time. |
||
4560 | */ |
||
4561 | public static function get_assumed_site_creation_date() { |
||
4588 | |||
4589 | public static function apply_activation_source_to_args( &$args ) { |
||
4600 | |||
4601 | function build_reconnect_url( $raw = false ) { |
||
4605 | |||
4606 | public static function admin_url( $args = null ) { |
||
4611 | |||
4612 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
4616 | |||
4617 | function dismiss_jetpack_notice() { |
||
4649 | |||
4650 | public static function sort_modules( $a, $b ) { |
||
4656 | |||
4657 | function ajax_recheck_ssl() { |
||
4665 | |||
4666 | /* Client API */ |
||
4667 | |||
4668 | /** |
||
4669 | * Returns the requested Jetpack API URL |
||
4670 | * |
||
4671 | * @return string |
||
4672 | */ |
||
4673 | public static function api_url( $relative_url ) { |
||
4676 | |||
4677 | /** |
||
4678 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
4679 | */ |
||
4680 | public static function fix_url_for_bad_hosts( $url ) { |
||
4696 | |||
4697 | /** |
||
4698 | * Create a random secret for validating onboarding payload |
||
4699 | * |
||
4700 | * @return string Secret token |
||
4701 | */ |
||
4702 | public static function create_onboarding_token() { |
||
4710 | |||
4711 | /** |
||
4712 | * Remove the onboarding token |
||
4713 | * |
||
4714 | * @return bool True on success, false on failure |
||
4715 | */ |
||
4716 | public static function invalidate_onboarding_token() { |
||
4719 | |||
4720 | /** |
||
4721 | * Validate an onboarding token for a specific action |
||
4722 | * |
||
4723 | * @return boolean True if token/action pair is accepted, false if not |
||
4724 | */ |
||
4725 | public static function validate_onboarding_token_action( $token, $action ) { |
||
4743 | |||
4744 | /** |
||
4745 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
4746 | * |
||
4747 | * @since 2.3.3 |
||
4748 | * @return boolean |
||
4749 | */ |
||
4750 | public static function permit_ssl( $force_recheck = false ) { |
||
4792 | |||
4793 | /* |
||
4794 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
4795 | */ |
||
4796 | public function alert_auto_ssl_fail() { |
||
4845 | |||
4846 | /** |
||
4847 | * Returns the Jetpack XML-RPC API |
||
4848 | * |
||
4849 | * @return string |
||
4850 | */ |
||
4851 | public static function xmlrpc_api_url() { |
||
4855 | |||
4856 | /** |
||
4857 | * Creates two secret tokens and the end of life timestamp for them. |
||
4858 | * |
||
4859 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
4860 | * |
||
4861 | * @since 2.6 |
||
4862 | * @return array |
||
4863 | */ |
||
4864 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
4890 | |||
4891 | public static function get_secrets( $action, $user_id ) { |
||
4906 | |||
4907 | public static function delete_secrets( $action, $user_id ) { |
||
4915 | |||
4916 | /** |
||
4917 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
4918 | * |
||
4919 | * Based on local php max_execution_time in php.ini |
||
4920 | * |
||
4921 | * @since 2.6 |
||
4922 | * @return int |
||
4923 | * @deprecated |
||
4924 | **/ |
||
4925 | public function get_remote_query_timeout_limit() { |
||
4929 | |||
4930 | /** |
||
4931 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
4932 | * |
||
4933 | * Based on local php max_execution_time in php.ini |
||
4934 | * |
||
4935 | * @since 5.4 |
||
4936 | * @return int |
||
4937 | **/ |
||
4938 | public static function get_max_execution_time() { |
||
4947 | |||
4948 | /** |
||
4949 | * Sets a minimum request timeout, and returns the current timeout |
||
4950 | * |
||
4951 | * @since 5.4 |
||
4952 | **/ |
||
4953 | public static function set_min_time_limit( $min_timeout ) { |
||
4961 | |||
4962 | |||
4963 | /** |
||
4964 | * Takes the response from the Jetpack register new site endpoint and |
||
4965 | * verifies it worked properly. |
||
4966 | * |
||
4967 | * @since 2.6 |
||
4968 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
4969 | **/ |
||
4970 | public function validate_remote_register_response( $response ) { |
||
5010 | /** |
||
5011 | * @return bool|WP_Error |
||
5012 | */ |
||
5013 | public static function register() { |
||
5118 | |||
5119 | /** |
||
5120 | * If the db version is showing something other that what we've got now, bump it to current. |
||
5121 | * |
||
5122 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
5123 | */ |
||
5124 | public static function maybe_set_version_option() { |
||
5138 | |||
5139 | /* Client Server API */ |
||
5140 | |||
5141 | /** |
||
5142 | * Loads the Jetpack XML-RPC client |
||
5143 | */ |
||
5144 | public static function load_xml_rpc_client() { |
||
5148 | |||
5149 | /** |
||
5150 | * Resets the saved authentication state in between testing requests. |
||
5151 | */ |
||
5152 | public function reset_saved_auth_state() { |
||
5156 | |||
5157 | function verify_xml_rpc_signature() { |
||
5287 | |||
5288 | /** |
||
5289 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
5290 | */ |
||
5291 | function authenticate_jetpack( $user, $username, $password ) { |
||
5314 | |||
5315 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
5316 | // Uses the existing XMLRPC request signing implementation. |
||
5317 | function wp_rest_authenticate( $user ) { |
||
5392 | |||
5393 | /** |
||
5394 | * Report authentication status to the WP REST API. |
||
5395 | * |
||
5396 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5397 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5398 | */ |
||
5399 | public function wp_rest_authentication_errors( $value ) { |
||
5405 | |||
5406 | function add_nonce( $timestamp, $nonce ) { |
||
5444 | |||
5445 | /** |
||
5446 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5447 | * Capture it here so we can verify the signature later. |
||
5448 | */ |
||
5449 | function xmlrpc_methods( $methods ) { |
||
5453 | |||
5454 | function public_xmlrpc_methods( $methods ) { |
||
5460 | |||
5461 | function jetpack_getOptions( $args ) { |
||
5501 | |||
5502 | function xmlrpc_options( $options ) { |
||
5520 | |||
5521 | public static function clean_nonces( $all = false ) { |
||
5542 | |||
5543 | /** |
||
5544 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5545 | * SET: state( $key, $value ); |
||
5546 | * GET: $value = state( $key ); |
||
5547 | * |
||
5548 | * @param string $key |
||
5549 | * @param string $value |
||
5550 | * @param bool $restate private |
||
5551 | */ |
||
5552 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5602 | |||
5603 | public static function restate() { |
||
5606 | |||
5607 | public static function check_privacy( $file ) { |
||
5642 | |||
5643 | /** |
||
5644 | * Helper method for multicall XMLRPC. |
||
5645 | */ |
||
5646 | public static function xmlrpc_async_call() { |
||
5688 | |||
5689 | public static function staticize_subdomain( $url ) { |
||
5724 | |||
5725 | /* JSON API Authorization */ |
||
5726 | |||
5727 | /** |
||
5728 | * Handles the login action for Authorizing the JSON API |
||
5729 | */ |
||
5730 | function login_form_json_api_authorization() { |
||
5739 | |||
5740 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
5741 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
5754 | |||
5755 | // Make sure the POSTed request is handled by the same action |
||
5756 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
5760 | |||
5761 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
5762 | function store_json_api_authorization_token( $user_login, $user ) { |
||
5768 | |||
5769 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
5770 | function allow_wpcom_public_api_domain( $domains ) { |
||
5774 | |||
5775 | static function is_redirect_encoded( $redirect_url ) { |
||
5778 | |||
5779 | // Add all wordpress.com environments to the safe redirect whitelist |
||
5780 | function allow_wpcom_environments( $domains ) { |
||
5787 | |||
5788 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
5789 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
5801 | |||
5802 | |||
5803 | /** |
||
5804 | * Verifies the request by checking the signature |
||
5805 | * |
||
5806 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
5807 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
5808 | * |
||
5809 | * @param null|array $environment |
||
5810 | */ |
||
5811 | function verify_json_api_authorization_request( $environment = null ) { |
||
5915 | |||
5916 | function login_message_json_api_authorization( $message ) { |
||
5922 | |||
5923 | /** |
||
5924 | * Get $content_width, but with a <s>twist</s> filter. |
||
5925 | */ |
||
5926 | public static function get_content_width() { |
||
5937 | |||
5938 | /** |
||
5939 | * Pings the WordPress.com Mirror Site for the specified options. |
||
5940 | * |
||
5941 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
5942 | * |
||
5943 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
5944 | */ |
||
5945 | public function get_cloud_site_options( $option_names ) { |
||
5961 | |||
5962 | /** |
||
5963 | * Checks if the site is currently in an identity crisis. |
||
5964 | * |
||
5965 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
5966 | */ |
||
5967 | public static function check_identity_crisis() { |
||
5974 | |||
5975 | /** |
||
5976 | * Checks whether the home and siteurl specifically are whitelisted |
||
5977 | * Written so that we don't have re-check $key and $value params every time |
||
5978 | * we want to check if this site is whitelisted, for example in footer.php |
||
5979 | * |
||
5980 | * @since 3.8.0 |
||
5981 | * @return bool True = already whitelisted False = not whitelisted |
||
5982 | */ |
||
5983 | public static function is_staging_site() { |
||
6042 | |||
6043 | /** |
||
6044 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
6045 | * |
||
6046 | * @since 4.4.0 |
||
6047 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
6048 | * |
||
6049 | * @return bool |
||
6050 | */ |
||
6051 | public static function validate_sync_error_idc_option() { |
||
6095 | |||
6096 | /** |
||
6097 | * Normalizes a url by doing three things: |
||
6098 | * - Strips protocol |
||
6099 | * - Strips www |
||
6100 | * - Adds a trailing slash |
||
6101 | * |
||
6102 | * @since 4.4.0 |
||
6103 | * @param string $url |
||
6104 | * @return WP_Error|string |
||
6105 | */ |
||
6106 | public static function normalize_url_protocol_agnostic( $url ) { |
||
6116 | |||
6117 | /** |
||
6118 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
6119 | * |
||
6120 | * @since 4.4.0 |
||
6121 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
6122 | * |
||
6123 | * @param array $response |
||
6124 | * @return array Array of the local urls, wpcom urls, and error code |
||
6125 | */ |
||
6126 | public static function get_sync_error_idc_option( $response = array() ) { |
||
6159 | |||
6160 | /** |
||
6161 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
6162 | * If set to true, the site will be put into staging mode. |
||
6163 | * |
||
6164 | * @since 4.3.2 |
||
6165 | * @return bool |
||
6166 | */ |
||
6167 | public static function sync_idc_optin() { |
||
6185 | |||
6186 | /** |
||
6187 | * Maybe Use a .min.css stylesheet, maybe not. |
||
6188 | * |
||
6189 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
6190 | */ |
||
6191 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
6233 | |||
6234 | /** |
||
6235 | * If the asset is minified, let's flag .min as the suffix. |
||
6236 | * |
||
6237 | * Attached to `style_loader_src` filter. |
||
6238 | * |
||
6239 | * @param string $tag The tag that would link to the external asset. |
||
6240 | * @param string $handle The registered handle of the script in question. |
||
6241 | * @param string $href The url of the asset in question. |
||
6242 | */ |
||
6243 | public static function set_suffix_on_min( $src, $handle ) { |
||
6259 | |||
6260 | /** |
||
6261 | * Maybe inlines a stylesheet. |
||
6262 | * |
||
6263 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
6264 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
6265 | * |
||
6266 | * Attached to `style_loader_tag` filter. |
||
6267 | * |
||
6268 | * @param string $tag The tag that would link to the external asset. |
||
6269 | * @param string $handle The registered handle of the script in question. |
||
6270 | * |
||
6271 | * @return string |
||
6272 | */ |
||
6273 | public static function maybe_inline_style( $tag, $handle ) { |
||
6323 | |||
6324 | /** |
||
6325 | * Loads a view file from the views |
||
6326 | * |
||
6327 | * Data passed in with the $data parameter will be available in the |
||
6328 | * template file as $data['value'] |
||
6329 | * |
||
6330 | * @param string $template - Template file to load |
||
6331 | * @param array $data - Any data to pass along to the template |
||
6332 | * @return boolean - If template file was found |
||
6333 | **/ |
||
6334 | public function load_view( $template, $data = array() ) { |
||
6345 | |||
6346 | /** |
||
6347 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
6348 | */ |
||
6349 | public function deprecated_hooks() { |
||
6418 | |||
6419 | /** |
||
6420 | * Converts any url in a stylesheet, to the correct absolute url. |
||
6421 | * |
||
6422 | * Considerations: |
||
6423 | * - Normal, relative URLs `feh.png` |
||
6424 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
6425 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
6426 | * - Absolute URLs `http://domain.com/feh.png` |
||
6427 | * - Domain root relative URLs `/feh.png` |
||
6428 | * |
||
6429 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
6430 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
6431 | * |
||
6432 | * @return mixed|string |
||
6433 | */ |
||
6434 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6478 | |||
6479 | /** |
||
6480 | * This methods removes all of the registered css files on the front end |
||
6481 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6482 | * all the files into one file. |
||
6483 | * |
||
6484 | * Pros: |
||
6485 | * - Uses only ONE css asset connection instead of 15 |
||
6486 | * - Saves a minimum of 56k |
||
6487 | * - Reduces server load |
||
6488 | * - Reduces time to first painted byte |
||
6489 | * |
||
6490 | * Cons: |
||
6491 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6492 | * should not cause any issues with themes. |
||
6493 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6494 | * jetpack_implode_frontend_css filter for a workaround |
||
6495 | * |
||
6496 | * For some situations developers may wish to disable css imploding and |
||
6497 | * instead operate in legacy mode where each file loads seperately and |
||
6498 | * can be edited individually or dequeued. This can be accomplished with |
||
6499 | * the following line: |
||
6500 | * |
||
6501 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6502 | * |
||
6503 | * @since 3.2 |
||
6504 | **/ |
||
6505 | public function implode_frontend_css( $travis_test = false ) { |
||
6562 | |||
6563 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
6573 | |||
6574 | /* |
||
6575 | * Check the heartbeat data |
||
6576 | * |
||
6577 | * Organizes the heartbeat data by severity. For example, if the site |
||
6578 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
6579 | * |
||
6580 | * Data will be added to "caution" array, if it either: |
||
6581 | * - Out of date Jetpack version |
||
6582 | * - Out of date WP version |
||
6583 | * - Out of date PHP version |
||
6584 | * |
||
6585 | * $return array $filtered_data |
||
6586 | */ |
||
6587 | public static function jetpack_check_heartbeat_data() { |
||
6640 | |||
6641 | |||
6642 | /* |
||
6643 | * This method is used to organize all options that can be reset |
||
6644 | * without disconnecting Jetpack. |
||
6645 | * |
||
6646 | * It is used in class.jetpack-cli.php to reset options |
||
6647 | * |
||
6648 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
6649 | * |
||
6650 | * @return array of options to delete. |
||
6651 | */ |
||
6652 | public static function get_jetpack_options_for_reset() { |
||
6655 | |||
6656 | /* |
||
6657 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
6658 | * so we can bring them directly to their site in calypso. |
||
6659 | * |
||
6660 | * @param string | url |
||
6661 | * @return string | url without the guff |
||
6662 | */ |
||
6663 | public static function build_raw_urls( $url ) { |
||
6669 | |||
6670 | /** |
||
6671 | * Stores and prints out domains to prefetch for page speed optimization. |
||
6672 | * |
||
6673 | * @param mixed $new_urls |
||
6674 | */ |
||
6675 | public static function dns_prefetch( $new_urls = null ) { |
||
6692 | |||
6693 | public function wp_dashboard_setup() { |
||
6726 | |||
6727 | /** |
||
6728 | * @param mixed $result Value for the user's option |
||
6729 | * @return mixed |
||
6730 | */ |
||
6731 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
6756 | |||
6757 | public static function dashboard_widget() { |
||
6765 | |||
6766 | public static function dashboard_widget_footer() { |
||
6799 | |||
6800 | /** |
||
6801 | * Return string containing the Jetpack logo. |
||
6802 | * |
||
6803 | * @since 3.9.0 |
||
6804 | * |
||
6805 | * @param bool $logotype Should we use the full logotype (logo + text). Default to false. |
||
6806 | * |
||
6807 | * @return string |
||
6808 | */ |
||
6809 | public static function get_jp_emblem( $logotype = false ) { |
||
6827 | |||
6828 | /* |
||
6829 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
6830 | */ |
||
6831 | function jetpack_icon_user_connected( $columns ) { |
||
6835 | |||
6836 | /* |
||
6837 | * Show Jetpack icon if the user is linked. |
||
6838 | */ |
||
6839 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
6851 | |||
6852 | /* |
||
6853 | * Style the Jetpack user column |
||
6854 | */ |
||
6855 | function jetpack_user_col_style() { |
||
6872 | |||
6873 | /** |
||
6874 | * Checks if Akismet is active and working. |
||
6875 | * |
||
6876 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
6877 | * that implied usage of methods present since more recent version. |
||
6878 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
6879 | * |
||
6880 | * @since 5.1.0 |
||
6881 | * |
||
6882 | * @return bool True = Akismet available. False = Aksimet not available. |
||
6883 | */ |
||
6884 | public static function is_akismet_active() { |
||
6919 | |||
6920 | /** |
||
6921 | * Checks if one or more function names is in debug_backtrace |
||
6922 | * |
||
6923 | * @param $names Mixed string name of function or array of string names of functions |
||
6924 | * |
||
6925 | * @return bool |
||
6926 | */ |
||
6927 | public static function is_function_in_backtrace( $names ) { |
||
6950 | |||
6951 | /** |
||
6952 | * Given a minified path, and a non-minified path, will return |
||
6953 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
6954 | * |
||
6955 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
6956 | * root Jetpack directory. |
||
6957 | * |
||
6958 | * @since 5.6.0 |
||
6959 | * |
||
6960 | * @param string $min_path |
||
6961 | * @param string $non_min_path |
||
6962 | * @return string The URL to the file |
||
6963 | */ |
||
6964 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
6971 | |||
6972 | /** |
||
6973 | * Checks for whether Jetpack Backup & Scan is enabled. |
||
6974 | * Will return true if the state of Backup & Scan is anything except "unavailable". |
||
6975 | * @return bool|int|mixed |
||
6976 | */ |
||
6977 | public static function is_rewind_enabled() { |
||
6996 | |||
6997 | /** |
||
6998 | * Checks whether or not TOS has been agreed upon. |
||
6999 | * Will return true if a user has clicked to register, or is already connected. |
||
7000 | */ |
||
7001 | public static function jetpack_tos_agreed() { |
||
7004 | |||
7005 | /** |
||
7006 | * Handles activating default modules as well general cleanup for the new connection. |
||
7007 | * |
||
7008 | * @param boolean $activate_sso Whether to activate the SSO module when activating default modules. |
||
7009 | * @param boolean $redirect_on_activation_error Whether to redirect on activation error. |
||
7010 | * @param boolean $send_state_messages Whether to send state messages. |
||
7011 | * @return void |
||
7012 | */ |
||
7013 | public static function handle_post_authorization_actions( |
||
7042 | |||
7043 | /** |
||
7044 | * Returns a boolean for whether backups UI should be displayed or not. |
||
7045 | * |
||
7046 | * @return bool Should backups UI be displayed? |
||
7047 | */ |
||
7048 | public static function show_backups_ui() { |
||
7058 | |||
7059 | /* |
||
7060 | * Deprecated manage functions |
||
7061 | */ |
||
7062 | function prepare_manage_jetpack_notice() { |
||
7083 | |||
7084 | /** |
||
7085 | * Clean leftoveruser meta. |
||
7086 | * |
||
7087 | * Delete Jetpack-related user meta when it is no longer needed. |
||
7088 | * |
||
7089 | * @since 7.3.0 |
||
7090 | * |
||
7091 | * @param int $user_id User ID being updated. |
||
7092 | */ |
||
7093 | public static function user_meta_cleanup( $user_id ) { |
||
7108 | } |
||
7109 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.