Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Jetpack { |
||
28 | public $xmlrpc_server = null; |
||
29 | |||
30 | private $xmlrpc_verification = null; |
||
31 | private $rest_authentication_status = null; |
||
32 | |||
33 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
34 | |||
35 | /** |
||
36 | * @var array The handles of styles that are concatenated into jetpack.css |
||
37 | */ |
||
38 | public $concatenated_style_handles = array( |
||
39 | 'jetpack-carousel', |
||
40 | 'grunion.css', |
||
41 | 'the-neverending-homepage', |
||
42 | 'jetpack_likes', |
||
43 | 'jetpack_related-posts', |
||
44 | 'sharedaddy', |
||
45 | 'jetpack-slideshow', |
||
46 | 'presentations', |
||
47 | 'jetpack-subscriptions', |
||
48 | 'jetpack-responsive-videos-style', |
||
49 | 'jetpack-social-menu', |
||
50 | 'tiled-gallery', |
||
51 | 'jetpack_display_posts_widget', |
||
52 | 'gravatar-profile-widget', |
||
53 | 'goodreads-widget', |
||
54 | 'jetpack_social_media_icons_widget', |
||
55 | 'jetpack-top-posts-widget', |
||
56 | 'jetpack_image_widget', |
||
57 | 'jetpack-my-community-widget', |
||
58 | 'wordads', |
||
59 | ); |
||
60 | |||
61 | public $plugins_to_deactivate = array( |
||
62 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
63 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
64 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
65 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
66 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
67 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
68 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
69 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
70 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
71 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
72 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
73 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
74 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
75 | 'omnisearch' => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ), |
||
76 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
77 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
78 | ); |
||
79 | |||
80 | static $capability_translations = array( |
||
|
|||
81 | 'administrator' => 'manage_options', |
||
82 | 'editor' => 'edit_others_posts', |
||
83 | 'author' => 'publish_posts', |
||
84 | 'contributor' => 'edit_posts', |
||
85 | 'subscriber' => 'read', |
||
86 | ); |
||
87 | |||
88 | /** |
||
89 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
90 | * if the plugins are active. Used by filter_default_modules |
||
91 | * |
||
92 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
93 | * change `module-slug` and add this to your plugin: |
||
94 | * |
||
95 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
96 | * function my_jetpack_get_default_modules( $modules ) { |
||
97 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
98 | * } |
||
99 | * |
||
100 | * @var array |
||
101 | */ |
||
102 | private $conflicting_plugins = array( |
||
103 | 'comments' => array( |
||
104 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
105 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
106 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
107 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
108 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
109 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
110 | ), |
||
111 | 'contact-form' => array( |
||
112 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
113 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
114 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
115 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
116 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
117 | ), |
||
118 | 'minileven' => array( |
||
119 | 'WPtouch' => 'wptouch/wptouch.php', |
||
120 | ), |
||
121 | 'latex' => array( |
||
122 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
123 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
124 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
125 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
126 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
127 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
128 | ), |
||
129 | 'protect' => array( |
||
130 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
131 | 'Captcha' => 'captcha/captcha.php', |
||
132 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
133 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
134 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
135 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
136 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
137 | 'Security-protection' => 'security-protection/security-protection.php', |
||
138 | 'Login Security' => 'login-security/login-security.php', |
||
139 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
140 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
141 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
142 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
143 | ), |
||
144 | 'random-redirect' => array( |
||
145 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
146 | ), |
||
147 | 'related-posts' => array( |
||
148 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
149 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
150 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
151 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
152 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
153 | 'outbrain' => 'outbrain/outbrain.php', |
||
154 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
155 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
156 | ), |
||
157 | 'sharedaddy' => array( |
||
158 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
159 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
160 | 'ShareThis' => 'share-this/sharethis.php', |
||
161 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
162 | ), |
||
163 | 'seo-tools' => array( |
||
164 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
165 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
166 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
167 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
168 | ), |
||
169 | 'verification-tools' => array( |
||
170 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
171 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
172 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
173 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
174 | ), |
||
175 | 'widget-visibility' => array( |
||
176 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
177 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
178 | ), |
||
179 | 'sitemaps' => array( |
||
180 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
181 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
182 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
183 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
184 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
185 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
186 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
187 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
188 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
189 | 'Sitemap' => 'sitemap/sitemap.php', |
||
190 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
191 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
192 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
193 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
194 | ), |
||
195 | ); |
||
196 | |||
197 | /** |
||
198 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
199 | * |
||
200 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
201 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
202 | * |
||
203 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
204 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
205 | */ |
||
206 | private $open_graph_conflicting_plugins = array( |
||
207 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
208 | // 2 Click Social Media Buttons |
||
209 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
210 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
211 | 'autodescription/autodescription.php', // The SEO Framework |
||
212 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
213 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
214 | // Open Graph Meta Tags by Heateor |
||
215 | 'facebook/facebook.php', // Facebook (official plugin) |
||
216 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
217 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
218 | // Facebook Featured Image & OG Meta Tags |
||
219 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
220 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
221 | // Facebook Open Graph Meta Tags for WordPress |
||
222 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
223 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
224 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
225 | // Fedmich's Facebook Open Graph Meta |
||
226 | 'header-footer/plugin.php', // Header and Footer |
||
227 | 'network-publisher/networkpub.php', // Network Publisher |
||
228 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
229 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
230 | // NextScripts SNAP |
||
231 | 'og-tags/og-tags.php', // OG Tags |
||
232 | 'opengraph/opengraph.php', // Open Graph |
||
233 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
234 | // Open Graph Protocol Framework |
||
235 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
236 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
237 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
238 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
239 | 'sharepress/sharepress.php', // SharePress |
||
240 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
241 | 'social-discussions/social-discussions.php', // Social Discussions |
||
242 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
243 | 'socialize/socialize.php', // Socialize |
||
244 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
245 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
246 | // Tweet, Like, Google +1 and Share |
||
247 | 'wordbooker/wordbooker.php', // Wordbooker |
||
248 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
249 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
250 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
251 | // WP Facebook Like Send & Open Graph Meta |
||
252 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
253 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
254 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
255 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php' // WP Facebook Like Button |
||
256 | ); |
||
257 | |||
258 | /** |
||
259 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
260 | */ |
||
261 | private $twitter_cards_conflicting_plugins = array( |
||
262 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
263 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
264 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
265 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
266 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
267 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
268 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
269 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
270 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
271 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
272 | ); |
||
273 | |||
274 | /** |
||
275 | * Message to display in admin_notice |
||
276 | * @var string |
||
277 | */ |
||
278 | public $message = ''; |
||
279 | |||
280 | /** |
||
281 | * Error to display in admin_notice |
||
282 | * @var string |
||
283 | */ |
||
284 | public $error = ''; |
||
285 | |||
286 | /** |
||
287 | * Modules that need more privacy description. |
||
288 | * @var string |
||
289 | */ |
||
290 | public $privacy_checks = ''; |
||
291 | |||
292 | /** |
||
293 | * Stats to record once the page loads |
||
294 | * |
||
295 | * @var array |
||
296 | */ |
||
297 | public $stats = array(); |
||
298 | |||
299 | /** |
||
300 | * Jetpack_Sync object |
||
301 | */ |
||
302 | public $sync; |
||
303 | |||
304 | /** |
||
305 | * Verified data for JSON authorization request |
||
306 | */ |
||
307 | public $json_api_authorization_request = array(); |
||
308 | |||
309 | /** |
||
310 | * Holds the singleton instance of this class |
||
311 | * @since 2.3.3 |
||
312 | * @var Jetpack |
||
313 | */ |
||
314 | static $instance = false; |
||
315 | |||
316 | /** |
||
317 | * Singleton |
||
318 | * @static |
||
319 | */ |
||
320 | public static function init() { |
||
321 | if ( ! self::$instance ) { |
||
322 | self::$instance = new Jetpack; |
||
323 | |||
324 | self::$instance->plugin_upgrade(); |
||
325 | } |
||
326 | |||
327 | return self::$instance; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Must never be called statically |
||
332 | */ |
||
333 | function plugin_upgrade() { |
||
334 | if ( Jetpack::is_active() ) { |
||
335 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
336 | if ( JETPACK__VERSION != $version ) { |
||
337 | |||
338 | // Check which active modules actually exist and remove others from active_modules list |
||
339 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
340 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
341 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
342 | Jetpack::update_active_modules( $modules ); |
||
343 | } |
||
344 | |||
345 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
346 | |||
347 | // Upgrade to 4.3.0 |
||
348 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
349 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
350 | } |
||
351 | |||
352 | Jetpack::maybe_set_version_option(); |
||
353 | |||
354 | add_action( 'wp_loaded', array( __CLASS__, 'migrate_widget_visibility' ) ); |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Upgrade routine to go through all widgets and move the Post Type |
||
361 | * setting to its newer location. |
||
362 | * |
||
363 | * @since 4.7.1 |
||
364 | * |
||
365 | */ |
||
366 | static function migrate_widget_visibility() { |
||
367 | global $wp_registered_widgets; |
||
368 | |||
369 | $sidebars_widgets = get_option( 'sidebars_widgets' ); |
||
370 | |||
371 | // Going through all sidebars and through inactive and orphaned widgets |
||
372 | foreach ( $sidebars_widgets as $s => $sidebar ) { |
||
373 | if ( ! is_array( $sidebar ) ) { |
||
374 | continue; |
||
375 | } |
||
376 | |||
377 | foreach ( $sidebar as $w => $widget ) { |
||
378 | // $widget is the id of the widget |
||
379 | if ( empty( $wp_registered_widgets[$widget] ) ) { |
||
380 | continue; |
||
381 | } |
||
382 | |||
383 | $opts = $wp_registered_widgets[$widget]; |
||
384 | $instances = get_option( $opts['callback'][0]->option_name ); |
||
385 | |||
386 | // Going through each instance of the widget |
||
387 | foreach( $instances as $number => $instance ) { |
||
388 | if ( |
||
389 | ! is_array( $instance ) || |
||
390 | empty( $instance['conditions'] ) || |
||
391 | empty( $instance['conditions']['rules'] ) |
||
392 | ) { |
||
393 | continue; |
||
394 | } |
||
395 | |||
396 | // Going through all visibility rules |
||
397 | foreach( $instance['conditions']['rules'] as $index => $rule ) { |
||
398 | |||
399 | // We only need Post Type rules |
||
400 | if ( 'post_type' !== $rule['major'] ) { |
||
401 | continue; |
||
402 | } |
||
403 | |||
404 | $post_type = false; |
||
405 | |||
406 | // Post type archive rule |
||
407 | View Code Duplication | if ( 0 === strpos( $rule['minor'], 'post_type_archive' ) ) { |
|
408 | $post_type = substr( |
||
409 | $rule['minor'], strlen( 'post_type_archive' ) + 1 |
||
410 | ); |
||
411 | } |
||
412 | |||
413 | // Post type archive rule |
||
414 | View Code Duplication | if ( 0 === strpos( $rule['minor'], 'post_type' ) ) { |
|
415 | $post_type = substr( |
||
416 | $rule['minor'], strlen( 'post_type' ) + 1 |
||
417 | ); |
||
418 | } |
||
419 | |||
420 | if ( $post_type ) { |
||
421 | $rule['minor'] = 'post_type-' . $post_type; |
||
422 | $rule['major'] = 'page'; |
||
423 | |||
424 | $instances[ $number ]['conditions']['rules'][ $index ] = $rule; |
||
425 | } |
||
426 | } |
||
427 | } |
||
428 | |||
429 | update_option( $opts['callback'][0]->option_name, $instances ); |
||
430 | } |
||
431 | } |
||
432 | |||
433 | $sidebars_widgets = get_option( 'sidebars_widgets' ); |
||
434 | } |
||
435 | |||
436 | static function activate_manage( ) { |
||
437 | if ( did_action( 'init' ) || current_filter() == 'init' ) { |
||
438 | self::activate_module( 'manage', false, false ); |
||
439 | } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) { |
||
440 | add_action( 'init', array( __CLASS__, 'activate_manage' ) ); |
||
441 | } |
||
442 | } |
||
443 | |||
444 | static function update_active_modules( $modules ) { |
||
445 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
446 | |||
447 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $modules ) ); |
||
448 | |||
449 | if ( is_array( $modules ) && is_array( $current_modules ) ) { |
||
450 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
451 | foreach( $new_active_modules as $module ) { |
||
452 | /** |
||
453 | * Fires when a specific module is activated. |
||
454 | * |
||
455 | * @since 1.9.0 |
||
456 | * |
||
457 | * @param string $module Module slug. |
||
458 | * @param boolean $success whether the module was activated. @since 4.2 |
||
459 | */ |
||
460 | do_action( 'jetpack_activate_module', $module, $success ); |
||
461 | |||
462 | /** |
||
463 | * Fires when a module is activated. |
||
464 | * The dynamic part of the filter, $module, is the module slug. |
||
465 | * |
||
466 | * @since 1.9.0 |
||
467 | * |
||
468 | * @param string $module Module slug. |
||
469 | */ |
||
470 | do_action( "jetpack_activate_module_$module", $module ); |
||
471 | } |
||
472 | |||
473 | $new_deactive_modules = array_diff( $current_modules, $modules ); |
||
474 | foreach( $new_deactive_modules as $module ) { |
||
475 | /** |
||
476 | * Fired after a module has been deactivated. |
||
477 | * |
||
478 | * @since 4.2.0 |
||
479 | * |
||
480 | * @param string $module Module slug. |
||
481 | * @param boolean $success whether the module was deactivated. |
||
482 | */ |
||
483 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
484 | /** |
||
485 | * Fires when a module is deactivated. |
||
486 | * The dynamic part of the filter, $module, is the module slug. |
||
487 | * |
||
488 | * @since 1.9.0 |
||
489 | * |
||
490 | * @param string $module Module slug. |
||
491 | */ |
||
492 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
493 | } |
||
494 | } |
||
495 | |||
496 | return $success; |
||
497 | } |
||
498 | |||
499 | static function delete_active_modules() { |
||
500 | self::update_active_modules( array() ); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Constructor. Initializes WordPress hooks |
||
505 | */ |
||
506 | private function __construct() { |
||
507 | /* |
||
508 | * Check for and alert any deprecated hooks |
||
509 | */ |
||
510 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
511 | |||
512 | /* |
||
513 | * Load things that should only be in Network Admin. |
||
514 | * |
||
515 | * For now blow away everything else until a more full |
||
516 | * understanding of what is needed at the network level is |
||
517 | * available |
||
518 | */ |
||
519 | if( is_multisite() ) { |
||
520 | Jetpack_Network::init(); |
||
521 | } |
||
522 | |||
523 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
524 | |||
525 | // Unlink user before deleting the user from .com |
||
526 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
527 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
528 | |||
529 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
530 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
531 | |||
532 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
533 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
534 | |||
535 | $this->require_jetpack_authentication(); |
||
536 | |||
537 | if ( Jetpack::is_active() ) { |
||
538 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
539 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
540 | |||
541 | $signed = $this->verify_xml_rpc_signature(); |
||
542 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
543 | // The actual API methods. |
||
544 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
545 | } else { |
||
546 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
547 | // active Jetpack connection, so that additional users can link their account. |
||
548 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
549 | } |
||
550 | } else { |
||
551 | // The bootstrap API methods. |
||
552 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
553 | } |
||
554 | |||
555 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
556 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
557 | } elseif ( |
||
558 | is_admin() && |
||
559 | isset( $_POST['action'] ) && ( |
||
560 | 'jetpack_upload_file' == $_POST['action'] || |
||
561 | 'jetpack_update_file' == $_POST['action'] |
||
562 | ) |
||
563 | ) { |
||
564 | $this->require_jetpack_authentication(); |
||
565 | $this->add_remote_request_handlers(); |
||
566 | } else { |
||
567 | if ( Jetpack::is_active() ) { |
||
568 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
569 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
570 | } |
||
571 | } |
||
572 | |||
573 | if ( Jetpack::is_active() ) { |
||
574 | Jetpack_Heartbeat::init(); |
||
575 | } |
||
576 | |||
577 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
578 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
579 | |||
580 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
581 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
582 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
583 | } |
||
584 | |||
585 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
586 | |||
587 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
588 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
589 | |||
590 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
591 | |||
592 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
593 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
594 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
595 | |||
596 | // returns HTTPS support status |
||
597 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
598 | |||
599 | // If any module option is updated before Jump Start is dismissed, hide Jump Start. |
||
600 | add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) ); |
||
601 | |||
602 | // JITM AJAX callback function |
||
603 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
604 | |||
605 | // Universal ajax callback for all tracking events triggered via js |
||
606 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
607 | |||
608 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
609 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
610 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
611 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
612 | |||
613 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
614 | |||
615 | /** |
||
616 | * These actions run checks to load additional files. |
||
617 | * They check for external files or plugins, so they need to run as late as possible. |
||
618 | */ |
||
619 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
620 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
621 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
622 | |||
623 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
624 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
625 | |||
626 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
627 | |||
628 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
629 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
630 | |||
631 | // A filter to control all just in time messages |
||
632 | add_filter( 'jetpack_just_in_time_msgs', '__return_true' ); |
||
633 | |||
634 | // Update the Jetpack plan from API on heartbeats |
||
635 | add_action( 'jetpack_heartbeat', array( $this, 'refresh_active_plan_from_wpcom' ) ); |
||
636 | |||
637 | /** |
||
638 | * This is the hack to concatinate all css files into one. |
||
639 | * For description and reasoning see the implode_frontend_css method |
||
640 | * |
||
641 | * Super late priority so we catch all the registered styles |
||
642 | */ |
||
643 | if( !is_admin() ) { |
||
644 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
645 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
646 | } |
||
647 | } |
||
648 | |||
649 | function jetpack_admin_ajax_tracks_callback() { |
||
650 | // Check for nonce |
||
651 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
652 | wp_die( 'Permissions check failed.' ); |
||
653 | } |
||
654 | |||
655 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
656 | wp_die( 'No valid event name or type.' ); |
||
657 | } |
||
658 | |||
659 | $tracks_data = array(); |
||
660 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
661 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
662 | } |
||
663 | |||
664 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
665 | wp_send_json_success(); |
||
666 | wp_die(); |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * The callback for the JITM ajax requests. |
||
671 | */ |
||
672 | function jetpack_jitm_ajax_callback() { |
||
673 | // Check for nonce |
||
674 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
675 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
676 | } |
||
677 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
678 | $module_slug = $_REQUEST['jitmModule']; |
||
679 | Jetpack::log( 'activate', $module_slug ); |
||
680 | Jetpack::activate_module( $module_slug, false, false ); |
||
681 | Jetpack::state( 'message', 'no_message' ); |
||
682 | |||
683 | //A Jetpack module is being activated through a JITM, track it |
||
684 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
685 | $this->do_stats( 'server_side' ); |
||
686 | |||
687 | wp_send_json_success(); |
||
688 | } |
||
689 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
690 | // get the hide_jitm options array |
||
691 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
692 | $module_slug = $_REQUEST['jitmModule']; |
||
693 | |||
694 | if( ! $jetpack_hide_jitm ) { |
||
695 | $jetpack_hide_jitm = array( |
||
696 | $module_slug => 'hide' |
||
697 | ); |
||
698 | } else { |
||
699 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
700 | } |
||
701 | |||
702 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
703 | |||
704 | //jitm is being dismissed forever, track it |
||
705 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
706 | $this->do_stats( 'server_side' ); |
||
707 | |||
708 | wp_send_json_success(); |
||
709 | } |
||
710 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
711 | $module_slug = $_REQUEST['jitmModule']; |
||
712 | |||
713 | // User went to WordPress.com, track this |
||
714 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
715 | $this->do_stats( 'server_side' ); |
||
716 | |||
717 | wp_send_json_success(); |
||
718 | } |
||
719 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
720 | $track = $_REQUEST['jitmModule']; |
||
721 | |||
722 | // User is viewing JITM, track it. |
||
723 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
724 | $this->do_stats( 'server_side' ); |
||
725 | |||
726 | wp_send_json_success(); |
||
727 | } |
||
728 | } |
||
729 | |||
730 | /** |
||
731 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
732 | */ |
||
733 | function __destruct() { |
||
734 | if ( ! empty( $this->stats ) ) { |
||
735 | $this->do_stats( 'server_side' ); |
||
736 | } |
||
737 | } |
||
738 | |||
739 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
740 | switch( $cap ) { |
||
741 | case 'jetpack_connect' : |
||
742 | case 'jetpack_reconnect' : |
||
743 | if ( Jetpack::is_development_mode() ) { |
||
744 | $caps = array( 'do_not_allow' ); |
||
745 | break; |
||
746 | } |
||
747 | /** |
||
748 | * Pass through. If it's not development mode, these should match disconnect. |
||
749 | * Let users disconnect if it's development mode, just in case things glitch. |
||
750 | */ |
||
751 | case 'jetpack_disconnect' : |
||
752 | /** |
||
753 | * In multisite, can individual site admins manage their own connection? |
||
754 | * |
||
755 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
756 | */ |
||
757 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
758 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
759 | /** |
||
760 | * We need to update the option name -- it's terribly unclear which |
||
761 | * direction the override goes. |
||
762 | * |
||
763 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
764 | */ |
||
765 | $caps = array( 'do_not_allow' ); |
||
766 | break; |
||
767 | } |
||
768 | } |
||
769 | |||
770 | $caps = array( 'manage_options' ); |
||
771 | break; |
||
772 | case 'jetpack_manage_modules' : |
||
773 | case 'jetpack_activate_modules' : |
||
774 | case 'jetpack_deactivate_modules' : |
||
775 | $caps = array( 'manage_options' ); |
||
776 | break; |
||
777 | case 'jetpack_configure_modules' : |
||
778 | $caps = array( 'manage_options' ); |
||
779 | break; |
||
780 | case 'jetpack_network_admin_page': |
||
781 | case 'jetpack_network_settings_page': |
||
782 | $caps = array( 'manage_network_plugins' ); |
||
783 | break; |
||
784 | case 'jetpack_network_sites_page': |
||
785 | $caps = array( 'manage_sites' ); |
||
786 | break; |
||
787 | case 'jetpack_admin_page' : |
||
788 | if ( Jetpack::is_development_mode() ) { |
||
789 | $caps = array( 'manage_options' ); |
||
790 | break; |
||
791 | } else { |
||
792 | $caps = array( 'read' ); |
||
793 | } |
||
794 | break; |
||
795 | case 'jetpack_connect_user' : |
||
796 | if ( Jetpack::is_development_mode() ) { |
||
797 | $caps = array( 'do_not_allow' ); |
||
798 | break; |
||
799 | } |
||
800 | $caps = array( 'read' ); |
||
801 | break; |
||
802 | } |
||
803 | return $caps; |
||
804 | } |
||
805 | |||
806 | function require_jetpack_authentication() { |
||
807 | // Don't let anyone authenticate |
||
808 | $_COOKIE = array(); |
||
809 | remove_all_filters( 'authenticate' ); |
||
810 | remove_all_actions( 'wp_login_failed' ); |
||
811 | |||
812 | if ( Jetpack::is_active() ) { |
||
813 | // Allow Jetpack authentication |
||
814 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
815 | } |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * Load language files |
||
820 | * @action plugins_loaded |
||
821 | */ |
||
822 | public static function plugin_textdomain() { |
||
823 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
824 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
825 | } |
||
826 | |||
827 | /** |
||
828 | * Register assets for use in various modules and the Jetpack admin page. |
||
829 | * |
||
830 | * @uses wp_script_is, wp_register_script, plugins_url |
||
831 | * @action wp_loaded |
||
832 | * @return null |
||
833 | */ |
||
834 | public function register_assets() { |
||
835 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
836 | wp_register_script( 'spin', plugins_url( '_inc/spin.js', JETPACK__PLUGIN_FILE ), false, '1.3' ); |
||
837 | } |
||
838 | |||
839 | View Code Duplication | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
|
840 | wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' ); |
||
841 | } |
||
842 | |||
843 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
844 | wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' ); |
||
845 | } |
||
846 | |||
847 | View Code Duplication | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
|
848 | wp_register_script( 'jetpack-twitter-timeline', plugins_url( '_inc/twitter-timeline.js', JETPACK__PLUGIN_FILE ) , array( 'jquery' ), '4.0.0', true ); |
||
849 | } |
||
850 | |||
851 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
852 | wp_register_script( 'jetpack-facebook-embed', plugins_url( '_inc/facebook-embed.js', __FILE__ ), array( 'jquery' ), null, true ); |
||
853 | |||
854 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
855 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
856 | if ( ! is_numeric( $fb_app_id ) ) { |
||
857 | $fb_app_id = ''; |
||
858 | } |
||
859 | wp_localize_script( |
||
860 | 'jetpack-facebook-embed', |
||
861 | 'jpfbembed', |
||
862 | array( |
||
863 | 'appid' => $fb_app_id, |
||
864 | 'locale' => $this->get_locale(), |
||
865 | ) |
||
866 | ); |
||
867 | } |
||
868 | |||
869 | /** |
||
870 | * As jetpack_register_genericons is by default fired off a hook, |
||
871 | * the hook may have already fired by this point. |
||
872 | * So, let's just trigger it manually. |
||
873 | */ |
||
874 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
875 | jetpack_register_genericons(); |
||
876 | |||
877 | /** |
||
878 | * Register the social logos |
||
879 | */ |
||
880 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
881 | jetpack_register_social_logos(); |
||
882 | |||
883 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
884 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * Guess locale from language code. |
||
889 | * |
||
890 | * @param string $lang Language code. |
||
891 | * @return string|bool |
||
892 | */ |
||
893 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
894 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
895 | return 'en_US'; |
||
896 | } |
||
897 | |||
898 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
899 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
900 | return false; |
||
901 | } |
||
902 | |||
903 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
904 | } |
||
905 | |||
906 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
907 | // WP.com: get_locale() returns 'it' |
||
908 | $locale = GP_Locales::by_slug( $lang ); |
||
909 | } else { |
||
910 | // Jetpack: get_locale() returns 'it_IT'; |
||
911 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
912 | } |
||
913 | |||
914 | if ( ! $locale ) { |
||
915 | return false; |
||
916 | } |
||
917 | |||
918 | if ( empty( $locale->facebook_locale ) ) { |
||
919 | if ( empty( $locale->wp_locale ) ) { |
||
920 | return false; |
||
921 | } else { |
||
922 | // Facebook SDK is smart enough to fall back to en_US if a |
||
923 | // locale isn't supported. Since supported Facebook locales |
||
924 | // can fall out of sync, we'll attempt to use the known |
||
925 | // wp_locale value and rely on said fallback. |
||
926 | return $locale->wp_locale; |
||
927 | } |
||
928 | } |
||
929 | |||
930 | return $locale->facebook_locale; |
||
931 | } |
||
932 | |||
933 | /** |
||
934 | * Get the locale. |
||
935 | * |
||
936 | * @return string|bool |
||
937 | */ |
||
938 | function get_locale() { |
||
939 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
940 | |||
941 | if ( ! $locale ) { |
||
942 | $locale = 'en_US'; |
||
943 | } |
||
944 | |||
945 | return $locale; |
||
946 | } |
||
947 | |||
948 | /** |
||
949 | * Device Pixels support |
||
950 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
951 | */ |
||
952 | function devicepx() { |
||
953 | if ( Jetpack::is_active() ) { |
||
954 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
955 | } |
||
956 | } |
||
957 | |||
958 | /** |
||
959 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
960 | * @param bool $option |
||
961 | * @return string |
||
962 | */ |
||
963 | public function jetpack_main_network_site_option( $option ) { |
||
964 | return network_site_url(); |
||
965 | } |
||
966 | /** |
||
967 | * Network Name. |
||
968 | */ |
||
969 | static function network_name( $option = null ) { |
||
970 | global $current_site; |
||
971 | return $current_site->site_name; |
||
972 | } |
||
973 | /** |
||
974 | * Does the network allow new user and site registrations. |
||
975 | * @return string |
||
976 | */ |
||
977 | static function network_allow_new_registrations( $option = null ) { |
||
978 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
979 | } |
||
980 | /** |
||
981 | * Does the network allow admins to add new users. |
||
982 | * @return boolian |
||
983 | */ |
||
984 | static function network_add_new_users( $option = null ) { |
||
985 | return (bool) get_site_option( 'add_new_users' ); |
||
986 | } |
||
987 | /** |
||
988 | * File upload psace left per site in MB. |
||
989 | * -1 means NO LIMIT. |
||
990 | * @return number |
||
991 | */ |
||
992 | static function network_site_upload_space( $option = null ) { |
||
993 | // value in MB |
||
994 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
995 | } |
||
996 | |||
997 | /** |
||
998 | * Network allowed file types. |
||
999 | * @return string |
||
1000 | */ |
||
1001 | static function network_upload_file_types( $option = null ) { |
||
1002 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * Maximum file upload size set by the network. |
||
1007 | * @return number |
||
1008 | */ |
||
1009 | static function network_max_upload_file_size( $option = null ) { |
||
1010 | // value in KB |
||
1011 | return get_site_option( 'fileupload_maxk', 300 ); |
||
1012 | } |
||
1013 | |||
1014 | /** |
||
1015 | * Lets us know if a site allows admins to manage the network. |
||
1016 | * @return array |
||
1017 | */ |
||
1018 | static function network_enable_administration_menus( $option = null ) { |
||
1019 | return get_site_option( 'menu_items' ); |
||
1020 | } |
||
1021 | |||
1022 | /** |
||
1023 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
1024 | * jetpack_other_linked_admins transient. |
||
1025 | * |
||
1026 | * @since 4.3.2 |
||
1027 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
1028 | * |
||
1029 | * @param int $user_id The user ID whose role changed. |
||
1030 | * @param string $role The new role. |
||
1031 | * @param array $old_roles An array of the user's previous roles. |
||
1032 | */ |
||
1033 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
1034 | if ( 'administrator' == $role |
||
1035 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
1036 | || is_null( $old_roles ) |
||
1037 | ) { |
||
1038 | delete_transient( 'jetpack_other_linked_admins' ); |
||
1039 | } |
||
1040 | } |
||
1041 | |||
1042 | /** |
||
1043 | * Checks to see if there are any other users available to become primary |
||
1044 | * Users must both: |
||
1045 | * - Be linked to wpcom |
||
1046 | * - Be an admin |
||
1047 | * |
||
1048 | * @return mixed False if no other users are linked, Int if there are. |
||
1049 | */ |
||
1050 | static function get_other_linked_admins() { |
||
1051 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
1052 | |||
1053 | if ( false === $other_linked_users ) { |
||
1054 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
1055 | if ( count( $admins ) > 1 ) { |
||
1056 | $available = array(); |
||
1057 | foreach ( $admins as $admin ) { |
||
1058 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
1059 | $available[] = $admin->ID; |
||
1060 | } |
||
1061 | } |
||
1062 | |||
1063 | $count_connected_admins = count( $available ); |
||
1064 | if ( count( $available ) > 1 ) { |
||
1065 | $other_linked_users = $count_connected_admins; |
||
1066 | } else { |
||
1067 | $other_linked_users = 0; |
||
1068 | } |
||
1069 | } else { |
||
1070 | $other_linked_users = 0; |
||
1071 | } |
||
1072 | |||
1073 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
1074 | } |
||
1075 | |||
1076 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
1077 | } |
||
1078 | |||
1079 | /** |
||
1080 | * Return whether we are dealing with a multi network setup or not. |
||
1081 | * The reason we are type casting this is because we want to avoid the situation where |
||
1082 | * the result is false since when is_main_network_option return false it cases |
||
1083 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
1084 | * database which could be set to anything as opposed to what this function returns. |
||
1085 | * @param bool $option |
||
1086 | * |
||
1087 | * @return boolean |
||
1088 | */ |
||
1089 | public function is_main_network_option( $option ) { |
||
1090 | // return '1' or '' |
||
1091 | return (string) (bool) Jetpack::is_multi_network(); |
||
1092 | } |
||
1093 | |||
1094 | /** |
||
1095 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
1096 | * |
||
1097 | * @param string $option |
||
1098 | * @return boolean |
||
1099 | */ |
||
1100 | public function is_multisite( $option ) { |
||
1101 | return (string) (bool) is_multisite(); |
||
1102 | } |
||
1103 | |||
1104 | /** |
||
1105 | * Implemented since there is no core is multi network function |
||
1106 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
1107 | * |
||
1108 | * @since 3.3 |
||
1109 | * @return boolean |
||
1110 | */ |
||
1111 | public static function is_multi_network() { |
||
1112 | global $wpdb; |
||
1113 | |||
1114 | // if we don't have a multi site setup no need to do any more |
||
1115 | if ( ! is_multisite() ) { |
||
1116 | return false; |
||
1117 | } |
||
1118 | |||
1119 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
1120 | if ( $num_sites > 1 ) { |
||
1121 | return true; |
||
1122 | } else { |
||
1123 | return false; |
||
1124 | } |
||
1125 | } |
||
1126 | |||
1127 | /** |
||
1128 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
1129 | * @return null |
||
1130 | */ |
||
1131 | function update_jetpack_main_network_site_option() { |
||
1132 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1133 | } |
||
1134 | /** |
||
1135 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
1136 | * |
||
1137 | */ |
||
1138 | function update_jetpack_network_settings() { |
||
1139 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1140 | // Only sync this info for the main network site. |
||
1141 | } |
||
1142 | |||
1143 | /** |
||
1144 | * Get back if the current site is single user site. |
||
1145 | * |
||
1146 | * @return bool |
||
1147 | */ |
||
1148 | public static function is_single_user_site() { |
||
1149 | global $wpdb; |
||
1150 | |||
1151 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
1152 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
1153 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
1154 | } |
||
1155 | return 1 === (int) $some_users; |
||
1156 | } |
||
1157 | |||
1158 | /** |
||
1159 | * Returns true if the site has file write access false otherwise. |
||
1160 | * @return string ( '1' | '0' ) |
||
1161 | **/ |
||
1162 | public static function file_system_write_access() { |
||
1163 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
1164 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
1165 | } |
||
1166 | |||
1167 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
1168 | |||
1169 | $filesystem_method = get_filesystem_method(); |
||
1170 | if ( $filesystem_method === 'direct' ) { |
||
1171 | return 1; |
||
1172 | } |
||
1173 | |||
1174 | ob_start(); |
||
1175 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
1176 | ob_end_clean(); |
||
1177 | if ( $filesystem_credentials_are_stored ) { |
||
1178 | return 1; |
||
1179 | } |
||
1180 | return 0; |
||
1181 | } |
||
1182 | |||
1183 | /** |
||
1184 | * Finds out if a site is using a version control system. |
||
1185 | * @return string ( '1' | '0' ) |
||
1186 | **/ |
||
1187 | public static function is_version_controlled() { |
||
1188 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
1189 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
1190 | } |
||
1191 | |||
1192 | /** |
||
1193 | * Determines whether the current theme supports featured images or not. |
||
1194 | * @return string ( '1' | '0' ) |
||
1195 | */ |
||
1196 | public static function featured_images_enabled() { |
||
1197 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1198 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
1199 | } |
||
1200 | |||
1201 | /** |
||
1202 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
1203 | * |
||
1204 | * @deprecated 4.7 use get_avatar_url instead. |
||
1205 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
1206 | * @param int $size Size of the avatar image |
||
1207 | * @param string $default URL to a default image to use if no avatar is available |
||
1208 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
1209 | * |
||
1210 | * @return array |
||
1211 | */ |
||
1212 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
1213 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
1214 | return get_avatar_url( $id_or_email, array( |
||
1215 | 'size' => $size, |
||
1216 | 'default' => $default, |
||
1217 | 'force_default' => $force_display, |
||
1218 | ) ); |
||
1219 | } |
||
1220 | |||
1221 | /** |
||
1222 | * jetpack_updates is saved in the following schema: |
||
1223 | * |
||
1224 | * array ( |
||
1225 | * 'plugins' => (int) Number of plugin updates available. |
||
1226 | * 'themes' => (int) Number of theme updates available. |
||
1227 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
1228 | * 'translations' => (int) Number of translation updates available. |
||
1229 | * 'total' => (int) Total of all available updates. |
||
1230 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
1231 | * ) |
||
1232 | * @return array |
||
1233 | */ |
||
1234 | public static function get_updates() { |
||
1235 | $update_data = wp_get_update_data(); |
||
1236 | |||
1237 | // Stores the individual update counts as well as the total count. |
||
1238 | if ( isset( $update_data['counts'] ) ) { |
||
1239 | $updates = $update_data['counts']; |
||
1240 | } |
||
1241 | |||
1242 | // If we need to update WordPress core, let's find the latest version number. |
||
1243 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
1244 | $cur = get_preferred_from_update_core(); |
||
1245 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
1246 | $updates['wp_update_version'] = $cur->current; |
||
1247 | } |
||
1248 | } |
||
1249 | return isset( $updates ) ? $updates : array(); |
||
1250 | } |
||
1251 | |||
1252 | public static function get_update_details() { |
||
1253 | $update_details = array( |
||
1254 | 'update_core' => get_site_transient( 'update_core' ), |
||
1255 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
1256 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
1257 | ); |
||
1258 | return $update_details; |
||
1259 | } |
||
1260 | |||
1261 | public static function refresh_update_data() { |
||
1262 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1263 | |||
1264 | } |
||
1265 | |||
1266 | public static function refresh_theme_data() { |
||
1267 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
1268 | } |
||
1269 | |||
1270 | /** |
||
1271 | * Is Jetpack active? |
||
1272 | */ |
||
1273 | public static function is_active() { |
||
1274 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1275 | } |
||
1276 | |||
1277 | /** |
||
1278 | * Make an API call to WordPress.com for plan status |
||
1279 | * |
||
1280 | * @uses Jetpack_Options::get_option() |
||
1281 | * @uses Jetpack_Client::wpcom_json_api_request_as_blog() |
||
1282 | * @uses update_option() |
||
1283 | * |
||
1284 | * @access public |
||
1285 | * @static |
||
1286 | * |
||
1287 | * @return bool True if plan is updated, false if no update |
||
1288 | */ |
||
1289 | public static function refresh_active_plan_from_wpcom() { |
||
1290 | // Make the API request |
||
1291 | $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) ); |
||
1292 | $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' ); |
||
1293 | |||
1294 | // Bail if there was an error or malformed response |
||
1295 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
||
1296 | return false; |
||
1297 | } |
||
1298 | |||
1299 | // Decode the results |
||
1300 | $results = json_decode( $response['body'], true ); |
||
1301 | |||
1302 | // Bail if there were no results or plan details returned |
||
1303 | if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) { |
||
1304 | return false; |
||
1305 | } |
||
1306 | |||
1307 | // Store the option and return true if updated |
||
1308 | return update_option( 'jetpack_active_plan', $results['plan'] ); |
||
1309 | } |
||
1310 | |||
1311 | /** |
||
1312 | * Get the plan that this Jetpack site is currently using |
||
1313 | * |
||
1314 | * @uses get_option() |
||
1315 | * |
||
1316 | * @access public |
||
1317 | * @static |
||
1318 | * |
||
1319 | * @return array Active Jetpack plan details |
||
1320 | */ |
||
1321 | public static function get_active_plan() { |
||
1322 | $plan = get_option( 'jetpack_active_plan' ); |
||
1323 | |||
1324 | // Set the default options |
||
1325 | if ( ! $plan ) { |
||
1326 | $plan = array( |
||
1327 | 'product_slug' => 'jetpack_free', |
||
1328 | 'supports' => array(), |
||
1329 | 'class' => 'free', |
||
1330 | ); |
||
1331 | } |
||
1332 | |||
1333 | // Define what paid modules are supported by personal plans |
||
1334 | $personal_plans = array( |
||
1335 | 'jetpack_personal', |
||
1336 | 'jetpack_personal_monthly', |
||
1337 | ); |
||
1338 | |||
1339 | if ( in_array( $plan['product_slug'], $personal_plans ) ) { |
||
1340 | $plan['supports'] = array( |
||
1341 | 'akismet', |
||
1342 | ); |
||
1343 | $plan['class'] = 'personal'; |
||
1344 | } |
||
1345 | |||
1346 | // Define what paid modules are supported by premium plans |
||
1347 | $premium_plans = array( |
||
1348 | 'jetpack_premium', |
||
1349 | 'jetpack_premium_monthly', |
||
1350 | ); |
||
1351 | |||
1352 | View Code Duplication | if ( in_array( $plan['product_slug'], $premium_plans ) ) { |
|
1353 | $plan['supports'] = array( |
||
1354 | 'videopress', |
||
1355 | 'akismet', |
||
1356 | 'vaultpress', |
||
1357 | 'wordads', |
||
1358 | ); |
||
1359 | $plan['class'] = 'premium'; |
||
1360 | } |
||
1361 | |||
1362 | // Define what paid modules are supported by professional plans |
||
1363 | $business_plans = array( |
||
1364 | 'jetpack_business', |
||
1365 | 'jetpack_business_monthly', |
||
1366 | ); |
||
1367 | |||
1368 | View Code Duplication | if ( in_array( $plan['product_slug'], $business_plans ) ) { |
|
1369 | $plan['supports'] = array( |
||
1370 | 'videopress', |
||
1371 | 'akismet', |
||
1372 | 'vaultpress', |
||
1373 | 'seo-tools', |
||
1374 | 'google-analytics', |
||
1375 | 'wordads', |
||
1376 | ); |
||
1377 | $plan['class'] = 'business'; |
||
1378 | } |
||
1379 | |||
1380 | // Make sure we have an array here in the event database data is stale |
||
1381 | if ( ! isset( $plan['supports'] ) ) { |
||
1382 | $plan['supports'] = array(); |
||
1383 | } |
||
1384 | |||
1385 | return $plan; |
||
1386 | } |
||
1387 | |||
1388 | /** |
||
1389 | * Determine whether the active plan supports a particular feature |
||
1390 | * |
||
1391 | * @uses Jetpack::get_active_plan() |
||
1392 | * |
||
1393 | * @access public |
||
1394 | * @static |
||
1395 | * |
||
1396 | * @return bool True if plan supports feature, false if not |
||
1397 | */ |
||
1398 | public static function active_plan_supports( $feature ) { |
||
1399 | $plan = Jetpack::get_active_plan(); |
||
1400 | |||
1401 | if ( in_array( $feature, $plan['supports'] ) ) { |
||
1402 | return true; |
||
1403 | } |
||
1404 | |||
1405 | return false; |
||
1406 | } |
||
1407 | |||
1408 | /** |
||
1409 | * Is Jetpack in development (offline) mode? |
||
1410 | */ |
||
1411 | public static function is_development_mode() { |
||
1412 | $development_mode = false; |
||
1413 | |||
1414 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
1415 | $development_mode = JETPACK_DEV_DEBUG; |
||
1416 | } elseif ( $site_url = site_url() ) { |
||
1417 | $development_mode = false === strpos( $site_url, '.' ); |
||
1418 | } |
||
1419 | |||
1420 | /** |
||
1421 | * Filters Jetpack's development mode. |
||
1422 | * |
||
1423 | * @see https://jetpack.com/support/development-mode/ |
||
1424 | * |
||
1425 | * @since 2.2.1 |
||
1426 | * |
||
1427 | * @param bool $development_mode Is Jetpack's development mode active. |
||
1428 | */ |
||
1429 | return apply_filters( 'jetpack_development_mode', $development_mode ); |
||
1430 | } |
||
1431 | |||
1432 | /** |
||
1433 | * Get Jetpack development mode notice text and notice class. |
||
1434 | * |
||
1435 | * Mirrors the checks made in Jetpack::is_development_mode |
||
1436 | * |
||
1437 | */ |
||
1438 | public static function show_development_mode_notice() { |
||
1439 | if ( Jetpack::is_development_mode() ) { |
||
1440 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
1441 | $notice = sprintf( |
||
1442 | /* translators: %s is a URL */ |
||
1443 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ), |
||
1444 | 'https://jetpack.com/support/development-mode/' |
||
1445 | ); |
||
1446 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
1447 | $notice = sprintf( |
||
1448 | /* translators: %s is a URL */ |
||
1449 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ), |
||
1450 | 'https://jetpack.com/support/development-mode/' |
||
1451 | ); |
||
1452 | } else { |
||
1453 | $notice = sprintf( |
||
1454 | /* translators: %s is a URL */ |
||
1455 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ), |
||
1456 | 'https://jetpack.com/support/development-mode/' |
||
1457 | ); |
||
1458 | } |
||
1459 | |||
1460 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1461 | } |
||
1462 | |||
1463 | // Throw up a notice if using a development version and as for feedback. |
||
1464 | if ( Jetpack::is_development_version() ) { |
||
1465 | /* translators: %s is a URL */ |
||
1466 | $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/' ); |
||
1467 | |||
1468 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1469 | } |
||
1470 | // Throw up a notice if using staging mode |
||
1471 | if ( Jetpack::is_staging_site() ) { |
||
1472 | /* translators: %s is a URL */ |
||
1473 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
1474 | |||
1475 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
1476 | } |
||
1477 | } |
||
1478 | |||
1479 | /** |
||
1480 | * Whether Jetpack's version maps to a public release, or a development version. |
||
1481 | */ |
||
1482 | public static function is_development_version() { |
||
1483 | /** |
||
1484 | * Allows filtering whether this is a development version of Jetpack. |
||
1485 | * |
||
1486 | * This filter is especially useful for tests. |
||
1487 | * |
||
1488 | * @since 4.3.0 |
||
1489 | * |
||
1490 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
1491 | */ |
||
1492 | return (bool) apply_filters( |
||
1493 | 'jetpack_development_version', |
||
1494 | ! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
1495 | ); |
||
1496 | } |
||
1497 | |||
1498 | /** |
||
1499 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
1500 | */ |
||
1501 | public static function is_user_connected( $user_id = false ) { |
||
1502 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
1503 | if ( ! $user_id ) { |
||
1504 | return false; |
||
1505 | } |
||
1506 | |||
1507 | return (bool) Jetpack_Data::get_access_token( $user_id ); |
||
1508 | } |
||
1509 | |||
1510 | /** |
||
1511 | * Get the wpcom user data of the current|specified connected user. |
||
1512 | */ |
||
1513 | public static function get_connected_user_data( $user_id = null ) { |
||
1514 | if ( ! $user_id ) { |
||
1515 | $user_id = get_current_user_id(); |
||
1516 | } |
||
1517 | |||
1518 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
1519 | |||
1520 | if ( $cached_user_data = get_transient( $transient_key ) ) { |
||
1521 | return $cached_user_data; |
||
1522 | } |
||
1523 | |||
1524 | Jetpack::load_xml_rpc_client(); |
||
1525 | $xml = new Jetpack_IXR_Client( array( |
||
1526 | 'user_id' => $user_id, |
||
1527 | ) ); |
||
1528 | $xml->query( 'wpcom.getUser' ); |
||
1529 | if ( ! $xml->isError() ) { |
||
1530 | $user_data = $xml->getResponse(); |
||
1531 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
1532 | return $user_data; |
||
1533 | } |
||
1534 | |||
1535 | return false; |
||
1536 | } |
||
1537 | |||
1538 | /** |
||
1539 | * Get the wpcom email of the current|specified connected user. |
||
1540 | */ |
||
1541 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
1542 | if ( ! $user_id ) { |
||
1543 | $user_id = get_current_user_id(); |
||
1544 | } |
||
1545 | Jetpack::load_xml_rpc_client(); |
||
1546 | $xml = new Jetpack_IXR_Client( array( |
||
1547 | 'user_id' => $user_id, |
||
1548 | ) ); |
||
1549 | $xml->query( 'wpcom.getUserEmail' ); |
||
1550 | if ( ! $xml->isError() ) { |
||
1551 | return $xml->getResponse(); |
||
1552 | } |
||
1553 | return false; |
||
1554 | } |
||
1555 | |||
1556 | /** |
||
1557 | * Get the wpcom email of the master user. |
||
1558 | */ |
||
1559 | public static function get_master_user_email() { |
||
1560 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
1561 | if ( $master_user_id ) { |
||
1562 | return self::get_connected_user_email( $master_user_id ); |
||
1563 | } |
||
1564 | return ''; |
||
1565 | } |
||
1566 | |||
1567 | function current_user_is_connection_owner() { |
||
1568 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
1569 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; |
||
1570 | } |
||
1571 | |||
1572 | /** |
||
1573 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
1574 | */ |
||
1575 | function extra_oembed_providers() { |
||
1576 | // Cloudup: https://dev.cloudup.com/#oembed |
||
1577 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
1578 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
1579 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
1580 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
1581 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
1582 | } |
||
1583 | |||
1584 | /** |
||
1585 | * Synchronize connected user role changes |
||
1586 | */ |
||
1587 | function user_role_change( $user_id ) { |
||
1588 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()' ); |
||
1589 | Jetpack_Sync_Users::user_role_change( $user_id ); |
||
1590 | } |
||
1591 | |||
1592 | /** |
||
1593 | * Loads the currently active modules. |
||
1594 | */ |
||
1595 | public static function load_modules() { |
||
1596 | if ( ! self::is_active() && !self::is_development_mode() ) { |
||
1597 | if ( ! is_multisite() || ! get_site_option( 'jetpack_protect_active' ) ) { |
||
1598 | return; |
||
1599 | } |
||
1600 | } |
||
1601 | |||
1602 | $version = Jetpack_Options::get_option( 'version' ); |
||
1603 | View Code Duplication | if ( ! $version ) { |
|
1604 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
1605 | /** This action is documented in class.jetpack.php */ |
||
1606 | do_action( 'updating_jetpack_version', $version, false ); |
||
1607 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
1608 | } |
||
1609 | list( $version ) = explode( ':', $version ); |
||
1610 | |||
1611 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
1612 | |||
1613 | $modules_data = array(); |
||
1614 | |||
1615 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
1616 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
1617 | $updated_modules = array(); |
||
1618 | foreach ( $modules as $module ) { |
||
1619 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1620 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
1621 | continue; |
||
1622 | } |
||
1623 | |||
1624 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
1625 | continue; |
||
1626 | } |
||
1627 | |||
1628 | $updated_modules[] = $module; |
||
1629 | } |
||
1630 | |||
1631 | $modules = array_diff( $modules, $updated_modules ); |
||
1632 | } |
||
1633 | |||
1634 | $is_development_mode = Jetpack::is_development_mode(); |
||
1635 | |||
1636 | foreach ( $modules as $index => $module ) { |
||
1637 | // If we're in dev mode, disable modules requiring a connection |
||
1638 | if ( $is_development_mode ) { |
||
1639 | // Prime the pump if we need to |
||
1640 | if ( empty( $modules_data[ $module ] ) ) { |
||
1641 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
1642 | } |
||
1643 | // If the module requires a connection, but we're in local mode, don't include it. |
||
1644 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
1645 | continue; |
||
1646 | } |
||
1647 | } |
||
1648 | |||
1649 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
1650 | continue; |
||
1651 | } |
||
1652 | |||
1653 | if ( ! @include( Jetpack::get_module_path( $module ) ) ) { |
||
1654 | unset( $modules[ $index ] ); |
||
1655 | self::update_active_modules( array_values( $modules ) ); |
||
1656 | continue; |
||
1657 | } |
||
1658 | |||
1659 | /** |
||
1660 | * Fires when a specific module is loaded. |
||
1661 | * The dynamic part of the hook, $module, is the module slug. |
||
1662 | * |
||
1663 | * @since 1.1.0 |
||
1664 | */ |
||
1665 | do_action( 'jetpack_module_loaded_' . $module ); |
||
1666 | } |
||
1667 | |||
1668 | /** |
||
1669 | * Fires when all the modules are loaded. |
||
1670 | * |
||
1671 | * @since 1.1.0 |
||
1672 | */ |
||
1673 | do_action( 'jetpack_modules_loaded' ); |
||
1674 | |||
1675 | // Load module-specific code that is needed even when a module isn't active. Loaded here because code contained therein may need actions such as setup_theme. |
||
1676 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) |
||
1677 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
1678 | } |
||
1679 | |||
1680 | /** |
||
1681 | * Check if Jetpack's REST API compat file should be included |
||
1682 | * @action plugins_loaded |
||
1683 | * @return null |
||
1684 | */ |
||
1685 | public function check_rest_api_compat() { |
||
1686 | /** |
||
1687 | * Filters the list of REST API compat files to be included. |
||
1688 | * |
||
1689 | * @since 2.2.5 |
||
1690 | * |
||
1691 | * @param array $args Array of REST API compat files to include. |
||
1692 | */ |
||
1693 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
1694 | |||
1695 | if ( function_exists( 'bbpress' ) ) |
||
1696 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
1697 | |||
1698 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
1699 | require_once $_jetpack_rest_api_compat_include; |
||
1700 | } |
||
1701 | |||
1702 | /** |
||
1703 | * Gets all plugins currently active in values, regardless of whether they're |
||
1704 | * traditionally activated or network activated. |
||
1705 | * |
||
1706 | * @todo Store the result in core's object cache maybe? |
||
1707 | */ |
||
1708 | public static function get_active_plugins() { |
||
1709 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
1710 | |||
1711 | if ( is_multisite() ) { |
||
1712 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
1713 | // whereas active_plugins stores them in the values. |
||
1714 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
1715 | if ( $network_plugins ) { |
||
1716 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
1717 | } |
||
1718 | } |
||
1719 | |||
1720 | sort( $active_plugins ); |
||
1721 | |||
1722 | return array_unique( $active_plugins ); |
||
1723 | } |
||
1724 | |||
1725 | /** |
||
1726 | * Gets and parses additional plugin data to send with the heartbeat data |
||
1727 | * |
||
1728 | * @since 3.8.1 |
||
1729 | * |
||
1730 | * @return array Array of plugin data |
||
1731 | */ |
||
1732 | public static function get_parsed_plugin_data() { |
||
1733 | if ( ! function_exists( 'get_plugins' ) ) { |
||
1734 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
1735 | } |
||
1736 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
1737 | $all_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
1738 | $active_plugins = Jetpack::get_active_plugins(); |
||
1739 | |||
1740 | $plugins = array(); |
||
1741 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
1742 | $plugins[ $path ] = array( |
||
1743 | 'is_active' => in_array( $path, $active_plugins ), |
||
1744 | 'file' => $path, |
||
1745 | 'name' => $plugin_data['Name'], |
||
1746 | 'version' => $plugin_data['Version'], |
||
1747 | 'author' => $plugin_data['Author'], |
||
1748 | ); |
||
1749 | } |
||
1750 | |||
1751 | return $plugins; |
||
1752 | } |
||
1753 | |||
1754 | /** |
||
1755 | * Gets and parses theme data to send with the heartbeat data |
||
1756 | * |
||
1757 | * @since 3.8.1 |
||
1758 | * |
||
1759 | * @return array Array of theme data |
||
1760 | */ |
||
1761 | public static function get_parsed_theme_data() { |
||
1762 | $all_themes = wp_get_themes( array( 'allowed' => true ) ); |
||
1763 | $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); |
||
1764 | |||
1765 | $themes = array(); |
||
1766 | foreach ( $all_themes as $slug => $theme_data ) { |
||
1767 | $theme_headers = array(); |
||
1768 | foreach ( $header_keys as $header_key ) { |
||
1769 | $theme_headers[ $header_key ] = $theme_data->get( $header_key ); |
||
1770 | } |
||
1771 | |||
1772 | $themes[ $slug ] = array( |
||
1773 | 'is_active_theme' => $slug == wp_get_theme()->get_template(), |
||
1774 | 'slug' => $slug, |
||
1775 | 'theme_root' => $theme_data->get_theme_root_uri(), |
||
1776 | 'parent' => $theme_data->parent(), |
||
1777 | 'headers' => $theme_headers |
||
1778 | ); |
||
1779 | } |
||
1780 | |||
1781 | return $themes; |
||
1782 | } |
||
1783 | |||
1784 | /** |
||
1785 | * Checks whether a specific plugin is active. |
||
1786 | * |
||
1787 | * We don't want to store these in a static variable, in case |
||
1788 | * there are switch_to_blog() calls involved. |
||
1789 | */ |
||
1790 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
1791 | return in_array( $plugin, self::get_active_plugins() ); |
||
1792 | } |
||
1793 | |||
1794 | /** |
||
1795 | * Check if Jetpack's Open Graph tags should be used. |
||
1796 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
1797 | * |
||
1798 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
1799 | * @action plugins_loaded |
||
1800 | * @return null |
||
1801 | */ |
||
1802 | public function check_open_graph() { |
||
1803 | if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { |
||
1804 | add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); |
||
1805 | } |
||
1806 | |||
1807 | $active_plugins = self::get_active_plugins(); |
||
1808 | |||
1809 | if ( ! empty( $active_plugins ) ) { |
||
1810 | foreach ( $this->open_graph_conflicting_plugins as $plugin ) { |
||
1811 | if ( in_array( $plugin, $active_plugins ) ) { |
||
1812 | add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); |
||
1813 | break; |
||
1814 | } |
||
1815 | } |
||
1816 | } |
||
1817 | |||
1818 | /** |
||
1819 | * Allow the addition of Open Graph Meta Tags to all pages. |
||
1820 | * |
||
1821 | * @since 2.0.3 |
||
1822 | * |
||
1823 | * @param bool false Should Open Graph Meta tags be added. Default to false. |
||
1824 | */ |
||
1825 | if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { |
||
1826 | require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; |
||
1827 | } |
||
1828 | } |
||
1829 | |||
1830 | /** |
||
1831 | * Check if Jetpack's Twitter tags should be used. |
||
1832 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
1833 | * |
||
1834 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
1835 | * @action plugins_loaded |
||
1836 | * @return null |
||
1837 | */ |
||
1838 | public function check_twitter_tags() { |
||
1839 | |||
1840 | $active_plugins = self::get_active_plugins(); |
||
1841 | |||
1842 | if ( ! empty( $active_plugins ) ) { |
||
1843 | foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { |
||
1844 | if ( in_array( $plugin, $active_plugins ) ) { |
||
1845 | add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); |
||
1846 | break; |
||
1847 | } |
||
1848 | } |
||
1849 | } |
||
1850 | |||
1851 | /** |
||
1852 | * Allow Twitter Card Meta tags to be disabled. |
||
1853 | * |
||
1854 | * @since 2.6.0 |
||
1855 | * |
||
1856 | * @param bool true Should Twitter Card Meta tags be disabled. Default to true. |
||
1857 | */ |
||
1858 | if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) { |
||
1859 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; |
||
1860 | } |
||
1861 | } |
||
1862 | |||
1863 | /** |
||
1864 | * Allows plugins to submit security reports. |
||
1865 | * |
||
1866 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
1867 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
1868 | * @param array $args See definitions above |
||
1869 | */ |
||
1870 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
1871 | _deprecated_function( __FUNCTION__, 'jetpack-4.2', null ); |
||
1872 | } |
||
1873 | |||
1874 | /* Jetpack Options API */ |
||
1875 | |||
1876 | public static function get_option_names( $type = 'compact' ) { |
||
1879 | |||
1880 | /** |
||
1881 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
1882 | * |
||
1883 | * @param string $name Option name |
||
1884 | * @param mixed $default (optional) |
||
1885 | */ |
||
1886 | public static function get_option( $name, $default = false ) { |
||
1889 | |||
1890 | /** |
||
1891 | * Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action |
||
1892 | * Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted |
||
1893 | * $name must be a registered option name. |
||
1894 | */ |
||
1895 | public static function create_nonce( $name ) { |
||
1909 | |||
1910 | /** |
||
1911 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
1912 | * |
||
1913 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
1914 | * @param string $name Option name |
||
1915 | * @param mixed $value Option value |
||
1916 | */ |
||
1917 | public static function update_option( $name, $value ) { |
||
1921 | |||
1922 | /** |
||
1923 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
1924 | * |
||
1925 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
1926 | * @param array $array array( option name => option value, ... ) |
||
1927 | */ |
||
1928 | public static function update_options( $array ) { |
||
1932 | |||
1933 | /** |
||
1934 | * Deletes the given option. May be passed multiple option names as an array. |
||
1935 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
1936 | * |
||
1937 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
1938 | * @param string|array $names |
||
1939 | */ |
||
1940 | public static function delete_option( $names ) { |
||
1944 | |||
1945 | /** |
||
1946 | * Enters a user token into the user_tokens option |
||
1947 | * |
||
1948 | * @param int $user_id |
||
1949 | * @param string $token |
||
1950 | * return bool |
||
1951 | */ |
||
1952 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
1966 | |||
1967 | /** |
||
1968 | * Returns an array of all PHP files in the specified absolute path. |
||
1969 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
1970 | * |
||
1971 | * @param string $absolute_path The absolute path of the directory to search. |
||
1972 | * @return array Array of absolute paths to the PHP files. |
||
1973 | */ |
||
1974 | public static function glob_php( $absolute_path ) { |
||
2003 | |||
2004 | public static function activate_new_modules( $redirect = false ) { |
||
2062 | |||
2063 | /** |
||
2064 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
2065 | * Make sure to tuck away module "library" files in a sub-directory. |
||
2066 | */ |
||
2067 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
2124 | |||
2125 | /** |
||
2126 | * Default modules loaded on activation. |
||
2127 | */ |
||
2128 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
2159 | |||
2160 | /** |
||
2161 | * Checks activated modules during auto-activation to determine |
||
2162 | * if any of those modules are being deprecated. If so, close |
||
2163 | * them out, and add any replacement modules. |
||
2164 | * |
||
2165 | * Runs at priority 99 by default. |
||
2166 | * |
||
2167 | * This is run late, so that it can still activate a module if |
||
2168 | * the new module is a replacement for another that the user |
||
2169 | * currently has active, even if something at the normal priority |
||
2170 | * would kibosh everything. |
||
2171 | * |
||
2172 | * @since 2.6 |
||
2173 | * @uses jetpack_get_default_modules filter |
||
2174 | * @param array $modules |
||
2175 | * @return array |
||
2176 | */ |
||
2177 | function handle_deprecated_modules( $modules ) { |
||
2203 | |||
2204 | /** |
||
2205 | * Checks activated plugins during auto-activation to determine |
||
2206 | * if any of those plugins are in the list with a corresponding module |
||
2207 | * that is not compatible with the plugin. The module will not be allowed |
||
2208 | * to auto-activate. |
||
2209 | * |
||
2210 | * @since 2.6 |
||
2211 | * @uses jetpack_get_default_modules filter |
||
2212 | * @param array $modules |
||
2213 | * @return array |
||
2214 | */ |
||
2215 | function filter_default_modules( $modules ) { |
||
2239 | |||
2240 | /** |
||
2241 | * Extract a module's slug from its full path. |
||
2242 | */ |
||
2243 | public static function get_module_slug( $file ) { |
||
2246 | |||
2247 | /** |
||
2248 | * Generate a module's path from its slug. |
||
2249 | */ |
||
2250 | public static function get_module_path( $slug ) { |
||
2253 | |||
2254 | /** |
||
2255 | * Load module data from module file. Headers differ from WordPress |
||
2256 | * plugin headers to avoid them being identified as standalone |
||
2257 | * plugins on the WordPress plugins page. |
||
2258 | */ |
||
2259 | public static function get_module( $module ) { |
||
2342 | |||
2343 | /** |
||
2344 | * Like core's get_file_data implementation, but caches the result. |
||
2345 | */ |
||
2346 | public static function get_file_data( $file, $headers ) { |
||
2367 | |||
2368 | /** |
||
2369 | * Return translated module tag. |
||
2370 | * |
||
2371 | * @param string $tag Tag as it appears in each module heading. |
||
2372 | * |
||
2373 | * @return mixed |
||
2374 | */ |
||
2375 | public static function translate_module_tag( $tag ) { |
||
2378 | |||
2379 | /** |
||
2380 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
2381 | * |
||
2382 | * @since 3.9.2 |
||
2383 | * |
||
2384 | * @param array $modules |
||
2385 | * |
||
2386 | * @return string|void |
||
2387 | */ |
||
2388 | public static function get_translated_modules( $modules ) { |
||
2389 | foreach ( $modules as $index => $module ) { |
||
2390 | $i18n_module = jetpack_get_module_i18n( $module['module'] ); |
||
2391 | if ( isset( $module['name'] ) ) { |
||
2392 | $modules[ $index ]['name'] = $i18n_module['name']; |
||
2393 | } |
||
2394 | if ( isset( $module['description'] ) ) { |
||
2395 | $modules[ $index ]['description'] = $i18n_module['description']; |
||
2396 | $modules[ $index ]['short_description'] = $i18n_module['description']; |
||
2397 | } |
||
2398 | } |
||
2399 | return $modules; |
||
2400 | } |
||
2401 | |||
2402 | /** |
||
2403 | * Get a list of activated modules as an array of module slugs. |
||
2404 | */ |
||
2405 | public static function get_active_modules() { |
||
2406 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
2407 | |||
2408 | if ( ! is_array( $active ) ) { |
||
2409 | $active = array(); |
||
2410 | } |
||
2411 | |||
2412 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
2413 | $active[] = 'vaultpress'; |
||
2414 | } else { |
||
2415 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
2416 | } |
||
2417 | |||
2418 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
2419 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
2420 | $active[] = 'protect'; |
||
2421 | } |
||
2422 | |||
2423 | return array_unique( $active ); |
||
2424 | } |
||
2425 | |||
2426 | /** |
||
2427 | * Check whether or not a Jetpack module is active. |
||
2428 | * |
||
2429 | * @param string $module The slug of a Jetpack module. |
||
2430 | * @return bool |
||
2431 | * |
||
2432 | * @static |
||
2433 | */ |
||
2434 | public static function is_module_active( $module ) { |
||
2437 | |||
2438 | public static function is_module( $module ) { |
||
2441 | |||
2442 | /** |
||
2443 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
2444 | * |
||
2445 | * @param bool $catch True to start catching, False to stop. |
||
2446 | * |
||
2447 | * @static |
||
2448 | */ |
||
2449 | public static function catch_errors( $catch ) { |
||
2462 | |||
2463 | /** |
||
2464 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
2465 | */ |
||
2466 | public static function catch_errors_on_shutdown() { |
||
2469 | |||
2470 | /** |
||
2471 | * Rewrite any string to make paths easier to read. |
||
2472 | * |
||
2473 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
2474 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
2475 | * |
||
2476 | * @param $string |
||
2477 | * @return mixed |
||
2478 | */ |
||
2479 | public static function alias_directories( $string ) { |
||
2480 | // ABSPATH has a trailing slash. |
||
2481 | $string = str_replace( ABSPATH, 'ABSPATH/', $string ); |
||
2482 | // WP_CONTENT_DIR does not have a trailing slash. |
||
2483 | $string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string ); |
||
2484 | |||
2485 | return $string; |
||
2486 | } |
||
2487 | |||
2488 | public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array(), $redirect = true ) { |
||
2489 | $jetpack = Jetpack::init(); |
||
2490 | |||
2491 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
2492 | $modules = array_merge( $other_modules, $modules ); |
||
2493 | |||
2494 | // Look for standalone plugins and disable if active. |
||
2495 | |||
2496 | $to_deactivate = array(); |
||
2497 | foreach ( $modules as $module ) { |
||
2498 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
2499 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
2500 | } |
||
2501 | } |
||
2502 | |||
2503 | $deactivated = array(); |
||
2504 | foreach ( $to_deactivate as $module => $deactivate_me ) { |
||
2505 | list( $probable_file, $probable_title ) = $deactivate_me; |
||
2506 | if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { |
||
2507 | $deactivated[] = $module; |
||
2508 | } |
||
2509 | } |
||
2510 | |||
2511 | if ( $deactivated && $redirect ) { |
||
2512 | Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) ); |
||
2513 | |||
2514 | $url = add_query_arg( |
||
2515 | array( |
||
2516 | 'action' => 'activate_default_modules', |
||
2517 | '_wpnonce' => wp_create_nonce( 'activate_default_modules' ), |
||
2518 | ), |
||
2519 | add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) |
||
2520 | ); |
||
2521 | wp_safe_redirect( $url ); |
||
2522 | exit; |
||
2523 | } |
||
2524 | |||
2525 | /** |
||
2526 | * Fires before default modules are activated. |
||
2527 | * |
||
2528 | * @since 1.9.0 |
||
2529 | * |
||
2530 | * @param string $min_version Minimum version number required to use modules. |
||
2531 | * @param string $max_version Maximum version number required to use modules. |
||
2532 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
2533 | */ |
||
2534 | do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
2535 | |||
2536 | // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating |
||
2605 | |||
2606 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
2699 | |||
2700 | function activate_module_actions( $module ) { |
||
2703 | |||
2704 | public static function deactivate_module( $module ) { |
||
2731 | |||
2732 | public static function enable_module_configurable( $module ) { |
||
2736 | |||
2737 | public static function module_configuration_url( $module ) { |
||
2741 | |||
2742 | public static function module_configuration_load( $module, $method ) { |
||
2746 | |||
2747 | public static function module_configuration_head( $module, $method ) { |
||
2751 | |||
2752 | public static function module_configuration_screen( $module, $method ) { |
||
2756 | |||
2757 | public static function module_configuration_activation_screen( $module, $method ) { |
||
2761 | |||
2762 | /* Installation */ |
||
2763 | |||
2764 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
2804 | |||
2805 | /** |
||
2806 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
2807 | * @static |
||
2808 | */ |
||
2809 | public static function plugin_activation( $network_wide ) { |
||
2821 | /** |
||
2822 | * Runs before bumping version numbers up to a new version |
||
2823 | * @param (string) $version Version:timestamp |
||
2824 | * @param (string) $old_version Old Version:timestamp or false if not set yet. |
||
2825 | * @return null [description] |
||
2826 | */ |
||
2827 | public static function do_version_bump( $version, $old_version ) { |
||
2834 | |||
2835 | /** |
||
2836 | * Sets the internal version number and activation state. |
||
2837 | * @static |
||
2838 | */ |
||
2839 | public static function plugin_initialize() { |
||
2855 | |||
2856 | /** |
||
2857 | * Removes all connection options |
||
2858 | * @static |
||
2859 | */ |
||
2860 | public static function plugin_deactivation( ) { |
||
2869 | |||
2870 | /** |
||
2871 | * Disconnects from the Jetpack servers. |
||
2872 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
2873 | * @static |
||
2874 | */ |
||
2875 | public static function disconnect( $update_activated_state = true ) { |
||
2936 | |||
2937 | /** |
||
2938 | * Unlinks the current user from the linked WordPress.com user |
||
2939 | */ |
||
2940 | public static function unlink_user( $user_id = null ) { |
||
2971 | |||
2972 | /** |
||
2973 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
2974 | */ |
||
2975 | public static function try_registration() { |
||
3000 | |||
3001 | /** |
||
3002 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
3003 | * |
||
3004 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
3005 | */ |
||
3006 | public static function log( $code, $data = null ) { |
||
3046 | |||
3047 | /** |
||
3048 | * Get the internal event log. |
||
3049 | * |
||
3050 | * @param $event (string) - only return the specific log events |
||
3051 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
3052 | * |
||
3053 | * @return array of log events || WP_Error for invalid params |
||
3054 | */ |
||
3055 | public static function get_log( $event = false, $num = false ) { |
||
3091 | |||
3092 | /** |
||
3093 | * Log modification of important settings. |
||
3094 | */ |
||
3095 | public static function log_settings_change( $option, $old_value, $value ) { |
||
3102 | |||
3103 | /** |
||
3104 | * Return stat data for WPCOM sync |
||
3105 | */ |
||
3106 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
3120 | |||
3121 | /** |
||
3122 | * Get additional stat data to sync to WPCOM |
||
3123 | */ |
||
3124 | public static function get_additional_stat_data( $prefix = '' ) { |
||
3135 | |||
3136 | private static function get_site_user_count() { |
||
3151 | |||
3152 | /* Admin Pages */ |
||
3153 | |||
3154 | function admin_init() { |
||
3203 | |||
3204 | function admin_body_class( $admin_body_class = '' ) { |
||
3212 | |||
3213 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
3216 | |||
3217 | /** |
||
3218 | * Call this function if you want the Big Jetpack Manage Notice to show up. |
||
3219 | * |
||
3220 | * @return null |
||
3221 | */ |
||
3222 | function prepare_manage_jetpack_notice() { |
||
3227 | |||
3228 | function manage_activate_screen() { |
||
3231 | /** |
||
3232 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
3233 | * This function artificially throws errors for such cases (whitelisted). |
||
3234 | * |
||
3235 | * @param string $plugin The activated plugin. |
||
3236 | */ |
||
3237 | function throw_error_on_activate_plugin( $plugin ) { |
||
3261 | |||
3262 | function intercept_plugin_error_scrape_init() { |
||
3265 | |||
3266 | function intercept_plugin_error_scrape( $action, $result ) { |
||
3277 | |||
3278 | function add_remote_request_handlers() { |
||
3282 | |||
3283 | function remote_request_handlers() { |
||
3323 | |||
3324 | /** |
||
3325 | * Uploads a file gotten from the global $_FILES. |
||
3326 | * If `$update_media_item` is true and `post_id` is defined |
||
3327 | * the attachment file of the media item (gotten through of the post_id) |
||
3328 | * will be updated instead of add a new one. |
||
3329 | * |
||
3330 | * @param boolean $update_media_item - update media attachment |
||
3331 | * @return array - An array describing the uploadind files process |
||
3332 | */ |
||
3333 | function upload_handler( $update_media_item = false ) { |
||
3455 | |||
3456 | /** |
||
3457 | * Add help to the Jetpack page |
||
3458 | * |
||
3459 | * @since Jetpack (1.2.3) |
||
3460 | * @return false if not the Jetpack page |
||
3461 | */ |
||
3462 | function admin_help() { |
||
3503 | |||
3504 | function admin_menu_css() { |
||
3507 | |||
3508 | function admin_menu_order() { |
||
3511 | |||
3512 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
3527 | |||
3528 | function admin_head() { |
||
3533 | |||
3534 | function admin_banner_styles() { |
||
3555 | |||
3556 | function plugin_action_links( $actions ) { |
||
3571 | |||
3572 | /** |
||
3573 | * This is the first banner |
||
3574 | * It should be visible only to user that can update the option |
||
3575 | * Are not connected |
||
3576 | * |
||
3577 | * @return null |
||
3578 | */ |
||
3579 | function admin_jetpack_manage_notice() { |
||
3609 | |||
3610 | /** |
||
3611 | * Returns the url that the user clicks to remove the notice for the big banner |
||
3612 | * @return (string) |
||
3613 | */ |
||
3614 | function opt_out_jetpack_manage_url() { |
||
3618 | /** |
||
3619 | * Returns the url that the user clicks to opt in to Jetpack Manage |
||
3620 | * @return (string) |
||
3621 | */ |
||
3622 | function opt_in_jetpack_manage_url() { |
||
3625 | |||
3626 | function opt_in_jetpack_manage_notice() { |
||
3636 | /** |
||
3637 | * Determines whether to show the notice of not true = display notice |
||
3638 | * @return (bool) |
||
3639 | */ |
||
3640 | function can_display_jetpack_manage_notice() { |
||
3662 | |||
3663 | /* |
||
3664 | * Registration flow: |
||
3665 | * 1 - ::admin_page_load() action=register |
||
3666 | * 2 - ::try_registration() |
||
3667 | * 3 - ::register() |
||
3668 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
3669 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
3670 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
3671 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
3672 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
3673 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
3674 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
3675 | * jetpack_id, jetpack_secret, jetpack_public |
||
3676 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
3677 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
3678 | * 5 - user logs in with WP.com account |
||
3679 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
3680 | * - Jetpack_Client_Server::authorize() |
||
3681 | * - Jetpack_Client_Server::get_token() |
||
3682 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
3683 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
3684 | * - which responds with access_token, token_type, scope |
||
3685 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
3686 | * - Jetpack::activate_default_modules() |
||
3687 | * - Deactivates deprecated plugins |
||
3688 | * - Activates all default modules |
||
3689 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
3690 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
3691 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
3692 | * Done! |
||
3693 | */ |
||
3694 | |||
3695 | /** |
||
3696 | * Handles the page load events for the Jetpack admin page |
||
3697 | */ |
||
3698 | function admin_page_load() { |
||
3924 | |||
3925 | function admin_notices() { |
||
4022 | |||
4023 | /** |
||
4024 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
4025 | */ |
||
4026 | function stat( $group, $detail ) { |
||
4031 | |||
4032 | /** |
||
4033 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
4034 | */ |
||
4035 | function do_stats( $method = '' ) { |
||
4050 | |||
4051 | /** |
||
4052 | * Runs stats code for a one-off, server-side. |
||
4053 | * |
||
4054 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
4055 | * |
||
4056 | * @return bool If it worked. |
||
4057 | */ |
||
4058 | static function do_server_side_stat( $args ) { |
||
4068 | |||
4069 | /** |
||
4070 | * Builds the stats url. |
||
4071 | * |
||
4072 | * @param $args array|string The arguments to append to the URL. |
||
4073 | * |
||
4074 | * @return string The URL to be pinged. |
||
4075 | */ |
||
4076 | static function build_stats_url( $args ) { |
||
4096 | |||
4097 | static function translate_current_user_to_role() { |
||
4106 | |||
4107 | static function translate_role_to_cap( $role ) { |
||
4114 | |||
4115 | static function sign_role( $role ) { |
||
4127 | |||
4128 | |||
4129 | /** |
||
4130 | * Builds a URL to the Jetpack connection auth page |
||
4131 | * |
||
4132 | * @since 3.9.5 |
||
4133 | * |
||
4134 | * @param bool $raw If true, URL will not be escaped. |
||
4135 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
4136 | * If string, will be a custom redirect. |
||
4137 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
4138 | * |
||
4139 | * @return string Connect URL |
||
4140 | */ |
||
4141 | function build_connect_url( $raw = false, $redirect = false, $from = false ) { |
||
4225 | |||
4226 | function build_reconnect_url( $raw = false ) { |
||
4230 | |||
4231 | public static function admin_url( $args = null ) { |
||
4236 | |||
4237 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
4241 | |||
4242 | function dismiss_jetpack_notice() { |
||
4316 | |||
4317 | function debugger_page() { |
||
4325 | |||
4326 | public static function admin_screen_configure_module( $module_id ) { |
||
4378 | |||
4379 | /** |
||
4380 | * Display link to activate the module to see the settings screen. |
||
4381 | * @param string $module_id |
||
4382 | * @return null |
||
4383 | */ |
||
4384 | public static function display_activate_module_link( $module_id ) { |
||
4436 | |||
4437 | public static function sort_modules( $a, $b ) { |
||
4443 | |||
4444 | function ajax_recheck_ssl() { |
||
4452 | |||
4453 | /* Client API */ |
||
4454 | |||
4455 | /** |
||
4456 | * Returns the requested Jetpack API URL |
||
4457 | * |
||
4458 | * @return string |
||
4459 | */ |
||
4460 | public static function api_url( $relative_url ) { |
||
4463 | |||
4464 | /** |
||
4465 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
4466 | */ |
||
4467 | public static function fix_url_for_bad_hosts( $url ) { |
||
4483 | |||
4484 | /** |
||
4485 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
4486 | * |
||
4487 | * @since 2.3.3 |
||
4488 | * @return boolean |
||
4489 | */ |
||
4490 | public static function permit_ssl( $force_recheck = false ) { |
||
4532 | |||
4533 | /* |
||
4534 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
4535 | */ |
||
4536 | public function alert_auto_ssl_fail() { |
||
4585 | |||
4586 | /** |
||
4587 | * Returns the Jetpack XML-RPC API |
||
4588 | * |
||
4589 | * @return string |
||
4590 | */ |
||
4591 | public static function xmlrpc_api_url() { |
||
4595 | |||
4596 | /** |
||
4597 | * Creates two secret tokens and the end of life timestamp for them. |
||
4598 | * |
||
4599 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
4600 | * |
||
4601 | * @since 2.6 |
||
4602 | * @return array |
||
4603 | */ |
||
4604 | public function generate_secrets( $action, $exp = 600 ) { |
||
4612 | |||
4613 | /** |
||
4614 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
4615 | * |
||
4616 | * Based on local php max_execution_time in php.ini |
||
4617 | * |
||
4618 | * @since 2.6 |
||
4619 | * @return int |
||
4620 | **/ |
||
4621 | public function get_remote_query_timeout_limit() { |
||
4627 | |||
4628 | |||
4629 | /** |
||
4630 | * Takes the response from the Jetpack register new site endpoint and |
||
4631 | * verifies it worked properly. |
||
4632 | * |
||
4633 | * @since 2.6 |
||
4634 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
4635 | **/ |
||
4636 | public function validate_remote_register_response( $response ) { |
||
4676 | /** |
||
4677 | * @return bool|WP_Error |
||
4678 | */ |
||
4679 | public static function register() { |
||
4769 | |||
4770 | /** |
||
4771 | * If the db version is showing something other that what we've got now, bump it to current. |
||
4772 | * |
||
4773 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
4774 | */ |
||
4775 | public static function maybe_set_version_option() { |
||
4789 | |||
4790 | /* Client Server API */ |
||
4791 | |||
4792 | /** |
||
4793 | * Loads the Jetpack XML-RPC client |
||
4794 | */ |
||
4795 | public static function load_xml_rpc_client() { |
||
4799 | |||
4800 | /** |
||
4801 | * Resets the saved authentication state in between testing requests. |
||
4802 | */ |
||
4803 | public function reset_saved_auth_state() { |
||
4807 | |||
4808 | function verify_xml_rpc_signature() { |
||
4907 | |||
4908 | /** |
||
4909 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
4910 | */ |
||
4911 | function authenticate_jetpack( $user, $username, $password ) { |
||
4934 | |||
4935 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
4936 | // Uses the existing XMLRPC request signing implementation. |
||
4937 | function wp_rest_authenticate( $user ) { |
||
5031 | |||
5032 | /** |
||
5033 | * Report authentication status to the WP REST API. |
||
5034 | * |
||
5035 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
5036 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
5037 | */ |
||
5038 | public function wp_rest_authentication_errors( $value ) { |
||
5044 | |||
5045 | function add_nonce( $timestamp, $nonce ) { |
||
5083 | |||
5084 | /** |
||
5085 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
5086 | * Capture it here so we can verify the signature later. |
||
5087 | */ |
||
5088 | function xmlrpc_methods( $methods ) { |
||
5092 | |||
5093 | function public_xmlrpc_methods( $methods ) { |
||
5099 | |||
5100 | function jetpack_getOptions( $args ) { |
||
5140 | |||
5141 | function xmlrpc_options( $options ) { |
||
5159 | |||
5160 | public static function clean_nonces( $all = false ) { |
||
5181 | |||
5182 | /** |
||
5183 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
5184 | * SET: state( $key, $value ); |
||
5185 | * GET: $value = state( $key ); |
||
5186 | * |
||
5187 | * @param string $key |
||
5188 | * @param string $value |
||
5189 | * @param bool $restate private |
||
5190 | */ |
||
5191 | public static function state( $key = null, $value = null, $restate = false ) { |
||
5241 | |||
5242 | public static function restate() { |
||
5245 | |||
5246 | public static function check_privacy( $file ) { |
||
5281 | |||
5282 | /** |
||
5283 | * Helper method for multicall XMLRPC. |
||
5284 | */ |
||
5285 | public static function xmlrpc_async_call() { |
||
5327 | |||
5328 | public static function staticize_subdomain( $url ) { |
||
5363 | |||
5364 | /* JSON API Authorization */ |
||
5365 | |||
5366 | /** |
||
5367 | * Handles the login action for Authorizing the JSON API |
||
5368 | */ |
||
5369 | function login_form_json_api_authorization() { |
||
5378 | |||
5379 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
5380 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
5393 | |||
5394 | // Make sure the POSTed request is handled by the same action |
||
5395 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
5399 | |||
5400 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
5401 | function store_json_api_authorization_token( $user_login, $user ) { |
||
5407 | |||
5408 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
5409 | function allow_wpcom_public_api_domain( $domains ) { |
||
5413 | |||
5414 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
5415 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
5427 | |||
5428 | |||
5429 | /** |
||
5430 | * Verifies the request by checking the signature |
||
5431 | * |
||
5432 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
5433 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
5434 | * |
||
5435 | * @param null|array $environment |
||
5436 | */ |
||
5437 | function verify_json_api_authorization_request( $environment = null ) { |
||
5530 | |||
5531 | function login_message_json_api_authorization( $message ) { |
||
5537 | |||
5538 | /** |
||
5539 | * Get $content_width, but with a <s>twist</s> filter. |
||
5540 | */ |
||
5541 | public static function get_content_width() { |
||
5552 | |||
5553 | /** |
||
5554 | * Pings the WordPress.com Mirror Site for the specified options. |
||
5555 | * |
||
5556 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
5557 | * |
||
5558 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
5559 | */ |
||
5560 | public function get_cloud_site_options( $option_names ) { |
||
5576 | |||
5577 | /** |
||
5578 | * Checks if the site is currently in an identity crisis. |
||
5579 | * |
||
5580 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
5581 | */ |
||
5582 | public static function check_identity_crisis() { |
||
5589 | |||
5590 | /** |
||
5591 | * Checks whether the home and siteurl specifically are whitelisted |
||
5592 | * Written so that we don't have re-check $key and $value params every time |
||
5593 | * we want to check if this site is whitelisted, for example in footer.php |
||
5594 | * |
||
5595 | * @since 3.8.0 |
||
5596 | * @return bool True = already whitelisted False = not whitelisted |
||
5597 | */ |
||
5598 | public static function is_staging_site() { |
||
5657 | |||
5658 | /** |
||
5659 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
5660 | * |
||
5661 | * @return bool |
||
5662 | */ |
||
5663 | public static function validate_sync_error_idc_option() { |
||
5707 | |||
5708 | /** |
||
5709 | * Normalizes a url by doing three things: |
||
5710 | * - Strips protocol |
||
5711 | * - Strips www |
||
5712 | * - Adds a trailing slash |
||
5713 | * |
||
5714 | * @since 4.4.0 |
||
5715 | * @param string $url |
||
5716 | * @return WP_Error|string |
||
5717 | */ |
||
5718 | public static function normalize_url_protocol_agnostic( $url ) { |
||
5728 | |||
5729 | /** |
||
5730 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
5731 | * |
||
5732 | * @since 4.4.0 |
||
5733 | * |
||
5734 | * @param array $response |
||
5735 | * @return array Array of the local urls, wpcom urls, and error code |
||
5736 | */ |
||
5737 | public static function get_sync_error_idc_option( $response = array() ) { |
||
5761 | |||
5762 | /** |
||
5763 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
5764 | * If set to true, the site will be put into staging mode. |
||
5765 | * |
||
5766 | * @since 4.3.2 |
||
5767 | * @return bool |
||
5768 | */ |
||
5769 | public static function sync_idc_optin() { |
||
5787 | |||
5788 | /** |
||
5789 | * Maybe Use a .min.css stylesheet, maybe not. |
||
5790 | * |
||
5791 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
5792 | */ |
||
5793 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
5828 | |||
5829 | /** |
||
5830 | * Maybe inlines a stylesheet. |
||
5831 | * |
||
5832 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
5833 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
5834 | * |
||
5835 | * Attached to `style_loader_tag` filter. |
||
5836 | * |
||
5837 | * @param string $tag The tag that would link to the external asset. |
||
5838 | * @param string $handle The registered handle of the script in question. |
||
5839 | * |
||
5840 | * @return string |
||
5841 | */ |
||
5842 | public static function maybe_inline_style( $tag, $handle ) { |
||
5892 | |||
5893 | /** |
||
5894 | * Loads a view file from the views |
||
5895 | * |
||
5896 | * Data passed in with the $data parameter will be available in the |
||
5897 | * template file as $data['value'] |
||
5898 | * |
||
5899 | * @param string $template - Template file to load |
||
5900 | * @param array $data - Any data to pass along to the template |
||
5901 | * @return boolean - If template file was found |
||
5902 | **/ |
||
5903 | public function load_view( $template, $data = array() ) { |
||
5914 | |||
5915 | /** |
||
5916 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
5917 | */ |
||
5918 | public function deprecated_hooks() { |
||
5967 | |||
5968 | /** |
||
5969 | * Converts any url in a stylesheet, to the correct absolute url. |
||
5970 | * |
||
5971 | * Considerations: |
||
5972 | * - Normal, relative URLs `feh.png` |
||
5973 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
5974 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
5975 | * - Absolute URLs `http://domain.com/feh.png` |
||
5976 | * - Domain root relative URLs `/feh.png` |
||
5977 | * |
||
5978 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
5979 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
5980 | * |
||
5981 | * @return mixed|string |
||
5982 | */ |
||
5983 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
6027 | |||
6028 | /** |
||
6029 | * This methods removes all of the registered css files on the front end |
||
6030 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
6031 | * all the files into one file. |
||
6032 | * |
||
6033 | * Pros: |
||
6034 | * - Uses only ONE css asset connection instead of 15 |
||
6035 | * - Saves a minimum of 56k |
||
6036 | * - Reduces server load |
||
6037 | * - Reduces time to first painted byte |
||
6038 | * |
||
6039 | * Cons: |
||
6040 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
6041 | * should not cause any issues with themes. |
||
6042 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
6043 | * jetpack_implode_frontend_css filter for a workaround |
||
6044 | * |
||
6045 | * For some situations developers may wish to disable css imploding and |
||
6046 | * instead operate in legacy mode where each file loads seperately and |
||
6047 | * can be edited individually or dequeued. This can be accomplished with |
||
6048 | * the following line: |
||
6049 | * |
||
6050 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
6051 | * |
||
6052 | * @since 3.2 |
||
6053 | **/ |
||
6054 | public function implode_frontend_css( $travis_test = false ) { |
||
6106 | |||
6107 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
6117 | |||
6118 | /* |
||
6119 | * Check the heartbeat data |
||
6120 | * |
||
6121 | * Organizes the heartbeat data by severity. For example, if the site |
||
6122 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
6123 | * |
||
6124 | * Data will be added to "caution" array, if it either: |
||
6125 | * - Out of date Jetpack version |
||
6126 | * - Out of date WP version |
||
6127 | * - Out of date PHP version |
||
6128 | * |
||
6129 | * $return array $filtered_data |
||
6130 | */ |
||
6131 | public static function jetpack_check_heartbeat_data() { |
||
6184 | |||
6185 | |||
6186 | /* |
||
6187 | * This method is used to organize all options that can be reset |
||
6188 | * without disconnecting Jetpack. |
||
6189 | * |
||
6190 | * It is used in class.jetpack-cli.php to reset options |
||
6191 | * |
||
6192 | * @return array of options to delete. |
||
6193 | */ |
||
6194 | public static function get_jetpack_options_for_reset() { |
||
6261 | |||
6262 | /** |
||
6263 | * Check if an option of a Jetpack module has been updated. |
||
6264 | * |
||
6265 | * If any module option has been updated before Jump Start has been dismissed, |
||
6266 | * update the 'jumpstart' option so we can hide Jump Start. |
||
6267 | * |
||
6268 | * @param string $option_name |
||
6269 | * |
||
6270 | * @return bool |
||
6271 | */ |
||
6272 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
6293 | |||
6294 | /* |
||
6295 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
6296 | * so we can bring them directly to their site in calypso. |
||
6297 | * |
||
6298 | * @param string | url |
||
6299 | * @return string | url without the guff |
||
6300 | */ |
||
6301 | public static function build_raw_urls( $url ) { |
||
6307 | |||
6308 | /** |
||
6309 | * Stores and prints out domains to prefetch for page speed optimization. |
||
6310 | * |
||
6311 | * @param mixed $new_urls |
||
6312 | */ |
||
6313 | public static function dns_prefetch( $new_urls = null ) { |
||
6330 | |||
6331 | public function wp_dashboard_setup() { |
||
6359 | |||
6360 | /** |
||
6361 | * @param mixed $result Value for the user's option |
||
6362 | * @return mixed |
||
6363 | */ |
||
6364 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
6389 | |||
6390 | public static function dashboard_widget() { |
||
6398 | |||
6399 | public static function dashboard_widget_footer() { |
||
6432 | |||
6433 | public function dashboard_widget_connect_to_wpcom() { |
||
6455 | |||
6456 | /** |
||
6457 | * Return string containing the Jetpack logo. |
||
6458 | * |
||
6459 | * @since 3.9.0 |
||
6460 | * |
||
6461 | * @return string |
||
6462 | */ |
||
6463 | public static function get_jp_emblem() { |
||
6466 | |||
6467 | /* |
||
6468 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
6469 | */ |
||
6470 | function jetpack_icon_user_connected( $columns ) { |
||
6474 | |||
6475 | /* |
||
6476 | * Show Jetpack icon if the user is linked. |
||
6477 | */ |
||
6478 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
6490 | |||
6491 | /* |
||
6492 | * Style the Jetpack user column |
||
6493 | */ |
||
6494 | function jetpack_user_col_style() { |
||
6511 | } |
||
6512 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.