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 |
||
| 25 | class Jetpack { |
||
| 26 | public $xmlrpc_server = null; |
||
| 27 | |||
| 28 | private $xmlrpc_verification = null; |
||
| 29 | private $rest_authentication_status = null; |
||
| 30 | |||
| 31 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array The handles of styles that are concatenated into jetpack.css |
||
| 35 | */ |
||
| 36 | public $concatenated_style_handles = array( |
||
| 37 | 'jetpack-carousel', |
||
| 38 | 'grunion.css', |
||
| 39 | 'the-neverending-homepage', |
||
| 40 | 'jetpack_likes', |
||
| 41 | 'jetpack_related-posts', |
||
| 42 | 'sharedaddy', |
||
| 43 | 'jetpack-slideshow', |
||
| 44 | 'presentations', |
||
| 45 | 'jetpack-subscriptions', |
||
| 46 | 'jetpack-responsive-videos-style', |
||
| 47 | 'jetpack-social-menu', |
||
| 48 | 'tiled-gallery', |
||
| 49 | 'jetpack_display_posts_widget', |
||
| 50 | 'gravatar-profile-widget', |
||
| 51 | 'goodreads-widget', |
||
| 52 | 'jetpack_social_media_icons_widget', |
||
| 53 | 'jetpack-top-posts-widget', |
||
| 54 | 'jetpack_image_widget', |
||
| 55 | 'jetpack-my-community-widget', |
||
| 56 | 'wordads', |
||
| 57 | ); |
||
| 58 | |||
| 59 | public $plugins_to_deactivate = array( |
||
| 60 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 61 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 62 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
| 63 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
| 64 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
| 65 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
| 66 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
| 67 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
| 68 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
| 69 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
| 70 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
| 71 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
| 72 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
| 73 | 'omnisearch' => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ), |
||
| 74 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
| 75 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
| 76 | ); |
||
| 77 | |||
| 78 | static $capability_translations = array( |
||
|
|
|||
| 79 | 'administrator' => 'manage_options', |
||
| 80 | 'editor' => 'edit_others_posts', |
||
| 81 | 'author' => 'publish_posts', |
||
| 82 | 'contributor' => 'edit_posts', |
||
| 83 | 'subscriber' => 'read', |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
| 88 | * if the plugins are active. Used by filter_default_modules |
||
| 89 | * |
||
| 90 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
| 91 | * change `module-slug` and add this to your plugin: |
||
| 92 | * |
||
| 93 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
| 94 | * function my_jetpack_get_default_modules( $modules ) { |
||
| 95 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
| 96 | * } |
||
| 97 | * |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | private $conflicting_plugins = array( |
||
| 101 | 'comments' => array( |
||
| 102 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
| 103 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
| 104 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
| 105 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
| 106 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
| 107 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
| 108 | ), |
||
| 109 | 'contact-form' => array( |
||
| 110 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
| 111 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
| 112 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
| 113 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
| 114 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
| 115 | ), |
||
| 116 | 'minileven' => array( |
||
| 117 | 'WPtouch' => 'wptouch/wptouch.php', |
||
| 118 | ), |
||
| 119 | 'latex' => array( |
||
| 120 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
| 121 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
| 122 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
| 123 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
| 124 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
| 125 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
| 126 | ), |
||
| 127 | 'protect' => array( |
||
| 128 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
| 129 | 'Captcha' => 'captcha/captcha.php', |
||
| 130 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
| 131 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
| 132 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
| 133 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
| 134 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
| 135 | 'Security-protection' => 'security-protection/security-protection.php', |
||
| 136 | 'Login Security' => 'login-security/login-security.php', |
||
| 137 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
| 138 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
| 139 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
| 140 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
| 141 | ), |
||
| 142 | 'random-redirect' => array( |
||
| 143 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
| 144 | ), |
||
| 145 | 'related-posts' => array( |
||
| 146 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
| 147 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
| 148 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
| 149 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
| 150 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
| 151 | 'outbrain' => 'outbrain/outbrain.php', |
||
| 152 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 153 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
| 154 | ), |
||
| 155 | 'sharedaddy' => array( |
||
| 156 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
| 157 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
| 158 | 'ShareThis' => 'share-this/sharethis.php', |
||
| 159 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 160 | ), |
||
| 161 | 'seo-tools' => array( |
||
| 162 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 163 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 164 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 165 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 166 | ), |
||
| 167 | 'verification-tools' => array( |
||
| 168 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 169 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 170 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 171 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 172 | ), |
||
| 173 | 'widget-visibility' => array( |
||
| 174 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
| 175 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
| 176 | ), |
||
| 177 | 'sitemaps' => array( |
||
| 178 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
| 179 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
| 180 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
| 181 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
| 182 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
| 183 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 184 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 185 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 186 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 187 | 'Sitemap' => 'sitemap/sitemap.php', |
||
| 188 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
| 189 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
| 190 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
| 191 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
| 192 | ), |
||
| 193 | ); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
| 197 | * |
||
| 198 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
| 199 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
| 200 | * |
||
| 201 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
| 202 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
| 203 | */ |
||
| 204 | private $open_graph_conflicting_plugins = array( |
||
| 205 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
| 206 | // 2 Click Social Media Buttons |
||
| 207 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
| 208 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
| 209 | 'autodescription/autodescription.php', // The SEO Framework |
||
| 210 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
| 211 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
| 212 | // Open Graph Meta Tags by Heateor |
||
| 213 | 'facebook/facebook.php', // Facebook (official plugin) |
||
| 214 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
| 215 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
| 216 | // Facebook Featured Image & OG Meta Tags |
||
| 217 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
| 218 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
| 219 | // Facebook Open Graph Meta Tags for WordPress |
||
| 220 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
| 221 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
| 222 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
| 223 | // Fedmich's Facebook Open Graph Meta |
||
| 224 | 'header-footer/plugin.php', // Header and Footer |
||
| 225 | 'network-publisher/networkpub.php', // Network Publisher |
||
| 226 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
| 227 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
| 228 | // NextScripts SNAP |
||
| 229 | 'og-tags/og-tags.php', // OG Tags |
||
| 230 | 'opengraph/opengraph.php', // Open Graph |
||
| 231 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
| 232 | // Open Graph Protocol Framework |
||
| 233 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
| 234 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
| 235 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
| 236 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
| 237 | 'sharepress/sharepress.php', // SharePress |
||
| 238 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
| 239 | 'social-discussions/social-discussions.php', // Social Discussions |
||
| 240 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
| 241 | 'socialize/socialize.php', // Socialize |
||
| 242 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
| 243 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
| 244 | // Tweet, Like, Google +1 and Share |
||
| 245 | 'wordbooker/wordbooker.php', // Wordbooker |
||
| 246 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
| 247 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
| 248 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
| 249 | // WP Facebook Like Send & Open Graph Meta |
||
| 250 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
| 251 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
| 252 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
| 253 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php' // WP Facebook Like Button |
||
| 254 | ); |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
| 258 | */ |
||
| 259 | private $twitter_cards_conflicting_plugins = array( |
||
| 260 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
| 261 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
| 262 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
| 263 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
| 264 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
| 265 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
| 266 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
| 267 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
| 268 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
| 269 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
| 270 | ); |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Message to display in admin_notice |
||
| 274 | * @var string |
||
| 275 | */ |
||
| 276 | public $message = ''; |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Error to display in admin_notice |
||
| 280 | * @var string |
||
| 281 | */ |
||
| 282 | public $error = ''; |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Modules that need more privacy description. |
||
| 286 | * @var string |
||
| 287 | */ |
||
| 288 | public $privacy_checks = ''; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Stats to record once the page loads |
||
| 292 | * |
||
| 293 | * @var array |
||
| 294 | */ |
||
| 295 | public $stats = array(); |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Jetpack_Sync object |
||
| 299 | */ |
||
| 300 | public $sync; |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Verified data for JSON authorization request |
||
| 304 | */ |
||
| 305 | public $json_api_authorization_request = array(); |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Holds the singleton instance of this class |
||
| 309 | * @since 2.3.3 |
||
| 310 | * @var Jetpack |
||
| 311 | */ |
||
| 312 | static $instance = false; |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Singleton |
||
| 316 | * @static |
||
| 317 | */ |
||
| 318 | public static function init() { |
||
| 319 | if ( ! self::$instance ) { |
||
| 320 | self::$instance = new Jetpack; |
||
| 321 | |||
| 322 | self::$instance->plugin_upgrade(); |
||
| 323 | } |
||
| 324 | |||
| 325 | return self::$instance; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Must never be called statically |
||
| 330 | */ |
||
| 331 | function plugin_upgrade() { |
||
| 332 | if ( Jetpack::is_active() ) { |
||
| 333 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
| 334 | if ( JETPACK__VERSION != $version ) { |
||
| 335 | |||
| 336 | // Check which active modules actually exist and remove others from active_modules list |
||
| 337 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
| 338 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
| 339 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
| 340 | Jetpack::update_active_modules( $modules ); |
||
| 341 | } |
||
| 342 | |||
| 343 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
| 344 | |||
| 345 | // Upgrade to 4.3.0 |
||
| 346 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
| 347 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
| 348 | } |
||
| 349 | |||
| 350 | Jetpack::maybe_set_version_option(); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | static function activate_manage( ) { |
||
| 356 | if ( did_action( 'init' ) || current_filter() == 'init' ) { |
||
| 357 | self::activate_module( 'manage', false, false ); |
||
| 358 | } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) { |
||
| 359 | add_action( 'init', array( __CLASS__, 'activate_manage' ) ); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | static function update_active_modules( $modules ) { |
||
| 364 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
| 365 | |||
| 366 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $modules ) ); |
||
| 367 | |||
| 368 | if ( is_array( $modules ) && is_array( $current_modules ) ) { |
||
| 369 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
| 370 | foreach( $new_active_modules as $module ) { |
||
| 371 | /** |
||
| 372 | * Fires when a specific module is activated. |
||
| 373 | * |
||
| 374 | * @since 1.9.0 |
||
| 375 | * |
||
| 376 | * @param string $module Module slug. |
||
| 377 | * @param boolean $success whether the module was activated. @since 4.2 |
||
| 378 | */ |
||
| 379 | do_action( 'jetpack_activate_module', $module, $success ); |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Fires when a module is activated. |
||
| 383 | * The dynamic part of the filter, $module, is the module slug. |
||
| 384 | * |
||
| 385 | * @since 1.9.0 |
||
| 386 | * |
||
| 387 | * @param string $module Module slug. |
||
| 388 | */ |
||
| 389 | do_action( "jetpack_activate_module_$module", $module ); |
||
| 390 | } |
||
| 391 | |||
| 392 | $new_deactive_modules = array_diff( $current_modules, $modules ); |
||
| 393 | foreach( $new_deactive_modules as $module ) { |
||
| 394 | /** |
||
| 395 | * Fired after a module has been deactivated. |
||
| 396 | * |
||
| 397 | * @since 4.2.0 |
||
| 398 | * |
||
| 399 | * @param string $module Module slug. |
||
| 400 | * @param boolean $success whether the module was deactivated. |
||
| 401 | */ |
||
| 402 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
| 403 | /** |
||
| 404 | * Fires when a module is deactivated. |
||
| 405 | * The dynamic part of the filter, $module, is the module slug. |
||
| 406 | * |
||
| 407 | * @since 1.9.0 |
||
| 408 | * |
||
| 409 | * @param string $module Module slug. |
||
| 410 | */ |
||
| 411 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | return $success; |
||
| 416 | } |
||
| 417 | |||
| 418 | static function delete_active_modules() { |
||
| 419 | self::update_active_modules( array() ); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Constructor. Initializes WordPress hooks |
||
| 424 | */ |
||
| 425 | private function __construct() { |
||
| 426 | /* |
||
| 427 | * Check for and alert any deprecated hooks |
||
| 428 | */ |
||
| 429 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
| 430 | |||
| 431 | |||
| 432 | /* |
||
| 433 | * Load things that should only be in Network Admin. |
||
| 434 | * |
||
| 435 | * For now blow away everything else until a more full |
||
| 436 | * understanding of what is needed at the network level is |
||
| 437 | * available |
||
| 438 | */ |
||
| 439 | if( is_multisite() ) { |
||
| 440 | Jetpack_Network::init(); |
||
| 441 | } |
||
| 442 | |||
| 443 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
| 444 | |||
| 445 | // Unlink user before deleting the user from .com |
||
| 446 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
| 447 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
| 448 | |||
| 449 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
| 450 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
| 451 | |||
| 452 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
| 453 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
| 454 | |||
| 455 | $this->require_jetpack_authentication(); |
||
| 456 | |||
| 457 | if ( Jetpack::is_active() ) { |
||
| 458 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
| 459 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 460 | |||
| 461 | $signed = $this->verify_xml_rpc_signature(); |
||
| 462 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
| 463 | // The actual API methods. |
||
| 464 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
| 465 | } else { |
||
| 466 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
| 467 | // active Jetpack connection, so that additional users can link their account. |
||
| 468 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
| 469 | } |
||
| 470 | } else { |
||
| 471 | // The bootstrap API methods. |
||
| 472 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
| 473 | } |
||
| 474 | |||
| 475 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
| 476 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
| 477 | } elseif ( is_admin() && isset( $_POST['action'] ) && 'jetpack_upload_file' == $_POST['action'] ) { |
||
| 478 | $this->require_jetpack_authentication(); |
||
| 479 | $this->add_remote_request_handlers(); |
||
| 480 | } else { |
||
| 481 | if ( Jetpack::is_active() ) { |
||
| 482 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
| 483 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | if ( Jetpack::is_active() ) { |
||
| 488 | Jetpack_Heartbeat::init(); |
||
| 489 | } |
||
| 490 | |||
| 491 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
| 492 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
| 493 | |||
| 494 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
| 495 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
| 496 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 497 | } |
||
| 498 | |||
| 499 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
| 500 | |||
| 501 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 502 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
| 503 | |||
| 504 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
| 505 | |||
| 506 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
| 507 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
| 508 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
| 509 | |||
| 510 | // returns HTTPS support status |
||
| 511 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
| 512 | |||
| 513 | // If any module option is updated before Jump Start is dismissed, hide Jump Start. |
||
| 514 | add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) ); |
||
| 515 | |||
| 516 | // JITM AJAX callback function |
||
| 517 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
| 518 | |||
| 519 | // Universal ajax callback for all tracking events triggered via js |
||
| 520 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
| 521 | |||
| 522 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
| 523 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 524 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 525 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 526 | |||
| 527 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
| 528 | |||
| 529 | /** |
||
| 530 | * These actions run checks to load additional files. |
||
| 531 | * They check for external files or plugins, so they need to run as late as possible. |
||
| 532 | */ |
||
| 533 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
| 534 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
| 535 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
| 536 | |||
| 537 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
| 538 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
| 539 | |||
| 540 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
| 541 | |||
| 542 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
| 543 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
| 544 | |||
| 545 | // A filter to control all just in time messages |
||
| 546 | add_filter( 'jetpack_just_in_time_msgs', '__return_true' ); |
||
| 547 | |||
| 548 | // Update the Jetpack plan from API on heartbeats |
||
| 549 | add_action( 'jetpack_heartbeat', array( $this, 'refresh_active_plan_from_wpcom' ) ); |
||
| 550 | |||
| 551 | /** |
||
| 552 | * This is the hack to concatinate all css files into one. |
||
| 553 | * For description and reasoning see the implode_frontend_css method |
||
| 554 | * |
||
| 555 | * Super late priority so we catch all the registered styles |
||
| 556 | */ |
||
| 557 | if( !is_admin() ) { |
||
| 558 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
| 559 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
| 560 | } |
||
| 561 | } |
||
| 562 | |||
| 563 | function jetpack_admin_ajax_tracks_callback() { |
||
| 564 | // Check for nonce |
||
| 565 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
| 566 | wp_die( 'Permissions check failed.' ); |
||
| 567 | } |
||
| 568 | |||
| 569 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
| 570 | wp_die( 'No valid event name or type.' ); |
||
| 571 | } |
||
| 572 | |||
| 573 | $tracks_data = array(); |
||
| 574 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
| 575 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
| 576 | } |
||
| 577 | |||
| 578 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
| 579 | wp_send_json_success(); |
||
| 580 | wp_die(); |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * The callback for the JITM ajax requests. |
||
| 585 | */ |
||
| 586 | function jetpack_jitm_ajax_callback() { |
||
| 587 | // Check for nonce |
||
| 588 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
| 589 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
| 590 | } |
||
| 591 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
| 592 | $module_slug = $_REQUEST['jitmModule']; |
||
| 593 | Jetpack::log( 'activate', $module_slug ); |
||
| 594 | Jetpack::activate_module( $module_slug, false, false ); |
||
| 595 | Jetpack::state( 'message', 'no_message' ); |
||
| 596 | |||
| 597 | //A Jetpack module is being activated through a JITM, track it |
||
| 598 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
| 599 | $this->do_stats( 'server_side' ); |
||
| 600 | |||
| 601 | wp_send_json_success(); |
||
| 602 | } |
||
| 603 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
| 604 | // get the hide_jitm options array |
||
| 605 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
| 606 | $jitm_name = $_REQUEST['jitmName']; |
||
| 607 | |||
| 608 | if( ! $jetpack_hide_jitm || ! is_array( $jetpack_hide_jitm ) ) { |
||
| 609 | $jetpack_hide_jitm = array( |
||
| 610 | $jitm_name => array ( |
||
| 611 | 'last_dismissed' => time(), |
||
| 612 | 'dismiss_count' => 1 |
||
| 613 | ) ); |
||
| 614 | } elseif ( ! is_array( $jetpack_hide_jitm[ $jitm_name ] ) ) { |
||
| 615 | $jetpack_hide_jitm[ $jitm_name ] = array ( |
||
| 616 | 'last_dismissed' => time(), |
||
| 617 | 'dismiss_count' => 1 |
||
| 618 | ); |
||
| 619 | } else { |
||
| 620 | $jetpack_hide_jitm[ $jitm_name ]['last_dismissed'] = time(); |
||
| 621 | $jetpack_hide_jitm[ $jitm_name ]['dismiss_count']++; |
||
| 622 | } |
||
| 623 | |||
| 624 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
| 625 | |||
| 626 | //jitm is being dismissed forever, track it |
||
| 627 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
| 628 | $this->do_stats( 'server_side' ); |
||
| 629 | |||
| 630 | wp_send_json_success(); |
||
| 631 | } |
||
| 632 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
| 633 | $module_slug = $_REQUEST['jitmModule']; |
||
| 634 | |||
| 635 | // User went to WordPress.com, track this |
||
| 636 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
| 637 | $this->do_stats( 'server_side' ); |
||
| 638 | |||
| 639 | wp_send_json_success(); |
||
| 640 | } |
||
| 641 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
| 642 | $track = $_REQUEST['jitmModule']; |
||
| 643 | |||
| 644 | // User is viewing JITM, track it. |
||
| 645 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
| 646 | $this->do_stats( 'server_side' ); |
||
| 647 | |||
| 648 | wp_send_json_success(); |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
| 654 | */ |
||
| 655 | function __destruct() { |
||
| 656 | if ( ! empty( $this->stats ) ) { |
||
| 657 | $this->do_stats( 'server_side' ); |
||
| 658 | } |
||
| 659 | } |
||
| 660 | |||
| 661 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
| 662 | switch( $cap ) { |
||
| 663 | case 'jetpack_connect' : |
||
| 664 | case 'jetpack_reconnect' : |
||
| 665 | if ( Jetpack::is_development_mode() ) { |
||
| 666 | $caps = array( 'do_not_allow' ); |
||
| 667 | break; |
||
| 668 | } |
||
| 669 | /** |
||
| 670 | * Pass through. If it's not development mode, these should match disconnect. |
||
| 671 | * Let users disconnect if it's development mode, just in case things glitch. |
||
| 672 | */ |
||
| 673 | case 'jetpack_disconnect' : |
||
| 674 | /** |
||
| 675 | * In multisite, can individual site admins manage their own connection? |
||
| 676 | * |
||
| 677 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
| 678 | */ |
||
| 679 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
| 680 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
| 681 | /** |
||
| 682 | * We need to update the option name -- it's terribly unclear which |
||
| 683 | * direction the override goes. |
||
| 684 | * |
||
| 685 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
| 686 | */ |
||
| 687 | $caps = array( 'do_not_allow' ); |
||
| 688 | break; |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | $caps = array( 'manage_options' ); |
||
| 693 | break; |
||
| 694 | case 'jetpack_manage_modules' : |
||
| 695 | case 'jetpack_activate_modules' : |
||
| 696 | case 'jetpack_deactivate_modules' : |
||
| 697 | $caps = array( 'manage_options' ); |
||
| 698 | break; |
||
| 699 | case 'jetpack_configure_modules' : |
||
| 700 | $caps = array( 'manage_options' ); |
||
| 701 | break; |
||
| 702 | case 'jetpack_network_admin_page': |
||
| 703 | case 'jetpack_network_settings_page': |
||
| 704 | $caps = array( 'manage_network_plugins' ); |
||
| 705 | break; |
||
| 706 | case 'jetpack_network_sites_page': |
||
| 707 | $caps = array( 'manage_sites' ); |
||
| 708 | break; |
||
| 709 | case 'jetpack_admin_page' : |
||
| 710 | if ( Jetpack::is_development_mode() ) { |
||
| 711 | $caps = array( 'manage_options' ); |
||
| 712 | break; |
||
| 713 | } else { |
||
| 714 | $caps = array( 'read' ); |
||
| 715 | } |
||
| 716 | break; |
||
| 717 | case 'jetpack_connect_user' : |
||
| 718 | if ( Jetpack::is_development_mode() ) { |
||
| 719 | $caps = array( 'do_not_allow' ); |
||
| 720 | break; |
||
| 721 | } |
||
| 722 | $caps = array( 'read' ); |
||
| 723 | break; |
||
| 724 | } |
||
| 725 | return $caps; |
||
| 726 | } |
||
| 727 | |||
| 728 | function require_jetpack_authentication() { |
||
| 729 | // Don't let anyone authenticate |
||
| 730 | $_COOKIE = array(); |
||
| 731 | remove_all_filters( 'authenticate' ); |
||
| 732 | remove_all_actions( 'wp_login_failed' ); |
||
| 733 | |||
| 734 | if ( Jetpack::is_active() ) { |
||
| 735 | // Allow Jetpack authentication |
||
| 736 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
| 737 | } |
||
| 738 | } |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Load language files |
||
| 742 | * @action plugins_loaded |
||
| 743 | */ |
||
| 744 | public static function plugin_textdomain() { |
||
| 745 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
| 746 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Register assets for use in various modules and the Jetpack admin page. |
||
| 751 | * |
||
| 752 | * @uses wp_script_is, wp_register_script, plugins_url |
||
| 753 | * @action wp_loaded |
||
| 754 | * @return null |
||
| 755 | */ |
||
| 756 | public function register_assets() { |
||
| 757 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
| 758 | wp_register_script( 'spin', plugins_url( '_inc/spin.js', JETPACK__PLUGIN_FILE ), false, '1.3' ); |
||
| 759 | } |
||
| 760 | |||
| 761 | View Code Duplication | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
|
| 762 | wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' ); |
||
| 763 | } |
||
| 764 | |||
| 765 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
| 766 | wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' ); |
||
| 767 | } |
||
| 768 | |||
| 769 | View Code Duplication | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
|
| 770 | wp_register_script( 'jetpack-twitter-timeline', plugins_url( '_inc/twitter-timeline.js', JETPACK__PLUGIN_FILE ) , array( 'jquery' ), '4.0.0', true ); |
||
| 771 | } |
||
| 772 | |||
| 773 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
| 774 | wp_register_script( 'jetpack-facebook-embed', plugins_url( '_inc/facebook-embed.js', __FILE__ ), array( 'jquery' ), null, true ); |
||
| 775 | |||
| 776 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
| 777 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
| 778 | if ( ! is_numeric( $fb_app_id ) ) { |
||
| 779 | $fb_app_id = ''; |
||
| 780 | } |
||
| 781 | wp_localize_script( |
||
| 782 | 'jetpack-facebook-embed', |
||
| 783 | 'jpfbembed', |
||
| 784 | array( |
||
| 785 | 'appid' => $fb_app_id, |
||
| 786 | 'locale' => $this->get_locale(), |
||
| 787 | ) |
||
| 788 | ); |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * As jetpack_register_genericons is by default fired off a hook, |
||
| 793 | * the hook may have already fired by this point. |
||
| 794 | * So, let's just trigger it manually. |
||
| 795 | */ |
||
| 796 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
| 797 | jetpack_register_genericons(); |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Register the social logos |
||
| 801 | */ |
||
| 802 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
| 803 | jetpack_register_social_logos(); |
||
| 804 | |||
| 805 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
| 806 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
| 807 | } |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Guess locale from language code. |
||
| 811 | * |
||
| 812 | * @param string $lang Language code. |
||
| 813 | * @return string|bool |
||
| 814 | */ |
||
| 815 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
| 816 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
| 817 | return 'en_US'; |
||
| 818 | } |
||
| 819 | |||
| 820 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
| 821 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 822 | return false; |
||
| 823 | } |
||
| 824 | |||
| 825 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 826 | } |
||
| 827 | |||
| 828 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 829 | // WP.com: get_locale() returns 'it' |
||
| 830 | $locale = GP_Locales::by_slug( $lang ); |
||
| 831 | } else { |
||
| 832 | // Jetpack: get_locale() returns 'it_IT'; |
||
| 833 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
| 834 | } |
||
| 835 | |||
| 836 | if ( ! $locale ) { |
||
| 837 | return false; |
||
| 838 | } |
||
| 839 | |||
| 840 | if ( empty( $locale->facebook_locale ) ) { |
||
| 841 | if ( empty( $locale->wp_locale ) ) { |
||
| 842 | return false; |
||
| 843 | } else { |
||
| 844 | // Facebook SDK is smart enough to fall back to en_US if a |
||
| 845 | // locale isn't supported. Since supported Facebook locales |
||
| 846 | // can fall out of sync, we'll attempt to use the known |
||
| 847 | // wp_locale value and rely on said fallback. |
||
| 848 | return $locale->wp_locale; |
||
| 849 | } |
||
| 850 | } |
||
| 851 | |||
| 852 | return $locale->facebook_locale; |
||
| 853 | } |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Get the locale. |
||
| 857 | * |
||
| 858 | * @return string|bool |
||
| 859 | */ |
||
| 860 | function get_locale() { |
||
| 861 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
| 862 | |||
| 863 | if ( ! $locale ) { |
||
| 864 | $locale = 'en_US'; |
||
| 865 | } |
||
| 866 | |||
| 867 | return $locale; |
||
| 868 | } |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Device Pixels support |
||
| 872 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
| 873 | */ |
||
| 874 | function devicepx() { |
||
| 875 | if ( Jetpack::is_active() ) { |
||
| 876 | wp_enqueue_script( 'devicepx', set_url_scheme( 'http://s0.wp.com/wp-content/js/devicepx-jetpack.js' ), array(), gmdate( 'oW' ), true ); |
||
| 877 | } |
||
| 878 | } |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
| 882 | * @param bool $option |
||
| 883 | * @return string |
||
| 884 | */ |
||
| 885 | public function jetpack_main_network_site_option( $option ) { |
||
| 886 | return network_site_url(); |
||
| 887 | } |
||
| 888 | /** |
||
| 889 | * Network Name. |
||
| 890 | */ |
||
| 891 | static function network_name( $option = null ) { |
||
| 892 | global $current_site; |
||
| 893 | return $current_site->site_name; |
||
| 894 | } |
||
| 895 | /** |
||
| 896 | * Does the network allow new user and site registrations. |
||
| 897 | * @return string |
||
| 898 | */ |
||
| 899 | static function network_allow_new_registrations( $option = null ) { |
||
| 900 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
| 901 | } |
||
| 902 | /** |
||
| 903 | * Does the network allow admins to add new users. |
||
| 904 | * @return boolian |
||
| 905 | */ |
||
| 906 | static function network_add_new_users( $option = null ) { |
||
| 907 | return (bool) get_site_option( 'add_new_users' ); |
||
| 908 | } |
||
| 909 | /** |
||
| 910 | * File upload psace left per site in MB. |
||
| 911 | * -1 means NO LIMIT. |
||
| 912 | * @return number |
||
| 913 | */ |
||
| 914 | static function network_site_upload_space( $option = null ) { |
||
| 915 | // value in MB |
||
| 916 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Network allowed file types. |
||
| 921 | * @return string |
||
| 922 | */ |
||
| 923 | static function network_upload_file_types( $option = null ) { |
||
| 924 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Maximum file upload size set by the network. |
||
| 929 | * @return number |
||
| 930 | */ |
||
| 931 | static function network_max_upload_file_size( $option = null ) { |
||
| 932 | // value in KB |
||
| 933 | return get_site_option( 'fileupload_maxk', 300 ); |
||
| 934 | } |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Lets us know if a site allows admins to manage the network. |
||
| 938 | * @return array |
||
| 939 | */ |
||
| 940 | static function network_enable_administration_menus( $option = null ) { |
||
| 941 | return get_site_option( 'menu_items' ); |
||
| 942 | } |
||
| 943 | |||
| 944 | /** |
||
| 945 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
| 946 | * jetpack_other_linked_admins transient. |
||
| 947 | * |
||
| 948 | * @since 4.3.2 |
||
| 949 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
| 950 | * |
||
| 951 | * @param int $user_id The user ID whose role changed. |
||
| 952 | * @param string $role The new role. |
||
| 953 | * @param array $old_roles An array of the user's previous roles. |
||
| 954 | */ |
||
| 955 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
| 956 | if ( 'administrator' == $role |
||
| 957 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
| 958 | || is_null( $old_roles ) |
||
| 959 | ) { |
||
| 960 | delete_transient( 'jetpack_other_linked_admins' ); |
||
| 961 | } |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Checks to see if there are any other users available to become primary |
||
| 966 | * Users must both: |
||
| 967 | * - Be linked to wpcom |
||
| 968 | * - Be an admin |
||
| 969 | * |
||
| 970 | * @return mixed False if no other users are linked, Int if there are. |
||
| 971 | */ |
||
| 972 | static function get_other_linked_admins() { |
||
| 973 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
| 974 | |||
| 975 | if ( false === $other_linked_users ) { |
||
| 976 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
| 977 | if ( count( $admins ) > 1 ) { |
||
| 978 | $available = array(); |
||
| 979 | foreach ( $admins as $admin ) { |
||
| 980 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
| 981 | $available[] = $admin->ID; |
||
| 982 | } |
||
| 983 | } |
||
| 984 | |||
| 985 | $count_connected_admins = count( $available ); |
||
| 986 | if ( count( $available ) > 1 ) { |
||
| 987 | $other_linked_users = $count_connected_admins; |
||
| 988 | } else { |
||
| 989 | $other_linked_users = 0; |
||
| 990 | } |
||
| 991 | } else { |
||
| 992 | $other_linked_users = 0; |
||
| 993 | } |
||
| 994 | |||
| 995 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
| 996 | } |
||
| 997 | |||
| 998 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
| 999 | } |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Return whether we are dealing with a multi network setup or not. |
||
| 1003 | * The reason we are type casting this is because we want to avoid the situation where |
||
| 1004 | * the result is false since when is_main_network_option return false it cases |
||
| 1005 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
| 1006 | * database which could be set to anything as opposed to what this function returns. |
||
| 1007 | * @param bool $option |
||
| 1008 | * |
||
| 1009 | * @return boolean |
||
| 1010 | */ |
||
| 1011 | public function is_main_network_option( $option ) { |
||
| 1012 | // return '1' or '' |
||
| 1013 | return (string) (bool) Jetpack::is_multi_network(); |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
| 1018 | * |
||
| 1019 | * @param string $option |
||
| 1020 | * @return boolean |
||
| 1021 | */ |
||
| 1022 | public function is_multisite( $option ) { |
||
| 1023 | return (string) (bool) is_multisite(); |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * Implemented since there is no core is multi network function |
||
| 1028 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
| 1029 | * |
||
| 1030 | * @since 3.3 |
||
| 1031 | * @return boolean |
||
| 1032 | */ |
||
| 1033 | public static function is_multi_network() { |
||
| 1034 | global $wpdb; |
||
| 1035 | |||
| 1036 | // if we don't have a multi site setup no need to do any more |
||
| 1037 | if ( ! is_multisite() ) { |
||
| 1038 | return false; |
||
| 1039 | } |
||
| 1040 | |||
| 1041 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
| 1042 | if ( $num_sites > 1 ) { |
||
| 1043 | return true; |
||
| 1044 | } else { |
||
| 1045 | return false; |
||
| 1046 | } |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
| 1051 | * @return null |
||
| 1052 | */ |
||
| 1053 | function update_jetpack_main_network_site_option() { |
||
| 1054 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1055 | } |
||
| 1056 | /** |
||
| 1057 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
| 1058 | * |
||
| 1059 | */ |
||
| 1060 | function update_jetpack_network_settings() { |
||
| 1061 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1062 | // Only sync this info for the main network site. |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Get back if the current site is single user site. |
||
| 1067 | * |
||
| 1068 | * @return bool |
||
| 1069 | */ |
||
| 1070 | public static function is_single_user_site() { |
||
| 1071 | global $wpdb; |
||
| 1072 | |||
| 1073 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
| 1074 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
| 1075 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
| 1076 | } |
||
| 1077 | return 1 === (int) $some_users; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Returns true if the site has file write access false otherwise. |
||
| 1082 | * @return string ( '1' | '0' ) |
||
| 1083 | **/ |
||
| 1084 | public static function file_system_write_access() { |
||
| 1085 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 1086 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
| 1090 | |||
| 1091 | $filesystem_method = get_filesystem_method(); |
||
| 1092 | if ( $filesystem_method === 'direct' ) { |
||
| 1093 | return 1; |
||
| 1094 | } |
||
| 1095 | |||
| 1096 | ob_start(); |
||
| 1097 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 1098 | ob_end_clean(); |
||
| 1099 | if ( $filesystem_credentials_are_stored ) { |
||
| 1100 | return 1; |
||
| 1101 | } |
||
| 1102 | return 0; |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Finds out if a site is using a version control system. |
||
| 1107 | * @return string ( '1' | '0' ) |
||
| 1108 | **/ |
||
| 1109 | public static function is_version_controlled() { |
||
| 1110 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
| 1111 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Determines whether the current theme supports featured images or not. |
||
| 1116 | * @return string ( '1' | '0' ) |
||
| 1117 | */ |
||
| 1118 | public static function featured_images_enabled() { |
||
| 1119 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1120 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * jetpack_updates is saved in the following schema: |
||
| 1125 | * |
||
| 1126 | * array ( |
||
| 1127 | * 'plugins' => (int) Number of plugin updates available. |
||
| 1128 | * 'themes' => (int) Number of theme updates available. |
||
| 1129 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
| 1130 | * 'translations' => (int) Number of translation updates available. |
||
| 1131 | * 'total' => (int) Total of all available updates. |
||
| 1132 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
| 1133 | * ) |
||
| 1134 | * @return array |
||
| 1135 | */ |
||
| 1136 | public static function get_updates() { |
||
| 1137 | $update_data = wp_get_update_data(); |
||
| 1138 | |||
| 1139 | // Stores the individual update counts as well as the total count. |
||
| 1140 | if ( isset( $update_data['counts'] ) ) { |
||
| 1141 | $updates = $update_data['counts']; |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | // If we need to update WordPress core, let's find the latest version number. |
||
| 1145 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
| 1146 | $cur = get_preferred_from_update_core(); |
||
| 1147 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
| 1148 | $updates['wp_update_version'] = $cur->current; |
||
| 1149 | } |
||
| 1150 | } |
||
| 1151 | return isset( $updates ) ? $updates : array(); |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | public static function get_update_details() { |
||
| 1155 | $update_details = array( |
||
| 1156 | 'update_core' => get_site_transient( 'update_core' ), |
||
| 1157 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
| 1158 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
| 1159 | ); |
||
| 1160 | return $update_details; |
||
| 1161 | } |
||
| 1162 | |||
| 1163 | public static function refresh_update_data() { |
||
| 1164 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1165 | |||
| 1166 | } |
||
| 1167 | |||
| 1168 | public static function refresh_theme_data() { |
||
| 1169 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1170 | } |
||
| 1171 | |||
| 1172 | /** |
||
| 1173 | * Is Jetpack active? |
||
| 1174 | */ |
||
| 1175 | public static function is_active() { |
||
| 1176 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Make an API call to WordPress.com for plan status |
||
| 1181 | * |
||
| 1182 | * @uses Jetpack_Options::get_option() |
||
| 1183 | * @uses Jetpack_Client::wpcom_json_api_request_as_blog() |
||
| 1184 | * @uses update_option() |
||
| 1185 | * |
||
| 1186 | * @access public |
||
| 1187 | * @static |
||
| 1188 | * |
||
| 1189 | * @return bool True if plan is updated, false if no update |
||
| 1190 | */ |
||
| 1191 | public static function refresh_active_plan_from_wpcom() { |
||
| 1192 | // Make the API request |
||
| 1193 | $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) ); |
||
| 1194 | $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' ); |
||
| 1195 | |||
| 1196 | // Bail if there was an error or malformed response |
||
| 1197 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
||
| 1198 | return false; |
||
| 1199 | } |
||
| 1200 | |||
| 1201 | // Decode the results |
||
| 1202 | $results = json_decode( $response['body'], true ); |
||
| 1203 | |||
| 1204 | // Bail if there were no results or plan details returned |
||
| 1205 | if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) { |
||
| 1206 | return false; |
||
| 1207 | } |
||
| 1208 | |||
| 1209 | // Store the option and return true if updated |
||
| 1210 | return update_option( 'jetpack_active_plan', $results['plan'] ); |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Get the plan that this Jetpack site is currently using |
||
| 1215 | * |
||
| 1216 | * @uses get_option() |
||
| 1217 | * |
||
| 1218 | * @access public |
||
| 1219 | * @static |
||
| 1220 | * |
||
| 1221 | * @return array Active Jetpack plan details |
||
| 1222 | */ |
||
| 1223 | public static function get_active_plan() { |
||
| 1224 | $plan = get_option( 'jetpack_active_plan' ); |
||
| 1225 | |||
| 1226 | // Set the default options |
||
| 1227 | if ( ! $plan ) { |
||
| 1228 | $plan = array( |
||
| 1229 | 'product_slug' => 'jetpack_free', |
||
| 1230 | 'supports' => array(), |
||
| 1231 | ); |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | // Define what paid modules are supported by personal plans |
||
| 1235 | $personal_plans = array( |
||
| 1236 | 'jetpack_personal', |
||
| 1237 | 'jetpack_personal_monthly', |
||
| 1238 | ); |
||
| 1239 | |||
| 1240 | if ( in_array( $plan['product_slug'], $personal_plans ) ) { |
||
| 1241 | $plan['supports'] = array( |
||
| 1242 | 'akismet', |
||
| 1243 | ); |
||
| 1244 | } |
||
| 1245 | |||
| 1246 | // Define what paid modules are supported by premium plans |
||
| 1247 | $premium_plans = array( |
||
| 1248 | 'jetpack_premium', |
||
| 1249 | 'jetpack_premium_monthly', |
||
| 1250 | ); |
||
| 1251 | |||
| 1252 | View Code Duplication | if ( in_array( $plan['product_slug'], $premium_plans ) ) { |
|
| 1253 | $plan['supports'] = array( |
||
| 1254 | 'videopress', |
||
| 1255 | 'akismet', |
||
| 1256 | 'vaultpress', |
||
| 1257 | ); |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | // Define what paid modules are supported by professional plans |
||
| 1261 | $business_plans = array( |
||
| 1262 | 'jetpack_business', |
||
| 1263 | 'jetpack_business_monthly', |
||
| 1264 | ); |
||
| 1265 | |||
| 1266 | View Code Duplication | if ( in_array( $plan['product_slug'], $business_plans ) ) { |
|
| 1267 | $plan['supports'] = array( |
||
| 1268 | 'videopress', |
||
| 1269 | 'akismet', |
||
| 1270 | 'vaultpress', |
||
| 1271 | 'seo-tools', |
||
| 1272 | ); |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | // Make sure we have an array here in the event database data is stale |
||
| 1276 | if ( ! isset( $plan['supports'] ) ) { |
||
| 1277 | $plan['supports'] = array(); |
||
| 1278 | } |
||
| 1279 | |||
| 1280 | return $plan; |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Determine whether the active plan supports a particular feature |
||
| 1285 | * |
||
| 1286 | * @uses Jetpack::get_active_plan() |
||
| 1287 | * |
||
| 1288 | * @access public |
||
| 1289 | * @static |
||
| 1290 | * |
||
| 1291 | * @return bool True if plan supports feature, false if not |
||
| 1292 | */ |
||
| 1293 | public static function active_plan_supports( $feature ) { |
||
| 1294 | $plan = Jetpack::get_active_plan(); |
||
| 1295 | |||
| 1296 | if ( in_array( $feature, $plan['supports'] ) ) { |
||
| 1297 | return true; |
||
| 1298 | } |
||
| 1299 | |||
| 1300 | return false; |
||
| 1301 | } |
||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Is Jetpack in development (offline) mode? |
||
| 1305 | */ |
||
| 1306 | public static function is_development_mode() { |
||
| 1307 | $development_mode = false; |
||
| 1308 | |||
| 1309 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
| 1310 | $development_mode = JETPACK_DEV_DEBUG; |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
| 1314 | $development_mode = true; |
||
| 1315 | } |
||
| 1316 | /** |
||
| 1317 | * Filters Jetpack's development mode. |
||
| 1318 | * |
||
| 1319 | * @see https://jetpack.com/support/development-mode/ |
||
| 1320 | * |
||
| 1321 | * @since 2.2.1 |
||
| 1322 | * |
||
| 1323 | * @param bool $development_mode Is Jetpack's development mode active. |
||
| 1324 | */ |
||
| 1325 | return apply_filters( 'jetpack_development_mode', $development_mode ); |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Get Jetpack development mode notice text and notice class. |
||
| 1330 | * |
||
| 1331 | * Mirrors the checks made in Jetpack::is_development_mode |
||
| 1332 | * |
||
| 1333 | */ |
||
| 1334 | public static function show_development_mode_notice() { |
||
| 1335 | if ( Jetpack::is_development_mode() ) { |
||
| 1336 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
| 1337 | $notice = sprintf( |
||
| 1338 | /* translators: %s is a URL */ |
||
| 1339 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ), |
||
| 1340 | 'https://jetpack.com/support/development-mode/' |
||
| 1341 | ); |
||
| 1342 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
| 1343 | $notice = sprintf( |
||
| 1344 | /* translators: %s is a URL */ |
||
| 1345 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ), |
||
| 1346 | 'https://jetpack.com/support/development-mode/' |
||
| 1347 | ); |
||
| 1348 | } else { |
||
| 1349 | $notice = sprintf( |
||
| 1350 | /* translators: %s is a URL */ |
||
| 1351 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ), |
||
| 1352 | 'https://jetpack.com/support/development-mode/' |
||
| 1353 | ); |
||
| 1354 | } |
||
| 1355 | |||
| 1356 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | // Throw up a notice if using a development version and as for feedback. |
||
| 1360 | if ( Jetpack::is_development_version() ) { |
||
| 1361 | /* translators: %s is a URL */ |
||
| 1362 | $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/' ); |
||
| 1363 | |||
| 1364 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1365 | } |
||
| 1366 | // Throw up a notice if using staging mode |
||
| 1367 | if ( Jetpack::is_staging_site() ) { |
||
| 1368 | /* translators: %s is a URL */ |
||
| 1369 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
| 1370 | |||
| 1371 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1372 | } |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Whether Jetpack's version maps to a public release, or a development version. |
||
| 1377 | */ |
||
| 1378 | public static function is_development_version() { |
||
| 1379 | /** |
||
| 1380 | * Allows filtering whether this is a development version of Jetpack. |
||
| 1381 | * |
||
| 1382 | * This filter is especially useful for tests. |
||
| 1383 | * |
||
| 1384 | * @since 4.3.0 |
||
| 1385 | * |
||
| 1386 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
| 1387 | */ |
||
| 1388 | return (bool) apply_filters( |
||
| 1389 | 'jetpack_development_version', |
||
| 1390 | ! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
| 1391 | ); |
||
| 1392 | } |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
| 1396 | */ |
||
| 1397 | public static function is_user_connected( $user_id = false ) { |
||
| 1398 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
| 1399 | if ( ! $user_id ) { |
||
| 1400 | return false; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | return (bool) Jetpack_Data::get_access_token( $user_id ); |
||
| 1404 | } |
||
| 1405 | |||
| 1406 | /** |
||
| 1407 | * Get the wpcom user data of the current|specified connected user. |
||
| 1408 | */ |
||
| 1409 | public static function get_connected_user_data( $user_id = null ) { |
||
| 1410 | if ( ! $user_id ) { |
||
| 1411 | $user_id = get_current_user_id(); |
||
| 1412 | } |
||
| 1413 | |||
| 1414 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 1415 | |||
| 1416 | if ( $cached_user_data = get_transient( $transient_key ) ) { |
||
| 1417 | return $cached_user_data; |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | Jetpack::load_xml_rpc_client(); |
||
| 1421 | $xml = new Jetpack_IXR_Client( array( |
||
| 1422 | 'user_id' => $user_id, |
||
| 1423 | ) ); |
||
| 1424 | $xml->query( 'wpcom.getUser' ); |
||
| 1425 | if ( ! $xml->isError() ) { |
||
| 1426 | $user_data = $xml->getResponse(); |
||
| 1427 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
| 1428 | return $user_data; |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | return false; |
||
| 1432 | } |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * Get the wpcom email of the current|specified connected user. |
||
| 1436 | */ |
||
| 1437 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
| 1438 | if ( ! $user_id ) { |
||
| 1439 | $user_id = get_current_user_id(); |
||
| 1440 | } |
||
| 1441 | Jetpack::load_xml_rpc_client(); |
||
| 1442 | $xml = new Jetpack_IXR_Client( array( |
||
| 1443 | 'user_id' => $user_id, |
||
| 1444 | ) ); |
||
| 1445 | $xml->query( 'wpcom.getUserEmail' ); |
||
| 1446 | if ( ! $xml->isError() ) { |
||
| 1447 | return $xml->getResponse(); |
||
| 1448 | } |
||
| 1449 | return false; |
||
| 1450 | } |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Get the wpcom email of the master user. |
||
| 1454 | */ |
||
| 1455 | public static function get_master_user_email() { |
||
| 1456 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
| 1457 | if ( $master_user_id ) { |
||
| 1458 | return self::get_connected_user_email( $master_user_id ); |
||
| 1459 | } |
||
| 1460 | return ''; |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | function current_user_is_connection_owner() { |
||
| 1464 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1465 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; |
||
| 1466 | } |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
| 1470 | */ |
||
| 1471 | function extra_oembed_providers() { |
||
| 1472 | // Cloudup: https://dev.cloudup.com/#oembed |
||
| 1473 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
| 1474 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
| 1475 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
| 1476 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
| 1477 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
| 1478 | } |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Synchronize connected user role changes |
||
| 1482 | */ |
||
| 1483 | function user_role_change( $user_id ) { |
||
| 1484 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()' ); |
||
| 1485 | Jetpack_Sync_Users::user_role_change( $user_id ); |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Loads the currently active modules. |
||
| 1490 | */ |
||
| 1491 | public static function load_modules() { |
||
| 1492 | if ( ! self::is_active() && !self::is_development_mode() ) { |
||
| 1493 | if ( ! is_multisite() || ! get_site_option( 'jetpack_protect_active' ) ) { |
||
| 1494 | return; |
||
| 1495 | } |
||
| 1496 | } |
||
| 1497 | |||
| 1498 | $version = Jetpack_Options::get_option( 'version' ); |
||
| 1499 | View Code Duplication | if ( ! $version ) { |
|
| 1500 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
| 1501 | /** This action is documented in class.jetpack.php */ |
||
| 1502 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 1503 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 1504 | } |
||
| 1505 | list( $version ) = explode( ':', $version ); |
||
| 1506 | |||
| 1507 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
| 1508 | |||
| 1509 | $modules_data = array(); |
||
| 1510 | |||
| 1511 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
| 1512 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
| 1513 | $updated_modules = array(); |
||
| 1514 | foreach ( $modules as $module ) { |
||
| 1515 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1516 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
| 1517 | continue; |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
| 1521 | continue; |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | $updated_modules[] = $module; |
||
| 1525 | } |
||
| 1526 | |||
| 1527 | $modules = array_diff( $modules, $updated_modules ); |
||
| 1528 | } |
||
| 1529 | |||
| 1530 | $is_development_mode = Jetpack::is_development_mode(); |
||
| 1531 | |||
| 1532 | foreach ( $modules as $index => $module ) { |
||
| 1533 | // If we're in dev mode, disable modules requiring a connection |
||
| 1534 | if ( $is_development_mode ) { |
||
| 1535 | // Prime the pump if we need to |
||
| 1536 | if ( empty( $modules_data[ $module ] ) ) { |
||
| 1537 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1538 | } |
||
| 1539 | // If the module requires a connection, but we're in local mode, don't include it. |
||
| 1540 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
| 1541 | continue; |
||
| 1542 | } |
||
| 1543 | } |
||
| 1544 | |||
| 1545 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
| 1546 | continue; |
||
| 1547 | } |
||
| 1548 | |||
| 1549 | if ( ! @include( Jetpack::get_module_path( $module ) ) ) { |
||
| 1550 | unset( $modules[ $index ] ); |
||
| 1551 | self::update_active_modules( array_values( $modules ) ); |
||
| 1552 | continue; |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Fires when a specific module is loaded. |
||
| 1557 | * The dynamic part of the hook, $module, is the module slug. |
||
| 1558 | * |
||
| 1559 | * @since 1.1.0 |
||
| 1560 | */ |
||
| 1561 | do_action( 'jetpack_module_loaded_' . $module ); |
||
| 1562 | } |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Fires when all the modules are loaded. |
||
| 1566 | * |
||
| 1567 | * @since 1.1.0 |
||
| 1568 | */ |
||
| 1569 | do_action( 'jetpack_modules_loaded' ); |
||
| 1570 | |||
| 1571 | // 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. |
||
| 1572 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) |
||
| 1573 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
| 1574 | } |
||
| 1575 | |||
| 1576 | /** |
||
| 1577 | * Check if Jetpack's REST API compat file should be included |
||
| 1578 | * @action plugins_loaded |
||
| 1579 | * @return null |
||
| 1580 | */ |
||
| 1581 | public function check_rest_api_compat() { |
||
| 1582 | /** |
||
| 1583 | * Filters the list of REST API compat files to be included. |
||
| 1584 | * |
||
| 1585 | * @since 2.2.5 |
||
| 1586 | * |
||
| 1587 | * @param array $args Array of REST API compat files to include. |
||
| 1588 | */ |
||
| 1589 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
| 1590 | |||
| 1591 | if ( function_exists( 'bbpress' ) ) |
||
| 1592 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
| 1593 | |||
| 1594 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
| 1595 | require_once $_jetpack_rest_api_compat_include; |
||
| 1596 | } |
||
| 1597 | |||
| 1598 | /** |
||
| 1599 | * Gets all plugins currently active in values, regardless of whether they're |
||
| 1600 | * traditionally activated or network activated. |
||
| 1601 | * |
||
| 1602 | * @todo Store the result in core's object cache maybe? |
||
| 1603 | */ |
||
| 1604 | public static function get_active_plugins() { |
||
| 1605 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
| 1606 | |||
| 1607 | if ( is_multisite() ) { |
||
| 1608 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
| 1609 | // whereas active_plugins stores them in the values. |
||
| 1610 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
| 1611 | if ( $network_plugins ) { |
||
| 1612 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
| 1613 | } |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | sort( $active_plugins ); |
||
| 1617 | |||
| 1618 | return array_unique( $active_plugins ); |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Gets and parses additional plugin data to send with the heartbeat data |
||
| 1623 | * |
||
| 1624 | * @since 3.8.1 |
||
| 1625 | * |
||
| 1626 | * @return array Array of plugin data |
||
| 1627 | */ |
||
| 1628 | public static function get_parsed_plugin_data() { |
||
| 1629 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 1630 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
| 1631 | } |
||
| 1632 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 1633 | $all_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
| 1634 | $active_plugins = Jetpack::get_active_plugins(); |
||
| 1635 | |||
| 1636 | $plugins = array(); |
||
| 1637 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
| 1638 | $plugins[ $path ] = array( |
||
| 1639 | 'is_active' => in_array( $path, $active_plugins ), |
||
| 1640 | 'file' => $path, |
||
| 1641 | 'name' => $plugin_data['Name'], |
||
| 1642 | 'version' => $plugin_data['Version'], |
||
| 1643 | 'author' => $plugin_data['Author'], |
||
| 1644 | ); |
||
| 1645 | } |
||
| 1646 | |||
| 1647 | return $plugins; |
||
| 1648 | } |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Gets and parses theme data to send with the heartbeat data |
||
| 1652 | * |
||
| 1653 | * @since 3.8.1 |
||
| 1654 | * |
||
| 1655 | * @return array Array of theme data |
||
| 1656 | */ |
||
| 1657 | public static function get_parsed_theme_data() { |
||
| 1658 | $all_themes = wp_get_themes( array( 'allowed' => true ) ); |
||
| 1659 | $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); |
||
| 1660 | |||
| 1661 | $themes = array(); |
||
| 1662 | foreach ( $all_themes as $slug => $theme_data ) { |
||
| 1663 | $theme_headers = array(); |
||
| 1664 | foreach ( $header_keys as $header_key ) { |
||
| 1665 | $theme_headers[ $header_key ] = $theme_data->get( $header_key ); |
||
| 1666 | } |
||
| 1667 | |||
| 1668 | $themes[ $slug ] = array( |
||
| 1669 | 'is_active_theme' => $slug == wp_get_theme()->get_template(), |
||
| 1670 | 'slug' => $slug, |
||
| 1671 | 'theme_root' => $theme_data->get_theme_root_uri(), |
||
| 1672 | 'parent' => $theme_data->parent(), |
||
| 1673 | 'headers' => $theme_headers |
||
| 1674 | ); |
||
| 1675 | } |
||
| 1676 | |||
| 1677 | return $themes; |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * Checks whether a specific plugin is active. |
||
| 1682 | * |
||
| 1683 | * We don't want to store these in a static variable, in case |
||
| 1684 | * there are switch_to_blog() calls involved. |
||
| 1685 | */ |
||
| 1686 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
| 1687 | return in_array( $plugin, self::get_active_plugins() ); |
||
| 1688 | } |
||
| 1689 | |||
| 1690 | /** |
||
| 1691 | * Check if Jetpack's Open Graph tags should be used. |
||
| 1692 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
| 1693 | * |
||
| 1694 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1695 | * @action plugins_loaded |
||
| 1696 | * @return null |
||
| 1697 | */ |
||
| 1698 | public function check_open_graph() { |
||
| 1699 | if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { |
||
| 1700 | add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); |
||
| 1701 | } |
||
| 1702 | |||
| 1703 | $active_plugins = self::get_active_plugins(); |
||
| 1704 | |||
| 1705 | if ( ! empty( $active_plugins ) ) { |
||
| 1706 | foreach ( $this->open_graph_conflicting_plugins as $plugin ) { |
||
| 1707 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 1708 | add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); |
||
| 1709 | break; |
||
| 1710 | } |
||
| 1711 | } |
||
| 1712 | } |
||
| 1713 | |||
| 1714 | /** |
||
| 1715 | * Allow the addition of Open Graph Meta Tags to all pages. |
||
| 1716 | * |
||
| 1717 | * @since 2.0.3 |
||
| 1718 | * |
||
| 1719 | * @param bool false Should Open Graph Meta tags be added. Default to false. |
||
| 1720 | */ |
||
| 1721 | if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { |
||
| 1722 | require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; |
||
| 1723 | } |
||
| 1724 | } |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Check if Jetpack's Twitter tags should be used. |
||
| 1728 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
| 1729 | * |
||
| 1730 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1731 | * @action plugins_loaded |
||
| 1732 | * @return null |
||
| 1733 | */ |
||
| 1734 | public function check_twitter_tags() { |
||
| 1735 | |||
| 1736 | $active_plugins = self::get_active_plugins(); |
||
| 1737 | |||
| 1738 | if ( ! empty( $active_plugins ) ) { |
||
| 1739 | foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { |
||
| 1740 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 1741 | add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); |
||
| 1742 | break; |
||
| 1743 | } |
||
| 1744 | } |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | /** |
||
| 1748 | * Allow Twitter Card Meta tags to be disabled. |
||
| 1749 | * |
||
| 1750 | * @since 2.6.0 |
||
| 1751 | * |
||
| 1752 | * @param bool true Should Twitter Card Meta tags be disabled. Default to true. |
||
| 1753 | */ |
||
| 1754 | if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) { |
||
| 1755 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; |
||
| 1756 | } |
||
| 1757 | } |
||
| 1758 | |||
| 1759 | /** |
||
| 1760 | * Allows plugins to submit security reports. |
||
| 1761 | * |
||
| 1762 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
| 1763 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
| 1764 | * @param array $args See definitions above |
||
| 1765 | */ |
||
| 1766 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
| 1767 | _deprecated_function( __FUNCTION__, 'jetpack-4.2', null ); |
||
| 1768 | } |
||
| 1769 | |||
| 1770 | /* Jetpack Options API */ |
||
| 1771 | |||
| 1772 | public static function get_option_names( $type = 'compact' ) { |
||
| 1775 | |||
| 1776 | /** |
||
| 1777 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
| 1778 | * |
||
| 1779 | * @param string $name Option name |
||
| 1780 | * @param mixed $default (optional) |
||
| 1781 | */ |
||
| 1782 | public static function get_option( $name, $default = false ) { |
||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action |
||
| 1788 | * Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted |
||
| 1789 | * $name must be a registered option name. |
||
| 1790 | */ |
||
| 1791 | public static function create_nonce( $name ) { |
||
| 1805 | |||
| 1806 | /** |
||
| 1807 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
| 1808 | * |
||
| 1809 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
| 1810 | * @param string $name Option name |
||
| 1811 | * @param mixed $value Option value |
||
| 1812 | */ |
||
| 1813 | public static function update_option( $name, $value ) { |
||
| 1817 | |||
| 1818 | /** |
||
| 1819 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
| 1820 | * |
||
| 1821 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
| 1822 | * @param array $array array( option name => option value, ... ) |
||
| 1823 | */ |
||
| 1824 | public static function update_options( $array ) { |
||
| 1828 | |||
| 1829 | /** |
||
| 1830 | * Deletes the given option. May be passed multiple option names as an array. |
||
| 1831 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
| 1832 | * |
||
| 1833 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
| 1834 | * @param string|array $names |
||
| 1835 | */ |
||
| 1836 | public static function delete_option( $names ) { |
||
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Enters a user token into the user_tokens option |
||
| 1843 | * |
||
| 1844 | * @param int $user_id |
||
| 1845 | * @param string $token |
||
| 1846 | * return bool |
||
| 1847 | */ |
||
| 1848 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Returns an array of all PHP files in the specified absolute path. |
||
| 1865 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
| 1866 | * |
||
| 1867 | * @param string $absolute_path The absolute path of the directory to search. |
||
| 1868 | * @return array Array of absolute paths to the PHP files. |
||
| 1869 | */ |
||
| 1870 | public static function glob_php( $absolute_path ) { |
||
| 1899 | |||
| 1900 | public static function activate_new_modules( $redirect = false ) { |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
| 1961 | * Make sure to tuck away module "library" files in a sub-directory. |
||
| 1962 | */ |
||
| 1963 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
| 2020 | |||
| 2021 | /** |
||
| 2022 | * Default modules loaded on activation. |
||
| 2023 | */ |
||
| 2024 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
| 2055 | |||
| 2056 | /** |
||
| 2057 | * Checks activated modules during auto-activation to determine |
||
| 2058 | * if any of those modules are being deprecated. If so, close |
||
| 2059 | * them out, and add any replacement modules. |
||
| 2060 | * |
||
| 2061 | * Runs at priority 99 by default. |
||
| 2062 | * |
||
| 2063 | * This is run late, so that it can still activate a module if |
||
| 2064 | * the new module is a replacement for another that the user |
||
| 2065 | * currently has active, even if something at the normal priority |
||
| 2066 | * would kibosh everything. |
||
| 2067 | * |
||
| 2068 | * @since 2.6 |
||
| 2069 | * @uses jetpack_get_default_modules filter |
||
| 2070 | * @param array $modules |
||
| 2071 | * @return array |
||
| 2072 | */ |
||
| 2073 | function handle_deprecated_modules( $modules ) { |
||
| 2099 | |||
| 2100 | /** |
||
| 2101 | * Checks activated plugins during auto-activation to determine |
||
| 2102 | * if any of those plugins are in the list with a corresponding module |
||
| 2103 | * that is not compatible with the plugin. The module will not be allowed |
||
| 2104 | * to auto-activate. |
||
| 2105 | * |
||
| 2106 | * @since 2.6 |
||
| 2107 | * @uses jetpack_get_default_modules filter |
||
| 2108 | * @param array $modules |
||
| 2109 | * @return array |
||
| 2110 | */ |
||
| 2111 | function filter_default_modules( $modules ) { |
||
| 2135 | |||
| 2136 | /** |
||
| 2137 | * Extract a module's slug from its full path. |
||
| 2138 | */ |
||
| 2139 | public static function get_module_slug( $file ) { |
||
| 2142 | |||
| 2143 | /** |
||
| 2144 | * Generate a module's path from its slug. |
||
| 2145 | */ |
||
| 2146 | public static function get_module_path( $slug ) { |
||
| 2149 | |||
| 2150 | /** |
||
| 2151 | * Load module data from module file. Headers differ from WordPress |
||
| 2152 | * plugin headers to avoid them being identified as standalone |
||
| 2153 | * plugins on the WordPress plugins page. |
||
| 2154 | */ |
||
| 2155 | public static function get_module( $module ) { |
||
| 2238 | |||
| 2239 | /** |
||
| 2240 | * Like core's get_file_data implementation, but caches the result. |
||
| 2241 | */ |
||
| 2242 | public static function get_file_data( $file, $headers ) { |
||
| 2263 | |||
| 2264 | /** |
||
| 2265 | * Return translated module tag. |
||
| 2266 | * |
||
| 2267 | * @param string $tag Tag as it appears in each module heading. |
||
| 2268 | * |
||
| 2269 | * @return mixed |
||
| 2270 | */ |
||
| 2271 | public static function translate_module_tag( $tag ) { |
||
| 2274 | |||
| 2275 | /** |
||
| 2276 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
| 2277 | * |
||
| 2278 | * @since 3.9.2 |
||
| 2279 | * |
||
| 2280 | * @param array $modules |
||
| 2281 | * |
||
| 2282 | * @return string|void |
||
| 2283 | */ |
||
| 2284 | public static function get_translated_modules( $modules ) { |
||
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Get a list of activated modules as an array of module slugs. |
||
| 2300 | */ |
||
| 2301 | public static function get_active_modules() { |
||
| 2302 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
| 2303 | |||
| 2304 | if ( ! is_array( $active ) ) { |
||
| 2305 | $active = array(); |
||
| 2306 | } |
||
| 2307 | |||
| 2308 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
| 2309 | $active[] = 'vaultpress'; |
||
| 2310 | } else { |
||
| 2311 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
| 2312 | } |
||
| 2313 | |||
| 2314 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
| 2315 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
| 2316 | $active[] = 'protect'; |
||
| 2317 | } |
||
| 2318 | |||
| 2319 | return array_unique( $active ); |
||
| 2320 | } |
||
| 2321 | |||
| 2322 | /** |
||
| 2323 | * Check whether or not a Jetpack module is active. |
||
| 2324 | * |
||
| 2325 | * @param string $module The slug of a Jetpack module. |
||
| 2326 | * @return bool |
||
| 2327 | * |
||
| 2328 | * @static |
||
| 2329 | */ |
||
| 2330 | public static function is_module_active( $module ) { |
||
| 2333 | |||
| 2334 | public static function is_module( $module ) { |
||
| 2337 | |||
| 2338 | /** |
||
| 2339 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
| 2340 | * |
||
| 2341 | * @param bool $catch True to start catching, False to stop. |
||
| 2342 | * |
||
| 2343 | * @static |
||
| 2344 | */ |
||
| 2345 | public static function catch_errors( $catch ) { |
||
| 2358 | |||
| 2359 | /** |
||
| 2360 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
| 2361 | */ |
||
| 2362 | public static function catch_errors_on_shutdown() { |
||
| 2365 | |||
| 2366 | public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array(), $redirect = true ) { |
||
| 2367 | $jetpack = Jetpack::init(); |
||
| 2368 | |||
| 2369 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
| 2370 | $modules = array_merge( $other_modules, $modules ); |
||
| 2371 | |||
| 2372 | // Look for standalone plugins and disable if active. |
||
| 2373 | |||
| 2374 | $to_deactivate = array(); |
||
| 2375 | foreach ( $modules as $module ) { |
||
| 2376 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
| 2377 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
| 2378 | } |
||
| 2483 | |||
| 2484 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
| 2485 | /** |
||
| 2486 | * Fires before a module is activated. |
||
| 2487 | * |
||
| 2488 | * @since 2.6.0 |
||
| 2489 | * |
||
| 2490 | * @param string $module Module slug. |
||
| 2491 | * @param bool $exit Should we exit after the module has been activated. Default to true. |
||
| 2492 | * @param bool $redirect Should the user be redirected after module activation? Default to true. |
||
| 2493 | */ |
||
| 2494 | do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); |
||
| 2495 | |||
| 2496 | $jetpack = Jetpack::init(); |
||
| 2497 | |||
| 2498 | if ( ! strlen( $module ) ) |
||
| 2499 | return false; |
||
| 2500 | |||
| 2501 | if ( ! Jetpack::is_module( $module ) ) |
||
| 2502 | return false; |
||
| 2503 | |||
| 2504 | // If it's already active, then don't do it again |
||
| 2505 | $active = Jetpack::get_active_modules(); |
||
| 2506 | foreach ( $active as $act ) { |
||
| 2507 | if ( $act == $module ) |
||
| 2508 | return true; |
||
| 2509 | } |
||
| 2510 | |||
| 2511 | $module_data = Jetpack::get_module( $module ); |
||
| 2512 | |||
| 2513 | if ( ! Jetpack::is_active() ) { |
||
| 2514 | if ( !Jetpack::is_development_mode() ) |
||
| 2515 | return false; |
||
| 2516 | |||
| 2517 | // If we're not connected but in development mode, make sure the module doesn't require a connection |
||
| 2518 | if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) |
||
| 2519 | return false; |
||
| 2520 | } |
||
| 2521 | |||
| 2522 | // Check and see if the old plugin is active |
||
| 2523 | if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { |
||
| 2524 | // Deactivate the old plugin |
||
| 2525 | if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { |
||
| 2526 | // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module |
||
| 2527 | // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. |
||
| 2528 | Jetpack::state( 'deactivated_plugins', $module ); |
||
| 2529 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
||
| 2530 | exit; |
||
| 2531 | } |
||
| 2532 | } |
||
| 2533 | |||
| 2578 | |||
| 2579 | function activate_module_actions( $module ) { |
||
| 2582 | |||
| 2583 | public static function deactivate_module( $module ) { |
||
| 2610 | |||
| 2611 | public static function enable_module_configurable( $module ) { |
||
| 2615 | |||
| 2616 | public static function module_configuration_url( $module ) { |
||
| 2620 | |||
| 2621 | public static function module_configuration_load( $module, $method ) { |
||
| 2625 | |||
| 2626 | public static function module_configuration_head( $module, $method ) { |
||
| 2630 | |||
| 2631 | public static function module_configuration_screen( $module, $method ) { |
||
| 2635 | |||
| 2636 | public static function module_configuration_activation_screen( $module, $method ) { |
||
| 2640 | |||
| 2641 | /* Installation */ |
||
| 2642 | |||
| 2643 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
| 2683 | |||
| 2684 | /** |
||
| 2685 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
| 2686 | * @static |
||
| 2687 | */ |
||
| 2688 | public static function plugin_activation( $network_wide ) { |
||
| 2700 | /** |
||
| 2701 | * Runs before bumping version numbers up to a new version |
||
| 2702 | * @param (string) $version Version:timestamp |
||
| 2703 | * @param (string) $old_version Old Version:timestamp or false if not set yet. |
||
| 2704 | * @return null [description] |
||
| 2705 | */ |
||
| 2706 | public static function do_version_bump( $version, $old_version ) { |
||
| 2713 | |||
| 2714 | /** |
||
| 2715 | * Sets the internal version number and activation state. |
||
| 2716 | * @static |
||
| 2717 | */ |
||
| 2718 | public static function plugin_initialize() { |
||
| 2734 | |||
| 2735 | /** |
||
| 2736 | * Removes all connection options |
||
| 2737 | * @static |
||
| 2738 | */ |
||
| 2739 | public static function plugin_deactivation( ) { |
||
| 2748 | |||
| 2749 | /** |
||
| 2750 | * Disconnects from the Jetpack servers. |
||
| 2751 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 2752 | * @static |
||
| 2753 | */ |
||
| 2754 | public static function disconnect( $update_activated_state = true ) { |
||
| 2814 | |||
| 2815 | /** |
||
| 2816 | * Unlinks the current user from the linked WordPress.com user |
||
| 2817 | */ |
||
| 2818 | public static function unlink_user( $user_id = null ) { |
||
| 2849 | |||
| 2850 | /** |
||
| 2851 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
| 2852 | */ |
||
| 2853 | public static function try_registration() { |
||
| 2878 | |||
| 2879 | /** |
||
| 2880 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
| 2881 | * |
||
| 2882 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
| 2883 | */ |
||
| 2884 | public static function log( $code, $data = null ) { |
||
| 2924 | |||
| 2925 | /** |
||
| 2926 | * Get the internal event log. |
||
| 2927 | * |
||
| 2928 | * @param $event (string) - only return the specific log events |
||
| 2929 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
| 2930 | * |
||
| 2931 | * @return array of log events || WP_Error for invalid params |
||
| 2932 | */ |
||
| 2933 | public static function get_log( $event = false, $num = false ) { |
||
| 2969 | |||
| 2970 | /** |
||
| 2971 | * Log modification of important settings. |
||
| 2972 | */ |
||
| 2973 | public static function log_settings_change( $option, $old_value, $value ) { |
||
| 2980 | |||
| 2981 | /** |
||
| 2982 | * Return stat data for WPCOM sync |
||
| 2983 | */ |
||
| 2984 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
| 2998 | |||
| 2999 | /** |
||
| 3000 | * Get additional stat data to sync to WPCOM |
||
| 3001 | */ |
||
| 3002 | public static function get_additional_stat_data( $prefix = '' ) { |
||
| 3013 | |||
| 3014 | private static function get_site_user_count() { |
||
| 3029 | |||
| 3030 | /* Admin Pages */ |
||
| 3031 | |||
| 3032 | function admin_init() { |
||
| 3081 | |||
| 3082 | function admin_body_class( $admin_body_class = '' ) { |
||
| 3090 | |||
| 3091 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
| 3094 | |||
| 3095 | /** |
||
| 3096 | * Call this function if you want the Big Jetpack Manage Notice to show up. |
||
| 3097 | * |
||
| 3098 | * @return null |
||
| 3099 | */ |
||
| 3100 | function prepare_manage_jetpack_notice() { |
||
| 3105 | |||
| 3106 | function manage_activate_screen() { |
||
| 3109 | /** |
||
| 3110 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
| 3111 | * This function artificially throws errors for such cases (whitelisted). |
||
| 3112 | * |
||
| 3113 | * @param string $plugin The activated plugin. |
||
| 3114 | */ |
||
| 3115 | function throw_error_on_activate_plugin( $plugin ) { |
||
| 3139 | |||
| 3140 | function intercept_plugin_error_scrape_init() { |
||
| 3143 | |||
| 3144 | function intercept_plugin_error_scrape( $action, $result ) { |
||
| 3155 | |||
| 3156 | function add_remote_request_handlers() { |
||
| 3159 | |||
| 3160 | function remote_request_handlers() { |
||
| 3194 | |||
| 3195 | function upload_handler() { |
||
| 3286 | |||
| 3287 | /** |
||
| 3288 | * Add help to the Jetpack page |
||
| 3289 | * |
||
| 3290 | * @since Jetpack (1.2.3) |
||
| 3291 | * @return false if not the Jetpack page |
||
| 3292 | */ |
||
| 3293 | function admin_help() { |
||
| 3334 | |||
| 3335 | function admin_menu_css() { |
||
| 3338 | |||
| 3339 | function admin_menu_order() { |
||
| 3342 | |||
| 3343 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 3358 | |||
| 3359 | function admin_head() { |
||
| 3364 | |||
| 3365 | function admin_banner_styles() { |
||
| 3386 | |||
| 3387 | function plugin_action_links( $actions ) { |
||
| 3402 | |||
| 3403 | /** |
||
| 3404 | * This is the first banner |
||
| 3405 | * It should be visible only to user that can update the option |
||
| 3406 | * Are not connected |
||
| 3407 | * |
||
| 3408 | * @return null |
||
| 3409 | */ |
||
| 3410 | function admin_jetpack_manage_notice() { |
||
| 3440 | |||
| 3441 | /** |
||
| 3442 | * Returns the url that the user clicks to remove the notice for the big banner |
||
| 3443 | * @return (string) |
||
| 3444 | */ |
||
| 3445 | function opt_out_jetpack_manage_url() { |
||
| 3449 | /** |
||
| 3450 | * Returns the url that the user clicks to opt in to Jetpack Manage |
||
| 3451 | * @return (string) |
||
| 3452 | */ |
||
| 3453 | function opt_in_jetpack_manage_url() { |
||
| 3456 | |||
| 3457 | function opt_in_jetpack_manage_notice() { |
||
| 3467 | /** |
||
| 3468 | * Determines whether to show the notice of not true = display notice |
||
| 3469 | * @return (bool) |
||
| 3470 | */ |
||
| 3471 | function can_display_jetpack_manage_notice() { |
||
| 3493 | |||
| 3494 | /* |
||
| 3495 | * Registration flow: |
||
| 3496 | * 1 - ::admin_page_load() action=register |
||
| 3497 | * 2 - ::try_registration() |
||
| 3498 | * 3 - ::register() |
||
| 3499 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
| 3500 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
| 3501 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
| 3502 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
| 3503 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
| 3504 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
| 3505 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
| 3506 | * jetpack_id, jetpack_secret, jetpack_public |
||
| 3507 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
| 3508 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
| 3509 | * 5 - user logs in with WP.com account |
||
| 3510 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
| 3511 | * - Jetpack_Client_Server::authorize() |
||
| 3512 | * - Jetpack_Client_Server::get_token() |
||
| 3513 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
| 3514 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
| 3515 | * - which responds with access_token, token_type, scope |
||
| 3516 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
| 3517 | * - Jetpack::activate_default_modules() |
||
| 3518 | * - Deactivates deprecated plugins |
||
| 3519 | * - Activates all default modules |
||
| 3520 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
| 3521 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
| 3522 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
| 3523 | * Done! |
||
| 3524 | */ |
||
| 3525 | |||
| 3526 | /** |
||
| 3527 | * Handles the page load events for the Jetpack admin page |
||
| 3528 | */ |
||
| 3529 | function admin_page_load() { |
||
| 3755 | |||
| 3756 | function admin_notices() { |
||
| 3853 | |||
| 3854 | /** |
||
| 3855 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
| 3856 | */ |
||
| 3857 | function stat( $group, $detail ) { |
||
| 3862 | |||
| 3863 | /** |
||
| 3864 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
| 3865 | */ |
||
| 3866 | function do_stats( $method = '' ) { |
||
| 3881 | |||
| 3882 | /** |
||
| 3883 | * Runs stats code for a one-off, server-side. |
||
| 3884 | * |
||
| 3885 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
| 3886 | * |
||
| 3887 | * @return bool If it worked. |
||
| 3888 | */ |
||
| 3889 | static function do_server_side_stat( $args ) { |
||
| 3899 | |||
| 3900 | /** |
||
| 3901 | * Builds the stats url. |
||
| 3902 | * |
||
| 3903 | * @param $args array|string The arguments to append to the URL. |
||
| 3904 | * |
||
| 3905 | * @return string The URL to be pinged. |
||
| 3906 | */ |
||
| 3907 | static function build_stats_url( $args ) { |
||
| 3927 | |||
| 3928 | static function translate_current_user_to_role() { |
||
| 3937 | |||
| 3938 | static function translate_role_to_cap( $role ) { |
||
| 3945 | |||
| 3946 | static function sign_role( $role ) { |
||
| 3958 | |||
| 3959 | |||
| 3960 | /** |
||
| 3961 | * Builds a URL to the Jetpack connection auth page |
||
| 3962 | * |
||
| 3963 | * @since 3.9.5 |
||
| 3964 | * |
||
| 3965 | * @param bool $raw If true, URL will not be escaped. |
||
| 3966 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 3967 | * If string, will be a custom redirect. |
||
| 3968 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 3969 | * |
||
| 3970 | * @return string Connect URL |
||
| 3971 | */ |
||
| 3972 | function build_connect_url( $raw = false, $redirect = false, $from = false ) { |
||
| 4056 | |||
| 4057 | function build_reconnect_url( $raw = false ) { |
||
| 4061 | |||
| 4062 | public static function admin_url( $args = null ) { |
||
| 4067 | |||
| 4068 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
| 4072 | |||
| 4073 | function dismiss_jetpack_notice() { |
||
| 4147 | |||
| 4148 | function debugger_page() { |
||
| 4156 | |||
| 4157 | public static function admin_screen_configure_module( $module_id ) { |
||
| 4209 | |||
| 4210 | /** |
||
| 4211 | * Display link to activate the module to see the settings screen. |
||
| 4212 | * @param string $module_id |
||
| 4213 | * @return null |
||
| 4214 | */ |
||
| 4215 | public static function display_activate_module_link( $module_id ) { |
||
| 4267 | |||
| 4268 | public static function sort_modules( $a, $b ) { |
||
| 4274 | |||
| 4275 | function ajax_recheck_ssl() { |
||
| 4283 | |||
| 4284 | /* Client API */ |
||
| 4285 | |||
| 4286 | /** |
||
| 4287 | * Returns the requested Jetpack API URL |
||
| 4288 | * |
||
| 4289 | * @return string |
||
| 4290 | */ |
||
| 4291 | public static function api_url( $relative_url ) { |
||
| 4294 | |||
| 4295 | /** |
||
| 4296 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
| 4297 | */ |
||
| 4298 | public static function fix_url_for_bad_hosts( $url ) { |
||
| 4314 | |||
| 4315 | /** |
||
| 4316 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
| 4317 | * |
||
| 4318 | * @since 2.3.3 |
||
| 4319 | * @return boolean |
||
| 4320 | */ |
||
| 4321 | public static function permit_ssl( $force_recheck = false ) { |
||
| 4363 | |||
| 4364 | /* |
||
| 4365 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
| 4366 | */ |
||
| 4367 | public function alert_auto_ssl_fail() { |
||
| 4416 | |||
| 4417 | /** |
||
| 4418 | * Returns the Jetpack XML-RPC API |
||
| 4419 | * |
||
| 4420 | * @return string |
||
| 4421 | */ |
||
| 4422 | public static function xmlrpc_api_url() { |
||
| 4426 | |||
| 4427 | /** |
||
| 4428 | * Creates two secret tokens and the end of life timestamp for them. |
||
| 4429 | * |
||
| 4430 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
| 4431 | * |
||
| 4432 | * @since 2.6 |
||
| 4433 | * @return array |
||
| 4434 | */ |
||
| 4435 | public function generate_secrets( $action, $exp = 600 ) { |
||
| 4443 | |||
| 4444 | /** |
||
| 4445 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 4446 | * |
||
| 4447 | * Based on local php max_execution_time in php.ini |
||
| 4448 | * |
||
| 4449 | * @since 2.6 |
||
| 4450 | * @return int |
||
| 4451 | **/ |
||
| 4452 | public function get_remote_query_timeout_limit() { |
||
| 4458 | |||
| 4459 | |||
| 4460 | /** |
||
| 4461 | * Takes the response from the Jetpack register new site endpoint and |
||
| 4462 | * verifies it worked properly. |
||
| 4463 | * |
||
| 4464 | * @since 2.6 |
||
| 4465 | * @return true or Jetpack_Error |
||
| 4466 | **/ |
||
| 4467 | public function validate_remote_register_response( $response ) { |
||
| 4507 | /** |
||
| 4508 | * @return bool|WP_Error |
||
| 4509 | */ |
||
| 4510 | public static function register() { |
||
| 4607 | |||
| 4608 | /** |
||
| 4609 | * If the db version is showing something other that what we've got now, bump it to current. |
||
| 4610 | * |
||
| 4611 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
| 4612 | */ |
||
| 4613 | public static function maybe_set_version_option() { |
||
| 4627 | |||
| 4628 | /* Client Server API */ |
||
| 4629 | |||
| 4630 | /** |
||
| 4631 | * Loads the Jetpack XML-RPC client |
||
| 4632 | */ |
||
| 4633 | public static function load_xml_rpc_client() { |
||
| 4637 | |||
| 4638 | /** |
||
| 4639 | * Resets the saved authentication state in between testing requests. |
||
| 4640 | */ |
||
| 4641 | public function reset_saved_auth_state() { |
||
| 4645 | |||
| 4646 | function verify_xml_rpc_signature() { |
||
| 4745 | |||
| 4746 | /** |
||
| 4747 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
| 4748 | */ |
||
| 4749 | function authenticate_jetpack( $user, $username, $password ) { |
||
| 4772 | |||
| 4773 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
| 4774 | // Uses the existing XMLRPC request signing implementation. |
||
| 4775 | function wp_rest_authenticate( $user ) { |
||
| 4868 | |||
| 4869 | /** |
||
| 4870 | * Report authentication status to the WP REST API. |
||
| 4871 | * |
||
| 4872 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
| 4873 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
| 4874 | */ |
||
| 4875 | public function wp_rest_authentication_errors( $value ) { |
||
| 4881 | |||
| 4882 | function add_nonce( $timestamp, $nonce ) { |
||
| 4920 | |||
| 4921 | /** |
||
| 4922 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
| 4923 | * Capture it here so we can verify the signature later. |
||
| 4924 | */ |
||
| 4925 | function xmlrpc_methods( $methods ) { |
||
| 4929 | |||
| 4930 | function public_xmlrpc_methods( $methods ) { |
||
| 4936 | |||
| 4937 | function jetpack_getOptions( $args ) { |
||
| 4977 | |||
| 4978 | function xmlrpc_options( $options ) { |
||
| 4996 | |||
| 4997 | public static function clean_nonces( $all = false ) { |
||
| 5018 | |||
| 5019 | /** |
||
| 5020 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
| 5021 | * SET: state( $key, $value ); |
||
| 5022 | * GET: $value = state( $key ); |
||
| 5023 | * |
||
| 5024 | * @param string $key |
||
| 5025 | * @param string $value |
||
| 5026 | * @param bool $restate private |
||
| 5027 | */ |
||
| 5028 | public static function state( $key = null, $value = null, $restate = false ) { |
||
| 5078 | |||
| 5079 | public static function restate() { |
||
| 5082 | |||
| 5083 | public static function check_privacy( $file ) { |
||
| 5118 | |||
| 5119 | /** |
||
| 5120 | * Helper method for multicall XMLRPC. |
||
| 5121 | */ |
||
| 5122 | public static function xmlrpc_async_call() { |
||
| 5164 | |||
| 5165 | public static function staticize_subdomain( $url ) { |
||
| 5200 | |||
| 5201 | /* JSON API Authorization */ |
||
| 5202 | |||
| 5203 | /** |
||
| 5204 | * Handles the login action for Authorizing the JSON API |
||
| 5205 | */ |
||
| 5206 | function login_form_json_api_authorization() { |
||
| 5215 | |||
| 5216 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
| 5217 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
| 5230 | |||
| 5231 | // Make sure the POSTed request is handled by the same action |
||
| 5232 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
| 5236 | |||
| 5237 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
| 5238 | function store_json_api_authorization_token( $user_login, $user ) { |
||
| 5244 | |||
| 5245 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
| 5246 | function allow_wpcom_public_api_domain( $domains ) { |
||
| 5250 | |||
| 5251 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
| 5252 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
| 5264 | |||
| 5265 | // Verifies the request by checking the signature |
||
| 5266 | function verify_json_api_authorization_request() { |
||
| 5345 | |||
| 5346 | function login_message_json_api_authorization( $message ) { |
||
| 5352 | |||
| 5353 | /** |
||
| 5354 | * Get $content_width, but with a <s>twist</s> filter. |
||
| 5355 | */ |
||
| 5356 | public static function get_content_width() { |
||
| 5367 | |||
| 5368 | /** |
||
| 5369 | * Centralize the function here until it gets added to core. |
||
| 5370 | * |
||
| 5371 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
| 5372 | * @param int $size Size of the avatar image |
||
| 5373 | * @param string $default URL to a default image to use if no avatar is available |
||
| 5374 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
| 5375 | * |
||
| 5376 | * @return array First element is the URL, second is the class. |
||
| 5377 | */ |
||
| 5378 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
| 5406 | |||
| 5407 | /** |
||
| 5408 | * Pings the WordPress.com Mirror Site for the specified options. |
||
| 5409 | * |
||
| 5410 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
| 5411 | * |
||
| 5412 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
| 5413 | */ |
||
| 5414 | public function get_cloud_site_options( $option_names ) { |
||
| 5430 | |||
| 5431 | /** |
||
| 5432 | * Checks if the site is currently in an identity crisis. |
||
| 5433 | * |
||
| 5434 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
| 5435 | */ |
||
| 5436 | public static function check_identity_crisis() { |
||
| 5443 | |||
| 5444 | /** |
||
| 5445 | * Checks whether the home and siteurl specifically are whitelisted |
||
| 5446 | * Written so that we don't have re-check $key and $value params every time |
||
| 5447 | * we want to check if this site is whitelisted, for example in footer.php |
||
| 5448 | * |
||
| 5449 | * @since 3.8.0 |
||
| 5450 | * @return bool True = already whitelisted False = not whitelisted |
||
| 5451 | */ |
||
| 5452 | public static function is_staging_site() { |
||
| 5511 | |||
| 5512 | /** |
||
| 5513 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
| 5514 | * |
||
| 5515 | * @return bool |
||
| 5516 | */ |
||
| 5517 | public static function validate_sync_error_idc_option() { |
||
| 5561 | |||
| 5562 | /** |
||
| 5563 | * Normalizes a url by doing three things: |
||
| 5564 | * - Strips protocol |
||
| 5565 | * - Strips www |
||
| 5566 | * - Adds a trailing slash |
||
| 5567 | * |
||
| 5568 | * @since 4.4.0 |
||
| 5569 | * @param string $url |
||
| 5570 | * @return WP_Error|string |
||
| 5571 | */ |
||
| 5572 | public static function normalize_url_protocol_agnostic( $url ) { |
||
| 5582 | |||
| 5583 | /** |
||
| 5584 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
| 5585 | * |
||
| 5586 | * @since 4.4.0 |
||
| 5587 | * |
||
| 5588 | * @param array $response |
||
| 5589 | * @return array Array of the local urls, wpcom urls, and error code |
||
| 5590 | */ |
||
| 5591 | public static function get_sync_error_idc_option( $response = array() ) { |
||
| 5615 | |||
| 5616 | /** |
||
| 5617 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
| 5618 | * If set to true, the site will be put into staging mode. |
||
| 5619 | * |
||
| 5620 | * @since 4.3.2 |
||
| 5621 | * @return bool |
||
| 5622 | */ |
||
| 5623 | public static function sync_idc_optin() { |
||
| 5641 | |||
| 5642 | /** |
||
| 5643 | * Maybe Use a .min.css stylesheet, maybe not. |
||
| 5644 | * |
||
| 5645 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
| 5646 | */ |
||
| 5647 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
| 5682 | |||
| 5683 | /** |
||
| 5684 | * Maybe inlines a stylesheet. |
||
| 5685 | * |
||
| 5686 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
| 5687 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
| 5688 | * |
||
| 5689 | * Attached to `style_loader_tag` filter. |
||
| 5690 | * |
||
| 5691 | * @param string $tag The tag that would link to the external asset. |
||
| 5692 | * @param string $handle The registered handle of the script in question. |
||
| 5693 | * |
||
| 5694 | * @return string |
||
| 5695 | */ |
||
| 5696 | public static function maybe_inline_style( $tag, $handle ) { |
||
| 5746 | |||
| 5747 | /** |
||
| 5748 | * Loads a view file from the views |
||
| 5749 | * |
||
| 5750 | * Data passed in with the $data parameter will be available in the |
||
| 5751 | * template file as $data['value'] |
||
| 5752 | * |
||
| 5753 | * @param string $template - Template file to load |
||
| 5754 | * @param array $data - Any data to pass along to the template |
||
| 5755 | * @return boolean - If template file was found |
||
| 5756 | **/ |
||
| 5757 | public function load_view( $template, $data = array() ) { |
||
| 5768 | |||
| 5769 | /** |
||
| 5770 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
| 5771 | */ |
||
| 5772 | public function deprecated_hooks() { |
||
| 5821 | |||
| 5822 | /** |
||
| 5823 | * Converts any url in a stylesheet, to the correct absolute url. |
||
| 5824 | * |
||
| 5825 | * Considerations: |
||
| 5826 | * - Normal, relative URLs `feh.png` |
||
| 5827 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
| 5828 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
| 5829 | * - Absolute URLs `http://domain.com/feh.png` |
||
| 5830 | * - Domain root relative URLs `/feh.png` |
||
| 5831 | * |
||
| 5832 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
| 5833 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
| 5834 | * |
||
| 5835 | * @return mixed|string |
||
| 5836 | */ |
||
| 5837 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
| 5881 | |||
| 5882 | /** |
||
| 5883 | * This methods removes all of the registered css files on the front end |
||
| 5884 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
| 5885 | * all the files into one file. |
||
| 5886 | * |
||
| 5887 | * Pros: |
||
| 5888 | * - Uses only ONE css asset connection instead of 15 |
||
| 5889 | * - Saves a minimum of 56k |
||
| 5890 | * - Reduces server load |
||
| 5891 | * - Reduces time to first painted byte |
||
| 5892 | * |
||
| 5893 | * Cons: |
||
| 5894 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
| 5895 | * should not cause any issues with themes. |
||
| 5896 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
| 5897 | * jetpack_implode_frontend_css filter for a workaround |
||
| 5898 | * |
||
| 5899 | * For some situations developers may wish to disable css imploding and |
||
| 5900 | * instead operate in legacy mode where each file loads seperately and |
||
| 5901 | * can be edited individually or dequeued. This can be accomplished with |
||
| 5902 | * the following line: |
||
| 5903 | * |
||
| 5904 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
| 5905 | * |
||
| 5906 | * @since 3.2 |
||
| 5907 | **/ |
||
| 5908 | public function implode_frontend_css( $travis_test = false ) { |
||
| 5960 | |||
| 5961 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
| 5971 | |||
| 5972 | /* |
||
| 5973 | * Check the heartbeat data |
||
| 5974 | * |
||
| 5975 | * Organizes the heartbeat data by severity. For example, if the site |
||
| 5976 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
| 5977 | * |
||
| 5978 | * Data will be added to "caution" array, if it either: |
||
| 5979 | * - Out of date Jetpack version |
||
| 5980 | * - Out of date WP version |
||
| 5981 | * - Out of date PHP version |
||
| 5982 | * |
||
| 5983 | * $return array $filtered_data |
||
| 5984 | */ |
||
| 5985 | public static function jetpack_check_heartbeat_data() { |
||
| 6038 | |||
| 6039 | |||
| 6040 | /* |
||
| 6041 | * This method is used to organize all options that can be reset |
||
| 6042 | * without disconnecting Jetpack. |
||
| 6043 | * |
||
| 6044 | * It is used in class.jetpack-cli.php to reset options |
||
| 6045 | * |
||
| 6046 | * @return array of options to delete. |
||
| 6047 | */ |
||
| 6048 | public static function get_jetpack_options_for_reset() { |
||
| 6115 | |||
| 6116 | /** |
||
| 6117 | * Check if an option of a Jetpack module has been updated. |
||
| 6118 | * |
||
| 6119 | * If any module option has been updated before Jump Start has been dismissed, |
||
| 6120 | * update the 'jumpstart' option so we can hide Jump Start. |
||
| 6121 | * |
||
| 6122 | * @param string $option_name |
||
| 6123 | * |
||
| 6124 | * @return bool |
||
| 6125 | */ |
||
| 6126 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
| 6147 | |||
| 6148 | /* |
||
| 6149 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
| 6150 | * so we can bring them directly to their site in calypso. |
||
| 6151 | * |
||
| 6152 | * @param string | url |
||
| 6153 | * @return string | url without the guff |
||
| 6154 | */ |
||
| 6155 | public static function build_raw_urls( $url ) { |
||
| 6161 | |||
| 6162 | /** |
||
| 6163 | * Stores and prints out domains to prefetch for page speed optimization. |
||
| 6164 | * |
||
| 6165 | * @param mixed $new_urls |
||
| 6166 | */ |
||
| 6167 | public static function dns_prefetch( $new_urls = null ) { |
||
| 6184 | |||
| 6185 | public function wp_dashboard_setup() { |
||
| 6213 | |||
| 6214 | /** |
||
| 6215 | * @param mixed $result Value for the user's option |
||
| 6216 | * @return mixed |
||
| 6217 | */ |
||
| 6218 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
| 6243 | |||
| 6244 | public static function dashboard_widget() { |
||
| 6252 | |||
| 6253 | public static function dashboard_widget_footer() { |
||
| 6286 | |||
| 6287 | public function dashboard_widget_connect_to_wpcom() { |
||
| 6309 | |||
| 6310 | /** |
||
| 6311 | * Return string containing the Jetpack logo. |
||
| 6312 | * |
||
| 6313 | * @since 3.9.0 |
||
| 6314 | * |
||
| 6315 | * @return string |
||
| 6316 | */ |
||
| 6317 | public static function get_jp_emblem() { |
||
| 6320 | |||
| 6321 | /* |
||
| 6322 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
| 6323 | */ |
||
| 6324 | function jetpack_icon_user_connected( $columns ) { |
||
| 6328 | |||
| 6329 | /* |
||
| 6330 | * Show Jetpack icon if the user is linked. |
||
| 6331 | */ |
||
| 6332 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
| 6344 | |||
| 6345 | /* |
||
| 6346 | * Style the Jetpack user column |
||
| 6347 | */ |
||
| 6348 | function jetpack_user_col_style() { |
||
| 6365 | } |
||
| 6366 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.