Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Jetpack { |
||
| 28 | public $xmlrpc_server = null; |
||
| 29 | |||
| 30 | private $xmlrpc_verification = null; |
||
| 31 | private $rest_authentication_status = null; |
||
| 32 | |||
| 33 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array The handles of styles that are concatenated into jetpack.css |
||
| 37 | */ |
||
| 38 | public $concatenated_style_handles = array( |
||
| 39 | 'jetpack-carousel', |
||
| 40 | 'grunion.css', |
||
| 41 | 'the-neverending-homepage', |
||
| 42 | 'jetpack_likes', |
||
| 43 | 'jetpack_related-posts', |
||
| 44 | 'sharedaddy', |
||
| 45 | 'jetpack-slideshow', |
||
| 46 | 'presentations', |
||
| 47 | 'jetpack-subscriptions', |
||
| 48 | 'jetpack-responsive-videos-style', |
||
| 49 | 'jetpack-social-menu', |
||
| 50 | 'tiled-gallery', |
||
| 51 | 'jetpack_display_posts_widget', |
||
| 52 | 'gravatar-profile-widget', |
||
| 53 | 'goodreads-widget', |
||
| 54 | 'jetpack_social_media_icons_widget', |
||
| 55 | 'jetpack-top-posts-widget', |
||
| 56 | 'jetpack_image_widget', |
||
| 57 | 'jetpack-my-community-widget', |
||
| 58 | 'wordads', |
||
| 59 | ); |
||
| 60 | |||
| 61 | public $plugins_to_deactivate = array( |
||
| 62 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 63 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 64 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
| 65 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
| 66 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
| 67 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
| 68 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
| 69 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
| 70 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
| 71 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
| 72 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
| 73 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
| 74 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
| 75 | 'omnisearch' => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ), |
||
| 76 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
| 77 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
| 78 | ); |
||
| 79 | |||
| 80 | static $capability_translations = array( |
||
|
|
|||
| 81 | 'administrator' => 'manage_options', |
||
| 82 | 'editor' => 'edit_others_posts', |
||
| 83 | 'author' => 'publish_posts', |
||
| 84 | 'contributor' => 'edit_posts', |
||
| 85 | 'subscriber' => 'read', |
||
| 86 | ); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
| 90 | * if the plugins are active. Used by filter_default_modules |
||
| 91 | * |
||
| 92 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
| 93 | * change `module-slug` and add this to your plugin: |
||
| 94 | * |
||
| 95 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
| 96 | * function my_jetpack_get_default_modules( $modules ) { |
||
| 97 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
| 98 | * } |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private $conflicting_plugins = array( |
||
| 103 | 'comments' => array( |
||
| 104 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
| 105 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
| 106 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
| 107 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
| 108 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
| 109 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
| 110 | ), |
||
| 111 | 'contact-form' => array( |
||
| 112 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
| 113 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
| 114 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
| 115 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
| 116 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
| 117 | ), |
||
| 118 | 'minileven' => array( |
||
| 119 | 'WPtouch' => 'wptouch/wptouch.php', |
||
| 120 | ), |
||
| 121 | 'latex' => array( |
||
| 122 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
| 123 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
| 124 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
| 125 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
| 126 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
| 127 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
| 128 | ), |
||
| 129 | 'protect' => array( |
||
| 130 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
| 131 | 'Captcha' => 'captcha/captcha.php', |
||
| 132 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
| 133 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
| 134 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
| 135 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
| 136 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
| 137 | 'Security-protection' => 'security-protection/security-protection.php', |
||
| 138 | 'Login Security' => 'login-security/login-security.php', |
||
| 139 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
| 140 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
| 141 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
| 142 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
| 143 | ), |
||
| 144 | 'random-redirect' => array( |
||
| 145 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
| 146 | ), |
||
| 147 | 'related-posts' => array( |
||
| 148 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
| 149 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
| 150 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
| 151 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
| 152 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
| 153 | 'outbrain' => 'outbrain/outbrain.php', |
||
| 154 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 155 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
| 156 | ), |
||
| 157 | 'sharedaddy' => array( |
||
| 158 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
| 159 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
| 160 | 'ShareThis' => 'share-this/sharethis.php', |
||
| 161 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 162 | ), |
||
| 163 | 'seo-tools' => array( |
||
| 164 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 165 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 166 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 167 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 168 | ), |
||
| 169 | 'verification-tools' => array( |
||
| 170 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 171 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 172 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 173 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 174 | ), |
||
| 175 | 'widget-visibility' => array( |
||
| 176 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
| 177 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
| 178 | ), |
||
| 179 | 'sitemaps' => array( |
||
| 180 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
| 181 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
| 182 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
| 183 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
| 184 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
| 185 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 186 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 187 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 188 | 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php', |
||
| 189 | 'Sitemap' => 'sitemap/sitemap.php', |
||
| 190 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
| 191 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
| 192 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
| 193 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
| 194 | ), |
||
| 195 | ); |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
| 199 | * |
||
| 200 | * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate |
||
| 201 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
| 202 | * |
||
| 203 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
| 204 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
| 205 | */ |
||
| 206 | private $open_graph_conflicting_plugins = array( |
||
| 207 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
| 208 | // 2 Click Social Media Buttons |
||
| 209 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
| 210 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
| 211 | 'autodescription/autodescription.php', // The SEO Framework |
||
| 212 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
| 213 | 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', |
||
| 214 | // Open Graph Meta Tags by Heateor |
||
| 215 | 'facebook/facebook.php', // Facebook (official plugin) |
||
| 216 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
| 217 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
| 218 | // Facebook Featured Image & OG Meta Tags |
||
| 219 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
| 220 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
| 221 | // Facebook Open Graph Meta Tags for WordPress |
||
| 222 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
| 223 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
| 224 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
| 225 | // Fedmich's Facebook Open Graph Meta |
||
| 226 | 'header-footer/plugin.php', // Header and Footer |
||
| 227 | 'network-publisher/networkpub.php', // Network Publisher |
||
| 228 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
| 229 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
| 230 | // NextScripts SNAP |
||
| 231 | 'og-tags/og-tags.php', // OG Tags |
||
| 232 | 'opengraph/opengraph.php', // Open Graph |
||
| 233 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
| 234 | // Open Graph Protocol Framework |
||
| 235 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
| 236 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
| 237 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
| 238 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
| 239 | 'sharepress/sharepress.php', // SharePress |
||
| 240 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
| 241 | 'social-discussions/social-discussions.php', // Social Discussions |
||
| 242 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
| 243 | 'socialize/socialize.php', // Socialize |
||
| 244 | 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™ |
||
| 245 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
| 246 | // Tweet, Like, Google +1 and Share |
||
| 247 | 'wordbooker/wordbooker.php', // Wordbooker |
||
| 248 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
| 249 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
| 250 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
| 251 | // WP Facebook Like Send & Open Graph Meta |
||
| 252 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
| 253 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
| 254 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
| 255 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php' // WP Facebook Like Button |
||
| 256 | ); |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
| 260 | */ |
||
| 261 | private $twitter_cards_conflicting_plugins = array( |
||
| 262 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
| 263 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
| 264 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
| 265 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
| 266 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
| 267 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
| 268 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
| 269 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
| 270 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
| 271 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
| 272 | ); |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Message to display in admin_notice |
||
| 276 | * @var string |
||
| 277 | */ |
||
| 278 | public $message = ''; |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Error to display in admin_notice |
||
| 282 | * @var string |
||
| 283 | */ |
||
| 284 | public $error = ''; |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Modules that need more privacy description. |
||
| 288 | * @var string |
||
| 289 | */ |
||
| 290 | public $privacy_checks = ''; |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Stats to record once the page loads |
||
| 294 | * |
||
| 295 | * @var array |
||
| 296 | */ |
||
| 297 | public $stats = array(); |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Jetpack_Sync object |
||
| 301 | */ |
||
| 302 | public $sync; |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Verified data for JSON authorization request |
||
| 306 | */ |
||
| 307 | public $json_api_authorization_request = array(); |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Holds the singleton instance of this class |
||
| 311 | * @since 2.3.3 |
||
| 312 | * @var Jetpack |
||
| 313 | */ |
||
| 314 | static $instance = false; |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Singleton |
||
| 318 | * @static |
||
| 319 | */ |
||
| 320 | public static function init() { |
||
| 321 | if ( ! self::$instance ) { |
||
| 322 | self::$instance = new Jetpack; |
||
| 323 | |||
| 324 | self::$instance->plugin_upgrade(); |
||
| 325 | } |
||
| 326 | |||
| 327 | return self::$instance; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Must never be called statically |
||
| 332 | */ |
||
| 333 | function plugin_upgrade() { |
||
| 334 | if ( Jetpack::is_active() ) { |
||
| 335 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
| 336 | if ( JETPACK__VERSION != $version ) { |
||
| 337 | |||
| 338 | // check which active modules actually exist and remove others from active_modules list |
||
| 339 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
| 340 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
| 341 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
| 342 | Jetpack::update_active_modules( $modules ); |
||
| 343 | } |
||
| 344 | |||
| 345 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
| 346 | |||
| 347 | // Upgrade to 4.3.0 |
||
| 348 | if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) { |
||
| 349 | Jetpack_Options::delete_option( 'identity_crisis_whitelist' ); |
||
| 350 | } |
||
| 351 | |||
| 352 | // Make sure Markdown for posts gets turned back on |
||
| 353 | if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) { |
||
| 354 | update_option( 'wpcom_publish_posts_with_markdown', true ); |
||
| 355 | } |
||
| 356 | |||
| 357 | if ( did_action( 'wp_loaded' ) ) { |
||
| 358 | self::upgrade_on_load(); |
||
| 359 | } else { |
||
| 360 | add_action( |
||
| 361 | 'wp_loaded', |
||
| 362 | array( __CLASS__, 'upgrade_on_load' ) |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Runs upgrade routines that need to have modules loaded. |
||
| 371 | */ |
||
| 372 | static function upgrade_on_load() { |
||
| 373 | |||
| 374 | // Not attempting any upgrades if jetpack_modules_loaded did not fire. |
||
| 375 | // This can happen in case Jetpack has been just upgraded and is |
||
| 376 | // being initialized late during the page load. In this case we wait |
||
| 377 | // until the next proper admin page load with Jetpack active. |
||
| 378 | if ( ! did_action( 'jetpack_modules_loaded' ) ) { |
||
| 379 | return; |
||
| 380 | } |
||
| 381 | |||
| 382 | Jetpack::maybe_set_version_option(); |
||
| 383 | |||
| 384 | if ( class_exists( 'Jetpack_Widget_Conditions' ) ) { |
||
| 385 | Jetpack_Widget_Conditions::migrate_post_type_rules(); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | static function activate_manage( ) { |
||
| 390 | if ( did_action( 'init' ) || current_filter() == 'init' ) { |
||
| 391 | self::activate_module( 'manage', false, false ); |
||
| 392 | } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) { |
||
| 393 | add_action( 'init', array( __CLASS__, 'activate_manage' ) ); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | static function update_active_modules( $modules ) { |
||
| 398 | $current_modules = Jetpack_Options::get_option( 'active_modules', array() ); |
||
| 399 | |||
| 400 | $success = Jetpack_Options::update_option( 'active_modules', array_unique( $modules ) ); |
||
| 401 | |||
| 402 | if ( is_array( $modules ) && is_array( $current_modules ) ) { |
||
| 403 | $new_active_modules = array_diff( $modules, $current_modules ); |
||
| 404 | foreach( $new_active_modules as $module ) { |
||
| 405 | /** |
||
| 406 | * Fires when a specific module is activated. |
||
| 407 | * |
||
| 408 | * @since 1.9.0 |
||
| 409 | * |
||
| 410 | * @param string $module Module slug. |
||
| 411 | * @param boolean $success whether the module was activated. @since 4.2 |
||
| 412 | */ |
||
| 413 | do_action( 'jetpack_activate_module', $module, $success ); |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Fires when a module is activated. |
||
| 417 | * The dynamic part of the filter, $module, is the module slug. |
||
| 418 | * |
||
| 419 | * @since 1.9.0 |
||
| 420 | * |
||
| 421 | * @param string $module Module slug. |
||
| 422 | */ |
||
| 423 | do_action( "jetpack_activate_module_$module", $module ); |
||
| 424 | } |
||
| 425 | |||
| 426 | $new_deactive_modules = array_diff( $current_modules, $modules ); |
||
| 427 | foreach( $new_deactive_modules as $module ) { |
||
| 428 | /** |
||
| 429 | * Fired after a module has been deactivated. |
||
| 430 | * |
||
| 431 | * @since 4.2.0 |
||
| 432 | * |
||
| 433 | * @param string $module Module slug. |
||
| 434 | * @param boolean $success whether the module was deactivated. |
||
| 435 | */ |
||
| 436 | do_action( 'jetpack_deactivate_module', $module, $success ); |
||
| 437 | /** |
||
| 438 | * Fires when a module is deactivated. |
||
| 439 | * The dynamic part of the filter, $module, is the module slug. |
||
| 440 | * |
||
| 441 | * @since 1.9.0 |
||
| 442 | * |
||
| 443 | * @param string $module Module slug. |
||
| 444 | */ |
||
| 445 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | return $success; |
||
| 450 | } |
||
| 451 | |||
| 452 | static function delete_active_modules() { |
||
| 453 | self::update_active_modules( array() ); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Constructor. Initializes WordPress hooks |
||
| 458 | */ |
||
| 459 | private function __construct() { |
||
| 460 | /* |
||
| 461 | * Check for and alert any deprecated hooks |
||
| 462 | */ |
||
| 463 | add_action( 'init', array( $this, 'deprecated_hooks' ) ); |
||
| 464 | |||
| 465 | /* |
||
| 466 | * Load things that should only be in Network Admin. |
||
| 467 | * |
||
| 468 | * For now blow away everything else until a more full |
||
| 469 | * understanding of what is needed at the network level is |
||
| 470 | * available |
||
| 471 | */ |
||
| 472 | if( is_multisite() ) { |
||
| 473 | Jetpack_Network::init(); |
||
| 474 | } |
||
| 475 | |||
| 476 | add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 ); |
||
| 477 | |||
| 478 | // Unlink user before deleting the user from .com |
||
| 479 | add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); |
||
| 480 | add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); |
||
| 481 | |||
| 482 | if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { |
||
| 483 | @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. |
||
| 484 | |||
| 485 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; |
||
| 486 | $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); |
||
| 487 | |||
| 488 | $this->require_jetpack_authentication(); |
||
| 489 | |||
| 490 | if ( Jetpack::is_active() ) { |
||
| 491 | // Hack to preserve $HTTP_RAW_POST_DATA |
||
| 492 | add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 493 | |||
| 494 | $signed = $this->verify_xml_rpc_signature(); |
||
| 495 | if ( $signed && ! is_wp_error( $signed ) ) { |
||
| 496 | // The actual API methods. |
||
| 497 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); |
||
| 498 | } else { |
||
| 499 | // The jetpack.authorize method should be available for unauthenticated users on a site with an |
||
| 500 | // active Jetpack connection, so that additional users can link their account. |
||
| 501 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) ); |
||
| 502 | } |
||
| 503 | } else { |
||
| 504 | // The bootstrap API methods. |
||
| 505 | add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); |
||
| 506 | } |
||
| 507 | |||
| 508 | // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. |
||
| 509 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
||
| 510 | } elseif ( |
||
| 511 | is_admin() && |
||
| 512 | isset( $_POST['action'] ) && ( |
||
| 513 | 'jetpack_upload_file' == $_POST['action'] || |
||
| 514 | 'jetpack_update_file' == $_POST['action'] |
||
| 515 | ) |
||
| 516 | ) { |
||
| 517 | $this->require_jetpack_authentication(); |
||
| 518 | $this->add_remote_request_handlers(); |
||
| 519 | } else { |
||
| 520 | if ( Jetpack::is_active() ) { |
||
| 521 | add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); |
||
| 522 | add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | if ( Jetpack::is_active() ) { |
||
| 527 | Jetpack_Heartbeat::init(); |
||
| 528 | } |
||
| 529 | |||
| 530 | add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) ); |
||
| 531 | add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) ); |
||
| 532 | |||
| 533 | add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); |
||
| 534 | if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { |
||
| 535 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 536 | } |
||
| 537 | |||
| 538 | add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); |
||
| 539 | |||
| 540 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 541 | add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); |
||
| 542 | |||
| 543 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
||
| 544 | |||
| 545 | add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); |
||
| 546 | // Filter the dashboard meta box order to swap the new one in in place of the old one. |
||
| 547 | add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); |
||
| 548 | |||
| 549 | // returns HTTPS support status |
||
| 550 | add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) ); |
||
| 551 | |||
| 552 | // If any module option is updated before Jump Start is dismissed, hide Jump Start. |
||
| 553 | add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) ); |
||
| 554 | |||
| 555 | // JITM AJAX callback function |
||
| 556 | add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); |
||
| 557 | |||
| 558 | // Universal ajax callback for all tracking events triggered via js |
||
| 559 | add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) ); |
||
| 560 | |||
| 561 | add_action( 'wp_loaded', array( $this, 'register_assets' ) ); |
||
| 562 | add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 563 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 564 | add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); |
||
| 565 | |||
| 566 | add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); |
||
| 567 | |||
| 568 | /** |
||
| 569 | * These actions run checks to load additional files. |
||
| 570 | * They check for external files or plugins, so they need to run as late as possible. |
||
| 571 | */ |
||
| 572 | add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); |
||
| 573 | add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); |
||
| 574 | add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); |
||
| 575 | |||
| 576 | add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); |
||
| 577 | add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); |
||
| 578 | |||
| 579 | add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); |
||
| 580 | |||
| 581 | add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); |
||
| 582 | add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); |
||
| 583 | |||
| 584 | // A filter to control all just in time messages |
||
| 585 | add_filter( 'jetpack_just_in_time_msgs', '__return_true' ); |
||
| 586 | |||
| 587 | // Update the Jetpack plan from API on heartbeats |
||
| 588 | add_action( 'jetpack_heartbeat', array( $this, 'refresh_active_plan_from_wpcom' ) ); |
||
| 589 | |||
| 590 | /** |
||
| 591 | * This is the hack to concatinate all css files into one. |
||
| 592 | * For description and reasoning see the implode_frontend_css method |
||
| 593 | * |
||
| 594 | * Super late priority so we catch all the registered styles |
||
| 595 | */ |
||
| 596 | if( !is_admin() ) { |
||
| 597 | add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first |
||
| 598 | add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | function jetpack_admin_ajax_tracks_callback() { |
||
| 603 | // Check for nonce |
||
| 604 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
| 605 | wp_die( 'Permissions check failed.' ); |
||
| 606 | } |
||
| 607 | |||
| 608 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
| 609 | wp_die( 'No valid event name or type.' ); |
||
| 610 | } |
||
| 611 | |||
| 612 | $tracks_data = array(); |
||
| 613 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
| 614 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
| 615 | } |
||
| 616 | |||
| 617 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
| 618 | wp_send_json_success(); |
||
| 619 | wp_die(); |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * The callback for the JITM ajax requests. |
||
| 624 | */ |
||
| 625 | function jetpack_jitm_ajax_callback() { |
||
| 626 | // Check for nonce |
||
| 627 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
| 628 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
| 629 | } |
||
| 630 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
| 631 | $module_slug = $_REQUEST['jitmModule']; |
||
| 632 | Jetpack::log( 'activate', $module_slug ); |
||
| 633 | Jetpack::activate_module( $module_slug, false, false ); |
||
| 634 | Jetpack::state( 'message', 'no_message' ); |
||
| 635 | |||
| 636 | //A Jetpack module is being activated through a JITM, track it |
||
| 637 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
| 638 | $this->do_stats( 'server_side' ); |
||
| 639 | |||
| 640 | wp_send_json_success(); |
||
| 641 | } |
||
| 642 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
| 643 | // get the hide_jitm options array |
||
| 644 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
| 645 | $module_slug = $_REQUEST['jitmModule']; |
||
| 646 | |||
| 647 | if( ! $jetpack_hide_jitm ) { |
||
| 648 | $jetpack_hide_jitm = array( |
||
| 649 | $module_slug => 'hide' |
||
| 650 | ); |
||
| 651 | } else { |
||
| 652 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
| 653 | } |
||
| 654 | |||
| 655 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
| 656 | |||
| 657 | //jitm is being dismissed forever, track it |
||
| 658 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
| 659 | $this->do_stats( 'server_side' ); |
||
| 660 | |||
| 661 | wp_send_json_success(); |
||
| 662 | } |
||
| 663 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
| 664 | $module_slug = $_REQUEST['jitmModule']; |
||
| 665 | |||
| 666 | // User went to WordPress.com, track this |
||
| 667 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
| 668 | $this->do_stats( 'server_side' ); |
||
| 669 | |||
| 670 | wp_send_json_success(); |
||
| 671 | } |
||
| 672 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
| 673 | $track = $_REQUEST['jitmModule']; |
||
| 674 | |||
| 675 | // User is viewing JITM, track it. |
||
| 676 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
| 677 | $this->do_stats( 'server_side' ); |
||
| 678 | |||
| 679 | wp_send_json_success(); |
||
| 680 | } |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
| 685 | */ |
||
| 686 | function __destruct() { |
||
| 687 | if ( ! empty( $this->stats ) ) { |
||
| 688 | $this->do_stats( 'server_side' ); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
| 693 | switch( $cap ) { |
||
| 694 | case 'jetpack_connect' : |
||
| 695 | case 'jetpack_reconnect' : |
||
| 696 | if ( Jetpack::is_development_mode() ) { |
||
| 697 | $caps = array( 'do_not_allow' ); |
||
| 698 | break; |
||
| 699 | } |
||
| 700 | /** |
||
| 701 | * Pass through. If it's not development mode, these should match disconnect. |
||
| 702 | * Let users disconnect if it's development mode, just in case things glitch. |
||
| 703 | */ |
||
| 704 | case 'jetpack_disconnect' : |
||
| 705 | /** |
||
| 706 | * In multisite, can individual site admins manage their own connection? |
||
| 707 | * |
||
| 708 | * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. |
||
| 709 | */ |
||
| 710 | if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
| 711 | if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { |
||
| 712 | /** |
||
| 713 | * We need to update the option name -- it's terribly unclear which |
||
| 714 | * direction the override goes. |
||
| 715 | * |
||
| 716 | * @todo: Update the option name to `sub-sites-can-manage-own-connections` |
||
| 717 | */ |
||
| 718 | $caps = array( 'do_not_allow' ); |
||
| 719 | break; |
||
| 720 | } |
||
| 721 | } |
||
| 722 | |||
| 723 | $caps = array( 'manage_options' ); |
||
| 724 | break; |
||
| 725 | case 'jetpack_manage_modules' : |
||
| 726 | case 'jetpack_activate_modules' : |
||
| 727 | case 'jetpack_deactivate_modules' : |
||
| 728 | $caps = array( 'manage_options' ); |
||
| 729 | break; |
||
| 730 | case 'jetpack_configure_modules' : |
||
| 731 | $caps = array( 'manage_options' ); |
||
| 732 | break; |
||
| 733 | case 'jetpack_network_admin_page': |
||
| 734 | case 'jetpack_network_settings_page': |
||
| 735 | $caps = array( 'manage_network_plugins' ); |
||
| 736 | break; |
||
| 737 | case 'jetpack_network_sites_page': |
||
| 738 | $caps = array( 'manage_sites' ); |
||
| 739 | break; |
||
| 740 | case 'jetpack_admin_page' : |
||
| 741 | if ( Jetpack::is_development_mode() ) { |
||
| 742 | $caps = array( 'manage_options' ); |
||
| 743 | break; |
||
| 744 | } else { |
||
| 745 | $caps = array( 'read' ); |
||
| 746 | } |
||
| 747 | break; |
||
| 748 | case 'jetpack_connect_user' : |
||
| 749 | if ( Jetpack::is_development_mode() ) { |
||
| 750 | $caps = array( 'do_not_allow' ); |
||
| 751 | break; |
||
| 752 | } |
||
| 753 | $caps = array( 'read' ); |
||
| 754 | break; |
||
| 755 | } |
||
| 756 | return $caps; |
||
| 757 | } |
||
| 758 | |||
| 759 | function require_jetpack_authentication() { |
||
| 760 | // Don't let anyone authenticate |
||
| 761 | $_COOKIE = array(); |
||
| 762 | remove_all_filters( 'authenticate' ); |
||
| 763 | remove_all_actions( 'wp_login_failed' ); |
||
| 764 | |||
| 765 | if ( Jetpack::is_active() ) { |
||
| 766 | // Allow Jetpack authentication |
||
| 767 | add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); |
||
| 768 | } |
||
| 769 | } |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Load language files |
||
| 773 | * @action plugins_loaded |
||
| 774 | */ |
||
| 775 | public static function plugin_textdomain() { |
||
| 776 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
| 777 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
| 778 | } |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Register assets for use in various modules and the Jetpack admin page. |
||
| 782 | * |
||
| 783 | * @uses wp_script_is, wp_register_script, plugins_url |
||
| 784 | * @action wp_loaded |
||
| 785 | * @return null |
||
| 786 | */ |
||
| 787 | public function register_assets() { |
||
| 788 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
| 789 | wp_register_script( 'spin', plugins_url( '_inc/spin.js', JETPACK__PLUGIN_FILE ), false, '1.3' ); |
||
| 790 | } |
||
| 791 | |||
| 792 | View Code Duplication | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
|
| 793 | wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' ); |
||
| 794 | } |
||
| 795 | |||
| 796 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
| 797 | wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' ); |
||
| 798 | } |
||
| 799 | |||
| 800 | View Code Duplication | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
|
| 801 | wp_register_script( 'jetpack-twitter-timeline', plugins_url( '_inc/twitter-timeline.js', JETPACK__PLUGIN_FILE ) , array( 'jquery' ), '4.0.0', true ); |
||
| 802 | } |
||
| 803 | |||
| 804 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
| 805 | wp_register_script( 'jetpack-facebook-embed', plugins_url( '_inc/facebook-embed.js', __FILE__ ), array( 'jquery' ), null, true ); |
||
| 806 | |||
| 807 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
| 808 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
| 809 | if ( ! is_numeric( $fb_app_id ) ) { |
||
| 810 | $fb_app_id = ''; |
||
| 811 | } |
||
| 812 | wp_localize_script( |
||
| 813 | 'jetpack-facebook-embed', |
||
| 814 | 'jpfbembed', |
||
| 815 | array( |
||
| 816 | 'appid' => $fb_app_id, |
||
| 817 | 'locale' => $this->get_locale(), |
||
| 818 | ) |
||
| 819 | ); |
||
| 820 | } |
||
| 821 | |||
| 822 | /** |
||
| 823 | * As jetpack_register_genericons is by default fired off a hook, |
||
| 824 | * the hook may have already fired by this point. |
||
| 825 | * So, let's just trigger it manually. |
||
| 826 | */ |
||
| 827 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
| 828 | jetpack_register_genericons(); |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Register the social logos |
||
| 832 | */ |
||
| 833 | require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' ); |
||
| 834 | jetpack_register_social_logos(); |
||
| 835 | |||
| 836 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
| 837 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Guess locale from language code. |
||
| 842 | * |
||
| 843 | * @param string $lang Language code. |
||
| 844 | * @return string|bool |
||
| 845 | */ |
||
| 846 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
| 847 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
| 848 | return 'en_US'; |
||
| 849 | } |
||
| 850 | |||
| 851 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
| 852 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 853 | return false; |
||
| 854 | } |
||
| 855 | |||
| 856 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 857 | } |
||
| 858 | |||
| 859 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 860 | // WP.com: get_locale() returns 'it' |
||
| 861 | $locale = GP_Locales::by_slug( $lang ); |
||
| 862 | } else { |
||
| 863 | // Jetpack: get_locale() returns 'it_IT'; |
||
| 864 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
| 865 | } |
||
| 866 | |||
| 867 | if ( ! $locale ) { |
||
| 868 | return false; |
||
| 869 | } |
||
| 870 | |||
| 871 | if ( empty( $locale->facebook_locale ) ) { |
||
| 872 | if ( empty( $locale->wp_locale ) ) { |
||
| 873 | return false; |
||
| 874 | } else { |
||
| 875 | // Facebook SDK is smart enough to fall back to en_US if a |
||
| 876 | // locale isn't supported. Since supported Facebook locales |
||
| 877 | // can fall out of sync, we'll attempt to use the known |
||
| 878 | // wp_locale value and rely on said fallback. |
||
| 879 | return $locale->wp_locale; |
||
| 880 | } |
||
| 881 | } |
||
| 882 | |||
| 883 | return $locale->facebook_locale; |
||
| 884 | } |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Get the locale. |
||
| 888 | * |
||
| 889 | * @return string|bool |
||
| 890 | */ |
||
| 891 | function get_locale() { |
||
| 892 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
| 893 | |||
| 894 | if ( ! $locale ) { |
||
| 895 | $locale = 'en_US'; |
||
| 896 | } |
||
| 897 | |||
| 898 | return $locale; |
||
| 899 | } |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Device Pixels support |
||
| 903 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
| 904 | */ |
||
| 905 | function devicepx() { |
||
| 906 | if ( Jetpack::is_active() ) { |
||
| 907 | wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true ); |
||
| 908 | } |
||
| 909 | } |
||
| 910 | |||
| 911 | /** |
||
| 912 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
| 913 | * @param bool $option |
||
| 914 | * @return string |
||
| 915 | */ |
||
| 916 | public function jetpack_main_network_site_option( $option ) { |
||
| 917 | return network_site_url(); |
||
| 918 | } |
||
| 919 | /** |
||
| 920 | * Network Name. |
||
| 921 | */ |
||
| 922 | static function network_name( $option = null ) { |
||
| 923 | global $current_site; |
||
| 924 | return $current_site->site_name; |
||
| 925 | } |
||
| 926 | /** |
||
| 927 | * Does the network allow new user and site registrations. |
||
| 928 | * @return string |
||
| 929 | */ |
||
| 930 | static function network_allow_new_registrations( $option = null ) { |
||
| 931 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
| 932 | } |
||
| 933 | /** |
||
| 934 | * Does the network allow admins to add new users. |
||
| 935 | * @return boolian |
||
| 936 | */ |
||
| 937 | static function network_add_new_users( $option = null ) { |
||
| 938 | return (bool) get_site_option( 'add_new_users' ); |
||
| 939 | } |
||
| 940 | /** |
||
| 941 | * File upload psace left per site in MB. |
||
| 942 | * -1 means NO LIMIT. |
||
| 943 | * @return number |
||
| 944 | */ |
||
| 945 | static function network_site_upload_space( $option = null ) { |
||
| 946 | // value in MB |
||
| 947 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
| 948 | } |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Network allowed file types. |
||
| 952 | * @return string |
||
| 953 | */ |
||
| 954 | static function network_upload_file_types( $option = null ) { |
||
| 955 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
| 956 | } |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Maximum file upload size set by the network. |
||
| 960 | * @return number |
||
| 961 | */ |
||
| 962 | static function network_max_upload_file_size( $option = null ) { |
||
| 963 | // value in KB |
||
| 964 | return get_site_option( 'fileupload_maxk', 300 ); |
||
| 965 | } |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Lets us know if a site allows admins to manage the network. |
||
| 969 | * @return array |
||
| 970 | */ |
||
| 971 | static function network_enable_administration_menus( $option = null ) { |
||
| 972 | return get_site_option( 'menu_items' ); |
||
| 973 | } |
||
| 974 | |||
| 975 | /** |
||
| 976 | * If a user has been promoted to or demoted from admin, we need to clear the |
||
| 977 | * jetpack_other_linked_admins transient. |
||
| 978 | * |
||
| 979 | * @since 4.3.2 |
||
| 980 | * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared. |
||
| 981 | * |
||
| 982 | * @param int $user_id The user ID whose role changed. |
||
| 983 | * @param string $role The new role. |
||
| 984 | * @param array $old_roles An array of the user's previous roles. |
||
| 985 | */ |
||
| 986 | function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) { |
||
| 987 | if ( 'administrator' == $role |
||
| 988 | || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) ) |
||
| 989 | || is_null( $old_roles ) |
||
| 990 | ) { |
||
| 991 | delete_transient( 'jetpack_other_linked_admins' ); |
||
| 992 | } |
||
| 993 | } |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Checks to see if there are any other users available to become primary |
||
| 997 | * Users must both: |
||
| 998 | * - Be linked to wpcom |
||
| 999 | * - Be an admin |
||
| 1000 | * |
||
| 1001 | * @return mixed False if no other users are linked, Int if there are. |
||
| 1002 | */ |
||
| 1003 | static function get_other_linked_admins() { |
||
| 1004 | $other_linked_users = get_transient( 'jetpack_other_linked_admins' ); |
||
| 1005 | |||
| 1006 | if ( false === $other_linked_users ) { |
||
| 1007 | $admins = get_users( array( 'role' => 'administrator' ) ); |
||
| 1008 | if ( count( $admins ) > 1 ) { |
||
| 1009 | $available = array(); |
||
| 1010 | foreach ( $admins as $admin ) { |
||
| 1011 | if ( Jetpack::is_user_connected( $admin->ID ) ) { |
||
| 1012 | $available[] = $admin->ID; |
||
| 1013 | } |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | $count_connected_admins = count( $available ); |
||
| 1017 | if ( count( $available ) > 1 ) { |
||
| 1018 | $other_linked_users = $count_connected_admins; |
||
| 1019 | } else { |
||
| 1020 | $other_linked_users = 0; |
||
| 1021 | } |
||
| 1022 | } else { |
||
| 1023 | $other_linked_users = 0; |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS ); |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | return ( 0 === $other_linked_users ) ? false : $other_linked_users; |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Return whether we are dealing with a multi network setup or not. |
||
| 1034 | * The reason we are type casting this is because we want to avoid the situation where |
||
| 1035 | * the result is false since when is_main_network_option return false it cases |
||
| 1036 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
| 1037 | * database which could be set to anything as opposed to what this function returns. |
||
| 1038 | * @param bool $option |
||
| 1039 | * |
||
| 1040 | * @return boolean |
||
| 1041 | */ |
||
| 1042 | public function is_main_network_option( $option ) { |
||
| 1043 | // return '1' or '' |
||
| 1044 | return (string) (bool) Jetpack::is_multi_network(); |
||
| 1045 | } |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
| 1049 | * |
||
| 1050 | * @param string $option |
||
| 1051 | * @return boolean |
||
| 1052 | */ |
||
| 1053 | public function is_multisite( $option ) { |
||
| 1054 | return (string) (bool) is_multisite(); |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Implemented since there is no core is multi network function |
||
| 1059 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
| 1060 | * |
||
| 1061 | * @since 3.3 |
||
| 1062 | * @return boolean |
||
| 1063 | */ |
||
| 1064 | public static function is_multi_network() { |
||
| 1065 | global $wpdb; |
||
| 1066 | |||
| 1067 | // if we don't have a multi site setup no need to do any more |
||
| 1068 | if ( ! is_multisite() ) { |
||
| 1069 | return false; |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
| 1073 | if ( $num_sites > 1 ) { |
||
| 1074 | return true; |
||
| 1075 | } else { |
||
| 1076 | return false; |
||
| 1077 | } |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
| 1082 | * @return null |
||
| 1083 | */ |
||
| 1084 | function update_jetpack_main_network_site_option() { |
||
| 1085 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1086 | } |
||
| 1087 | /** |
||
| 1088 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
| 1089 | * |
||
| 1090 | */ |
||
| 1091 | function update_jetpack_network_settings() { |
||
| 1092 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1093 | // Only sync this info for the main network site. |
||
| 1094 | } |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Get back if the current site is single user site. |
||
| 1098 | * |
||
| 1099 | * @return bool |
||
| 1100 | */ |
||
| 1101 | public static function is_single_user_site() { |
||
| 1102 | global $wpdb; |
||
| 1103 | |||
| 1104 | View Code Duplication | if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) { |
|
| 1105 | $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" ); |
||
| 1106 | set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS ); |
||
| 1107 | } |
||
| 1108 | return 1 === (int) $some_users; |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Returns true if the site has file write access false otherwise. |
||
| 1113 | * @return string ( '1' | '0' ) |
||
| 1114 | **/ |
||
| 1115 | public static function file_system_write_access() { |
||
| 1116 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 1117 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
| 1121 | |||
| 1122 | $filesystem_method = get_filesystem_method(); |
||
| 1123 | if ( $filesystem_method === 'direct' ) { |
||
| 1124 | return 1; |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | ob_start(); |
||
| 1128 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 1129 | ob_end_clean(); |
||
| 1130 | if ( $filesystem_credentials_are_stored ) { |
||
| 1131 | return 1; |
||
| 1132 | } |
||
| 1133 | return 0; |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Finds out if a site is using a version control system. |
||
| 1138 | * @return string ( '1' | '0' ) |
||
| 1139 | **/ |
||
| 1140 | public static function is_version_controlled() { |
||
| 1141 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' ); |
||
| 1142 | return (string) (int) Jetpack_Sync_Functions::is_version_controlled(); |
||
| 1143 | } |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Determines whether the current theme supports featured images or not. |
||
| 1147 | * @return string ( '1' | '0' ) |
||
| 1148 | */ |
||
| 1149 | public static function featured_images_enabled() { |
||
| 1150 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1151 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Wrapper for core's get_avatar_url(). This one is deprecated. |
||
| 1156 | * |
||
| 1157 | * @deprecated 4.7 use get_avatar_url instead. |
||
| 1158 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
| 1159 | * @param int $size Size of the avatar image |
||
| 1160 | * @param string $default URL to a default image to use if no avatar is available |
||
| 1161 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
| 1162 | * |
||
| 1163 | * @return array |
||
| 1164 | */ |
||
| 1165 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
| 1166 | _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' ); |
||
| 1167 | return get_avatar_url( $id_or_email, array( |
||
| 1168 | 'size' => $size, |
||
| 1169 | 'default' => $default, |
||
| 1170 | 'force_default' => $force_display, |
||
| 1171 | ) ); |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * jetpack_updates is saved in the following schema: |
||
| 1176 | * |
||
| 1177 | * array ( |
||
| 1178 | * 'plugins' => (int) Number of plugin updates available. |
||
| 1179 | * 'themes' => (int) Number of theme updates available. |
||
| 1180 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
| 1181 | * 'translations' => (int) Number of translation updates available. |
||
| 1182 | * 'total' => (int) Total of all available updates. |
||
| 1183 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
| 1184 | * ) |
||
| 1185 | * @return array |
||
| 1186 | */ |
||
| 1187 | public static function get_updates() { |
||
| 1188 | $update_data = wp_get_update_data(); |
||
| 1189 | |||
| 1190 | // Stores the individual update counts as well as the total count. |
||
| 1191 | if ( isset( $update_data['counts'] ) ) { |
||
| 1192 | $updates = $update_data['counts']; |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | // If we need to update WordPress core, let's find the latest version number. |
||
| 1196 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
| 1197 | $cur = get_preferred_from_update_core(); |
||
| 1198 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
| 1199 | $updates['wp_update_version'] = $cur->current; |
||
| 1200 | } |
||
| 1201 | } |
||
| 1202 | return isset( $updates ) ? $updates : array(); |
||
| 1203 | } |
||
| 1204 | |||
| 1205 | public static function get_update_details() { |
||
| 1206 | $update_details = array( |
||
| 1207 | 'update_core' => get_site_transient( 'update_core' ), |
||
| 1208 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
| 1209 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
| 1210 | ); |
||
| 1211 | return $update_details; |
||
| 1212 | } |
||
| 1213 | |||
| 1214 | public static function refresh_update_data() { |
||
| 1215 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1216 | |||
| 1217 | } |
||
| 1218 | |||
| 1219 | public static function refresh_theme_data() { |
||
| 1220 | _deprecated_function( __METHOD__, 'jetpack-4.2' ); |
||
| 1221 | } |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Is Jetpack active? |
||
| 1225 | */ |
||
| 1226 | public static function is_active() { |
||
| 1227 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Make an API call to WordPress.com for plan status |
||
| 1232 | * |
||
| 1233 | * @uses Jetpack_Options::get_option() |
||
| 1234 | * @uses Jetpack_Client::wpcom_json_api_request_as_blog() |
||
| 1235 | * @uses update_option() |
||
| 1236 | * |
||
| 1237 | * @access public |
||
| 1238 | * @static |
||
| 1239 | * |
||
| 1240 | * @return bool True if plan is updated, false if no update |
||
| 1241 | */ |
||
| 1242 | public static function refresh_active_plan_from_wpcom() { |
||
| 1243 | // Make the API request |
||
| 1244 | $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) ); |
||
| 1245 | $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' ); |
||
| 1246 | |||
| 1247 | // Bail if there was an error or malformed response |
||
| 1248 | if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
||
| 1249 | return false; |
||
| 1250 | } |
||
| 1251 | |||
| 1252 | // Decode the results |
||
| 1253 | $results = json_decode( $response['body'], true ); |
||
| 1254 | |||
| 1255 | // Bail if there were no results or plan details returned |
||
| 1256 | if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) { |
||
| 1257 | return false; |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | // Store the option and return true if updated |
||
| 1261 | return update_option( 'jetpack_active_plan', $results['plan'] ); |
||
| 1262 | } |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Get the plan that this Jetpack site is currently using |
||
| 1266 | * |
||
| 1267 | * @uses get_option() |
||
| 1268 | * |
||
| 1269 | * @access public |
||
| 1270 | * @static |
||
| 1271 | * |
||
| 1272 | * @return array Active Jetpack plan details |
||
| 1273 | */ |
||
| 1274 | public static function get_active_plan() { |
||
| 1275 | $plan = get_option( 'jetpack_active_plan' ); |
||
| 1276 | |||
| 1277 | // Set the default options |
||
| 1278 | if ( ! $plan ) { |
||
| 1279 | $plan = array( |
||
| 1280 | 'product_slug' => 'jetpack_free', |
||
| 1281 | 'supports' => array(), |
||
| 1282 | 'class' => 'free', |
||
| 1283 | ); |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | // Define what paid modules are supported by personal plans |
||
| 1287 | $personal_plans = array( |
||
| 1288 | 'jetpack_personal', |
||
| 1289 | 'jetpack_personal_monthly', |
||
| 1290 | ); |
||
| 1291 | |||
| 1292 | if ( in_array( $plan['product_slug'], $personal_plans ) ) { |
||
| 1293 | $plan['supports'] = array( |
||
| 1294 | 'akismet', |
||
| 1295 | ); |
||
| 1296 | $plan['class'] = 'personal'; |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | // Define what paid modules are supported by premium plans |
||
| 1300 | $premium_plans = array( |
||
| 1301 | 'jetpack_premium', |
||
| 1302 | 'jetpack_premium_monthly', |
||
| 1303 | ); |
||
| 1304 | |||
| 1305 | View Code Duplication | if ( in_array( $plan['product_slug'], $premium_plans ) ) { |
|
| 1306 | $plan['supports'] = array( |
||
| 1307 | 'videopress', |
||
| 1308 | 'akismet', |
||
| 1309 | 'vaultpress', |
||
| 1310 | 'wordads', |
||
| 1311 | ); |
||
| 1312 | $plan['class'] = 'premium'; |
||
| 1313 | } |
||
| 1314 | |||
| 1315 | // Define what paid modules are supported by professional plans |
||
| 1316 | $business_plans = array( |
||
| 1317 | 'jetpack_business', |
||
| 1318 | 'jetpack_business_monthly', |
||
| 1319 | ); |
||
| 1320 | |||
| 1321 | View Code Duplication | if ( in_array( $plan['product_slug'], $business_plans ) ) { |
|
| 1322 | $plan['supports'] = array( |
||
| 1323 | 'videopress', |
||
| 1324 | 'akismet', |
||
| 1325 | 'vaultpress', |
||
| 1326 | 'seo-tools', |
||
| 1327 | 'google-analytics', |
||
| 1328 | 'wordads', |
||
| 1329 | ); |
||
| 1330 | $plan['class'] = 'business'; |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | // Make sure we have an array here in the event database data is stale |
||
| 1334 | if ( ! isset( $plan['supports'] ) ) { |
||
| 1335 | $plan['supports'] = array(); |
||
| 1336 | } |
||
| 1337 | |||
| 1338 | return $plan; |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | /** |
||
| 1342 | * Determine whether the active plan supports a particular feature |
||
| 1343 | * |
||
| 1344 | * @uses Jetpack::get_active_plan() |
||
| 1345 | * |
||
| 1346 | * @access public |
||
| 1347 | * @static |
||
| 1348 | * |
||
| 1349 | * @return bool True if plan supports feature, false if not |
||
| 1350 | */ |
||
| 1351 | public static function active_plan_supports( $feature ) { |
||
| 1352 | $plan = Jetpack::get_active_plan(); |
||
| 1353 | |||
| 1354 | if ( in_array( $feature, $plan['supports'] ) ) { |
||
| 1355 | return true; |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | return false; |
||
| 1359 | } |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Is Jetpack in development (offline) mode? |
||
| 1363 | */ |
||
| 1364 | public static function is_development_mode() { |
||
| 1365 | $development_mode = false; |
||
| 1366 | |||
| 1367 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
| 1368 | $development_mode = JETPACK_DEV_DEBUG; |
||
| 1369 | } elseif ( $site_url = site_url() ) { |
||
| 1370 | $development_mode = false === strpos( $site_url, '.' ); |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Filters Jetpack's development mode. |
||
| 1375 | * |
||
| 1376 | * @see https://jetpack.com/support/development-mode/ |
||
| 1377 | * |
||
| 1378 | * @since 2.2.1 |
||
| 1379 | * |
||
| 1380 | * @param bool $development_mode Is Jetpack's development mode active. |
||
| 1381 | */ |
||
| 1382 | return apply_filters( 'jetpack_development_mode', $development_mode ); |
||
| 1383 | } |
||
| 1384 | |||
| 1385 | /** |
||
| 1386 | * Get Jetpack development mode notice text and notice class. |
||
| 1387 | * |
||
| 1388 | * Mirrors the checks made in Jetpack::is_development_mode |
||
| 1389 | * |
||
| 1390 | */ |
||
| 1391 | public static function show_development_mode_notice() { |
||
| 1392 | if ( Jetpack::is_development_mode() ) { |
||
| 1393 | if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { |
||
| 1394 | $notice = sprintf( |
||
| 1395 | /* translators: %s is a URL */ |
||
| 1396 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ), |
||
| 1397 | 'https://jetpack.com/support/development-mode/' |
||
| 1398 | ); |
||
| 1399 | } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
| 1400 | $notice = sprintf( |
||
| 1401 | /* translators: %s is a URL */ |
||
| 1402 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ), |
||
| 1403 | 'https://jetpack.com/support/development-mode/' |
||
| 1404 | ); |
||
| 1405 | } else { |
||
| 1406 | $notice = sprintf( |
||
| 1407 | /* translators: %s is a URL */ |
||
| 1408 | __( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ), |
||
| 1409 | 'https://jetpack.com/support/development-mode/' |
||
| 1410 | ); |
||
| 1411 | } |
||
| 1412 | |||
| 1413 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1414 | } |
||
| 1415 | |||
| 1416 | // Throw up a notice if using a development version and as for feedback. |
||
| 1417 | if ( Jetpack::is_development_version() ) { |
||
| 1418 | /* translators: %s is a URL */ |
||
| 1419 | $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/' ); |
||
| 1420 | |||
| 1421 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1422 | } |
||
| 1423 | // Throw up a notice if using staging mode |
||
| 1424 | if ( Jetpack::is_staging_site() ) { |
||
| 1425 | /* translators: %s is a URL */ |
||
| 1426 | $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' ); |
||
| 1427 | |||
| 1428 | echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; |
||
| 1429 | } |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * Whether Jetpack's version maps to a public release, or a development version. |
||
| 1434 | */ |
||
| 1435 | public static function is_development_version() { |
||
| 1436 | /** |
||
| 1437 | * Allows filtering whether this is a development version of Jetpack. |
||
| 1438 | * |
||
| 1439 | * This filter is especially useful for tests. |
||
| 1440 | * |
||
| 1441 | * @since 4.3.0 |
||
| 1442 | * |
||
| 1443 | * @param bool $development_version Is this a develoment version of Jetpack? |
||
| 1444 | */ |
||
| 1445 | return (bool) apply_filters( |
||
| 1446 | 'jetpack_development_version', |
||
| 1447 | ! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) ) |
||
| 1448 | ); |
||
| 1449 | } |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
| 1453 | */ |
||
| 1454 | public static function is_user_connected( $user_id = false ) { |
||
| 1455 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
| 1456 | if ( ! $user_id ) { |
||
| 1457 | return false; |
||
| 1458 | } |
||
| 1459 | |||
| 1460 | return (bool) Jetpack_Data::get_access_token( $user_id ); |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Get the wpcom user data of the current|specified connected user. |
||
| 1465 | */ |
||
| 1466 | public static function get_connected_user_data( $user_id = null ) { |
||
| 1467 | if ( ! $user_id ) { |
||
| 1468 | $user_id = get_current_user_id(); |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | $transient_key = "jetpack_connected_user_data_$user_id"; |
||
| 1472 | |||
| 1473 | if ( $cached_user_data = get_transient( $transient_key ) ) { |
||
| 1474 | return $cached_user_data; |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | Jetpack::load_xml_rpc_client(); |
||
| 1478 | $xml = new Jetpack_IXR_Client( array( |
||
| 1479 | 'user_id' => $user_id, |
||
| 1480 | ) ); |
||
| 1481 | $xml->query( 'wpcom.getUser' ); |
||
| 1482 | if ( ! $xml->isError() ) { |
||
| 1483 | $user_data = $xml->getResponse(); |
||
| 1484 | set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS ); |
||
| 1485 | return $user_data; |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | return false; |
||
| 1489 | } |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Get the wpcom email of the current|specified connected user. |
||
| 1493 | */ |
||
| 1494 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
| 1495 | if ( ! $user_id ) { |
||
| 1496 | $user_id = get_current_user_id(); |
||
| 1497 | } |
||
| 1498 | Jetpack::load_xml_rpc_client(); |
||
| 1499 | $xml = new Jetpack_IXR_Client( array( |
||
| 1500 | 'user_id' => $user_id, |
||
| 1501 | ) ); |
||
| 1502 | $xml->query( 'wpcom.getUserEmail' ); |
||
| 1503 | if ( ! $xml->isError() ) { |
||
| 1504 | return $xml->getResponse(); |
||
| 1505 | } |
||
| 1506 | return false; |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Get the wpcom email of the master user. |
||
| 1511 | */ |
||
| 1512 | public static function get_master_user_email() { |
||
| 1513 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
| 1514 | if ( $master_user_id ) { |
||
| 1515 | return self::get_connected_user_email( $master_user_id ); |
||
| 1516 | } |
||
| 1517 | return ''; |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | function current_user_is_connection_owner() { |
||
| 1521 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1522 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; |
||
| 1523 | } |
||
| 1524 | |||
| 1525 | /** |
||
| 1526 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
| 1527 | */ |
||
| 1528 | function extra_oembed_providers() { |
||
| 1529 | // Cloudup: https://dev.cloudup.com/#oembed |
||
| 1530 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
| 1531 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
| 1532 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
| 1533 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
| 1534 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
| 1535 | } |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * Synchronize connected user role changes |
||
| 1539 | */ |
||
| 1540 | function user_role_change( $user_id ) { |
||
| 1541 | _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()' ); |
||
| 1542 | Jetpack_Sync_Users::user_role_change( $user_id ); |
||
| 1543 | } |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * Loads the currently active modules. |
||
| 1547 | */ |
||
| 1548 | public static function load_modules() { |
||
| 1549 | if ( ! self::is_active() && !self::is_development_mode() ) { |
||
| 1550 | if ( ! is_multisite() || ! get_site_option( 'jetpack_protect_active' ) ) { |
||
| 1551 | return; |
||
| 1552 | } |
||
| 1553 | } |
||
| 1554 | |||
| 1555 | $version = Jetpack_Options::get_option( 'version' ); |
||
| 1556 | View Code Duplication | if ( ! $version ) { |
|
| 1557 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
| 1558 | /** This action is documented in class.jetpack.php */ |
||
| 1559 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 1560 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 1561 | } |
||
| 1562 | list( $version ) = explode( ':', $version ); |
||
| 1563 | |||
| 1564 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
| 1565 | |||
| 1566 | $modules_data = array(); |
||
| 1567 | |||
| 1568 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
| 1569 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
| 1570 | $updated_modules = array(); |
||
| 1571 | foreach ( $modules as $module ) { |
||
| 1572 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1573 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
| 1574 | continue; |
||
| 1575 | } |
||
| 1576 | |||
| 1577 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
| 1578 | continue; |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | $updated_modules[] = $module; |
||
| 1582 | } |
||
| 1583 | |||
| 1584 | $modules = array_diff( $modules, $updated_modules ); |
||
| 1585 | } |
||
| 1586 | |||
| 1587 | $is_development_mode = Jetpack::is_development_mode(); |
||
| 1588 | |||
| 1589 | foreach ( $modules as $index => $module ) { |
||
| 1590 | // If we're in dev mode, disable modules requiring a connection |
||
| 1591 | if ( $is_development_mode ) { |
||
| 1592 | // Prime the pump if we need to |
||
| 1593 | if ( empty( $modules_data[ $module ] ) ) { |
||
| 1594 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1595 | } |
||
| 1596 | // If the module requires a connection, but we're in local mode, don't include it. |
||
| 1597 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
| 1598 | continue; |
||
| 1599 | } |
||
| 1600 | } |
||
| 1601 | |||
| 1602 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
| 1603 | continue; |
||
| 1604 | } |
||
| 1605 | |||
| 1606 | if ( ! @include( Jetpack::get_module_path( $module ) ) ) { |
||
| 1607 | unset( $modules[ $index ] ); |
||
| 1608 | self::update_active_modules( array_values( $modules ) ); |
||
| 1609 | continue; |
||
| 1610 | } |
||
| 1611 | |||
| 1612 | /** |
||
| 1613 | * Fires when a specific module is loaded. |
||
| 1614 | * The dynamic part of the hook, $module, is the module slug. |
||
| 1615 | * |
||
| 1616 | * @since 1.1.0 |
||
| 1617 | */ |
||
| 1618 | do_action( 'jetpack_module_loaded_' . $module ); |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * Fires when all the modules are loaded. |
||
| 1623 | * |
||
| 1624 | * @since 1.1.0 |
||
| 1625 | */ |
||
| 1626 | do_action( 'jetpack_modules_loaded' ); |
||
| 1627 | |||
| 1628 | // 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. |
||
| 1629 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) |
||
| 1630 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
| 1631 | } |
||
| 1632 | |||
| 1633 | /** |
||
| 1634 | * Check if Jetpack's REST API compat file should be included |
||
| 1635 | * @action plugins_loaded |
||
| 1636 | * @return null |
||
| 1637 | */ |
||
| 1638 | public function check_rest_api_compat() { |
||
| 1639 | /** |
||
| 1640 | * Filters the list of REST API compat files to be included. |
||
| 1641 | * |
||
| 1642 | * @since 2.2.5 |
||
| 1643 | * |
||
| 1644 | * @param array $args Array of REST API compat files to include. |
||
| 1645 | */ |
||
| 1646 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
| 1647 | |||
| 1648 | if ( function_exists( 'bbpress' ) ) |
||
| 1649 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
| 1650 | |||
| 1651 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
| 1652 | require_once $_jetpack_rest_api_compat_include; |
||
| 1653 | } |
||
| 1654 | |||
| 1655 | /** |
||
| 1656 | * Gets all plugins currently active in values, regardless of whether they're |
||
| 1657 | * traditionally activated or network activated. |
||
| 1658 | * |
||
| 1659 | * @todo Store the result in core's object cache maybe? |
||
| 1660 | */ |
||
| 1661 | public static function get_active_plugins() { |
||
| 1662 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
| 1663 | |||
| 1664 | if ( is_multisite() ) { |
||
| 1665 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
| 1666 | // whereas active_plugins stores them in the values. |
||
| 1667 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
| 1668 | if ( $network_plugins ) { |
||
| 1669 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
| 1670 | } |
||
| 1671 | } |
||
| 1672 | |||
| 1673 | sort( $active_plugins ); |
||
| 1674 | |||
| 1675 | return array_unique( $active_plugins ); |
||
| 1676 | } |
||
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Gets and parses additional plugin data to send with the heartbeat data |
||
| 1680 | * |
||
| 1681 | * @since 3.8.1 |
||
| 1682 | * |
||
| 1683 | * @return array Array of plugin data |
||
| 1684 | */ |
||
| 1685 | public static function get_parsed_plugin_data() { |
||
| 1686 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 1687 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
| 1688 | } |
||
| 1689 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 1690 | $all_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
| 1691 | $active_plugins = Jetpack::get_active_plugins(); |
||
| 1692 | |||
| 1693 | $plugins = array(); |
||
| 1694 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
| 1695 | $plugins[ $path ] = array( |
||
| 1696 | 'is_active' => in_array( $path, $active_plugins ), |
||
| 1697 | 'file' => $path, |
||
| 1698 | 'name' => $plugin_data['Name'], |
||
| 1699 | 'version' => $plugin_data['Version'], |
||
| 1700 | 'author' => $plugin_data['Author'], |
||
| 1701 | ); |
||
| 1702 | } |
||
| 1703 | |||
| 1704 | return $plugins; |
||
| 1705 | } |
||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Gets and parses theme data to send with the heartbeat data |
||
| 1709 | * |
||
| 1710 | * @since 3.8.1 |
||
| 1711 | * |
||
| 1712 | * @return array Array of theme data |
||
| 1713 | */ |
||
| 1714 | public static function get_parsed_theme_data() { |
||
| 1715 | $all_themes = wp_get_themes( array( 'allowed' => true ) ); |
||
| 1716 | $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); |
||
| 1717 | |||
| 1718 | $themes = array(); |
||
| 1719 | foreach ( $all_themes as $slug => $theme_data ) { |
||
| 1720 | $theme_headers = array(); |
||
| 1721 | foreach ( $header_keys as $header_key ) { |
||
| 1722 | $theme_headers[ $header_key ] = $theme_data->get( $header_key ); |
||
| 1723 | } |
||
| 1724 | |||
| 1725 | $themes[ $slug ] = array( |
||
| 1726 | 'is_active_theme' => $slug == wp_get_theme()->get_template(), |
||
| 1727 | 'slug' => $slug, |
||
| 1728 | 'theme_root' => $theme_data->get_theme_root_uri(), |
||
| 1729 | 'parent' => $theme_data->parent(), |
||
| 1730 | 'headers' => $theme_headers |
||
| 1731 | ); |
||
| 1732 | } |
||
| 1733 | |||
| 1734 | return $themes; |
||
| 1735 | } |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Checks whether a specific plugin is active. |
||
| 1739 | * |
||
| 1740 | * We don't want to store these in a static variable, in case |
||
| 1741 | * there are switch_to_blog() calls involved. |
||
| 1742 | */ |
||
| 1743 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
| 1744 | return in_array( $plugin, self::get_active_plugins() ); |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | /** |
||
| 1748 | * Check if Jetpack's Open Graph tags should be used. |
||
| 1749 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
| 1750 | * |
||
| 1751 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1752 | * @action plugins_loaded |
||
| 1753 | * @return null |
||
| 1754 | */ |
||
| 1755 | public function check_open_graph() { |
||
| 1756 | if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { |
||
| 1757 | add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); |
||
| 1758 | } |
||
| 1759 | |||
| 1760 | $active_plugins = self::get_active_plugins(); |
||
| 1761 | |||
| 1762 | if ( ! empty( $active_plugins ) ) { |
||
| 1763 | foreach ( $this->open_graph_conflicting_plugins as $plugin ) { |
||
| 1764 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 1765 | add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); |
||
| 1766 | break; |
||
| 1767 | } |
||
| 1768 | } |
||
| 1769 | } |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Allow the addition of Open Graph Meta Tags to all pages. |
||
| 1773 | * |
||
| 1774 | * @since 2.0.3 |
||
| 1775 | * |
||
| 1776 | * @param bool false Should Open Graph Meta tags be added. Default to false. |
||
| 1777 | */ |
||
| 1778 | if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { |
||
| 1779 | require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; |
||
| 1780 | } |
||
| 1781 | } |
||
| 1782 | |||
| 1783 | /** |
||
| 1784 | * Check if Jetpack's Twitter tags should be used. |
||
| 1785 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
| 1786 | * |
||
| 1787 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1788 | * @action plugins_loaded |
||
| 1789 | * @return null |
||
| 1790 | */ |
||
| 1791 | public function check_twitter_tags() { |
||
| 1792 | |||
| 1793 | $active_plugins = self::get_active_plugins(); |
||
| 1794 | |||
| 1795 | if ( ! empty( $active_plugins ) ) { |
||
| 1796 | foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { |
||
| 1797 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 1798 | add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); |
||
| 1799 | break; |
||
| 1800 | } |
||
| 1801 | } |
||
| 1802 | } |
||
| 1803 | |||
| 1804 | /** |
||
| 1805 | * Allow Twitter Card Meta tags to be disabled. |
||
| 1806 | * |
||
| 1807 | * @since 2.6.0 |
||
| 1808 | * |
||
| 1809 | * @param bool true Should Twitter Card Meta tags be disabled. Default to true. |
||
| 1810 | */ |
||
| 1811 | if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) { |
||
| 1812 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; |
||
| 1813 | } |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | /** |
||
| 1817 | * Allows plugins to submit security reports. |
||
| 1818 | * |
||
| 1819 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
| 1820 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
| 1821 | * @param array $args See definitions above |
||
| 1822 | */ |
||
| 1823 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
| 1824 | _deprecated_function( __FUNCTION__, 'jetpack-4.2', null ); |
||
| 1825 | } |
||
| 1826 | |||
| 1827 | /* Jetpack Options API */ |
||
| 1828 | |||
| 1829 | public static function get_option_names( $type = 'compact' ) { |
||
| 1830 | return Jetpack_Options::get_option_names( $type ); |
||
| 1831 | } |
||
| 1832 | |||
| 1833 | /** |
||
| 1834 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
| 1835 | * |
||
| 1836 | * @param string $name Option name |
||
| 1837 | * @param mixed $default (optional) |
||
| 1838 | */ |
||
| 1839 | public static function get_option( $name, $default = false ) { |
||
| 1840 | return Jetpack_Options::get_option( $name, $default ); |
||
| 1841 | } |
||
| 1842 | |||
| 1843 | /** |
||
| 1844 | * Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action |
||
| 1845 | * Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted |
||
| 1846 | * $name must be a registered option name. |
||
| 1847 | */ |
||
| 1848 | public static function create_nonce( $name ) { |
||
| 1849 | $secret = wp_generate_password( 32, false ) . ':' . wp_generate_password( 32, false ) . ':' . ( time() + 600 ); |
||
| 1850 | |||
| 1851 | Jetpack_Options::update_option( $name, $secret ); |
||
| 1852 | @list( $secret_1, $secret_2, $eol ) = explode( ':', Jetpack_Options::get_option( $name ) ); |
||
| 1853 | if ( empty( $secret_1 ) || empty( $secret_2 ) || $eol < time() ) |
||
| 1854 | return new Jetpack_Error( 'missing_secrets' ); |
||
| 1855 | |||
| 1856 | return array( |
||
| 1857 | 'secret_1' => $secret_1, |
||
| 1858 | 'secret_2' => $secret_2, |
||
| 1859 | 'eol' => $eol, |
||
| 1860 | ); |
||
| 1861 | } |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
| 1865 | * |
||
| 1866 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
| 1867 | * @param string $name Option name |
||
| 1868 | * @param mixed $value Option value |
||
| 1869 | */ |
||
| 1870 | public static function update_option( $name, $value ) { |
||
| 1871 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' ); |
||
| 1872 | return Jetpack_Options::update_option( $name, $value ); |
||
| 1873 | } |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
| 1877 | * |
||
| 1878 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
| 1879 | * @param array $array array( option name => option value, ... ) |
||
| 1880 | */ |
||
| 1881 | public static function update_options( $array ) { |
||
| 1882 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' ); |
||
| 1883 | return Jetpack_Options::update_options( $array ); |
||
| 1884 | } |
||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * Deletes the given option. May be passed multiple option names as an array. |
||
| 1888 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
| 1889 | * |
||
| 1890 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
| 1891 | * @param string|array $names |
||
| 1892 | */ |
||
| 1893 | public static function delete_option( $names ) { |
||
| 1894 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' ); |
||
| 1895 | return Jetpack_Options::delete_option( $names ); |
||
| 1896 | } |
||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Enters a user token into the user_tokens option |
||
| 1900 | * |
||
| 1901 | * @param int $user_id |
||
| 1902 | * @param string $token |
||
| 1903 | * return bool |
||
| 1904 | */ |
||
| 1905 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
| 1906 | // not designed for concurrent updates |
||
| 1907 | $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); |
||
| 1908 | if ( ! is_array( $user_tokens ) ) |
||
| 1909 | $user_tokens = array(); |
||
| 1910 | $user_tokens[$user_id] = $token; |
||
| 1911 | if ( $is_master_user ) { |
||
| 1912 | $master_user = $user_id; |
||
| 1913 | $options = compact( 'user_tokens', 'master_user' ); |
||
| 1914 | } else { |
||
| 1915 | $options = compact( 'user_tokens' ); |
||
| 1916 | } |
||
| 1917 | return Jetpack_Options::update_options( $options ); |
||
| 1918 | } |
||
| 1919 | |||
| 1920 | /** |
||
| 1921 | * Returns an array of all PHP files in the specified absolute path. |
||
| 1922 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
| 1923 | * |
||
| 1924 | * @param string $absolute_path The absolute path of the directory to search. |
||
| 1925 | * @return array Array of absolute paths to the PHP files. |
||
| 1926 | */ |
||
| 1927 | public static function glob_php( $absolute_path ) { |
||
| 1928 | if ( function_exists( 'glob' ) ) { |
||
| 1929 | return glob( "$absolute_path/*.php" ); |
||
| 1930 | } |
||
| 1931 | |||
| 1932 | $absolute_path = untrailingslashit( $absolute_path ); |
||
| 1933 | $files = array(); |
||
| 1934 | if ( ! $dir = @opendir( $absolute_path ) ) { |
||
| 1935 | return $files; |
||
| 1936 | } |
||
| 1937 | |||
| 1938 | while ( false !== $file = readdir( $dir ) ) { |
||
| 1939 | if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) { |
||
| 1940 | continue; |
||
| 1941 | } |
||
| 1942 | |||
| 1943 | $file = "$absolute_path/$file"; |
||
| 1944 | |||
| 1945 | if ( ! is_file( $file ) ) { |
||
| 1946 | continue; |
||
| 1947 | } |
||
| 1948 | |||
| 1949 | $files[] = $file; |
||
| 1950 | } |
||
| 1951 | |||
| 1952 | closedir( $dir ); |
||
| 1953 | |||
| 1954 | return $files; |
||
| 1955 | } |
||
| 1956 | |||
| 1957 | public static function activate_new_modules( $redirect = false ) { |
||
| 1958 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
| 1959 | return; |
||
| 1960 | } |
||
| 1961 | |||
| 1962 | $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic] |
||
| 1963 | View Code Duplication | if ( ! $jetpack_old_version ) { |
|
| 1964 | $jetpack_old_version = $version = $old_version = '1.1:' . time(); |
||
| 1965 | /** This action is documented in class.jetpack.php */ |
||
| 1966 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 1967 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 1968 | } |
||
| 1969 | |||
| 1970 | list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic] |
||
| 1971 | |||
| 1972 | if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) { |
||
| 1973 | return; |
||
| 1974 | } |
||
| 1975 | |||
| 1976 | $active_modules = Jetpack::get_active_modules(); |
||
| 1977 | $reactivate_modules = array(); |
||
| 1978 | foreach ( $active_modules as $active_module ) { |
||
| 1979 | $module = Jetpack::get_module( $active_module ); |
||
| 1980 | if ( ! isset( $module['changed'] ) ) { |
||
| 1981 | continue; |
||
| 1982 | } |
||
| 1983 | |||
| 1984 | if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) { |
||
| 1985 | continue; |
||
| 1986 | } |
||
| 1987 | |||
| 1988 | $reactivate_modules[] = $active_module; |
||
| 1989 | Jetpack::deactivate_module( $active_module ); |
||
| 1990 | } |
||
| 1991 | |||
| 1992 | $new_version = JETPACK__VERSION . ':' . time(); |
||
| 1993 | /** This action is documented in class.jetpack.php */ |
||
| 1994 | do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version ); |
||
| 1995 | Jetpack_Options::update_options( |
||
| 1996 | array( |
||
| 1997 | 'version' => $new_version, |
||
| 1998 | 'old_version' => $jetpack_old_version, |
||
| 1999 | ) |
||
| 2000 | ); |
||
| 2001 | |||
| 2002 | Jetpack::state( 'message', 'modules_activated' ); |
||
| 2003 | Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules ); |
||
| 2004 | |||
| 2005 | if ( $redirect ) { |
||
| 2006 | $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page |
||
| 2007 | if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) { |
||
| 2008 | $page = $_GET['page']; |
||
| 2009 | } |
||
| 2010 | |||
| 2011 | wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) ); |
||
| 2012 | exit; |
||
| 2013 | } |
||
| 2014 | } |
||
| 2015 | |||
| 2016 | /** |
||
| 2017 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
| 2018 | * Make sure to tuck away module "library" files in a sub-directory. |
||
| 2019 | */ |
||
| 2020 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
| 2021 | static $modules = null; |
||
| 2022 | |||
| 2023 | if ( ! isset( $modules ) ) { |
||
| 2024 | $available_modules_option = Jetpack_Options::get_option( 'available_modules', array() ); |
||
| 2025 | // Use the cache if we're on the front-end and it's available... |
||
| 2026 | if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) { |
||
| 2027 | $modules = $available_modules_option[ JETPACK__VERSION ]; |
||
| 2028 | } else { |
||
| 2029 | $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' ); |
||
| 2030 | |||
| 2031 | $modules = array(); |
||
| 2032 | |||
| 2033 | foreach ( $files as $file ) { |
||
| 2034 | if ( ! $headers = Jetpack::get_module( $file ) ) { |
||
| 2035 | continue; |
||
| 2036 | } |
||
| 2037 | |||
| 2038 | $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced']; |
||
| 2039 | } |
||
| 2040 | |||
| 2041 | Jetpack_Options::update_option( 'available_modules', array( |
||
| 2042 | JETPACK__VERSION => $modules, |
||
| 2043 | ) ); |
||
| 2044 | } |
||
| 2045 | } |
||
| 2046 | |||
| 2047 | /** |
||
| 2048 | * Filters the array of modules available to be activated. |
||
| 2049 | * |
||
| 2050 | * @since 2.4.0 |
||
| 2051 | * |
||
| 2052 | * @param array $modules Array of available modules. |
||
| 2053 | * @param string $min_version Minimum version number required to use modules. |
||
| 2054 | * @param string $max_version Maximum version number required to use modules. |
||
| 2055 | */ |
||
| 2056 | $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version ); |
||
| 2057 | |||
| 2058 | if ( ! $min_version && ! $max_version ) { |
||
| 2059 | return array_keys( $mods ); |
||
| 2060 | } |
||
| 2061 | |||
| 2062 | $r = array(); |
||
| 2063 | foreach ( $mods as $slug => $introduced ) { |
||
| 2064 | if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) { |
||
| 2065 | continue; |
||
| 2066 | } |
||
| 2067 | |||
| 2068 | if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) { |
||
| 2069 | continue; |
||
| 2070 | } |
||
| 2071 | |||
| 2072 | $r[] = $slug; |
||
| 2073 | } |
||
| 2074 | |||
| 2075 | return $r; |
||
| 2076 | } |
||
| 2077 | |||
| 2078 | /** |
||
| 2079 | * Default modules loaded on activation. |
||
| 2080 | */ |
||
| 2081 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
| 2082 | $return = array(); |
||
| 2083 | |||
| 2084 | foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) { |
||
| 2085 | $module_data = Jetpack::get_module( $module ); |
||
| 2086 | |||
| 2087 | switch ( strtolower( $module_data['auto_activate'] ) ) { |
||
| 2088 | case 'yes' : |
||
| 2089 | $return[] = $module; |
||
| 2090 | break; |
||
| 2091 | case 'public' : |
||
| 2092 | if ( Jetpack_Options::get_option( 'public' ) ) { |
||
| 2093 | $return[] = $module; |
||
| 2094 | } |
||
| 2095 | break; |
||
| 2096 | case 'no' : |
||
| 2097 | default : |
||
| 2098 | break; |
||
| 2099 | } |
||
| 2100 | } |
||
| 2101 | /** |
||
| 2102 | * Filters the array of default modules. |
||
| 2103 | * |
||
| 2104 | * @since 2.5.0 |
||
| 2105 | * |
||
| 2106 | * @param array $return Array of default modules. |
||
| 2107 | * @param string $min_version Minimum version number required to use modules. |
||
| 2108 | * @param string $max_version Maximum version number required to use modules. |
||
| 2109 | */ |
||
| 2110 | return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version ); |
||
| 2111 | } |
||
| 2112 | |||
| 2113 | /** |
||
| 2114 | * Checks activated modules during auto-activation to determine |
||
| 2115 | * if any of those modules are being deprecated. If so, close |
||
| 2116 | * them out, and add any replacement modules. |
||
| 2117 | * |
||
| 2118 | * Runs at priority 99 by default. |
||
| 2119 | * |
||
| 2120 | * This is run late, so that it can still activate a module if |
||
| 2121 | * the new module is a replacement for another that the user |
||
| 2122 | * currently has active, even if something at the normal priority |
||
| 2123 | * would kibosh everything. |
||
| 2124 | * |
||
| 2125 | * @since 2.6 |
||
| 2126 | * @uses jetpack_get_default_modules filter |
||
| 2127 | * @param array $modules |
||
| 2128 | * @return array |
||
| 2129 | */ |
||
| 2130 | function handle_deprecated_modules( $modules ) { |
||
| 2131 | $deprecated_modules = array( |
||
| 2132 | 'debug' => null, // Closed out and moved to ./class.jetpack-debugger.php |
||
| 2133 | 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality. |
||
| 2134 | 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support. |
||
| 2135 | ); |
||
| 2136 | |||
| 2137 | // Don't activate SSO if they never completed activating WPCC. |
||
| 2138 | if ( Jetpack::is_module_active( 'wpcc' ) ) { |
||
| 2139 | $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' ); |
||
| 2140 | if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) { |
||
| 2141 | $deprecated_modules['wpcc'] = null; |
||
| 2142 | } |
||
| 2143 | } |
||
| 2144 | |||
| 2145 | foreach ( $deprecated_modules as $module => $replacement ) { |
||
| 2146 | if ( Jetpack::is_module_active( $module ) ) { |
||
| 2147 | self::deactivate_module( $module ); |
||
| 2148 | if ( $replacement ) { |
||
| 2149 | $modules[] = $replacement; |
||
| 2150 | } |
||
| 2151 | } |
||
| 2152 | } |
||
| 2153 | |||
| 2154 | return array_unique( $modules ); |
||
| 2155 | } |
||
| 2156 | |||
| 2157 | /** |
||
| 2158 | * Checks activated plugins during auto-activation to determine |
||
| 2159 | * if any of those plugins are in the list with a corresponding module |
||
| 2160 | * that is not compatible with the plugin. The module will not be allowed |
||
| 2161 | * to auto-activate. |
||
| 2162 | * |
||
| 2163 | * @since 2.6 |
||
| 2164 | * @uses jetpack_get_default_modules filter |
||
| 2165 | * @param array $modules |
||
| 2166 | * @return array |
||
| 2167 | */ |
||
| 2168 | function filter_default_modules( $modules ) { |
||
| 2169 | |||
| 2170 | $active_plugins = self::get_active_plugins(); |
||
| 2171 | |||
| 2172 | if ( ! empty( $active_plugins ) ) { |
||
| 2173 | |||
| 2174 | // For each module we'd like to auto-activate... |
||
| 2175 | foreach ( $modules as $key => $module ) { |
||
| 2176 | // If there are potential conflicts for it... |
||
| 2177 | if ( ! empty( $this->conflicting_plugins[ $module ] ) ) { |
||
| 2178 | // For each potential conflict... |
||
| 2179 | foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) { |
||
| 2180 | // If that conflicting plugin is active... |
||
| 2181 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 2182 | // Remove that item from being auto-activated. |
||
| 2183 | unset( $modules[ $key ] ); |
||
| 2184 | } |
||
| 2185 | } |
||
| 2186 | } |
||
| 2187 | } |
||
| 2188 | } |
||
| 2189 | |||
| 2190 | return $modules; |
||
| 2191 | } |
||
| 2192 | |||
| 2193 | /** |
||
| 2194 | * Extract a module's slug from its full path. |
||
| 2195 | */ |
||
| 2196 | public static function get_module_slug( $file ) { |
||
| 2197 | return str_replace( '.php', '', basename( $file ) ); |
||
| 2198 | } |
||
| 2199 | |||
| 2200 | /** |
||
| 2201 | * Generate a module's path from its slug. |
||
| 2202 | */ |
||
| 2203 | public static function get_module_path( $slug ) { |
||
| 2204 | return JETPACK__PLUGIN_DIR . "modules/$slug.php"; |
||
| 2205 | } |
||
| 2206 | |||
| 2207 | /** |
||
| 2208 | * Load module data from module file. Headers differ from WordPress |
||
| 2209 | * plugin headers to avoid them being identified as standalone |
||
| 2210 | * plugins on the WordPress plugins page. |
||
| 2211 | */ |
||
| 2212 | public static function get_module( $module ) { |
||
| 2213 | $headers = array( |
||
| 2214 | 'name' => 'Module Name', |
||
| 2215 | 'description' => 'Module Description', |
||
| 2216 | 'jumpstart_desc' => 'Jumpstart Description', |
||
| 2217 | 'sort' => 'Sort Order', |
||
| 2218 | 'recommendation_order' => 'Recommendation Order', |
||
| 2219 | 'introduced' => 'First Introduced', |
||
| 2220 | 'changed' => 'Major Changes In', |
||
| 2221 | 'deactivate' => 'Deactivate', |
||
| 2222 | 'free' => 'Free', |
||
| 2223 | 'requires_connection' => 'Requires Connection', |
||
| 2224 | 'auto_activate' => 'Auto Activate', |
||
| 2225 | 'module_tags' => 'Module Tags', |
||
| 2226 | 'feature' => 'Feature', |
||
| 2227 | 'additional_search_queries' => 'Additional Search Queries', |
||
| 2228 | ); |
||
| 2229 | |||
| 2230 | $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) ); |
||
| 2231 | |||
| 2232 | $mod = Jetpack::get_file_data( $file, $headers ); |
||
| 2233 | if ( empty( $mod['name'] ) ) { |
||
| 2234 | return false; |
||
| 2235 | } |
||
| 2236 | |||
| 2237 | $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; |
||
| 2238 | $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; |
||
| 2239 | $mod['deactivate'] = empty( $mod['deactivate'] ); |
||
| 2240 | $mod['free'] = empty( $mod['free'] ); |
||
| 2241 | $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true; |
||
| 2242 | |||
| 2243 | if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) { |
||
| 2244 | $mod['auto_activate'] = 'No'; |
||
| 2245 | } else { |
||
| 2246 | $mod['auto_activate'] = (string) $mod['auto_activate']; |
||
| 2247 | } |
||
| 2248 | |||
| 2249 | if ( $mod['module_tags'] ) { |
||
| 2250 | $mod['module_tags'] = explode( ',', $mod['module_tags'] ); |
||
| 2251 | $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] ); |
||
| 2252 | $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] ); |
||
| 2253 | } else { |
||
| 2254 | $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) ); |
||
| 2255 | } |
||
| 2256 | |||
| 2257 | if ( $mod['feature'] ) { |
||
| 2258 | $mod['feature'] = explode( ',', $mod['feature'] ); |
||
| 2259 | $mod['feature'] = array_map( 'trim', $mod['feature'] ); |
||
| 2260 | } else { |
||
| 2261 | $mod['feature'] = array( self::translate_module_tag( 'Other' ) ); |
||
| 2262 | } |
||
| 2263 | |||
| 2264 | /** |
||
| 2265 | * Filters the feature array on a module. |
||
| 2266 | * |
||
| 2267 | * This filter allows you to control where each module is filtered: Recommended, |
||
| 2268 | * Jumpstart, and the default "Other" listing. |
||
| 2269 | * |
||
| 2270 | * @since 3.5.0 |
||
| 2271 | * |
||
| 2272 | * @param array $mod['feature'] The areas to feature this module: |
||
| 2273 | * 'Jumpstart' adds to the "Jumpstart" option to activate many modules at once. |
||
| 2274 | * 'Recommended' shows on the main Jetpack admin screen. |
||
| 2275 | * 'Other' should be the default if no other value is in the array. |
||
| 2276 | * @param string $module The slug of the module, e.g. sharedaddy. |
||
| 2277 | * @param array $mod All the currently assembled module data. |
||
| 2278 | */ |
||
| 2279 | $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod ); |
||
| 2280 | |||
| 2281 | /** |
||
| 2282 | * Filter the returned data about a module. |
||
| 2283 | * |
||
| 2284 | * This filter allows overriding any info about Jetpack modules. It is dangerous, |
||
| 2285 | * so please be careful. |
||
| 2286 | * |
||
| 2287 | * @since 3.6.0 |
||
| 2288 | * |
||
| 2289 | * @param array $mod The details of the requested module. |
||
| 2290 | * @param string $module The slug of the module, e.g. sharedaddy |
||
| 2291 | * @param string $file The path to the module source file. |
||
| 2292 | */ |
||
| 2293 | return apply_filters( 'jetpack_get_module', $mod, $module, $file ); |
||
| 2294 | } |
||
| 2295 | |||
| 2296 | /** |
||
| 2297 | * Like core's get_file_data implementation, but caches the result. |
||
| 2298 | */ |
||
| 2299 | public static function get_file_data( $file, $headers ) { |
||
| 2300 | //Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated |
||
| 2301 | $file_name = basename( $file ); |
||
| 2302 | $file_data_option = Jetpack_Options::get_option( 'file_data', array() ); |
||
| 2303 | $key = md5( $file_name . serialize( $headers ) ); |
||
| 2304 | $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 ); |
||
| 2305 | |||
| 2306 | // If we don't need to refresh the cache, and already have the value, short-circuit! |
||
| 2307 | if ( ! $refresh_cache && isset( $file_data_option[ JETPACK__VERSION ][ $key ] ) ) { |
||
| 2308 | return $file_data_option[ JETPACK__VERSION ][ $key ]; |
||
| 2309 | } |
||
| 2310 | |||
| 2311 | $data = get_file_data( $file, $headers ); |
||
| 2312 | |||
| 2313 | // Strip out any old Jetpack versions that are cluttering the option. |
||
| 2314 | $file_data_option = array_intersect_key( (array) $file_data_option, array( JETPACK__VERSION => null ) ); |
||
| 2315 | $file_data_option[ JETPACK__VERSION ][ $key ] = $data; |
||
| 2316 | Jetpack_Options::update_option( 'file_data', $file_data_option ); |
||
| 2317 | |||
| 2318 | return $data; |
||
| 2319 | } |
||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * Return translated module tag. |
||
| 2323 | * |
||
| 2324 | * @param string $tag Tag as it appears in each module heading. |
||
| 2325 | * |
||
| 2326 | * @return mixed |
||
| 2327 | */ |
||
| 2328 | public static function translate_module_tag( $tag ) { |
||
| 2329 | return jetpack_get_module_i18n_tag( $tag ); |
||
| 2330 | } |
||
| 2331 | |||
| 2332 | /** |
||
| 2333 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
| 2334 | * |
||
| 2335 | * @since 3.9.2 |
||
| 2336 | * |
||
| 2337 | * @param array $modules |
||
| 2338 | * |
||
| 2339 | * @return string|void |
||
| 2340 | */ |
||
| 2341 | public static function get_translated_modules( $modules ) { |
||
| 2342 | foreach ( $modules as $index => $module ) { |
||
| 2343 | $i18n_module = jetpack_get_module_i18n( $module['module'] ); |
||
| 2344 | if ( isset( $module['name'] ) ) { |
||
| 2345 | $modules[ $index ]['name'] = $i18n_module['name']; |
||
| 2346 | } |
||
| 2347 | if ( isset( $module['description'] ) ) { |
||
| 2348 | $modules[ $index ]['description'] = $i18n_module['description']; |
||
| 2349 | $modules[ $index ]['short_description'] = $i18n_module['description']; |
||
| 2350 | } |
||
| 2351 | } |
||
| 2352 | return $modules; |
||
| 2353 | } |
||
| 2354 | |||
| 2355 | /** |
||
| 2356 | * Get a list of activated modules as an array of module slugs. |
||
| 2357 | */ |
||
| 2358 | public static function get_active_modules() { |
||
| 2359 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
| 2360 | |||
| 2361 | if ( ! is_array( $active ) ) { |
||
| 2362 | $active = array(); |
||
| 2363 | } |
||
| 2364 | |||
| 2365 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
| 2366 | $active[] = 'vaultpress'; |
||
| 2367 | } else { |
||
| 2368 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
| 2369 | } |
||
| 2370 | |||
| 2371 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
| 2372 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
| 2373 | $active[] = 'protect'; |
||
| 2374 | } |
||
| 2375 | |||
| 2376 | return array_unique( $active ); |
||
| 2377 | } |
||
| 2378 | |||
| 2379 | /** |
||
| 2380 | * Check whether or not a Jetpack module is active. |
||
| 2381 | * |
||
| 2382 | * @param string $module The slug of a Jetpack module. |
||
| 2383 | * @return bool |
||
| 2384 | * |
||
| 2385 | * @static |
||
| 2386 | */ |
||
| 2387 | public static function is_module_active( $module ) { |
||
| 2388 | return in_array( $module, self::get_active_modules() ); |
||
| 2389 | } |
||
| 2390 | |||
| 2391 | public static function is_module( $module ) { |
||
| 2392 | return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() ); |
||
| 2393 | } |
||
| 2394 | |||
| 2395 | /** |
||
| 2396 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
| 2397 | * |
||
| 2398 | * @param bool $catch True to start catching, False to stop. |
||
| 2399 | * |
||
| 2400 | * @static |
||
| 2401 | */ |
||
| 2402 | public static function catch_errors( $catch ) { |
||
| 2403 | static $display_errors, $error_reporting; |
||
| 2404 | |||
| 2405 | if ( $catch ) { |
||
| 2406 | $display_errors = @ini_set( 'display_errors', 1 ); |
||
| 2407 | $error_reporting = @error_reporting( E_ALL ); |
||
| 2408 | add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
| 2409 | } else { |
||
| 2410 | @ini_set( 'display_errors', $display_errors ); |
||
| 2411 | @error_reporting( $error_reporting ); |
||
| 2412 | remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
| 2413 | } |
||
| 2414 | } |
||
| 2415 | |||
| 2416 | /** |
||
| 2417 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
| 2418 | */ |
||
| 2419 | public static function catch_errors_on_shutdown() { |
||
| 2420 | Jetpack::state( 'php_errors', ob_get_clean() ); |
||
| 2421 | } |
||
| 2422 | |||
| 2423 | public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array(), $redirect = true ) { |
||
| 2424 | $jetpack = Jetpack::init(); |
||
| 2425 | |||
| 2426 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
| 2427 | $modules = array_merge( $other_modules, $modules ); |
||
| 2428 | |||
| 2429 | // Look for standalone plugins and disable if active. |
||
| 2430 | |||
| 2431 | $to_deactivate = array(); |
||
| 2432 | foreach ( $modules as $module ) { |
||
| 2433 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
| 2434 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
| 2435 | } |
||
| 2436 | } |
||
| 2437 | |||
| 2438 | $deactivated = array(); |
||
| 2439 | foreach ( $to_deactivate as $module => $deactivate_me ) { |
||
| 2440 | list( $probable_file, $probable_title ) = $deactivate_me; |
||
| 2441 | if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { |
||
| 2442 | $deactivated[] = $module; |
||
| 2443 | } |
||
| 2444 | } |
||
| 2445 | |||
| 2446 | if ( $deactivated && $redirect ) { |
||
| 2447 | Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) ); |
||
| 2448 | |||
| 2449 | $url = add_query_arg( |
||
| 2450 | array( |
||
| 2451 | 'action' => 'activate_default_modules', |
||
| 2452 | '_wpnonce' => wp_create_nonce( 'activate_default_modules' ), |
||
| 2453 | ), |
||
| 2454 | add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) |
||
| 2455 | ); |
||
| 2456 | wp_safe_redirect( $url ); |
||
| 2457 | exit; |
||
| 2458 | } |
||
| 2459 | |||
| 2460 | /** |
||
| 2461 | * Fires before default modules are activated. |
||
| 2462 | * |
||
| 2463 | * @since 1.9.0 |
||
| 2464 | * |
||
| 2465 | * @param string $min_version Minimum version number required to use modules. |
||
| 2466 | * @param string $max_version Maximum version number required to use modules. |
||
| 2467 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
| 2468 | */ |
||
| 2469 | do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
| 2470 | |||
| 2471 | // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating |
||
| 2472 | Jetpack::restate(); |
||
| 2473 | Jetpack::catch_errors( true ); |
||
| 2474 | |||
| 2475 | $active = Jetpack::get_active_modules(); |
||
| 2476 | |||
| 2477 | foreach ( $modules as $module ) { |
||
| 2478 | if ( did_action( "jetpack_module_loaded_$module" ) ) { |
||
| 2479 | $active[] = $module; |
||
| 2480 | self::update_active_modules( $active ); |
||
| 2481 | continue; |
||
| 2482 | } |
||
| 2483 | |||
| 2484 | if ( in_array( $module, $active ) ) { |
||
| 2485 | $module_info = Jetpack::get_module( $module ); |
||
| 2486 | if ( ! $module_info['deactivate'] ) { |
||
| 2487 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
| 2488 | View Code Duplication | if ( $active_state = Jetpack::state( $state ) ) { |
|
| 2489 | $active_state = explode( ',', $active_state ); |
||
| 2490 | } else { |
||
| 2491 | $active_state = array(); |
||
| 2492 | } |
||
| 2493 | $active_state[] = $module; |
||
| 2494 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
| 2495 | } |
||
| 2496 | continue; |
||
| 2497 | } |
||
| 2498 | |||
| 2499 | $file = Jetpack::get_module_path( $module ); |
||
| 2500 | if ( ! file_exists( $file ) ) { |
||
| 2501 | continue; |
||
| 2502 | } |
||
| 2503 | |||
| 2504 | // we'll override this later if the plugin can be included without fatal error |
||
| 2505 | if ( $redirect ) { |
||
| 2506 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 2507 | } |
||
| 2508 | Jetpack::state( 'error', 'module_activation_failed' ); |
||
| 2509 | Jetpack::state( 'module', $module ); |
||
| 2510 | ob_start(); |
||
| 2511 | require $file; |
||
| 2512 | |||
| 2513 | $active[] = $module; |
||
| 2514 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
| 2515 | View Code Duplication | if ( $active_state = Jetpack::state( $state ) ) { |
|
| 2516 | $active_state = explode( ',', $active_state ); |
||
| 2517 | } else { |
||
| 2518 | $active_state = array(); |
||
| 2519 | } |
||
| 2520 | $active_state[] = $module; |
||
| 2521 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
| 2522 | Jetpack::update_active_modules( $active ); |
||
| 2523 | |||
| 2524 | ob_end_clean(); |
||
| 2525 | } |
||
| 2526 | Jetpack::state( 'error', false ); |
||
| 2527 | Jetpack::state( 'module', false ); |
||
| 2528 | Jetpack::catch_errors( false ); |
||
| 2529 | /** |
||
| 2530 | * Fires when default modules are activated. |
||
| 2531 | * |
||
| 2532 | * @since 1.9.0 |
||
| 2533 | * |
||
| 2534 | * @param string $min_version Minimum version number required to use modules. |
||
| 2535 | * @param string $max_version Maximum version number required to use modules. |
||
| 2536 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
| 2537 | */ |
||
| 2538 | do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
| 2539 | } |
||
| 2540 | |||
| 2541 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
| 2542 | /** |
||
| 2543 | * Fires before a module is activated. |
||
| 2544 | * |
||
| 2545 | * @since 2.6.0 |
||
| 2546 | * |
||
| 2547 | * @param string $module Module slug. |
||
| 2548 | * @param bool $exit Should we exit after the module has been activated. Default to true. |
||
| 2549 | * @param bool $redirect Should the user be redirected after module activation? Default to true. |
||
| 2550 | */ |
||
| 2551 | do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); |
||
| 2552 | |||
| 2553 | $jetpack = Jetpack::init(); |
||
| 2554 | |||
| 2555 | if ( ! strlen( $module ) ) |
||
| 2556 | return false; |
||
| 2557 | |||
| 2558 | if ( ! Jetpack::is_module( $module ) ) |
||
| 2559 | return false; |
||
| 2560 | |||
| 2561 | // If it's already active, then don't do it again |
||
| 2562 | $active = Jetpack::get_active_modules(); |
||
| 2563 | foreach ( $active as $act ) { |
||
| 2564 | if ( $act == $module ) |
||
| 2565 | return true; |
||
| 2566 | } |
||
| 2567 | |||
| 2568 | $module_data = Jetpack::get_module( $module ); |
||
| 2569 | |||
| 2570 | if ( ! Jetpack::is_active() ) { |
||
| 2571 | if ( !Jetpack::is_development_mode() ) |
||
| 2572 | return false; |
||
| 2573 | |||
| 2574 | // If we're not connected but in development mode, make sure the module doesn't require a connection |
||
| 2575 | if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) |
||
| 2576 | return false; |
||
| 2577 | } |
||
| 2578 | |||
| 2579 | // Check and see if the old plugin is active |
||
| 2580 | if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { |
||
| 2581 | // Deactivate the old plugin |
||
| 2582 | if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { |
||
| 2583 | // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module |
||
| 2584 | // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. |
||
| 2585 | Jetpack::state( 'deactivated_plugins', $module ); |
||
| 2586 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
||
| 2587 | exit; |
||
| 2588 | } |
||
| 2589 | } |
||
| 2590 | |||
| 2591 | // Protect won't work with mis-configured IPs |
||
| 2592 | if ( 'protect' === $module ) { |
||
| 2593 | include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; |
||
| 2594 | if ( ! jetpack_protect_get_ip() ) { |
||
| 2595 | Jetpack::state( 'message', 'protect_misconfigured_ip' ); |
||
| 2596 | return false; |
||
| 2597 | } |
||
| 2598 | } |
||
| 2599 | |||
| 2600 | // Check the file for fatal errors, a la wp-admin/plugins.php::activate |
||
| 2601 | Jetpack::state( 'module', $module ); |
||
| 2602 | Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error |
||
| 2603 | |||
| 2604 | Jetpack::catch_errors( true ); |
||
| 2605 | ob_start(); |
||
| 2606 | require Jetpack::get_module_path( $module ); |
||
| 2607 | /** This action is documented in class.jetpack.php */ |
||
| 2608 | do_action( 'jetpack_activate_module', $module ); |
||
| 2609 | $active[] = $module; |
||
| 2610 | Jetpack::update_active_modules( $active ); |
||
| 2611 | |||
| 2612 | Jetpack::state( 'error', false ); // the override |
||
| 2613 | ob_end_clean(); |
||
| 2614 | Jetpack::catch_errors( false ); |
||
| 2615 | |||
| 2616 | // A flag for Jump Start so it's not shown again. Only set if it hasn't been yet. |
||
| 2617 | View Code Duplication | if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { |
|
| 2618 | Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); |
||
| 2619 | |||
| 2620 | //Jump start is being dismissed send data to MC Stats |
||
| 2621 | $jetpack->stat( 'jumpstart', 'manual,'.$module ); |
||
| 2622 | |||
| 2623 | $jetpack->do_stats( 'server_side' ); |
||
| 2624 | } |
||
| 2625 | |||
| 2626 | if ( $redirect ) { |
||
| 2627 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 2628 | } |
||
| 2629 | if ( $exit ) { |
||
| 2630 | exit; |
||
| 2631 | } |
||
| 2632 | return true; |
||
| 2633 | } |
||
| 2634 | |||
| 2635 | function activate_module_actions( $module ) { |
||
| 2636 | _deprecated_function( __METHOD__, 'jeptack-4.2' ); |
||
| 2637 | } |
||
| 2638 | |||
| 2639 | public static function deactivate_module( $module ) { |
||
| 2640 | /** |
||
| 2641 | * Fires when a module is deactivated. |
||
| 2642 | * |
||
| 2643 | * @since 1.9.0 |
||
| 2644 | * |
||
| 2645 | * @param string $module Module slug. |
||
| 2646 | */ |
||
| 2647 | do_action( 'jetpack_pre_deactivate_module', $module ); |
||
| 2648 | |||
| 2649 | $jetpack = Jetpack::init(); |
||
| 2650 | |||
| 2651 | $active = Jetpack::get_active_modules(); |
||
| 2652 | $new = array_filter( array_diff( $active, (array) $module ) ); |
||
| 2653 | |||
| 2654 | // A flag for Jump Start so it's not shown again. |
||
| 2655 | View Code Duplication | if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { |
|
| 2656 | Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); |
||
| 2657 | |||
| 2658 | //Jump start is being dismissed send data to MC Stats |
||
| 2659 | $jetpack->stat( 'jumpstart', 'manual,deactivated-'.$module ); |
||
| 2660 | |||
| 2661 | $jetpack->do_stats( 'server_side' ); |
||
| 2662 | } |
||
| 2663 | |||
| 2664 | return self::update_active_modules( $new ); |
||
| 2665 | } |
||
| 2666 | |||
| 2667 | public static function enable_module_configurable( $module ) { |
||
| 2671 | |||
| 2672 | public static function module_configuration_url( $module ) { |
||
| 2676 | |||
| 2677 | public static function module_configuration_load( $module, $method ) { |
||
| 2681 | |||
| 2682 | public static function module_configuration_head( $module, $method ) { |
||
| 2686 | |||
| 2687 | public static function module_configuration_screen( $module, $method ) { |
||
| 2691 | |||
| 2692 | public static function module_configuration_activation_screen( $module, $method ) { |
||
| 2696 | |||
| 2697 | /* Installation */ |
||
| 2698 | |||
| 2699 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
| 2742 | * @static |
||
| 2743 | */ |
||
| 2744 | public static function plugin_activation( $network_wide ) { |
||
| 2756 | /** |
||
| 2757 | * Runs before bumping version numbers up to a new version |
||
| 2758 | * @param (string) $version Version:timestamp |
||
| 2759 | * @param (string) $old_version Old Version:timestamp or false if not set yet. |
||
| 2760 | * @return null [description] |
||
| 2761 | */ |
||
| 2762 | public static function do_version_bump( $version, $old_version ) { |
||
| 2769 | |||
| 2770 | /** |
||
| 2771 | * Sets the internal version number and activation state. |
||
| 2772 | * @static |
||
| 2773 | */ |
||
| 2774 | public static function plugin_initialize() { |
||
| 2790 | |||
| 2791 | /** |
||
| 2792 | * Removes all connection options |
||
| 2793 | * @static |
||
| 2794 | */ |
||
| 2795 | public static function plugin_deactivation( ) { |
||
| 2804 | |||
| 2805 | /** |
||
| 2806 | * Disconnects from the Jetpack servers. |
||
| 2807 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 2808 | * @static |
||
| 2809 | */ |
||
| 2810 | public static function disconnect( $update_activated_state = true ) { |
||
| 2811 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
| 2812 | Jetpack::clean_nonces( true ); |
||
| 2813 | |||
| 2814 | // If the site is in an IDC because sync is not allowed, |
||
| 2815 | // let's make sure to not disconnect the production site. |
||
| 2816 | if ( ! self::validate_sync_error_idc_option() ) { |
||
| 2817 | JetpackTracking::record_user_event( 'disconnect_site', array() ); |
||
| 2818 | Jetpack::load_xml_rpc_client(); |
||
| 2819 | $xml = new Jetpack_IXR_Client(); |
||
| 2820 | $xml->query( 'jetpack.deregister' ); |
||
| 2821 | } |
||
| 2822 | |||
| 2823 | Jetpack_Options::delete_option( |
||
| 2824 | array( |
||
| 2825 | 'register', |
||
| 2826 | 'blog_token', |
||
| 2827 | 'user_token', |
||
| 2828 | 'user_tokens', |
||
| 2829 | 'master_user', |
||
| 2830 | 'time_diff', |
||
| 2831 | 'fallback_no_verify_ssl_certs', |
||
| 2832 | ) |
||
| 2833 | ); |
||
| 2834 | |||
| 2835 | Jetpack_IDC::clear_all_idc_options(); |
||
| 2836 | |||
| 2837 | if ( $update_activated_state ) { |
||
| 2838 | Jetpack_Options::update_option( 'activated', 4 ); |
||
| 2839 | } |
||
| 2840 | |||
| 2841 | if ( $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ) ) { |
||
| 2842 | // Check then record unique disconnection if site has never been disconnected previously |
||
| 2843 | if ( - 1 == $jetpack_unique_connection['disconnected'] ) { |
||
| 2844 | $jetpack_unique_connection['disconnected'] = 1; |
||
| 2845 | } else { |
||
| 2846 | if ( 0 == $jetpack_unique_connection['disconnected'] ) { |
||
| 2847 | //track unique disconnect |
||
| 2848 | $jetpack = Jetpack::init(); |
||
| 2849 | |||
| 2850 | $jetpack->stat( 'connections', 'unique-disconnect' ); |
||
| 2851 | $jetpack->do_stats( 'server_side' ); |
||
| 2852 | } |
||
| 2853 | // increment number of times disconnected |
||
| 2854 | $jetpack_unique_connection['disconnected'] += 1; |
||
| 2855 | } |
||
| 2856 | |||
| 2857 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
| 2858 | } |
||
| 2859 | |||
| 2860 | // Delete cached connected user data |
||
| 2861 | $transient_key = "jetpack_connected_user_data_" . get_current_user_id(); |
||
| 2862 | delete_transient( $transient_key ); |
||
| 2863 | |||
| 2864 | // Delete all the sync related data. Since it could be taking up space. |
||
| 2865 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
||
| 2866 | Jetpack_Sync_Sender::get_instance()->uninstall(); |
||
| 2867 | |||
| 2868 | // Disable the Heartbeat cron |
||
| 2869 | Jetpack_Heartbeat::init()->deactivate(); |
||
| 2870 | } |
||
| 2871 | |||
| 2872 | /** |
||
| 2873 | * Unlinks the current user from the linked WordPress.com user |
||
| 2874 | */ |
||
| 2875 | public static function unlink_user( $user_id = null ) { |
||
| 2876 | if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) ) |
||
| 2877 | return false; |
||
| 2878 | |||
| 2879 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
| 2880 | |||
| 2881 | if ( Jetpack_Options::get_option( 'master_user' ) == $user_id ) |
||
| 2882 | return false; |
||
| 2883 | |||
| 2884 | if ( ! isset( $tokens[ $user_id ] ) ) |
||
| 2885 | return false; |
||
| 2886 | |||
| 2887 | Jetpack::load_xml_rpc_client(); |
||
| 2888 | $xml = new Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
| 2889 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
| 2890 | |||
| 2891 | unset( $tokens[ $user_id ] ); |
||
| 2892 | |||
| 2893 | Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
| 2894 | |||
| 2895 | /** |
||
| 2896 | * Fires after the current user has been unlinked from WordPress.com. |
||
| 2897 | * |
||
| 2906 | |||
| 2907 | /** |
||
| 2908 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
| 2909 | */ |
||
| 2910 | public static function try_registration() { |
||
| 2935 | |||
| 2936 | /** |
||
| 2937 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
| 2938 | * |
||
| 2939 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
| 2940 | */ |
||
| 2941 | public static function log( $code, $data = null ) { |
||
| 2981 | |||
| 2982 | /** |
||
| 2983 | * Get the internal event log. |
||
| 2984 | * |
||
| 2985 | * @param $event (string) - only return the specific log events |
||
| 2986 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
| 2987 | * |
||
| 2988 | * @return array of log events || WP_Error for invalid params |
||
| 2989 | */ |
||
| 2990 | public static function get_log( $event = false, $num = false ) { |
||
| 3026 | |||
| 3027 | /** |
||
| 3028 | * Log modification of important settings. |
||
| 3029 | */ |
||
| 3030 | public static function log_settings_change( $option, $old_value, $value ) { |
||
| 3037 | |||
| 3038 | /** |
||
| 3039 | * Return stat data for WPCOM sync |
||
| 3040 | */ |
||
| 3041 | public static function get_stat_data( $encode = true, $extended = true ) { |
||
| 3055 | |||
| 3056 | /** |
||
| 3057 | * Get additional stat data to sync to WPCOM |
||
| 3058 | */ |
||
| 3059 | public static function get_additional_stat_data( $prefix = '' ) { |
||
| 3070 | |||
| 3071 | private static function get_site_user_count() { |
||
| 3086 | |||
| 3087 | /* Admin Pages */ |
||
| 3088 | |||
| 3089 | function admin_init() { |
||
| 3138 | |||
| 3139 | function admin_body_class( $admin_body_class = '' ) { |
||
| 3147 | |||
| 3148 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
| 3151 | |||
| 3152 | /** |
||
| 3153 | * Call this function if you want the Big Jetpack Manage Notice to show up. |
||
| 3154 | * |
||
| 3155 | * @return null |
||
| 3156 | */ |
||
| 3157 | function prepare_manage_jetpack_notice() { |
||
| 3162 | |||
| 3163 | function manage_activate_screen() { |
||
| 3166 | /** |
||
| 3167 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
| 3168 | * This function artificially throws errors for such cases (whitelisted). |
||
| 3169 | * |
||
| 3170 | * @param string $plugin The activated plugin. |
||
| 3171 | */ |
||
| 3172 | function throw_error_on_activate_plugin( $plugin ) { |
||
| 3196 | |||
| 3197 | function intercept_plugin_error_scrape_init() { |
||
| 3200 | |||
| 3201 | function intercept_plugin_error_scrape( $action, $result ) { |
||
| 3212 | |||
| 3213 | function add_remote_request_handlers() { |
||
| 3217 | |||
| 3218 | function remote_request_handlers() { |
||
| 3258 | |||
| 3259 | /** |
||
| 3260 | * Uploads a file gotten from the global $_FILES. |
||
| 3261 | * If `$update_media_item` is true and `post_id` is defined |
||
| 3262 | * the attachment file of the media item (gotten through of the post_id) |
||
| 3263 | * will be updated instead of add a new one. |
||
| 3264 | * |
||
| 3265 | * @param boolean $update_media_item - update media attachment |
||
| 3266 | * @return array - An array describing the uploadind files process |
||
| 3267 | */ |
||
| 3268 | function upload_handler( $update_media_item = false ) { |
||
| 3390 | |||
| 3391 | /** |
||
| 3392 | * Add help to the Jetpack page |
||
| 3393 | * |
||
| 3394 | * @since Jetpack (1.2.3) |
||
| 3395 | * @return false if not the Jetpack page |
||
| 3396 | */ |
||
| 3397 | function admin_help() { |
||
| 3438 | |||
| 3439 | function admin_menu_css() { |
||
| 3442 | |||
| 3443 | function admin_menu_order() { |
||
| 3446 | |||
| 3447 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 3462 | |||
| 3463 | function admin_head() { |
||
| 3468 | |||
| 3469 | function admin_banner_styles() { |
||
| 3490 | |||
| 3491 | function plugin_action_links( $actions ) { |
||
| 3506 | |||
| 3507 | /** |
||
| 3508 | * This is the first banner |
||
| 3509 | * It should be visible only to user that can update the option |
||
| 3510 | * Are not connected |
||
| 3511 | * |
||
| 3512 | * @return null |
||
| 3513 | */ |
||
| 3514 | function admin_jetpack_manage_notice() { |
||
| 3544 | |||
| 3545 | /** |
||
| 3546 | * Returns the url that the user clicks to remove the notice for the big banner |
||
| 3547 | * @return (string) |
||
| 3548 | */ |
||
| 3549 | function opt_out_jetpack_manage_url() { |
||
| 3553 | /** |
||
| 3554 | * Returns the url that the user clicks to opt in to Jetpack Manage |
||
| 3555 | * @return (string) |
||
| 3556 | */ |
||
| 3557 | function opt_in_jetpack_manage_url() { |
||
| 3560 | |||
| 3561 | function opt_in_jetpack_manage_notice() { |
||
| 3571 | /** |
||
| 3572 | * Determines whether to show the notice of not true = display notice |
||
| 3573 | * @return (bool) |
||
| 3574 | */ |
||
| 3575 | function can_display_jetpack_manage_notice() { |
||
| 3597 | |||
| 3598 | /* |
||
| 3599 | * Registration flow: |
||
| 3600 | * 1 - ::admin_page_load() action=register |
||
| 3601 | * 2 - ::try_registration() |
||
| 3602 | * 3 - ::register() |
||
| 3603 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
| 3604 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
| 3605 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
| 3606 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
| 3607 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
| 3608 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
| 3609 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
| 3610 | * jetpack_id, jetpack_secret, jetpack_public |
||
| 3611 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
| 3612 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
| 3613 | * 5 - user logs in with WP.com account |
||
| 3614 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
| 3615 | * - Jetpack_Client_Server::authorize() |
||
| 3616 | * - Jetpack_Client_Server::get_token() |
||
| 3617 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
| 3618 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
| 3619 | * - which responds with access_token, token_type, scope |
||
| 3620 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
| 3621 | * - Jetpack::activate_default_modules() |
||
| 3622 | * - Deactivates deprecated plugins |
||
| 3623 | * - Activates all default modules |
||
| 3624 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
| 3625 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
| 3626 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
| 3627 | * Done! |
||
| 3628 | */ |
||
| 3629 | |||
| 3630 | /** |
||
| 3631 | * Handles the page load events for the Jetpack admin page |
||
| 3632 | */ |
||
| 3633 | function admin_page_load() { |
||
| 3859 | |||
| 3860 | function admin_notices() { |
||
| 3957 | |||
| 3958 | /** |
||
| 3959 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
| 3960 | */ |
||
| 3961 | function stat( $group, $detail ) { |
||
| 3966 | |||
| 3967 | /** |
||
| 3968 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
| 3969 | */ |
||
| 3970 | function do_stats( $method = '' ) { |
||
| 3985 | |||
| 3986 | /** |
||
| 3987 | * Runs stats code for a one-off, server-side. |
||
| 3988 | * |
||
| 3989 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
| 3990 | * |
||
| 3991 | * @return bool If it worked. |
||
| 3992 | */ |
||
| 3993 | static function do_server_side_stat( $args ) { |
||
| 4003 | |||
| 4004 | /** |
||
| 4005 | * Builds the stats url. |
||
| 4006 | * |
||
| 4007 | * @param $args array|string The arguments to append to the URL. |
||
| 4008 | * |
||
| 4009 | * @return string The URL to be pinged. |
||
| 4010 | */ |
||
| 4011 | static function build_stats_url( $args ) { |
||
| 4031 | |||
| 4032 | static function translate_current_user_to_role() { |
||
| 4041 | |||
| 4042 | static function translate_role_to_cap( $role ) { |
||
| 4049 | |||
| 4050 | static function sign_role( $role ) { |
||
| 4062 | |||
| 4063 | |||
| 4064 | /** |
||
| 4065 | * Builds a URL to the Jetpack connection auth page |
||
| 4066 | * |
||
| 4067 | * @since 3.9.5 |
||
| 4068 | * |
||
| 4069 | * @param bool $raw If true, URL will not be escaped. |
||
| 4070 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 4071 | * If string, will be a custom redirect. |
||
| 4072 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 4073 | * |
||
| 4074 | * @return string Connect URL |
||
| 4075 | */ |
||
| 4076 | function build_connect_url( $raw = false, $redirect = false, $from = false ) { |
||
| 4160 | |||
| 4161 | function build_reconnect_url( $raw = false ) { |
||
| 4165 | |||
| 4166 | public static function admin_url( $args = null ) { |
||
| 4171 | |||
| 4172 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
| 4176 | |||
| 4177 | function dismiss_jetpack_notice() { |
||
| 4251 | |||
| 4252 | function debugger_page() { |
||
| 4260 | |||
| 4261 | public static function admin_screen_configure_module( $module_id ) { |
||
| 4313 | |||
| 4314 | /** |
||
| 4315 | * Display link to activate the module to see the settings screen. |
||
| 4316 | * @param string $module_id |
||
| 4317 | * @return null |
||
| 4318 | */ |
||
| 4319 | public static function display_activate_module_link( $module_id ) { |
||
| 4371 | |||
| 4372 | public static function sort_modules( $a, $b ) { |
||
| 4378 | |||
| 4379 | function ajax_recheck_ssl() { |
||
| 4387 | |||
| 4388 | /* Client API */ |
||
| 4389 | |||
| 4390 | /** |
||
| 4391 | * Returns the requested Jetpack API URL |
||
| 4392 | * |
||
| 4393 | * @return string |
||
| 4394 | */ |
||
| 4395 | public static function api_url( $relative_url ) { |
||
| 4398 | |||
| 4399 | /** |
||
| 4400 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
| 4401 | */ |
||
| 4402 | public static function fix_url_for_bad_hosts( $url ) { |
||
| 4418 | |||
| 4419 | /** |
||
| 4420 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
| 4421 | * |
||
| 4422 | * @since 2.3.3 |
||
| 4423 | * @return boolean |
||
| 4424 | */ |
||
| 4425 | public static function permit_ssl( $force_recheck = false ) { |
||
| 4467 | |||
| 4468 | /* |
||
| 4469 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
| 4470 | */ |
||
| 4471 | public function alert_auto_ssl_fail() { |
||
| 4520 | |||
| 4521 | /** |
||
| 4522 | * Returns the Jetpack XML-RPC API |
||
| 4523 | * |
||
| 4524 | * @return string |
||
| 4525 | */ |
||
| 4526 | public static function xmlrpc_api_url() { |
||
| 4530 | |||
| 4531 | /** |
||
| 4532 | * Creates two secret tokens and the end of life timestamp for them. |
||
| 4533 | * |
||
| 4534 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
| 4535 | * |
||
| 4536 | * @since 2.6 |
||
| 4537 | * @return array |
||
| 4538 | */ |
||
| 4539 | public function generate_secrets( $action, $exp = 600 ) { |
||
| 4547 | |||
| 4548 | /** |
||
| 4549 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 4550 | * |
||
| 4551 | * Based on local php max_execution_time in php.ini |
||
| 4552 | * |
||
| 4553 | * @since 2.6 |
||
| 4554 | * @return int |
||
| 4555 | **/ |
||
| 4556 | public function get_remote_query_timeout_limit() { |
||
| 4562 | |||
| 4563 | |||
| 4564 | /** |
||
| 4565 | * Takes the response from the Jetpack register new site endpoint and |
||
| 4566 | * verifies it worked properly. |
||
| 4567 | * |
||
| 4568 | * @since 2.6 |
||
| 4569 | * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures |
||
| 4570 | **/ |
||
| 4571 | public function validate_remote_register_response( $response ) { |
||
| 4611 | /** |
||
| 4612 | * @return bool|WP_Error |
||
| 4613 | */ |
||
| 4614 | public static function register() { |
||
| 4704 | |||
| 4705 | /** |
||
| 4706 | * If the db version is showing something other that what we've got now, bump it to current. |
||
| 4707 | * |
||
| 4708 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
| 4709 | */ |
||
| 4710 | public static function maybe_set_version_option() { |
||
| 4724 | |||
| 4725 | /* Client Server API */ |
||
| 4726 | |||
| 4727 | /** |
||
| 4728 | * Loads the Jetpack XML-RPC client |
||
| 4729 | */ |
||
| 4730 | public static function load_xml_rpc_client() { |
||
| 4734 | |||
| 4735 | /** |
||
| 4736 | * Resets the saved authentication state in between testing requests. |
||
| 4737 | */ |
||
| 4738 | public function reset_saved_auth_state() { |
||
| 4742 | |||
| 4743 | function verify_xml_rpc_signature() { |
||
| 4842 | |||
| 4843 | /** |
||
| 4844 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
| 4845 | */ |
||
| 4846 | function authenticate_jetpack( $user, $username, $password ) { |
||
| 4869 | |||
| 4870 | // Authenticates requests from Jetpack server to WP REST API endpoints. |
||
| 4871 | // Uses the existing XMLRPC request signing implementation. |
||
| 4872 | function wp_rest_authenticate( $user ) { |
||
| 4966 | |||
| 4967 | /** |
||
| 4968 | * Report authentication status to the WP REST API. |
||
| 4969 | * |
||
| 4970 | * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not |
||
| 4971 | * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication} |
||
| 4972 | */ |
||
| 4973 | public function wp_rest_authentication_errors( $value ) { |
||
| 4979 | |||
| 4980 | function add_nonce( $timestamp, $nonce ) { |
||
| 5018 | |||
| 5019 | /** |
||
| 5020 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
| 5021 | * Capture it here so we can verify the signature later. |
||
| 5022 | */ |
||
| 5023 | function xmlrpc_methods( $methods ) { |
||
| 5027 | |||
| 5028 | function public_xmlrpc_methods( $methods ) { |
||
| 5034 | |||
| 5035 | function jetpack_getOptions( $args ) { |
||
| 5075 | |||
| 5076 | function xmlrpc_options( $options ) { |
||
| 5094 | |||
| 5095 | public static function clean_nonces( $all = false ) { |
||
| 5116 | |||
| 5117 | /** |
||
| 5118 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
| 5119 | * SET: state( $key, $value ); |
||
| 5120 | * GET: $value = state( $key ); |
||
| 5121 | * |
||
| 5122 | * @param string $key |
||
| 5123 | * @param string $value |
||
| 5124 | * @param bool $restate private |
||
| 5125 | */ |
||
| 5126 | public static function state( $key = null, $value = null, $restate = false ) { |
||
| 5176 | |||
| 5177 | public static function restate() { |
||
| 5180 | |||
| 5181 | public static function check_privacy( $file ) { |
||
| 5216 | |||
| 5217 | /** |
||
| 5218 | * Helper method for multicall XMLRPC. |
||
| 5219 | */ |
||
| 5220 | public static function xmlrpc_async_call() { |
||
| 5262 | |||
| 5263 | public static function staticize_subdomain( $url ) { |
||
| 5298 | |||
| 5299 | /* JSON API Authorization */ |
||
| 5300 | |||
| 5301 | /** |
||
| 5302 | * Handles the login action for Authorizing the JSON API |
||
| 5303 | */ |
||
| 5304 | function login_form_json_api_authorization() { |
||
| 5313 | |||
| 5314 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
| 5315 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
| 5328 | |||
| 5329 | // Make sure the POSTed request is handled by the same action |
||
| 5330 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
| 5334 | |||
| 5335 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
| 5336 | function store_json_api_authorization_token( $user_login, $user ) { |
||
| 5342 | |||
| 5343 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
| 5344 | function allow_wpcom_public_api_domain( $domains ) { |
||
| 5348 | |||
| 5349 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
| 5350 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
| 5362 | |||
| 5363 | |||
| 5364 | /** |
||
| 5365 | * Verifies the request by checking the signature |
||
| 5366 | * |
||
| 5367 | * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow |
||
| 5368 | * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO. |
||
| 5369 | * |
||
| 5370 | * @param null|array $environment |
||
| 5371 | */ |
||
| 5372 | function verify_json_api_authorization_request( $environment = null ) { |
||
| 5465 | |||
| 5466 | function login_message_json_api_authorization( $message ) { |
||
| 5472 | |||
| 5473 | /** |
||
| 5474 | * Get $content_width, but with a <s>twist</s> filter. |
||
| 5475 | */ |
||
| 5476 | public static function get_content_width() { |
||
| 5487 | |||
| 5488 | /** |
||
| 5489 | * Pings the WordPress.com Mirror Site for the specified options. |
||
| 5490 | * |
||
| 5491 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
| 5492 | * |
||
| 5493 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
| 5494 | */ |
||
| 5495 | public function get_cloud_site_options( $option_names ) { |
||
| 5511 | |||
| 5512 | /** |
||
| 5513 | * Checks if the site is currently in an identity crisis. |
||
| 5514 | * |
||
| 5515 | * @return array|bool Array of options that are in a crisis, or false if everything is OK. |
||
| 5516 | */ |
||
| 5517 | public static function check_identity_crisis() { |
||
| 5524 | |||
| 5525 | /** |
||
| 5526 | * Checks whether the home and siteurl specifically are whitelisted |
||
| 5527 | * Written so that we don't have re-check $key and $value params every time |
||
| 5528 | * we want to check if this site is whitelisted, for example in footer.php |
||
| 5529 | * |
||
| 5530 | * @since 3.8.0 |
||
| 5531 | * @return bool True = already whitelisted False = not whitelisted |
||
| 5532 | */ |
||
| 5533 | public static function is_staging_site() { |
||
| 5592 | |||
| 5593 | /** |
||
| 5594 | * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. |
||
| 5595 | * |
||
| 5596 | * @return bool |
||
| 5597 | */ |
||
| 5598 | public static function validate_sync_error_idc_option() { |
||
| 5642 | |||
| 5643 | /** |
||
| 5644 | * Normalizes a url by doing three things: |
||
| 5645 | * - Strips protocol |
||
| 5646 | * - Strips www |
||
| 5647 | * - Adds a trailing slash |
||
| 5648 | * |
||
| 5649 | * @since 4.4.0 |
||
| 5650 | * @param string $url |
||
| 5651 | * @return WP_Error|string |
||
| 5652 | */ |
||
| 5653 | public static function normalize_url_protocol_agnostic( $url ) { |
||
| 5663 | |||
| 5664 | /** |
||
| 5665 | * Gets the value that is to be saved in the jetpack_sync_error_idc option. |
||
| 5666 | * |
||
| 5667 | * @since 4.4.0 |
||
| 5668 | * |
||
| 5669 | * @param array $response |
||
| 5670 | * @return array Array of the local urls, wpcom urls, and error code |
||
| 5671 | */ |
||
| 5672 | public static function get_sync_error_idc_option( $response = array() ) { |
||
| 5696 | |||
| 5697 | /** |
||
| 5698 | * Returns the value of the jetpack_sync_idc_optin filter, or constant. |
||
| 5699 | * If set to true, the site will be put into staging mode. |
||
| 5700 | * |
||
| 5701 | * @since 4.3.2 |
||
| 5702 | * @return bool |
||
| 5703 | */ |
||
| 5704 | public static function sync_idc_optin() { |
||
| 5722 | |||
| 5723 | /** |
||
| 5724 | * Maybe Use a .min.css stylesheet, maybe not. |
||
| 5725 | * |
||
| 5726 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
| 5727 | */ |
||
| 5728 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
| 5763 | |||
| 5764 | /** |
||
| 5765 | * Maybe inlines a stylesheet. |
||
| 5766 | * |
||
| 5767 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
| 5768 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
| 5769 | * |
||
| 5770 | * Attached to `style_loader_tag` filter. |
||
| 5771 | * |
||
| 5772 | * @param string $tag The tag that would link to the external asset. |
||
| 5773 | * @param string $handle The registered handle of the script in question. |
||
| 5774 | * |
||
| 5775 | * @return string |
||
| 5776 | */ |
||
| 5777 | public static function maybe_inline_style( $tag, $handle ) { |
||
| 5827 | |||
| 5828 | /** |
||
| 5829 | * Loads a view file from the views |
||
| 5830 | * |
||
| 5831 | * Data passed in with the $data parameter will be available in the |
||
| 5832 | * template file as $data['value'] |
||
| 5833 | * |
||
| 5834 | * @param string $template - Template file to load |
||
| 5835 | * @param array $data - Any data to pass along to the template |
||
| 5836 | * @return boolean - If template file was found |
||
| 5837 | **/ |
||
| 5838 | public function load_view( $template, $data = array() ) { |
||
| 5849 | |||
| 5850 | /** |
||
| 5851 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
| 5852 | */ |
||
| 5853 | public function deprecated_hooks() { |
||
| 5902 | |||
| 5903 | /** |
||
| 5904 | * Converts any url in a stylesheet, to the correct absolute url. |
||
| 5905 | * |
||
| 5906 | * Considerations: |
||
| 5907 | * - Normal, relative URLs `feh.png` |
||
| 5908 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
| 5909 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
| 5910 | * - Absolute URLs `http://domain.com/feh.png` |
||
| 5911 | * - Domain root relative URLs `/feh.png` |
||
| 5912 | * |
||
| 5913 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
| 5914 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
| 5915 | * |
||
| 5916 | * @return mixed|string |
||
| 5917 | */ |
||
| 5918 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
| 5962 | |||
| 5963 | /** |
||
| 5964 | * This methods removes all of the registered css files on the front end |
||
| 5965 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
| 5966 | * all the files into one file. |
||
| 5967 | * |
||
| 5968 | * Pros: |
||
| 5969 | * - Uses only ONE css asset connection instead of 15 |
||
| 5970 | * - Saves a minimum of 56k |
||
| 5971 | * - Reduces server load |
||
| 5972 | * - Reduces time to first painted byte |
||
| 5973 | * |
||
| 5974 | * Cons: |
||
| 5975 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
| 5976 | * should not cause any issues with themes. |
||
| 5977 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
| 5978 | * jetpack_implode_frontend_css filter for a workaround |
||
| 5979 | * |
||
| 5980 | * For some situations developers may wish to disable css imploding and |
||
| 5981 | * instead operate in legacy mode where each file loads seperately and |
||
| 5982 | * can be edited individually or dequeued. This can be accomplished with |
||
| 5983 | * the following line: |
||
| 5984 | * |
||
| 5985 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
| 5986 | * |
||
| 5987 | * @since 3.2 |
||
| 5988 | **/ |
||
| 5989 | public function implode_frontend_css( $travis_test = false ) { |
||
| 6041 | |||
| 6042 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
| 6052 | |||
| 6053 | /* |
||
| 6054 | * Check the heartbeat data |
||
| 6055 | * |
||
| 6056 | * Organizes the heartbeat data by severity. For example, if the site |
||
| 6057 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
| 6058 | * |
||
| 6059 | * Data will be added to "caution" array, if it either: |
||
| 6060 | * - Out of date Jetpack version |
||
| 6061 | * - Out of date WP version |
||
| 6062 | * - Out of date PHP version |
||
| 6063 | * |
||
| 6064 | * $return array $filtered_data |
||
| 6065 | */ |
||
| 6066 | public static function jetpack_check_heartbeat_data() { |
||
| 6119 | |||
| 6120 | |||
| 6121 | /* |
||
| 6122 | * This method is used to organize all options that can be reset |
||
| 6123 | * without disconnecting Jetpack. |
||
| 6124 | * |
||
| 6125 | * It is used in class.jetpack-cli.php to reset options |
||
| 6126 | * |
||
| 6127 | * @return array of options to delete. |
||
| 6128 | */ |
||
| 6129 | public static function get_jetpack_options_for_reset() { |
||
| 6196 | |||
| 6197 | /** |
||
| 6198 | * Check if an option of a Jetpack module has been updated. |
||
| 6199 | * |
||
| 6200 | * If any module option has been updated before Jump Start has been dismissed, |
||
| 6201 | * update the 'jumpstart' option so we can hide Jump Start. |
||
| 6202 | * |
||
| 6203 | * @param string $option_name |
||
| 6204 | * |
||
| 6205 | * @return bool |
||
| 6206 | */ |
||
| 6207 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
| 6228 | |||
| 6229 | /* |
||
| 6230 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
| 6231 | * so we can bring them directly to their site in calypso. |
||
| 6232 | * |
||
| 6233 | * @param string | url |
||
| 6234 | * @return string | url without the guff |
||
| 6235 | */ |
||
| 6236 | public static function build_raw_urls( $url ) { |
||
| 6242 | |||
| 6243 | /** |
||
| 6244 | * Stores and prints out domains to prefetch for page speed optimization. |
||
| 6245 | * |
||
| 6246 | * @param mixed $new_urls |
||
| 6247 | */ |
||
| 6248 | public static function dns_prefetch( $new_urls = null ) { |
||
| 6265 | |||
| 6266 | public function wp_dashboard_setup() { |
||
| 6294 | |||
| 6295 | /** |
||
| 6296 | * @param mixed $result Value for the user's option |
||
| 6297 | * @return mixed |
||
| 6298 | */ |
||
| 6299 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
| 6324 | |||
| 6325 | public static function dashboard_widget() { |
||
| 6333 | |||
| 6334 | public static function dashboard_widget_footer() { |
||
| 6367 | |||
| 6368 | public function dashboard_widget_connect_to_wpcom() { |
||
| 6390 | |||
| 6391 | /** |
||
| 6392 | * Return string containing the Jetpack logo. |
||
| 6393 | * |
||
| 6394 | * @since 3.9.0 |
||
| 6395 | * |
||
| 6396 | * @return string |
||
| 6397 | */ |
||
| 6398 | public static function get_jp_emblem() { |
||
| 6401 | |||
| 6402 | /* |
||
| 6403 | * Adds a "blank" column in the user admin table to display indication of user connection. |
||
| 6404 | */ |
||
| 6405 | function jetpack_icon_user_connected( $columns ) { |
||
| 6409 | |||
| 6410 | /* |
||
| 6411 | * Show Jetpack icon if the user is linked. |
||
| 6412 | */ |
||
| 6413 | function jetpack_show_user_connected_icon( $val, $col, $user_id ) { |
||
| 6425 | |||
| 6426 | /* |
||
| 6427 | * Style the Jetpack user column |
||
| 6428 | */ |
||
| 6429 | function jetpack_user_col_style() { |
||
| 6446 | } |
||
| 6447 |
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.