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 |
||
| 40 | class Jetpack { |
||
| 41 | public $xmlrpc_server = null; |
||
| 42 | |||
| 43 | private $rest_authentication_status = null; |
||
| 44 | |||
| 45 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
| 46 | |||
| 47 | private $tracking; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array The handles of styles that are concatenated into jetpack.css. |
||
| 51 | * |
||
| 52 | * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js. |
||
| 53 | */ |
||
| 54 | public $concatenated_style_handles = array( |
||
| 55 | 'jetpack-carousel', |
||
| 56 | 'grunion.css', |
||
| 57 | 'the-neverending-homepage', |
||
| 58 | 'jetpack_likes', |
||
| 59 | 'jetpack_related-posts', |
||
| 60 | 'sharedaddy', |
||
| 61 | 'jetpack-slideshow', |
||
| 62 | 'presentations', |
||
| 63 | 'quiz', |
||
| 64 | 'jetpack-subscriptions', |
||
| 65 | 'jetpack-responsive-videos-style', |
||
| 66 | 'jetpack-social-menu', |
||
| 67 | 'tiled-gallery', |
||
| 68 | 'jetpack_display_posts_widget', |
||
| 69 | 'gravatar-profile-widget', |
||
| 70 | 'goodreads-widget', |
||
| 71 | 'jetpack_social_media_icons_widget', |
||
| 72 | 'jetpack-top-posts-widget', |
||
| 73 | 'jetpack_image_widget', |
||
| 74 | 'jetpack-my-community-widget', |
||
| 75 | 'jetpack-authors-widget', |
||
| 76 | 'wordads', |
||
| 77 | 'eu-cookie-law-style', |
||
| 78 | 'flickr-widget-style', |
||
| 79 | 'jetpack-search-widget', |
||
| 80 | 'jetpack-simple-payments-widget-style', |
||
| 81 | 'jetpack-widget-social-icons-styles', |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Contains all assets that have had their URL rewritten to minified versions. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | static $min_assets = array(); |
||
| 90 | |||
| 91 | public $plugins_to_deactivate = array( |
||
| 92 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 93 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 94 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
| 95 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
| 96 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
| 97 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
| 98 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
| 99 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
| 100 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
| 101 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
| 102 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
| 103 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
| 104 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
| 105 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Map of roles we care about, and their corresponding minimum capabilities. |
||
| 110 | * |
||
| 111 | * @deprecated 7.6 Use Automattic\Jetpack\Roles::$capability_translations instead. |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * @static |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | public static $capability_translations = array( |
||
| 119 | 'administrator' => 'manage_options', |
||
| 120 | 'editor' => 'edit_others_posts', |
||
| 121 | 'author' => 'publish_posts', |
||
| 122 | 'contributor' => 'edit_posts', |
||
| 123 | 'subscriber' => 'read', |
||
| 124 | ); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
| 128 | * if the plugins are active. Used by filter_default_modules |
||
| 129 | * |
||
| 130 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
| 131 | * change `module-slug` and add this to your plugin: |
||
| 132 | * |
||
| 133 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
| 134 | * function my_jetpack_get_default_modules( $modules ) { |
||
| 135 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
| 136 | * } |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $conflicting_plugins = array( |
||
| 141 | 'comments' => array( |
||
| 142 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
| 143 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
| 144 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
| 145 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
| 146 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
| 147 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
| 148 | ), |
||
| 149 | 'comment-likes' => array( |
||
| 150 | 'Epoch' => 'epoch/plugincore.php', |
||
| 151 | ), |
||
| 152 | 'contact-form' => array( |
||
| 153 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
| 154 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
| 155 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
| 156 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
| 157 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
| 158 | 'Ninja Forms' => 'ninja-forms/ninja-forms.php', |
||
| 159 | ), |
||
| 160 | 'minileven' => array( |
||
| 161 | 'WPtouch' => 'wptouch/wptouch.php', |
||
| 162 | ), |
||
| 163 | 'latex' => array( |
||
| 164 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
| 165 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
| 166 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
| 167 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
| 168 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
| 169 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
| 170 | ), |
||
| 171 | 'protect' => array( |
||
| 172 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
| 173 | 'Captcha' => 'captcha/captcha.php', |
||
| 174 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
| 175 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
| 176 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
| 177 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
| 178 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
| 179 | 'Security-protection' => 'security-protection/security-protection.php', |
||
| 180 | 'Login Security' => 'login-security/login-security.php', |
||
| 181 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
| 182 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
| 183 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
| 184 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
| 185 | ), |
||
| 186 | 'random-redirect' => array( |
||
| 187 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
| 188 | ), |
||
| 189 | 'related-posts' => array( |
||
| 190 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
| 191 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
| 192 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
| 193 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
| 194 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
| 195 | 'outbrain' => 'outbrain/outbrain.php', |
||
| 196 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 197 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
| 198 | ), |
||
| 199 | 'sharedaddy' => array( |
||
| 200 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
| 201 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
| 202 | 'ShareThis' => 'share-this/sharethis.php', |
||
| 203 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 204 | ), |
||
| 205 | 'seo-tools' => array( |
||
| 206 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 207 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 208 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 209 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 210 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
| 211 | ), |
||
| 212 | 'verification-tools' => array( |
||
| 213 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 214 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 215 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 216 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 217 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
| 218 | ), |
||
| 219 | 'widget-visibility' => array( |
||
| 220 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
| 221 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
| 222 | ), |
||
| 223 | 'sitemaps' => array( |
||
| 224 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
| 225 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
| 226 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
| 227 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
| 228 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
| 229 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 230 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 231 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 232 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 233 | 'The SEO Framework' => 'autodescription/autodescription.php', |
||
| 234 | 'Sitemap' => 'sitemap/sitemap.php', |
||
| 235 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
| 236 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
| 237 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
| 238 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
| 239 | ), |
||
| 240 | 'lazy-images' => array( |
||
| 241 | 'Lazy Load' => 'lazy-load/lazy-load.php', |
||
| 242 | 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php', |
||
| 243 | 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php', |
||
| 244 | ), |
||
| 245 | ); |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
| 249 | * |
||
| 250 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
| 251 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
| 252 | * |
||
| 253 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
| 254 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
| 255 | */ |
||
| 256 | private $open_graph_conflicting_plugins = array( |
||
| 257 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
| 258 | // 2 Click Social Media Buttons |
||
| 259 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
| 260 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
| 261 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
| 262 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
| 263 | // Open Graph Meta Tags by Heateor |
||
| 264 | 'facebook/facebook.php', // Facebook (official plugin) |
||
| 265 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
| 266 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
| 267 | // Facebook Featured Image & OG Meta Tags |
||
| 268 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
| 269 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
| 270 | // Facebook Open Graph Meta Tags for WordPress |
||
| 271 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
| 272 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
| 273 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
| 274 | // Fedmich's Facebook Open Graph Meta |
||
| 275 | 'network-publisher/networkpub.php', // Network Publisher |
||
| 276 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
| 277 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
| 278 | // NextScripts SNAP |
||
| 279 | 'og-tags/og-tags.php', // OG Tags |
||
| 280 | 'opengraph/opengraph.php', // Open Graph |
||
| 281 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
| 282 | // Open Graph Protocol Framework |
||
| 283 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
| 284 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
| 285 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
| 286 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
| 287 | 'sharepress/sharepress.php', // SharePress |
||
| 288 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
| 289 | 'social-discussions/social-discussions.php', // Social Discussions |
||
| 290 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
| 291 | 'socialize/socialize.php', // Socialize |
||
| 292 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
| 293 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
| 294 | // Tweet, Like, Google +1 and Share |
||
| 295 | 'wordbooker/wordbooker.php', // Wordbooker |
||
| 296 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
| 297 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
| 298 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
| 299 | // WP Facebook Like Send & Open Graph Meta |
||
| 300 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
| 301 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
| 302 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
| 303 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button |
||
| 304 | 'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox |
||
| 305 | ); |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
| 309 | */ |
||
| 310 | private $twitter_cards_conflicting_plugins = array( |
||
| 311 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
| 312 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
| 313 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
| 314 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
| 315 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
| 316 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
| 317 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
| 318 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
| 319 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
| 320 | 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter |
||
| 321 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
| 322 | ); |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Message to display in admin_notice |
||
| 326 | * @var string |
||
| 327 | */ |
||
| 328 | public $message = ''; |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Error to display in admin_notice |
||
| 332 | * @var string |
||
| 333 | */ |
||
| 334 | public $error = ''; |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Modules that need more privacy description. |
||
| 338 | * @var string |
||
| 339 | */ |
||
| 340 | public $privacy_checks = ''; |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Stats to record once the page loads |
||
| 344 | * |
||
| 345 | * @var array |
||
| 346 | */ |
||
| 347 | public $stats = array(); |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Jetpack_Sync object |
||
| 351 | */ |
||
| 352 | public $sync; |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Verified data for JSON authorization request |
||
| 356 | */ |
||
| 357 | public $json_api_authorization_request = array(); |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @var \Automattic\Jetpack\Connection\Manager |
||
| 361 | */ |
||
| 362 | protected $connection_manager; |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @var string Transient key used to prevent multiple simultaneous plugin upgrades |
||
| 366 | */ |
||
| 367 | public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock'; |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Holds the singleton instance of this class |
||
| 371 | * @since 2.3.3 |
||
| 372 | * @var Jetpack |
||
| 373 | */ |
||
| 374 | static $instance = false; |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Singleton |
||
| 378 | * @static |
||
| 379 | */ |
||
| 380 | public static function init() { |
||
| 381 | if ( ! self::$instance ) { |
||
| 382 | self::$instance = new Jetpack; |
||
| 383 | |||
| 384 | self::$instance->plugin_upgrade(); |
||
| 385 | } |
||
| 386 | |||
| 387 | return self::$instance; |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Must never be called statically |
||
| 392 | */ |
||
| 393 | function plugin_upgrade() { |
||
| 394 | if ( Jetpack::is_active() ) { |
||
| 395 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
| 396 | if ( JETPACK__VERSION != $version ) { |
||
| 397 | // Prevent multiple upgrades at once - only a single process should trigger |
||
| 398 | // an upgrade to avoid stampedes |
||
| 399 | if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) { |
||
| 400 | return; |
||
| 401 | } |
||
| 402 | |||
| 403 | // Set a short lock to prevent multiple instances of the upgrade |
||
| 404 | set_transient( self::$plugin_upgrade_lock_key, 1, 10 ); |
||
| 405 | |||
| 406 | // check which active modules actually exist and remove others from active_modules list |
||
| 407 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
| 408 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
| 409 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
| 410 | Jetpack::update_active_modules( $modules ); |
||
| 411 | } |
||
| 412 | |||
| 413 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
| 414 | |||
| 415 | // Upgrade to 4.3.0 |
||
| 416 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
| 417 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
| 418 | } |
||
| 419 | |||
| 420 | // Make sure Markdown for posts gets turned back on |
||
| 421 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
| 422 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
| 423 | } |
||
| 424 | |||
| 425 | if ( did_action( 'wp_loaded' ) ) { |
||
| 426 | self::upgrade_on_load(); |
||
| 427 | } else { |
||
| 428 | add_action( |
||
| 429 | 'wp_loaded', |
||
| 430 | array( __CLASS__, 'upgrade_on_load' ) |
||
| 431 | ); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Runs upgrade routines that need to have modules loaded. |
||
| 439 | */ |
||
| 440 | static function upgrade_on_load() { |
||
| 441 | |||
| 442 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
| 443 | // This can happen in case Jetpack has been just upgraded and is |
||
| 444 | // being initialized late during the page load. In this case we wait |
||
| 445 | // until the next proper admin page load with Jetpack active. |
||
| 446 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
| 447 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
| 448 | |||
| 449 | return; |
||
| 450 | } |
||
| 451 | |||
| 452 | Jetpack::maybe_set_version_option(); |
||
| 453 | |||
| 454 | if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) { |
||
| 455 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
| 456 | } |
||
| 457 | |||
| 458 | if ( |
||
| 459 | class_exists( 'Jetpack_Sitemap_Manager' ) |
||
| 460 | && version_compare( JETPACK__VERSION, '5.3', '>=' ) |
||
| 461 | ) { |
||
| 462 | do_action( 'jetpack_sitemaps_purge_data' ); |
||
| 463 | } |
||
| 464 | |||
| 465 | // Delete old stats cache |
||
| 466 | delete_option( 'jetpack_restapi_stats_cache' ); |
||
| 467 | |||
| 468 | delete_transient( self::$plugin_upgrade_lock_key ); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Saves all the currently active modules to options. |
||
| 473 | * Also fires Action hooks for each newly activated and deactivated module. |
||
| 474 | * |
||
| 475 | * @param $modules Array Array of active modules to be saved in options. |
||
| 476 | * |
||
| 477 | * @return $success bool true for success, false for failure. |
||
|
|
|||
| 478 | */ |
||
| 479 | static function update_active_modules( $modules ) { |
||
| 480 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
| 481 | $active_modules = Jetpack::get_active_modules(); |
||
| 482 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
| 483 | $new_inactive_modules = array_diff( $active_modules, $modules ); |
||
| 484 | $new_current_modules = array_diff( array_merge( $current_modules, $new_active_modules ), $new_inactive_modules ); |
||
| 485 | $reindexed_modules = array_values( $new_current_modules ); |
||
| 486 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $reindexed_modules ) ); |
||
| 487 | |||
| 488 | foreach ( $new_active_modules as $module ) { |
||
| 489 | /** |
||
| 490 | * Fires when a specific module is activated. |
||
| 491 | * |
||
| 492 | * @since 1.9.0 |
||
| 493 | * |
||
| 494 | * @param string $module Module slug. |
||
| 495 | * @param boolean $success whether the module was activated. @since 4.2 |
||
| 496 | */ |
||
| 497 | do_action( 'jetpack_activate_module', $module, $success ); |
||
| 498 | /** |
||
| 499 | * Fires when a module is activated. |
||
| 500 | * The dynamic part of the filter, $module, is the module slug. |
||
| 501 | * |
||
| 502 | * @since 1.9.0 |
||
| 503 | * |
||
| 504 | * @param string $module Module slug. |
||
| 505 | */ |
||
| 506 | do_action( "jetpack_activate_module_$module", $module ); |
||
| 507 | } |
||
| 508 | |||
| 509 | foreach ( $new_inactive_modules as $module ) { |
||
| 510 | /** |
||
| 511 | * Fired after a module has been deactivated. |
||
| 512 | * |
||
| 513 | * @since 4.2.0 |
||
| 514 | * |
||
| 515 | * @param string $module Module slug. |
||
| 516 | * @param boolean $success whether the module was deactivated. |
||
| 517 | */ |
||
| 518 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
| 519 | /** |
||
| 520 | * Fires when a module is deactivated. |
||
| 521 | * The dynamic part of the filter, $module, is the module slug. |
||
| 522 | * |
||
| 523 | * @since 1.9.0 |
||
| 524 | * |
||
| 525 | * @param string $module Module slug. |
||
| 526 | */ |
||
| 527 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
| 528 | } |
||
| 529 | |||
| 530 | return $success; |
||
| 531 | } |
||
| 532 | |||
| 533 | static function delete_active_modules() { |
||
| 534 | self::update_active_modules( array() ); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Constructor. Initializes WordPress hooks |
||
| 539 | */ |
||
| 540 | private function __construct() { |
||
| 541 | /* |
||
| 542 | * Check for and alert any deprecated hooks |
||
| 543 | */ |
||
| 544 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
| 545 | |||
| 546 | /* |
||
| 547 | * Enable enhanced handling of previewing sites in Calypso |
||
| 548 | */ |
||
| 549 | if ( Jetpack::is_active() ) { |
||
| 550 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php'; |
||
| 551 | add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 ); |
||
| 552 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php'; |
||
| 553 | add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 ); |
||
| 554 | } |
||
| 555 | |||
| 556 | if ( self::jetpack_tos_agreed() ) { |
||
| 557 | $tracking = new Automattic\Jetpack\Plugin\Tracking(); |
||
| 558 | add_action( 'init', array( $tracking, 'init' ) ); |
||
| 559 | } |
||
| 560 | |||
| 561 | |||
| 562 | add_filter( 'jetpack_connection_secret_generator', function( $callable ) { |
||
| 563 | return function() { |
||
| 564 | return wp_generate_password( 32, false ); |
||
| 565 | }; |
||
| 566 | } ); |
||
| 567 | |||
| 568 | $this->connection_manager = new Connection_Manager(); |
||
| 569 | $this->connection_manager->init(); |
||
| 570 | |||
| 571 | /* |
||
| 572 | * Load things that should only be in Network Admin. |
||
| 573 | * |
||
| 574 | * For now blow away everything else until a more full |
||
| 575 | * understanding of what is needed at the network level is |
||
| 576 | * available |
||
| 577 | */ |
||
| 578 | if ( is_multisite() ) { |
||
| 579 | $network = Jetpack_Network::init(); |
||
| 580 | $network->set_connection( $this->connection_manager ); |
||
| 581 | } |
||
| 582 | |||
| 583 | add_filter( |
||
| 584 | 'jetpack_signature_check_token', |
||
| 585 | array( __CLASS__, 'verify_onboarding_token' ), |
||
| 586 | 10, |
||
| 587 | 3 |
||
| 588 | ); |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Prepare Gutenberg Editor functionality |
||
| 592 | */ |
||
| 593 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php'; |
||
| 594 | Jetpack_Gutenberg::init(); |
||
| 595 | Jetpack_Gutenberg::load_independent_blocks(); |
||
| 596 | add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) ); |
||
| 597 | |||
| 598 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
| 599 | |||
| 600 | // Unlink user before deleting the user from .com |
||
| 601 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
| 602 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
| 603 | |||
| 604 | if ( Jetpack::is_active() ) { |
||
| 605 | Jetpack_Heartbeat::init(); |
||
| 606 | if ( Jetpack::is_module_active( 'stats' ) && Jetpack::is_module_active( 'search' ) ) { |
||
| 607 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php'; |
||
| 608 | Jetpack_Search_Performance_Logger::init(); |
||
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
| 613 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
| 614 | |||
| 615 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 616 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
| 617 | |||
| 618 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
| 619 | |||
| 620 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
| 621 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
| 622 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
| 623 | |||
| 624 | // returns HTTPS support status |
||
| 625 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
| 626 | |||
| 627 | // JITM AJAX callback function |
||
| 628 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
| 629 | |||
| 630 | add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) ); |
||
| 631 | |||
| 632 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
| 633 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 634 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 635 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 636 | |||
| 637 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
| 638 | |||
| 639 | /** |
||
| 640 | * These actions run checks to load additional files. |
||
| 641 | * They check for external files or plugins, so they need to run as late as possible. |
||
| 642 | */ |
||
| 643 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
| 644 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
| 645 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
| 646 | |||
| 647 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
| 648 | add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 ); |
||
| 649 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
| 650 | |||
| 651 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
| 652 | add_filter( 'profile_update', array( 'Jetpack', 'user_meta_cleanup' ) ); |
||
| 653 | |||
| 654 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
| 655 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
| 656 | |||
| 657 | // A filter to control all just in time messages |
||
| 658 | add_filter( 'jetpack_just_in_time_msgs', array( $this, 'is_active_and_not_development_mode' ), 9 ); |
||
| 659 | |||
| 660 | add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9); |
||
| 661 | |||
| 662 | // If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin. |
||
| 663 | // We should make sure to only do this for front end links. |
||
| 664 | if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) { |
||
| 665 | add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 ); |
||
| 666 | add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 ); |
||
| 667 | |||
| 668 | //we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions |
||
| 669 | //so they point moderation links on emails to Calypso |
||
| 670 | jetpack_require_lib( 'functions.wp-notify' ); |
||
| 671 | } |
||
| 672 | |||
| 673 | // Hide edit post link if mobile app. |
||
| 674 | if ( Jetpack_User_Agent_Info::is_mobile_app() ) { |
||
| 675 | add_filter( 'edit_post_link', '__return_empty_string' ); |
||
| 676 | } |
||
| 677 | |||
| 678 | // Update the Jetpack plan from API on heartbeats |
||
| 679 | add_action( 'jetpack_heartbeat', array( 'Jetpack_Plan', 'refresh_from_wpcom' ) ); |
||
| 680 | |||
| 681 | /** |
||
| 682 | * This is the hack to concatenate all css files into one. |
||
| 683 | * For description and reasoning see the implode_frontend_css method |
||
| 684 | * |
||
| 685 | * Super late priority so we catch all the registered styles |
||
| 686 | */ |
||
| 687 | if( !is_admin() ) { |
||
| 688 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
| 689 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
| 690 | } |
||
| 691 | |||
| 692 | |||
| 693 | /** |
||
| 694 | * These are sync actions that we need to keep track of for jitms |
||
| 695 | */ |
||
| 696 | add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 ); |
||
| 697 | |||
| 698 | // Actually push the stats on shutdown. |
||
| 699 | if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) { |
||
| 700 | add_action( 'shutdown', array( $this, 'push_stats' ) ); |
||
| 701 | } |
||
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Sets up the XMLRPC request handlers. |
||
| 706 | * |
||
| 707 | * @todo Deprecate this method in favor of Automattic\\Jetpack\\Connection\\Manager::setup_xmlrpc_handlers(). |
||
| 708 | * |
||
| 709 | * @param Array $request_params Incoming request parameters. |
||
| 710 | * @param Boolean $is_active Whether the connection is currently active. |
||
| 711 | * @param Boolean $is_signed Whether the signature check has been successful. |
||
| 712 | * @param Jetpack_XMLRPC_Server $xmlrpc_server (optional) An instance of the server to use instead of instantiating a new one. |
||
| 713 | */ |
||
| 714 | public function setup_xmlrpc_handlers( |
||
| 715 | $request_params, |
||
| 716 | $is_active, |
||
| 717 | $is_signed, |
||
| 718 | Jetpack_XMLRPC_Server $xmlrpc_server = null |
||
| 719 | ) { |
||
| 720 | return $this->connection_manager->setup_xmlrpc_handlers( |
||
| 721 | $request_params, |
||
| 722 | $is_active, |
||
| 723 | $is_signed, |
||
| 724 | $xmlrpc_server |
||
| 725 | ); |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Initialize REST API registration connector. |
||
| 730 | * |
||
| 731 | * @deprecated since 7.7.0 |
||
| 732 | * @see Automattic\Jetpack\Connection\Manager::initialize_rest_api_registration_connector() |
||
| 733 | */ |
||
| 734 | public function initialize_rest_api_registration_connector() { |
||
| 735 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::initialize_rest_api_registration_connector' ); |
||
| 736 | $this->connection_manager->initialize_rest_api_registration_connector(); |
||
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
| 741 | * |
||
| 742 | * @param $domains |
||
| 743 | */ |
||
| 744 | function add_wpcom_to_allowed_redirect_hosts( $domains ) { |
||
| 745 | add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_domain' ) ); |
||
| 746 | } |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Return $domains, with 'wordpress.com' appended. |
||
| 750 | * This is ported over from the manage module, which has been deprecated and baked in here. |
||
| 751 | * |
||
| 752 | * @param $domains |
||
| 753 | * @return array |
||
| 754 | */ |
||
| 755 | function allow_wpcom_domain( $domains ) { |
||
| 756 | if ( empty( $domains ) ) { |
||
| 757 | $domains = array(); |
||
| 758 | } |
||
| 759 | $domains[] = 'wordpress.com'; |
||
| 760 | return array_unique( $domains ); |
||
| 761 | } |
||
| 762 | |||
| 763 | function point_edit_post_links_to_calypso( $default_url, $post_id ) { |
||
| 764 | $post = get_post( $post_id ); |
||
| 765 | |||
| 766 | if ( empty( $post ) ) { |
||
| 767 | return $default_url; |
||
| 768 | } |
||
| 769 | |||
| 770 | $post_type = $post->post_type; |
||
| 771 | |||
| 772 | // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso. |
||
| 773 | // https://en.support.wordpress.com/custom-post-types/ |
||
| 774 | $allowed_post_types = array( |
||
| 775 | 'post' => 'post', |
||
| 776 | 'page' => 'page', |
||
| 777 | 'jetpack-portfolio' => 'edit/jetpack-portfolio', |
||
| 778 | 'jetpack-testimonial' => 'edit/jetpack-testimonial', |
||
| 779 | ); |
||
| 780 | |||
| 781 | if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) { |
||
| 782 | return $default_url; |
||
| 783 | } |
||
| 784 | |||
| 785 | $path_prefix = $allowed_post_types[ $post_type ]; |
||
| 786 | |||
| 787 | $site_slug = Jetpack::build_raw_urls( get_home_url() ); |
||
| 788 | |||
| 789 | return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) ); |
||
| 790 | } |
||
| 791 | |||
| 792 | function point_edit_comment_links_to_calypso( $url ) { |
||
| 793 | // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID. |
||
| 794 | wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args ); |
||
| 795 | return esc_url( sprintf( 'https://wordpress.com/comment/%s/%d', |
||
| 796 | Jetpack::build_raw_urls( get_home_url() ), |
||
| 797 | $query_args['amp;c'] |
||
| 798 | ) ); |
||
| 799 | } |
||
| 800 | |||
| 801 | function jetpack_track_last_sync_callback( $params ) { |
||
| 802 | /** |
||
| 803 | * Filter to turn off jitm caching |
||
| 804 | * |
||
| 805 | * @since 5.4.0 |
||
| 806 | * |
||
| 807 | * @param bool false Whether to cache just in time messages |
||
| 808 | */ |
||
| 809 | if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) { |
||
| 810 | return $params; |
||
| 811 | } |
||
| 812 | |||
| 813 | if ( is_array( $params ) && isset( $params[0] ) ) { |
||
| 814 | $option = $params[0]; |
||
| 815 | if ( 'active_plugins' === $option ) { |
||
| 816 | // use the cache if we can, but not terribly important if it gets evicted |
||
| 817 | set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS ); |
||
| 818 | } |
||
| 819 | } |
||
| 820 | |||
| 821 | return $params; |
||
| 822 | } |
||
| 823 | |||
| 824 | function jetpack_connection_banner_callback() { |
||
| 825 | check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' ); |
||
| 826 | |||
| 827 | if ( isset( $_REQUEST['dismissBanner'] ) ) { |
||
| 828 | Jetpack_Options::update_option( 'dismissed_connection_banner', 1 ); |
||
| 829 | wp_send_json_success(); |
||
| 830 | } |
||
| 831 | |||
| 832 | wp_die(); |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Removes all XML-RPC methods that are not `jetpack.*`. |
||
| 837 | * Only used in our alternate XML-RPC endpoint, where we want to |
||
| 838 | * ensure that Core and other plugins' methods are not exposed. |
||
| 839 | * |
||
| 840 | * @deprecated since 7.7.0 |
||
| 841 | * @see Automattic\Jetpack\Connection\Manager::remove_non_jetpack_xmlrpc_methods() |
||
| 842 | * |
||
| 843 | * @param array $methods A list of registered WordPress XMLRPC methods. |
||
| 844 | * @return array Filtered $methods |
||
| 845 | */ |
||
| 846 | public function remove_non_jetpack_xmlrpc_methods( $methods ) { |
||
| 847 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::remove_non_jetpack_xmlrpc_methods' ); |
||
| 848 | return $this->connection_manager->remove_non_jetpack_xmlrpc_methods( $methods ); |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Since a lot of hosts use a hammer approach to "protecting" WordPress sites, |
||
| 853 | * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive |
||
| 854 | * security/firewall policies, we provide our own alternate XML RPC API endpoint |
||
| 855 | * which is accessible via a different URI. Most of the below is copied directly |
||
| 856 | * from /xmlrpc.php so that we're replicating it as closely as possible. |
||
| 857 | * |
||
| 858 | * @deprecated since 7.7.0 |
||
| 859 | * @see Automattic\Jetpack\Connection\Manager::alternate_xmlrpc() |
||
| 860 | */ |
||
| 861 | public function alternate_xmlrpc() { |
||
| 862 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::alternate_xmlrpc' ); |
||
| 863 | $this->connection_manager->alternate_xmlrpc(); |
||
| 864 | } |
||
| 865 | |||
| 866 | /** |
||
| 867 | * The callback for the JITM ajax requests. |
||
| 868 | */ |
||
| 869 | function jetpack_jitm_ajax_callback() { |
||
| 870 | // Check for nonce |
||
| 871 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
| 872 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
| 873 | } |
||
| 874 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
| 875 | $module_slug = $_REQUEST['jitmModule']; |
||
| 876 | Jetpack::log( 'activate', $module_slug ); |
||
| 877 | Jetpack::activate_module( $module_slug, false, false ); |
||
| 878 | Jetpack::state( 'message', 'no_message' ); |
||
| 879 | |||
| 880 | //A Jetpack module is being activated through a JITM, track it |
||
| 881 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
| 882 | $this->do_stats( 'server_side' ); |
||
| 883 | |||
| 884 | wp_send_json_success(); |
||
| 885 | } |
||
| 886 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
| 887 | // get the hide_jitm options array |
||
| 888 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
| 889 | $module_slug = $_REQUEST['jitmModule']; |
||
| 890 | |||
| 891 | if( ! $jetpack_hide_jitm ) { |
||
| 892 | $jetpack_hide_jitm = array( |
||
| 893 | $module_slug => 'hide' |
||
| 894 | ); |
||
| 895 | } else { |
||
| 896 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
| 897 | } |
||
| 898 | |||
| 899 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
| 900 | |||
| 901 | //jitm is being dismissed forever, track it |
||
| 902 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
| 903 | $this->do_stats( 'server_side' ); |
||
| 904 | |||
| 905 | wp_send_json_success(); |
||
| 906 | } |
||
| 907 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
| 908 | $module_slug = $_REQUEST['jitmModule']; |
||
| 909 | |||
| 910 | // User went to WordPress.com, track this |
||
| 911 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
| 912 | $this->do_stats( 'server_side' ); |
||
| 913 | |||
| 914 | wp_send_json_success(); |
||
| 915 | } |
||
| 916 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
| 917 | $track = $_REQUEST['jitmModule']; |
||
| 918 | |||
| 919 | // User is viewing JITM, track it. |
||
| 920 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
| 921 | $this->do_stats( 'server_side' ); |
||
| 922 | |||
| 923 | wp_send_json_success(); |
||
| 924 | } |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
| 929 | */ |
||
| 930 | function push_stats() { |
||
| 931 | if ( ! empty( $this->stats ) ) { |
||
| 932 | $this->do_stats( 'server_side' ); |
||
| 933 | } |
||
| 934 | } |
||
| 935 | |||
| 936 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
| 937 | switch( $cap ) { |
||
| 938 | case 'jetpack_connect' : |
||
| 939 | case 'jetpack_reconnect' : |
||
| 940 | if ( Jetpack::is_development_mode() ) { |
||
| 941 | $caps = array( 'do_not_allow' ); |
||
| 942 | break; |
||
| 943 | } |
||
| 944 | /** |
||
| 945 | * Pass through. If it's not development mode, these should match disconnect. |
||
| 946 | * Let users disconnect if it's development mode, just in case things glitch. |
||
| 947 | */ |
||
| 948 | case 'jetpack_disconnect' : |
||
| 949 | /** |
||
| 950 | * In multisite, can individual site admins manage their own connection? |
||
| 951 | * |
||
| 952 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
| 953 | */ |
||
| 954 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
| 955 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
| 956 | /** |
||
| 957 | * We need to update the option name -- it's terribly unclear which |
||
| 958 | * direction the override goes. |
||
| 959 | * |
||
| 960 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
| 961 | */ |
||
| 962 | $caps = array( 'do_not_allow' ); |
||
| 963 | break; |
||
| 964 | } |
||
| 965 | } |
||
| 966 | |||
| 967 | $caps = array( 'manage_options' ); |
||
| 968 | break; |
||
| 969 | case 'jetpack_manage_modules' : |
||
| 970 | case 'jetpack_activate_modules' : |
||
| 971 | case 'jetpack_deactivate_modules' : |
||
| 972 | $caps = array( 'manage_options' ); |
||
| 973 | break; |
||
| 974 | case 'jetpack_configure_modules' : |
||
| 975 | $caps = array( 'manage_options' ); |
||
| 976 | break; |
||
| 977 | case 'jetpack_manage_autoupdates' : |
||
| 978 | $caps = array( |
||
| 979 | 'manage_options', |
||
| 980 | 'update_plugins', |
||
| 981 | ); |
||
| 982 | break; |
||
| 983 | case 'jetpack_network_admin_page': |
||
| 984 | case 'jetpack_network_settings_page': |
||
| 985 | $caps = array( 'manage_network_plugins' ); |
||
| 986 | break; |
||
| 987 | case 'jetpack_network_sites_page': |
||
| 988 | $caps = array( 'manage_sites' ); |
||
| 989 | break; |
||
| 990 | case 'jetpack_admin_page' : |
||
| 991 | if ( Jetpack::is_development_mode() ) { |
||
| 992 | $caps = array( 'manage_options' ); |
||
| 993 | break; |
||
| 994 | } else { |
||
| 995 | $caps = array( 'read' ); |
||
| 996 | } |
||
| 997 | break; |
||
| 998 | case 'jetpack_connect_user' : |
||
| 999 | if ( Jetpack::is_development_mode() ) { |
||
| 1000 | $caps = array( 'do_not_allow' ); |
||
| 1001 | break; |
||
| 1002 | } |
||
| 1003 | $caps = array( 'read' ); |
||
| 1004 | break; |
||
| 1005 | } |
||
| 1006 | return $caps; |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Require a Jetpack authentication. |
||
| 1011 | * |
||
| 1012 | * @deprecated since 7.7.0 |
||
| 1013 | * @see Automattic\Jetpack\Connection\Manager::require_jetpack_authentication() |
||
| 1014 | */ |
||
| 1015 | public function require_jetpack_authentication() { |
||
| 1016 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::require_jetpack_authentication' ); |
||
| 1017 | $this->connection_manager->require_jetpack_authentication(); |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Load language files |
||
| 1022 | * @action plugins_loaded |
||
| 1023 | */ |
||
| 1024 | public static function plugin_textdomain() { |
||
| 1025 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
| 1026 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Register assets for use in various modules and the Jetpack admin page. |
||
| 1031 | * |
||
| 1032 | * @uses wp_script_is, wp_register_script, plugins_url |
||
| 1033 | * @action wp_loaded |
||
| 1034 | * @return null |
||
| 1035 | */ |
||
| 1036 | public function register_assets() { |
||
| 1037 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
| 1038 | wp_register_script( |
||
| 1039 | 'spin', |
||
| 1040 | Assets::get_file_url_for_environment( '_inc/build/spin.min.js', '_inc/spin.js' ), |
||
| 1041 | false, |
||
| 1042 | '1.3' |
||
| 1043 | ); |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | View Code Duplication | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
|
| 1047 | wp_register_script( |
||
| 1048 | 'jquery.spin', |
||
| 1049 | Assets::get_file_url_for_environment( '_inc/build/jquery.spin.min.js', '_inc/jquery.spin.js' ), |
||
| 1050 | array( 'jquery', 'spin' ), |
||
| 1051 | '1.3' |
||
| 1052 | ); |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
| 1056 | wp_register_script( |
||
| 1057 | 'jetpack-gallery-settings', |
||
| 1058 | Assets::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ), |
||
| 1059 | array( 'media-views' ), |
||
| 1060 | '20121225' |
||
| 1061 | ); |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | View Code Duplication | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
|
| 1065 | wp_register_script( |
||
| 1066 | 'jetpack-twitter-timeline', |
||
| 1067 | Assets::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ), |
||
| 1068 | array( 'jquery' ), |
||
| 1069 | '4.0.0', |
||
| 1070 | true |
||
| 1071 | ); |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
| 1075 | wp_register_script( |
||
| 1076 | 'jetpack-facebook-embed', |
||
| 1077 | Assets::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ), |
||
| 1078 | array( 'jquery' ), |
||
| 1079 | null, |
||
| 1080 | true |
||
| 1081 | ); |
||
| 1082 | |||
| 1083 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
| 1084 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
| 1085 | if ( ! is_numeric( $fb_app_id ) ) { |
||
| 1086 | $fb_app_id = ''; |
||
| 1087 | } |
||
| 1088 | wp_localize_script( |
||
| 1089 | 'jetpack-facebook-embed', |
||
| 1090 | 'jpfbembed', |
||
| 1091 | array( |
||
| 1092 | 'appid' => $fb_app_id, |
||
| 1093 | 'locale' => $this->get_locale(), |
||
| 1094 | ) |
||
| 1095 | ); |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | /** |
||
| 1099 | * As jetpack_register_genericons is by default fired off a hook, |
||
| 1100 | * the hook may have already fired by this point. |
||
| 1101 | * So, let's just trigger it manually. |
||
| 1102 | */ |
||
| 1103 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
| 1104 | jetpack_register_genericons(); |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Register the social logos |
||
| 1108 | */ |
||
| 1109 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
| 1110 | jetpack_register_social_logos(); |
||
| 1111 | |||
| 1112 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
| 1113 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Guess locale from language code. |
||
| 1118 | * |
||
| 1119 | * @param string $lang Language code. |
||
| 1120 | * @return string|bool |
||
| 1121 | */ |
||
| 1122 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
| 1123 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
| 1124 | return 'en_US'; |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
| 1128 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 1129 | return false; |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 1136 | // WP.com: get_locale() returns 'it' |
||
| 1137 | $locale = GP_Locales::by_slug( $lang ); |
||
| 1138 | } else { |
||
| 1139 | // Jetpack: get_locale() returns 'it_IT'; |
||
| 1140 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | if ( ! $locale ) { |
||
| 1144 | return false; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | if ( empty( $locale->facebook_locale ) ) { |
||
| 1148 | if ( empty( $locale->wp_locale ) ) { |
||
| 1149 | return false; |
||
| 1150 | } else { |
||
| 1151 | // Facebook SDK is smart enough to fall back to en_US if a |
||
| 1152 | // locale isn't supported. Since supported Facebook locales |
||
| 1153 | // can fall out of sync, we'll attempt to use the known |
||
| 1154 | // wp_locale value and rely on said fallback. |
||
| 1155 | return $locale->wp_locale; |
||
| 1156 | } |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | return $locale->facebook_locale; |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Get the locale. |
||
| 1164 | * |
||
| 1165 | * @return string|bool |
||
| 1166 | */ |
||
| 1167 | function get_locale() { |
||
| 1168 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
| 1169 | |||
| 1170 | if ( ! $locale ) { |
||
| 1171 | $locale = 'en_US'; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | return $locale; |
||
| 1175 | } |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * Device Pixels support |
||
| 1179 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
| 1180 | */ |
||
| 1181 | function devicepx() { |
||
| 1182 | if ( Jetpack::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) { |
||
| 1183 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
| 1184 | } |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
| 1189 | * @param bool $option |
||
| 1190 | * @return string |
||
| 1191 | */ |
||
| 1192 | public function jetpack_main_network_site_option( $option ) { |
||
| 1193 | return network_site_url(); |
||
| 1194 | } |
||
| 1195 | /** |
||
| 1196 | * Network Name. |
||
| 1197 | */ |
||
| 1198 | static function network_name( $option = null ) { |
||
| 1199 | global $current_site; |
||
| 1200 | return $current_site->site_name; |
||
| 1201 | } |
||
| 1202 | /** |
||
| 1203 | * Does the network allow new user and site registrations. |
||
| 1204 | * @return string |
||
| 1205 | */ |
||
| 1206 | static function network_allow_new_registrations( $option = null ) { |
||
| 1207 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
| 1208 | } |
||
| 1209 | /** |
||
| 1210 | * Does the network allow admins to add new users. |
||
| 1211 | * @return boolian |
||
| 1212 | */ |
||
| 1213 | static function network_add_new_users( $option = null ) { |
||
| 1214 | return (bool) get_site_option( 'add_new_users' ); |
||
| 1215 | } |
||
| 1216 | /** |
||
| 1217 | * File upload psace left per site in MB. |
||
| 1218 | * -1 means NO LIMIT. |
||
| 1219 | * @return number |
||
| 1220 | */ |
||
| 1221 | static function network_site_upload_space( $option = null ) { |
||
| 1222 | // value in MB |
||
| 1223 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
| 1224 | } |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Network allowed file types. |
||
| 1228 | * @return string |
||
| 1229 | */ |
||
| 1230 | static function network_upload_file_types( $option = null ) { |
||
| 1231 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Maximum file upload size set by the network. |
||
| 1236 | * @return number |
||
| 1237 | */ |
||
| 1238 | static function network_max_upload_file_size( $option = null ) { |
||
| 1239 | // value in KB |
||
| 1240 | return get_site_option( 'fileupload_maxk', 300 ); |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Lets us know if a site allows admins to manage the network. |
||
| 1245 | * @return array |
||
| 1246 | */ |
||
| 1247 | static function network_enable_administration_menus( $option = null ) { |
||
| 1248 | return get_site_option( 'menu_items' ); |
||
| 1249 | } |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
| 1253 | * jetpack_other_linked_admins transient. |
||
| 1254 | * |
||
| 1255 | * @since 4.3.2 |
||
| 1256 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
| 1257 | * |
||
| 1258 | * @param int $user_id The user ID whose role changed. |
||
| 1259 | * @param string $role The new role. |
||
| 1260 | * @param array $old_roles An array of the user's previous roles. |
||
| 1261 | */ |
||
| 1262 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
| 1263 | if ( 'administrator' == $role |
||
| 1264 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
| 1265 | || is_null( $old_roles ) |
||
| 1266 | ) { |
||
| 1267 | delete_transient( 'jetpack_other_linked_admins' ); |
||
| 1268 | } |
||
| 1269 | } |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Checks to see if there are any other users available to become primary |
||
| 1273 | * Users must both: |
||
| 1274 | * - Be linked to wpcom |
||
| 1275 | * - Be an admin |
||
| 1276 | * |
||
| 1277 | * @return mixed False if no other users are linked, Int if there are. |
||
| 1278 | */ |
||
| 1279 | static function get_other_linked_admins() { |
||
| 1280 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
| 1281 | |||
| 1282 | if ( false === $other_linked_users ) { |
||
| 1283 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
| 1284 | if ( count( $admins ) > 1 ) { |
||
| 1285 | $available = array(); |
||
| 1286 | foreach ( $admins as $admin ) { |
||
| 1287 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
| 1288 | $available[] = $admin->ID; |
||
| 1289 | } |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | $count_connected_admins = count( $available ); |
||
| 1293 | if ( count( $available ) > 1 ) { |
||
| 1294 | $other_linked_users = $count_connected_admins; |
||
| 1295 | } else { |
||
| 1296 | $other_linked_users = 0; |
||
| 1297 | } |
||
| 1298 | } else { |
||
| 1299 | $other_linked_users = 0; |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
| 1306 | } |
||
| 1307 | |||
| 1308 | /** |
||
| 1309 | * Return whether we are dealing with a multi network setup or not. |
||
| 1310 | * The reason we are type casting this is because we want to avoid the situation where |
||
| 1311 | * the result is false since when is_main_network_option return false it cases |
||
| 1312 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
| 1313 | * database which could be set to anything as opposed to what this function returns. |
||
| 1314 | * @param bool $option |
||
| 1315 | * |
||
| 1316 | * @return boolean |
||
| 1317 | */ |
||
| 1318 | public function is_main_network_option( $option ) { |
||
| 1319 | // return '1' or '' |
||
| 1320 | return (string) (bool) Jetpack::is_multi_network(); |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
| 1325 | * |
||
| 1326 | * @param string $option |
||
| 1327 | * @return boolean |
||
| 1328 | */ |
||
| 1329 | public function is_multisite( $option ) { |
||
| 1330 | return (string) (bool) is_multisite(); |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * Implemented since there is no core is multi network function |
||
| 1335 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
| 1336 | * |
||
| 1337 | * @since 3.3 |
||
| 1338 | * @return boolean |
||
| 1339 | */ |
||
| 1340 | View Code Duplication | public static function is_multi_network() { |
|
| 1341 | global $wpdb; |
||
| 1342 | |||
| 1343 | // if we don't have a multi site setup no need to do any more |
||
| 1344 | if ( ! is_multisite() ) { |
||
| 1345 | return false; |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
| 1349 | if ( $num_sites > 1 ) { |
||
| 1350 | return true; |
||
| 1351 | } else { |
||
| 1352 | return false; |
||
| 1353 | } |
||
| 1354 | } |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
| 1358 | * @return null |
||
| 1359 | */ |
||
| 1360 | function update_jetpack_main_network_site_option() { |
||
| 1361 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1362 | } |
||
| 1363 | /** |
||
| 1364 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
| 1365 | * |
||
| 1366 | */ |
||
| 1367 | function update_jetpack_network_settings() { |
||
| 1368 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1369 | // Only sync this info for the main network site. |
||
| 1370 | } |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Get back if the current site is single user site. |
||
| 1374 | * |
||
| 1375 | * @return bool |
||
| 1376 | */ |
||
| 1377 | View Code Duplication | public static function is_single_user_site() { |
|
| 1378 | global $wpdb; |
||
| 1379 | |||
| 1380 | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
||
| 1381 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
| 1382 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
| 1383 | } |
||
| 1384 | return 1 === (int) $some_users; |
||
| 1385 | } |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Returns true if the site has file write access false otherwise. |
||
| 1389 | * @return string ( '1' | '0' ) |
||
| 1390 | **/ |
||
| 1391 | public static function file_system_write_access() { |
||
| 1392 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 1393 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
| 1394 | } |
||
| 1395 | |||
| 1396 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
| 1397 | |||
| 1398 | $filesystem_method = get_filesystem_method(); |
||
| 1399 | if ( $filesystem_method === 'direct' ) { |
||
| 1400 | return 1; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | ob_start(); |
||
| 1404 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 1405 | ob_end_clean(); |
||
| 1406 | if ( $filesystem_credentials_are_stored ) { |
||
| 1407 | return 1; |
||
| 1408 | } |
||
| 1409 | return 0; |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Finds out if a site is using a version control system. |
||
| 1414 | * @return string ( '1' | '0' ) |
||
| 1415 | **/ |
||
| 1416 | public static function is_version_controlled() { |
||
| 1417 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Functions::is_version_controlled' ); |
||
| 1418 | return (string) (int) Functions::is_version_controlled(); |
||
| 1419 | } |
||
| 1420 | |||
| 1421 | /** |
||
| 1422 | * Determines whether the current theme supports featured images or not. |
||
| 1423 | * @return string ( '1' | '0' ) |
||
| 1424 | */ |
||
| 1425 | public static function featured_images_enabled() { |
||
| 1426 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1427 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
| 1428 | } |
||
| 1429 | |||
| 1430 | /** |
||
| 1431 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
| 1432 | * |
||
| 1433 | * @deprecated 4.7 use get_avatar_url instead. |
||
| 1434 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
| 1435 | * @param int $size Size of the avatar image |
||
| 1436 | * @param string $default URL to a default image to use if no avatar is available |
||
| 1437 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
| 1438 | * |
||
| 1439 | * @return array |
||
| 1440 | */ |
||
| 1441 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
| 1442 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
| 1443 | return get_avatar_url( $id_or_email, array( |
||
| 1444 | 'size' => $size, |
||
| 1445 | 'default' => $default, |
||
| 1446 | 'force_default' => $force_display, |
||
| 1447 | ) ); |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | /** |
||
| 1451 | * jetpack_updates is saved in the following schema: |
||
| 1452 | * |
||
| 1453 | * array ( |
||
| 1454 | * 'plugins' => (int) Number of plugin updates available. |
||
| 1455 | * 'themes' => (int) Number of theme updates available. |
||
| 1456 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
| 1457 | * 'translations' => (int) Number of translation updates available. |
||
| 1458 | * 'total' => (int) Total of all available updates. |
||
| 1459 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
| 1460 | * ) |
||
| 1461 | * @return array |
||
| 1462 | */ |
||
| 1463 | public static function get_updates() { |
||
| 1464 | $update_data = wp_get_update_data(); |
||
| 1465 | |||
| 1466 | // Stores the individual update counts as well as the total count. |
||
| 1467 | if ( isset( $update_data['counts'] ) ) { |
||
| 1468 | $updates = $update_data['counts']; |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | // If we need to update WordPress core, let's find the latest version number. |
||
| 1472 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
| 1473 | $cur = get_preferred_from_update_core(); |
||
| 1474 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
| 1475 | $updates['wp_update_version'] = $cur->current; |
||
| 1476 | } |
||
| 1477 | } |
||
| 1478 | return isset( $updates ) ? $updates : array(); |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | public static function get_update_details() { |
||
| 1482 | $update_details = array( |
||
| 1483 | 'update_core' => get_site_transient( 'update_core' ), |
||
| 1484 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
| 1485 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
| 1486 | ); |
||
| 1487 | return $update_details; |
||
| 1488 | } |
||
| 1489 | |||
| 1490 | public static function refresh_update_data() { |
||
| 1491 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1492 | |||
| 1493 | } |
||
| 1494 | |||
| 1495 | public static function refresh_theme_data() { |
||
| 1496 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1497 | } |
||
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Is Jetpack active? |
||
| 1501 | */ |
||
| 1502 | public static function is_active() { |
||
| 1503 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1504 | } |
||
| 1505 | |||
| 1506 | /** |
||
| 1507 | * Make an API call to WordPress.com for plan status |
||
| 1508 | * |
||
| 1509 | * @deprecated 7.2.0 Use Jetpack_Plan::refresh_from_wpcom. |
||
| 1510 | * |
||
| 1511 | * @return bool True if plan is updated, false if no update |
||
| 1512 | */ |
||
| 1513 | public static function refresh_active_plan_from_wpcom() { |
||
| 1514 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::refresh_from_wpcom' ); |
||
| 1515 | return Jetpack_Plan::refresh_from_wpcom(); |
||
| 1516 | } |
||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * Get the plan that this Jetpack site is currently using |
||
| 1520 | * |
||
| 1521 | * @deprecated 7.2.0 Use Jetpack_Plan::get. |
||
| 1522 | * @return array Active Jetpack plan details. |
||
| 1523 | */ |
||
| 1524 | public static function get_active_plan() { |
||
| 1525 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::get' ); |
||
| 1526 | return Jetpack_Plan::get(); |
||
| 1527 | } |
||
| 1528 | |||
| 1529 | /** |
||
| 1530 | * Determine whether the active plan supports a particular feature |
||
| 1531 | * |
||
| 1532 | * @deprecated 7.2.0 Use Jetpack_Plan::supports. |
||
| 1533 | * @return bool True if plan supports feature, false if not. |
||
| 1534 | */ |
||
| 1535 | public static function active_plan_supports( $feature ) { |
||
| 1536 | _deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::supports' ); |
||
| 1537 | return Jetpack_Plan::supports( $feature ); |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | /** |
||
| 1541 | * Is Jetpack in development (offline) mode? |
||
| 1542 | */ |
||
| 1543 | View Code Duplication | public static function is_development_mode() { |
|
| 1544 | $development_mode = false; |
||
| 1545 | |||
| 1546 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
| 1547 | $development_mode = JETPACK_DEV_DEBUG; |
||
| 1548 | } elseif ( $site_url = site_url() ) { |
||
| 1549 | $development_mode = false === strpos( $site_url, '.' ); |
||
| 1550 | } |
||
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Filters Jetpack's development mode. |
||
| 1554 | * |
||
| 1555 | * @see https://jetpack.com/support/development-mode/ |
||
| 1556 | * |
||
| 1557 | * @since 2.2.1 |
||
| 1558 | * |
||
| 1559 | * @param bool $development_mode Is Jetpack's development mode active. |
||
| 1560 | */ |
||
| 1561 | $development_mode = ( bool ) apply_filters( 'jetpack_development_mode', $development_mode ); |
||
| 1562 | return $development_mode; |
||
| 1563 | } |
||
| 1564 | |||
| 1565 | /** |
||
| 1566 | * Whether the site is currently onboarding or not. |
||
| 1567 | * A site is considered as being onboarded if it currently has an onboarding token. |
||
| 1568 | * |
||
| 1569 | * @since 5.8 |
||
| 1570 | * |
||
| 1571 | * @access public |
||
| 1572 | * @static |
||
| 1573 | * |
||
| 1574 | * @return bool True if the site is currently onboarding, false otherwise |
||
| 1575 | */ |
||
| 1576 | public static function is_onboarding() { |
||
| 1577 | return Jetpack_Options::get_option( 'onboarding' ) !== false; |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Determines reason for Jetpack development mode. |
||
| 1582 | */ |
||
| 1583 | public static function development_mode_trigger_text() { |
||
| 1584 | if ( ! Jetpack::is_development_mode() ) { |
||
| 1585 | return __( 'Jetpack is not in Development Mode.', 'jetpack' ); |
||
| 1586 | } |
||
| 1587 | |||
| 1588 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
| 1589 | $notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' ); |
||
| 1590 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
| 1591 | $notice = __( 'The site URL lacking a dot (e.g. http://localhost).', 'jetpack' ); |
||
| 1592 | } else { |
||
| 1593 | $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' ); |
||
| 1594 | } |
||
| 1595 | |||
| 1596 | return $notice; |
||
| 1597 | |||
| 1598 | } |
||
| 1599 | /** |
||
| 1600 | * Get Jetpack development mode notice text and notice class. |
||
| 1601 | * |
||
| 1602 | * Mirrors the checks made in Jetpack::is_development_mode |
||
| 1603 | * |
||
| 1604 | */ |
||
| 1605 | public static function show_development_mode_notice() { |
||
| 1606 | View Code Duplication | if ( Jetpack::is_development_mode() ) { |
|
| 1607 | $notice = sprintf( |
||
| 1608 | /* translators: %s is a URL */ |
||
| 1609 | __( 'In <a href="%s" target="_blank">Development Mode</a>:', 'jetpack' ), |
||
| 1610 | 'https://jetpack.com/support/development-mode/' |
||
| 1611 | ); |
||
| 1612 | |||
| 1613 | $notice .= ' ' . Jetpack::development_mode_trigger_text(); |
||
| 1614 | |||
| 1615 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1616 | } |
||
| 1617 | |||
| 1618 | // Throw up a notice if using a development version and as for feedback. |
||
| 1619 | if ( Jetpack::is_development_version() ) { |
||
| 1620 | /* translators: %s is a URL */ |
||
| 1621 | $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/' ); |
||
| 1622 | |||
| 1623 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1624 | } |
||
| 1625 | // Throw up a notice if using staging mode |
||
| 1626 | if ( Jetpack::is_staging_site() ) { |
||
| 1627 | /* translators: %s is a URL */ |
||
| 1628 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
| 1629 | |||
| 1630 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1631 | } |
||
| 1632 | } |
||
| 1633 | |||
| 1634 | /** |
||
| 1635 | * Whether Jetpack's version maps to a public release, or a development version. |
||
| 1636 | */ |
||
| 1637 | public static function is_development_version() { |
||
| 1638 | /** |
||
| 1639 | * Allows filtering whether this is a development version of Jetpack. |
||
| 1640 | * |
||
| 1641 | * This filter is especially useful for tests. |
||
| 1642 | * |
||
| 1643 | * @since 4.3.0 |
||
| 1644 | * |
||
| 1645 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
| 1646 | */ |
||
| 1647 | return (bool) apply_filters( |
||
| 1648 | 'jetpack_development_version', |
||
| 1649 | ! preg_match( '/^\d+(\.\d+)+$/', Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
| 1650 | ); |
||
| 1651 | } |
||
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
| 1655 | */ |
||
| 1656 | public static function is_user_connected( $user_id = false ) { |
||
| 1657 | return self::connection()->is_user_connected( $user_id ); |
||
| 1658 | } |
||
| 1659 | |||
| 1660 | /** |
||
| 1661 | * Get the wpcom user data of the current|specified connected user. |
||
| 1662 | */ |
||
| 1663 | View Code Duplication | public static function get_connected_user_data( $user_id = null ) { |
|
| 1664 | // TODO: remove in favor of Connection_Manager->get_connected_user_data |
||
| 1665 | if ( ! $user_id ) { |
||
| 1666 | $user_id = get_current_user_id(); |
||
| 1667 | } |
||
| 1668 | |||
| 1669 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 1670 | |||
| 1671 | if ( $cached_user_data = get_transient( $transient_key ) ) { |
||
| 1672 | return $cached_user_data; |
||
| 1673 | } |
||
| 1674 | |||
| 1675 | Jetpack::load_xml_rpc_client(); |
||
| 1676 | $xml = new Jetpack_IXR_Client( array( |
||
| 1677 | 'user_id' => $user_id, |
||
| 1678 | ) ); |
||
| 1679 | $xml->query( 'wpcom.getUser' ); |
||
| 1680 | if ( ! $xml->isError() ) { |
||
| 1681 | $user_data = $xml->getResponse(); |
||
| 1682 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
| 1683 | return $user_data; |
||
| 1684 | } |
||
| 1685 | |||
| 1686 | return false; |
||
| 1687 | } |
||
| 1688 | |||
| 1689 | /** |
||
| 1690 | * Get the wpcom email of the current|specified connected user. |
||
| 1691 | */ |
||
| 1692 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
| 1693 | if ( ! $user_id ) { |
||
| 1694 | $user_id = get_current_user_id(); |
||
| 1695 | } |
||
| 1696 | Jetpack::load_xml_rpc_client(); |
||
| 1697 | $xml = new Jetpack_IXR_Client( array( |
||
| 1698 | 'user_id' => $user_id, |
||
| 1699 | ) ); |
||
| 1700 | $xml->query( 'wpcom.getUserEmail' ); |
||
| 1701 | if ( ! $xml->isError() ) { |
||
| 1702 | return $xml->getResponse(); |
||
| 1703 | } |
||
| 1704 | return false; |
||
| 1705 | } |
||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Get the wpcom email of the master user. |
||
| 1709 | */ |
||
| 1710 | public static function get_master_user_email() { |
||
| 1711 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
| 1712 | if ( $master_user_id ) { |
||
| 1713 | return self::get_connected_user_email( $master_user_id ); |
||
| 1714 | } |
||
| 1715 | return ''; |
||
| 1716 | } |
||
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Whether the current user is the connection owner. |
||
| 1720 | * |
||
| 1721 | * @deprecated since 7.7 |
||
| 1722 | * |
||
| 1723 | * @return bool Whether the current user is the connection owner. |
||
| 1724 | */ |
||
| 1725 | public function current_user_is_connection_owner() { |
||
| 1726 | _deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::is_connection_owner' ); |
||
| 1727 | return self::connection()->is_connection_owner(); |
||
| 1728 | } |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Gets current user IP address. |
||
| 1732 | * |
||
| 1733 | * @param bool $check_all_headers Check all headers? Default is `false`. |
||
| 1734 | * |
||
| 1735 | * @return string Current user IP address. |
||
| 1736 | */ |
||
| 1737 | public static function current_user_ip( $check_all_headers = false ) { |
||
| 1738 | if ( $check_all_headers ) { |
||
| 1739 | foreach ( array( |
||
| 1740 | 'HTTP_CF_CONNECTING_IP', |
||
| 1741 | 'HTTP_CLIENT_IP', |
||
| 1742 | 'HTTP_X_FORWARDED_FOR', |
||
| 1743 | 'HTTP_X_FORWARDED', |
||
| 1744 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
| 1745 | 'HTTP_FORWARDED_FOR', |
||
| 1746 | 'HTTP_FORWARDED', |
||
| 1747 | 'HTTP_VIA', |
||
| 1748 | ) as $key ) { |
||
| 1749 | if ( ! empty( $_SERVER[ $key ] ) ) { |
||
| 1750 | return $_SERVER[ $key ]; |
||
| 1751 | } |
||
| 1752 | } |
||
| 1753 | } |
||
| 1754 | |||
| 1755 | return ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
||
| 1756 | } |
||
| 1757 | |||
| 1758 | /** |
||
| 1759 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
| 1760 | */ |
||
| 1761 | function extra_oembed_providers() { |
||
| 1762 | // Cloudup: https://dev.cloudup.com/#oembed |
||
| 1763 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
| 1764 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
| 1765 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
| 1766 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
| 1767 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
| 1768 | wp_oembed_add_provider( '#https?://(www\.)?icloud\.com/keynote/.*#i', 'https://iwmb.icloud.com/iwmb/oembed', true ); |
||
| 1769 | wp_oembed_add_provider( 'https://song.link/*', 'https://song.link/oembed', false ); |
||
| 1770 | } |
||
| 1771 | |||
| 1772 | /** |
||
| 1773 | * Synchronize connected user role changes |
||
| 1774 | */ |
||
| 1775 | function user_role_change( $user_id ) { |
||
| 1776 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Users::user_role_change()' ); |
||
| 1777 | Users::user_role_change( $user_id ); |
||
| 1778 | } |
||
| 1779 | |||
| 1780 | /** |
||
| 1781 | * Loads the currently active modules. |
||
| 1782 | */ |
||
| 1783 | public static function load_modules() { |
||
| 1784 | if ( |
||
| 1785 | ! self::is_active() |
||
| 1786 | && ! self::is_development_mode() |
||
| 1787 | && ! self::is_onboarding() |
||
| 1788 | && ( |
||
| 1789 | ! is_multisite() |
||
| 1790 | || ! get_site_option( 'jetpack_protect_active' ) |
||
| 1791 | ) |
||
| 1792 | ) { |
||
| 1793 | return; |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | $version = Jetpack_Options::get_option( 'version' ); |
||
| 1797 | View Code Duplication | if ( ! $version ) { |
|
| 1798 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
| 1799 | /** This action is documented in class.jetpack.php */ |
||
| 1800 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 1801 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 1802 | } |
||
| 1803 | list( $version ) = explode( ':', $version ); |
||
| 1804 | |||
| 1805 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
| 1806 | |||
| 1807 | $modules_data = array(); |
||
| 1808 | |||
| 1809 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
| 1810 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
| 1811 | $updated_modules = array(); |
||
| 1812 | foreach ( $modules as $module ) { |
||
| 1813 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1814 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
| 1815 | continue; |
||
| 1816 | } |
||
| 1817 | |||
| 1818 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
| 1819 | continue; |
||
| 1820 | } |
||
| 1821 | |||
| 1822 | $updated_modules[] = $module; |
||
| 1823 | } |
||
| 1824 | |||
| 1825 | $modules = array_diff( $modules, $updated_modules ); |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | $is_development_mode = Jetpack::is_development_mode(); |
||
| 1829 | |||
| 1830 | foreach ( $modules as $index => $module ) { |
||
| 1831 | // If we're in dev mode, disable modules requiring a connection |
||
| 1832 | if ( $is_development_mode ) { |
||
| 1833 | // Prime the pump if we need to |
||
| 1834 | if ( empty( $modules_data[ $module ] ) ) { |
||
| 1835 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1836 | } |
||
| 1837 | // If the module requires a connection, but we're in local mode, don't include it. |
||
| 1838 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
| 1839 | continue; |
||
| 1840 | } |
||
| 1841 | } |
||
| 1842 | |||
| 1843 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
| 1844 | continue; |
||
| 1845 | } |
||
| 1846 | |||
| 1847 | if ( ! include_once( Jetpack::get_module_path( $module ) ) ) { |
||
| 1848 | unset( $modules[ $index ] ); |
||
| 1849 | self::update_active_modules( array_values( $modules ) ); |
||
| 1850 | continue; |
||
| 1851 | } |
||
| 1852 | |||
| 1853 | /** |
||
| 1854 | * Fires when a specific module is loaded. |
||
| 1855 | * The dynamic part of the hook, $module, is the module slug. |
||
| 1856 | * |
||
| 1857 | * @since 1.1.0 |
||
| 1858 | */ |
||
| 1859 | do_action( 'jetpack_module_loaded_' . $module ); |
||
| 1860 | } |
||
| 1861 | |||
| 1862 | /** |
||
| 1863 | * Fires when all the modules are loaded. |
||
| 1864 | * |
||
| 1865 | * @since 1.1.0 |
||
| 1866 | */ |
||
| 1867 | do_action( 'jetpack_modules_loaded' ); |
||
| 1868 | |||
| 1869 | // 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. |
||
| 1870 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
| 1871 | } |
||
| 1872 | |||
| 1873 | /** |
||
| 1874 | * Check if Jetpack's REST API compat file should be included |
||
| 1875 | * @action plugins_loaded |
||
| 1876 | * @return null |
||
| 1877 | */ |
||
| 1878 | public function check_rest_api_compat() { |
||
| 1879 | /** |
||
| 1880 | * Filters the list of REST API compat files to be included. |
||
| 1881 | * |
||
| 1882 | * @since 2.2.5 |
||
| 1883 | * |
||
| 1884 | * @param array $args Array of REST API compat files to include. |
||
| 1885 | */ |
||
| 1886 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
| 1887 | |||
| 1888 | if ( function_exists( 'bbpress' ) ) |
||
| 1889 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
| 1890 | |||
| 1891 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
| 1892 | require_once $_jetpack_rest_api_compat_include; |
||
| 1893 | } |
||
| 1894 | |||
| 1895 | /** |
||
| 1896 | * Gets all plugins currently active in values, regardless of whether they're |
||
| 1897 | * traditionally activated or network activated. |
||
| 1898 | * |
||
| 1899 | * @todo Store the result in core's object cache maybe? |
||
| 1900 | */ |
||
| 1901 | public static function get_active_plugins() { |
||
| 1902 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
| 1903 | |||
| 1904 | if ( is_multisite() ) { |
||
| 1905 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
| 1906 | // whereas active_plugins stores them in the values. |
||
| 1907 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
| 1908 | if ( $network_plugins ) { |
||
| 1909 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
| 1910 | } |
||
| 1911 | } |
||
| 1912 | |||
| 1913 | sort( $active_plugins ); |
||
| 1914 | |||
| 1915 | return array_unique( $active_plugins ); |
||
| 1916 | } |
||
| 1917 | |||
| 1918 | /** |
||
| 1919 | * Gets and parses additional plugin data to send with the heartbeat data |
||
| 1920 | * |
||
| 1921 | * @since 3.8.1 |
||
| 1922 | * |
||
| 1923 | * @return array Array of plugin data |
||
| 1924 | */ |
||
| 1925 | public static function get_parsed_plugin_data() { |
||
| 1926 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 1927 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
| 1928 | } |
||
| 1929 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 1930 | $all_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
| 1931 | $active_plugins = Jetpack::get_active_plugins(); |
||
| 1932 | |||
| 1933 | $plugins = array(); |
||
| 1934 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
| 1935 | $plugins[ $path ] = array( |
||
| 1936 | 'is_active' => in_array( $path, $active_plugins ), |
||
| 1937 | 'file' => $path, |
||
| 1938 | 'name' => $plugin_data['Name'], |
||
| 1939 | 'version' => $plugin_data['Version'], |
||
| 1940 | 'author' => $plugin_data['Author'], |
||
| 1941 | ); |
||
| 1942 | } |
||
| 1943 | |||
| 1944 | return $plugins; |
||
| 1945 | } |
||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * Gets and parses theme data to send with the heartbeat data |
||
| 1949 | * |
||
| 1950 | * @since 3.8.1 |
||
| 1951 | * |
||
| 1952 | * @return array Array of theme data |
||
| 1953 | */ |
||
| 1954 | public static function get_parsed_theme_data() { |
||
| 1955 | $all_themes = wp_get_themes( array( 'allowed' => true ) ); |
||
| 1956 | $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); |
||
| 1957 | |||
| 1958 | $themes = array(); |
||
| 1959 | foreach ( $all_themes as $slug => $theme_data ) { |
||
| 1960 | $theme_headers = array(); |
||
| 1961 | foreach ( $header_keys as $header_key ) { |
||
| 1962 | $theme_headers[ $header_key ] = $theme_data->get( $header_key ); |
||
| 1963 | } |
||
| 1964 | |||
| 1965 | $themes[ $slug ] = array( |
||
| 1966 | 'is_active_theme' => $slug == wp_get_theme()->get_template(), |
||
| 1967 | 'slug' => $slug, |
||
| 1968 | 'theme_root' => $theme_data->get_theme_root_uri(), |
||
| 1969 | 'parent' => $theme_data->parent(), |
||
| 1970 | 'headers' => $theme_headers |
||
| 1971 | ); |
||
| 1972 | } |
||
| 1973 | |||
| 1974 | return $themes; |
||
| 1975 | } |
||
| 1976 | |||
| 1977 | /** |
||
| 1978 | * Checks whether a specific plugin is active. |
||
| 1979 | * |
||
| 1980 | * We don't want to store these in a static variable, in case |
||
| 1981 | * there are switch_to_blog() calls involved. |
||
| 1982 | */ |
||
| 1983 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
| 1984 | return in_array( $plugin, self::get_active_plugins() ); |
||
| 1985 | } |
||
| 1986 | |||
| 1987 | /** |
||
| 1988 | * Check if Jetpack's Open Graph tags should be used. |
||
| 1989 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
| 1990 | * |
||
| 1991 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1992 | * @action plugins_loaded |
||
| 1993 | * @return null |
||
| 1994 | */ |
||
| 1995 | public function check_open_graph() { |
||
| 1996 | if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { |
||
| 1997 | add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); |
||
| 1998 | } |
||
| 1999 | |||
| 2000 | $active_plugins = self::get_active_plugins(); |
||
| 2001 | |||
| 2002 | if ( ! empty( $active_plugins ) ) { |
||
| 2003 | foreach ( $this->open_graph_conflicting_plugins as $plugin ) { |
||
| 2004 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 2005 | add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); |
||
| 2006 | break; |
||
| 2007 | } |
||
| 2008 | } |
||
| 2009 | } |
||
| 2010 | |||
| 2011 | /** |
||
| 2012 | * Allow the addition of Open Graph Meta Tags to all pages. |
||
| 2013 | * |
||
| 2014 | * @since 2.0.3 |
||
| 2015 | * |
||
| 2016 | * @param bool false Should Open Graph Meta tags be added. Default to false. |
||
| 2017 | */ |
||
| 2018 | if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { |
||
| 2019 | require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; |
||
| 2020 | } |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | /** |
||
| 2024 | * Check if Jetpack's Twitter tags should be used. |
||
| 2025 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
| 2026 | * |
||
| 2027 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 2028 | * @action plugins_loaded |
||
| 2029 | * @return null |
||
| 2030 | */ |
||
| 2031 | public function check_twitter_tags() { |
||
| 2032 | |||
| 2033 | $active_plugins = self::get_active_plugins(); |
||
| 2034 | |||
| 2035 | if ( ! empty( $active_plugins ) ) { |
||
| 2036 | foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { |
||
| 2037 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 2038 | add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); |
||
| 2039 | break; |
||
| 2040 | } |
||
| 2041 | } |
||
| 2042 | } |
||
| 2043 | |||
| 2044 | /** |
||
| 2045 | * Allow Twitter Card Meta tags to be disabled. |
||
| 2046 | * |
||
| 2047 | * @since 2.6.0 |
||
| 2048 | * |
||
| 2049 | * @param bool true Should Twitter Card Meta tags be disabled. Default to true. |
||
| 2050 | */ |
||
| 2051 | if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) { |
||
| 2052 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; |
||
| 2053 | } |
||
| 2054 | } |
||
| 2055 | |||
| 2056 | /** |
||
| 2057 | * Allows plugins to submit security reports. |
||
| 2058 | * |
||
| 2059 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
| 2060 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
| 2061 | * @param array $args See definitions above |
||
| 2062 | */ |
||
| 2063 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
| 2064 | _deprecated_function( __FUNCTION__, 'jetpack-4.2', null ); |
||
| 2065 | } |
||
| 2066 | |||
| 2067 | /* Jetpack Options API */ |
||
| 2068 | |||
| 2069 | public static function get_option_names( $type = 'compact' ) { |
||
| 2070 | return Jetpack_Options::get_option_names( $type ); |
||
| 2071 | } |
||
| 2072 | |||
| 2073 | /** |
||
| 2074 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
| 2075 | * |
||
| 2076 | * @param string $name Option name |
||
| 2077 | * @param mixed $default (optional) |
||
| 2078 | */ |
||
| 2079 | public static function get_option( $name, $default = false ) { |
||
| 2080 | return Jetpack_Options::get_option( $name, $default ); |
||
| 2081 | } |
||
| 2082 | |||
| 2083 | /** |
||
| 2084 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
| 2085 | * |
||
| 2086 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
| 2087 | * @param string $name Option name |
||
| 2088 | * @param mixed $value Option value |
||
| 2089 | */ |
||
| 2090 | public static function update_option( $name, $value ) { |
||
| 2091 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' ); |
||
| 2092 | return Jetpack_Options::update_option( $name, $value ); |
||
| 2093 | } |
||
| 2094 | |||
| 2095 | /** |
||
| 2096 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
| 2097 | * |
||
| 2098 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
| 2099 | * @param array $array array( option name => option value, ... ) |
||
| 2100 | */ |
||
| 2101 | public static function update_options( $array ) { |
||
| 2102 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' ); |
||
| 2103 | return Jetpack_Options::update_options( $array ); |
||
| 2104 | } |
||
| 2105 | |||
| 2106 | /** |
||
| 2107 | * Deletes the given option. May be passed multiple option names as an array. |
||
| 2108 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
| 2109 | * |
||
| 2110 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
| 2111 | * @param string|array $names |
||
| 2112 | */ |
||
| 2113 | public static function delete_option( $names ) { |
||
| 2114 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' ); |
||
| 2115 | return Jetpack_Options::delete_option( $names ); |
||
| 2116 | } |
||
| 2117 | |||
| 2118 | /** |
||
| 2119 | * Enters a user token into the user_tokens option |
||
| 2120 | * |
||
| 2121 | * @param int $user_id |
||
| 2122 | * @param string $token |
||
| 2123 | * return bool |
||
| 2124 | */ |
||
| 2125 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
| 2126 | // not designed for concurrent updates |
||
| 2127 | $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); |
||
| 2128 | if ( ! is_array( $user_tokens ) ) |
||
| 2129 | $user_tokens = array(); |
||
| 2130 | $user_tokens[$user_id] = $token; |
||
| 2131 | if ( $is_master_user ) { |
||
| 2132 | $master_user = $user_id; |
||
| 2133 | $options = compact( 'user_tokens', 'master_user' ); |
||
| 2134 | } else { |
||
| 2135 | $options = compact( 'user_tokens' ); |
||
| 2136 | } |
||
| 2137 | return Jetpack_Options::update_options( $options ); |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | /** |
||
| 2141 | * Returns an array of all PHP files in the specified absolute path. |
||
| 2142 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
| 2143 | * |
||
| 2144 | * @param string $absolute_path The absolute path of the directory to search. |
||
| 2145 | * @return array Array of absolute paths to the PHP files. |
||
| 2146 | */ |
||
| 2147 | public static function glob_php( $absolute_path ) { |
||
| 2148 | if ( function_exists( 'glob' ) ) { |
||
| 2149 | return glob( "$absolute_path/*.php" ); |
||
| 2150 | } |
||
| 2151 | |||
| 2152 | $absolute_path = untrailingslashit( $absolute_path ); |
||
| 2153 | $files = array(); |
||
| 2154 | if ( ! $dir = @opendir( $absolute_path ) ) { |
||
| 2155 | return $files; |
||
| 2156 | } |
||
| 2157 | |||
| 2158 | while ( false !== $file = readdir( $dir ) ) { |
||
| 2159 | if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) { |
||
| 2160 | continue; |
||
| 2161 | } |
||
| 2162 | |||
| 2163 | $file = "$absolute_path/$file"; |
||
| 2164 | |||
| 2165 | if ( ! is_file( $file ) ) { |
||
| 2166 | continue; |
||
| 2167 | } |
||
| 2168 | |||
| 2169 | $files[] = $file; |
||
| 2170 | } |
||
| 2171 | |||
| 2172 | closedir( $dir ); |
||
| 2173 | |||
| 2174 | return $files; |
||
| 2175 | } |
||
| 2176 | |||
| 2177 | public static function activate_new_modules( $redirect = false ) { |
||
| 2178 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
| 2179 | return; |
||
| 2180 | } |
||
| 2181 | |||
| 2182 | $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic] |
||
| 2183 | View Code Duplication | if ( ! $jetpack_old_version ) { |
|
| 2184 | $jetpack_old_version = $version = $old_version = '1.1:' . time(); |
||
| 2185 | /** This action is documented in class.jetpack.php */ |
||
| 2186 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 2187 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 2188 | } |
||
| 2189 | |||
| 2190 | list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic] |
||
| 2191 | |||
| 2192 | if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) { |
||
| 2193 | return; |
||
| 2194 | } |
||
| 2195 | |||
| 2196 | $active_modules = Jetpack::get_active_modules(); |
||
| 2197 | $reactivate_modules = array(); |
||
| 2198 | foreach ( $active_modules as $active_module ) { |
||
| 2199 | $module = Jetpack::get_module( $active_module ); |
||
| 2200 | if ( ! isset( $module['changed'] ) ) { |
||
| 2201 | continue; |
||
| 2202 | } |
||
| 2203 | |||
| 2204 | if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) { |
||
| 2205 | continue; |
||
| 2206 | } |
||
| 2207 | |||
| 2208 | $reactivate_modules[] = $active_module; |
||
| 2209 | Jetpack::deactivate_module( $active_module ); |
||
| 2210 | } |
||
| 2211 | |||
| 2212 | $new_version = JETPACK__VERSION . ':' . time(); |
||
| 2213 | /** This action is documented in class.jetpack.php */ |
||
| 2214 | do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version ); |
||
| 2215 | Jetpack_Options::update_options( |
||
| 2216 | array( |
||
| 2217 | 'version' => $new_version, |
||
| 2218 | 'old_version' => $jetpack_old_version, |
||
| 2219 | ) |
||
| 2220 | ); |
||
| 2221 | |||
| 2222 | Jetpack::state( 'message', 'modules_activated' ); |
||
| 2223 | Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules ); |
||
| 2224 | |||
| 2225 | if ( $redirect ) { |
||
| 2226 | $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page |
||
| 2227 | if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) { |
||
| 2228 | $page = $_GET['page']; |
||
| 2229 | } |
||
| 2230 | |||
| 2231 | wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) ); |
||
| 2232 | exit; |
||
| 2233 | } |
||
| 2234 | } |
||
| 2235 | |||
| 2236 | /** |
||
| 2237 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
| 2238 | * Make sure to tuck away module "library" files in a sub-directory. |
||
| 2239 | */ |
||
| 2240 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
| 2241 | static $modules = null; |
||
| 2242 | |||
| 2243 | if ( ! isset( $modules ) ) { |
||
| 2244 | $available_modules_option = Jetpack_Options::get_option( 'available_modules', array() ); |
||
| 2245 | // Use the cache if we're on the front-end and it's available... |
||
| 2246 | if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) { |
||
| 2247 | $modules = $available_modules_option[ JETPACK__VERSION ]; |
||
| 2248 | } else { |
||
| 2249 | $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' ); |
||
| 2250 | |||
| 2251 | $modules = array(); |
||
| 2252 | |||
| 2253 | foreach ( $files as $file ) { |
||
| 2254 | if ( ! $headers = Jetpack::get_module( $file ) ) { |
||
| 2255 | continue; |
||
| 2256 | } |
||
| 2257 | |||
| 2258 | $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced']; |
||
| 2259 | } |
||
| 2260 | |||
| 2261 | Jetpack_Options::update_option( 'available_modules', array( |
||
| 2262 | JETPACK__VERSION => $modules, |
||
| 2263 | ) ); |
||
| 2264 | } |
||
| 2265 | } |
||
| 2266 | |||
| 2267 | /** |
||
| 2268 | * Filters the array of modules available to be activated. |
||
| 2269 | * |
||
| 2270 | * @since 2.4.0 |
||
| 2271 | * |
||
| 2272 | * @param array $modules Array of available modules. |
||
| 2273 | * @param string $min_version Minimum version number required to use modules. |
||
| 2274 | * @param string $max_version Maximum version number required to use modules. |
||
| 2275 | */ |
||
| 2276 | $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version ); |
||
| 2277 | |||
| 2278 | if ( ! $min_version && ! $max_version ) { |
||
| 2279 | return array_keys( $mods ); |
||
| 2280 | } |
||
| 2281 | |||
| 2282 | $r = array(); |
||
| 2283 | foreach ( $mods as $slug => $introduced ) { |
||
| 2284 | if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) { |
||
| 2285 | continue; |
||
| 2286 | } |
||
| 2287 | |||
| 2288 | if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) { |
||
| 2289 | continue; |
||
| 2290 | } |
||
| 2291 | |||
| 2292 | $r[] = $slug; |
||
| 2293 | } |
||
| 2294 | |||
| 2295 | return $r; |
||
| 2296 | } |
||
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Default modules loaded on activation. |
||
| 2300 | */ |
||
| 2301 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
| 2302 | $return = array(); |
||
| 2303 | |||
| 2304 | foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) { |
||
| 2305 | $module_data = Jetpack::get_module( $module ); |
||
| 2306 | |||
| 2307 | switch ( strtolower( $module_data['auto_activate'] ) ) { |
||
| 2308 | case 'yes' : |
||
| 2309 | $return[] = $module; |
||
| 2310 | break; |
||
| 2311 | case 'public' : |
||
| 2312 | if ( Jetpack_Options::get_option( 'public' ) ) { |
||
| 2313 | $return[] = $module; |
||
| 2314 | } |
||
| 2315 | break; |
||
| 2316 | case 'no' : |
||
| 2317 | default : |
||
| 2318 | break; |
||
| 2319 | } |
||
| 2320 | } |
||
| 2321 | /** |
||
| 2322 | * Filters the array of default modules. |
||
| 2323 | * |
||
| 2324 | * @since 2.5.0 |
||
| 2325 | * |
||
| 2326 | * @param array $return Array of default modules. |
||
| 2327 | * @param string $min_version Minimum version number required to use modules. |
||
| 2328 | * @param string $max_version Maximum version number required to use modules. |
||
| 2329 | */ |
||
| 2330 | return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version ); |
||
| 2331 | } |
||
| 2332 | |||
| 2333 | /** |
||
| 2334 | * Checks activated modules during auto-activation to determine |
||
| 2335 | * if any of those modules are being deprecated. If so, close |
||
| 2336 | * them out, and add any replacement modules. |
||
| 2337 | * |
||
| 2338 | * Runs at priority 99 by default. |
||
| 2339 | * |
||
| 2340 | * This is run late, so that it can still activate a module if |
||
| 2341 | * the new module is a replacement for another that the user |
||
| 2342 | * currently has active, even if something at the normal priority |
||
| 2343 | * would kibosh everything. |
||
| 2344 | * |
||
| 2345 | * @since 2.6 |
||
| 2346 | * @uses jetpack_get_default_modules filter |
||
| 2347 | * @param array $modules |
||
| 2348 | * @return array |
||
| 2349 | */ |
||
| 2350 | function handle_deprecated_modules( $modules ) { |
||
| 2351 | $deprecated_modules = array( |
||
| 2352 | 'debug' => null, // Closed out and moved to the debugger library. |
||
| 2353 | 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality. |
||
| 2354 | 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support. |
||
| 2355 | ); |
||
| 2356 | |||
| 2357 | // Don't activate SSO if they never completed activating WPCC. |
||
| 2358 | if ( Jetpack::is_module_active( 'wpcc' ) ) { |
||
| 2359 | $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' ); |
||
| 2360 | if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) { |
||
| 2361 | $deprecated_modules['wpcc'] = null; |
||
| 2362 | } |
||
| 2363 | } |
||
| 2364 | |||
| 2365 | foreach ( $deprecated_modules as $module => $replacement ) { |
||
| 2366 | if ( Jetpack::is_module_active( $module ) ) { |
||
| 2367 | self::deactivate_module( $module ); |
||
| 2368 | if ( $replacement ) { |
||
| 2369 | $modules[] = $replacement; |
||
| 2370 | } |
||
| 2371 | } |
||
| 2372 | } |
||
| 2373 | |||
| 2374 | return array_unique( $modules ); |
||
| 2375 | } |
||
| 2376 | |||
| 2377 | /** |
||
| 2378 | * Checks activated plugins during auto-activation to determine |
||
| 2379 | * if any of those plugins are in the list with a corresponding module |
||
| 2380 | * that is not compatible with the plugin. The module will not be allowed |
||
| 2381 | * to auto-activate. |
||
| 2382 | * |
||
| 2383 | * @since 2.6 |
||
| 2384 | * @uses jetpack_get_default_modules filter |
||
| 2385 | * @param array $modules |
||
| 2386 | * @return array |
||
| 2387 | */ |
||
| 2388 | function filter_default_modules( $modules ) { |
||
| 2389 | |||
| 2390 | $active_plugins = self::get_active_plugins(); |
||
| 2391 | |||
| 2392 | if ( ! empty( $active_plugins ) ) { |
||
| 2393 | |||
| 2394 | // For each module we'd like to auto-activate... |
||
| 2395 | foreach ( $modules as $key => $module ) { |
||
| 2396 | // If there are potential conflicts for it... |
||
| 2397 | if ( ! empty( $this->conflicting_plugins[ $module ] ) ) { |
||
| 2398 | // For each potential conflict... |
||
| 2399 | foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) { |
||
| 2400 | // If that conflicting plugin is active... |
||
| 2401 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 2402 | // Remove that item from being auto-activated. |
||
| 2403 | unset( $modules[ $key ] ); |
||
| 2404 | } |
||
| 2405 | } |
||
| 2406 | } |
||
| 2407 | } |
||
| 2408 | } |
||
| 2409 | |||
| 2410 | return $modules; |
||
| 2411 | } |
||
| 2412 | |||
| 2413 | /** |
||
| 2414 | * Extract a module's slug from its full path. |
||
| 2415 | */ |
||
| 2416 | public static function get_module_slug( $file ) { |
||
| 2417 | return str_replace( '.php', '', basename( $file ) ); |
||
| 2418 | } |
||
| 2419 | |||
| 2420 | /** |
||
| 2421 | * Generate a module's path from its slug. |
||
| 2422 | */ |
||
| 2423 | public static function get_module_path( $slug ) { |
||
| 2424 | /** |
||
| 2425 | * Filters the path of a modules. |
||
| 2426 | * |
||
| 2427 | * @since 7.4.0 |
||
| 2428 | * |
||
| 2429 | * @param array $return The absolute path to a module's root php file |
||
| 2430 | * @param string $slug The module slug |
||
| 2431 | */ |
||
| 2432 | return apply_filters( 'jetpack_get_module_path', JETPACK__PLUGIN_DIR . "modules/$slug.php", $slug ); |
||
| 2433 | } |
||
| 2434 | |||
| 2435 | /** |
||
| 2436 | * Load module data from module file. Headers differ from WordPress |
||
| 2437 | * plugin headers to avoid them being identified as standalone |
||
| 2438 | * plugins on the WordPress plugins page. |
||
| 2439 | */ |
||
| 2440 | public static function get_module( $module ) { |
||
| 2441 | $headers = array( |
||
| 2442 | 'name' => 'Module Name', |
||
| 2443 | 'description' => 'Module Description', |
||
| 2444 | 'sort' => 'Sort Order', |
||
| 2445 | 'recommendation_order' => 'Recommendation Order', |
||
| 2446 | 'introduced' => 'First Introduced', |
||
| 2447 | 'changed' => 'Major Changes In', |
||
| 2448 | 'deactivate' => 'Deactivate', |
||
| 2449 | 'free' => 'Free', |
||
| 2450 | 'requires_connection' => 'Requires Connection', |
||
| 2451 | 'auto_activate' => 'Auto Activate', |
||
| 2452 | 'module_tags' => 'Module Tags', |
||
| 2453 | 'feature' => 'Feature', |
||
| 2454 | 'additional_search_queries' => 'Additional Search Queries', |
||
| 2455 | 'plan_classes' => 'Plans', |
||
| 2456 | ); |
||
| 2457 | |||
| 2458 | $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) ); |
||
| 2459 | |||
| 2460 | $mod = Jetpack::get_file_data( $file, $headers ); |
||
| 2461 | if ( empty( $mod['name'] ) ) { |
||
| 2462 | return false; |
||
| 2463 | } |
||
| 2464 | |||
| 2465 | $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; |
||
| 2466 | $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; |
||
| 2467 | $mod['deactivate'] = empty( $mod['deactivate'] ); |
||
| 2468 | $mod['free'] = empty( $mod['free'] ); |
||
| 2469 | $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true; |
||
| 2470 | |||
| 2471 | if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) { |
||
| 2472 | $mod['auto_activate'] = 'No'; |
||
| 2473 | } else { |
||
| 2474 | $mod['auto_activate'] = (string) $mod['auto_activate']; |
||
| 2475 | } |
||
| 2476 | |||
| 2477 | if ( $mod['module_tags'] ) { |
||
| 2478 | $mod['module_tags'] = explode( ',', $mod['module_tags'] ); |
||
| 2479 | $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] ); |
||
| 2480 | $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] ); |
||
| 2481 | } else { |
||
| 2482 | $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) ); |
||
| 2483 | } |
||
| 2484 | |||
| 2485 | View Code Duplication | if ( $mod['plan_classes'] ) { |
|
| 2486 | $mod['plan_classes'] = explode( ',', $mod['plan_classes'] ); |
||
| 2487 | $mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) ); |
||
| 2488 | } else { |
||
| 2489 | $mod['plan_classes'] = array( 'free' ); |
||
| 2490 | } |
||
| 2491 | |||
| 2492 | View Code Duplication | if ( $mod['feature'] ) { |
|
| 2493 | $mod['feature'] = explode( ',', $mod['feature'] ); |
||
| 2494 | $mod['feature'] = array_map( 'trim', $mod['feature'] ); |
||
| 2495 | } else { |
||
| 2496 | $mod['feature'] = array( self::translate_module_tag( 'Other' ) ); |
||
| 2497 | } |
||
| 2498 | |||
| 2499 | /** |
||
| 2500 | * Filters the feature array on a module. |
||
| 2501 | * |
||
| 2502 | * This filter allows you to control where each module is filtered: Recommended, |
||
| 2503 | * and the default "Other" listing. |
||
| 2504 | * |
||
| 2505 | * @since 3.5.0 |
||
| 2506 | * |
||
| 2507 | * @param array $mod['feature'] The areas to feature this module: |
||
| 2508 | * 'Recommended' shows on the main Jetpack admin screen. |
||
| 2509 | * 'Other' should be the default if no other value is in the array. |
||
| 2510 | * @param string $module The slug of the module, e.g. sharedaddy. |
||
| 2511 | * @param array $mod All the currently assembled module data. |
||
| 2512 | */ |
||
| 2513 | $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod ); |
||
| 2514 | |||
| 2515 | /** |
||
| 2516 | * Filter the returned data about a module. |
||
| 2517 | * |
||
| 2518 | * This filter allows overriding any info about Jetpack modules. It is dangerous, |
||
| 2519 | * so please be careful. |
||
| 2520 | * |
||
| 2521 | * @since 3.6.0 |
||
| 2522 | * |
||
| 2523 | * @param array $mod The details of the requested module. |
||
| 2524 | * @param string $module The slug of the module, e.g. sharedaddy |
||
| 2525 | * @param string $file The path to the module source file. |
||
| 2526 | */ |
||
| 2527 | return apply_filters( 'jetpack_get_module', $mod, $module, $file ); |
||
| 2528 | } |
||
| 2529 | |||
| 2530 | /** |
||
| 2531 | * Like core's get_file_data implementation, but caches the result. |
||
| 2532 | */ |
||
| 2533 | public static function get_file_data( $file, $headers ) { |
||
| 2534 | //Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated |
||
| 2535 | $file_name = basename( $file ); |
||
| 2536 | |||
| 2537 | $cache_key = 'jetpack_file_data_' . JETPACK__VERSION; |
||
| 2538 | |||
| 2539 | $file_data_option = get_transient( $cache_key ); |
||
| 2540 | |||
| 2541 | if ( false === $file_data_option ) { |
||
| 2542 | $file_data_option = array(); |
||
| 2543 | } |
||
| 2544 | |||
| 2545 | $key = md5( $file_name . serialize( $headers ) ); |
||
| 2546 | $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 ); |
||
| 2547 | |||
| 2548 | // If we don't need to refresh the cache, and already have the value, short-circuit! |
||
| 2549 | if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) { |
||
| 2550 | return $file_data_option[ $key ]; |
||
| 2551 | } |
||
| 2552 | |||
| 2553 | $data = get_file_data( $file, $headers ); |
||
| 2554 | |||
| 2555 | $file_data_option[ $key ] = $data; |
||
| 2556 | |||
| 2557 | set_transient( $cache_key, $file_data_option, 29 * DAY_IN_SECONDS ); |
||
| 2558 | |||
| 2559 | return $data; |
||
| 2560 | } |
||
| 2561 | |||
| 2562 | |||
| 2563 | /** |
||
| 2564 | * Return translated module tag. |
||
| 2565 | * |
||
| 2566 | * @param string $tag Tag as it appears in each module heading. |
||
| 2567 | * |
||
| 2568 | * @return mixed |
||
| 2569 | */ |
||
| 2570 | public static function translate_module_tag( $tag ) { |
||
| 2571 | return jetpack_get_module_i18n_tag( $tag ); |
||
| 2572 | } |
||
| 2573 | |||
| 2574 | /** |
||
| 2575 | * Get i18n strings as a JSON-encoded string |
||
| 2576 | * |
||
| 2577 | * @return string The locale as JSON |
||
| 2578 | */ |
||
| 2579 | public static function get_i18n_data_json() { |
||
| 2580 | |||
| 2581 | // WordPress 5.0 uses md5 hashes of file paths to associate translation |
||
| 2582 | // JSON files with the file they should be included for. This is an md5 |
||
| 2583 | // of '_inc/build/admin.js'. |
||
| 2584 | $path_md5 = '1bac79e646a8bf4081a5011ab72d5807'; |
||
| 2585 | |||
| 2586 | $i18n_json = |
||
| 2587 | JETPACK__PLUGIN_DIR |
||
| 2588 | . 'languages/json/jetpack-' |
||
| 2589 | . get_user_locale() |
||
| 2590 | . '-' |
||
| 2591 | . $path_md5 |
||
| 2592 | . '.json'; |
||
| 2593 | |||
| 2594 | if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { |
||
| 2595 | $locale_data = @file_get_contents( $i18n_json ); |
||
| 2596 | if ( $locale_data ) { |
||
| 2597 | return $locale_data; |
||
| 2598 | } |
||
| 2599 | } |
||
| 2600 | |||
| 2601 | // Return valid empty Jed locale |
||
| 2602 | return '{ "locale_data": { "messages": { "": {} } } }'; |
||
| 2603 | } |
||
| 2604 | |||
| 2605 | /** |
||
| 2606 | * Add locale data setup to wp-i18n |
||
| 2607 | * |
||
| 2608 | * Any Jetpack script that depends on wp-i18n should use this method to set up the locale. |
||
| 2609 | * |
||
| 2610 | * The locale setup depends on an adding inline script. This is error-prone and could easily |
||
| 2611 | * result in multiple additions of the same script when exactly 0 or 1 is desireable. |
||
| 2612 | * |
||
| 2613 | * This method provides a safe way to request the setup multiple times but add the script at |
||
| 2614 | * most once. |
||
| 2615 | * |
||
| 2616 | * @since 6.7.0 |
||
| 2617 | * |
||
| 2618 | * @return void |
||
| 2619 | */ |
||
| 2620 | public static function setup_wp_i18n_locale_data() { |
||
| 2621 | static $script_added = false; |
||
| 2622 | if ( ! $script_added ) { |
||
| 2623 | $script_added = true; |
||
| 2624 | wp_add_inline_script( |
||
| 2625 | 'wp-i18n', |
||
| 2626 | 'wp.i18n.setLocaleData( ' . Jetpack::get_i18n_data_json() . ', \'jetpack\' );' |
||
| 2627 | ); |
||
| 2628 | } |
||
| 2629 | } |
||
| 2630 | |||
| 2631 | /** |
||
| 2632 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
| 2633 | * |
||
| 2634 | * @since 3.9.2 |
||
| 2635 | * |
||
| 2636 | * @param array $modules |
||
| 2637 | * |
||
| 2638 | * @return string|void |
||
| 2639 | */ |
||
| 2640 | public static function get_translated_modules( $modules ) { |
||
| 2641 | foreach ( $modules as $index => $module ) { |
||
| 2642 | $i18n_module = jetpack_get_module_i18n( $module['module'] ); |
||
| 2643 | if ( isset( $module['name'] ) ) { |
||
| 2644 | $modules[ $index ]['name'] = $i18n_module['name']; |
||
| 2645 | } |
||
| 2646 | if ( isset( $module['description'] ) ) { |
||
| 2647 | $modules[ $index ]['description'] = $i18n_module['description']; |
||
| 2648 | $modules[ $index ]['short_description'] = $i18n_module['description']; |
||
| 2649 | } |
||
| 2650 | } |
||
| 2651 | return $modules; |
||
| 2652 | } |
||
| 2653 | |||
| 2654 | /** |
||
| 2655 | * Get a list of activated modules as an array of module slugs. |
||
| 2656 | */ |
||
| 2657 | public static function get_active_modules() { |
||
| 2658 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
| 2659 | |||
| 2660 | if ( ! is_array( $active ) ) { |
||
| 2661 | $active = array(); |
||
| 2662 | } |
||
| 2663 | |||
| 2664 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
| 2665 | $active[] = 'vaultpress'; |
||
| 2666 | } else { |
||
| 2667 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
| 2668 | } |
||
| 2669 | |||
| 2670 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
| 2671 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
| 2672 | $active[] = 'protect'; |
||
| 2673 | } |
||
| 2674 | |||
| 2675 | /** |
||
| 2676 | * Allow filtering of the active modules. |
||
| 2677 | * |
||
| 2678 | * Gives theme and plugin developers the power to alter the modules that |
||
| 2679 | * are activated on the fly. |
||
| 2680 | * |
||
| 2681 | * @since 5.8.0 |
||
| 2682 | * |
||
| 2683 | * @param array $active Array of active module slugs. |
||
| 2684 | */ |
||
| 2685 | $active = apply_filters( 'jetpack_active_modules', $active ); |
||
| 2686 | |||
| 2687 | return array_unique( $active ); |
||
| 2688 | } |
||
| 2689 | |||
| 2690 | /** |
||
| 2691 | * Check whether or not a Jetpack module is active. |
||
| 2692 | * |
||
| 2693 | * @param string $module The slug of a Jetpack module. |
||
| 2694 | * @return bool |
||
| 2695 | * |
||
| 2696 | * @static |
||
| 2697 | */ |
||
| 2698 | public static function is_module_active( $module ) { |
||
| 2699 | return in_array( $module, self::get_active_modules() ); |
||
| 2700 | } |
||
| 2701 | |||
| 2702 | public static function is_module( $module ) { |
||
| 2703 | return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() ); |
||
| 2704 | } |
||
| 2705 | |||
| 2706 | /** |
||
| 2707 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
| 2708 | * |
||
| 2709 | * @param bool $catch True to start catching, False to stop. |
||
| 2710 | * |
||
| 2711 | * @static |
||
| 2712 | */ |
||
| 2713 | public static function catch_errors( $catch ) { |
||
| 2714 | static $display_errors, $error_reporting; |
||
| 2715 | |||
| 2716 | if ( $catch ) { |
||
| 2717 | $display_errors = @ini_set( 'display_errors', 1 ); |
||
| 2718 | $error_reporting = @error_reporting( E_ALL ); |
||
| 2719 | add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
| 2720 | } else { |
||
| 2721 | @ini_set( 'display_errors', $display_errors ); |
||
| 2722 | @error_reporting( $error_reporting ); |
||
| 2723 | remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
| 2724 | } |
||
| 2725 | } |
||
| 2726 | |||
| 2727 | /** |
||
| 2728 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
| 2729 | */ |
||
| 2730 | public static function catch_errors_on_shutdown() { |
||
| 2731 | Jetpack::state( 'php_errors', self::alias_directories( ob_get_clean() ) ); |
||
| 2732 | } |
||
| 2733 | |||
| 2734 | /** |
||
| 2735 | * Rewrite any string to make paths easier to read. |
||
| 2736 | * |
||
| 2737 | * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR |
||
| 2738 | * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR. |
||
| 2739 | * |
||
| 2740 | * @param $string |
||
| 2741 | * @return mixed |
||
| 2742 | */ |
||
| 2743 | public static function alias_directories( $string ) { |
||
| 2744 | // ABSPATH has a trailing slash. |
||
| 2745 | $string = str_replace( ABSPATH, 'ABSPATH/', $string ); |
||
| 2746 | // WP_CONTENT_DIR does not have a trailing slash. |
||
| 2747 | $string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string ); |
||
| 2748 | |||
| 2749 | return $string; |
||
| 2750 | } |
||
| 2751 | |||
| 2752 | public static function activate_default_modules( |
||
| 2753 | $min_version = false, |
||
| 2754 | $max_version = false, |
||
| 2755 | $other_modules = array(), |
||
| 2756 | $redirect = true, |
||
| 2757 | $send_state_messages = true |
||
| 2758 | ) { |
||
| 2759 | $jetpack = Jetpack::init(); |
||
| 2760 | |||
| 2761 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
| 2762 | $modules = array_merge( $other_modules, $modules ); |
||
| 2763 | |||
| 2764 | // Look for standalone plugins and disable if active. |
||
| 2765 | |||
| 2766 | $to_deactivate = array(); |
||
| 2767 | foreach ( $modules as $module ) { |
||
| 2768 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
| 2769 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
| 2770 | } |
||
| 2771 | } |
||
| 2772 | |||
| 2773 | $deactivated = array(); |
||
| 2774 | foreach ( $to_deactivate as $module => $deactivate_me ) { |
||
| 2775 | list( $probable_file, $probable_title ) = $deactivate_me; |
||
| 2776 | if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { |
||
| 2777 | $deactivated[] = $module; |
||
| 2778 | } |
||
| 2779 | } |
||
| 2780 | |||
| 2781 | if ( $deactivated && $redirect ) { |
||
| 2782 | Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) ); |
||
| 2783 | |||
| 2784 | $url = add_query_arg( |
||
| 2785 | array( |
||
| 2786 | 'action' => 'activate_default_modules', |
||
| 2787 | '_wpnonce' => wp_create_nonce( 'activate_default_modules' ), |
||
| 2788 | ), |
||
| 2789 | add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) |
||
| 2790 | ); |
||
| 2791 | wp_safe_redirect( $url ); |
||
| 2792 | exit; |
||
| 2793 | } |
||
| 2794 | |||
| 2795 | /** |
||
| 2796 | * Fires before default modules are activated. |
||
| 2797 | * |
||
| 2798 | * @since 1.9.0 |
||
| 2799 | * |
||
| 2800 | * @param string $min_version Minimum version number required to use modules. |
||
| 2801 | * @param string $max_version Maximum version number required to use modules. |
||
| 2802 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
| 2803 | */ |
||
| 2804 | do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
| 2805 | |||
| 2806 | // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating |
||
| 2807 | if ( $send_state_messages ) { |
||
| 2808 | Jetpack::restate(); |
||
| 2809 | Jetpack::catch_errors( true ); |
||
| 2810 | } |
||
| 2811 | |||
| 2812 | $active = Jetpack::get_active_modules(); |
||
| 2813 | |||
| 2814 | foreach ( $modules as $module ) { |
||
| 2815 | if ( did_action( "jetpack_module_loaded_$module" ) ) { |
||
| 2816 | $active[] = $module; |
||
| 2817 | self::update_active_modules( $active ); |
||
| 2818 | continue; |
||
| 2819 | } |
||
| 2820 | |||
| 2821 | if ( $send_state_messages && in_array( $module, $active ) ) { |
||
| 2822 | $module_info = Jetpack::get_module( $module ); |
||
| 2823 | View Code Duplication | if ( ! $module_info['deactivate'] ) { |
|
| 2824 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
| 2825 | if ( $active_state = Jetpack::state( $state ) ) { |
||
| 2826 | $active_state = explode( ',', $active_state ); |
||
| 2827 | } else { |
||
| 2828 | $active_state = array(); |
||
| 2829 | } |
||
| 2830 | $active_state[] = $module; |
||
| 2831 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
| 2832 | } |
||
| 2833 | continue; |
||
| 2834 | } |
||
| 2835 | |||
| 2836 | $file = Jetpack::get_module_path( $module ); |
||
| 2837 | if ( ! file_exists( $file ) ) { |
||
| 2838 | continue; |
||
| 2839 | } |
||
| 2840 | |||
| 2841 | // we'll override this later if the plugin can be included without fatal error |
||
| 2842 | if ( $redirect ) { |
||
| 2843 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 2844 | } |
||
| 2845 | |||
| 2846 | if ( $send_state_messages ) { |
||
| 2847 | Jetpack::state( 'error', 'module_activation_failed' ); |
||
| 2848 | Jetpack::state( 'module', $module ); |
||
| 2849 | } |
||
| 2850 | |||
| 2851 | ob_start(); |
||
| 2852 | require_once $file; |
||
| 2853 | |||
| 2854 | $active[] = $module; |
||
| 2855 | |||
| 2856 | View Code Duplication | if ( $send_state_messages ) { |
|
| 2857 | |||
| 2858 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
| 2859 | if ( $active_state = Jetpack::state( $state ) ) { |
||
| 2860 | $active_state = explode( ',', $active_state ); |
||
| 2861 | } else { |
||
| 2862 | $active_state = array(); |
||
| 2863 | } |
||
| 2864 | $active_state[] = $module; |
||
| 2865 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
| 2866 | } |
||
| 2867 | |||
| 2868 | Jetpack::update_active_modules( $active ); |
||
| 2869 | |||
| 2870 | ob_end_clean(); |
||
| 2871 | } |
||
| 2872 | |||
| 2873 | if ( $send_state_messages ) { |
||
| 2874 | Jetpack::state( 'error', false ); |
||
| 2875 | Jetpack::state( 'module', false ); |
||
| 2876 | } |
||
| 2877 | |||
| 2878 | Jetpack::catch_errors( false ); |
||
| 2879 | /** |
||
| 2880 | * Fires when default modules are activated. |
||
| 2881 | * |
||
| 2882 | * @since 1.9.0 |
||
| 2883 | * |
||
| 2884 | * @param string $min_version Minimum version number required to use modules. |
||
| 2885 | * @param string $max_version Maximum version number required to use modules. |
||
| 2886 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
| 2887 | */ |
||
| 2888 | do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
| 2889 | } |
||
| 2890 | |||
| 2891 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
| 2892 | /** |
||
| 2893 | * Fires before a module is activated. |
||
| 2894 | * |
||
| 2895 | * @since 2.6.0 |
||
| 2896 | * |
||
| 2897 | * @param string $module Module slug. |
||
| 2898 | * @param bool $exit Should we exit after the module has been activated. Default to true. |
||
| 2899 | * @param bool $redirect Should the user be redirected after module activation? Default to true. |
||
| 2900 | */ |
||
| 2901 | do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); |
||
| 2902 | |||
| 2903 | $jetpack = Jetpack::init(); |
||
| 2904 | |||
| 2905 | if ( ! strlen( $module ) ) |
||
| 2906 | return false; |
||
| 2907 | |||
| 2908 | if ( ! Jetpack::is_module( $module ) ) |
||
| 2909 | return false; |
||
| 2910 | |||
| 2911 | // If it's already active, then don't do it again |
||
| 2912 | $active = Jetpack::get_active_modules(); |
||
| 2913 | foreach ( $active as $act ) { |
||
| 2914 | if ( $act == $module ) |
||
| 2915 | return true; |
||
| 2916 | } |
||
| 2917 | |||
| 2918 | $module_data = Jetpack::get_module( $module ); |
||
| 2919 | |||
| 2920 | if ( ! Jetpack::is_active() ) { |
||
| 2921 | if ( ! Jetpack::is_development_mode() && ! Jetpack::is_onboarding() ) |
||
| 2922 | return false; |
||
| 2923 | |||
| 2924 | // If we're not connected but in development mode, make sure the module doesn't require a connection |
||
| 2925 | if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) |
||
| 2926 | return false; |
||
| 2927 | } |
||
| 2928 | |||
| 2929 | // Check and see if the old plugin is active |
||
| 2930 | if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { |
||
| 2931 | // Deactivate the old plugin |
||
| 2932 | if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { |
||
| 2933 | // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module |
||
| 2934 | // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. |
||
| 2935 | Jetpack::state( 'deactivated_plugins', $module ); |
||
| 2936 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
||
| 2937 | exit; |
||
| 2938 | } |
||
| 2939 | } |
||
| 2940 | |||
| 2941 | // Protect won't work with mis-configured IPs |
||
| 2942 | if ( 'protect' === $module ) { |
||
| 2943 | include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; |
||
| 2944 | if ( ! jetpack_protect_get_ip() ) { |
||
| 2945 | Jetpack::state( 'message', 'protect_misconfigured_ip' ); |
||
| 2946 | return false; |
||
| 2947 | } |
||
| 2948 | } |
||
| 2949 | |||
| 2950 | if ( ! Jetpack_Plan::supports( $module ) ) { |
||
| 2951 | return false; |
||
| 2952 | } |
||
| 2953 | |||
| 2954 | // Check the file for fatal errors, a la wp-admin/plugins.php::activate |
||
| 2955 | Jetpack::state( 'module', $module ); |
||
| 2956 | Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error |
||
| 2957 | |||
| 2958 | Jetpack::catch_errors( true ); |
||
| 2959 | ob_start(); |
||
| 2960 | require Jetpack::get_module_path( $module ); |
||
| 2961 | /** This action is documented in class.jetpack.php */ |
||
| 2962 | do_action( 'jetpack_activate_module', $module ); |
||
| 2963 | $active[] = $module; |
||
| 2964 | Jetpack::update_active_modules( $active ); |
||
| 2965 | |||
| 2966 | Jetpack::state( 'error', false ); // the override |
||
| 2967 | ob_end_clean(); |
||
| 2968 | Jetpack::catch_errors( false ); |
||
| 2969 | |||
| 2970 | if ( $redirect ) { |
||
| 2971 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 2972 | } |
||
| 2973 | if ( $exit ) { |
||
| 2974 | exit; |
||
| 2975 | } |
||
| 2976 | return true; |
||
| 2977 | } |
||
| 2978 | |||
| 2979 | function activate_module_actions( $module ) { |
||
| 2982 | |||
| 2983 | public static function deactivate_module( $module ) { |
||
| 2984 | /** |
||
| 2985 | * Fires when a module is deactivated. |
||
| 2986 | * |
||
| 2987 | * @since 1.9.0 |
||
| 2988 | * |
||
| 2989 | * @param string $module Module slug. |
||
| 2990 | */ |
||
| 2991 | do_action( 'jetpack_pre_deactivate_module', $module ); |
||
| 2992 | |||
| 2993 | $jetpack = Jetpack::init(); |
||
| 2994 | |||
| 2995 | $active = Jetpack::get_active_modules(); |
||
| 2996 | $new = array_filter( array_diff( $active, (array) $module ) ); |
||
| 2997 | |||
| 2998 | return self::update_active_modules( $new ); |
||
| 2999 | } |
||
| 3000 | |||
| 3001 | public static function enable_module_configurable( $module ) { |
||
| 3002 | $module = Jetpack::get_module_slug( $module ); |
||
| 3003 | add_filter( 'jetpack_module_configurable_' . $module, '__return_true' ); |
||
| 3004 | } |
||
| 3005 | |||
| 3006 | /** |
||
| 3007 | * Composes a module configure URL. It uses Jetpack settings search as default value |
||
| 3008 | * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter |
||
| 3009 | * |
||
| 3010 | * @param string $module Module slug |
||
| 3011 | * @return string $url module configuration URL |
||
| 3012 | */ |
||
| 3013 | public static function module_configuration_url( $module ) { |
||
| 3014 | $module = Jetpack::get_module_slug( $module ); |
||
| 3015 | $default_url = Jetpack::admin_url() . "#/settings?term=$module"; |
||
| 3016 | /** |
||
| 3017 | * Allows to modify configure_url of specific module to be able to redirect to some custom location. |
||
| 3018 | * |
||
| 3019 | * @since 6.9.0 |
||
| 3020 | * |
||
| 3021 | * @param string $default_url Default url, which redirects to jetpack settings page. |
||
| 3022 | */ |
||
| 3023 | $url = apply_filters( 'jetpack_module_configuration_url_' . $module, $default_url ); |
||
| 3024 | |||
| 3025 | return $url; |
||
| 3026 | } |
||
| 3027 | |||
| 3028 | /* Installation */ |
||
| 3029 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
| 3030 | ?> |
||
| 3031 | <!doctype html> |
||
| 3032 | <html> |
||
| 3033 | <head> |
||
| 3034 | <meta charset="<?php bloginfo( 'charset' ); ?>"> |
||
| 3035 | <style> |
||
| 3036 | * { |
||
| 3037 | text-align: center; |
||
| 3038 | margin: 0; |
||
| 3039 | padding: 0; |
||
| 3040 | font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; |
||
| 3041 | } |
||
| 3042 | p { |
||
| 3043 | margin-top: 1em; |
||
| 3044 | font-size: 18px; |
||
| 3045 | } |
||
| 3046 | </style> |
||
| 3047 | <body> |
||
| 3048 | <p><?php echo esc_html( $message ); ?></p> |
||
| 3049 | </body> |
||
| 3050 | </html> |
||
| 3051 | <?php |
||
| 3052 | if ( $deactivate ) { |
||
| 3053 | $plugins = get_option( 'active_plugins' ); |
||
| 3054 | $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ); |
||
| 3055 | $update = false; |
||
| 3056 | foreach ( $plugins as $i => $plugin ) { |
||
| 3057 | if ( $plugin === $jetpack ) { |
||
| 3058 | $plugins[$i] = false; |
||
| 3059 | $update = true; |
||
| 3060 | } |
||
| 3061 | } |
||
| 3062 | |||
| 3063 | if ( $update ) { |
||
| 3064 | update_option( 'active_plugins', array_filter( $plugins ) ); |
||
| 3065 | } |
||
| 3066 | } |
||
| 3067 | exit; |
||
| 3068 | } |
||
| 3069 | |||
| 3070 | /** |
||
| 3071 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
| 3072 | * @static |
||
| 3073 | */ |
||
| 3074 | public static function plugin_activation( $network_wide ) { |
||
| 3075 | Jetpack_Options::update_option( 'activated', 1 ); |
||
| 3076 | |||
| 3077 | if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) { |
||
| 3078 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) ); |
||
| 3079 | } |
||
| 3080 | |||
| 3081 | if ( $network_wide ) |
||
| 3082 | Jetpack::state( 'network_nag', true ); |
||
| 3083 | |||
| 3084 | // For firing one-off events (notices) immediately after activation |
||
| 3085 | set_transient( 'activated_jetpack', true, .1 * MINUTE_IN_SECONDS ); |
||
| 3086 | |||
| 3087 | update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) ); |
||
| 3088 | |||
| 3089 | Jetpack::plugin_initialize(); |
||
| 3091 | |||
| 3092 | public static function get_activation_source( $referer_url ) { |
||
| 3141 | |||
| 3142 | /** |
||
| 3143 | * Runs before bumping version numbers up to a new version |
||
| 3144 | * @param string $version Version:timestamp |
||
| 3145 | * @param string $old_version Old Version:timestamp or false if not set yet. |
||
| 3146 | * @return null [description] |
||
| 3147 | */ |
||
| 3148 | public static function do_version_bump( $version, $old_version ) { |
||
| 3153 | |||
| 3154 | /** |
||
| 3155 | * Sets the internal version number and activation state. |
||
| 3156 | * @static |
||
| 3157 | */ |
||
| 3158 | public static function plugin_initialize() { |
||
| 3175 | |||
| 3176 | /** |
||
| 3177 | * Removes all connection options |
||
| 3178 | * @static |
||
| 3179 | */ |
||
| 3180 | public static function plugin_deactivation( ) { |
||
| 3189 | |||
| 3190 | /** |
||
| 3191 | * Disconnects from the Jetpack servers. |
||
| 3192 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 3193 | * @static |
||
| 3194 | */ |
||
| 3195 | public static function disconnect( $update_activated_state = true ) { |
||
| 3257 | |||
| 3258 | /** |
||
| 3259 | * Unlinks the current user from the linked WordPress.com user |
||
| 3260 | */ |
||
| 3261 | public static function unlink_user( $user_id = null ) { |
||
| 3292 | |||
| 3293 | /** |
||
| 3294 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
| 3295 | */ |
||
| 3296 | public static function try_registration() { |
||
| 3324 | |||
| 3325 | /** |
||
| 3326 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
| 3327 | * |
||
| 3328 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
| 3329 | */ |
||
| 3330 | public static function log( $code, $data = null ) { |
||
| 3370 | |||
| 3371 | /** |
||
| 3372 | * Get the internal event log. |
||
| 3373 | * |
||
| 3374 | * @param $event (string) - only return the specific log events |
||
| 3375 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
| 3376 | * |
||
| 3377 | * @return array of log events || WP_Error for invalid params |
||
| 3378 | */ |
||
| 3379 | public static function get_log( $event = false, $num = false ) { |
||
| 3415 | |||
| 3416 | /** |
||
| 3417 | * Log modification of important settings. |
||
| 3418 | */ |
||
| 3419 | public static function log_settings_change( $option, $old_value, $value ) { |
||
| 3426 | |||
| 3427 | /** |
||
| 3428 | * Return stat data for WPCOM sync |
||
| 3429 | */ |
||
| 3430 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
| 3444 | |||
| 3445 | /** |
||
| 3446 | * Get additional stat data to sync to WPCOM |
||
| 3447 | */ |
||
| 3448 | public static function get_additional_stat_data( $prefix = '' ) { |
||
| 3459 | |||
| 3460 | private static function get_site_user_count() { |
||
| 3475 | |||
| 3476 | /* Admin Pages */ |
||
| 3477 | |||
| 3478 | function admin_init() { |
||
| 3522 | |||
| 3523 | function admin_body_class( $admin_body_class = '' ) { |
||
| 3531 | |||
| 3532 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
| 3535 | |||
| 3536 | /** |
||
| 3537 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
| 3538 | * This function artificially throws errors for such cases (whitelisted). |
||
| 3539 | * |
||
| 3540 | * @param string $plugin The activated plugin. |
||
| 3541 | */ |
||
| 3542 | function throw_error_on_activate_plugin( $plugin ) { |
||
| 3566 | |||
| 3567 | function intercept_plugin_error_scrape_init() { |
||
| 3570 | |||
| 3571 | function intercept_plugin_error_scrape( $action, $result ) { |
||
| 3582 | |||
| 3583 | function add_remote_request_handlers() { |
||
| 3587 | |||
| 3588 | function remote_request_handlers() { |
||
| 3628 | |||
| 3629 | /** |
||
| 3630 | * Uploads a file gotten from the global $_FILES. |
||
| 3631 | * If `$update_media_item` is true and `post_id` is defined |
||
| 3632 | * the attachment file of the media item (gotten through of the post_id) |
||
| 3633 | * will be updated instead of add a new one. |
||
| 3634 | * |
||
| 3635 | * @param boolean $update_media_item - update media attachment |
||
| 3636 | * @return array - An array describing the uploadind files process |
||
| 3637 | */ |
||
| 3638 | function upload_handler( $update_media_item = false ) { |
||
| 3760 | |||
| 3761 | /** |
||
| 3762 | * Add help to the Jetpack page |
||
| 3763 | * |
||
| 3764 | * @since Jetpack (1.2.3) |
||
| 3765 | * @return false if not the Jetpack page |
||
| 3766 | */ |
||
| 3767 | function admin_help() { |
||
| 3808 | |||
| 3809 | function admin_menu_css() { |
||
| 3812 | |||
| 3813 | function admin_menu_order() { |
||
| 3816 | |||
| 3817 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 3832 | |||
| 3833 | function admin_banner_styles() { |
||
| 3854 | |||
| 3855 | function plugin_action_links( $actions ) { |
||
| 3870 | |||
| 3871 | /* |
||
| 3872 | * Registration flow: |
||
| 3873 | * 1 - ::admin_page_load() action=register |
||
| 3874 | * 2 - ::try_registration() |
||
| 3875 | * 3 - ::register() |
||
| 3876 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
| 3877 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
| 3878 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
| 3879 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
| 3880 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
| 3881 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
| 3882 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
| 3883 | * jetpack_id, jetpack_secret, jetpack_public |
||
| 3884 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
| 3885 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
| 3886 | * 5 - user logs in with WP.com account |
||
| 3887 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
| 3888 | * - Jetpack_Client_Server::authorize() |
||
| 3889 | * - Jetpack_Client_Server::get_token() |
||
| 3890 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
| 3891 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
| 3892 | * - which responds with access_token, token_type, scope |
||
| 3893 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
| 3894 | * - Jetpack::activate_default_modules() |
||
| 3895 | * - Deactivates deprecated plugins |
||
| 3896 | * - Activates all default modules |
||
| 3897 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
| 3898 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
| 3899 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
| 3900 | * Done! |
||
| 3901 | */ |
||
| 3902 | |||
| 3903 | /** |
||
| 3904 | * Handles the page load events for the Jetpack admin page |
||
| 3905 | */ |
||
| 3906 | function admin_page_load() { |
||
| 4183 | |||
| 4184 | function admin_notices() { |
||
| 4276 | |||
| 4277 | /** |
||
| 4278 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
| 4279 | */ |
||
| 4280 | function stat( $group, $detail ) { |
||
| 4285 | |||
| 4286 | /** |
||
| 4287 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
| 4288 | */ |
||
| 4289 | function do_stats( $method = '' ) { |
||
| 4304 | |||
| 4305 | /** |
||
| 4306 | * Runs stats code for a one-off, server-side. |
||
| 4307 | * |
||
| 4308 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
| 4309 | * |
||
| 4310 | * @return bool If it worked. |
||
| 4311 | */ |
||
| 4312 | static function do_server_side_stat( $args ) { |
||
| 4322 | |||
| 4323 | /** |
||
| 4324 | * Builds the stats url. |
||
| 4325 | * |
||
| 4326 | * @param $args array|string The arguments to append to the URL. |
||
| 4327 | * |
||
| 4328 | * @return string The URL to be pinged. |
||
| 4329 | */ |
||
| 4330 | static function build_stats_url( $args ) { |
||
| 4350 | |||
| 4351 | /** |
||
| 4352 | * Get the role of the current user. |
||
| 4353 | * |
||
| 4354 | * @deprecated 7.6 Use Automattic\Jetpack\Roles::translate_current_user_to_role() instead. |
||
| 4355 | * |
||
| 4356 | * @access public |
||
| 4357 | * @static |
||
| 4358 | * |
||
| 4359 | * @return string|boolean Current user's role, false if not enough capabilities for any of the roles. |
||
| 4360 | */ |
||
| 4361 | public static function translate_current_user_to_role() { |
||
| 4367 | |||
| 4368 | /** |
||
| 4369 | * Get the role of a particular user. |
||
| 4370 | * |
||
| 4371 | * @deprecated 7.6 Use Automattic\Jetpack\Roles::translate_user_to_role() instead. |
||
| 4372 | * |
||
| 4373 | * @access public |
||
| 4374 | * @static |
||
| 4375 | * |
||
| 4376 | * @param \WP_User $user User object. |
||
| 4377 | * @return string|boolean User's role, false if not enough capabilities for any of the roles. |
||
| 4378 | */ |
||
| 4379 | public static function translate_user_to_role( $user ) { |
||
| 4385 | |||
| 4386 | /** |
||
| 4387 | * Get the minimum capability for a role. |
||
| 4388 | * |
||
| 4389 | * @deprecated 7.6 Use Automattic\Jetpack\Roles::translate_role_to_cap() instead. |
||
| 4390 | * |
||
| 4391 | * @access public |
||
| 4392 | * @static |
||
| 4393 | * |
||
| 4394 | * @param string $role Role name. |
||
| 4395 | * @return string|boolean Capability, false if role isn't mapped to any capabilities. |
||
| 4396 | */ |
||
| 4397 | public static function translate_role_to_cap( $role ) { |
||
| 4403 | |||
| 4404 | static function sign_role( $role, $user_id = null ) { |
||
| 4420 | |||
| 4421 | |||
| 4422 | /** |
||
| 4423 | * Builds a URL to the Jetpack connection auth page |
||
| 4424 | * |
||
| 4425 | * @since 3.9.5 |
||
| 4426 | * |
||
| 4427 | * @param bool $raw If true, URL will not be escaped. |
||
| 4428 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 4429 | * If string, will be a custom redirect. |
||
| 4430 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 4431 | * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0 |
||
| 4432 | * |
||
| 4433 | * @return string Connect URL |
||
| 4434 | */ |
||
| 4435 | function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) { |
||
| 4566 | |||
| 4567 | /** |
||
| 4568 | * Get our assumed site creation date. |
||
| 4569 | * Calculated based on the earlier date of either: |
||
| 4570 | * - Earliest admin user registration date. |
||
| 4571 | * - Earliest date of post of any post type. |
||
| 4572 | * |
||
| 4573 | * @since 7.2.0 |
||
| 4574 | * |
||
| 4575 | * @return string Assumed site creation date and time. |
||
| 4576 | */ |
||
| 4577 | View Code Duplication | public static function get_assumed_site_creation_date() { |
|
| 4604 | |||
| 4605 | View Code Duplication | public static function apply_activation_source_to_args( &$args ) { |
|
| 4616 | |||
| 4617 | function build_reconnect_url( $raw = false ) { |
||
| 4621 | |||
| 4622 | public static function admin_url( $args = null ) { |
||
| 4627 | |||
| 4628 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
| 4632 | |||
| 4633 | function dismiss_jetpack_notice() { |
||
| 4665 | |||
| 4666 | public static function sort_modules( $a, $b ) { |
||
| 4672 | |||
| 4673 | function ajax_recheck_ssl() { |
||
| 4681 | |||
| 4682 | /* Client API */ |
||
| 4683 | |||
| 4684 | /** |
||
| 4685 | * Returns the requested Jetpack API URL |
||
| 4686 | * |
||
| 4687 | * @deprecated since 7.7 |
||
| 4688 | * @return string |
||
| 4689 | */ |
||
| 4690 | public static function api_url( $relative_url ) { |
||
| 4695 | |||
| 4696 | /** |
||
| 4697 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
| 4698 | */ |
||
| 4699 | public static function fix_url_for_bad_hosts( $url ) { |
||
| 4715 | |||
| 4716 | public static function verify_onboarding_token( $token_data, $token, $request_data ) { |
||
| 4761 | |||
| 4762 | /** |
||
| 4763 | * Create a random secret for validating onboarding payload |
||
| 4764 | * |
||
| 4765 | * @return string Secret token |
||
| 4766 | */ |
||
| 4767 | public static function create_onboarding_token() { |
||
| 4775 | |||
| 4776 | /** |
||
| 4777 | * Remove the onboarding token |
||
| 4778 | * |
||
| 4779 | * @return bool True on success, false on failure |
||
| 4780 | */ |
||
| 4781 | public static function invalidate_onboarding_token() { |
||
| 4784 | |||
| 4785 | /** |
||
| 4786 | * Validate an onboarding token for a specific action |
||
| 4787 | * |
||
| 4788 | * @return boolean True if token/action pair is accepted, false if not |
||
| 4789 | */ |
||
| 4790 | public static function validate_onboarding_token_action( $token, $action ) { |
||
| 4808 | |||
| 4809 | /** |
||
| 4810 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
| 4811 | * |
||
| 4812 | * @since 2.3.3 |
||
| 4813 | * @return boolean |
||
| 4814 | */ |
||
| 4815 | public static function permit_ssl( $force_recheck = false ) { |
||
| 4857 | |||
| 4858 | /* |
||
| 4859 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
| 4860 | */ |
||
| 4861 | public function alert_auto_ssl_fail() { |
||
| 4910 | |||
| 4911 | /** |
||
| 4912 | * Returns the Jetpack XML-RPC API |
||
| 4913 | * |
||
| 4914 | * @return string |
||
| 4915 | */ |
||
| 4916 | public static function xmlrpc_api_url() { |
||
| 4920 | |||
| 4921 | public static function connection() { |
||
| 4924 | |||
| 4925 | /** |
||
| 4926 | * Creates two secret tokens and the end of life timestamp for them. |
||
| 4927 | * |
||
| 4928 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
| 4929 | * |
||
| 4930 | * @since 2.6 |
||
| 4931 | * @return array |
||
| 4932 | */ |
||
| 4933 | public static function generate_secrets( $action, $user_id = false, $exp = 600 ) { |
||
| 4940 | |||
| 4941 | public static function get_secrets( $action, $user_id ) { |
||
| 4954 | |||
| 4955 | /** |
||
| 4956 | * @deprecated 7.5 Use Connection_Manager instead. |
||
| 4957 | * |
||
| 4958 | * @param $action |
||
| 4959 | * @param $user_id |
||
| 4960 | */ |
||
| 4961 | public static function delete_secrets( $action, $user_id ) { |
||
| 4964 | |||
| 4965 | /** |
||
| 4966 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 4967 | * |
||
| 4968 | * Based on local php max_execution_time in php.ini |
||
| 4969 | * |
||
| 4970 | * @since 2.6 |
||
| 4971 | * @return int |
||
| 4972 | * @deprecated |
||
| 4973 | **/ |
||
| 4974 | public function get_remote_query_timeout_limit() { |
||
| 4978 | |||
| 4979 | /** |
||
| 4980 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 4981 | * |
||
| 4982 | * Based on local php max_execution_time in php.ini |
||
| 4983 | * |
||
| 4984 | * @since 5.4 |
||
| 4985 | * @return int |
||
| 4986 | **/ |
||
| 4987 | public static function get_max_execution_time() { |
||
| 4996 | |||
| 4997 | /** |
||
| 4998 | * Sets a minimum request timeout, and returns the current timeout |
||
| 4999 | * |
||
| 5000 | * @since 5.4 |
||
| 5001 | **/ |
||
| 5002 | View Code Duplication | public static function set_min_time_limit( $min_timeout ) { |
|
| 5010 | |||
| 5011 | /** |
||
| 5012 | * Takes the response from the Jetpack register new site endpoint and |
||
| 5013 | * verifies it worked properly. |
||
| 5014 | * |
||
| 5015 | * @since 2.6 |
||
| 5016 | * @deprecated since 7.7.0 |
||
| 5017 | * @see Automattic\Jetpack\Connection\Manager::validate_remote_register_response() |
||
| 5018 | **/ |
||
| 5019 | public function validate_remote_register_response() { |
||
| 5022 | |||
| 5023 | /** |
||
| 5024 | * @return bool|WP_Error |
||
| 5025 | */ |
||
| 5026 | public static function register() { |
||
| 5043 | |||
| 5044 | /** |
||
| 5045 | * Filters the registration request body to include tracking properties. |
||
| 5046 | * |
||
| 5047 | * @param Array $properties |
||
| 5048 | * @return Array amended properties. |
||
| 5049 | */ |
||
| 5050 | public static function filter_register_request_body( $properties ) { |
||
| 5062 | |||
| 5063 | /** |
||
| 5064 | * If the db version is showing something other that what we've got now, bump it to current. |
||
| 5065 | * |
||
| 5066 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
| 5067 | */ |
||
| 5068 | public static function maybe_set_version_option() { |
||
| 5082 | |||
| 5083 | /* Client Server API */ |
||
| 5084 | |||
| 5085 | /** |
||
| 5086 | * Loads the Jetpack XML-RPC client |
||
| 5087 | */ |
||
| 5088 | public static function load_xml_rpc_client() { |
||
| 5092 | |||
| 5093 | /** |
||
| 5094 | * Resets the saved authentication state in between testing requests. |
||
| 5095 | */ |
||
| 5096 | public function reset_saved_auth_state() { |
||
| 5100 | |||
| 5101 | /** |
||
| 5102 | * Verifies the signature of the current request. |
||
| 5103 | * |
||
| 5104 | * @deprecated since 7.7.0 |
||
| 5105 | * @see Automattic\Jetpack\Connection\Manager::verify_xml_rpc_signature() |
||
| 5106 | * |
||
| 5107 | * @return false|array |
||
| 5108 | */ |
||
| 5109 | public function verify_xml_rpc_signature() { |
||
| 5113 | |||
| 5114 | /** |
||
| 5115 | * Verifies the signature of the current request. |
||
| 5116 | * |
||
| 5117 | * This function has side effects and should not be used. Instead, |
||
| 5118 | * use the memoized version `->verify_xml_rpc_signature()`. |
||
| 5119 | * |
||
| 5120 | * @deprecated since 7.7.0 |
||
| 5121 | * @see Automattic\Jetpack\Connection\Manager::internal_verify_xml_rpc_signature() |
||
| 5122 | * @internal |
||
| 5123 | */ |
||
| 5124 | private function internal_verify_xml_rpc_signature() { |
||
| 5127 | |||
| 5128 | /** |
||
| 5129 | * Authenticates XML-RPC and other requests from the Jetpack Server. |
||
| 5130 | * |
||
| 5131 | * @deprecated since 7.7.0 |
||
| 5132 | * @see Automattic\Jetpack\Connection\Manager::authenticate_jetpack() |
||
| 5133 | * |
||
| 5134 | * @param \WP_User|mixed $user User object if authenticated. |
||
| 5135 | * @param string $username Username. |
||
| 5136 | * @param string $password Password string. |
||
| 5137 | * @return \WP_User|mixed Authenticated user or error. |
||
| 5138 | */ |
||
| 5139 | public function authenticate_jetpack( $user, $username, $password ) { |
||
| 5143 | |||
| 5144 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
| 5145 | // Uses the existing XMLRPC request signing implementation. |
||
| 5146 | function wp_rest_authenticate( $user ) { |
||
| 5216 | |||
| 5217 | /** |
||
| 5218 | * Report authentication status to the WP REST API. |
||
| 5219 | * |
||
| 5220 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
| 5221 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
| 5222 | */ |
||
| 5223 | public function wp_rest_authentication_errors( $value ) { |
||
| 5229 | |||
| 5230 | /** |
||
| 5231 | * Add our nonce to this request. |
||
| 5232 | * |
||
| 5233 | * @deprecated since 7.7.0 |
||
| 5234 | * @see Automattic\Jetpack\Connection\Manager::add_nonce() |
||
| 5235 | * |
||
| 5236 | * @param int $timestamp Timestamp of the request. |
||
| 5237 | * @param string $nonce Nonce string. |
||
| 5238 | */ |
||
| 5239 | public function add_nonce( $timestamp, $nonce ) { |
||
| 5243 | |||
| 5244 | /** |
||
| 5245 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
| 5246 | * Capture it here so we can verify the signature later. |
||
| 5247 | * |
||
| 5248 | * @deprecated since 7.7.0 |
||
| 5249 | * @see Automattic\Jetpack\Connection\Manager::xmlrpc_methods() |
||
| 5250 | * |
||
| 5251 | * @param array $methods XMLRPC methods. |
||
| 5252 | * @return array XMLRPC methods, with the $HTTP_RAW_POST_DATA one. |
||
| 5253 | */ |
||
| 5254 | public function xmlrpc_methods( $methods ) { |
||
| 5258 | |||
| 5259 | /** |
||
| 5260 | * Register additional public XMLRPC methods. |
||
| 5261 | * |
||
| 5262 | * @deprecated since 7.7.0 |
||
| 5263 | * @see Automattic\Jetpack\Connection\Manager::public_xmlrpc_methods() |
||
| 5264 | * |
||
| 5265 | * @param array $methods Public XMLRPC methods. |
||
| 5266 | * @return array Public XMLRPC methods, with the getOptions one. |
||
| 5267 | */ |
||
| 5268 | public function public_xmlrpc_methods( $methods ) { |
||
| 5272 | |||
| 5273 | /** |
||
| 5274 | * Handles a getOptions XMLRPC method call. |
||
| 5275 | * |
||
| 5276 | * @deprecated since 7.7.0 |
||
| 5277 | * @see Automattic\Jetpack\Connection\Manager::jetpack_getOptions() |
||
| 5278 | * |
||
| 5279 | * @param array $args method call arguments. |
||
| 5280 | * @return array an amended XMLRPC server options array. |
||
| 5281 | */ |
||
| 5282 | public function jetpack_getOptions( $args ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
||
| 5286 | |||
| 5287 | /** |
||
| 5288 | * Adds Jetpack-specific options to the output of the XMLRPC options method. |
||
| 5289 | * |
||
| 5290 | * @deprecated since 7.7.0 |
||
| 5291 | * @see Automattic\Jetpack\Connection\Manager::xmlrpc_options() |
||
| 5292 | * |
||
| 5293 | * @param array $options Standard Core options. |
||
| 5294 | * @return array Amended options. |
||
| 5295 | */ |
||
| 5296 | public function xmlrpc_options( $options ) { |
||
| 5300 | |||
| 5301 | /** |
||
| 5302 | * Cleans nonces that were saved when calling ::add_nonce. |
||
| 5303 | * |
||
| 5304 | * @deprecated since 7.7.0 |
||
| 5305 | * @see Automattic\Jetpack\Connection\Manager::clean_nonces() |
||
| 5306 | * |
||
| 5307 | * @param bool $all whether to clean even non-expired nonces. |
||
| 5308 | */ |
||
| 5309 | public static function clean_nonces( $all = false ) { |
||
| 5313 | |||
| 5314 | /** |
||
| 5315 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
| 5316 | * SET: state( $key, $value ); |
||
| 5317 | * GET: $value = state( $key ); |
||
| 5318 | * |
||
| 5319 | * @param string $key |
||
| 5320 | * @param string $value |
||
| 5321 | * @param bool $restate private |
||
| 5322 | */ |
||
| 5323 | public static function state( $key = null, $value = null, $restate = false ) { |
||
| 5373 | |||
| 5374 | public static function restate() { |
||
| 5377 | |||
| 5378 | public static function check_privacy( $file ) { |
||
| 5413 | |||
| 5414 | /** |
||
| 5415 | * Helper method for multicall XMLRPC. |
||
| 5416 | */ |
||
| 5417 | public static function xmlrpc_async_call() { |
||
| 5459 | |||
| 5460 | public static function staticize_subdomain( $url ) { |
||
| 5495 | |||
| 5496 | /* JSON API Authorization */ |
||
| 5497 | |||
| 5498 | /** |
||
| 5499 | * Handles the login action for Authorizing the JSON API |
||
| 5500 | */ |
||
| 5501 | function login_form_json_api_authorization() { |
||
| 5510 | |||
| 5511 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
| 5512 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
| 5525 | |||
| 5526 | // Make sure the POSTed request is handled by the same action |
||
| 5527 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
| 5531 | |||
| 5532 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
| 5533 | function store_json_api_authorization_token( $user_login, $user ) { |
||
| 5539 | |||
| 5540 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
| 5541 | function allow_wpcom_public_api_domain( $domains ) { |
||
| 5545 | |||
| 5546 | static function is_redirect_encoded( $redirect_url ) { |
||
| 5549 | |||
| 5550 | // Add all wordpress.com environments to the safe redirect whitelist |
||
| 5551 | function allow_wpcom_environments( $domains ) { |
||
| 5558 | |||
| 5559 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
| 5560 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
| 5572 | |||
| 5573 | |||
| 5574 | /** |
||
| 5575 | * Verifies the request by checking the signature |
||
| 5576 | * |
||
| 5577 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
| 5578 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
| 5579 | * |
||
| 5580 | * @param null|array $environment |
||
| 5581 | */ |
||
| 5582 | function verify_json_api_authorization_request( $environment = null ) { |
||
| 5689 | |||
| 5690 | function login_message_json_api_authorization( $message ) { |
||
| 5696 | |||
| 5697 | /** |
||
| 5698 | * Get $content_width, but with a <s>twist</s> filter. |
||
| 5699 | */ |
||
| 5700 | public static function get_content_width() { |
||
| 5713 | |||
| 5714 | /** |
||
| 5715 | * Pings the WordPress.com Mirror Site for the specified options. |
||
| 5716 | * |
||
| 5717 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
| 5718 | * |
||
| 5719 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
| 5720 | */ |
||
| 5721 | public function get_cloud_site_options( $option_names ) { |
||
| 5737 | |||
| 5738 | /** |
||
| 5739 | * Checks if the site is currently in an identity crisis. |
||
| 5740 | * |
||
| 5741 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
| 5742 | */ |
||
| 5743 | public static function check_identity_crisis() { |
||
| 5750 | |||
| 5751 | /** |
||
| 5752 | * Checks whether the home and siteurl specifically are whitelisted |
||
| 5753 | * Written so that we don't have re-check $key and $value params every time |
||
| 5754 | * we want to check if this site is whitelisted, for example in footer.php |
||
| 5755 | * |
||
| 5756 | * @since 3.8.0 |
||
| 5757 | * @return bool True = already whitelisted False = not whitelisted |
||
| 5758 | */ |
||
| 5759 | public static function is_staging_site() { |
||
| 5819 | |||
| 5820 | /** |
||
| 5821 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
| 5822 | * |
||
| 5823 | * @since 4.4.0 |
||
| 5824 | * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC |
||
| 5825 | * |
||
| 5826 | * @return bool |
||
| 5827 | */ |
||
| 5828 | public static function validate_sync_error_idc_option() { |
||
| 5872 | |||
| 5873 | /** |
||
| 5874 | * Normalizes a url by doing three things: |
||
| 5875 | * - Strips protocol |
||
| 5876 | * - Strips www |
||
| 5877 | * - Adds a trailing slash |
||
| 5878 | * |
||
| 5879 | * @since 4.4.0 |
||
| 5880 | * @param string $url |
||
| 5881 | * @return WP_Error|string |
||
| 5882 | */ |
||
| 5883 | public static function normalize_url_protocol_agnostic( $url ) { |
||
| 5893 | |||
| 5894 | /** |
||
| 5895 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
| 5896 | * |
||
| 5897 | * @since 4.4.0 |
||
| 5898 | * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB |
||
| 5899 | * |
||
| 5900 | * @param array $response |
||
| 5901 | * @return array Array of the local urls, wpcom urls, and error code |
||
| 5902 | */ |
||
| 5903 | public static function get_sync_error_idc_option( $response = array() ) { |
||
| 5935 | |||
| 5936 | /** |
||
| 5937 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
| 5938 | * If set to true, the site will be put into staging mode. |
||
| 5939 | * |
||
| 5940 | * @since 4.3.2 |
||
| 5941 | * @return bool |
||
| 5942 | */ |
||
| 5943 | public static function sync_idc_optin() { |
||
| 5961 | |||
| 5962 | /** |
||
| 5963 | * Maybe Use a .min.css stylesheet, maybe not. |
||
| 5964 | * |
||
| 5965 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
| 5966 | */ |
||
| 5967 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
| 6009 | |||
| 6010 | /** |
||
| 6011 | * If the asset is minified, let's flag .min as the suffix. |
||
| 6012 | * |
||
| 6013 | * Attached to `style_loader_src` filter. |
||
| 6014 | * |
||
| 6015 | * @param string $tag The tag that would link to the external asset. |
||
| 6016 | * @param string $handle The registered handle of the script in question. |
||
| 6017 | * @param string $href The url of the asset in question. |
||
| 6018 | */ |
||
| 6019 | public static function set_suffix_on_min( $src, $handle ) { |
||
| 6035 | |||
| 6036 | /** |
||
| 6037 | * Maybe inlines a stylesheet. |
||
| 6038 | * |
||
| 6039 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
| 6040 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
| 6041 | * |
||
| 6042 | * Attached to `style_loader_tag` filter. |
||
| 6043 | * |
||
| 6044 | * @param string $tag The tag that would link to the external asset. |
||
| 6045 | * @param string $handle The registered handle of the script in question. |
||
| 6046 | * |
||
| 6047 | * @return string |
||
| 6048 | */ |
||
| 6049 | public static function maybe_inline_style( $tag, $handle ) { |
||
| 6099 | |||
| 6100 | /** |
||
| 6101 | * Loads a view file from the views |
||
| 6102 | * |
||
| 6103 | * Data passed in with the $data parameter will be available in the |
||
| 6104 | * template file as $data['value'] |
||
| 6105 | * |
||
| 6106 | * @param string $template - Template file to load |
||
| 6107 | * @param array $data - Any data to pass along to the template |
||
| 6108 | * @return boolean - If template file was found |
||
| 6109 | **/ |
||
| 6110 | public function load_view( $template, $data = array() ) { |
||
| 6121 | |||
| 6122 | /** |
||
| 6123 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
| 6124 | */ |
||
| 6125 | public function deprecated_hooks() { |
||
| 6194 | |||
| 6195 | /** |
||
| 6196 | * Converts any url in a stylesheet, to the correct absolute url. |
||
| 6197 | * |
||
| 6198 | * Considerations: |
||
| 6199 | * - Normal, relative URLs `feh.png` |
||
| 6200 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
| 6201 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
| 6202 | * - Absolute URLs `http://domain.com/feh.png` |
||
| 6203 | * - Domain root relative URLs `/feh.png` |
||
| 6204 | * |
||
| 6205 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
| 6206 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
| 6207 | * |
||
| 6208 | * @return mixed|string |
||
| 6209 | */ |
||
| 6210 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
| 6254 | |||
| 6255 | /** |
||
| 6256 | * This methods removes all of the registered css files on the front end |
||
| 6257 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
| 6258 | * all the files into one file. |
||
| 6259 | * |
||
| 6260 | * Pros: |
||
| 6261 | * - Uses only ONE css asset connection instead of 15 |
||
| 6262 | * - Saves a minimum of 56k |
||
| 6263 | * - Reduces server load |
||
| 6264 | * - Reduces time to first painted byte |
||
| 6265 | * |
||
| 6266 | * Cons: |
||
| 6267 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
| 6268 | * should not cause any issues with themes. |
||
| 6269 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
| 6270 | * jetpack_implode_frontend_css filter for a workaround |
||
| 6271 | * |
||
| 6272 | * For some situations developers may wish to disable css imploding and |
||
| 6273 | * instead operate in legacy mode where each file loads seperately and |
||
| 6274 | * can be edited individually or dequeued. This can be accomplished with |
||
| 6275 | * the following line: |
||
| 6276 | * |
||
| 6277 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
| 6278 | * |
||
| 6279 | * @since 3.2 |
||
| 6280 | **/ |
||
| 6281 | public function implode_frontend_css( $travis_test = false ) { |
||
| 6338 | |||
| 6339 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
| 6349 | |||
| 6350 | /* |
||
| 6351 | * Check the heartbeat data |
||
| 6352 | * |
||
| 6353 | * Organizes the heartbeat data by severity. For example, if the site |
||
| 6354 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
| 6355 | * |
||
| 6356 | * Data will be added to "caution" array, if it either: |
||
| 6357 | * - Out of date Jetpack version |
||
| 6358 | * - Out of date WP version |
||
| 6359 | * - Out of date PHP version |
||
| 6360 | * |
||
| 6361 | * $return array $filtered_data |
||
| 6362 | */ |
||
| 6363 | public static function jetpack_check_heartbeat_data() { |
||
| 6416 | |||
| 6417 | |||
| 6418 | /* |
||
| 6419 | * This method is used to organize all options that can be reset |
||
| 6420 | * without disconnecting Jetpack. |
||
| 6421 | * |
||
| 6422 | * It is used in class.jetpack-cli.php to reset options |
||
| 6423 | * |
||
| 6424 | * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat. |
||
| 6425 | * |
||
| 6426 | * @return array of options to delete. |
||
| 6427 | */ |
||
| 6428 | public static function get_jetpack_options_for_reset() { |
||
| 6431 | |||
| 6432 | /* |
||
| 6433 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
| 6434 | * so we can bring them directly to their site in calypso. |
||
| 6435 | * |
||
| 6436 | * @param string | url |
||
| 6437 | * @return string | url without the guff |
||
| 6438 | */ |
||
| 6439 | public static function build_raw_urls( $url ) { |
||
| 6445 | |||
| 6446 | /** |
||
| 6447 | * Stores and prints out domains to prefetch for page speed optimization. |
||
| 6448 | * |
||
| 6449 | * @param mixed $new_urls |
||
| 6450 | */ |
||
| 6451 | public static function dns_prefetch( $new_urls = null ) { |
||
| 6468 | |||
| 6469 | public function wp_dashboard_setup() { |
||
| 6503 | |||
| 6504 | /** |
||
| 6505 | * @param mixed $result Value for the user's option |
||
| 6506 | * @return mixed |
||
| 6507 | */ |
||
| 6508 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
| 6533 | |||
| 6534 | public static function dashboard_widget() { |
||
| 6542 | |||
| 6543 | public static function dashboard_widget_footer() { |
||
| 6576 | |||
| 6577 | /* |
||
| 6578 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
| 6579 | */ |
||
| 6580 | function jetpack_icon_user_connected( $columns ) { |
||
| 6584 | |||
| 6585 | /* |
||
| 6586 | * Show Jetpack icon if the user is linked. |
||
| 6587 | */ |
||
| 6588 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
| 6601 | |||
| 6602 | /* |
||
| 6603 | * Style the Jetpack user column |
||
| 6604 | */ |
||
| 6605 | function jetpack_user_col_style() { |
||
| 6622 | |||
| 6623 | /** |
||
| 6624 | * Checks if Akismet is active and working. |
||
| 6625 | * |
||
| 6626 | * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key |
||
| 6627 | * that implied usage of methods present since more recent version. |
||
| 6628 | * See https://github.com/Automattic/jetpack/pull/9585 |
||
| 6629 | * |
||
| 6630 | * @since 5.1.0 |
||
| 6631 | * |
||
| 6632 | * @return bool True = Akismet available. False = Aksimet not available. |
||
| 6633 | */ |
||
| 6634 | public static function is_akismet_active() { |
||
| 6669 | |||
| 6670 | /** |
||
| 6671 | * @deprecated |
||
| 6672 | * |
||
| 6673 | * @see Automattic\Jetpack\Sync\Modules\Users::is_function_in_backtrace |
||
| 6674 | */ |
||
| 6675 | public static function is_function_in_backtrace() { |
||
| 6678 | |||
| 6679 | /** |
||
| 6680 | * Given a minified path, and a non-minified path, will return |
||
| 6681 | * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy. |
||
| 6682 | * |
||
| 6683 | * Both `$min_base` and `$non_min_base` are expected to be relative to the |
||
| 6684 | * root Jetpack directory. |
||
| 6685 | * |
||
| 6686 | * @since 5.6.0 |
||
| 6687 | * |
||
| 6688 | * @param string $min_path |
||
| 6689 | * @param string $non_min_path |
||
| 6690 | * @return string The URL to the file |
||
| 6691 | */ |
||
| 6692 | public static function get_file_url_for_environment( $min_path, $non_min_path ) { |
||
| 6695 | |||
| 6696 | /** |
||
| 6697 | * Checks for whether Jetpack Backup & Scan is enabled. |
||
| 6698 | * Will return true if the state of Backup & Scan is anything except "unavailable". |
||
| 6699 | * @return bool|int|mixed |
||
| 6700 | */ |
||
| 6701 | public static function is_rewind_enabled() { |
||
| 6720 | |||
| 6721 | /** |
||
| 6722 | * Return Calypso environment value; used for developing Jetpack and pairing |
||
| 6723 | * it with different Calypso enrionments, such as localhost. |
||
| 6724 | * |
||
| 6725 | * @since 7.4.0 |
||
| 6726 | * |
||
| 6727 | * @return string Calypso environment |
||
| 6728 | */ |
||
| 6729 | public static function get_calypso_env() { |
||
| 6744 | |||
| 6745 | /** |
||
| 6746 | * Checks whether or not TOS has been agreed upon. |
||
| 6747 | * Will return true if a user has clicked to register, or is already connected. |
||
| 6748 | */ |
||
| 6749 | public static function jetpack_tos_agreed() { |
||
| 6752 | |||
| 6753 | /** |
||
| 6754 | * Handles activating default modules as well general cleanup for the new connection. |
||
| 6755 | * |
||
| 6756 | * @param boolean $activate_sso Whether to activate the SSO module when activating default modules. |
||
| 6757 | * @param boolean $redirect_on_activation_error Whether to redirect on activation error. |
||
| 6758 | * @param boolean $send_state_messages Whether to send state messages. |
||
| 6759 | * @return void |
||
| 6760 | */ |
||
| 6761 | public static function handle_post_authorization_actions( |
||
| 6790 | |||
| 6791 | /** |
||
| 6792 | * Returns a boolean for whether backups UI should be displayed or not. |
||
| 6793 | * |
||
| 6794 | * @return bool Should backups UI be displayed? |
||
| 6795 | */ |
||
| 6796 | public static function show_backups_ui() { |
||
| 6806 | |||
| 6807 | /* |
||
| 6808 | * Deprecated manage functions |
||
| 6809 | */ |
||
| 6810 | function prepare_manage_jetpack_notice() { |
||
| 6831 | |||
| 6832 | /** |
||
| 6833 | * Clean leftoveruser meta. |
||
| 6834 | * |
||
| 6835 | * Delete Jetpack-related user meta when it is no longer needed. |
||
| 6836 | * |
||
| 6837 | * @since 7.3.0 |
||
| 6838 | * |
||
| 6839 | * @param int $user_id User ID being updated. |
||
| 6840 | */ |
||
| 6841 | public static function user_meta_cleanup( $user_id ) { |
||
| 6856 | |||
| 6857 | function is_active_and_not_development_mode( $maybe ) { |
||
| 6863 | } |
||
| 6864 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.