Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Jetpack { |
||
28 | public $xmlrpc_server = null; |
||
29 | |||
30 | private $xmlrpc_verification = null; |
||
31 | private $rest_authentication_status = null; |
||
32 | |||
33 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
34 | |||
35 | /** |
||
36 | * @var array The handles of styles that are concatenated into jetpack.css. |
||
37 | * |
||
38 | * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js. |
||
39 | */ |
||
40 | public $concatenated_style_handles = array( |
||
41 | 'jetpack-carousel', |
||
42 | 'grunion.css', |
||
43 | 'the-neverending-homepage', |
||
44 | 'jetpack_likes', |
||
45 | 'jetpack_related-posts', |
||
46 | 'sharedaddy', |
||
47 | 'jetpack-slideshow', |
||
48 | 'presentations', |
||
49 | 'quiz', |
||
50 | 'jetpack-subscriptions', |
||
51 | 'jetpack-responsive-videos-style', |
||
52 | 'jetpack-social-menu', |
||
53 | 'tiled-gallery', |
||
54 | 'jetpack_display_posts_widget', |
||
55 | 'gravatar-profile-widget', |
||
56 | 'goodreads-widget', |
||
57 | 'jetpack_social_media_icons_widget', |
||
58 | 'jetpack-top-posts-widget', |
||
59 | 'jetpack_image_widget', |
||
60 | 'jetpack-my-community-widget', |
||
61 | 'jetpack-authors-widget', |
||
62 | 'wordads', |
||
63 | 'eu-cookie-law-style', |
||
64 | 'flickr-widget-style', |
||
65 | 'jetpack-search-widget', |
||
66 | 'jetpack-simple-payments-widget-style', |
||
67 | 'jetpack-widget-social-icons-styles', |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * Contains all assets that have had their URL rewritten to minified versions. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | static $min_assets = array(); |
||
76 | |||
77 | public $plugins_to_deactivate = array( |
||
78 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
79 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
80 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
81 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
82 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
83 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
84 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
85 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
86 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
87 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
88 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
89 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
90 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
91 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
92 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
93 | ); |
||
94 | |||
95 | static $capability_translations = array( |
||
96 | 'administrator' => 'manage_options', |
||
97 | 'editor' => 'edit_others_posts', |
||
98 | 'author' => 'publish_posts', |
||
99 | 'contributor' => 'edit_posts', |
||
100 | 'subscriber' => 'read', |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
105 | * if the plugins are active. Used by filter_default_modules |
||
106 | * |
||
107 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
108 | * change `module-slug` and add this to your plugin: |
||
109 | * |
||
110 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
111 | * function my_jetpack_get_default_modules( $modules ) { |
||
112 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
113 | * } |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | private $conflicting_plugins = array( |
||
118 | 'comments' => array( |
||
119 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
120 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
121 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
122 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
123 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
124 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
125 | ), |
||
126 | 'comment-likes' => array( |
||
127 | 'Epoch' => 'epoch/plugincore.php', |
||
128 | ), |
||
129 | 'contact-form' => array( |
||
130 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
131 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
132 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
133 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
134 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
135 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
136 | ), |
||
137 | 'minileven' => array( |
||
138 | 'WPtouch' => 'wptouch/wptouch.php', |
||
139 | ), |
||
140 | 'latex' => array( |
||
141 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
142 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
143 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
144 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
145 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
146 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
147 | ), |
||
148 | 'protect' => array( |
||
149 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
150 | 'Captcha' => 'captcha/captcha.php', |
||
151 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
152 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
153 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
154 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
155 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
156 | 'Security-protection' => 'security-protection/security-protection.php', |
||
157 | 'Login Security' => 'login-security/login-security.php', |
||
158 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
159 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
160 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
161 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
162 | ), |
||
163 | 'random-redirect' => array( |
||
164 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
165 | ), |
||
166 | 'related-posts' => array( |
||
167 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
168 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
169 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
170 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
171 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
172 | 'outbrain' => 'outbrain/outbrain.php', |
||
173 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
174 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
175 | ), |
||
176 | 'sharedaddy' => array( |
||
177 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
178 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
179 | 'ShareThis' => 'share-this/sharethis.php', |
||
180 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
181 | ), |
||
182 | 'seo-tools' => array( |
||
183 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
184 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
185 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
186 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
187 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
188 | ), |
||
189 | 'verification-tools' => array( |
||
190 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
191 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
192 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
193 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
194 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
195 | ), |
||
196 | 'widget-visibility' => array( |
||
197 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
198 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
199 | ), |
||
200 | 'sitemaps' => array( |
||
201 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
202 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
203 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
204 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
205 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
206 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
207 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
208 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
209 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
210 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
211 | 'Sitemap' => 'sitemap/sitemap.php', |
||
212 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
213 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
214 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
215 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
216 | ), |
||
217 | 'lazy-images' => array( |
||
218 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
219 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
220 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
221 | ), |
||
222 | ); |
||
223 | |||
224 | /** |
||
225 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
226 | * |
||
227 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
228 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
229 | * |
||
230 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
231 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
232 | */ |
||
233 | private $open_graph_conflicting_plugins = array( |
||
234 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
235 | // 2 Click Social Media Buttons |
||
236 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
237 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
238 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
239 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
240 | // Open Graph Meta Tags by Heateor |
||
241 | 'facebook/facebook.php', // Facebook (official plugin) |
||
242 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
243 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
244 | // Facebook Featured Image & OG Meta Tags |
||
245 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
246 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
247 | // Facebook Open Graph Meta Tags for WordPress |
||
248 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
249 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
250 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
251 | // Fedmich's Facebook Open Graph Meta |
||
252 | 'network-publisher/networkpub.php', // Network Publisher |
||
253 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
254 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
255 | // NextScripts SNAP |
||
256 | 'og-tags/og-tags.php', // OG Tags |
||
257 | 'opengraph/opengraph.php', // Open Graph |
||
258 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
259 | // Open Graph Protocol Framework |
||
260 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
261 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
262 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
263 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
264 | 'sharepress/sharepress.php', // SharePress |
||
265 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
266 | 'social-discussions/social-discussions.php', // Social Discussions |
||
267 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
268 | 'socialize/socialize.php', // Socialize |
||
269 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
270 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
271 | // Tweet, Like, Google +1 and Share |
||
272 | 'wordbooker/wordbooker.php', // Wordbooker |
||
273 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
274 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
275 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
276 | // WP Facebook Like Send & Open Graph Meta |
||
277 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
278 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
279 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
280 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button |
||
281 | 'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox |
||
282 | ); |
||
283 | |||
284 | /** |
||
285 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
286 | */ |
||
287 | private $twitter_cards_conflicting_plugins = array( |
||
288 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
289 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
290 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
291 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
292 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
293 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
294 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
295 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
296 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
297 | 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter |
||
298 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
299 | ); |
||
300 | |||
301 | /** |
||
302 | * Message to display in admin_notice |
||
303 | * @var string |
||
304 | */ |
||
305 | public $message = ''; |
||
306 | |||
307 | /** |
||
308 | * Error to display in admin_notice |
||
309 | * @var string |
||
310 | */ |
||
311 | public $error = ''; |
||
312 | |||
313 | /** |
||
314 | * Modules that need more privacy description. |
||
315 | * @var string |
||
316 | */ |
||
317 | public $privacy_checks = ''; |
||
318 | |||
319 | /** |
||
320 | * Stats to record once the page loads |
||
321 | * |
||
322 | * @var array |
||
323 | */ |
||
324 | public $stats = array(); |
||
325 | |||
326 | /** |
||
327 | * Jetpack_Sync object |
||
328 | */ |
||
329 | public $sync; |
||
330 | |||
331 | /** |
||
332 | * Verified data for JSON authorization request |
||
333 | */ |
||
334 | public $json_api_authorization_request = array(); |
||
335 | |||
336 | /** |
||
337 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
338 | */ |
||
339 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
340 | |||
341 | /** |
||
342 | * Holds the singleton instance of this class |
||
343 | * @since 2.3.3 |
||
344 | * @var Jetpack |
||
345 | */ |
||
346 | static $instance = false; |
||
347 | |||
348 | /** |
||
349 | * Singleton |
||
350 | * @static |
||
351 | */ |
||
352 | public static function init() { |
||
361 | |||
362 | /** |
||
363 | * Must never be called statically |
||
364 | */ |
||
365 | function plugin_upgrade() { |
||
408 | |||
409 | /** |
||
410 | * Runs upgrade routines that need to have modules loaded. |
||
411 | */ |
||
412 | static function upgrade_on_load() { |
||
439 | |||
440 | static function activate_manage( ) { |
||
447 | |||
448 | static function update_active_modules( $modules ) { |
||
502 | |||
503 | static function delete_active_modules() { |
||
506 | |||
507 | /** |
||
508 | * Constructor. Initializes WordPress hooks |
||
509 | */ |
||
510 | private function __construct() { |
||
715 | |||
716 | function point_edit_post_links_to_calypso( $default_url, $post_id ) { |
||
744 | |||
745 | function point_edit_comment_links_to_calypso( $url ) { |
||
753 | |||
754 | function jetpack_track_last_sync_callback( $params ) { |
||
776 | |||
777 | function jetpack_connection_banner_callback() { |
||
787 | |||
788 | /** |
||
789 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
790 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
791 | * ensure that Core and other plugins' methods are not exposed. |
||
792 | * |
||
793 | * @param array $methods |
||
794 | * @return array filtered $methods |
||
795 | */ |
||
796 | function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
807 | |||
808 | /** |
||
809 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
810 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
811 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
812 | * which is accessible via a different URI. Most of the below is copied directly |
||
813 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
814 | */ |
||
815 | function alternate_xmlrpc() { |
||
855 | |||
856 | function jetpack_admin_ajax_tracks_callback() { |
||
879 | |||
880 | /** |
||
881 | * The callback for the JITM ajax requests. |
||
882 | */ |
||
883 | function jetpack_jitm_ajax_callback() { |
||
940 | |||
941 | /** |
||
942 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
943 | */ |
||
944 | function push_stats() { |
||
949 | |||
950 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
1022 | |||
1023 | function require_jetpack_authentication() { |
||
1034 | |||
1035 | /** |
||
1036 | * Load language files |
||
1037 | * @action plugins_loaded |
||
1038 | */ |
||
1039 | public static function plugin_textdomain() { |
||
1043 | |||
1044 | /** |
||
1045 | * Register assets for use in various modules and the Jetpack admin page. |
||
1046 | * |
||
1047 | * @uses wp_script_is, wp_register_script, plugins_url |
||
1048 | * @action wp_loaded |
||
1049 | * @return null |
||
1050 | */ |
||
1051 | public function register_assets() { |
||
1130 | |||
1131 | /** |
||
1132 | * Guess locale from language code. |
||
1133 | * |
||
1134 | * @param string $lang Language code. |
||
1135 | * @return string|bool |
||
1136 | */ |
||
1137 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
1176 | |||
1177 | /** |
||
1178 | * Get the locale. |
||
1179 | * |
||
1180 | * @return string|bool |
||
1181 | */ |
||
1182 | function get_locale() { |
||
1191 | |||
1192 | /** |
||
1193 | * Device Pixels support |
||
1194 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
1195 | */ |
||
1196 | function devicepx() { |
||
1201 | |||
1202 | /** |
||
1203 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
1204 | * @param bool $option |
||
1205 | * @return string |
||
1206 | */ |
||
1207 | public function jetpack_main_network_site_option( $option ) { |
||
1210 | /** |
||
1211 | * Network Name. |
||
1212 | */ |
||
1213 | static function network_name( $option = null ) { |
||
1217 | /** |
||
1218 | * Does the network allow new user and site registrations. |
||
1219 | * @return string |
||
1220 | */ |
||
1221 | static function network_allow_new_registrations( $option = null ) { |
||
1224 | /** |
||
1225 | * Does the network allow admins to add new users. |
||
1226 | * @return boolian |
||
1227 | */ |
||
1228 | static function network_add_new_users( $option = null ) { |
||
1231 | /** |
||
1232 | * File upload psace left per site in MB. |
||
1233 | * -1 means NO LIMIT. |
||
1234 | * @return number |
||
1235 | */ |
||
1236 | static function network_site_upload_space( $option = null ) { |
||
1240 | |||
1241 | /** |
||
1242 | * Network allowed file types. |
||
1243 | * @return string |
||
1244 | */ |
||
1245 | static function network_upload_file_types( $option = null ) { |
||
1248 | |||
1249 | /** |
||
1250 | * Maximum file upload size set by the network. |
||
1251 | * @return number |
||
1252 | */ |
||
1253 | static function network_max_upload_file_size( $option = null ) { |
||
1257 | |||
1258 | /** |
||
1259 | * Lets us know if a site allows admins to manage the network. |
||
1260 | * @return array |
||
1261 | */ |
||
1262 | static function network_enable_administration_menus( $option = null ) { |
||
1265 | |||
1266 | /** |
||
1267 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1268 | * jetpack_other_linked_admins transient. |
||
1269 | * |
||
1270 | * @since 4.3.2 |
||
1271 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1272 | * |
||
1273 | * @param int $user_id The user ID whose role changed. |
||
1274 | * @param string $role The new role. |
||
1275 | * @param array $old_roles An array of the user's previous roles. |
||
1276 | */ |
||
1277 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1285 | |||
1286 | /** |
||
1287 | * Checks to see if there are any other users available to become primary |
||
1288 | * Users must both: |
||
1289 | * - Be linked to wpcom |
||
1290 | * - Be an admin |
||
1291 | * |
||
1292 | * @return mixed False if no other users are linked, Int if there are. |
||
1293 | */ |
||
1294 | static function get_other_linked_admins() { |
||
1322 | |||
1323 | /** |
||
1324 | * Return whether we are dealing with a multi network setup or not. |
||
1325 | * The reason we are type casting this is because we want to avoid the situation where |
||
1326 | * the result is false since when is_main_network_option return false it cases |
||
1327 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1328 | * database which could be set to anything as opposed to what this function returns. |
||
1329 | * @param bool $option |
||
1330 | * |
||
1331 | * @return boolean |
||
1332 | */ |
||
1333 | public function is_main_network_option( $option ) { |
||
1337 | |||
1338 | /** |
||
1339 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1340 | * |
||
1341 | * @param string $option |
||
1342 | * @return boolean |
||
1343 | */ |
||
1344 | public function is_multisite( $option ) { |
||
1347 | |||
1348 | /** |
||
1349 | * Implemented since there is no core is multi network function |
||
1350 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1351 | * |
||
1352 | * @since 3.3 |
||
1353 | * @return boolean |
||
1354 | */ |
||
1355 | public static function is_multi_network() { |
||
1370 | |||
1371 | /** |
||
1372 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1373 | * @return null |
||
1374 | */ |
||
1375 | function update_jetpack_main_network_site_option() { |
||
1378 | /** |
||
1379 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1380 | * |
||
1381 | */ |
||
1382 | function update_jetpack_network_settings() { |
||
1386 | |||
1387 | /** |
||
1388 | * Get back if the current site is single user site. |
||
1389 | * |
||
1390 | * @return bool |
||
1391 | */ |
||
1392 | public static function is_single_user_site() { |
||
1401 | |||
1402 | /** |
||
1403 | * Returns true if the site has file write access false otherwise. |
||
1404 | * @return string ( '1' | '0' ) |
||
1405 | **/ |
||
1406 | public static function file_system_write_access() { |
||
1426 | |||
1427 | /** |
||
1428 | * Finds out if a site is using a version control system. |
||
1429 | * @return string ( '1' | '0' ) |
||
1430 | **/ |
||
1431 | public static function is_version_controlled() { |
||
1435 | |||
1436 | /** |
||
1437 | * Determines whether the current theme supports featured images or not. |
||
1438 | * @return string ( '1' | '0' ) |
||
1439 | */ |
||
1440 | public static function featured_images_enabled() { |
||
1444 | |||
1445 | /** |
||
1446 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1447 | * |
||
1448 | * @deprecated 4.7 use get_avatar_url instead. |
||
1449 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1450 | * @param int $size Size of the avatar image |
||
1451 | * @param string $default URL to a default image to use if no avatar is available |
||
1452 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1453 | * |
||
1454 | * @return array |
||
1455 | */ |
||
1456 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1464 | |||
1465 | /** |
||
1466 | * jetpack_updates is saved in the following schema: |
||
1467 | * |
||
1468 | * array ( |
||
1469 | * 'plugins' => (int) Number of plugin updates available. |
||
1470 | * 'themes' => (int) Number of theme updates available. |
||
1471 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1472 | * 'translations' => (int) Number of translation updates available. |
||
1473 | * 'total' => (int) Total of all available updates. |
||
1474 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1475 | * ) |
||
1476 | * @return array |
||
1477 | */ |
||
1478 | public static function get_updates() { |
||
1495 | |||
1496 | public static function get_update_details() { |
||
1504 | |||
1505 | public static function refresh_update_data() { |
||
1509 | |||
1510 | public static function refresh_theme_data() { |
||
1513 | |||
1514 | /** |
||
1515 | * Is Jetpack active? |
||
1516 | */ |
||
1517 | public static function is_active() { |
||
1520 | |||
1521 | /** |
||
1522 | * Make an API call to WordPress.com for plan status |
||
1523 | * |
||
1524 | * @uses Jetpack_Options::get_option() |
||
1525 | * @uses Jetpack_Client::wpcom_json_api_request_as_blog() |
||
1526 | * @uses update_option() |
||
1527 | * |
||
1528 | * @access public |
||
1529 | * @static |
||
1530 | * |
||
1531 | * @return bool True if plan is updated, false if no update |
||
1532 | */ |
||
1533 | public static function refresh_active_plan_from_wpcom() { |
||
1554 | |||
1555 | /** |
||
1556 | * Get the plan that this Jetpack site is currently using |
||
1557 | * |
||
1558 | * @uses get_option() |
||
1559 | * |
||
1560 | * @access public |
||
1561 | * @static |
||
1562 | * |
||
1563 | * @return array Active Jetpack plan details |
||
1564 | */ |
||
1565 | public static function get_active_plan() { |
||
1566 | global $active_plan_cache; |
||
1567 | |||
1568 | // this can be expensive to compute so we cache for the duration of a request |
||
1569 | if ( is_array( $active_plan_cache ) && ! empty( $active_plan_cache ) ) { |
||
1570 | return $active_plan_cache; |
||
1571 | } |
||
1572 | |||
1573 | $plan = get_option( 'jetpack_active_plan', array() ); |
||
1574 | |||
1575 | // Set the default options |
||
1576 | $plan = wp_parse_args( $plan, array( |
||
1577 | 'product_slug' => 'jetpack_free', |
||
1578 | 'class' => 'free', |
||
1579 | 'features' => array( |
||
1580 | 'active' => array() |
||
1581 | ), |
||
1582 | ) ); |
||
1583 | |||
1584 | $supports = array(); |
||
1585 | |||
1586 | // Define what paid modules are supported by personal plans |
||
1587 | $personal_plans = array( |
||
1588 | 'jetpack_personal', |
||
1589 | 'jetpack_personal_monthly', |
||
1590 | 'personal-bundle', |
||
1591 | 'personal-bundle-2y', |
||
1592 | ); |
||
1593 | |||
1594 | if ( in_array( $plan['product_slug'], $personal_plans ) ) { |
||
1595 | // special support value, not a module but a separate plugin |
||
1596 | $supports[] = 'akismet'; |
||
1597 | $plan['class'] = 'personal'; |
||
1598 | } |
||
1599 | |||
1600 | // Define what paid modules are supported by premium plans |
||
1601 | $premium_plans = array( |
||
1602 | 'jetpack_premium', |
||
1603 | 'jetpack_premium_monthly', |
||
1604 | 'value_bundle', |
||
1605 | 'value_bundle-2y', |
||
1606 | ); |
||
1607 | |||
1608 | View Code Duplication | if ( in_array( $plan['product_slug'], $premium_plans ) ) { |
|
1609 | $supports[] = 'akismet'; |
||
1610 | $supports[] = 'simple-payments'; |
||
1611 | $supports[] = 'vaultpress'; |
||
1612 | $supports[] = 'videopress'; |
||
1613 | $plan['class'] = 'premium'; |
||
1614 | } |
||
1615 | |||
1616 | // Define what paid modules are supported by professional plans |
||
1617 | $business_plans = array( |
||
1618 | 'jetpack_business', |
||
1619 | 'jetpack_business_monthly', |
||
1620 | 'business-bundle', |
||
1621 | 'business-bundle-2y', |
||
1622 | 'ecommerce-bundle', |
||
1623 | 'ecommerce-bundle-2y', |
||
1624 | 'vip', |
||
1625 | ); |
||
1626 | |||
1627 | View Code Duplication | if ( in_array( $plan['product_slug'], $business_plans ) ) { |
|
1628 | $supports[] = 'akismet'; |
||
1629 | $supports[] = 'simple-payments'; |
||
1630 | $supports[] = 'vaultpress'; |
||
1631 | $supports[] = 'videopress'; |
||
1632 | $plan['class'] = 'business'; |
||
1633 | } |
||
1634 | |||
1635 | // get available features |
||
1636 | foreach ( self::get_available_modules() as $module_slug ) { |
||
1637 | $module = self::get_module( $module_slug ); |
||
1638 | if ( ! isset( $module ) || ! is_array( $module ) ) { |
||
1639 | continue; |
||
1640 | } |
||
1641 | if ( in_array( 'free', $module['plan_classes'] ) || in_array( $plan['class'], $module['plan_classes'] ) ) { |
||
1642 | $supports[] = $module_slug; |
||
1643 | } |
||
1644 | } |
||
1645 | |||
1646 | $plan['supports'] = $supports; |
||
1647 | |||
1648 | $active_plan_cache = $plan; |
||
1649 | |||
1650 | return $plan; |
||
1651 | } |
||
1652 | |||
1653 | /** |
||
1654 | * Determine whether the active plan supports a particular feature |
||
1655 | * |
||
1656 | * @uses Jetpack::get_active_plan() |
||
1657 | * |
||
1658 | * @access public |
||
1659 | * @static |
||
1660 | * |
||
1661 | * @return bool True if plan supports feature, false if not |
||
1662 | */ |
||
1663 | public static function active_plan_supports( $feature ) { |
||
1688 | |||
1689 | /** |
||
1690 | * Is Jetpack in development (offline) mode? |
||
1691 | */ |
||
1692 | public static function is_development_mode() { |
||
1713 | |||
1714 | /** |
||
1715 | * Whether the site is currently onboarding or not. |
||
1716 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
1717 | * |
||
1718 | * @since 5.8 |
||
1719 | * |
||
1720 | * @access public |
||
1721 | * @static |
||
1722 | * |
||
1723 | * @return bool True if the site is currently onboarding, false otherwise |
||
1724 | */ |
||
1725 | public static function is_onboarding() { |
||
1728 | |||
1729 | /** |
||
1730 | * Determines reason for Jetpack development mode. |
||
1731 | */ |
||
1732 | public static function development_mode_trigger_text() { |
||
1748 | /** |
||
1749 | * Get Jetpack development mode notice text and notice class. |
||
1750 | * |
||
1751 | * Mirrors the checks made in Jetpack::is_development_mode |
||
1752 | * |
||
1753 | */ |
||
1754 | public static function show_development_mode_notice() { |
||
1755 | View Code Duplication | if ( Jetpack::is_development_mode() ) { |
|
1756 | $notice = sprintf( |
||
1757 | /* translators: %s is a URL */ |
||
1758 | __( 'In <a href="%s" target="_blank">Development Mode</a>:', 'jetpack' ), |
||
1759 | 'https://jetpack.com/support/development-mode/' |
||
1760 | ); |
||
1761 | |||
1762 | $notice .= ' ' . Jetpack::development_mode_trigger_text(); |
||
1763 | |||
1764 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1765 | } |
||
1766 | |||
1767 | // Throw up a notice if using a development version and as for feedback. |
||
1768 | if ( Jetpack::is_development_version() ) { |
||
1769 | /* translators: %s is a URL */ |
||
1770 | $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/' ); |
||
1771 | |||
1772 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1773 | } |
||
1774 | // Throw up a notice if using staging mode |
||
1775 | if ( Jetpack::is_staging_site() ) { |
||
1776 | /* translators: %s is a URL */ |
||
1777 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
1778 | |||
1779 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1780 | } |
||
1781 | } |
||
1782 | |||
1783 | /** |
||
1784 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1785 | */ |
||
1786 | public static function is_development_version() { |
||
1801 | |||
1802 | /** |
||
1803 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1804 | */ |
||
1805 | public static function is_user_connected( $user_id = false ) { |
||
1813 | |||
1814 | /** |
||
1815 | * Get the wpcom user data of the current|specified connected user. |
||
1816 | */ |
||
1817 | public static function get_connected_user_data( $user_id = null ) { |
||
1841 | |||
1842 | /** |
||
1843 | * Get the wpcom email of the current|specified connected user. |
||
1844 | */ |
||
1845 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
1859 | |||
1860 | /** |
||
1861 | * Get the wpcom email of the master user. |
||
1862 | */ |
||
1863 | public static function get_master_user_email() { |
||
1870 | |||
1871 | function current_user_is_connection_owner() { |
||
1875 | |||
1876 | /** |
||
1877 | * Gets current user IP address. |
||
1878 | * |
||
1879 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
1880 | * |
||
1881 | * @return string Current user IP address. |
||
1882 | */ |
||
1883 | public static function current_user_ip( $check_all_headers = false ) { |
||
1903 | |||
1904 | /** |
||
1905 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
1906 | */ |
||
1907 | function extra_oembed_providers() { |
||
1916 | |||
1917 | /** |
||
1918 | * Synchronize connected user role changes |
||
1919 | */ |
||
1920 | function user_role_change( $user_id ) { |
||
1924 | |||
1925 | /** |
||
1926 | * Loads the currently active modules. |
||
1927 | */ |
||
1928 | public static function load_modules() { |
||
2017 | |||
2018 | /** |
||
2019 | * Check if Jetpack's REST API compat file should be included |
||
2020 | * @action plugins_loaded |
||
2021 | * @return null |
||
2022 | */ |
||
2023 | public function check_rest_api_compat() { |
||
2039 | |||
2040 | /** |
||
2041 | * Gets all plugins currently active in values, regardless of whether they're |
||
2042 | * traditionally activated or network activated. |
||
2043 | * |
||
2044 | * @todo Store the result in core's object cache maybe? |
||
2045 | */ |
||
2046 | public static function get_active_plugins() { |
||
2062 | |||
2063 | /** |
||
2064 | * Gets and parses additional plugin data to send with the heartbeat data |
||
2065 | * |
||
2066 | * @since 3.8.1 |
||
2067 | * |
||
2068 | * @return array Array of plugin data |
||
2069 | */ |
||
2070 | public static function get_parsed_plugin_data() { |
||
2091 | |||
2092 | /** |
||
2093 | * Gets and parses theme data to send with the heartbeat data |
||
2094 | * |
||
2095 | * @since 3.8.1 |
||
2096 | * |
||
2097 | * @return array Array of theme data |
||
2098 | */ |
||
2099 | public static function get_parsed_theme_data() { |
||
2121 | |||
2122 | /** |
||
2123 | * Checks whether a specific plugin is active. |
||
2124 | * |
||
2125 | * We don't want to store these in a static variable, in case |
||
2126 | * there are switch_to_blog() calls involved. |
||
2127 | */ |
||
2128 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
2131 | |||
2132 | /** |
||
2133 | * Check if Jetpack's Open Graph tags should be used. |
||
2134 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
2135 | * |
||
2136 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2137 | * @action plugins_loaded |
||
2138 | * @return null |
||
2139 | */ |
||
2140 | public function check_open_graph() { |
||
2167 | |||
2168 | /** |
||
2169 | * Check if Jetpack's Twitter tags should be used. |
||
2170 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
2171 | * |
||
2172 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
2173 | * @action plugins_loaded |
||
2174 | * @return null |
||
2175 | */ |
||
2176 | public function check_twitter_tags() { |
||
2200 | |||
2201 | /** |
||
2202 | * Allows plugins to submit security reports. |
||
2203 | * |
||
2204 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
2205 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
2206 | * @param array $args See definitions above |
||
2207 | */ |
||
2208 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
2211 | |||
2212 | /* Jetpack Options API */ |
||
2213 | |||
2214 | public static function get_option_names( $type = 'compact' ) { |
||
2217 | |||
2218 | /** |
||
2219 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
2220 | * |
||
2221 | * @param string $name Option name |
||
2222 | * @param mixed $default (optional) |
||
2223 | */ |
||
2224 | public static function get_option( $name, $default = false ) { |
||
2227 | |||
2228 | /** |
||
2229 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
2230 | * |
||
2231 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
2232 | * @param string $name Option name |
||
2233 | * @param mixed $value Option value |
||
2234 | */ |
||
2235 | public static function update_option( $name, $value ) { |
||
2239 | |||
2240 | /** |
||
2241 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
2242 | * |
||
2243 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
2244 | * @param array $array array( option name => option value, ... ) |
||
2245 | */ |
||
2246 | public static function update_options( $array ) { |
||
2250 | |||
2251 | /** |
||
2252 | * Deletes the given option. May be passed multiple option names as an array. |
||
2253 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
2254 | * |
||
2255 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
2256 | * @param string|array $names |
||
2257 | */ |
||
2258 | public static function delete_option( $names ) { |
||
2262 | |||
2263 | /** |
||
2264 | * Enters a user token into the user_tokens option |
||
2265 | * |
||
2266 | * @param int $user_id |
||
2267 | * @param string $token |
||
2268 | * return bool |
||
2269 | */ |
||
2270 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
2284 | |||
2285 | /** |
||
2286 | * Returns an array of all PHP files in the specified absolute path. |
||
2287 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
2288 | * |
||
2289 | * @param string $absolute_path The absolute path of the directory to search. |
||
2290 | * @return array Array of absolute paths to the PHP files. |
||
2291 | */ |
||
2292 | public static function glob_php( $absolute_path ) { |
||
2321 | |||
2322 | public static function activate_new_modules( $redirect = false ) { |
||
2380 | |||
2381 | /** |
||
2382 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2383 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2384 | */ |
||
2385 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
2442 | |||
2443 | /** |
||
2444 | * Default modules loaded on activation. |
||
2445 | */ |
||
2446 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
2477 | |||
2478 | /** |
||
2479 | * Checks activated modules during auto-activation to determine |
||
2480 | * if any of those modules are being deprecated. If so, close |
||
2481 | * them out, and add any replacement modules. |
||
2482 | * |
||
2483 | * Runs at priority 99 by default. |
||
2484 | * |
||
2485 | * This is run late, so that it can still activate a module if |
||
2486 | * the new module is a replacement for another that the user |
||
2487 | * currently has active, even if something at the normal priority |
||
2488 | * would kibosh everything. |
||
2489 | * |
||
2490 | * @since 2.6 |
||
2491 | * @uses jetpack_get_default_modules filter |
||
2492 | * @param array $modules |
||
2493 | * @return array |
||
2494 | */ |
||
2495 | function handle_deprecated_modules( $modules ) { |
||
2521 | |||
2522 | /** |
||
2523 | * Checks activated plugins during auto-activation to determine |
||
2524 | * if any of those plugins are in the list with a corresponding module |
||
2525 | * that is not compatible with the plugin. The module will not be allowed |
||
2526 | * to auto-activate. |
||
2527 | * |
||
2528 | * @since 2.6 |
||
2529 | * @uses jetpack_get_default_modules filter |
||
2530 | * @param array $modules |
||
2531 | * @return array |
||
2532 | */ |
||
2533 | function filter_default_modules( $modules ) { |
||
2557 | |||
2558 | /** |
||
2559 | * Extract a module's slug from its full path. |
||
2560 | */ |
||
2561 | public static function get_module_slug( $file ) { |
||
2564 | |||
2565 | /** |
||
2566 | * Generate a module's path from its slug. |
||
2567 | */ |
||
2568 | public static function get_module_path( $slug ) { |
||
2571 | |||
2572 | /** |
||
2573 | * Load module data from module file. Headers differ from WordPress |
||
2574 | * plugin headers to avoid them being identified as standalone |
||
2575 | * plugins on the WordPress plugins page. |
||
2576 | */ |
||
2577 | public static function get_module( $module ) { |
||
2668 | |||
2669 | /** |
||
2670 | * Like core's get_file_data implementation, but caches the result. |
||
2671 | */ |
||
2672 | public static function get_file_data( $file, $headers ) { |
||
2700 | |||
2701 | |||
2702 | /** |
||
2703 | * Return translated module tag. |
||
2704 | * |
||
2705 | * @param string $tag Tag as it appears in each module heading. |
||
2706 | * |
||
2707 | * @return mixed |
||
2708 | */ |
||
2709 | public static function translate_module_tag( $tag ) { |
||
2712 | |||
2713 | /** |
||
2714 | * Get i18n strings as a JSON-encoded string |
||
2715 | * |
||
2716 | * @return string The locale as JSON |
||
2717 | */ |
||
2718 | public static function get_i18n_data_json() { |
||
2748 | |||
2749 | /** |
||
2750 | * Add locale data setup to wp-i18n |
||
2751 | * |
||
2752 | * Any Jetpack script that depends on wp-i18n should use this method to set up the locale. |
||
2753 | * |
||
2754 | * The locale setup depends on an adding inline script. This is error-prone and could easily |
||
2755 | * result in multiple additions of the same script when exactly 0 or 1 is desireable. |
||
2756 | * |
||
2757 | * This method provides a safe way to request the setup multiple times but add the script at |
||
2758 | * most once. |
||
2759 | * |
||
2760 | * @since 6.7.0 |
||
2761 | * |
||
2762 | * @return void |
||
2763 | */ |
||
2764 | public static function setup_wp_i18n_locale_data() { |
||
2774 | |||
2775 | /** |
||
2776 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2777 | * |
||
2778 | * @since 3.9.2 |
||
2779 | * |
||
2780 | * @param array $modules |
||
2781 | * |
||
2782 | * @return string|void |
||
2783 | */ |
||
2784 | public static function get_translated_modules( $modules ) { |
||
2797 | |||
2798 | /** |
||
2799 | * Get a list of activated modules as an array of module slugs. |
||
2800 | */ |
||
2801 | public static function get_active_modules() { |
||
2833 | |||
2834 | /** |
||
2835 | * Check whether or not a Jetpack module is active. |
||
2836 | * |
||
2837 | * @param string $module The slug of a Jetpack module. |
||
2838 | * @return bool |
||
2839 | * |
||
2840 | * @static |
||
2841 | */ |
||
2842 | public static function is_module_active( $module ) { |
||
2845 | |||
2846 | public static function is_module( $module ) { |
||
2849 | |||
2850 | /** |
||
2851 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2852 | * |
||
2853 | * @param bool $catch True to start catching, False to stop. |
||
2854 | * |
||
2855 | * @static |
||
2856 | */ |
||
2857 | public static function catch_errors( $catch ) { |
||
2870 | |||
2871 | /** |
||
2872 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2873 | */ |
||
2874 | public static function catch_errors_on_shutdown() { |
||
2877 | |||
2878 | /** |
||
2879 | * Rewrite any string to make paths easier to read. |
||
2880 | * |
||
2881 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2882 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2883 | * |
||
2884 | * @param $string |
||
2885 | * @return mixed |
||
2886 | */ |
||
2887 | public static function alias_directories( $string ) { |
||
2895 | |||
2896 | public static function activate_default_modules( |
||
3034 | |||
3035 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
3132 | |||
3133 | function activate_module_actions( $module ) { |
||
3136 | |||
3137 | public static function deactivate_module( $module ) { |
||
3164 | |||
3165 | public static function enable_module_configurable( $module ) { |
||
3169 | |||
3170 | /** |
||
3171 | * Composes a module configure URL. It uses Jetpack settings search as default value |
||
3172 | * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter |
||
3173 | * |
||
3174 | * @param string $module Module slug |
||
3175 | * @return string $url module configuration URL |
||
3176 | */ |
||
3177 | public static function module_configuration_url( $module ) { |
||
3191 | |||
3192 | public static function module_configuration_load( $module, $method ) { |
||
3196 | |||
3197 | public static function module_configuration_head( $module, $method ) { |
||
3201 | |||
3202 | public static function module_configuration_screen( $module, $method ) { |
||
3206 | |||
3207 | public static function module_configuration_activation_screen( $module, $method ) { |
||
3211 | |||
3212 | /* Installation */ |
||
3213 | |||
3214 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
3254 | |||
3255 | /** |
||
3256 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
3257 | * @static |
||
3258 | */ |
||
3259 | public static function plugin_activation( $network_wide ) { |
||
3276 | |||
3277 | public static function get_activation_source( $referer_url ) { |
||
3326 | |||
3327 | /** |
||
3328 | * Runs before bumping version numbers up to a new version |
||
3329 | * @param string $version Version:timestamp |
||
3330 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
3331 | * @return null [description] |
||
3332 | */ |
||
3333 | public static function do_version_bump( $version, $old_version ) { |
||
3344 | |||
3345 | /** |
||
3346 | * Sets the internal version number and activation state. |
||
3347 | * @static |
||
3348 | */ |
||
3349 | public static function plugin_initialize() { |
||
3366 | |||
3367 | /** |
||
3368 | * Removes all connection options |
||
3369 | * @static |
||
3370 | */ |
||
3371 | public static function plugin_deactivation( ) { |
||
3380 | |||
3381 | /** |
||
3382 | * Disconnects from the Jetpack servers. |
||
3383 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
3384 | * @static |
||
3385 | */ |
||
3386 | public static function disconnect( $update_activated_state = true ) { |
||
3447 | |||
3448 | /** |
||
3449 | * Unlinks the current user from the linked WordPress.com user |
||
3450 | */ |
||
3451 | public static function unlink_user( $user_id = null ) { |
||
3482 | |||
3483 | /** |
||
3484 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
3485 | */ |
||
3486 | public static function try_registration() { |
||
3514 | |||
3515 | /** |
||
3516 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3517 | * |
||
3518 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3519 | */ |
||
3520 | public static function log( $code, $data = null ) { |
||
3560 | |||
3561 | /** |
||
3562 | * Get the internal event log. |
||
3563 | * |
||
3564 | * @param $event (string) - only return the specific log events |
||
3565 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3566 | * |
||
3567 | * @return array of log events || WP_Error for invalid params |
||
3568 | */ |
||
3569 | public static function get_log( $event = false, $num = false ) { |
||
3605 | |||
3606 | /** |
||
3607 | * Log modification of important settings. |
||
3608 | */ |
||
3609 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3616 | |||
3617 | /** |
||
3618 | * Return stat data for WPCOM sync |
||
3619 | */ |
||
3620 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3634 | |||
3635 | /** |
||
3636 | * Get additional stat data to sync to WPCOM |
||
3637 | */ |
||
3638 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3649 | |||
3650 | private static function get_site_user_count() { |
||
3665 | |||
3666 | /* Admin Pages */ |
||
3667 | |||
3668 | function admin_init() { |
||
3717 | |||
3718 | function admin_body_class( $admin_body_class = '' ) { |
||
3726 | |||
3727 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3730 | |||
3731 | /** |
||
3732 | * Call this function if you want the Big Jetpack Manage Notice to show up. |
||
3733 | * |
||
3734 | * @return null |
||
3735 | */ |
||
3736 | function prepare_manage_jetpack_notice() { |
||
3741 | |||
3742 | function manage_activate_screen() { |
||
3745 | /** |
||
3746 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3747 | * This function artificially throws errors for such cases (whitelisted). |
||
3748 | * |
||
3749 | * @param string $plugin The activated plugin. |
||
3750 | */ |
||
3751 | function throw_error_on_activate_plugin( $plugin ) { |
||
3775 | |||
3776 | function intercept_plugin_error_scrape_init() { |
||
3779 | |||
3780 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3791 | |||
3792 | function add_remote_request_handlers() { |
||
3796 | |||
3797 | function remote_request_handlers() { |
||
3837 | |||
3838 | /** |
||
3839 | * Uploads a file gotten from the global $_FILES. |
||
3840 | * If `$update_media_item` is true and `post_id` is defined |
||
3841 | * the attachment file of the media item (gotten through of the post_id) |
||
3842 | * will be updated instead of add a new one. |
||
3843 | * |
||
3844 | * @param boolean $update_media_item - update media attachment |
||
3845 | * @return array - An array describing the uploadind files process |
||
3846 | */ |
||
3847 | function upload_handler( $update_media_item = false ) { |
||
3969 | |||
3970 | /** |
||
3971 | * Add help to the Jetpack page |
||
3972 | * |
||
3973 | * @since Jetpack (1.2.3) |
||
3974 | * @return false if not the Jetpack page |
||
3975 | */ |
||
3976 | function admin_help() { |
||
4017 | |||
4018 | function admin_menu_css() { |
||
4021 | |||
4022 | function admin_menu_order() { |
||
4025 | |||
4026 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
4041 | |||
4042 | function admin_head() { |
||
4047 | |||
4048 | function admin_banner_styles() { |
||
4069 | |||
4070 | function plugin_action_links( $actions ) { |
||
4085 | |||
4086 | /** |
||
4087 | * This is the first banner |
||
4088 | * It should be visible only to user that can update the option |
||
4089 | * Are not connected |
||
4090 | * |
||
4091 | * @return null |
||
4092 | */ |
||
4093 | function admin_jetpack_manage_notice() { |
||
4123 | |||
4124 | /** |
||
4125 | * Returns the url that the user clicks to remove the notice for the big banner |
||
4126 | * @return string |
||
4127 | */ |
||
4128 | function opt_out_jetpack_manage_url() { |
||
4132 | /** |
||
4133 | * Returns the url that the user clicks to opt in to Jetpack Manage |
||
4134 | * @return string |
||
4135 | */ |
||
4136 | function opt_in_jetpack_manage_url() { |
||
4139 | |||
4140 | function opt_in_jetpack_manage_notice() { |
||
4150 | /** |
||
4151 | * Determines whether to show the notice of not true = display notice |
||
4152 | * @return bool |
||
4153 | */ |
||
4154 | function can_display_jetpack_manage_notice() { |
||
4176 | |||
4177 | /* |
||
4178 | * Registration flow: |
||
4179 | * 1 - ::admin_page_load() action=register |
||
4180 | * 2 - ::try_registration() |
||
4181 | * 3 - ::register() |
||
4182 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
4183 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
4184 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
4185 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
4186 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
4187 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
4188 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
4189 | * jetpack_id, jetpack_secret, jetpack_public |
||
4190 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
4191 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
4192 | * 5 - user logs in with WP.com account |
||
4193 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
4194 | * - Jetpack_Client_Server::authorize() |
||
4195 | * - Jetpack_Client_Server::get_token() |
||
4196 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
4197 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
4198 | * - which responds with access_token, token_type, scope |
||
4199 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
4200 | * - Jetpack::activate_default_modules() |
||
4201 | * - Deactivates deprecated plugins |
||
4202 | * - Activates all default modules |
||
4203 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
4204 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
4205 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
4206 | * Done! |
||
4207 | */ |
||
4208 | |||
4209 | /** |
||
4210 | * Handles the page load events for the Jetpack admin page |
||
4211 | */ |
||
4212 | function admin_page_load() { |
||
4487 | |||
4488 | function admin_notices() { |
||
4585 | |||
4586 | /** |
||
4587 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4588 | */ |
||
4589 | function stat( $group, $detail ) { |
||
4594 | |||
4595 | /** |
||
4596 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4597 | */ |
||
4598 | function do_stats( $method = '' ) { |
||
4613 | |||
4614 | /** |
||
4615 | * Runs stats code for a one-off, server-side. |
||
4616 | * |
||
4617 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4618 | * |
||
4619 | * @return bool If it worked. |
||
4620 | */ |
||
4621 | static function do_server_side_stat( $args ) { |
||
4631 | |||
4632 | /** |
||
4633 | * Builds the stats url. |
||
4634 | * |
||
4635 | * @param $args array|string The arguments to append to the URL. |
||
4636 | * |
||
4637 | * @return string The URL to be pinged. |
||
4638 | */ |
||
4639 | static function build_stats_url( $args ) { |
||
4659 | |||
4660 | static function translate_current_user_to_role() { |
||
4669 | |||
4670 | static function translate_user_to_role( $user ) { |
||
4679 | |||
4680 | static function translate_role_to_cap( $role ) { |
||
4687 | |||
4688 | static function sign_role( $role, $user_id = null ) { |
||
4704 | |||
4705 | |||
4706 | /** |
||
4707 | * Builds a URL to the Jetpack connection auth page |
||
4708 | * |
||
4709 | * @since 3.9.5 |
||
4710 | * |
||
4711 | * @param bool $raw If true, URL will not be escaped. |
||
4712 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4713 | * If string, will be a custom redirect. |
||
4714 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4715 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
4716 | * |
||
4717 | * @return string Connect URL |
||
4718 | */ |
||
4719 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
4720 | $site_id = Jetpack_Options::get_option( 'id' ); |
||
4721 | $token = Jetpack_Options::get_option( 'blog_token' ); |
||
4722 | |||
4723 | if ( $register || ! $token || ! $site_id ) { |
||
4724 | $url = Jetpack::nonce_url_no_esc( Jetpack::admin_url( 'action=register' ), 'jetpack-register' ); |
||
4725 | |||
4726 | if ( ! empty( $redirect ) ) { |
||
4727 | $url = add_query_arg( |
||
4728 | 'redirect', |
||
4729 | urlencode( wp_validate_redirect( esc_url_raw( $redirect ) ) ), |
||
4730 | $url |
||
4731 | ); |
||
4732 | } |
||
4733 | |||
4734 | if( is_network_admin() ) { |
||
4735 | $url = add_query_arg( 'is_multisite', network_admin_url( 'admin.php?page=jetpack-settings' ), $url ); |
||
4736 | } |
||
4737 | } else { |
||
4738 | |||
4739 | // Let's check the existing blog token to see if we need to re-register. We only check once per minute |
||
4740 | // because otherwise this logic can get us in to a loop. |
||
4741 | $last_connect_url_check = intval( Jetpack_Options::get_raw_option( 'jetpack_last_connect_url_check' ) ); |
||
4742 | if ( ! $last_connect_url_check || ( time() - $last_connect_url_check ) > MINUTE_IN_SECONDS ) { |
||
4743 | Jetpack_Options::update_raw_option( 'jetpack_last_connect_url_check', time() ); |
||
4744 | |||
4745 | $response = Jetpack_Client::wpcom_json_api_request_as_blog( |
||
4746 | sprintf( '/sites/%d', $site_id ) .'?force=wpcom', |
||
4747 | '1.1' |
||
4748 | ); |
||
4749 | |||
4750 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
||
4751 | // Generating a register URL instead to refresh the existing token |
||
4752 | return $this->build_connect_url( $raw, $redirect, $from, true ); |
||
4753 | } |
||
4754 | } |
||
4755 | |||
4756 | if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && include_once JETPACK__GLOTPRESS_LOCALES_PATH ) { |
||
4757 | $gp_locale = GP_Locales::by_field( 'wp_locale', get_locale() ); |
||
4758 | } |
||
4759 | |||
4760 | $role = self::translate_current_user_to_role(); |
||
4761 | $signed_role = self::sign_role( $role ); |
||
4762 | |||
4763 | $user = wp_get_current_user(); |
||
4764 | |||
4765 | $jetpack_admin_page = esc_url_raw( admin_url( 'admin.php?page=jetpack' ) ); |
||
4766 | $redirect = $redirect |
||
4767 | ? wp_validate_redirect( esc_url_raw( $redirect ), $jetpack_admin_page ) |
||
4768 | : $jetpack_admin_page; |
||
4769 | |||
4770 | if( isset( $_REQUEST['is_multisite'] ) ) { |
||
4771 | $redirect = Jetpack_Network::init()->get_url( 'network_admin_page' ); |
||
4772 | } |
||
4773 | |||
4774 | $secrets = Jetpack::generate_secrets( 'authorize', false, 2 * HOUR_IN_SECONDS ); |
||
4775 | |||
4776 | /** |
||
4777 | * Filter the type of authorization. |
||
4778 | * 'calypso' completes authorization on wordpress.com/jetpack/connect |
||
4779 | * while 'jetpack' ( or any other value ) completes the authorization at jetpack.wordpress.com. |
||
4780 | * |
||
4781 | * @since 4.3.3 |
||
4782 | * |
||
4783 | * @param string $auth_type Defaults to 'calypso', can also be 'jetpack'. |
||
4784 | */ |
||
4785 | $auth_type = apply_filters( 'jetpack_auth_type', 'calypso' ); |
||
4786 | |||
4787 | $tracks_identity = jetpack_tracks_get_identity( get_current_user_id() ); |
||
4788 | |||
4789 | $args = urlencode_deep( |
||
4790 | array( |
||
4791 | 'response_type' => 'code', |
||
4792 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
4793 | 'redirect_uri' => add_query_arg( |
||
4794 | array( |
||
4795 | 'action' => 'authorize', |
||
4796 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
4797 | 'redirect' => urlencode( $redirect ), |
||
4798 | ), |
||
4799 | esc_url( admin_url( 'admin.php?page=jetpack' ) ) |
||
4800 | ), |
||
4801 | 'state' => $user->ID, |
||
4802 | 'scope' => $signed_role, |
||
4803 | 'user_email' => $user->user_email, |
||
4804 | 'user_login' => $user->user_login, |
||
4805 | 'is_active' => Jetpack::is_active(), |
||
4806 | 'jp_version' => JETPACK__VERSION, |
||
4807 | 'auth_type' => $auth_type, |
||
4808 | 'secret' => $secrets['secret_1'], |
||
4809 | 'locale' => ( isset( $gp_locale ) && isset( $gp_locale->slug ) ) ? $gp_locale->slug : '', |
||
4810 | 'blogname' => get_option( 'blogname' ), |
||
4811 | 'site_url' => site_url(), |
||
4812 | 'home_url' => home_url(), |
||
4813 | 'site_icon' => get_site_icon_url(), |
||
4814 | 'site_lang' => get_locale(), |
||
4815 | '_ui' => $tracks_identity['_ui'], |
||
4816 | '_ut' => $tracks_identity['_ut'], |
||
4817 | 'site_created' => Jetpack::get_assumed_site_creation_date(), |
||
4818 | ) |
||
4819 | ); |
||
4820 | |||
4821 | self::apply_activation_source_to_args( $args ); |
||
4822 | |||
4823 | $url = add_query_arg( $args, Jetpack::api_url( 'authorize' ) ); |
||
4824 | } |
||
4825 | |||
4826 | if ( $from ) { |
||
4827 | $url = add_query_arg( 'from', $from, $url ); |
||
4828 | } |
||
4829 | |||
4830 | // Ensure that class to get the affiliate code is loaded |
||
4831 | if ( ! class_exists( 'Jetpack_Affiliate' ) ) { |
||
4832 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-affiliate.php'; |
||
4833 | } |
||
4834 | // Get affiliate code and add it to the URL |
||
4835 | $url = Jetpack_Affiliate::init()->add_code_as_query_arg( $url ); |
||
4836 | |||
4837 | if ( isset( $_GET['calypso_env'] ) ) { |
||
4838 | $url = add_query_arg( 'calypso_env', sanitize_key( $_GET['calypso_env'] ), $url ); |
||
4839 | } |
||
4840 | |||
4841 | return $raw ? $url : esc_url( $url ); |
||
4842 | } |
||
4843 | |||
4844 | /** |
||
4845 | * Get our assumed site creation date. |
||
4846 | * Calculated based on the earlier date of either: |
||
4847 | * - Earliest admin user registration date. |
||
4848 | * - Earliest date of post of any post type. |
||
4849 | * |
||
4850 | * @since 7.2.0 |
||
4851 | * |
||
4852 | * @return string Assumed site creation date and time. |
||
4853 | */ |
||
4854 | public static function get_assumed_site_creation_date() { |
||
4855 | $earliest_registered_users = get_users( array( |
||
4856 | 'role' => 'administrator', |
||
4857 | 'orderby' => 'user_registered', |
||
4858 | 'order' => 'ASC', |
||
4859 | 'fields' => array( 'user_registered' ), |
||
4860 | 'number' => 1, |
||
4861 | ) ); |
||
4862 | $earliest_registration_date = $earliest_registered_users[0]->user_registered; |
||
4863 | |||
4864 | $earliest_posts = get_posts( array( |
||
4865 | 'posts_per_page' => 1, |
||
4866 | 'post_type' => 'any', |
||
4867 | 'post_status' => 'any', |
||
4868 | 'orderby' => 'date', |
||
4869 | 'order' => 'ASC', |
||
4870 | ) ); |
||
4871 | |||
4872 | // If there are no posts at all, we'll count only on user registration date. |
||
4873 | if ( $earliest_posts ) { |
||
4874 | $earliest_post_date = $earliest_posts[0]->post_date; |
||
4875 | } else { |
||
4876 | $earliest_post_date = PHP_INT_MAX; |
||
4877 | } |
||
4878 | |||
4879 | return min( $earliest_registration_date, $earliest_post_date ); |
||
4880 | } |
||
4881 | |||
4882 | public static function apply_activation_source_to_args( &$args ) { |
||
4883 | list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' ); |
||
4884 | |||
4885 | if ( $activation_source_name ) { |
||
4886 | $args['_as'] = urlencode( $activation_source_name ); |
||
4887 | } |
||
4888 | |||
4889 | if ( $activation_source_keyword ) { |
||
4890 | $args['_ak'] = urlencode( $activation_source_keyword ); |
||
4891 | } |
||
4892 | } |
||
4893 | |||
4894 | function build_reconnect_url( $raw = false ) { |
||
4895 | $url = wp_nonce_url( Jetpack::admin_url( 'action=reconnect' ), 'jetpack-reconnect' ); |
||
4896 | return $raw ? $url : esc_url( $url ); |
||
4897 | } |
||
4898 | |||
4899 | public static function admin_url( $args = null ) { |
||
4900 | $args = wp_parse_args( $args, array( 'page' => 'jetpack' ) ); |
||
4901 | $url = add_query_arg( $args, admin_url( 'admin.php' ) ); |
||
4902 | return $url; |
||
4903 | } |
||
4904 | |||
4905 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
4906 | $actionurl = str_replace( '&', '&', $actionurl ); |
||
4907 | return add_query_arg( $name, wp_create_nonce( $action ), $actionurl ); |
||
4908 | } |
||
4909 | |||
4910 | function dismiss_jetpack_notice() { |
||
4911 | |||
4912 | if ( ! isset( $_GET['jetpack-notice'] ) ) { |
||
4913 | return; |
||
4914 | } |
||
4915 | |||
4916 | switch( $_GET['jetpack-notice'] ) { |
||
4917 | case 'dismiss': |
||
4918 | if ( check_admin_referer( 'jetpack-deactivate' ) && ! is_plugin_active_for_network( plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ) ) ) { |
||
4919 | |||
4920 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
4921 | deactivate_plugins( JETPACK__PLUGIN_DIR . 'jetpack.php', false, false ); |
||
4922 | wp_safe_redirect( admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s=' ); |
||
4923 | } |
||
4924 | break; |
||
4925 | View Code Duplication | case 'jetpack-manage-opt-out': |
|
4926 | |||
4927 | if ( check_admin_referer( 'jetpack_manage_banner_opt_out' ) ) { |
||
4928 | // Don't show the banner again |
||
4929 | |||
4930 | Jetpack_Options::update_option( 'dismissed_manage_banner', true ); |
||
4931 | // redirect back to the page that had the notice |
||
4932 | if ( wp_get_referer() ) { |
||
4933 | wp_safe_redirect( wp_get_referer() ); |
||
4934 | } else { |
||
4935 | // Take me to Jetpack |
||
4936 | wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) ); |
||
4937 | } |
||
4938 | } |
||
4939 | break; |
||
4940 | View Code Duplication | case 'jetpack-protect-multisite-opt-out': |
|
4941 | |||
4942 | if ( check_admin_referer( 'jetpack_protect_multisite_banner_opt_out' ) ) { |
||
4943 | // Don't show the banner again |
||
4944 | |||
4945 | update_site_option( 'jetpack_dismissed_protect_multisite_banner', true ); |
||
4946 | // redirect back to the page that had the notice |
||
4947 | if ( wp_get_referer() ) { |
||
4948 | wp_safe_redirect( wp_get_referer() ); |
||
4949 | } else { |
||
4950 | // Take me to Jetpack |
||
4951 | wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) ); |
||
4952 | } |
||
4953 | } |
||
4954 | break; |
||
4955 | case 'jetpack-manage-opt-in': |
||
4956 | if ( check_admin_referer( 'jetpack_manage_banner_opt_in' ) ) { |
||
4957 | // This makes sure that we are redirect to jetpack home so that we can see the Success Message. |
||
4958 | |||
4959 | $redirection_url = Jetpack::admin_url(); |
||
4960 | remove_action( 'jetpack_pre_activate_module', array( Jetpack_Admin::init(), 'fix_redirect' ) ); |
||
4961 | |||
4962 | // Don't redirect form the Jetpack Setting Page |
||
4963 | $referer_parsed = wp_parse_url ( wp_get_referer() ); |
||
4964 | // check that we do have a wp_get_referer and the query paramater is set orderwise go to the Jetpack Home |
||
4965 | if ( isset( $referer_parsed['query'] ) && false !== strpos( $referer_parsed['query'], 'page=jetpack_modules' ) ) { |
||
4966 | // Take the user to Jetpack home except when on the setting page |
||
4967 | $redirection_url = wp_get_referer(); |
||
4968 | add_action( 'jetpack_pre_activate_module', array( Jetpack_Admin::init(), 'fix_redirect' ) ); |
||
4969 | } |
||
4970 | // Also update the JSON API FULL MANAGEMENT Option |
||
4971 | Jetpack::activate_module( 'manage', false, false ); |
||
4972 | |||
4973 | // Special Message when option in. |
||
4974 | Jetpack::state( 'optin-manage', 'true' ); |
||
4975 | // Activate the Module if not activated already |
||
4976 | |||
4977 | // Redirect properly |
||
4978 | wp_safe_redirect( $redirection_url ); |
||
4979 | |||
4980 | } |
||
4981 | break; |
||
4982 | } |
||
4983 | } |
||
4984 | |||
4985 | public static function admin_screen_configure_module( $module_id ) { |
||
4986 | |||
4987 | // User that doesn't have 'jetpack_configure_modules' will never end up here since Jetpack Landing Page woun't let them. |
||
4988 | if ( ! in_array( $module_id, Jetpack::get_active_modules() ) && current_user_can( 'manage_options' ) ) { |
||
4989 | if ( has_action( 'display_activate_module_setting_' . $module_id ) ) { |
||
4990 | /** |
||
4991 | * Fires to diplay a custom module activation screen. |
||
4992 | * |
||
4993 | * To add a module actionation screen use Jetpack::module_configuration_activation_screen method. |
||
4994 | * Example: Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) ); |
||
4995 | * |
||
4996 | * @module manage |
||
4997 | * |
||
4998 | * @since 3.8.0 |
||
4999 | * |
||
5000 | * @param int $module_id Module ID. |
||
5001 | */ |
||
5002 | do_action( 'display_activate_module_setting_' . $module_id ); |
||
5003 | } else { |
||
5004 | self::display_activate_module_link( $module_id ); |
||
5005 | } |
||
5006 | |||
5007 | return false; |
||
5008 | } ?> |
||
5009 | |||
5010 | <div id="jp-settings-screen" style="position: relative"> |
||
5011 | <h3> |
||
5012 | <?php |
||
5013 | $module = Jetpack::get_module( $module_id ); |
||
5014 | printf( __( 'Configure %s', 'jetpack' ), $module['name'] ); |
||
5015 | ?> |
||
5016 | </h3> |
||
5017 | <?php |
||
5018 | /** |
||
5019 | * Fires within the displayed message when a feature configuation is updated. |
||
5020 | * |
||
5021 | * @since 3.4.0 |
||
5022 | * |
||
5023 | * @param int $module_id Module ID. |
||
5024 | */ |
||
5025 | do_action( 'jetpack_notices_update_settings', $module_id ); |
||
5026 | /** |
||
5027 | * Fires when a feature configuation screen is loaded. |
||
5028 | * The dynamic part of the hook, $module_id, is the module ID. |
||
5029 | * |
||
5030 | * @since 1.1.0 |
||
5031 | */ |
||
5032 | do_action( 'jetpack_module_configuration_screen_' . $module_id ); |
||
5033 | ?> |
||
5034 | </div><?php |
||
5035 | } |
||
5036 | |||
5037 | /** |
||
5038 | * Display link to activate the module to see the settings screen. |
||
5039 | * @param string $module_id |
||
5040 | * @return null |
||
5041 | */ |
||
5042 | public static function display_activate_module_link( $module_id ) { |
||
5043 | |||
5044 | $info = Jetpack::get_module( $module_id ); |
||
5045 | $extra = ''; |
||
5046 | $activate_url = wp_nonce_url( |
||
5047 | Jetpack::admin_url( |
||
5048 | array( |
||
5049 | 'page' => 'jetpack', |
||
5050 | 'action' => 'activate', |
||
5051 | 'module' => $module_id, |
||
5052 | ) |
||
5053 | ), |
||
5054 | "jetpack_activate-$module_id" |
||
5055 | ); |
||
5056 | |||
5057 | ?> |
||
5058 | |||
5059 | <div class="wrap configure-module"> |
||
5060 | <div id="jp-settings-screen"> |
||
5061 | <?php |
||
5062 | if ( $module_id == 'json-api' ) { |
||
5063 | |||
5064 | $info['name'] = esc_html__( 'Activate Site Management and JSON API', 'jetpack' ); |
||
5065 | |||
5066 | $activate_url = Jetpack::init()->opt_in_jetpack_manage_url(); |
||
5067 | |||
5068 | $info['description'] = sprintf( __( 'Manage your multiple Jetpack sites from our centralized dashboard at wordpress.com/sites. <a href="%s" target="_blank">Learn more</a>.', 'jetpack' ), 'https://jetpack.com/support/site-management' ); |
||
5069 | |||
5070 | // $extra = __( 'To use Site Management, you need to first activate JSON API to allow remote management of your site. ', 'jetpack' ); |
||
5071 | } ?> |
||
5072 | |||
5073 | <h3><?php echo esc_html( $info['name'] ); ?></h3> |
||
5074 | <div class="narrow"> |
||
5075 | <p><?php echo $info['description']; ?></p> |
||
5076 | <?php if( $extra ) { ?> |
||
5077 | <p><?php echo esc_html( $extra ); ?></p> |
||
5078 | <?php } ?> |
||
5079 | <p> |
||
5080 | <?php |
||
5081 | if( wp_get_referer() ) { |
||
5082 | printf( __( '<a class="button-primary" href="%s">Activate Now</a> or <a href="%s" >return to previous page</a>.', 'jetpack' ) , $activate_url, wp_get_referer() ); |
||
5083 | } else { |
||
5084 | printf( __( '<a class="button-primary" href="%s">Activate Now</a>', 'jetpack' ) , $activate_url ); |
||
5085 | } ?> |
||
5086 | </p> |
||
5087 | </div> |
||
5088 | |||
5089 | </div> |
||
5090 | </div> |
||
5091 | |||
5092 | <?php |
||
5093 | } |
||
5094 | |||
5095 | public static function sort_modules( $a, $b ) { |
||
5096 | if ( $a['sort'] == $b['sort'] ) |
||
5097 | return 0; |
||
5098 | |||
5099 | return ( $a['sort'] < $b['sort'] ) ? -1 : 1; |
||
5100 | } |
||
5101 | |||
5102 | function ajax_recheck_ssl() { |
||
5103 | check_ajax_referer( 'recheck-ssl', 'ajax-nonce' ); |
||
5104 | $result = Jetpack::permit_ssl( true ); |
||
5105 | wp_send_json( array( |
||
5106 | 'enabled' => $result, |
||
5107 | 'message' => get_transient( 'jetpack_https_test_message' ) |
||
5108 | ) ); |
||
5109 | } |
||
5110 | |||
5111 | /* Client API */ |
||
5112 | |||
5113 | /** |
||
5114 | * Returns the requested Jetpack API URL |
||
5115 | * |
||
5116 | * @return string |
||
5117 | */ |
||
5118 | public static function api_url( $relative_url ) { |
||
5119 | return trailingslashit( JETPACK__API_BASE . $relative_url ) . JETPACK__API_VERSION . '/'; |
||
5120 | } |
||
5121 | |||
5122 | /** |
||
5123 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
5124 | */ |
||
5125 | public static function fix_url_for_bad_hosts( $url ) { |
||
5126 | if ( 0 !== strpos( $url, 'https://' ) ) { |
||
5127 | return $url; |
||
5128 | } |
||
5129 | |||
5130 | switch ( JETPACK_CLIENT__HTTPS ) { |
||
5131 | case 'ALWAYS' : |
||
5132 | return $url; |
||
5133 | case 'NEVER' : |
||
5134 | return set_url_scheme( $url, 'http' ); |
||
5135 | // default : case 'AUTO' : |
||
5136 | } |
||
5137 | |||
5138 | // we now return the unmodified SSL URL by default, as a security precaution |
||
5139 | return $url; |
||
5140 | } |
||
5141 | |||
5142 | /** |
||
5143 | * Create a random secret for validating onboarding payload |
||
5144 | * |
||
5145 | * @return string Secret token |
||
5146 | */ |
||
5147 | public static function create_onboarding_token() { |
||
5148 | if ( false === ( $token = Jetpack_Options::get_option( 'onboarding' ) ) ) { |
||
5149 | $token = wp_generate_password( 32, false ); |
||
5150 | Jetpack_Options::update_option( 'onboarding', $token ); |
||
5151 | } |
||
5152 | |||
5153 | return $token; |
||
5154 | } |
||
5155 | |||
5156 | /** |
||
5157 | * Remove the onboarding token |
||
5158 | * |
||
5159 | * @return bool True on success, false on failure |
||
5160 | */ |
||
5161 | public static function invalidate_onboarding_token() { |
||
5162 | return Jetpack_Options::delete_option( 'onboarding' ); |
||
5163 | } |
||
5164 | |||
5165 | /** |
||
5166 | * Validate an onboarding token for a specific action |
||
5167 | * |
||
5168 | * @return boolean True if token/action pair is accepted, false if not |
||
5169 | */ |
||
5170 | public static function validate_onboarding_token_action( $token, $action ) { |
||
5171 | // Compare tokens, bail if tokens do not match |
||
5172 | if ( ! hash_equals( $token, Jetpack_Options::get_option( 'onboarding' ) ) ) { |
||
5173 | return false; |
||
5174 | } |
||
5175 | |||
5176 | // List of valid actions we can take |
||
5177 | $valid_actions = array( |
||
5178 | '/jetpack/v4/settings', |
||
5179 | ); |
||
5180 | |||
5181 | // Whitelist the action |
||
5182 | if ( ! in_array( $action, $valid_actions ) ) { |
||
5183 | return false; |
||
5184 | } |
||
5185 | |||
5186 | return true; |
||
5187 | } |
||
5188 | |||
5189 | /** |
||
5190 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
5191 | * |
||
5192 | * @since 2.3.3 |
||
5193 | * @return boolean |
||
5194 | */ |
||
5195 | public static function permit_ssl( $force_recheck = false ) { |
||
5196 | // Do some fancy tests to see if ssl is being supported |
||
5197 | if ( $force_recheck || false === ( $ssl = get_transient( 'jetpack_https_test' ) ) ) { |
||
5198 | $message = ''; |
||
5199 | if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) { |
||
5200 | $ssl = 0; |
||
5201 | } else { |
||
5202 | switch ( JETPACK_CLIENT__HTTPS ) { |
||
5203 | case 'NEVER': |
||
5204 | $ssl = 0; |
||
5205 | $message = __( 'JETPACK_CLIENT__HTTPS is set to NEVER', 'jetpack' ); |
||
5206 | break; |
||
5207 | case 'ALWAYS': |
||
5208 | case 'AUTO': |
||
5209 | default: |
||
5210 | $ssl = 1; |
||
5211 | break; |
||
5212 | } |
||
5213 | |||
5214 | // If it's not 'NEVER', test to see |
||
5215 | if ( $ssl ) { |
||
5216 | if ( ! wp_http_supports( array( 'ssl' => true ) ) ) { |
||
5217 | $ssl = 0; |
||
5218 | $message = __( 'WordPress reports no SSL support', 'jetpack' ); |
||
5219 | } else { |
||
5220 | $response = wp_remote_get( JETPACK__API_BASE . 'test/1/' ); |
||
5221 | if ( is_wp_error( $response ) ) { |
||
5222 | $ssl = 0; |
||
5223 | $message = __( 'WordPress reports no SSL support', 'jetpack' ); |
||
5224 | } elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) { |
||
5225 | $ssl = 0; |
||
5226 | $message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response ); |
||
5227 | } |
||
5228 | } |
||
5229 | } |
||
5230 | } |
||
5231 | set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS ); |
||
5232 | set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS ); |
||
5233 | } |
||
5234 | |||
5235 | return (bool) $ssl; |
||
5236 | } |
||
5237 | |||
5238 | /* |
||
5239 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
5240 | */ |
||
5241 | public function alert_auto_ssl_fail() { |
||
5242 | if ( ! current_user_can( 'manage_options' ) ) |
||
5243 | return; |
||
5244 | |||
5245 | $ajax_nonce = wp_create_nonce( 'recheck-ssl' ); |
||
5246 | ?> |
||
5247 | |||
5248 | <div id="jetpack-ssl-warning" class="error jp-identity-crisis"> |
||
5249 | <div class="jp-banner__content"> |
||
5250 | <h2><?php _e( 'Outbound HTTPS not working', 'jetpack' ); ?></h2> |
||
5251 | <p><?php _e( 'Your site could not connect to WordPress.com via HTTPS. This could be due to any number of reasons, including faulty SSL certificates, misconfigured or missing SSL libraries, or network issues.', 'jetpack' ); ?></p> |
||
5252 | <p> |
||
5253 | <?php _e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?> |
||
5254 | <a href="#" id="jetpack-recheck-ssl-button"><?php _e( 'Try again', 'jetpack' ); ?></a> |
||
5255 | <span id="jetpack-recheck-ssl-output"><?php echo get_transient( 'jetpack_https_test_message' ); ?></span> |
||
5256 | </p> |
||
5257 | <p> |
||
5258 | <?php printf( __( 'For more help, try our <a href="%1$s">connection debugger</a> or <a href="%2$s" target="_blank">troubleshooting tips</a>.', 'jetpack' ), |
||
5259 | esc_url( Jetpack::admin_url( array( 'page' => 'jetpack-debugger' ) ) ), |
||
5260 | esc_url( 'https://jetpack.com/support/getting-started-with-jetpack/troubleshooting-tips/' ) ); ?> |
||
5261 | </p> |
||
5262 | </div> |
||
5263 | </div> |
||
5264 | <style> |
||
5265 | #jetpack-recheck-ssl-output { margin-left: 5px; color: red; } |
||
5266 | </style> |
||
5267 | <script type="text/javascript"> |
||
5268 | jQuery( document ).ready( function( $ ) { |
||
5269 | $( '#jetpack-recheck-ssl-button' ).click( function( e ) { |
||
5270 | var $this = $( this ); |
||
5271 | $this.html( <?php echo json_encode( __( 'Checking', 'jetpack' ) ); ?> ); |
||
5272 | $( '#jetpack-recheck-ssl-output' ).html( '' ); |
||
5273 | e.preventDefault(); |
||
5274 | var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': '<?php echo $ajax_nonce; ?>' }; |
||
5275 | $.post( ajaxurl, data ) |
||
5276 | .done( function( response ) { |
||
5277 | if ( response.enabled ) { |
||
5278 | $( '#jetpack-ssl-warning' ).hide(); |
||
5279 | } else { |
||
5280 | this.html( <?php echo json_encode( __( 'Try again', 'jetpack' ) ); ?> ); |
||
5281 | $( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message ); |
||
5282 | } |
||
5283 | }.bind( $this ) ); |
||
5284 | } ); |
||
5285 | } ); |
||
5286 | </script> |
||
5287 | |||
5288 | <?php |
||
5289 | } |
||
5290 | |||
5291 | /** |
||
5292 | * Returns the Jetpack XML-RPC API |
||
5293 | * |
||
5294 | * @return string |
||
5295 | */ |
||
5296 | public static function xmlrpc_api_url() { |
||
5297 | $base = preg_replace( '#(https?://[^?/]+)(/?.*)?$#', '\\1', JETPACK__API_BASE ); |
||
5298 | return untrailingslashit( $base ) . '/xmlrpc.php'; |
||
5299 | } |
||
5300 | |||
5301 | /** |
||
5302 | * Creates two secret tokens and the end of life timestamp for them. |
||
5303 | * |
||
5304 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
5305 | * |
||
5306 | * @since 2.6 |
||
5307 | * @return array |
||
5308 | */ |
||
5309 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
5310 | if ( ! $user_id ) { |
||
5311 | $user_id = get_current_user_id(); |
||
5312 | } |
||
5313 | |||
5314 | $secret_name = 'jetpack_' . $action . '_' . $user_id; |
||
5315 | $secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() ); |
||
5316 | |||
5317 | if ( |
||
5318 | isset( $secrets[ $secret_name ] ) && |
||
5319 | $secrets[ $secret_name ]['exp'] > time() |
||
5320 | ) { |
||
5321 | return $secrets[ $secret_name ]; |
||
5322 | } |
||
5323 | |||
5324 | $secret_value = array( |
||
5325 | 'secret_1' => wp_generate_password( 32, false ), |
||
5326 | 'secret_2' => wp_generate_password( 32, false ), |
||
5327 | 'exp' => time() + $exp, |
||
5328 | ); |
||
5329 | |||
5330 | $secrets[ $secret_name ] = $secret_value; |
||
5331 | |||
5332 | Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets ); |
||
5333 | return $secrets[ $secret_name ]; |
||
5334 | } |
||
5335 | |||
5336 | public static function get_secrets( $action, $user_id ) { |
||
5337 | $secret_name = 'jetpack_' . $action . '_' . $user_id; |
||
5338 | $secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() ); |
||
5339 | |||
5340 | if ( ! isset( $secrets[ $secret_name ] ) ) { |
||
5341 | return new WP_Error( 'verify_secrets_missing', 'Verification secrets not found' ); |
||
5342 | } |
||
5343 | |||
5344 | if ( $secrets[ $secret_name ]['exp'] < time() ) { |
||
5345 | self::delete_secrets( $action, $user_id ); |
||
5346 | return new WP_Error( 'verify_secrets_expired', 'Verification took too long' ); |
||
5347 | } |
||
5348 | |||
5349 | return $secrets[ $secret_name ]; |
||
5350 | } |
||
5351 | |||
5352 | public static function delete_secrets( $action, $user_id ) { |
||
5353 | $secret_name = 'jetpack_' . $action . '_' . $user_id; |
||
5354 | $secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() ); |
||
5355 | if ( isset( $secrets[ $secret_name ] ) ) { |
||
5356 | unset( $secrets[ $secret_name ] ); |
||
5357 | Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets ); |
||
5358 | } |
||
5359 | } |
||
5360 | |||
5361 | /** |
||
5362 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5363 | * |
||
5364 | * Based on local php max_execution_time in php.ini |
||
5365 | * |
||
5366 | * @since 2.6 |
||
5367 | * @return int |
||
5368 | * @deprecated |
||
5369 | **/ |
||
5370 | public function get_remote_query_timeout_limit() { |
||
5371 | _deprecated_function( __METHOD__, 'jetpack-5.4' ); |
||
5372 | return Jetpack::get_max_execution_time(); |
||
5373 | } |
||
5374 | |||
5375 | /** |
||
5376 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
5377 | * |
||
5378 | * Based on local php max_execution_time in php.ini |
||
5379 | * |
||
5380 | * @since 5.4 |
||
5381 | * @return int |
||
5382 | **/ |
||
5383 | public static function get_max_execution_time() { |
||
5384 | $timeout = (int) ini_get( 'max_execution_time' ); |
||
5385 | |||
5386 | // Ensure exec time set in php.ini |
||
5387 | if ( ! $timeout ) { |
||
5388 | $timeout = 30; |
||
5389 | } |
||
5390 | return $timeout; |
||
5391 | } |
||
5392 | |||
5393 | /** |
||
5394 | * Sets a minimum request timeout, and returns the current timeout |
||
5395 | * |
||
5396 | * @since 5.4 |
||
5397 | **/ |
||
5398 | public static function set_min_time_limit( $min_timeout ) { |
||
5399 | $timeout = self::get_max_execution_time(); |
||
5400 | if ( $timeout < $min_timeout ) { |
||
5401 | $timeout = $min_timeout; |
||
5402 | set_time_limit( $timeout ); |
||
5403 | } |
||
5404 | return $timeout; |
||
5405 | } |
||
5406 | |||
5407 | |||
5408 | /** |
||
5409 | * Takes the response from the Jetpack register new site endpoint and |
||
5410 | * verifies it worked properly. |
||
5411 | * |
||
5412 | * @since 2.6 |
||
5413 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
5414 | **/ |
||
5415 | public function validate_remote_register_response( $response ) { |
||
5416 | if ( is_wp_error( $response ) ) { |
||
5417 | return new Jetpack_Error( 'register_http_request_failed', $response->get_error_message() ); |
||
5418 | } |
||
5419 | |||
5420 | $code = wp_remote_retrieve_response_code( $response ); |
||
5421 | $entity = wp_remote_retrieve_body( $response ); |
||
5422 | if ( $entity ) |
||
5423 | $registration_response = json_decode( $entity ); |
||
5424 | else |
||
5425 | $registration_response = false; |
||
5426 | |||
5427 | $code_type = intval( $code / 100 ); |
||
5428 | if ( 5 == $code_type ) { |
||
5429 | return new Jetpack_Error( 'wpcom_5??', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code ); |
||
5430 | } elseif ( 408 == $code ) { |
||
5431 | return new Jetpack_Error( 'wpcom_408', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code ); |
||
5432 | } elseif ( ! empty( $registration_response->error ) ) { |
||
5433 | if ( 'xml_rpc-32700' == $registration_response->error && ! function_exists( 'xml_parser_create' ) ) { |
||
5434 | $error_description = __( "PHP's XML extension is not available. Jetpack requires the XML extension to communicate with WordPress.com. Please contact your hosting provider to enable PHP's XML extension.", 'jetpack' ); |
||
5435 | } else { |
||
5436 | $error_description = isset( $registration_response->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $registration_response->error_description ) : ''; |
||
5437 | } |
||
5438 | |||
5439 | return new Jetpack_Error( (string) $registration_response->error, $error_description, $code ); |
||
5440 | } elseif ( 200 != $code ) { |
||
5441 | return new Jetpack_Error( 'wpcom_bad_response', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code ); |
||
5442 | } |
||
5443 | |||
5444 | // Jetpack ID error block |
||
5445 | if ( empty( $registration_response->jetpack_id ) ) { |
||
5446 | return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is empty. Do not publicly post this error message! %s', 'jetpack' ), $entity ), $entity ); |
||
5447 | } elseif ( ! is_scalar( $registration_response->jetpack_id ) ) { |
||
5448 | return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is not a scalar. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity ); |
||
5449 | } elseif ( preg_match( '/[^0-9]/', $registration_response->jetpack_id ) ) { |
||
5450 | return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID begins with a numeral. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity ); |
||
5451 | } |
||
5452 | |||
5453 | return $registration_response; |
||
5454 | } |
||
5455 | /** |
||
5456 | * @return bool|WP_Error |
||
5457 | */ |
||
5458 | public static function register() { |
||
5459 | JetpackTracking::record_user_event( 'jpc_register_begin' ); |
||
5460 | add_action( 'pre_update_jetpack_option_register', array( 'Jetpack_Options', 'delete_option' ) ); |
||
5461 | $secrets = Jetpack::generate_secrets( 'register' ); |
||
5462 | |||
5463 | View Code Duplication | if ( |
|
5464 | empty( $secrets['secret_1'] ) || |
||
5465 | empty( $secrets['secret_2'] ) || |
||
5466 | empty( $secrets['exp'] ) |
||
5467 | ) { |
||
5468 | return new Jetpack_Error( 'missing_secrets' ); |
||
5469 | } |
||
5470 | |||
5471 | // better to try (and fail) to set a higher timeout than this system |
||
5472 | // supports than to have register fail for more users than it should |
||
5473 | $timeout = Jetpack::set_min_time_limit( 60 ) / 2; |
||
5474 | |||
5475 | $gmt_offset = get_option( 'gmt_offset' ); |
||
5476 | if ( ! $gmt_offset ) { |
||
5477 | $gmt_offset = 0; |
||
5478 | } |
||
5479 | |||
5480 | $stats_options = get_option( 'stats_options' ); |
||
5481 | $stats_id = isset($stats_options['blog_id']) ? $stats_options['blog_id'] : null; |
||
5482 | |||
5483 | $tracks_identity = jetpack_tracks_get_identity( get_current_user_id() ); |
||
5484 | |||
5485 | $args = array( |
||
5486 | 'method' => 'POST', |
||
5487 | 'body' => array( |
||
5488 | 'siteurl' => site_url(), |
||
5489 | 'home' => home_url(), |
||
5490 | 'gmt_offset' => $gmt_offset, |
||
5491 | 'timezone_string' => (string) get_option( 'timezone_string' ), |
||
5492 | 'site_name' => (string) get_option( 'blogname' ), |
||
5493 | 'secret_1' => $secrets['secret_1'], |
||
5494 | 'secret_2' => $secrets['secret_2'], |
||
5495 | 'site_lang' => get_locale(), |
||
5496 | 'timeout' => $timeout, |
||
5497 | 'stats_id' => $stats_id, |
||
5498 | 'state' => get_current_user_id(), |
||
5499 | '_ui' => $tracks_identity['_ui'], |
||
5500 | '_ut' => $tracks_identity['_ut'], |
||
5501 | 'site_created' => Jetpack::get_assumed_site_creation_date(), |
||
5502 | 'jetpack_version' => JETPACK__VERSION |
||
5503 | ), |
||
5504 | 'headers' => array( |
||
5505 | 'Accept' => 'application/json', |
||
5506 | ), |
||
5507 | 'timeout' => $timeout, |
||
5508 | ); |
||
5509 | |||
5510 | self::apply_activation_source_to_args( $args['body'] ); |
||
5511 | |||
5512 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'register' ) ), $args, true ); |
||
5513 | |||
5514 | // Make sure the response is valid and does not contain any Jetpack errors |
||
5515 | $registration_details = Jetpack::init()->validate_remote_register_response( $response ); |
||
5516 | if ( is_wp_error( $registration_details ) ) { |
||
5517 | return $registration_details; |
||
5518 | } elseif ( ! $registration_details ) { |
||
5519 | return new Jetpack_Error( 'unknown_error', __( 'Unknown error registering your Jetpack site', 'jetpack' ), wp_remote_retrieve_response_code( $response ) ); |
||
5520 | } |
||
5521 | |||
5522 | View Code Duplication | if ( empty( $registration_details->jetpack_secret ) || ! is_string( $registration_details->jetpack_secret ) ) { |
|
5523 | return new Jetpack_Error( 'jetpack_secret', '', wp_remote_retrieve_response_code( $response ) ); |
||
5524 | } |
||
5525 | |||
5526 | if ( isset( $registration_details->jetpack_public ) ) { |
||
5527 | $jetpack_public = (int) $registration_details->jetpack_public; |
||
5528 | } else { |
||
5529 | $jetpack_public = false; |
||
5530 | } |
||
5531 | |||
5532 | Jetpack_Options::update_options( |
||
5533 | array( |
||
5534 | 'id' => (int) $registration_details->jetpack_id, |
||
5535 | 'blog_token' => (string) $registration_details->jetpack_secret, |
||
5536 | 'public' => $jetpack_public, |
||
5537 | ) |
||
5538 | ); |
||
5539 | |||
5540 | /** |
||
5541 | * Fires when a site is registered on WordPress.com. |
||
5542 | * |
||
5543 | * @since 3.7.0 |
||
5544 | * |
||
5545 | * @param int $json->jetpack_id Jetpack Blog ID. |
||
5546 | * @param string $json->jetpack_secret Jetpack Blog Token. |
||
5547 | * @param int|bool $jetpack_public Is the site public. |
||
5548 | */ |
||
5549 | do_action( 'jetpack_site_registered', $registration_details->jetpack_id, $registration_details->jetpack_secret, $jetpack_public ); |
||
5550 | |||
5551 | // Initialize Jump Start for the first and only time. |
||
5552 | if ( ! Jetpack_Options::get_option( 'jumpstart' ) ) { |
||
5553 | Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); |
||
5554 | |||
5555 | $jetpack = Jetpack::init(); |
||
5556 | |||
5557 | $jetpack->stat( 'jumpstart', 'unique-views' ); |
||
5558 | $jetpack->do_stats( 'server_side' ); |
||
5559 | }; |
||
5560 | |||
5561 | return true; |
||
5562 | } |
||
5563 | |||
5564 | /** |
||
5565 | * If the db version is showing something other that what we've got now, bump it to current. |
||
5566 | * |
||
5567 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
5568 | */ |
||
5569 | public static function maybe_set_version_option() { |
||
5570 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
5571 | if ( JETPACK__VERSION != $version ) { |
||
5572 | Jetpack_Options::update_option( 'version', JETPACK__VERSION . ':' . time() ); |
||
5573 | |||
5574 | if ( version_compare( JETPACK__VERSION, $version, '>' ) ) { |
||
5575 | /** This action is documented in class.jetpack.php */ |
||
5576 | do_action( 'updating_jetpack_version', JETPACK__VERSION, $version ); |
||
5577 | } |
||
5578 | |||
5579 | return true; |
||
5580 | } |
||
5581 | return false; |
||
5582 | } |
||
5583 | |||
5584 | /* Client Server API */ |
||
5585 | |||
5586 | /** |
||
5587 | * Loads the Jetpack XML-RPC client |
||
5588 | */ |
||
5589 | public static function load_xml_rpc_client() { |
||
5590 | require_once ABSPATH . WPINC . '/class-IXR.php'; |
||
5591 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-ixr-client.php'; |
||
5592 | } |
||
5593 | |||
5594 | /** |
||
5595 | * Resets the saved authentication state in between testing requests. |
||
5596 | */ |
||
5597 | public function reset_saved_auth_state() { |
||
5598 | $this->xmlrpc_verification = null; |
||
5599 | $this->rest_authentication_status = null; |
||
5600 | } |
||
5601 | |||
5602 | function verify_xml_rpc_signature() { |
||
5603 | if ( $this->xmlrpc_verification ) { |
||
5604 | return $this->xmlrpc_verification; |
||
5605 | } |
||
5606 | |||
5607 | // It's not for us |
||
5608 | if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { |
||
5609 | return false; |
||
5610 | } |
||
5611 | |||
5612 | @list( $token_key, $version, $user_id ) = explode( ':', $_GET['token'] ); |
||
5613 | if ( |
||
5614 | empty( $token_key ) |
||
5615 | || |
||
5616 | empty( $version ) || strval( JETPACK__API_VERSION ) !== $version |
||
5617 | ) { |
||
5618 | return false; |
||
5619 | } |
||
5620 | |||
5621 | if ( '0' === $user_id ) { |
||
5622 | $token_type = 'blog'; |
||
5623 | $user_id = 0; |
||
5624 | } else { |
||
5625 | $token_type = 'user'; |
||
5626 | if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { |
||
5627 | return false; |
||
5628 | } |
||
5629 | $user_id = (int) $user_id; |
||
5630 | |||
5631 | $user = new WP_User( $user_id ); |
||
5632 | if ( ! $user || ! $user->exists() ) { |
||
5633 | return false; |
||
5634 | } |
||
5635 | } |
||
5636 | |||
5637 | $token = Jetpack_Data::get_access_token( $user_id ); |
||
5638 | if ( ! $token ) { |
||
5639 | return false; |
||
5640 | } |
||
5641 | |||
5642 | $token_check = "$token_key."; |
||
5643 | if ( ! hash_equals( substr( $token->secret, 0, strlen( $token_check ) ), $token_check ) ) { |
||
5644 | return false; |
||
5645 | } |
||
5646 | |||
5647 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
||
5648 | |||
5649 | $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) ); |
||
5650 | if ( isset( $_POST['_jetpack_is_multipart'] ) ) { |
||
5651 | $post_data = $_POST; |
||
5652 | $file_hashes = array(); |
||
5653 | foreach ( $post_data as $post_data_key => $post_data_value ) { |
||
5654 | if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { |
||
5655 | continue; |
||
5656 | } |
||
5657 | $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); |
||
5658 | $file_hashes[$post_data_key] = $post_data_value; |
||
5659 | } |
||
5660 | |||
5661 | foreach ( $file_hashes as $post_data_key => $post_data_value ) { |
||
5662 | unset( $post_data["_jetpack_file_hmac_{$post_data_key}"] ); |
||
5663 | $post_data[$post_data_key] = $post_data_value; |
||
5664 | } |
||
5665 | |||
5666 | ksort( $post_data ); |
||
5667 | |||
5668 | $body = http_build_query( stripslashes_deep( $post_data ) ); |
||
5669 | } elseif ( is_null( $this->HTTP_RAW_POST_DATA ) ) { |
||
5670 | $body = file_get_contents( 'php://input' ); |
||
5671 | } else { |
||
5672 | $body = null; |
||
5673 | } |
||
5674 | |||
5675 | $signature = $jetpack_signature->sign_current_request( |
||
5676 | array( 'body' => is_null( $body ) ? $this->HTTP_RAW_POST_DATA : $body, ) |
||
5677 | ); |
||
5678 | |||
5679 | if ( ! $signature ) { |
||
5680 | return false; |
||
5681 | } else if ( is_wp_error( $signature ) ) { |
||
5682 | return $signature; |
||
5683 | } else if ( ! hash_equals( $signature, $_GET['signature'] ) ) { |
||
5684 | return false; |
||
5685 | } |
||
5686 | |||
5687 | $timestamp = (int) $_GET['timestamp']; |
||
5688 | $nonce = stripslashes( (string) $_GET['nonce'] ); |
||
5689 | |||
5690 | if ( ! $this->add_nonce( $timestamp, $nonce ) ) { |
||
5691 | return false; |
||
5692 | } |
||
5693 | |||
5694 | // Let's see if this is onboarding. In such case, use user token type and the provided user id. |
||
5695 | if ( isset( $this->HTTP_RAW_POST_DATA ) || ! empty( $_GET['onboarding'] ) ) { |
||
5696 | if ( ! empty( $_GET['onboarding'] ) ) { |
||
5697 | $jpo = $_GET; |
||
5698 | } else { |
||
5699 | $jpo = json_decode( $this->HTTP_RAW_POST_DATA, true ); |
||
5700 | } |
||
5701 | |||
5702 | $jpo_token = ! empty( $jpo['onboarding']['token'] ) ? $jpo['onboarding']['token'] : null; |
||
5703 | $jpo_user = ! empty( $jpo['onboarding']['jpUser'] ) ? $jpo['onboarding']['jpUser'] : null; |
||
5704 | |||
5705 | if ( |
||
5706 | isset( $jpo_user ) && isset( $jpo_token ) && |
||
5707 | is_email( $jpo_user ) && ctype_alnum( $jpo_token ) && |
||
5708 | isset( $_GET['rest_route'] ) && |
||
5709 | self::validate_onboarding_token_action( $jpo_token, $_GET['rest_route'] ) |
||
5710 | ) { |
||
5711 | $jpUser = get_user_by( 'email', $jpo_user ); |
||
5712 | if ( is_a( $jpUser, 'WP_User' ) ) { |
||
5713 | wp_set_current_user( $jpUser->ID ); |
||
5714 | $user_can = is_multisite() |
||
5715 | ? current_user_can_for_blog( get_current_blog_id(), 'manage_options' ) |
||
5716 | : current_user_can( 'manage_options' ); |
||
5717 | if ( $user_can ) { |
||
5718 | $token_type = 'user'; |
||
5719 | $token->external_user_id = $jpUser->ID; |
||
5720 | } |
||
5721 | } |
||
5722 | } |
||
5723 | } |
||
5724 | |||
5725 | $this->xmlrpc_verification = array( |
||
5726 | 'type' => $token_type, |
||
5727 | 'user_id' => $token->external_user_id, |
||
5728 | ); |
||
5729 | |||
5730 | return $this->xmlrpc_verification; |
||
5731 | } |
||
5732 | |||
5733 | /** |
||
5734 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
5735 | */ |
||
5736 | function authenticate_jetpack( $user, $username, $password ) { |
||
5737 | if ( is_a( $user, 'WP_User' ) ) { |
||
5738 | return $user; |
||
5739 | } |
||
5740 | |||
5741 | $token_details = $this->verify_xml_rpc_signature(); |
||
5742 | |||
5743 | if ( ! $token_details || is_wp_error( $token_details ) ) { |
||
5744 | return $user; |
||
5745 | } |
||
5746 | |||
5747 | if ( 'user' !== $token_details['type'] ) { |
||
5748 | return $user; |
||
5749 | } |
||
5750 | |||
5751 | if ( ! $token_details['user_id'] ) { |
||
5752 | return $user; |
||
5753 | } |
||
5754 | |||
5755 | nocache_headers(); |
||
5756 | |||
5757 | return new WP_User( $token_details['user_id'] ); |
||
5758 | } |
||
5759 | |||
5760 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
5761 | // Uses the existing XMLRPC request signing implementation. |
||
5762 | function wp_rest_authenticate( $user ) { |
||
5763 | if ( ! empty( $user ) ) { |
||
5764 | // Another authentication method is in effect. |
||
5765 | return $user; |
||
5766 | } |
||
5767 | |||
5768 | if ( ! isset( $_GET['_for'] ) || $_GET['_for'] !== 'jetpack' ) { |
||
5769 | // Nothing to do for this authentication method. |
||
5770 | return null; |
||
5771 | } |
||
5772 | |||
5773 | if ( ! isset( $_GET['token'] ) && ! isset( $_GET['signature'] ) ) { |
||
5774 | // Nothing to do for this authentication method. |
||
5775 | return null; |
||
5776 | } |
||
5777 | |||
5778 | // Ensure that we always have the request body available. At this |
||
5779 | // point, the WP REST API code to determine the request body has not |
||
5780 | // run yet. That code may try to read from 'php://input' later, but |
||
5781 | // this can only be done once per request in PHP versions prior to 5.6. |
||
5782 | // So we will go ahead and perform this read now if needed, and save |
||
5783 | // the request body where both the Jetpack signature verification code |
||
5784 | // and the WP REST API code can see it. |
||
5785 | if ( ! isset( $GLOBALS['HTTP_RAW_POST_DATA'] ) ) { |
||
5786 | $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents( 'php://input' ); |
||
5787 | } |
||
5788 | $this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA']; |
||
5789 | |||
5790 | // Only support specific request parameters that have been tested and |
||
5791 | // are known to work with signature verification. A different method |
||
5792 | // can be passed to the WP REST API via the '?_method=' parameter if |
||
5793 | // needed. |
||
5794 | if ( $_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST' ) { |
||
5795 | $this->rest_authentication_status = new WP_Error( |
||
5796 | 'rest_invalid_request', |
||
5797 | __( 'This request method is not supported.', 'jetpack' ), |
||
5798 | array( 'status' => 400 ) |
||
5799 | ); |
||
5800 | return null; |
||
5801 | } |
||
5802 | if ( $_SERVER['REQUEST_METHOD'] !== 'POST' && ! empty( $this->HTTP_RAW_POST_DATA ) ) { |
||
5803 | $this->rest_authentication_status = new WP_Error( |
||
5804 | 'rest_invalid_request', |
||
5805 | __( 'This request method does not support body parameters.', 'jetpack' ), |
||
5806 | array( 'status' => 400 ) |
||
5807 | ); |
||
5808 | return null; |
||
5809 | } |
||
5810 | |||
5811 | $verified = $this->verify_xml_rpc_signature(); |
||
5812 | |||
5813 | if ( is_wp_error( $verified ) ) { |
||
5814 | $this->rest_authentication_status = $verified; |
||
5815 | return null; |
||
5816 | } |
||
5817 | |||
5818 | if ( |
||
5819 | $verified && |
||
5820 | isset( $verified['type'] ) && |
||
5821 | 'user' === $verified['type'] && |
||
5822 | ! empty( $verified['user_id'] ) |
||
5823 | ) { |
||
5824 | // Authentication successful. |
||
5825 | $this->rest_authentication_status = true; |
||
5826 | return $verified['user_id']; |
||
5827 | } |
||
5828 | |||
5829 | // Something else went wrong. Probably a signature error. |
||
5830 | $this->rest_authentication_status = new WP_Error( |
||
5831 | 'rest_invalid_signature', |
||
5832 | __( 'The request is not signed correctly.', 'jetpack' ), |
||
5833 | array( 'status' => 400 ) |
||
5834 | ); |
||
5835 | return null; |
||
5836 | } |
||
5837 | |||
5838 | /** |
||
5839 | * Report authentication status to the WP REST API. |
||
5840 | * |
||
5841 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5842 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5843 | */ |
||
5844 | public function wp_rest_authentication_errors( $value ) { |
||
5845 | if ( $value !== null ) { |
||
5846 | return $value; |
||
5847 | } |
||
5848 | return $this->rest_authentication_status; |
||
5849 | } |
||
5850 | |||
5851 | function add_nonce( $timestamp, $nonce ) { |
||
5852 | global $wpdb; |
||
5853 | static $nonces_used_this_request = array(); |
||
5854 | |||
5855 | if ( isset( $nonces_used_this_request["$timestamp:$nonce"] ) ) { |
||
5856 | return $nonces_used_this_request["$timestamp:$nonce"]; |
||
5857 | } |
||
5858 | |||
5859 | // This should always have gone through Jetpack_Signature::sign_request() first to check $timestamp an $nonce |
||
5860 | $timestamp = (int) $timestamp; |
||
5861 | $nonce = esc_sql( $nonce ); |
||
5862 | |||
5863 | // Raw query so we can avoid races: add_option will also update |
||
5864 | $show_errors = $wpdb->show_errors( false ); |
||
5865 | |||
5866 | $old_nonce = $wpdb->get_row( |
||
5867 | $wpdb->prepare( "SELECT * FROM `$wpdb->options` WHERE option_name = %s", "jetpack_nonce_{$timestamp}_{$nonce}" ) |
||
5868 | ); |
||
5869 | |||
5870 | if ( is_null( $old_nonce ) ) { |
||
5871 | $return = $wpdb->query( |
||
5872 | $wpdb->prepare( |
||
5873 | "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)", |
||
5874 | "jetpack_nonce_{$timestamp}_{$nonce}", |
||
5875 | time(), |
||
5876 | 'no' |
||
5877 | ) |
||
5878 | ); |
||
5879 | } else { |
||
5880 | $return = false; |
||
5881 | } |
||
5882 | |||
5883 | $wpdb->show_errors( $show_errors ); |
||
5884 | |||
5885 | $nonces_used_this_request["$timestamp:$nonce"] = $return; |
||
5886 | |||
5887 | return $return; |
||
5888 | } |
||
5889 | |||
5890 | /** |
||
5891 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5892 | * Capture it here so we can verify the signature later. |
||
5893 | */ |
||
5894 | function xmlrpc_methods( $methods ) { |
||
5895 | $this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA']; |
||
5896 | return $methods; |
||
5897 | } |
||
5898 | |||
5899 | function public_xmlrpc_methods( $methods ) { |
||
5900 | if ( array_key_exists( 'wp.getOptions', $methods ) ) { |
||
5901 | $methods['wp.getOptions'] = array( $this, 'jetpack_getOptions' ); |
||
5902 | } |
||
5903 | return $methods; |
||
5904 | } |
||
5905 | |||
5906 | function jetpack_getOptions( $args ) { |
||
5907 | global $wp_xmlrpc_server; |
||
5908 | |||
5909 | $wp_xmlrpc_server->escape( $args ); |
||
5910 | |||
5911 | $username = $args[1]; |
||
5912 | $password = $args[2]; |
||
5913 | |||
5914 | if ( !$user = $wp_xmlrpc_server->login($username, $password) ) { |
||
5915 | return $wp_xmlrpc_server->error; |
||
5916 | } |
||
5917 | |||
5918 | $options = array(); |
||
5919 | $user_data = $this->get_connected_user_data(); |
||
5920 | if ( is_array( $user_data ) ) { |
||
5921 | $options['jetpack_user_id'] = array( |
||
5922 | 'desc' => __( 'The WP.com user ID of the connected user', 'jetpack' ), |
||
5923 | 'readonly' => true, |
||
5924 | 'value' => $user_data['ID'], |
||
5925 | ); |
||
5926 | $options['jetpack_user_login'] = array( |
||
5927 | 'desc' => __( 'The WP.com username of the connected user', 'jetpack' ), |
||
5928 | 'readonly' => true, |
||
5929 | 'value' => $user_data['login'], |
||
5930 | ); |
||
5931 | $options['jetpack_user_email'] = array( |
||
5932 | 'desc' => __( 'The WP.com user email of the connected user', 'jetpack' ), |
||
5933 | 'readonly' => true, |
||
5934 | 'value' => $user_data['email'], |
||
5935 | ); |
||
5936 | $options['jetpack_user_site_count'] = array( |
||
5937 | 'desc' => __( 'The number of sites of the connected WP.com user', 'jetpack' ), |
||
5938 | 'readonly' => true, |
||
5939 | 'value' => $user_data['site_count'], |
||
5940 | ); |
||
5941 | } |
||
5942 | $wp_xmlrpc_server->blog_options = array_merge( $wp_xmlrpc_server->blog_options, $options ); |
||
5943 | $args = stripslashes_deep( $args ); |
||
5944 | return $wp_xmlrpc_server->wp_getOptions( $args ); |
||
5945 | } |
||
5946 | |||
5947 | function xmlrpc_options( $options ) { |
||
5948 | $jetpack_client_id = false; |
||
5949 | if ( self::is_active() ) { |
||
5950 | $jetpack_client_id = Jetpack_Options::get_option( 'id' ); |
||
5951 | } |
||
5952 | $options['jetpack_version'] = array( |
||
5953 | 'desc' => __( 'Jetpack Plugin Version', 'jetpack' ), |
||
5954 | 'readonly' => true, |
||
5955 | 'value' => JETPACK__VERSION, |
||
5956 | ); |
||
5957 | |||
5958 | $options['jetpack_client_id'] = array( |
||
5959 | 'desc' => __( 'The Client ID/WP.com Blog ID of this site', 'jetpack' ), |
||
5960 | 'readonly' => true, |
||
5961 | 'value' => $jetpack_client_id, |
||
5962 | ); |
||
5963 | return $options; |
||
5964 | } |
||
5965 | |||
5966 | public static function clean_nonces( $all = false ) { |
||
5967 | global $wpdb; |
||
5968 | |||
5969 | $sql = "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s"; |
||
5970 | $sql_args = array( $wpdb->esc_like( 'jetpack_nonce_' ) . '%' ); |
||
5971 | |||
5972 | if ( true !== $all ) { |
||
5973 | $sql .= ' AND CAST( `option_value` AS UNSIGNED ) < %d'; |
||
5974 | $sql_args[] = time() - 3600; |
||
5975 | } |
||
5976 | |||
5977 | $sql .= ' ORDER BY `option_id` LIMIT 100'; |
||
5978 | |||
5979 | $sql = $wpdb->prepare( $sql, $sql_args ); |
||
5980 | |||
5981 | for ( $i = 0; $i < 1000; $i++ ) { |
||
5982 | if ( ! $wpdb->query( $sql ) ) { |
||
5983 | break; |
||
5984 | } |
||
5985 | } |
||
5986 | } |
||
5987 | |||
5988 | /** |
||
5989 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5990 | * SET: state( $key, $value ); |
||
5991 | * GET: $value = state( $key ); |
||
5992 | * |
||
5993 | * @param string $key |
||
5994 | * @param string $value |
||
5995 | * @param bool $restate private |
||
5996 | */ |
||
5997 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5998 | static $state = array(); |
||
5999 | static $path, $domain; |
||
6000 | if ( ! isset( $path ) ) { |
||
6001 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
6002 | $admin_url = Jetpack::admin_url(); |
||
6003 | $bits = wp_parse_url( $admin_url ); |
||
6004 | |||
6005 | if ( is_array( $bits ) ) { |
||
6006 | $path = ( isset( $bits['path'] ) ) ? dirname( $bits['path'] ) : null; |
||
6007 | $domain = ( isset( $bits['host'] ) ) ? $bits['host'] : null; |
||
6008 | } else { |
||
6009 | $path = $domain = null; |
||
6010 | } |
||
6011 | } |
||
6012 | |||
6013 | // Extract state from cookies and delete cookies |
||
6014 | if ( isset( $_COOKIE[ 'jetpackState' ] ) && is_array( $_COOKIE[ 'jetpackState' ] ) ) { |
||
6015 | $yum = $_COOKIE[ 'jetpackState' ]; |
||
6016 | unset( $_COOKIE[ 'jetpackState' ] ); |
||
6017 | foreach ( $yum as $k => $v ) { |
||
6018 | if ( strlen( $v ) ) |
||
6019 | $state[ $k ] = $v; |
||
6020 | setcookie( "jetpackState[$k]", false, 0, $path, $domain ); |
||
6021 | } |
||
6022 | } |
||
6023 | |||
6024 | if ( $restate ) { |
||
6025 | foreach ( $state as $k => $v ) { |
||
6026 | setcookie( "jetpackState[$k]", $v, 0, $path, $domain ); |
||
6027 | } |
||
6028 | return; |
||
6029 | } |
||
6030 | |||
6031 | // Get a state variable |
||
6032 | if ( isset( $key ) && ! isset( $value ) ) { |
||
6033 | if ( array_key_exists( $key, $state ) ) |
||
6034 | return $state[ $key ]; |
||
6035 | return null; |
||
6036 | } |
||
6037 | |||
6038 | // Set a state variable |
||
6039 | if ( isset ( $key ) && isset( $value ) ) { |
||
6040 | if( is_array( $value ) && isset( $value[0] ) ) { |
||
6041 | $value = $value[0]; |
||
6042 | } |
||
6043 | $state[ $key ] = $value; |
||
6044 | setcookie( "jetpackState[$key]", $value, 0, $path, $domain ); |
||
6045 | } |
||
6046 | } |
||
6047 | |||
6048 | public static function restate() { |
||
6049 | Jetpack::state( null, null, true ); |
||
6050 | } |
||
6051 | |||
6052 | public static function check_privacy( $file ) { |
||
6053 | static $is_site_publicly_accessible = null; |
||
6054 | |||
6055 | if ( is_null( $is_site_publicly_accessible ) ) { |
||
6056 | $is_site_publicly_accessible = false; |
||
6057 | |||
6058 | Jetpack::load_xml_rpc_client(); |
||
6059 | $rpc = new Jetpack_IXR_Client(); |
||
6060 | |||
6061 | $success = $rpc->query( 'jetpack.isSitePubliclyAccessible', home_url() ); |
||
6062 | if ( $success ) { |
||
6063 | $response = $rpc->getResponse(); |
||
6064 | if ( $response ) { |
||
6065 | $is_site_publicly_accessible = true; |
||
6066 | } |
||
6067 | } |
||
6068 | |||
6069 | Jetpack_Options::update_option( 'public', (int) $is_site_publicly_accessible ); |
||
6070 | } |
||
6071 | |||
6072 | if ( $is_site_publicly_accessible ) { |
||
6073 | return; |
||
6074 | } |
||
6075 | |||
6076 | $module_slug = self::get_module_slug( $file ); |
||
6077 | |||
6078 | $privacy_checks = Jetpack::state( 'privacy_checks' ); |
||
6079 | if ( ! $privacy_checks ) { |
||
6080 | $privacy_checks = $module_slug; |
||
6081 | } else { |
||
6082 | $privacy_checks .= ",$module_slug"; |
||
6083 | } |
||
6084 | |||
6085 | Jetpack::state( 'privacy_checks', $privacy_checks ); |
||
6086 | } |
||
6087 | |||
6088 | /** |
||
6089 | * Helper method for multicall XMLRPC. |
||
6090 | */ |
||
6091 | public static function xmlrpc_async_call() { |
||
6092 | global $blog_id; |
||
6093 | static $clients = array(); |
||
6094 | |||
6095 | $client_blog_id = is_multisite() ? $blog_id : 0; |
||
6096 | |||
6097 | if ( ! isset( $clients[$client_blog_id] ) ) { |
||
6098 | Jetpack::load_xml_rpc_client(); |
||
6099 | $clients[$client_blog_id] = new Jetpack_IXR_ClientMulticall( array( 'user_id' => JETPACK_MASTER_USER, ) ); |
||
6100 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
6101 | ignore_user_abort( true ); |
||
6102 | } |
||
6103 | add_action( 'shutdown', array( 'Jetpack', 'xmlrpc_async_call' ) ); |
||
6104 | } |
||
6105 | |||
6106 | $args = func_get_args(); |
||
6107 | |||
6108 | if ( ! empty( $args[0] ) ) { |
||
6109 | call_user_func_array( array( $clients[$client_blog_id], 'addCall' ), $args ); |
||
6110 | } elseif ( is_multisite() ) { |
||
6111 | foreach ( $clients as $client_blog_id => $client ) { |
||
6112 | if ( ! $client_blog_id || empty( $client->calls ) ) { |
||
6113 | continue; |
||
6114 | } |
||
6115 | |||
6116 | $switch_success = switch_to_blog( $client_blog_id, true ); |
||
6117 | if ( ! $switch_success ) { |
||
6118 | continue; |
||
6119 | } |
||
6120 | |||
6121 | flush(); |
||
6122 | $client->query(); |
||
6123 | |||
6124 | restore_current_blog(); |
||
6125 | } |
||
6126 | } else { |
||
6127 | if ( isset( $clients[0] ) && ! empty( $clients[0]->calls ) ) { |
||
6128 | flush(); |
||
6129 | $clients[0]->query(); |
||
6130 | } |
||
6131 | } |
||
6132 | } |
||
6133 | |||
6134 | public static function staticize_subdomain( $url ) { |
||
6135 | |||
6136 | // Extract hostname from URL |
||
6137 | $host = parse_url( $url, PHP_URL_HOST ); |
||
6138 | |||
6139 | // Explode hostname on '.' |
||
6140 | $exploded_host = explode( '.', $host ); |
||
6141 | |||
6142 | // Retrieve the name and TLD |
||
6143 | if ( count( $exploded_host ) > 1 ) { |
||
6144 | $name = $exploded_host[ count( $exploded_host ) - 2 ]; |
||
6145 | $tld = $exploded_host[ count( $exploded_host ) - 1 ]; |
||
6146 | // Rebuild domain excluding subdomains |
||
6147 | $domain = $name . '.' . $tld; |
||
6148 | } else { |
||
6149 | $domain = $host; |
||
6150 | } |
||
6151 | // Array of Automattic domains |
||
6152 | $domain_whitelist = array( 'wordpress.com', 'wp.com' ); |
||
6153 | |||
6154 | // Return $url if not an Automattic domain |
||
6155 | if ( ! in_array( $domain, $domain_whitelist ) ) { |
||
6156 | return $url; |
||
6157 | } |
||
6158 | |||
6159 | if ( is_ssl() ) { |
||
6160 | return preg_replace( '|https?://[^/]++/|', 'https://s-ssl.wordpress.com/', $url ); |
||
6161 | } |
||
6162 | |||
6163 | srand( crc32( basename( $url ) ) ); |
||
6164 | $static_counter = rand( 0, 2 ); |
||
6165 | srand(); // this resets everything that relies on this, like array_rand() and shuffle() |
||
6166 | |||
6167 | return preg_replace( '|://[^/]+?/|', "://s$static_counter.wp.com/", $url ); |
||
6168 | } |
||
6169 | |||
6170 | /* JSON API Authorization */ |
||
6171 | |||
6172 | /** |
||
6173 | * Handles the login action for Authorizing the JSON API |
||
6174 | */ |
||
6175 | function login_form_json_api_authorization() { |
||
6176 | $this->verify_json_api_authorization_request(); |
||
6177 | |||
6178 | add_action( 'wp_login', array( &$this, 'store_json_api_authorization_token' ), 10, 2 ); |
||
6179 | |||
6180 | add_action( 'login_message', array( &$this, 'login_message_json_api_authorization' ) ); |
||
6181 | add_action( 'login_form', array( &$this, 'preserve_action_in_login_form_for_json_api_authorization' ) ); |
||
6182 | add_filter( 'site_url', array( &$this, 'post_login_form_to_signed_url' ), 10, 3 ); |
||
6183 | } |
||
6184 | |||
6185 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
6186 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
6187 | if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) { |
||
6188 | return $url; |
||
6189 | } |
||
6190 | |||
6191 | $parsed_url = parse_url( $url ); |
||
6192 | $url = strtok( $url, '?' ); |
||
6193 | $url = "$url?{$_SERVER['QUERY_STRING']}"; |
||
6194 | if ( ! empty( $parsed_url['query'] ) ) |
||
6195 | $url .= "&{$parsed_url['query']}"; |
||
6196 | |||
6197 | return $url; |
||
6198 | } |
||
6199 | |||
6200 | // Make sure the POSTed request is handled by the same action |
||
6201 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
6202 | echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n"; |
||
6203 | echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) . "' />\n"; |
||
6204 | } |
||
6205 | |||
6206 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
6207 | function store_json_api_authorization_token( $user_login, $user ) { |
||
6208 | add_filter( 'login_redirect', array( &$this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 ); |
||
6209 | add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_public_api_domain' ) ); |
||
6210 | $token = wp_generate_password( 32, false ); |
||
6211 | update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token ); |
||
6212 | } |
||
6213 | |||
6214 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
6215 | function allow_wpcom_public_api_domain( $domains ) { |
||
6216 | $domains[] = 'public-api.wordpress.com'; |
||
6217 | return $domains; |
||
6218 | } |
||
6219 | |||
6220 | static function is_redirect_encoded( $redirect_url ) { |
||
6221 | return preg_match( '/https?%3A%2F%2F/i', $redirect_url ) > 0; |
||
6222 | } |
||
6223 | |||
6224 | // Add all wordpress.com environments to the safe redirect whitelist |
||
6225 | function allow_wpcom_environments( $domains ) { |
||
6232 | |||
6233 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
6234 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
6246 | |||
6247 | |||
6248 | /** |
||
6249 | * Verifies the request by checking the signature |
||
6250 | * |
||
6251 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
6252 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
6253 | * |
||
6254 | * @param null|array $environment |
||
6255 | */ |
||
6256 | function verify_json_api_authorization_request( $environment = null ) { |
||
6257 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
||
6258 | |||
6259 | $environment = is_null( $environment ) |
||
6260 | ? $_REQUEST |
||
6261 | : $environment; |
||
6262 | |||
6263 | list( $envToken, $envVersion, $envUserId ) = explode( ':', $environment['token'] ); |
||
6264 | $token = Jetpack_Data::get_access_token( $envUserId ); |
||
6265 | if ( ! $token || empty( $token->secret ) ) { |
||
6266 | wp_die( __( 'You must connect your Jetpack plugin to WordPress.com to use this feature.' , 'jetpack' ) ); |
||
6267 | } |
||
6268 | |||
6269 | $die_error = __( 'Someone may be trying to trick you into giving them access to your site. Or it could be you just encountered a bug :). Either way, please close this window.', 'jetpack' ); |
||
6270 | |||
6271 | // Host has encoded the request URL, probably as a result of a bad http => https redirect |
||
6272 | if ( Jetpack::is_redirect_encoded( $_GET['redirect_to'] ) ) { |
||
6273 | JetpackTracking::record_user_event( 'error_double_encode' ); |
||
6274 | |||
6275 | $die_error = sprintf( |
||
6276 | /* translators: %s is a URL */ |
||
6277 | __( 'Your site is incorrectly double-encoding redirects from http to https. This is preventing Jetpack from authenticating your connection. Please visit our <a href="%s">support page</a> for details about how to resolve this.', 'jetpack' ), |
||
6278 | 'https://jetpack.com/support/double-encoding/' |
||
6279 | ); |
||
6280 | } |
||
6281 | |||
6282 | $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) ); |
||
6283 | |||
6284 | if ( isset( $environment['jetpack_json_api_original_query'] ) ) { |
||
6285 | $signature = $jetpack_signature->sign_request( |
||
6286 | $environment['token'], |
||
6287 | $environment['timestamp'], |
||
6288 | $environment['nonce'], |
||
6289 | '', |
||
6290 | 'GET', |
||
6291 | $environment['jetpack_json_api_original_query'], |
||
6292 | null, |
||
6293 | true |
||
6294 | ); |
||
6295 | } else { |
||
6296 | $signature = $jetpack_signature->sign_current_request( array( 'body' => null, 'method' => 'GET' ) ); |
||
6297 | } |
||
6360 | |||
6361 | function login_message_json_api_authorization( $message ) { |
||
6367 | |||
6368 | /** |
||
6369 | * Get $content_width, but with a <s>twist</s> filter. |
||
6370 | */ |
||
6371 | public static function get_content_width() { |
||
6382 | |||
6383 | /** |
||
6384 | * Pings the WordPress.com Mirror Site for the specified options. |
||
6385 | * |
||
6386 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
6387 | * |
||
6388 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
6389 | */ |
||
6390 | public function get_cloud_site_options( $option_names ) { |
||
6406 | |||
6407 | /** |
||
6408 | * Checks if the site is currently in an identity crisis. |
||
6409 | * |
||
6410 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
6411 | */ |
||
6412 | public static function check_identity_crisis() { |
||
6419 | |||
6420 | /** |
||
6421 | * Checks whether the home and siteurl specifically are whitelisted |
||
6422 | * Written so that we don't have re-check $key and $value params every time |
||
6423 | * we want to check if this site is whitelisted, for example in footer.php |
||
6424 | * |
||
6425 | * @since 3.8.0 |
||
6426 | * @return bool True = already whitelisted False = not whitelisted |
||
6427 | */ |
||
6428 | public static function is_staging_site() { |
||
6487 | |||
6488 | /** |
||
6489 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
6490 | * |
||
6491 | * @since 4.4.0 |
||
6492 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
6493 | * |
||
6494 | * @return bool |
||
6495 | */ |
||
6496 | public static function validate_sync_error_idc_option() { |
||
6540 | |||
6541 | /** |
||
6542 | * Normalizes a url by doing three things: |
||
6543 | * - Strips protocol |
||
6544 | * - Strips www |
||
6545 | * - Adds a trailing slash |
||
6546 | * |
||
6547 | * @since 4.4.0 |
||
6548 | * @param string $url |
||
6549 | * @return WP_Error|string |
||
6550 | */ |
||
6551 | public static function normalize_url_protocol_agnostic( $url ) { |
||
6561 | |||
6562 | /** |
||
6563 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
6564 | * |
||
6565 | * @since 4.4.0 |
||
6566 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
6567 | * |
||
6568 | * @param array $response |
||
6569 | * @return array Array of the local urls, wpcom urls, and error code |
||
6570 | */ |
||
6571 | public static function get_sync_error_idc_option( $response = array() ) { |
||
6604 | |||
6605 | /** |
||
6606 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
6607 | * If set to true, the site will be put into staging mode. |
||
6608 | * |
||
6609 | * @since 4.3.2 |
||
6610 | * @return bool |
||
6611 | */ |
||
6612 | public static function sync_idc_optin() { |
||
6630 | |||
6631 | /** |
||
6632 | * Maybe Use a .min.css stylesheet, maybe not. |
||
6633 | * |
||
6634 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
6635 | */ |
||
6636 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
6678 | |||
6679 | /** |
||
6680 | * If the asset is minified, let's flag .min as the suffix. |
||
6681 | * |
||
6682 | * Attached to `style_loader_src` filter. |
||
6683 | * |
||
6684 | * @param string $tag The tag that would link to the external asset. |
||
6685 | * @param string $handle The registered handle of the script in question. |
||
6686 | * @param string $href The url of the asset in question. |
||
6687 | */ |
||
6688 | public static function set_suffix_on_min( $src, $handle ) { |
||
6704 | |||
6705 | /** |
||
6706 | * Maybe inlines a stylesheet. |
||
6707 | * |
||
6708 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
6709 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
6710 | * |
||
6711 | * Attached to `style_loader_tag` filter. |
||
6712 | * |
||
6713 | * @param string $tag The tag that would link to the external asset. |
||
6714 | * @param string $handle The registered handle of the script in question. |
||
6715 | * |
||
6716 | * @return string |
||
6717 | */ |
||
6718 | public static function maybe_inline_style( $tag, $handle ) { |
||
6768 | |||
6769 | /** |
||
6770 | * Loads a view file from the views |
||
6771 | * |
||
6772 | * Data passed in with the $data parameter will be available in the |
||
6773 | * template file as $data['value'] |
||
6774 | * |
||
6775 | * @param string $template - Template file to load |
||
6776 | * @param array $data - Any data to pass along to the template |
||
6777 | * @return boolean - If template file was found |
||
6778 | **/ |
||
6779 | public function load_view( $template, $data = array() ) { |
||
6790 | |||
6791 | /** |
||
6792 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
6793 | */ |
||
6794 | public function deprecated_hooks() { |
||
6856 | |||
6857 | /** |
||
6858 | * Converts any url in a stylesheet, to the correct absolute url. |
||
6859 | * |
||
6860 | * Considerations: |
||
6861 | * - Normal, relative URLs `feh.png` |
||
6862 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
6863 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
6864 | * - Absolute URLs `http://domain.com/feh.png` |
||
6865 | * - Domain root relative URLs `/feh.png` |
||
6866 | * |
||
6867 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
6868 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
6869 | * |
||
6870 | * @return mixed|string |
||
6871 | */ |
||
6872 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6916 | |||
6917 | /** |
||
6918 | * This methods removes all of the registered css files on the front end |
||
6919 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6920 | * all the files into one file. |
||
6921 | * |
||
6922 | * Pros: |
||
6923 | * - Uses only ONE css asset connection instead of 15 |
||
6924 | * - Saves a minimum of 56k |
||
6925 | * - Reduces server load |
||
6926 | * - Reduces time to first painted byte |
||
6927 | * |
||
6928 | * Cons: |
||
6929 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6930 | * should not cause any issues with themes. |
||
6931 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6932 | * jetpack_implode_frontend_css filter for a workaround |
||
6933 | * |
||
6934 | * For some situations developers may wish to disable css imploding and |
||
6935 | * instead operate in legacy mode where each file loads seperately and |
||
6936 | * can be edited individually or dequeued. This can be accomplished with |
||
6937 | * the following line: |
||
6938 | * |
||
6939 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6940 | * |
||
6941 | * @since 3.2 |
||
6942 | **/ |
||
6943 | public function implode_frontend_css( $travis_test = false ) { |
||
7000 | |||
7001 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
7011 | |||
7012 | /* |
||
7013 | * Check the heartbeat data |
||
7014 | * |
||
7015 | * Organizes the heartbeat data by severity. For example, if the site |
||
7016 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
7017 | * |
||
7018 | * Data will be added to "caution" array, if it either: |
||
7019 | * - Out of date Jetpack version |
||
7020 | * - Out of date WP version |
||
7021 | * - Out of date PHP version |
||
7022 | * |
||
7023 | * $return array $filtered_data |
||
7024 | */ |
||
7025 | public static function jetpack_check_heartbeat_data() { |
||
7078 | |||
7079 | |||
7080 | /* |
||
7081 | * This method is used to organize all options that can be reset |
||
7082 | * without disconnecting Jetpack. |
||
7083 | * |
||
7084 | * It is used in class.jetpack-cli.php to reset options |
||
7085 | * |
||
7086 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
7087 | * |
||
7088 | * @return array of options to delete. |
||
7089 | */ |
||
7090 | public static function get_jetpack_options_for_reset() { |
||
7093 | |||
7094 | /** |
||
7095 | * Check if an option of a Jetpack module has been updated. |
||
7096 | * |
||
7097 | * If any module option has been updated before Jump Start has been dismissed, |
||
7098 | * update the 'jumpstart' option so we can hide Jump Start. |
||
7099 | * |
||
7100 | * @param string $option_name |
||
7101 | * |
||
7102 | * @return bool |
||
7103 | */ |
||
7104 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
7125 | |||
7126 | /* |
||
7127 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
7128 | * so we can bring them directly to their site in calypso. |
||
7129 | * |
||
7130 | * @param string | url |
||
7131 | * @return string | url without the guff |
||
7132 | */ |
||
7133 | public static function build_raw_urls( $url ) { |
||
7139 | |||
7140 | /** |
||
7141 | * Stores and prints out domains to prefetch for page speed optimization. |
||
7142 | * |
||
7143 | * @param mixed $new_urls |
||
7144 | */ |
||
7145 | public static function dns_prefetch( $new_urls = null ) { |
||
7162 | |||
7163 | public function wp_dashboard_setup() { |
||
7196 | |||
7197 | /** |
||
7198 | * @param mixed $result Value for the user's option |
||
7199 | * @return mixed |
||
7200 | */ |
||
7201 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
7226 | |||
7227 | public static function dashboard_widget() { |
||
7235 | |||
7236 | public static function dashboard_widget_footer() { |
||
7269 | |||
7270 | /** |
||
7271 | * Return string containing the Jetpack logo. |
||
7272 | * |
||
7273 | * @since 3.9.0 |
||
7274 | * |
||
7275 | * @param bool $logotype Should we use the full logotype (logo + text). Default to false. |
||
7276 | * |
||
7277 | * @return string |
||
7278 | */ |
||
7279 | public static function get_jp_emblem( $logotype = false ) { |
||
7297 | |||
7298 | /* |
||
7299 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
7300 | */ |
||
7301 | function jetpack_icon_user_connected( $columns ) { |
||
7305 | |||
7306 | /* |
||
7307 | * Show Jetpack icon if the user is linked. |
||
7308 | */ |
||
7309 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
7321 | |||
7322 | /* |
||
7323 | * Style the Jetpack user column |
||
7324 | */ |
||
7325 | function jetpack_user_col_style() { |
||
7342 | |||
7343 | /** |
||
7344 | * Checks if Akismet is active and working. |
||
7345 | * |
||
7346 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
7347 | * that implied usage of methods present since more recent version. |
||
7348 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
7349 | * |
||
7350 | * @since 5.1.0 |
||
7351 | * |
||
7352 | * @return bool True = Akismet available. False = Aksimet not available. |
||
7353 | */ |
||
7354 | public static function is_akismet_active() { |
||
7389 | |||
7390 | /** |
||
7391 | * Checks if one or more function names is in debug_backtrace |
||
7392 | * |
||
7393 | * @param $names Mixed string name of function or array of string names of functions |
||
7394 | * |
||
7395 | * @return bool |
||
7396 | */ |
||
7397 | public static function is_function_in_backtrace( $names ) { |
||
7420 | |||
7421 | /** |
||
7422 | * Given a minified path, and a non-minified path, will return |
||
7423 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
7424 | * |
||
7425 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
7426 | * root Jetpack directory. |
||
7427 | * |
||
7428 | * @since 5.6.0 |
||
7429 | * |
||
7430 | * @param string $min_path |
||
7431 | * @param string $non_min_path |
||
7432 | * @return string The URL to the file |
||
7433 | */ |
||
7434 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
7441 | |||
7442 | /** |
||
7443 | * Checks for whether Jetpack Rewind is enabled. |
||
7444 | * Will return true if the state of Rewind is anything except "unavailable". |
||
7445 | * @return bool|int|mixed |
||
7446 | */ |
||
7447 | public static function is_rewind_enabled() { |
||
7466 | |||
7467 | /** |
||
7468 | * Checks whether or not TOS has been agreed upon. |
||
7469 | * Will return true if a user has clicked to register, or is already connected. |
||
7470 | */ |
||
7471 | public static function jetpack_tos_agreed() { |
||
7474 | |||
7475 | /** |
||
7476 | * Handles activating default modules as well general cleanup for the new connection. |
||
7477 | * |
||
7478 | * @param boolean $activate_sso Whether to activate the SSO module when activating default modules. |
||
7479 | * @param boolean $redirect_on_activation_error Whether to redirect on activation error. |
||
7480 | * @param boolean $send_state_messages Whether to send state messages. |
||
7481 | * @return void |
||
7482 | */ |
||
7483 | public static function handle_post_authorization_actions( |
||
7512 | |||
7513 | /** |
||
7514 | * Returns a boolean for whether backups UI should be displayed or not. |
||
7515 | * |
||
7516 | * @return bool Should backups UI be displayed? |
||
7517 | */ |
||
7518 | public static function show_backups_ui() { |
||
7528 | } |
||
7529 |
If you suppress an error, we recommend checking for the error condition explicitly: