Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Jetpack { |
||
| 26 | public $xmlrpc_server = null; |
||
| 27 | |||
| 28 | private $xmlrpc_verification = null; |
||
| 29 | |||
| 30 | public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array The handles of styles that are concatenated into jetpack.css |
||
| 34 | */ |
||
| 35 | public $concatenated_style_handles = array( |
||
| 36 | 'jetpack-carousel', |
||
| 37 | 'grunion.css', |
||
| 38 | 'the-neverending-homepage', |
||
| 39 | 'jetpack_likes', |
||
| 40 | 'jetpack_related-posts', |
||
| 41 | 'sharedaddy', |
||
| 42 | 'jetpack-slideshow', |
||
| 43 | 'presentations', |
||
| 44 | 'jetpack-subscriptions', |
||
| 45 | 'tiled-gallery', |
||
| 46 | 'widget-conditions', |
||
| 47 | 'jetpack_display_posts_widget', |
||
| 48 | 'gravatar-profile-widget', |
||
| 49 | 'widget-grid-and-list', |
||
| 50 | 'jetpack-widgets', |
||
| 51 | 'goodreads-widget', |
||
| 52 | 'jetpack_social_media_icons_widget', |
||
| 53 | ); |
||
| 54 | |||
| 55 | public $plugins_to_deactivate = array( |
||
| 56 | 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 57 | 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ), |
||
| 58 | 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ), |
||
| 59 | 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ), |
||
| 60 | 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ), |
||
| 61 | 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ), |
||
| 62 | 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ), |
||
| 63 | 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ), |
||
| 64 | 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ), |
||
| 65 | 'videopress' => array( 'video/video.php', 'VideoPress' ), |
||
| 66 | 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ), |
||
| 67 | 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ), |
||
| 68 | 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ), |
||
| 69 | 'omnisearch' => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ), |
||
| 70 | 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ), |
||
| 71 | 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) |
||
| 72 | ); |
||
| 73 | |||
| 74 | public $capability_translations = array( |
||
| 75 | 'administrator' => 'manage_options', |
||
| 76 | 'editor' => 'edit_others_posts', |
||
| 77 | 'author' => 'publish_posts', |
||
| 78 | 'contributor' => 'edit_posts', |
||
| 79 | 'subscriber' => 'read', |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Map of modules that have conflicts with plugins and should not be auto-activated |
||
| 84 | * if the plugins are active. Used by filter_default_modules |
||
| 85 | * |
||
| 86 | * Plugin Authors: If you'd like to prevent a single module from auto-activating, |
||
| 87 | * change `module-slug` and add this to your plugin: |
||
| 88 | * |
||
| 89 | * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); |
||
| 90 | * function my_jetpack_get_default_modules( $modules ) { |
||
| 91 | * return array_diff( $modules, array( 'module-slug' ) ); |
||
| 92 | * } |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private $conflicting_plugins = array( |
||
| 97 | 'comments' => array( |
||
| 98 | 'Intense Debate' => 'intensedebate/intensedebate.php', |
||
| 99 | 'Disqus' => 'disqus-comment-system/disqus.php', |
||
| 100 | 'Livefyre' => 'livefyre-comments/livefyre.php', |
||
| 101 | 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php', |
||
| 102 | 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php', |
||
| 103 | 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php', |
||
| 104 | ), |
||
| 105 | 'contact-form' => array( |
||
| 106 | 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php', |
||
| 107 | 'Gravity Forms' => 'gravityforms/gravityforms.php', |
||
| 108 | 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php', |
||
| 109 | 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php', |
||
| 110 | 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php', |
||
| 111 | ), |
||
| 112 | 'minileven' => array( |
||
| 113 | 'WPtouch' => 'wptouch/wptouch.php', |
||
| 114 | ), |
||
| 115 | 'latex' => array( |
||
| 116 | 'LaTeX for WordPress' => 'latex/latex.php', |
||
| 117 | 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php', |
||
| 118 | 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php', |
||
| 119 | 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php', |
||
| 120 | 'Enable Latex' => 'enable-latex/enable-latex.php', |
||
| 121 | 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php', |
||
| 122 | ), |
||
| 123 | 'protect' => array( |
||
| 124 | 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php', |
||
| 125 | 'Captcha' => 'captcha/captcha.php', |
||
| 126 | 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php', |
||
| 127 | 'Login Security Solution' => 'login-security-solution/login-security-solution.php', |
||
| 128 | 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php', |
||
| 129 | 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php', |
||
| 130 | 'SiteGuard WP Plugin' => 'siteguard/siteguard.php', |
||
| 131 | 'Security-protection' => 'security-protection/security-protection.php', |
||
| 132 | 'Login Security' => 'login-security/login-security.php', |
||
| 133 | 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php', |
||
| 134 | 'Wordfence Security' => 'wordfence/wordfence.php', |
||
| 135 | 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php', |
||
| 136 | 'iThemes Security' => 'better-wp-security/better-wp-security.php', |
||
| 137 | ), |
||
| 138 | 'random-redirect' => array( |
||
| 139 | 'Random Redirect 2' => 'random-redirect-2/random-redirect.php', |
||
| 140 | ), |
||
| 141 | 'related-posts' => array( |
||
| 142 | 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php', |
||
| 143 | 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php', |
||
| 144 | 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php', |
||
| 145 | 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php', |
||
| 146 | 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php', |
||
| 147 | 'outbrain' => 'outbrain/outbrain.php', |
||
| 148 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 149 | 'Sexybookmarks' => 'sexybookmarks/shareaholic.php', |
||
| 150 | ), |
||
| 151 | 'sharedaddy' => array( |
||
| 152 | 'AddThis' => 'addthis/addthis_social_widget.php', |
||
| 153 | 'Add To Any' => 'add-to-any/add-to-any.php', |
||
| 154 | 'ShareThis' => 'share-this/sharethis.php', |
||
| 155 | 'Shareaholic' => 'shareaholic/shareaholic.php', |
||
| 156 | ), |
||
| 157 | 'verification-tools' => array( |
||
| 158 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 159 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 160 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 161 | ), |
||
| 162 | 'widget-visibility' => array( |
||
| 163 | 'Widget Logic' => 'widget-logic/widget_logic.php', |
||
| 164 | 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php', |
||
| 165 | ), |
||
| 166 | 'sitemaps' => array( |
||
| 167 | 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php', |
||
| 168 | 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php', |
||
| 169 | 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php', |
||
| 170 | 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php', |
||
| 171 | 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php', |
||
| 172 | 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php', |
||
| 173 | 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php', |
||
| 174 | 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php', |
||
| 175 | 'Sitemap' => 'sitemap/sitemap.php', |
||
| 176 | 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php', |
||
| 177 | 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php', |
||
| 178 | 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php', |
||
| 179 | 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php', |
||
| 180 | ), |
||
| 181 | ); |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Plugins for which we turn off our Facebook OG Tags implementation. |
||
| 185 | * |
||
| 186 | * Note: WordPress SEO by Yoast and WordPress SEO Premium by Yoast automatically deactivate |
||
| 187 | * Jetpack's Open Graph tags via filter when their Social Meta modules are active. |
||
| 188 | * |
||
| 189 | * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: |
||
| 190 | * add_filter( 'jetpack_enable_open_graph', '__return_false' ); |
||
| 191 | */ |
||
| 192 | private $open_graph_conflicting_plugins = array( |
||
| 193 | '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', |
||
| 194 | // 2 Click Social Media Buttons |
||
| 195 | 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook |
||
| 196 | 'add-meta-tags/add-meta-tags.php', // Add Meta Tags |
||
| 197 | 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail |
||
| 198 | 'facebook/facebook.php', // Facebook (official plugin) |
||
| 199 | 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one |
||
| 200 | 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', |
||
| 201 | // Facebook Featured Image & OG Meta Tags |
||
| 202 | 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags |
||
| 203 | 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', |
||
| 204 | // Facebook Open Graph Meta Tags for WordPress |
||
| 205 | 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag |
||
| 206 | 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer |
||
| 207 | 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', |
||
| 208 | // Fedmich's Facebook Open Graph Meta |
||
| 209 | 'header-footer/plugin.php', // Header and Footer |
||
| 210 | 'network-publisher/networkpub.php', // Network Publisher |
||
| 211 | 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG |
||
| 212 | 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', |
||
| 213 | // NextScripts SNAP |
||
| 214 | 'opengraph/opengraph.php', // Open Graph |
||
| 215 | 'open-graph-protocol-framework/open-graph-protocol-framework.php', |
||
| 216 | // Open Graph Protocol Framework |
||
| 217 | 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments |
||
| 218 | 'seo-ultimate/seo-ultimate.php', // SEO Ultimate |
||
| 219 | 'sexybookmarks/sexy-bookmarks.php', // Shareaholic |
||
| 220 | 'shareaholic/sexy-bookmarks.php', // Shareaholic |
||
| 221 | 'sharepress/sharepress.php', // SharePress |
||
| 222 | 'simple-facebook-connect/sfc.php', // Simple Facebook Connect |
||
| 223 | 'social-discussions/social-discussions.php', // Social Discussions |
||
| 224 | 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit |
||
| 225 | 'socialize/socialize.php', // Socialize |
||
| 226 | 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', |
||
| 227 | // Tweet, Like, Google +1 and Share |
||
| 228 | 'wordbooker/wordbooker.php', // Wordbooker |
||
| 229 | 'wpsso/wpsso.php', // WordPress Social Sharing Optimization |
||
| 230 | 'wp-caregiver/wp-caregiver.php', // WP Caregiver |
||
| 231 | 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', |
||
| 232 | // WP Facebook Like Send & Open Graph Meta |
||
| 233 | 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol |
||
| 234 | 'wp-ogp/wp-ogp.php', // WP-OGP |
||
| 235 | 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin |
||
| 236 | 'wp-fb-share-like-button/wp_fb_share-like_widget.php' // WP Facebook Like Button |
||
| 237 | ); |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Plugins for which we turn off our Twitter Cards Tags implementation. |
||
| 241 | */ |
||
| 242 | private $twitter_cards_conflicting_plugins = array( |
||
| 243 | // 'twitter/twitter.php', // The official one handles this on its own. |
||
| 244 | // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php |
||
| 245 | 'eewee-twitter-card/index.php', // Eewee Twitter Card |
||
| 246 | 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards |
||
| 247 | 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards |
||
| 248 | 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', |
||
| 249 | // Pure Web Brilliant's Social Graph Twitter Cards Extension |
||
| 250 | 'twitter-cards/twitter-cards.php', // Twitter Cards |
||
| 251 | 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta |
||
| 252 | 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards |
||
| 253 | ); |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Message to display in admin_notice |
||
| 257 | * @var string |
||
| 258 | */ |
||
| 259 | public $message = ''; |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Error to display in admin_notice |
||
| 263 | * @var string |
||
| 264 | */ |
||
| 265 | public $error = ''; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Modules that need more privacy description. |
||
| 269 | * @var string |
||
| 270 | */ |
||
| 271 | public $privacy_checks = ''; |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Stats to record once the page loads |
||
| 275 | * |
||
| 276 | * @var array |
||
| 277 | */ |
||
| 278 | public $stats = array(); |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Allows us to build a temporary security report |
||
| 282 | * |
||
| 283 | * @var array |
||
| 284 | */ |
||
| 285 | static $security_report = array(); |
||
|
|
|||
| 286 | |||
| 287 | /** |
||
| 288 | * Jetpack_Sync object |
||
| 289 | */ |
||
| 290 | public $sync; |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Verified data for JSON authorization request |
||
| 294 | */ |
||
| 295 | public $json_api_authorization_request = array(); |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Holds the singleton instance of this class |
||
| 299 | * @since 2.3.3 |
||
| 300 | * @var Jetpack |
||
| 301 | */ |
||
| 302 | static $instance = false; |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Singleton |
||
| 306 | * @static |
||
| 307 | */ |
||
| 308 | public static function init() { |
||
| 309 | if ( ! self::$instance ) { |
||
| 310 | self::$instance = new Jetpack; |
||
| 311 | |||
| 312 | self::$instance->plugin_upgrade(); |
||
| 313 | |||
| 314 | add_action( 'init', array( __CLASS__, 'perform_security_reporting' ) ); |
||
| 315 | |||
| 316 | } |
||
| 317 | |||
| 318 | return self::$instance; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Must never be called statically |
||
| 323 | */ |
||
| 324 | function plugin_upgrade() { |
||
| 325 | if ( Jetpack::is_active() ) { |
||
| 326 | list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); |
||
| 327 | if ( JETPACK__VERSION != $version ) { |
||
| 328 | |||
| 329 | // Check which active modules actually exist and remove others from active_modules list |
||
| 330 | $unfiltered_modules = Jetpack::get_active_modules(); |
||
| 331 | $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) ); |
||
| 332 | if ( array_diff( $unfiltered_modules, $modules ) ) { |
||
| 333 | Jetpack_Options::update_option( 'active_modules', $modules ); |
||
| 334 | } |
||
| 335 | |||
| 336 | add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); |
||
| 337 | /** |
||
| 338 | * Fires when synchronizing all registered options and constants. |
||
| 339 | * |
||
| 340 | * @since 3.3.0 |
||
| 341 | */ |
||
| 342 | do_action( 'jetpack_sync_all_registered_options' ); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | static function activate_manage( ) { |
||
| 348 | |||
| 349 | if ( did_action( 'init' ) || current_filter() == 'init' ) { |
||
| 350 | self::activate_module( 'manage', false, false ); |
||
| 351 | } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) { |
||
| 352 | add_action( 'init', array( __CLASS__, 'activate_manage' ) ); |
||
| 353 | } |
||
| 354 | |||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Constructor. Initializes WordPress hooks |
||
| 359 | */ |
||
| 360 | private function __construct() { |
||
| 508 | |||
| 509 | /* |
||
| 510 | * Make sure any site icon added to core can get |
||
| 511 | * synced back to dotcom, so we can display it there. |
||
| 512 | */ |
||
| 513 | View Code Duplication | function jetpack_sync_core_icon() { |
|
| 514 | if ( function_exists( 'get_site_icon_url' ) ) { |
||
| 515 | $url = get_site_icon_url(); |
||
| 516 | } else { |
||
| 517 | return; |
||
| 518 | } |
||
| 519 | |||
| 520 | require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' ); |
||
| 521 | // If there's a core icon, maybe update the option. If not, fall back to Jetpack's. |
||
| 522 | if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) { |
||
| 523 | // This is the option that is synced with dotcom |
||
| 524 | Jetpack_Options::update_option( 'site_icon_url', $url ); |
||
| 525 | } else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) { |
||
| 526 | Jetpack_Options::delete_option( 'site_icon_url' ); |
||
| 527 | } |
||
| 528 | } |
||
| 529 | |||
| 530 | function jetpack_admin_ajax_tracks_callback() { |
||
| 531 | // Check for nonce |
||
| 532 | if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) { |
||
| 533 | wp_die( 'Permissions check failed.' ); |
||
| 534 | } |
||
| 535 | |||
| 536 | if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) { |
||
| 537 | wp_die( 'No valid event name or type.' ); |
||
| 538 | } |
||
| 539 | |||
| 540 | $tracks_data = array(); |
||
| 541 | if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) { |
||
| 542 | $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] ); |
||
| 543 | } |
||
| 544 | |||
| 545 | JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data ); |
||
| 546 | wp_send_json_success(); |
||
| 547 | wp_die(); |
||
| 548 | } |
||
| 549 | |||
| 550 | function jetpack_admin_ajax_callback() { |
||
| 551 | // Check for nonce |
||
| 552 | View Code Duplication | if ( ! isset( $_REQUEST['adminNonce'] ) || ! wp_verify_nonce( $_REQUEST['adminNonce'], 'jetpack-admin-nonce' ) || ! current_user_can( 'jetpack_manage_modules' ) ) { |
|
| 553 | wp_die( 'permissions check failed' ); |
||
| 554 | } |
||
| 555 | |||
| 556 | if ( isset( $_REQUEST['toggleModule'] ) && 'nux-toggle-module' == $_REQUEST['toggleModule'] ) { |
||
| 557 | $slug = $_REQUEST['thisModuleSlug']; |
||
| 558 | |||
| 559 | if ( ! in_array( $slug, Jetpack::get_available_modules() ) ) { |
||
| 560 | wp_die( 'That is not a Jetpack module slug' ); |
||
| 561 | } |
||
| 562 | |||
| 563 | if ( Jetpack::is_module_active( $slug ) ) { |
||
| 564 | Jetpack::deactivate_module( $slug ); |
||
| 565 | } else { |
||
| 566 | Jetpack::activate_module( $slug, false, false ); |
||
| 567 | } |
||
| 568 | |||
| 569 | $modules = Jetpack_Admin::init()->get_modules(); |
||
| 570 | echo json_encode( $modules[ $slug ] ); |
||
| 571 | |||
| 572 | exit; |
||
| 573 | } |
||
| 574 | |||
| 575 | wp_die(); |
||
| 576 | } |
||
| 577 | |||
| 578 | /* |
||
| 579 | * Sometimes we need to refresh the data, |
||
| 580 | * especially if the page is visited via a 'history' |
||
| 581 | * event like back/forward |
||
| 582 | */ |
||
| 583 | function jetpack_admin_ajax_refresh_data() { |
||
| 584 | // Check for nonce |
||
| 585 | View Code Duplication | if ( ! isset( $_REQUEST['adminNonce'] ) || ! wp_verify_nonce( $_REQUEST['adminNonce'], 'jetpack-admin-nonce' ) ) { |
|
| 586 | wp_die( 'permissions check failed' ); |
||
| 587 | } |
||
| 588 | |||
| 589 | if ( isset( $_REQUEST['refreshData'] ) && 'refresh' == $_REQUEST['refreshData'] ) { |
||
| 590 | $modules = Jetpack_Admin::init()->get_modules(); |
||
| 591 | echo json_encode( $modules ); |
||
| 592 | exit; |
||
| 593 | } |
||
| 594 | |||
| 595 | wp_die(); |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * The callback for the JITM ajax requests. |
||
| 600 | */ |
||
| 601 | function jetpack_jitm_ajax_callback() { |
||
| 602 | // Check for nonce |
||
| 603 | if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { |
||
| 604 | wp_die( 'Module activation failed due to lack of appropriate permissions' ); |
||
| 605 | } |
||
| 606 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { |
||
| 607 | $module_slug = $_REQUEST['jitmModule']; |
||
| 608 | Jetpack::log( 'activate', $module_slug ); |
||
| 609 | Jetpack::activate_module( $module_slug, false, false ); |
||
| 610 | Jetpack::state( 'message', 'no_message' ); |
||
| 611 | |||
| 612 | //A Jetpack module is being activated through a JITM, track it |
||
| 613 | $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); |
||
| 614 | $this->do_stats( 'server_side' ); |
||
| 615 | |||
| 616 | wp_send_json_success(); |
||
| 617 | } |
||
| 618 | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { |
||
| 619 | // get the hide_jitm options array |
||
| 620 | $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); |
||
| 621 | $module_slug = $_REQUEST['jitmModule']; |
||
| 622 | |||
| 623 | if( ! $jetpack_hide_jitm ) { |
||
| 624 | $jetpack_hide_jitm = array( |
||
| 625 | $module_slug => 'hide' |
||
| 626 | ); |
||
| 627 | } else { |
||
| 628 | $jetpack_hide_jitm[$module_slug] = 'hide'; |
||
| 629 | } |
||
| 630 | |||
| 631 | Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); |
||
| 632 | |||
| 633 | //jitm is being dismissed forever, track it |
||
| 634 | $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); |
||
| 635 | $this->do_stats( 'server_side' ); |
||
| 636 | |||
| 637 | wp_send_json_success(); |
||
| 638 | } |
||
| 639 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { |
|
| 640 | $module_slug = $_REQUEST['jitmModule']; |
||
| 641 | |||
| 642 | // User went to WordPress.com, track this |
||
| 643 | $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); |
||
| 644 | $this->do_stats( 'server_side' ); |
||
| 645 | |||
| 646 | wp_send_json_success(); |
||
| 647 | } |
||
| 648 | View Code Duplication | if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) { |
|
| 649 | $track = $_REQUEST['jitmModule']; |
||
| 650 | |||
| 651 | // User is viewing JITM, track it. |
||
| 652 | $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION ); |
||
| 653 | $this->do_stats( 'server_side' ); |
||
| 654 | |||
| 655 | wp_send_json_success(); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * If there are any stats that need to be pushed, but haven't been, push them now. |
||
| 661 | */ |
||
| 662 | function __destruct() { |
||
| 663 | if ( ! empty( $this->stats ) ) { |
||
| 664 | $this->do_stats( 'server_side' ); |
||
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { |
||
| 734 | |||
| 735 | function require_jetpack_authentication() { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Load language files |
||
| 755 | * @action plugins_loaded |
||
| 756 | */ |
||
| 757 | public static function plugin_textdomain() { |
||
| 758 | // Note to self, the third argument must not be hardcoded, to account for relocated folders. |
||
| 759 | load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); |
||
| 760 | } |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Register assets for use in various modules and the Jetpack admin page. |
||
| 764 | * |
||
| 765 | * @uses wp_script_is, wp_register_script, plugins_url |
||
| 766 | * @action wp_loaded |
||
| 767 | * @return null |
||
| 768 | */ |
||
| 769 | public function register_assets() { |
||
| 770 | if ( ! wp_script_is( 'spin', 'registered' ) ) { |
||
| 771 | wp_register_script( 'spin', plugins_url( '_inc/spin.js', JETPACK__PLUGIN_FILE ), false, '1.3' ); |
||
| 772 | } |
||
| 773 | |||
| 774 | View Code Duplication | if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { |
|
| 775 | wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' ); |
||
| 776 | } |
||
| 777 | |||
| 778 | View Code Duplication | if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { |
|
| 779 | wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' ); |
||
| 780 | } |
||
| 781 | |||
| 782 | View Code Duplication | if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) { |
|
| 783 | wp_register_script( 'jetpack-twitter-timeline', plugins_url( '_inc/twitter-timeline.js', JETPACK__PLUGIN_FILE ) , array( 'jquery' ), '4.0.0', true ); |
||
| 784 | } |
||
| 785 | |||
| 786 | if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) { |
||
| 787 | wp_register_script( 'jetpack-facebook-embed', plugins_url( '_inc/facebook-embed.js', __FILE__ ), array( 'jquery' ), null, true ); |
||
| 788 | |||
| 789 | /** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
||
| 790 | $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ); |
||
| 791 | if ( ! is_numeric( $fb_app_id ) ) { |
||
| 792 | $fb_app_id = ''; |
||
| 793 | } |
||
| 794 | wp_localize_script( |
||
| 795 | 'jetpack-facebook-embed', |
||
| 796 | 'jpfbembed', |
||
| 797 | array( |
||
| 798 | 'appid' => $fb_app_id, |
||
| 799 | 'locale' => $this->get_locale(), |
||
| 800 | ) |
||
| 801 | ); |
||
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * As jetpack_register_genericons is by default fired off a hook, |
||
| 806 | * the hook may have already fired by this point. |
||
| 807 | * So, let's just trigger it manually. |
||
| 808 | */ |
||
| 809 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
| 810 | jetpack_register_genericons(); |
||
| 811 | |||
| 812 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) |
|
| 813 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
| 814 | } |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Guess locale from language code. |
||
| 818 | * |
||
| 819 | * @param string $lang Language code. |
||
| 820 | * @return string|bool |
||
| 821 | */ |
||
| 822 | View Code Duplication | function guess_locale_from_lang( $lang ) { |
|
| 823 | if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) { |
||
| 824 | return 'en_US'; |
||
| 825 | } |
||
| 826 | |||
| 827 | if ( ! class_exists( 'GP_Locales' ) ) { |
||
| 828 | if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
||
| 829 | return false; |
||
| 830 | } |
||
| 831 | |||
| 832 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 833 | } |
||
| 834 | |||
| 835 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 836 | // WP.com: get_locale() returns 'it' |
||
| 837 | $locale = GP_Locales::by_slug( $lang ); |
||
| 838 | } else { |
||
| 839 | // Jetpack: get_locale() returns 'it_IT'; |
||
| 840 | $locale = GP_Locales::by_field( 'facebook_locale', $lang ); |
||
| 841 | } |
||
| 842 | |||
| 843 | if ( ! $locale ) { |
||
| 844 | return false; |
||
| 845 | } |
||
| 846 | |||
| 847 | if ( empty( $locale->facebook_locale ) ) { |
||
| 848 | if ( empty( $locale->wp_locale ) ) { |
||
| 849 | return false; |
||
| 850 | } else { |
||
| 851 | // Facebook SDK is smart enough to fall back to en_US if a |
||
| 852 | // locale isn't supported. Since supported Facebook locales |
||
| 853 | // can fall out of sync, we'll attempt to use the known |
||
| 854 | // wp_locale value and rely on said fallback. |
||
| 855 | return $locale->wp_locale; |
||
| 856 | } |
||
| 857 | } |
||
| 858 | |||
| 859 | return $locale->facebook_locale; |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Get the locale. |
||
| 864 | * |
||
| 865 | * @return string|bool |
||
| 866 | */ |
||
| 867 | function get_locale() { |
||
| 868 | $locale = $this->guess_locale_from_lang( get_locale() ); |
||
| 869 | |||
| 870 | if ( ! $locale ) { |
||
| 871 | $locale = 'en_US'; |
||
| 872 | } |
||
| 873 | |||
| 874 | return $locale; |
||
| 875 | } |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Device Pixels support |
||
| 879 | * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. |
||
| 880 | */ |
||
| 881 | function devicepx() { |
||
| 882 | if ( Jetpack::is_active() ) { |
||
| 883 | wp_enqueue_script( 'devicepx', set_url_scheme( 'http://s0.wp.com/wp-content/js/devicepx-jetpack.js' ), array(), gmdate( 'oW' ), true ); |
||
| 884 | } |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Return the network_site_url so that .com knows what network this site is a part of. |
||
| 889 | * @param bool $option |
||
| 890 | * @return string |
||
| 891 | */ |
||
| 892 | public function jetpack_main_network_site_option( $option ) { |
||
| 893 | return network_site_url(); |
||
| 894 | } |
||
| 895 | /** |
||
| 896 | * Network Name. |
||
| 897 | */ |
||
| 898 | static function network_name( $option = null ) { |
||
| 899 | global $current_site; |
||
| 900 | return $current_site->site_name; |
||
| 901 | } |
||
| 902 | /** |
||
| 903 | * Does the network allow new user and site registrations. |
||
| 904 | * @return string |
||
| 905 | */ |
||
| 906 | static function network_allow_new_registrations( $option = null ) { |
||
| 907 | return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); |
||
| 908 | } |
||
| 909 | /** |
||
| 910 | * Does the network allow admins to add new users. |
||
| 911 | * @return boolian |
||
| 912 | */ |
||
| 913 | static function network_add_new_users( $option = null ) { |
||
| 914 | return (bool) get_site_option( 'add_new_users' ); |
||
| 915 | } |
||
| 916 | /** |
||
| 917 | * File upload psace left per site in MB. |
||
| 918 | * -1 means NO LIMIT. |
||
| 919 | * @return number |
||
| 920 | */ |
||
| 921 | static function network_site_upload_space( $option = null ) { |
||
| 922 | // value in MB |
||
| 923 | return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); |
||
| 924 | } |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Network allowed file types. |
||
| 928 | * @return string |
||
| 929 | */ |
||
| 930 | static function network_upload_file_types( $option = null ) { |
||
| 931 | return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); |
||
| 932 | } |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Maximum file upload size set by the network. |
||
| 936 | * @return number |
||
| 937 | */ |
||
| 938 | static function network_max_upload_file_size( $option = null ) { |
||
| 939 | // value in KB |
||
| 940 | return get_site_option( 'fileupload_maxk', 300 ); |
||
| 941 | } |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Lets us know if a site allows admins to manage the network. |
||
| 945 | * @return array |
||
| 946 | */ |
||
| 947 | static function network_enable_administration_menus( $option = null ) { |
||
| 948 | return get_site_option( 'menu_items' ); |
||
| 949 | } |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Return whether we are dealing with a multi network setup or not. |
||
| 953 | * The reason we are type casting this is because we want to avoid the situation where |
||
| 954 | * the result is false since when is_main_network_option return false it cases |
||
| 955 | * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the |
||
| 956 | * database which could be set to anything as opposed to what this function returns. |
||
| 957 | * @param bool $option |
||
| 958 | * |
||
| 959 | * @return boolean |
||
| 960 | */ |
||
| 961 | public function is_main_network_option( $option ) { |
||
| 962 | // return '1' or '' |
||
| 963 | return (string) (bool) Jetpack::is_multi_network(); |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Return true if we are with multi-site or multi-network false if we are dealing with single site. |
||
| 968 | * |
||
| 969 | * @param string $option |
||
| 970 | * @return boolean |
||
| 971 | */ |
||
| 972 | public function is_multisite( $option ) { |
||
| 973 | return (string) (bool) is_multisite(); |
||
| 974 | } |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Implemented since there is no core is multi network function |
||
| 978 | * Right now there is no way to tell if we which network is the dominant network on the system |
||
| 979 | * |
||
| 980 | * @since 3.3 |
||
| 981 | * @return boolean |
||
| 982 | */ |
||
| 983 | public static function is_multi_network() { |
||
| 984 | global $wpdb; |
||
| 985 | |||
| 986 | // if we don't have a multi site setup no need to do any more |
||
| 987 | if ( ! is_multisite() ) { |
||
| 988 | return false; |
||
| 989 | } |
||
| 990 | |||
| 991 | $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); |
||
| 992 | if ( $num_sites > 1 ) { |
||
| 993 | return true; |
||
| 994 | } else { |
||
| 995 | return false; |
||
| 996 | } |
||
| 997 | } |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Trigger an update to the main_network_site when we update the siteurl of a site. |
||
| 1001 | * @return null |
||
| 1002 | */ |
||
| 1003 | function update_jetpack_main_network_site_option() { |
||
| 1004 | // do_action( 'add_option_$option', '$option', '$value-of-the-option' ); |
||
| 1005 | /** |
||
| 1006 | * Fires when the site URL is updated. |
||
| 1007 | * Determines if the site is the main site of a Mulitiste network. |
||
| 1008 | * |
||
| 1009 | * @since 3.3.0 |
||
| 1010 | * |
||
| 1011 | * @param string jetpack_main_network_site. |
||
| 1012 | * @param string network_site_url() Site URL for the "main" site of the current Multisite network. |
||
| 1013 | */ |
||
| 1014 | do_action( 'add_option_jetpack_main_network_site', 'jetpack_main_network_site', network_site_url() ); |
||
| 1015 | /** |
||
| 1016 | * Fires when the site URL is updated. |
||
| 1017 | * Determines if the is part of a multi network. |
||
| 1018 | * |
||
| 1019 | * @since 3.3.0 |
||
| 1020 | * |
||
| 1021 | * @param string jetpack_is_main_network. |
||
| 1022 | * @param bool Jetpack::is_multi_network() Is the site part of a multi network. |
||
| 1023 | */ |
||
| 1024 | do_action( 'add_option_jetpack_is_main_network', 'jetpack_is_main_network', (string) (bool) Jetpack::is_multi_network() ); |
||
| 1025 | /** |
||
| 1026 | * Fires when the site URL is updated. |
||
| 1027 | * Determines if the site is part of a multisite network. |
||
| 1028 | * |
||
| 1029 | * @since 3.4.0 |
||
| 1030 | * |
||
| 1031 | * @param string jetpack_is_multi_site. |
||
| 1032 | * @param bool is_multisite() Is the site part of a mutlisite network. |
||
| 1033 | */ |
||
| 1034 | do_action( 'add_option_jetpack_is_multi_site', 'jetpack_is_multi_site', (string) (bool) is_multisite() ); |
||
| 1035 | } |
||
| 1036 | /** |
||
| 1037 | * Triggered after a user updates the network settings via Network Settings Admin Page |
||
| 1038 | * |
||
| 1039 | */ |
||
| 1040 | function update_jetpack_network_settings() { |
||
| 1041 | // Only sync this info for the main network site. |
||
| 1042 | do_action( 'add_option_jetpack_network_name', 'jetpack_network_name', Jetpack::network_name() ); |
||
| 1043 | do_action( 'add_option_jetpack_network_allow_new_registrations', 'jetpack_network_allow_new_registrations', Jetpack::network_allow_new_registrations() ); |
||
| 1044 | do_action( 'add_option_jetpack_network_add_new_users', 'jetpack_network_add_new_users', Jetpack::network_add_new_users() ); |
||
| 1045 | do_action( 'add_option_jetpack_network_site_upload_space', 'jetpack_network_site_upload_space', Jetpack::network_site_upload_space() ); |
||
| 1046 | do_action( 'add_option_jetpack_network_upload_file_types', 'jetpack_network_upload_file_types', Jetpack::network_upload_file_types() ); |
||
| 1047 | do_action( 'add_option_jetpack_network_enable_administration_menus', 'jetpack_network_enable_administration_menus', Jetpack::network_enable_administration_menus() ); |
||
| 1048 | |||
| 1049 | } |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Get back if the current site is single user site. |
||
| 1053 | * |
||
| 1054 | * @return bool |
||
| 1055 | */ |
||
| 1056 | public static function is_single_user_site() { |
||
| 1057 | |||
| 1058 | $user_query = new WP_User_Query( array( |
||
| 1059 | 'blog_id' => get_current_blog_id(), |
||
| 1060 | 'fields' => 'ID', |
||
| 1061 | 'number' => 2 |
||
| 1062 | ) ); |
||
| 1063 | return 1 === (int) $user_query->get_total(); |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Returns true if the site has file write access false otherwise. |
||
| 1068 | * @return string ( '1' | '0' ) |
||
| 1069 | **/ |
||
| 1070 | View Code Duplication | public static function file_system_write_access() { |
|
| 1071 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 1072 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | require_once( ABSPATH . 'wp-admin/includes/template.php' ); |
||
| 1076 | |||
| 1077 | $filesystem_method = get_filesystem_method(); |
||
| 1078 | if ( $filesystem_method === 'direct' ) { |
||
| 1079 | return 1; |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | ob_start(); |
||
| 1083 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 1084 | ob_end_clean(); |
||
| 1085 | if ( $filesystem_credentials_are_stored ) { |
||
| 1086 | return 1; |
||
| 1087 | } |
||
| 1088 | return 0; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Finds out if a site is using a version control system. |
||
| 1093 | * @return string ( '1' | '0' ) |
||
| 1094 | **/ |
||
| 1095 | View Code Duplication | public static function is_version_controlled() { |
|
| 1096 | |||
| 1097 | if ( !class_exists( 'WP_Automatic_Updater' ) ) { |
||
| 1098 | require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
||
| 1099 | } |
||
| 1100 | $updater = new WP_Automatic_Updater(); |
||
| 1101 | $is_version_controlled = strval( $updater->is_vcs_checkout( $context = ABSPATH ) ); |
||
| 1102 | // transients should not be empty |
||
| 1103 | if ( empty( $is_version_controlled ) ) { |
||
| 1104 | $is_version_controlled = '0'; |
||
| 1105 | } |
||
| 1106 | return $is_version_controlled; |
||
| 1107 | } |
||
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Determines whether the current theme supports featured images or not. |
||
| 1111 | * @return string ( '1' | '0' ) |
||
| 1112 | */ |
||
| 1113 | public static function featured_images_enabled() { |
||
| 1114 | return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | /* |
||
| 1118 | * Sync back wp_version |
||
| 1119 | */ |
||
| 1120 | public static function get_wp_version() { |
||
| 1121 | global $wp_version; |
||
| 1122 | return $wp_version; |
||
| 1123 | } |
||
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Keeps wp_version in sync with .com when WordPress core updates |
||
| 1127 | **/ |
||
| 1128 | public static function update_get_wp_version( $update, $meta_data ) { |
||
| 1129 | if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) { |
||
| 1130 | /** This action is documented in wp-includes/option.php */ |
||
| 1131 | /** |
||
| 1132 | * This triggers the sync for the jetpack version |
||
| 1133 | * See Jetpack_Sync options method for more info. |
||
| 1134 | */ |
||
| 1135 | do_action( 'add_option_jetpack_wp_version', 'jetpack_wp_version', (string) Jetpack::get_wp_version() ); |
||
| 1136 | } |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * jetpack_updates is saved in the following schema: |
||
| 1141 | * |
||
| 1142 | * array ( |
||
| 1143 | * 'plugins' => (int) Number of plugin updates available. |
||
| 1144 | * 'themes' => (int) Number of theme updates available. |
||
| 1145 | * 'wordpress' => (int) Number of WordPress core updates available. |
||
| 1146 | * 'translations' => (int) Number of translation updates available. |
||
| 1147 | * 'total' => (int) Total of all available updates. |
||
| 1148 | * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. |
||
| 1149 | * ) |
||
| 1150 | * @return array |
||
| 1151 | */ |
||
| 1152 | public static function get_updates() { |
||
| 1153 | $update_data = wp_get_update_data(); |
||
| 1154 | |||
| 1155 | // Stores the individual update counts as well as the total count. |
||
| 1156 | if ( isset( $update_data['counts'] ) ) { |
||
| 1157 | $updates = $update_data['counts']; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | // If we need to update WordPress core, let's find the latest version number. |
||
| 1161 | View Code Duplication | if ( ! empty( $updates['wordpress'] ) ) { |
|
| 1162 | $cur = get_preferred_from_update_core(); |
||
| 1163 | if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { |
||
| 1164 | $updates['wp_update_version'] = $cur->current; |
||
| 1165 | } |
||
| 1166 | } |
||
| 1167 | return isset( $updates ) ? $updates : array(); |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | public static function get_update_details() { |
||
| 1171 | $update_details = array( |
||
| 1172 | 'update_core' => get_site_transient( 'update_core' ), |
||
| 1173 | 'update_plugins' => get_site_transient( 'update_plugins' ), |
||
| 1174 | 'update_themes' => get_site_transient( 'update_themes' ), |
||
| 1175 | ); |
||
| 1176 | return $update_details; |
||
| 1177 | } |
||
| 1178 | |||
| 1179 | public static function refresh_update_data() { |
||
| 1180 | if ( current_user_can( 'update_core' ) && current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' ) ) { |
||
| 1181 | /** |
||
| 1182 | * Fires whenever the amount of updates needed for a site changes. |
||
| 1183 | * Syncs an array that includes the number of theme, plugin, and core updates available, as well as the latest core version available. |
||
| 1184 | * |
||
| 1185 | * @since 3.7.0 |
||
| 1186 | * |
||
| 1187 | * @param string jetpack_updates |
||
| 1188 | * @param array Update counts calculated by Jetpack::get_updates |
||
| 1189 | */ |
||
| 1190 | do_action( 'add_option_jetpack_updates', 'jetpack_updates', Jetpack::get_updates() ); |
||
| 1191 | } |
||
| 1192 | /** |
||
| 1193 | * Fires whenever the amount of updates needed for a site changes. |
||
| 1194 | * Syncs an array of core, theme, and plugin data, and which of each is out of date |
||
| 1195 | * |
||
| 1196 | * @since 3.7.0 |
||
| 1197 | * |
||
| 1198 | * @param string jetpack_update_details |
||
| 1199 | * @param array Update details calculated by Jetpack::get_update_details |
||
| 1200 | */ |
||
| 1201 | do_action( 'add_option_jetpack_update_details', 'jetpack_update_details', Jetpack::get_update_details() ); |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | public static function refresh_theme_data() { |
||
| 1205 | /** |
||
| 1206 | * Fires whenever a theme change is made. |
||
| 1207 | * |
||
| 1208 | * @since 3.8.1 |
||
| 1209 | * |
||
| 1210 | * @param string featured_images_enabled |
||
| 1211 | * @param boolean Whether featured images are enabled or not |
||
| 1212 | */ |
||
| 1213 | do_action( 'add_option_jetpack_featured_images_enabled', 'jetpack_featured_images_enabled', Jetpack::featured_images_enabled() ); |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Is Jetpack active? |
||
| 1218 | */ |
||
| 1219 | public static function is_active() { |
||
| 1220 | return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1221 | } |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Is Jetpack in development (offline) mode? |
||
| 1225 | */ |
||
| 1226 | public static function is_development_mode() { |
||
| 1227 | $development_mode = false; |
||
| 1228 | |||
| 1229 | if ( defined( 'JETPACK_DEV_DEBUG' ) ) { |
||
| 1230 | $development_mode = JETPACK_DEV_DEBUG; |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | elseif ( site_url() && false === strpos( site_url(), '.' ) ) { |
||
| 1234 | $development_mode = true; |
||
| 1235 | } |
||
| 1236 | /** |
||
| 1237 | * Filters Jetpack's development mode. |
||
| 1238 | * |
||
| 1239 | * @see https://jetpack.com/support/development-mode/ |
||
| 1240 | * |
||
| 1241 | * @since 2.2.1 |
||
| 1242 | * |
||
| 1243 | * @param bool $development_mode Is Jetpack's development mode active. |
||
| 1244 | */ |
||
| 1245 | return apply_filters( 'jetpack_development_mode', $development_mode ); |
||
| 1246 | } |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Get Jetpack development mode notice text and notice class. |
||
| 1250 | * |
||
| 1251 | * Mirrors the checks made in Jetpack::is_development_mode |
||
| 1252 | * |
||
| 1253 | */ |
||
| 1254 | public static function show_development_mode_notice() { |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Whether Jetpack's version maps to a public release, or a development version. |
||
| 1297 | */ |
||
| 1298 | public static function is_development_version() { |
||
| 1299 | return ! preg_match( '/^\d+(\.\d+)+$/', JETPACK__VERSION ); |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Is a given user (or the current user if none is specified) linked to a WordPress.com user? |
||
| 1304 | */ |
||
| 1305 | public static function is_user_connected( $user_id = false ) { |
||
| 1306 | $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); |
||
| 1307 | if ( ! $user_id ) { |
||
| 1308 | return false; |
||
| 1309 | } |
||
| 1310 | return (bool) Jetpack_Data::get_access_token( $user_id ); |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Get the wpcom user data of the current|specified connected user. |
||
| 1315 | */ |
||
| 1316 | View Code Duplication | public static function get_connected_user_data( $user_id = null ) { |
|
| 1317 | if ( ! $user_id ) { |
||
| 1318 | $user_id = get_current_user_id(); |
||
| 1319 | } |
||
| 1320 | Jetpack::load_xml_rpc_client(); |
||
| 1321 | $xml = new Jetpack_IXR_Client( array( |
||
| 1322 | 'user_id' => $user_id, |
||
| 1323 | ) ); |
||
| 1324 | $xml->query( 'wpcom.getUser' ); |
||
| 1325 | if ( ! $xml->isError() ) { |
||
| 1326 | return $xml->getResponse(); |
||
| 1327 | } |
||
| 1328 | return false; |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * Get the wpcom email of the current|specified connected user. |
||
| 1333 | */ |
||
| 1334 | View Code Duplication | public static function get_connected_user_email( $user_id = null ) { |
|
| 1335 | if ( ! $user_id ) { |
||
| 1336 | $user_id = get_current_user_id(); |
||
| 1337 | } |
||
| 1338 | Jetpack::load_xml_rpc_client(); |
||
| 1339 | $xml = new Jetpack_IXR_Client( array( |
||
| 1340 | 'user_id' => $user_id, |
||
| 1341 | ) ); |
||
| 1342 | $xml->query( 'wpcom.getUserEmail' ); |
||
| 1343 | if ( ! $xml->isError() ) { |
||
| 1344 | return $xml->getResponse(); |
||
| 1345 | } |
||
| 1346 | return false; |
||
| 1347 | } |
||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Get the wpcom email of the master user. |
||
| 1351 | */ |
||
| 1352 | public static function get_master_user_email() { |
||
| 1353 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
| 1354 | if ( $master_user_id ) { |
||
| 1355 | return self::get_connected_user_email( $master_user_id ); |
||
| 1356 | } |
||
| 1357 | return ''; |
||
| 1358 | } |
||
| 1359 | |||
| 1360 | function current_user_is_connection_owner() { |
||
| 1361 | $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1362 | return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; |
||
| 1363 | } |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. |
||
| 1367 | */ |
||
| 1368 | function extra_oembed_providers() { |
||
| 1369 | // Cloudup: https://dev.cloudup.com/#oembed |
||
| 1370 | wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); |
||
| 1371 | wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); |
||
| 1372 | wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); |
||
| 1373 | wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true ); |
||
| 1374 | wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true ); |
||
| 1375 | } |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Synchronize connected user role changes |
||
| 1379 | */ |
||
| 1380 | function user_role_change( $user_id ) { |
||
| 1381 | if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) { |
||
| 1382 | $current_user_id = get_current_user_id(); |
||
| 1383 | wp_set_current_user( $user_id ); |
||
| 1384 | $role = $this->translate_current_user_to_role(); |
||
| 1385 | $signed_role = $this->sign_role( $role ); |
||
| 1386 | wp_set_current_user( $current_user_id ); |
||
| 1387 | |||
| 1388 | $master_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); |
||
| 1389 | $master_user_id = absint( $master_token->external_user_id ); |
||
| 1390 | |||
| 1391 | if ( ! $master_user_id ) |
||
| 1392 | return; // this shouldn't happen |
||
| 1393 | |||
| 1394 | Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role ); |
||
| 1395 | //@todo retry on failure |
||
| 1396 | |||
| 1397 | //try to choose a new master if we're demoting the current one |
||
| 1398 | View Code Duplication | if ( $user_id == $master_user_id && 'administrator' != $role ) { |
|
| 1399 | $query = new WP_User_Query( |
||
| 1400 | array( |
||
| 1401 | 'fields' => array( 'id' ), |
||
| 1402 | 'role' => 'administrator', |
||
| 1403 | 'orderby' => 'id', |
||
| 1404 | 'exclude' => array( $master_user_id ), |
||
| 1405 | ) |
||
| 1406 | ); |
||
| 1407 | $new_master = false; |
||
| 1408 | foreach ( $query->results as $result ) { |
||
| 1409 | $uid = absint( $result->id ); |
||
| 1410 | if ( $uid && Jetpack::is_user_connected( $uid ) ) { |
||
| 1411 | $new_master = $uid; |
||
| 1412 | break; |
||
| 1413 | } |
||
| 1414 | } |
||
| 1415 | |||
| 1416 | if ( $new_master ) { |
||
| 1417 | Jetpack_Options::update_option( 'master_user', $new_master ); |
||
| 1418 | } |
||
| 1419 | // else disconnect..? |
||
| 1420 | } |
||
| 1421 | } |
||
| 1422 | } |
||
| 1423 | |||
| 1424 | /** |
||
| 1425 | * Loads the currently active modules. |
||
| 1426 | */ |
||
| 1427 | public static function load_modules() { |
||
| 1428 | if ( ! self::is_active() && !self::is_development_mode() ) { |
||
| 1429 | if ( ! is_multisite() || ! get_site_option( 'jetpack_protect_active' ) ) { |
||
| 1430 | return; |
||
| 1431 | } |
||
| 1432 | } |
||
| 1433 | |||
| 1434 | $version = Jetpack_Options::get_option( 'version' ); |
||
| 1435 | View Code Duplication | if ( ! $version ) { |
|
| 1436 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
| 1437 | /** This action is documented in class.jetpack.php */ |
||
| 1438 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 1439 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 1440 | } |
||
| 1441 | list( $version ) = explode( ':', $version ); |
||
| 1442 | |||
| 1443 | $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); |
||
| 1444 | |||
| 1445 | $modules_data = array(); |
||
| 1446 | |||
| 1447 | // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. |
||
| 1448 | if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { |
||
| 1449 | $updated_modules = array(); |
||
| 1450 | foreach ( $modules as $module ) { |
||
| 1451 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1452 | if ( ! isset( $modules_data[ $module ]['changed'] ) ) { |
||
| 1453 | continue; |
||
| 1454 | } |
||
| 1455 | |||
| 1456 | if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { |
||
| 1457 | continue; |
||
| 1458 | } |
||
| 1459 | |||
| 1460 | $updated_modules[] = $module; |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | $modules = array_diff( $modules, $updated_modules ); |
||
| 1464 | } |
||
| 1465 | |||
| 1466 | $is_development_mode = Jetpack::is_development_mode(); |
||
| 1467 | |||
| 1468 | foreach ( $modules as $index => $module ) { |
||
| 1469 | // If we're in dev mode, disable modules requiring a connection |
||
| 1470 | if ( $is_development_mode ) { |
||
| 1471 | // Prime the pump if we need to |
||
| 1472 | if ( empty( $modules_data[ $module ] ) ) { |
||
| 1473 | $modules_data[ $module ] = Jetpack::get_module( $module ); |
||
| 1474 | } |
||
| 1475 | // If the module requires a connection, but we're in local mode, don't include it. |
||
| 1476 | if ( $modules_data[ $module ]['requires_connection'] ) { |
||
| 1477 | continue; |
||
| 1478 | } |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | if ( did_action( 'jetpack_module_loaded_' . $module ) ) { |
||
| 1482 | continue; |
||
| 1483 | } |
||
| 1484 | |||
| 1485 | if ( ! @include( Jetpack::get_module_path( $module ) ) ) { |
||
| 1486 | unset( $modules[ $index ] ); |
||
| 1487 | Jetpack_Options::update_option( 'active_modules', array_values( $modules ) ); |
||
| 1488 | continue; |
||
| 1489 | } |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Fires when a specific module is loaded. |
||
| 1493 | * The dynamic part of the hook, $module, is the module slug. |
||
| 1494 | * |
||
| 1495 | * @since 1.1.0 |
||
| 1496 | */ |
||
| 1497 | do_action( 'jetpack_module_loaded_' . $module ); |
||
| 1498 | } |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Fires when all the modules are loaded. |
||
| 1502 | * |
||
| 1503 | * @since 1.1.0 |
||
| 1504 | */ |
||
| 1505 | do_action( 'jetpack_modules_loaded' ); |
||
| 1506 | |||
| 1507 | // 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. |
||
| 1508 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) |
||
| 1509 | require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); |
||
| 1510 | } |
||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * Check if Jetpack's REST API compat file should be included |
||
| 1514 | * @action plugins_loaded |
||
| 1515 | * @return null |
||
| 1516 | */ |
||
| 1517 | public function check_rest_api_compat() { |
||
| 1518 | /** |
||
| 1519 | * Filters the list of REST API compat files to be included. |
||
| 1520 | * |
||
| 1521 | * @since 2.2.5 |
||
| 1522 | * |
||
| 1523 | * @param array $args Array of REST API compat files to include. |
||
| 1524 | */ |
||
| 1525 | $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); |
||
| 1526 | |||
| 1527 | if ( function_exists( 'bbpress' ) ) |
||
| 1528 | $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; |
||
| 1529 | |||
| 1530 | foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) |
||
| 1531 | require_once $_jetpack_rest_api_compat_include; |
||
| 1532 | } |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Gets all plugins currently active in values, regardless of whether they're |
||
| 1536 | * traditionally activated or network activated. |
||
| 1537 | * |
||
| 1538 | * @todo Store the result in core's object cache maybe? |
||
| 1539 | */ |
||
| 1540 | public static function get_active_plugins() { |
||
| 1541 | $active_plugins = (array) get_option( 'active_plugins', array() ); |
||
| 1542 | |||
| 1543 | if ( is_multisite() ) { |
||
| 1544 | // Due to legacy code, active_sitewide_plugins stores them in the keys, |
||
| 1545 | // whereas active_plugins stores them in the values. |
||
| 1546 | $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); |
||
| 1547 | if ( $network_plugins ) { |
||
| 1548 | $active_plugins = array_merge( $active_plugins, $network_plugins ); |
||
| 1549 | } |
||
| 1550 | } |
||
| 1551 | |||
| 1552 | sort( $active_plugins ); |
||
| 1553 | |||
| 1554 | return array_unique( $active_plugins ); |
||
| 1555 | } |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Gets and parses additional plugin data to send with the heartbeat data |
||
| 1559 | * |
||
| 1560 | * @since 3.8.1 |
||
| 1561 | * |
||
| 1562 | * @return array Array of plugin data |
||
| 1563 | */ |
||
| 1564 | public static function get_parsed_plugin_data() { |
||
| 1565 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 1566 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
| 1567 | } |
||
| 1568 | $all_plugins = get_plugins(); |
||
| 1569 | $active_plugins = Jetpack::get_active_plugins(); |
||
| 1570 | |||
| 1571 | $plugins = array(); |
||
| 1572 | foreach ( $all_plugins as $path => $plugin_data ) { |
||
| 1573 | $plugins[ $path ] = array( |
||
| 1574 | 'is_active' => in_array( $path, $active_plugins ), |
||
| 1575 | 'file' => $path, |
||
| 1576 | 'name' => $plugin_data['Name'], |
||
| 1577 | 'version' => $plugin_data['Version'], |
||
| 1578 | 'author' => $plugin_data['Author'], |
||
| 1579 | ); |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | return $plugins; |
||
| 1583 | } |
||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Gets and parses theme data to send with the heartbeat data |
||
| 1587 | * |
||
| 1588 | * @since 3.8.1 |
||
| 1589 | * |
||
| 1590 | * @return array Array of theme data |
||
| 1591 | */ |
||
| 1592 | public static function get_parsed_theme_data() { |
||
| 1593 | $all_themes = wp_get_themes( array( 'allowed' => true ) ); |
||
| 1594 | $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); |
||
| 1595 | |||
| 1596 | $themes = array(); |
||
| 1597 | foreach ( $all_themes as $slug => $theme_data ) { |
||
| 1598 | $theme_headers = array(); |
||
| 1599 | foreach ( $header_keys as $header_key ) { |
||
| 1600 | $theme_headers[ $header_key ] = $theme_data->get( $header_key ); |
||
| 1601 | } |
||
| 1602 | |||
| 1603 | $themes[ $slug ] = array( |
||
| 1604 | 'is_active_theme' => $slug == wp_get_theme()->get_template(), |
||
| 1605 | 'slug' => $slug, |
||
| 1606 | 'theme_root' => $theme_data->get_theme_root_uri(), |
||
| 1607 | 'parent' => $theme_data->parent(), |
||
| 1608 | 'headers' => $theme_headers |
||
| 1609 | ); |
||
| 1610 | } |
||
| 1611 | |||
| 1612 | return $themes; |
||
| 1613 | } |
||
| 1614 | |||
| 1615 | /** |
||
| 1616 | * Checks whether a specific plugin is active. |
||
| 1617 | * |
||
| 1618 | * We don't want to store these in a static variable, in case |
||
| 1619 | * there are switch_to_blog() calls involved. |
||
| 1620 | */ |
||
| 1621 | public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { |
||
| 1622 | return in_array( $plugin, self::get_active_plugins() ); |
||
| 1623 | } |
||
| 1624 | |||
| 1625 | /** |
||
| 1626 | * Check if Jetpack's Open Graph tags should be used. |
||
| 1627 | * If certain plugins are active, Jetpack's og tags are suppressed. |
||
| 1628 | * |
||
| 1629 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1630 | * @action plugins_loaded |
||
| 1631 | * @return null |
||
| 1632 | */ |
||
| 1633 | public function check_open_graph() { |
||
| 1634 | if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { |
||
| 1635 | add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); |
||
| 1636 | } |
||
| 1637 | |||
| 1638 | $active_plugins = self::get_active_plugins(); |
||
| 1639 | |||
| 1640 | if ( ! empty( $active_plugins ) ) { |
||
| 1641 | foreach ( $this->open_graph_conflicting_plugins as $plugin ) { |
||
| 1642 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 1643 | add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); |
||
| 1644 | break; |
||
| 1645 | } |
||
| 1646 | } |
||
| 1647 | } |
||
| 1648 | |||
| 1649 | /** |
||
| 1650 | * Allow the addition of Open Graph Meta Tags to all pages. |
||
| 1651 | * |
||
| 1652 | * @since 2.0.3 |
||
| 1653 | * |
||
| 1654 | * @param bool false Should Open Graph Meta tags be added. Default to false. |
||
| 1655 | */ |
||
| 1656 | if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { |
||
| 1657 | require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; |
||
| 1658 | } |
||
| 1659 | } |
||
| 1660 | |||
| 1661 | /** |
||
| 1662 | * Check if Jetpack's Twitter tags should be used. |
||
| 1663 | * If certain plugins are active, Jetpack's twitter tags are suppressed. |
||
| 1664 | * |
||
| 1665 | * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters |
||
| 1666 | * @action plugins_loaded |
||
| 1667 | * @return null |
||
| 1668 | */ |
||
| 1669 | public function check_twitter_tags() { |
||
| 1670 | |||
| 1671 | $active_plugins = self::get_active_plugins(); |
||
| 1672 | |||
| 1673 | if ( ! empty( $active_plugins ) ) { |
||
| 1674 | foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { |
||
| 1675 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 1676 | add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); |
||
| 1677 | break; |
||
| 1678 | } |
||
| 1679 | } |
||
| 1680 | } |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Allow Twitter Card Meta tags to be disabled. |
||
| 1684 | * |
||
| 1685 | * @since 2.6.0 |
||
| 1686 | * |
||
| 1687 | * @param bool true Should Twitter Card Meta tags be disabled. Default to true. |
||
| 1688 | */ |
||
| 1689 | if ( apply_filters( 'jetpack_disable_twitter_cards', true ) ) { |
||
| 1690 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; |
||
| 1691 | } |
||
| 1692 | } |
||
| 1693 | |||
| 1694 | |||
| 1695 | |||
| 1696 | |||
| 1697 | /* |
||
| 1698 | * |
||
| 1699 | * Jetpack Security Reports |
||
| 1700 | * |
||
| 1701 | * Allowed types: login_form, backup, file_scanning, spam |
||
| 1702 | * |
||
| 1703 | * Args for login_form and spam: 'blocked'=>(int)(optional), 'status'=>(string)(ok, warning, error), 'message'=>(optional, disregarded if status is ok, allowed tags: a, em, strong) |
||
| 1704 | * |
||
| 1705 | * Args for backup and file_scanning: 'last'=>(timestamp)(optional), 'next'=>(timestamp)(optional), 'status'=>(string)(ok, warning, error), 'message'=>(optional, disregarded if status is ok, allowed tags: a, em, strong) |
||
| 1706 | * |
||
| 1707 | * |
||
| 1708 | * Example code to submit a security report: |
||
| 1709 | * |
||
| 1710 | * function akismet_submit_jetpack_security_report() { |
||
| 1711 | * Jetpack::submit_security_report( 'spam', __FILE__, $args = array( 'blocked' => 138284, status => 'ok' ) ); |
||
| 1712 | * } |
||
| 1713 | * add_action( 'jetpack_security_report', 'akismet_submit_jetpack_security_report' ); |
||
| 1714 | * |
||
| 1715 | */ |
||
| 1716 | |||
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Calls for security report submissions. |
||
| 1720 | * |
||
| 1721 | * @return null |
||
| 1722 | */ |
||
| 1723 | public static function perform_security_reporting() { |
||
| 1724 | $no_check_needed = get_site_transient( 'security_report_performed_recently' ); |
||
| 1725 | |||
| 1726 | if ( $no_check_needed ) { |
||
| 1727 | return; |
||
| 1728 | } |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Fires before a security report is created. |
||
| 1732 | * |
||
| 1733 | * @since 3.4.0 |
||
| 1734 | */ |
||
| 1735 | do_action( 'jetpack_security_report' ); |
||
| 1736 | |||
| 1737 | Jetpack_Options::update_option( 'security_report', self::$security_report ); |
||
| 1738 | set_site_transient( 'security_report_performed_recently', 1, 15 * MINUTE_IN_SECONDS ); |
||
| 1739 | } |
||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * Allows plugins to submit security reports. |
||
| 1743 | * |
||
| 1744 | * @param string $type Report type (login_form, backup, file_scanning, spam) |
||
| 1745 | * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data |
||
| 1746 | * @param array $args See definitions above |
||
| 1747 | */ |
||
| 1748 | public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { |
||
| 1749 | |||
| 1750 | if( !doing_action( 'jetpack_security_report' ) ) { |
||
| 1751 | return new WP_Error( 'not_collecting_report', 'Not currently collecting security reports. Please use the jetpack_security_report hook.' ); |
||
| 1752 | } |
||
| 1753 | |||
| 1754 | if( !is_string( $type ) || !is_string( $plugin_file ) ) { |
||
| 1755 | return new WP_Error( 'invalid_security_report', 'Invalid Security Report' ); |
||
| 1756 | } |
||
| 1757 | |||
| 1758 | if( !function_exists( 'get_plugin_data' ) ) { |
||
| 1759 | include( ABSPATH . 'wp-admin/includes/plugin.php' ); |
||
| 1760 | } |
||
| 1761 | |||
| 1762 | //Get rid of any non-allowed args |
||
| 1763 | $args = array_intersect_key( $args, array_flip( array( 'blocked', 'last', 'next', 'status', 'message' ) ) ); |
||
| 1764 | |||
| 1765 | $plugin = get_plugin_data( $plugin_file ); |
||
| 1766 | |||
| 1767 | if ( !$plugin['Name'] ) { |
||
| 1768 | return new WP_Error( 'security_report_missing_plugin_name', 'Invalid Plugin File Provided' ); |
||
| 1769 | } |
||
| 1770 | |||
| 1771 | // Sanitize everything to make sure we're not syncing something wonky |
||
| 1772 | $type = sanitize_key( $type ); |
||
| 1773 | |||
| 1774 | $args['plugin'] = $plugin; |
||
| 1775 | |||
| 1776 | // Cast blocked, last and next as integers. |
||
| 1777 | // Last and next should be in unix timestamp format |
||
| 1778 | if ( isset( $args['blocked'] ) ) { |
||
| 1779 | $args['blocked'] = (int) $args['blocked']; |
||
| 1780 | } |
||
| 1781 | if ( isset( $args['last'] ) ) { |
||
| 1782 | $args['last'] = (int) $args['last']; |
||
| 1783 | } |
||
| 1784 | if ( isset( $args['next'] ) ) { |
||
| 1785 | $args['next'] = (int) $args['next']; |
||
| 1786 | } |
||
| 1787 | if ( !in_array( $args['status'], array( 'ok', 'warning', 'error' ) ) ) { |
||
| 1788 | $args['status'] = 'ok'; |
||
| 1789 | } |
||
| 1790 | if ( isset( $args['message'] ) ) { |
||
| 1791 | |||
| 1792 | if( $args['status'] == 'ok' ) { |
||
| 1793 | unset( $args['message'] ); |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | $allowed_html = array( |
||
| 1797 | 'a' => array( |
||
| 1798 | 'href' => array(), |
||
| 1799 | 'title' => array() |
||
| 1800 | ), |
||
| 1801 | 'em' => array(), |
||
| 1802 | 'strong' => array(), |
||
| 1803 | ); |
||
| 1804 | |||
| 1805 | $args['message'] = wp_kses( $args['message'], $allowed_html ); |
||
| 1806 | } |
||
| 1807 | |||
| 1808 | $plugin_name = $plugin[ 'Name' ]; |
||
| 1809 | |||
| 1810 | self::$security_report[ $type ][ $plugin_name ] = $args; |
||
| 1811 | } |
||
| 1812 | |||
| 1813 | /** |
||
| 1814 | * Collects a new report if needed, then returns it. |
||
| 1815 | */ |
||
| 1816 | public function get_security_report() { |
||
| 1817 | self::perform_security_reporting(); |
||
| 1818 | return Jetpack_Options::get_option( 'security_report' ); |
||
| 1819 | } |
||
| 1820 | |||
| 1821 | |||
| 1822 | /* Jetpack Options API */ |
||
| 1823 | |||
| 1824 | public static function get_option_names( $type = 'compact' ) { |
||
| 1825 | return Jetpack_Options::get_option_names( $type ); |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | /** |
||
| 1829 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
| 1830 | * |
||
| 1831 | * @param string $name Option name |
||
| 1832 | * @param mixed $default (optional) |
||
| 1833 | */ |
||
| 1834 | public static function get_option( $name, $default = false ) { |
||
| 1835 | return Jetpack_Options::get_option( $name, $default ); |
||
| 1836 | } |
||
| 1837 | |||
| 1838 | /** |
||
| 1839 | * Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action |
||
| 1840 | * Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted |
||
| 1841 | * $name must be a registered option name. |
||
| 1842 | */ |
||
| 1843 | public static function create_nonce( $name ) { |
||
| 1844 | $secret = wp_generate_password( 32, false ) . ':' . wp_generate_password( 32, false ) . ':' . ( time() + 600 ); |
||
| 1845 | |||
| 1846 | Jetpack_Options::update_option( $name, $secret ); |
||
| 1847 | @list( $secret_1, $secret_2, $eol ) = explode( ':', Jetpack_Options::get_option( $name ) ); |
||
| 1848 | if ( empty( $secret_1 ) || empty( $secret_2 ) || $eol < time() ) |
||
| 1849 | return new Jetpack_Error( 'missing_secrets' ); |
||
| 1850 | |||
| 1851 | return array( |
||
| 1852 | 'secret_1' => $secret_1, |
||
| 1853 | 'secret_2' => $secret_2, |
||
| 1854 | 'eol' => $eol, |
||
| 1855 | ); |
||
| 1856 | } |
||
| 1857 | |||
| 1858 | /** |
||
| 1859 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
| 1860 | * |
||
| 1861 | * @deprecated 3.4 use Jetpack_Options::update_option() instead. |
||
| 1862 | * @param string $name Option name |
||
| 1863 | * @param mixed $value Option value |
||
| 1864 | */ |
||
| 1865 | public static function update_option( $name, $value ) { |
||
| 1866 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' ); |
||
| 1867 | return Jetpack_Options::update_option( $name, $value ); |
||
| 1868 | } |
||
| 1869 | |||
| 1870 | /** |
||
| 1871 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
| 1872 | * |
||
| 1873 | * @deprecated 3.4 use Jetpack_Options::update_options() instead. |
||
| 1874 | * @param array $array array( option name => option value, ... ) |
||
| 1875 | */ |
||
| 1876 | public static function update_options( $array ) { |
||
| 1877 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' ); |
||
| 1878 | return Jetpack_Options::update_options( $array ); |
||
| 1879 | } |
||
| 1880 | |||
| 1881 | /** |
||
| 1882 | * Deletes the given option. May be passed multiple option names as an array. |
||
| 1883 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
| 1884 | * |
||
| 1885 | * @deprecated 3.4 use Jetpack_Options::delete_option() instead. |
||
| 1886 | * @param string|array $names |
||
| 1887 | */ |
||
| 1888 | public static function delete_option( $names ) { |
||
| 1889 | _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' ); |
||
| 1890 | return Jetpack_Options::delete_option( $names ); |
||
| 1891 | } |
||
| 1892 | |||
| 1893 | /** |
||
| 1894 | * Enters a user token into the user_tokens option |
||
| 1895 | * |
||
| 1896 | * @param int $user_id |
||
| 1897 | * @param string $token |
||
| 1898 | * return bool |
||
| 1899 | */ |
||
| 1900 | public static function update_user_token( $user_id, $token, $is_master_user ) { |
||
| 1901 | // not designed for concurrent updates |
||
| 1902 | $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); |
||
| 1903 | if ( ! is_array( $user_tokens ) ) |
||
| 1904 | $user_tokens = array(); |
||
| 1905 | $user_tokens[$user_id] = $token; |
||
| 1906 | if ( $is_master_user ) { |
||
| 1907 | $master_user = $user_id; |
||
| 1908 | $options = compact( 'user_tokens', 'master_user' ); |
||
| 1909 | } else { |
||
| 1910 | $options = compact( 'user_tokens' ); |
||
| 1911 | } |
||
| 1912 | return Jetpack_Options::update_options( $options ); |
||
| 1913 | } |
||
| 1914 | |||
| 1915 | /** |
||
| 1916 | * Returns an array of all PHP files in the specified absolute path. |
||
| 1917 | * Equivalent to glob( "$absolute_path/*.php" ). |
||
| 1918 | * |
||
| 1919 | * @param string $absolute_path The absolute path of the directory to search. |
||
| 1920 | * @return array Array of absolute paths to the PHP files. |
||
| 1921 | */ |
||
| 1922 | public static function glob_php( $absolute_path ) { |
||
| 1923 | if ( function_exists( 'glob' ) ) { |
||
| 1924 | return glob( "$absolute_path/*.php" ); |
||
| 1925 | } |
||
| 1926 | |||
| 1927 | $absolute_path = untrailingslashit( $absolute_path ); |
||
| 1928 | $files = array(); |
||
| 1929 | if ( ! $dir = @opendir( $absolute_path ) ) { |
||
| 1930 | return $files; |
||
| 1931 | } |
||
| 1932 | |||
| 1933 | while ( false !== $file = readdir( $dir ) ) { |
||
| 1934 | if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) { |
||
| 1935 | continue; |
||
| 1936 | } |
||
| 1937 | |||
| 1938 | $file = "$absolute_path/$file"; |
||
| 1939 | |||
| 1940 | if ( ! is_file( $file ) ) { |
||
| 1941 | continue; |
||
| 1942 | } |
||
| 1943 | |||
| 1944 | $files[] = $file; |
||
| 1945 | } |
||
| 1946 | |||
| 1947 | closedir( $dir ); |
||
| 1948 | |||
| 1949 | return $files; |
||
| 1950 | } |
||
| 1951 | |||
| 1952 | public static function activate_new_modules( $redirect = false ) { |
||
| 1953 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
| 1954 | return; |
||
| 1955 | } |
||
| 1956 | |||
| 1957 | $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic] |
||
| 1958 | View Code Duplication | if ( ! $jetpack_old_version ) { |
|
| 1959 | $jetpack_old_version = $version = $old_version = '1.1:' . time(); |
||
| 1960 | /** This action is documented in class.jetpack.php */ |
||
| 1961 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 1962 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 1963 | } |
||
| 1964 | |||
| 1965 | list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic] |
||
| 1966 | |||
| 1967 | if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) { |
||
| 1968 | return; |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | $active_modules = Jetpack::get_active_modules(); |
||
| 1972 | $reactivate_modules = array(); |
||
| 1973 | foreach ( $active_modules as $active_module ) { |
||
| 1974 | $module = Jetpack::get_module( $active_module ); |
||
| 1975 | if ( ! isset( $module['changed'] ) ) { |
||
| 1976 | continue; |
||
| 1977 | } |
||
| 1978 | |||
| 1979 | if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) { |
||
| 1980 | continue; |
||
| 1981 | } |
||
| 1982 | |||
| 1983 | $reactivate_modules[] = $active_module; |
||
| 1984 | Jetpack::deactivate_module( $active_module ); |
||
| 1985 | } |
||
| 1986 | |||
| 1987 | $new_version = JETPACK__VERSION . ':' . time(); |
||
| 1988 | /** This action is documented in class.jetpack.php */ |
||
| 1989 | do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version ); |
||
| 1990 | Jetpack_Options::update_options( |
||
| 1991 | array( |
||
| 1992 | 'version' => $new_version, |
||
| 1993 | 'old_version' => $jetpack_old_version, |
||
| 1994 | ) |
||
| 1995 | ); |
||
| 1996 | |||
| 1997 | Jetpack::state( 'message', 'modules_activated' ); |
||
| 1998 | Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules ); |
||
| 1999 | |||
| 2000 | if ( $redirect ) { |
||
| 2001 | $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page |
||
| 2002 | if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) { |
||
| 2003 | $page = $_GET['page']; |
||
| 2004 | } |
||
| 2005 | |||
| 2006 | wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) ); |
||
| 2007 | exit; |
||
| 2008 | } |
||
| 2009 | } |
||
| 2010 | |||
| 2011 | /** |
||
| 2012 | * List available Jetpack modules. Simply lists .php files in /modules/. |
||
| 2013 | * Make sure to tuck away module "library" files in a sub-directory. |
||
| 2014 | */ |
||
| 2015 | public static function get_available_modules( $min_version = false, $max_version = false ) { |
||
| 2016 | static $modules = null; |
||
| 2017 | |||
| 2018 | if ( ! isset( $modules ) ) { |
||
| 2019 | $available_modules_option = Jetpack_Options::get_option( 'available_modules', array() ); |
||
| 2020 | // Use the cache if we're on the front-end and it's available... |
||
| 2021 | if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) { |
||
| 2022 | $modules = $available_modules_option[ JETPACK__VERSION ]; |
||
| 2023 | } else { |
||
| 2024 | $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' ); |
||
| 2025 | |||
| 2026 | $modules = array(); |
||
| 2027 | |||
| 2028 | foreach ( $files as $file ) { |
||
| 2029 | if ( ! $headers = Jetpack::get_module( $file ) ) { |
||
| 2030 | continue; |
||
| 2031 | } |
||
| 2032 | |||
| 2033 | $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced']; |
||
| 2034 | } |
||
| 2035 | |||
| 2036 | Jetpack_Options::update_option( 'available_modules', array( |
||
| 2037 | JETPACK__VERSION => $modules, |
||
| 2038 | ) ); |
||
| 2039 | } |
||
| 2040 | } |
||
| 2041 | |||
| 2042 | /** |
||
| 2043 | * Filters the array of modules available to be activated. |
||
| 2044 | * |
||
| 2045 | * @since 2.4.0 |
||
| 2046 | * |
||
| 2047 | * @param array $modules Array of available modules. |
||
| 2048 | * @param string $min_version Minimum version number required to use modules. |
||
| 2049 | * @param string $max_version Maximum version number required to use modules. |
||
| 2050 | */ |
||
| 2051 | $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version ); |
||
| 2052 | |||
| 2053 | if ( ! $min_version && ! $max_version ) { |
||
| 2054 | return array_keys( $mods ); |
||
| 2055 | } |
||
| 2056 | |||
| 2057 | $r = array(); |
||
| 2058 | foreach ( $mods as $slug => $introduced ) { |
||
| 2059 | if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) { |
||
| 2060 | continue; |
||
| 2061 | } |
||
| 2062 | |||
| 2063 | if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) { |
||
| 2064 | continue; |
||
| 2065 | } |
||
| 2066 | |||
| 2067 | $r[] = $slug; |
||
| 2068 | } |
||
| 2069 | |||
| 2070 | return $r; |
||
| 2071 | } |
||
| 2072 | |||
| 2073 | /** |
||
| 2074 | * Default modules loaded on activation. |
||
| 2075 | */ |
||
| 2076 | public static function get_default_modules( $min_version = false, $max_version = false ) { |
||
| 2077 | $return = array(); |
||
| 2078 | |||
| 2079 | foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) { |
||
| 2080 | $module_data = Jetpack::get_module( $module ); |
||
| 2081 | |||
| 2082 | switch ( strtolower( $module_data['auto_activate'] ) ) { |
||
| 2083 | case 'yes' : |
||
| 2084 | $return[] = $module; |
||
| 2085 | break; |
||
| 2086 | case 'public' : |
||
| 2087 | if ( Jetpack_Options::get_option( 'public' ) ) { |
||
| 2088 | $return[] = $module; |
||
| 2089 | } |
||
| 2090 | break; |
||
| 2091 | case 'no' : |
||
| 2092 | default : |
||
| 2093 | break; |
||
| 2094 | } |
||
| 2095 | } |
||
| 2096 | /** |
||
| 2097 | * Filters the array of default modules. |
||
| 2098 | * |
||
| 2099 | * @since 2.5.0 |
||
| 2100 | * |
||
| 2101 | * @param array $return Array of default modules. |
||
| 2102 | * @param string $min_version Minimum version number required to use modules. |
||
| 2103 | * @param string $max_version Maximum version number required to use modules. |
||
| 2104 | */ |
||
| 2105 | return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version ); |
||
| 2106 | } |
||
| 2107 | |||
| 2108 | /** |
||
| 2109 | * Checks activated modules during auto-activation to determine |
||
| 2110 | * if any of those modules are being deprecated. If so, close |
||
| 2111 | * them out, and add any replacement modules. |
||
| 2112 | * |
||
| 2113 | * Runs at priority 99 by default. |
||
| 2114 | * |
||
| 2115 | * This is run late, so that it can still activate a module if |
||
| 2116 | * the new module is a replacement for another that the user |
||
| 2117 | * currently has active, even if something at the normal priority |
||
| 2118 | * would kibosh everything. |
||
| 2119 | * |
||
| 2120 | * @since 2.6 |
||
| 2121 | * @uses jetpack_get_default_modules filter |
||
| 2122 | * @param array $modules |
||
| 2123 | * @return array |
||
| 2124 | */ |
||
| 2125 | function handle_deprecated_modules( $modules ) { |
||
| 2126 | $deprecated_modules = array( |
||
| 2127 | 'debug' => null, // Closed out and moved to ./class.jetpack-debugger.php |
||
| 2128 | 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality. |
||
| 2129 | 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support. |
||
| 2130 | ); |
||
| 2131 | |||
| 2132 | // Don't activate SSO if they never completed activating WPCC. |
||
| 2133 | if ( Jetpack::is_module_active( 'wpcc' ) ) { |
||
| 2134 | $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' ); |
||
| 2135 | if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) { |
||
| 2136 | $deprecated_modules['wpcc'] = null; |
||
| 2137 | } |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | foreach ( $deprecated_modules as $module => $replacement ) { |
||
| 2141 | if ( Jetpack::is_module_active( $module ) ) { |
||
| 2142 | self::deactivate_module( $module ); |
||
| 2143 | if ( $replacement ) { |
||
| 2144 | $modules[] = $replacement; |
||
| 2145 | } |
||
| 2146 | } |
||
| 2147 | } |
||
| 2148 | |||
| 2149 | return array_unique( $modules ); |
||
| 2150 | } |
||
| 2151 | |||
| 2152 | /** |
||
| 2153 | * Checks activated plugins during auto-activation to determine |
||
| 2154 | * if any of those plugins are in the list with a corresponding module |
||
| 2155 | * that is not compatible with the plugin. The module will not be allowed |
||
| 2156 | * to auto-activate. |
||
| 2157 | * |
||
| 2158 | * @since 2.6 |
||
| 2159 | * @uses jetpack_get_default_modules filter |
||
| 2160 | * @param array $modules |
||
| 2161 | * @return array |
||
| 2162 | */ |
||
| 2163 | function filter_default_modules( $modules ) { |
||
| 2164 | |||
| 2165 | $active_plugins = self::get_active_plugins(); |
||
| 2166 | |||
| 2167 | if ( ! empty( $active_plugins ) ) { |
||
| 2168 | |||
| 2169 | // For each module we'd like to auto-activate... |
||
| 2170 | foreach ( $modules as $key => $module ) { |
||
| 2171 | // If there are potential conflicts for it... |
||
| 2172 | if ( ! empty( $this->conflicting_plugins[ $module ] ) ) { |
||
| 2173 | // For each potential conflict... |
||
| 2174 | foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) { |
||
| 2175 | // If that conflicting plugin is active... |
||
| 2176 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 2177 | // Remove that item from being auto-activated. |
||
| 2178 | unset( $modules[ $key ] ); |
||
| 2179 | } |
||
| 2180 | } |
||
| 2181 | } |
||
| 2182 | } |
||
| 2183 | } |
||
| 2184 | |||
| 2185 | return $modules; |
||
| 2186 | } |
||
| 2187 | |||
| 2188 | /** |
||
| 2189 | * Extract a module's slug from its full path. |
||
| 2190 | */ |
||
| 2191 | public static function get_module_slug( $file ) { |
||
| 2192 | return str_replace( '.php', '', basename( $file ) ); |
||
| 2193 | } |
||
| 2194 | |||
| 2195 | /** |
||
| 2196 | * Generate a module's path from its slug. |
||
| 2197 | */ |
||
| 2198 | public static function get_module_path( $slug ) { |
||
| 2199 | return JETPACK__PLUGIN_DIR . "modules/$slug.php"; |
||
| 2200 | } |
||
| 2201 | |||
| 2202 | /** |
||
| 2203 | * Load module data from module file. Headers differ from WordPress |
||
| 2204 | * plugin headers to avoid them being identified as standalone |
||
| 2205 | * plugins on the WordPress plugins page. |
||
| 2206 | */ |
||
| 2207 | public static function get_module( $module ) { |
||
| 2208 | $headers = array( |
||
| 2209 | 'name' => 'Module Name', |
||
| 2210 | 'description' => 'Module Description', |
||
| 2211 | 'jumpstart_desc' => 'Jumpstart Description', |
||
| 2212 | 'sort' => 'Sort Order', |
||
| 2213 | 'recommendation_order' => 'Recommendation Order', |
||
| 2214 | 'introduced' => 'First Introduced', |
||
| 2215 | 'changed' => 'Major Changes In', |
||
| 2216 | 'deactivate' => 'Deactivate', |
||
| 2217 | 'free' => 'Free', |
||
| 2218 | 'requires_connection' => 'Requires Connection', |
||
| 2219 | 'auto_activate' => 'Auto Activate', |
||
| 2220 | 'module_tags' => 'Module Tags', |
||
| 2221 | 'feature' => 'Feature', |
||
| 2222 | 'additional_search_queries' => 'Additional Search Queries', |
||
| 2223 | ); |
||
| 2224 | |||
| 2225 | $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) ); |
||
| 2226 | |||
| 2227 | $mod = Jetpack::get_file_data( $file, $headers ); |
||
| 2228 | if ( empty( $mod['name'] ) ) { |
||
| 2229 | return false; |
||
| 2230 | } |
||
| 2231 | |||
| 2232 | $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; |
||
| 2233 | $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; |
||
| 2234 | $mod['deactivate'] = empty( $mod['deactivate'] ); |
||
| 2235 | $mod['free'] = empty( $mod['free'] ); |
||
| 2236 | $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true; |
||
| 2237 | |||
| 2238 | if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) { |
||
| 2239 | $mod['auto_activate'] = 'No'; |
||
| 2240 | } else { |
||
| 2241 | $mod['auto_activate'] = (string) $mod['auto_activate']; |
||
| 2242 | } |
||
| 2243 | |||
| 2244 | if ( $mod['module_tags'] ) { |
||
| 2245 | $mod['module_tags'] = explode( ',', $mod['module_tags'] ); |
||
| 2246 | $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] ); |
||
| 2247 | $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] ); |
||
| 2248 | } else { |
||
| 2249 | $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) ); |
||
| 2250 | } |
||
| 2251 | |||
| 2252 | if ( $mod['feature'] ) { |
||
| 2253 | $mod['feature'] = explode( ',', $mod['feature'] ); |
||
| 2254 | $mod['feature'] = array_map( 'trim', $mod['feature'] ); |
||
| 2255 | } else { |
||
| 2256 | $mod['feature'] = array( self::translate_module_tag( 'Other' ) ); |
||
| 2257 | } |
||
| 2258 | |||
| 2259 | /** |
||
| 2260 | * Filters the feature array on a module. |
||
| 2261 | * |
||
| 2262 | * This filter allows you to control where each module is filtered: Recommended, |
||
| 2263 | * Jumpstart, and the default "Other" listing. |
||
| 2264 | * |
||
| 2265 | * @since 3.5.0 |
||
| 2266 | * |
||
| 2267 | * @param array $mod['feature'] The areas to feature this module: |
||
| 2268 | * 'Jumpstart' adds to the "Jumpstart" option to activate many modules at once. |
||
| 2269 | * 'Recommended' shows on the main Jetpack admin screen. |
||
| 2270 | * 'Other' should be the default if no other value is in the array. |
||
| 2271 | * @param string $module The slug of the module, e.g. sharedaddy. |
||
| 2272 | * @param array $mod All the currently assembled module data. |
||
| 2273 | */ |
||
| 2274 | $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod ); |
||
| 2275 | |||
| 2276 | /** |
||
| 2277 | * Filter the returned data about a module. |
||
| 2278 | * |
||
| 2279 | * This filter allows overriding any info about Jetpack modules. It is dangerous, |
||
| 2280 | * so please be careful. |
||
| 2281 | * |
||
| 2282 | * @since 3.6.0 |
||
| 2283 | * |
||
| 2284 | * @param array $mod The details of the requested module. |
||
| 2285 | * @param string $module The slug of the module, e.g. sharedaddy |
||
| 2286 | * @param string $file The path to the module source file. |
||
| 2287 | */ |
||
| 2288 | return apply_filters( 'jetpack_get_module', $mod, $module, $file ); |
||
| 2289 | } |
||
| 2290 | |||
| 2291 | /** |
||
| 2292 | * Like core's get_file_data implementation, but caches the result. |
||
| 2293 | */ |
||
| 2294 | public static function get_file_data( $file, $headers ) { |
||
| 2295 | //Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated |
||
| 2296 | $file_name = basename( $file ); |
||
| 2297 | $file_data_option = Jetpack_Options::get_option( 'file_data', array() ); |
||
| 2298 | $key = md5( $file_name . serialize( $headers ) ); |
||
| 2299 | $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 ); |
||
| 2300 | |||
| 2301 | // If we don't need to refresh the cache, and already have the value, short-circuit! |
||
| 2302 | if ( ! $refresh_cache && isset( $file_data_option[ JETPACK__VERSION ][ $key ] ) ) { |
||
| 2303 | return $file_data_option[ JETPACK__VERSION ][ $key ]; |
||
| 2304 | } |
||
| 2305 | |||
| 2306 | $data = get_file_data( $file, $headers ); |
||
| 2307 | |||
| 2308 | // Strip out any old Jetpack versions that are cluttering the option. |
||
| 2309 | $file_data_option = array_intersect_key( (array) $file_data_option, array( JETPACK__VERSION => null ) ); |
||
| 2310 | $file_data_option[ JETPACK__VERSION ][ $key ] = $data; |
||
| 2311 | Jetpack_Options::update_option( 'file_data', $file_data_option ); |
||
| 2312 | |||
| 2313 | return $data; |
||
| 2314 | } |
||
| 2315 | |||
| 2316 | /** |
||
| 2317 | * Return translated module tag. |
||
| 2318 | * |
||
| 2319 | * @param string $tag Tag as it appears in each module heading. |
||
| 2320 | * |
||
| 2321 | * @return mixed |
||
| 2322 | */ |
||
| 2323 | public static function translate_module_tag( $tag ) { |
||
| 2324 | return jetpack_get_module_i18n_tag( $tag ); |
||
| 2325 | } |
||
| 2326 | |||
| 2327 | /** |
||
| 2328 | * Return module name translation. Uses matching string created in modules/module-headings.php. |
||
| 2329 | * |
||
| 2330 | * @since 3.9.2 |
||
| 2331 | * |
||
| 2332 | * @param array $modules |
||
| 2333 | * |
||
| 2334 | * @return string|void |
||
| 2335 | */ |
||
| 2336 | public static function get_translated_modules( $modules ) { |
||
| 2337 | foreach ( $modules as $index => $module ) { |
||
| 2338 | $i18n_module = jetpack_get_module_i18n( $module['module'] ); |
||
| 2339 | if ( isset( $module['name'] ) ) { |
||
| 2340 | $modules[ $index ]['name'] = $i18n_module['name']; |
||
| 2341 | } |
||
| 2342 | if ( isset( $module['description'] ) ) { |
||
| 2343 | $modules[ $index ]['description'] = $i18n_module['description']; |
||
| 2344 | $modules[ $index ]['short_description'] = $i18n_module['description']; |
||
| 2345 | } |
||
| 2346 | } |
||
| 2347 | return $modules; |
||
| 2348 | } |
||
| 2349 | |||
| 2350 | /** |
||
| 2351 | * Get a list of activated modules as an array of module slugs. |
||
| 2352 | */ |
||
| 2353 | public static function get_active_modules() { |
||
| 2354 | $active = Jetpack_Options::get_option( 'active_modules' ); |
||
| 2355 | if ( ! is_array( $active ) ) |
||
| 2356 | $active = array(); |
||
| 2357 | if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { |
||
| 2358 | $active[] = 'vaultpress'; |
||
| 2359 | } else { |
||
| 2360 | $active = array_diff( $active, array( 'vaultpress' ) ); |
||
| 2361 | } |
||
| 2362 | |||
| 2363 | //If protect is active on the main site of a multisite, it should be active on all sites. |
||
| 2364 | if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { |
||
| 2365 | $active[] = 'protect'; |
||
| 2366 | } |
||
| 2367 | |||
| 2368 | return array_unique( $active ); |
||
| 2369 | } |
||
| 2370 | |||
| 2371 | /** |
||
| 2372 | * Check whether or not a Jetpack module is active. |
||
| 2373 | * |
||
| 2374 | * @param string $module The slug of a Jetpack module. |
||
| 2375 | * @return bool |
||
| 2376 | * |
||
| 2377 | * @static |
||
| 2378 | */ |
||
| 2379 | public static function is_module_active( $module ) { |
||
| 2380 | return in_array( $module, self::get_active_modules() ); |
||
| 2381 | } |
||
| 2382 | |||
| 2383 | public static function is_module( $module ) { |
||
| 2384 | return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() ); |
||
| 2385 | } |
||
| 2386 | |||
| 2387 | /** |
||
| 2388 | * Catches PHP errors. Must be used in conjunction with output buffering. |
||
| 2389 | * |
||
| 2390 | * @param bool $catch True to start catching, False to stop. |
||
| 2391 | * |
||
| 2392 | * @static |
||
| 2393 | */ |
||
| 2394 | public static function catch_errors( $catch ) { |
||
| 2395 | static $display_errors, $error_reporting; |
||
| 2396 | |||
| 2397 | if ( $catch ) { |
||
| 2398 | $display_errors = @ini_set( 'display_errors', 1 ); |
||
| 2399 | $error_reporting = @error_reporting( E_ALL ); |
||
| 2400 | add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
| 2401 | } else { |
||
| 2402 | @ini_set( 'display_errors', $display_errors ); |
||
| 2403 | @error_reporting( $error_reporting ); |
||
| 2404 | remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); |
||
| 2405 | } |
||
| 2406 | } |
||
| 2407 | |||
| 2408 | /** |
||
| 2409 | * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) |
||
| 2410 | */ |
||
| 2411 | public static function catch_errors_on_shutdown() { |
||
| 2412 | Jetpack::state( 'php_errors', ob_get_clean() ); |
||
| 2413 | } |
||
| 2414 | |||
| 2415 | public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array(), $redirect = true ) { |
||
| 2416 | $jetpack = Jetpack::init(); |
||
| 2417 | |||
| 2418 | $modules = Jetpack::get_default_modules( $min_version, $max_version ); |
||
| 2419 | $modules = array_merge( $other_modules, $modules ); |
||
| 2420 | |||
| 2421 | // Look for standalone plugins and disable if active. |
||
| 2422 | |||
| 2423 | $to_deactivate = array(); |
||
| 2424 | foreach ( $modules as $module ) { |
||
| 2425 | if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { |
||
| 2426 | $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; |
||
| 2427 | } |
||
| 2428 | } |
||
| 2429 | |||
| 2430 | $deactivated = array(); |
||
| 2431 | foreach ( $to_deactivate as $module => $deactivate_me ) { |
||
| 2432 | list( $probable_file, $probable_title ) = $deactivate_me; |
||
| 2433 | if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { |
||
| 2434 | $deactivated[] = $module; |
||
| 2435 | } |
||
| 2436 | } |
||
| 2437 | |||
| 2438 | if ( $deactivated && $redirect ) { |
||
| 2439 | Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) ); |
||
| 2440 | |||
| 2441 | $url = add_query_arg( |
||
| 2442 | array( |
||
| 2443 | 'action' => 'activate_default_modules', |
||
| 2444 | '_wpnonce' => wp_create_nonce( 'activate_default_modules' ), |
||
| 2445 | ), |
||
| 2446 | add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) |
||
| 2447 | ); |
||
| 2448 | wp_safe_redirect( $url ); |
||
| 2449 | exit; |
||
| 2450 | } |
||
| 2451 | |||
| 2452 | /** |
||
| 2453 | * Fires before default modules are activated. |
||
| 2454 | * |
||
| 2455 | * @since 1.9.0 |
||
| 2456 | * |
||
| 2457 | * @param string $min_version Minimum version number required to use modules. |
||
| 2458 | * @param string $max_version Maximum version number required to use modules. |
||
| 2459 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
| 2460 | */ |
||
| 2461 | do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
| 2462 | |||
| 2463 | // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating |
||
| 2464 | Jetpack::restate(); |
||
| 2465 | Jetpack::catch_errors( true ); |
||
| 2466 | |||
| 2467 | $active = Jetpack::get_active_modules(); |
||
| 2468 | |||
| 2469 | foreach ( $modules as $module ) { |
||
| 2470 | if ( did_action( "jetpack_module_loaded_$module" ) ) { |
||
| 2471 | $active[] = $module; |
||
| 2472 | Jetpack_Options::update_option( 'active_modules', array_unique( $active ) ); |
||
| 2473 | continue; |
||
| 2474 | } |
||
| 2475 | |||
| 2476 | if ( in_array( $module, $active ) ) { |
||
| 2477 | $module_info = Jetpack::get_module( $module ); |
||
| 2478 | if ( ! $module_info['deactivate'] ) { |
||
| 2479 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
| 2480 | View Code Duplication | if ( $active_state = Jetpack::state( $state ) ) { |
|
| 2481 | $active_state = explode( ',', $active_state ); |
||
| 2482 | } else { |
||
| 2483 | $active_state = array(); |
||
| 2484 | } |
||
| 2485 | $active_state[] = $module; |
||
| 2486 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
| 2487 | } |
||
| 2488 | continue; |
||
| 2489 | } |
||
| 2490 | |||
| 2491 | $file = Jetpack::get_module_path( $module ); |
||
| 2492 | if ( ! file_exists( $file ) ) { |
||
| 2493 | continue; |
||
| 2494 | } |
||
| 2495 | |||
| 2496 | // we'll override this later if the plugin can be included without fatal error |
||
| 2497 | if ( $redirect ) { |
||
| 2498 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 2499 | } |
||
| 2500 | Jetpack::state( 'error', 'module_activation_failed' ); |
||
| 2501 | Jetpack::state( 'module', $module ); |
||
| 2502 | ob_start(); |
||
| 2503 | require $file; |
||
| 2504 | /** |
||
| 2505 | * Fires when a specific module is activated. |
||
| 2506 | * |
||
| 2507 | * @since 1.9.0 |
||
| 2508 | * |
||
| 2509 | * @param string $module Module slug. |
||
| 2510 | */ |
||
| 2511 | do_action( 'jetpack_activate_module', $module ); |
||
| 2512 | $active[] = $module; |
||
| 2513 | $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; |
||
| 2514 | View Code Duplication | if ( $active_state = Jetpack::state( $state ) ) { |
|
| 2515 | $active_state = explode( ',', $active_state ); |
||
| 2516 | } else { |
||
| 2517 | $active_state = array(); |
||
| 2518 | } |
||
| 2519 | $active_state[] = $module; |
||
| 2520 | Jetpack::state( $state, implode( ',', $active_state ) ); |
||
| 2521 | Jetpack_Options::update_option( 'active_modules', array_unique( $active ) ); |
||
| 2522 | ob_end_clean(); |
||
| 2523 | } |
||
| 2524 | Jetpack::state( 'error', false ); |
||
| 2525 | Jetpack::state( 'module', false ); |
||
| 2526 | Jetpack::catch_errors( false ); |
||
| 2527 | /** |
||
| 2528 | * Fires when default modules are activated. |
||
| 2529 | * |
||
| 2530 | * @since 1.9.0 |
||
| 2531 | * |
||
| 2532 | * @param string $min_version Minimum version number required to use modules. |
||
| 2533 | * @param string $max_version Maximum version number required to use modules. |
||
| 2534 | * @param array $other_modules Array of other modules to activate alongside the default modules. |
||
| 2535 | */ |
||
| 2536 | do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules ); |
||
| 2537 | } |
||
| 2538 | |||
| 2539 | public static function activate_module( $module, $exit = true, $redirect = true ) { |
||
| 2540 | /** |
||
| 2541 | * Fires before a module is activated. |
||
| 2542 | * |
||
| 2543 | * @since 2.6.0 |
||
| 2544 | * |
||
| 2545 | * @param string $module Module slug. |
||
| 2546 | * @param bool $exit Should we exit after the module has been activated. Default to true. |
||
| 2547 | * @param bool $redirect Should the user be redirected after module activation? Default to true. |
||
| 2548 | */ |
||
| 2549 | do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); |
||
| 2550 | |||
| 2551 | $jetpack = Jetpack::init(); |
||
| 2552 | |||
| 2553 | if ( ! strlen( $module ) ) |
||
| 2554 | return false; |
||
| 2555 | |||
| 2556 | if ( ! Jetpack::is_module( $module ) ) |
||
| 2557 | return false; |
||
| 2558 | |||
| 2559 | // If it's already active, then don't do it again |
||
| 2560 | $active = Jetpack::get_active_modules(); |
||
| 2561 | foreach ( $active as $act ) { |
||
| 2562 | if ( $act == $module ) |
||
| 2563 | return true; |
||
| 2564 | } |
||
| 2565 | |||
| 2566 | $module_data = Jetpack::get_module( $module ); |
||
| 2567 | |||
| 2568 | if ( ! Jetpack::is_active() ) { |
||
| 2569 | if ( !Jetpack::is_development_mode() ) |
||
| 2570 | return false; |
||
| 2571 | |||
| 2572 | // If we're not connected but in development mode, make sure the module doesn't require a connection |
||
| 2573 | if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) |
||
| 2574 | return false; |
||
| 2575 | } |
||
| 2576 | |||
| 2577 | // Check and see if the old plugin is active |
||
| 2578 | if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { |
||
| 2579 | // Deactivate the old plugin |
||
| 2580 | if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { |
||
| 2581 | // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module |
||
| 2582 | // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. |
||
| 2583 | Jetpack::state( 'deactivated_plugins', $module ); |
||
| 2584 | wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); |
||
| 2585 | exit; |
||
| 2586 | } |
||
| 2587 | } |
||
| 2588 | |||
| 2589 | // Check the file for fatal errors, a la wp-admin/plugins.php::activate |
||
| 2590 | Jetpack::state( 'module', $module ); |
||
| 2591 | Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error |
||
| 2592 | |||
| 2593 | Jetpack::catch_errors( true ); |
||
| 2594 | ob_start(); |
||
| 2595 | require Jetpack::get_module_path( $module ); |
||
| 2596 | /** This action is documented in class.jetpack.php */ |
||
| 2597 | do_action( 'jetpack_activate_module', $module ); |
||
| 2598 | $active[] = $module; |
||
| 2599 | Jetpack_Options::update_option( 'active_modules', array_unique( $active ) ); |
||
| 2600 | Jetpack::state( 'error', false ); // the override |
||
| 2601 | Jetpack::state( 'message', 'module_activated' ); |
||
| 2602 | Jetpack::state( 'module', $module ); |
||
| 2603 | ob_end_clean(); |
||
| 2604 | Jetpack::catch_errors( false ); |
||
| 2605 | |||
| 2606 | // A flag for Jump Start so it's not shown again. Only set if it hasn't been yet. |
||
| 2607 | View Code Duplication | if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { |
|
| 2608 | Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); |
||
| 2609 | |||
| 2610 | //Jump start is being dismissed send data to MC Stats |
||
| 2611 | $jetpack->stat( 'jumpstart', 'manual,'.$module ); |
||
| 2612 | |||
| 2613 | $jetpack->do_stats( 'server_side' ); |
||
| 2614 | } |
||
| 2615 | |||
| 2616 | if ( $redirect ) { |
||
| 2617 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 2618 | } |
||
| 2619 | if ( $exit ) { |
||
| 2620 | exit; |
||
| 2621 | } |
||
| 2622 | return true; |
||
| 2623 | } |
||
| 2624 | |||
| 2625 | function activate_module_actions( $module ) { |
||
| 2626 | /** |
||
| 2627 | * Fires when a module is activated. |
||
| 2628 | * The dynamic part of the filter, $module, is the module slug. |
||
| 2629 | * |
||
| 2630 | * @since 1.9.0 |
||
| 2631 | * |
||
| 2632 | * @param string $module Module slug. |
||
| 2633 | */ |
||
| 2634 | do_action( "jetpack_activate_module_$module", $module ); |
||
| 2635 | } |
||
| 2636 | |||
| 2637 | public static function deactivate_module( $module ) { |
||
| 2638 | /** |
||
| 2639 | * Fires when a module is deactivated. |
||
| 2640 | * |
||
| 2641 | * @since 1.9.0 |
||
| 2642 | * |
||
| 2643 | * @param string $module Module slug. |
||
| 2644 | */ |
||
| 2645 | do_action( 'jetpack_pre_deactivate_module', $module ); |
||
| 2646 | |||
| 2647 | $jetpack = Jetpack::init(); |
||
| 2648 | |||
| 2649 | $active = Jetpack::get_active_modules(); |
||
| 2650 | $new = array_filter( array_diff( $active, (array) $module ) ); |
||
| 2651 | |||
| 2652 | /** |
||
| 2653 | * Fires when a module is deactivated. |
||
| 2654 | * The dynamic part of the filter, $module, is the module slug. |
||
| 2655 | * |
||
| 2656 | * @since 1.9.0 |
||
| 2657 | * |
||
| 2658 | * @param string $module Module slug. |
||
| 2659 | */ |
||
| 2660 | do_action( "jetpack_deactivate_module_$module", $module ); |
||
| 2661 | |||
| 2662 | // A flag for Jump Start so it's not shown again. |
||
| 2663 | View Code Duplication | if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { |
|
| 2664 | Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); |
||
| 2665 | |||
| 2666 | //Jump start is being dismissed send data to MC Stats |
||
| 2667 | $jetpack->stat( 'jumpstart', 'manual,deactivated-'.$module ); |
||
| 2668 | |||
| 2669 | $jetpack->do_stats( 'server_side' ); |
||
| 2670 | } |
||
| 2671 | |||
| 2672 | return Jetpack_Options::update_option( 'active_modules', array_unique( $new ) ); |
||
| 2673 | } |
||
| 2674 | |||
| 2675 | public static function enable_module_configurable( $module ) { |
||
| 2676 | $module = Jetpack::get_module_slug( $module ); |
||
| 2677 | add_filter( 'jetpack_module_configurable_' . $module, '__return_true' ); |
||
| 2678 | } |
||
| 2679 | |||
| 2680 | public static function module_configuration_url( $module ) { |
||
| 2681 | $module = Jetpack::get_module_slug( $module ); |
||
| 2682 | return Jetpack::admin_url( array( 'page' => 'jetpack', 'configure' => $module ) ); |
||
| 2683 | } |
||
| 2684 | |||
| 2685 | public static function module_configuration_load( $module, $method ) { |
||
| 2686 | $module = Jetpack::get_module_slug( $module ); |
||
| 2687 | add_action( 'jetpack_module_configuration_load_' . $module, $method ); |
||
| 2688 | } |
||
| 2689 | |||
| 2690 | public static function module_configuration_head( $module, $method ) { |
||
| 2691 | $module = Jetpack::get_module_slug( $module ); |
||
| 2692 | add_action( 'jetpack_module_configuration_head_' . $module, $method ); |
||
| 2693 | } |
||
| 2694 | |||
| 2695 | public static function module_configuration_screen( $module, $method ) { |
||
| 2696 | $module = Jetpack::get_module_slug( $module ); |
||
| 2697 | add_action( 'jetpack_module_configuration_screen_' . $module, $method ); |
||
| 2698 | } |
||
| 2699 | |||
| 2700 | public static function module_configuration_activation_screen( $module, $method ) { |
||
| 2701 | $module = Jetpack::get_module_slug( $module ); |
||
| 2702 | add_action( 'display_activate_module_setting_' . $module, $method ); |
||
| 2703 | } |
||
| 2704 | |||
| 2705 | /* Installation */ |
||
| 2706 | |||
| 2707 | public static function bail_on_activation( $message, $deactivate = true ) { |
||
| 2708 | ?> |
||
| 2709 | <!doctype html> |
||
| 2710 | <html> |
||
| 2711 | <head> |
||
| 2712 | <meta charset="<?php bloginfo( 'charset' ); ?>"> |
||
| 2713 | <style> |
||
| 2714 | * { |
||
| 2715 | text-align: center; |
||
| 2716 | margin: 0; |
||
| 2717 | padding: 0; |
||
| 2718 | font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; |
||
| 2719 | } |
||
| 2720 | p { |
||
| 2721 | margin-top: 1em; |
||
| 2722 | font-size: 18px; |
||
| 2723 | } |
||
| 2724 | </style> |
||
| 2725 | <body> |
||
| 2726 | <p><?php echo esc_html( $message ); ?></p> |
||
| 2727 | </body> |
||
| 2728 | </html> |
||
| 2729 | <?php |
||
| 2730 | if ( $deactivate ) { |
||
| 2731 | $plugins = get_option( 'active_plugins' ); |
||
| 2732 | $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ); |
||
| 2733 | $update = false; |
||
| 2734 | foreach ( $plugins as $i => $plugin ) { |
||
| 2735 | if ( $plugin === $jetpack ) { |
||
| 2736 | $plugins[$i] = false; |
||
| 2737 | $update = true; |
||
| 2738 | } |
||
| 2739 | } |
||
| 2740 | |||
| 2741 | if ( $update ) { |
||
| 2742 | update_option( 'active_plugins', array_filter( $plugins ) ); |
||
| 2743 | } |
||
| 2744 | } |
||
| 2745 | exit; |
||
| 2746 | } |
||
| 2747 | |||
| 2748 | /** |
||
| 2749 | * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() |
||
| 2750 | * @static |
||
| 2751 | */ |
||
| 2752 | public static function plugin_activation( $network_wide ) { |
||
| 2753 | Jetpack_Options::update_option( 'activated', 1 ); |
||
| 2754 | |||
| 2755 | if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) { |
||
| 2756 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) ); |
||
| 2757 | } |
||
| 2758 | |||
| 2759 | if ( $network_wide ) |
||
| 2760 | Jetpack::state( 'network_nag', true ); |
||
| 2761 | |||
| 2762 | Jetpack::plugin_initialize(); |
||
| 2763 | } |
||
| 2764 | /** |
||
| 2765 | * Runs before bumping version numbers up to a new version |
||
| 2766 | * @param (string) $version Version:timestamp |
||
| 2767 | * @param (string) $old_version Old Version:timestamp or false if not set yet. |
||
| 2768 | * @return null [description] |
||
| 2769 | */ |
||
| 2770 | public static function do_version_bump( $version, $old_version ) { |
||
| 2771 | |||
| 2772 | if ( ! $old_version ) { // For new sites |
||
| 2773 | // Setting up jetpack manage |
||
| 2774 | Jetpack::activate_manage(); |
||
| 2775 | } |
||
| 2776 | } |
||
| 2777 | |||
| 2778 | /** |
||
| 2779 | * Sets the internal version number and activation state. |
||
| 2780 | * @static |
||
| 2781 | */ |
||
| 2782 | public static function plugin_initialize() { |
||
| 2783 | if ( ! Jetpack_Options::get_option( 'activated' ) ) { |
||
| 2784 | Jetpack_Options::update_option( 'activated', 2 ); |
||
| 2785 | } |
||
| 2786 | |||
| 2787 | View Code Duplication | if ( ! Jetpack_Options::get_option( 'version' ) ) { |
|
| 2788 | $version = $old_version = JETPACK__VERSION . ':' . time(); |
||
| 2789 | /** This action is documented in class.jetpack.php */ |
||
| 2790 | do_action( 'updating_jetpack_version', $version, false ); |
||
| 2791 | Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); |
||
| 2792 | } |
||
| 2793 | |||
| 2794 | Jetpack::load_modules(); |
||
| 2795 | |||
| 2796 | Jetpack_Options::delete_option( 'do_activate' ); |
||
| 2797 | } |
||
| 2798 | |||
| 2799 | /** |
||
| 2800 | * Removes all connection options |
||
| 2801 | * @static |
||
| 2802 | */ |
||
| 2803 | public static function plugin_deactivation( ) { |
||
| 2804 | require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
||
| 2805 | if( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
| 2806 | Jetpack_Network::init()->deactivate(); |
||
| 2807 | } else { |
||
| 2808 | Jetpack::disconnect( false ); |
||
| 2809 | //Jetpack_Heartbeat::init()->deactivate(); |
||
| 2810 | } |
||
| 2811 | } |
||
| 2812 | |||
| 2813 | /** |
||
| 2814 | * Disconnects from the Jetpack servers. |
||
| 2815 | * Forgets all connection details and tells the Jetpack servers to do the same. |
||
| 2816 | * @static |
||
| 2817 | */ |
||
| 2818 | public static function disconnect( $update_activated_state = true ) { |
||
| 2819 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
| 2820 | Jetpack::clean_nonces( true ); |
||
| 2821 | |||
| 2822 | Jetpack::load_xml_rpc_client(); |
||
| 2823 | $xml = new Jetpack_IXR_Client(); |
||
| 2824 | $xml->query( 'jetpack.deregister' ); |
||
| 2825 | |||
| 2826 | Jetpack_Options::delete_option( |
||
| 2827 | array( |
||
| 2828 | 'register', |
||
| 2829 | 'blog_token', |
||
| 2830 | 'user_token', |
||
| 2831 | 'user_tokens', |
||
| 2832 | 'master_user', |
||
| 2833 | 'time_diff', |
||
| 2834 | 'fallback_no_verify_ssl_certs', |
||
| 2835 | ) |
||
| 2836 | ); |
||
| 2837 | |||
| 2838 | if ( $update_activated_state ) { |
||
| 2839 | Jetpack_Options::update_option( 'activated', 4 ); |
||
| 2840 | } |
||
| 2841 | |||
| 2842 | $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); |
||
| 2843 | // Check then record unique disconnection if site has never been disconnected previously |
||
| 2844 | if ( -1 == $jetpack_unique_connection['disconnected'] ) { |
||
| 2845 | $jetpack_unique_connection['disconnected'] = 1; |
||
| 2846 | } |
||
| 2847 | else { |
||
| 2848 | if ( 0 == $jetpack_unique_connection['disconnected'] ) { |
||
| 2849 | //track unique disconnect |
||
| 2850 | $jetpack = Jetpack::init(); |
||
| 2851 | |||
| 2852 | $jetpack->stat( 'connections', 'unique-disconnect' ); |
||
| 2853 | $jetpack->do_stats( 'server_side' ); |
||
| 2854 | } |
||
| 2855 | // increment number of times disconnected |
||
| 2856 | $jetpack_unique_connection['disconnected'] += 1; |
||
| 2857 | } |
||
| 2858 | |||
| 2859 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
| 2860 | |||
| 2861 | // Disable the Heartbeat cron |
||
| 2862 | Jetpack_Heartbeat::init()->deactivate(); |
||
| 2863 | } |
||
| 2864 | |||
| 2865 | /** |
||
| 2866 | * Unlinks the current user from the linked WordPress.com user |
||
| 2867 | */ |
||
| 2868 | public static function unlink_user( $user_id = null ) { |
||
| 2869 | if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) ) |
||
| 2870 | return false; |
||
| 2871 | |||
| 2872 | $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); |
||
| 2873 | |||
| 2874 | if ( Jetpack_Options::get_option( 'master_user' ) == $user_id ) |
||
| 2875 | return false; |
||
| 2876 | |||
| 2877 | if ( ! isset( $tokens[ $user_id ] ) ) |
||
| 2878 | return false; |
||
| 2879 | |||
| 2880 | Jetpack::load_xml_rpc_client(); |
||
| 2881 | $xml = new Jetpack_IXR_Client( compact( 'user_id' ) ); |
||
| 2882 | $xml->query( 'jetpack.unlink_user', $user_id ); |
||
| 2883 | |||
| 2884 | unset( $tokens[ $user_id ] ); |
||
| 2885 | |||
| 2886 | Jetpack_Options::update_option( 'user_tokens', $tokens ); |
||
| 2887 | |||
| 2888 | /** |
||
| 2889 | * Fires after the current user has been unlinked from WordPress.com. |
||
| 2890 | * |
||
| 2891 | * @since 4.1.0 |
||
| 2892 | * |
||
| 2893 | * @param int $user_id The current user's ID. |
||
| 2894 | */ |
||
| 2895 | do_action( 'jetpack_unlinked_user', $user_id ); |
||
| 2896 | |||
| 2897 | return true; |
||
| 2898 | } |
||
| 2899 | |||
| 2900 | /** |
||
| 2901 | * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() |
||
| 2902 | */ |
||
| 2903 | public static function try_registration() { |
||
| 2904 | // Let's get some testing in beta versions and such. |
||
| 2905 | if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) { |
||
| 2906 | // Before attempting to connect, let's make sure that the domains are viable. |
||
| 2907 | $domains_to_check = array_unique( array( |
||
| 2908 | 'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ), |
||
| 2909 | 'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ), |
||
| 2910 | ) ); |
||
| 2911 | foreach ( $domains_to_check as $domain ) { |
||
| 2912 | $result = Jetpack_Data::is_usable_domain( $domain ); |
||
| 2913 | if ( is_wp_error( $result ) ) { |
||
| 2914 | return $result; |
||
| 2915 | } |
||
| 2916 | } |
||
| 2917 | } |
||
| 2918 | |||
| 2919 | $result = Jetpack::register(); |
||
| 2920 | |||
| 2921 | // If there was an error with registration and the site was not registered, record this so we can show a message. |
||
| 2922 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 2923 | return $result; |
||
| 2924 | } else { |
||
| 2925 | return true; |
||
| 2926 | } |
||
| 2927 | } |
||
| 2928 | |||
| 2929 | /** |
||
| 2930 | * Tracking an internal event log. Try not to put too much chaff in here. |
||
| 2931 | * |
||
| 2932 | * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) |
||
| 2933 | */ |
||
| 2934 | public static function log( $code, $data = null ) { |
||
| 2935 | // only grab the latest 200 entries |
||
| 2936 | $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 ); |
||
| 2937 | |||
| 2938 | // Append our event to the log |
||
| 2939 | $log_entry = array( |
||
| 2940 | 'time' => time(), |
||
| 2941 | 'user_id' => get_current_user_id(), |
||
| 2942 | 'blog_id' => Jetpack_Options::get_option( 'id' ), |
||
| 2943 | 'code' => $code, |
||
| 2944 | ); |
||
| 2945 | // Don't bother storing it unless we've got some. |
||
| 2946 | if ( ! is_null( $data ) ) { |
||
| 2947 | $log_entry['data'] = $data; |
||
| 2948 | } |
||
| 2949 | $log[] = $log_entry; |
||
| 2950 | |||
| 2951 | // Try add_option first, to make sure it's not autoloaded. |
||
| 2952 | // @todo: Add an add_option method to Jetpack_Options |
||
| 2953 | if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) { |
||
| 2954 | Jetpack_Options::update_option( 'log', $log ); |
||
| 2955 | } |
||
| 2956 | |||
| 2957 | /** |
||
| 2958 | * Fires when Jetpack logs an internal event. |
||
| 2959 | * |
||
| 2960 | * @since 3.0.0 |
||
| 2961 | * |
||
| 2962 | * @param array $log_entry { |
||
| 2963 | * Array of details about the log entry. |
||
| 2964 | * |
||
| 2965 | * @param string time Time of the event. |
||
| 2966 | * @param int user_id ID of the user who trigerred the event. |
||
| 2967 | * @param int blog_id Jetpack Blog ID. |
||
| 2968 | * @param string code Unique name for the event. |
||
| 2969 | * @param string data Data about the event. |
||
| 2970 | * } |
||
| 2971 | */ |
||
| 2972 | do_action( 'jetpack_log_entry', $log_entry ); |
||
| 2973 | } |
||
| 2974 | |||
| 2975 | /** |
||
| 2976 | * Get the internal event log. |
||
| 2977 | * |
||
| 2978 | * @param $event (string) - only return the specific log events |
||
| 2979 | * @param $num (int) - get specific number of latest results, limited to 200 |
||
| 2980 | * |
||
| 2981 | * @return array of log events || WP_Error for invalid params |
||
| 2982 | */ |
||
| 2983 | public static function get_log( $event = false, $num = false ) { |
||
| 2984 | if ( $event && ! is_string( $event ) ) { |
||
| 2985 | return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) ); |
||
| 2986 | } |
||
| 2987 | |||
| 2988 | if ( $num && ! is_numeric( $num ) ) { |
||
| 2989 | return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) ); |
||
| 2990 | } |
||
| 2991 | |||
| 2992 | $entire_log = Jetpack_Options::get_option( 'log', array() ); |
||
| 2993 | |||
| 2994 | // If nothing set - act as it did before, otherwise let's start customizing the output |
||
| 2995 | if ( ! $num && ! $event ) { |
||
| 2996 | return $entire_log; |
||
| 2997 | } else { |
||
| 2998 | $entire_log = array_reverse( $entire_log ); |
||
| 2999 | } |
||
| 3000 | |||
| 3001 | $custom_log_output = array(); |
||
| 3002 | |||
| 3003 | if ( $event ) { |
||
| 3004 | foreach ( $entire_log as $log_event ) { |
||
| 3005 | if ( $event == $log_event[ 'code' ] ) { |
||
| 3006 | $custom_log_output[] = $log_event; |
||
| 3007 | } |
||
| 3008 | } |
||
| 3009 | } else { |
||
| 3010 | $custom_log_output = $entire_log; |
||
| 3011 | } |
||
| 3012 | |||
| 3013 | if ( $num ) { |
||
| 3014 | $custom_log_output = array_slice( $custom_log_output, 0, $num ); |
||
| 3015 | } |
||
| 3016 | |||
| 3017 | return $custom_log_output; |
||
| 3018 | } |
||
| 3019 | |||
| 3020 | /** |
||
| 3021 | * Log modification of important settings. |
||
| 3022 | */ |
||
| 3023 | public static function log_settings_change( $option, $old_value, $value ) { |
||
| 3024 | switch( $option ) { |
||
| 3025 | case 'jetpack_sync_non_public_post_stati': |
||
| 3026 | self::log( $option, $value ); |
||
| 3027 | break; |
||
| 3028 | } |
||
| 3029 | } |
||
| 3030 | |||
| 3031 | /** |
||
| 3032 | * Return stat data for WPCOM sync |
||
| 3033 | */ |
||
| 3034 | function get_stat_data() { |
||
| 3035 | $heartbeat_data = Jetpack_Heartbeat::generate_stats_array(); |
||
| 3036 | $additional_data = $this->get_additional_stat_data(); |
||
| 3037 | |||
| 3038 | return json_encode( array_merge( $heartbeat_data, $additional_data ) ); |
||
| 3039 | } |
||
| 3040 | |||
| 3041 | /** |
||
| 3042 | * Get additional stat data to sync to WPCOM |
||
| 3043 | */ |
||
| 3044 | function get_additional_stat_data( $prefix = '' ) { |
||
| 3045 | $return["{$prefix}themes"] = Jetpack::get_parsed_theme_data(); |
||
| 3046 | $return["{$prefix}plugins-extra"] = Jetpack::get_parsed_plugin_data(); |
||
| 3047 | $return["{$prefix}users"] = count_users(); |
||
| 3048 | $return["{$prefix}site-count"] = 0; |
||
| 3049 | if ( function_exists( 'get_blog_count' ) ) { |
||
| 3050 | $return["{$prefix}site-count"] = get_blog_count(); |
||
| 3051 | } |
||
| 3052 | return $return; |
||
| 3053 | } |
||
| 3054 | |||
| 3055 | /* Admin Pages */ |
||
| 3056 | |||
| 3057 | function admin_init() { |
||
| 3058 | // If the plugin is not connected, display a connect message. |
||
| 3059 | if ( |
||
| 3060 | // the plugin was auto-activated and needs its candy |
||
| 3061 | Jetpack_Options::get_option( 'do_activate' ) |
||
| 3062 | || |
||
| 3063 | // the plugin is active, but was never activated. Probably came from a site-wide network activation |
||
| 3064 | ! Jetpack_Options::get_option( 'activated' ) |
||
| 3065 | ) { |
||
| 3066 | Jetpack::plugin_initialize(); |
||
| 3067 | } |
||
| 3068 | |||
| 3069 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
| 3070 | if ( 4 != Jetpack_Options::get_option( 'activated' ) ) { |
||
| 3071 | // Show connect notice on dashboard and plugins pages |
||
| 3072 | add_action( 'load-index.php', array( $this, 'prepare_connect_notice' ) ); |
||
| 3073 | add_action( 'load-plugins.php', array( $this, 'prepare_connect_notice' ) ); |
||
| 3074 | } |
||
| 3075 | } elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) { |
||
| 3076 | // Upgrade: 1.1 -> 1.1.1 |
||
| 3077 | // Check and see if host can verify the Jetpack servers' SSL certificate |
||
| 3078 | $args = array(); |
||
| 3079 | Jetpack_Client::_wp_remote_request( |
||
| 3080 | Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ) ), |
||
| 3081 | $args, |
||
| 3082 | true |
||
| 3083 | ); |
||
| 3084 | } else { |
||
| 3085 | // Show the notice on the Dashboard only for now |
||
| 3086 | |||
| 3087 | add_action( 'load-index.php', array( $this, 'prepare_manage_jetpack_notice' ) ); |
||
| 3088 | |||
| 3089 | // Identity crisis notices |
||
| 3090 | add_action( 'jetpack_notices', array( $this, 'alert_identity_crisis' ) ); |
||
| 3091 | } |
||
| 3092 | |||
| 3093 | if ( current_user_can( 'manage_options' ) && 'AUTO' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) { |
||
| 3094 | add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) ); |
||
| 3095 | } |
||
| 3096 | |||
| 3097 | add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) ); |
||
| 3098 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
||
| 3099 | add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) ); |
||
| 3100 | |||
| 3101 | if ( Jetpack::is_active() || Jetpack::is_development_mode() ) { |
||
| 3102 | // Artificially throw errors in certain whitelisted cases during plugin activation |
||
| 3103 | add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) ); |
||
| 3104 | |||
| 3105 | // Kick off synchronization of user role when it changes |
||
| 3106 | add_action( 'set_user_role', array( $this, 'user_role_change' ) ); |
||
| 3107 | } |
||
| 3108 | |||
| 3109 | // Jetpack Manage Activation Screen from .com |
||
| 3110 | Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) ); |
||
| 3111 | } |
||
| 3112 | |||
| 3113 | function admin_body_class( $admin_body_class = '' ) { |
||
| 3114 | $classes = explode( ' ', trim( $admin_body_class ) ); |
||
| 3115 | |||
| 3116 | $classes[] = self::is_active() ? 'jetpack-connected' : 'jetpack-disconnected'; |
||
| 3117 | |||
| 3118 | $admin_body_class = implode( ' ', array_unique( $classes ) ); |
||
| 3119 | return " $admin_body_class "; |
||
| 3120 | } |
||
| 3121 | |||
| 3122 | static function add_jetpack_pagestyles( $admin_body_class = '' ) { |
||
| 3123 | return $admin_body_class . ' jetpack-pagestyles '; |
||
| 3124 | } |
||
| 3125 | |||
| 3126 | function prepare_connect_notice() { |
||
| 3127 | add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) ); |
||
| 3128 | |||
| 3129 | add_action( 'admin_notices', array( $this, 'admin_connect_notice' ) ); |
||
| 3130 | |||
| 3131 | if ( Jetpack::state( 'network_nag' ) ) |
||
| 3132 | add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) ); |
||
| 3133 | } |
||
| 3134 | /** |
||
| 3135 | * Call this function if you want the Big Jetpack Manage Notice to show up. |
||
| 3136 | * |
||
| 3137 | * @return null |
||
| 3138 | */ |
||
| 3139 | function prepare_manage_jetpack_notice() { |
||
| 3140 | |||
| 3141 | add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) ); |
||
| 3142 | add_action( 'admin_notices', array( $this, 'admin_jetpack_manage_notice' ) ); |
||
| 3143 | } |
||
| 3144 | |||
| 3145 | function manage_activate_screen() { |
||
| 3146 | include ( JETPACK__PLUGIN_DIR . 'modules/manage/activate-admin.php' ); |
||
| 3147 | } |
||
| 3148 | /** |
||
| 3149 | * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. |
||
| 3150 | * This function artificially throws errors for such cases (whitelisted). |
||
| 3151 | * |
||
| 3152 | * @param string $plugin The activated plugin. |
||
| 3153 | */ |
||
| 3154 | function throw_error_on_activate_plugin( $plugin ) { |
||
| 3155 | $active_modules = Jetpack::get_active_modules(); |
||
| 3156 | |||
| 3157 | // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks. |
||
| 3158 | if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) { |
||
| 3159 | $throw = false; |
||
| 3160 | |||
| 3161 | // Try and make sure it really was the stats plugin |
||
| 3162 | if ( ! class_exists( 'ReflectionFunction' ) ) { |
||
| 3163 | if ( 'stats.php' == basename( $plugin ) ) { |
||
| 3164 | $throw = true; |
||
| 3165 | } |
||
| 3166 | } else { |
||
| 3167 | $reflection = new ReflectionFunction( 'stats_get_api_key' ); |
||
| 3168 | if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) { |
||
| 3169 | $throw = true; |
||
| 3170 | } |
||
| 3171 | } |
||
| 3172 | |||
| 3173 | if ( $throw ) { |
||
| 3174 | trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR ); |
||
| 3175 | } |
||
| 3176 | } |
||
| 3177 | } |
||
| 3178 | |||
| 3179 | function intercept_plugin_error_scrape_init() { |
||
| 3180 | add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 ); |
||
| 3181 | } |
||
| 3182 | |||
| 3183 | function intercept_plugin_error_scrape( $action, $result ) { |
||
| 3184 | if ( ! $result ) { |
||
| 3185 | return; |
||
| 3186 | } |
||
| 3187 | |||
| 3188 | foreach ( $this->plugins_to_deactivate as $deactivate_me ) { |
||
| 3189 | if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) { |
||
| 3190 | Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), $deactivate_me[1] ), false ); |
||
| 3191 | } |
||
| 3192 | } |
||
| 3193 | } |
||
| 3194 | |||
| 3195 | function add_remote_request_handlers() { |
||
| 3196 | add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) ); |
||
| 3197 | } |
||
| 3198 | |||
| 3199 | function remote_request_handlers() { |
||
| 3200 | switch ( current_filter() ) { |
||
| 3201 | case 'wp_ajax_nopriv_jetpack_upload_file' : |
||
| 3202 | $response = $this->upload_handler(); |
||
| 3203 | break; |
||
| 3204 | default : |
||
| 3205 | $response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 ); |
||
| 3206 | break; |
||
| 3207 | } |
||
| 3208 | |||
| 3209 | if ( ! $response ) { |
||
| 3210 | $response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 ); |
||
| 3211 | } |
||
| 3212 | |||
| 3213 | if ( is_wp_error( $response ) ) { |
||
| 3214 | $status_code = $response->get_error_data(); |
||
| 3215 | $error = $response->get_error_code(); |
||
| 3216 | $error_description = $response->get_error_message(); |
||
| 3217 | |||
| 3218 | if ( ! is_int( $status_code ) ) { |
||
| 3219 | $status_code = 400; |
||
| 3220 | } |
||
| 3221 | |||
| 3222 | status_header( $status_code ); |
||
| 3223 | die( json_encode( (object) compact( 'error', 'error_description' ) ) ); |
||
| 3224 | } |
||
| 3225 | |||
| 3226 | status_header( 200 ); |
||
| 3227 | if ( true === $response ) { |
||
| 3228 | exit; |
||
| 3229 | } |
||
| 3230 | |||
| 3231 | die( json_encode( (object) $response ) ); |
||
| 3232 | } |
||
| 3233 | |||
| 3234 | function upload_handler() { |
||
| 3235 | if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { |
||
| 3236 | return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 ); |
||
| 3237 | } |
||
| 3238 | |||
| 3239 | $user = wp_authenticate( '', '' ); |
||
| 3240 | if ( ! $user || is_wp_error( $user ) ) { |
||
| 3241 | return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 ); |
||
| 3242 | } |
||
| 3243 | |||
| 3244 | wp_set_current_user( $user->ID ); |
||
| 3245 | |||
| 3246 | if ( ! current_user_can( 'upload_files' ) ) { |
||
| 3247 | return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 ); |
||
| 3248 | } |
||
| 3249 | |||
| 3250 | if ( empty( $_FILES ) ) { |
||
| 3251 | return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 ); |
||
| 3252 | } |
||
| 3253 | |||
| 3254 | foreach ( array_keys( $_FILES ) as $files_key ) { |
||
| 3255 | if ( ! isset( $_POST["_jetpack_file_hmac_{$files_key}"] ) ) { |
||
| 3256 | return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 ); |
||
| 3257 | } |
||
| 3258 | } |
||
| 3259 | |||
| 3260 | $media_keys = array_keys( $_FILES['media'] ); |
||
| 3261 | |||
| 3262 | $token = Jetpack_Data::get_access_token( get_current_user_id() ); |
||
| 3263 | if ( ! $token || is_wp_error( $token ) ) { |
||
| 3264 | return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 ); |
||
| 3265 | } |
||
| 3266 | |||
| 3267 | $uploaded_files = array(); |
||
| 3268 | $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; |
||
| 3269 | unset( $GLOBALS['post'] ); |
||
| 3270 | foreach ( $_FILES['media']['name'] as $index => $name ) { |
||
| 3271 | $file = array(); |
||
| 3272 | foreach ( $media_keys as $media_key ) { |
||
| 3273 | $file[$media_key] = $_FILES['media'][$media_key][$index]; |
||
| 3274 | } |
||
| 3275 | |||
| 3276 | list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][$index] ); |
||
| 3277 | |||
| 3278 | $hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret ); |
||
| 3279 | if ( $hmac_provided !== $hmac_file ) { |
||
| 3280 | $uploaded_files[$index] = (object) array( 'error' => 'invalid_hmac', 'error_description' => 'The corresponding HMAC for this file does not match' ); |
||
| 3281 | continue; |
||
| 3282 | } |
||
| 3283 | |||
| 3284 | $_FILES['.jetpack.upload.'] = $file; |
||
| 3285 | $post_id = isset( $_POST['post_id'][$index] ) ? absint( $_POST['post_id'][$index] ) : 0; |
||
| 3286 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
||
| 3287 | $post_id = 0; |
||
| 3288 | } |
||
| 3289 | $attachment_id = media_handle_upload( |
||
| 3290 | '.jetpack.upload.', |
||
| 3291 | $post_id, |
||
| 3292 | array(), |
||
| 3293 | array( |
||
| 3294 | 'action' => 'jetpack_upload_file', |
||
| 3295 | ) |
||
| 3296 | ); |
||
| 3297 | |||
| 3298 | if ( ! $attachment_id ) { |
||
| 3299 | $uploaded_files[$index] = (object) array( 'error' => 'unknown', 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site' ); |
||
| 3300 | } elseif ( is_wp_error( $attachment_id ) ) { |
||
| 3301 | $uploaded_files[$index] = (object) array( 'error' => 'attachment_' . $attachment_id->get_error_code(), 'error_description' => $attachment_id->get_error_message() ); |
||
| 3302 | } else { |
||
| 3303 | $attachment = get_post( $attachment_id ); |
||
| 3304 | $uploaded_files[$index] = (object) array( |
||
| 3305 | 'id' => (string) $attachment_id, |
||
| 3306 | 'file' => $attachment->post_title, |
||
| 3307 | 'url' => wp_get_attachment_url( $attachment_id ), |
||
| 3308 | 'type' => $attachment->post_mime_type, |
||
| 3309 | 'meta' => wp_get_attachment_metadata( $attachment_id ), |
||
| 3310 | ); |
||
| 3311 | } |
||
| 3312 | } |
||
| 3313 | if ( ! is_null( $global_post ) ) { |
||
| 3314 | $GLOBALS['post'] = $global_post; |
||
| 3315 | } |
||
| 3316 | |||
| 3317 | return $uploaded_files; |
||
| 3318 | } |
||
| 3319 | |||
| 3320 | /** |
||
| 3321 | * Add help to the Jetpack page |
||
| 3322 | * |
||
| 3323 | * @since Jetpack (1.2.3) |
||
| 3324 | * @return false if not the Jetpack page |
||
| 3325 | */ |
||
| 3326 | function admin_help() { |
||
| 3367 | |||
| 3368 | function admin_menu_css() { |
||
| 3369 | wp_enqueue_style( 'jetpack-icons' ); |
||
| 3370 | } |
||
| 3371 | |||
| 3372 | function admin_menu_order() { |
||
| 3373 | return true; |
||
| 3374 | } |
||
| 3375 | |||
| 3376 | View Code Duplication | function jetpack_menu_order( $menu_order ) { |
|
| 3377 | $jp_menu_order = array(); |
||
| 3378 | |||
| 3379 | foreach ( $menu_order as $index => $item ) { |
||
| 3380 | if ( $item != 'jetpack' ) { |
||
| 3381 | $jp_menu_order[] = $item; |
||
| 3382 | } |
||
| 3383 | |||
| 3384 | if ( $index == 0 ) { |
||
| 3385 | $jp_menu_order[] = 'jetpack'; |
||
| 3386 | } |
||
| 3387 | } |
||
| 3388 | |||
| 3389 | return $jp_menu_order; |
||
| 3390 | } |
||
| 3391 | |||
| 3392 | function admin_head() { |
||
| 3393 | View Code Duplication | if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) |
|
| 3394 | /** This action is documented in class.jetpack-admin-page.php */ |
||
| 3395 | do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] ); |
||
| 3396 | } |
||
| 3397 | |||
| 3398 | function admin_banner_styles() { |
||
| 3399 | $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
| 3400 | |||
| 3401 | wp_enqueue_style( 'jetpack', plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-20121016' ); |
||
| 3402 | wp_style_add_data( 'jetpack', 'rtl', 'replace' ); |
||
| 3403 | wp_style_add_data( 'jetpack', 'suffix', $min ); |
||
| 3404 | } |
||
| 3405 | |||
| 3406 | function admin_scripts() { |
||
| 3407 | wp_enqueue_script( 'jetpack-js', plugins_url( '_inc/jp.js', JETPACK__PLUGIN_FILE ), array( 'jquery', 'wp-util' ), JETPACK__VERSION . '-20121111' ); |
||
| 3408 | wp_localize_script( |
||
| 3409 | 'jetpack-js', |
||
| 3410 | 'jetpackL10n', |
||
| 3411 | array( |
||
| 3412 | 'ays_disconnect' => "This will deactivate all Jetpack modules.\nAre you sure you want to disconnect?", |
||
| 3413 | 'ays_unlink' => "This will prevent user-specific modules such as Publicize, Notifications and Post By Email from working.\nAre you sure you want to unlink?", |
||
| 3414 | 'ays_dismiss' => "This will deactivate Jetpack.\nAre you sure you want to deactivate Jetpack?", |
||
| 3415 | ) |
||
| 3416 | ); |
||
| 3417 | add_action( 'admin_footer', array( $this, 'do_stats' ) ); |
||
| 3418 | } |
||
| 3419 | |||
| 3420 | function plugin_action_links( $actions ) { |
||
| 3421 | |||
| 3422 | $jetpack_home = array( 'jetpack-home' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack' ), __( 'Jetpack', 'jetpack' ) ) ); |
||
| 3423 | |||
| 3424 | if( current_user_can( 'jetpack_manage_modules' ) && ( Jetpack::is_active() || Jetpack::is_development_mode() ) ) { |
||
| 3425 | return array_merge( |
||
| 3426 | $jetpack_home, |
||
| 3427 | array( 'settings' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack_modules' ), __( 'Settings', 'jetpack' ) ) ), |
||
| 3428 | array( 'support' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack-debugger '), __( 'Support', 'jetpack' ) ) ), |
||
| 3429 | $actions |
||
| 3430 | ); |
||
| 3431 | } |
||
| 3432 | |||
| 3433 | return array_merge( $jetpack_home, $actions ); |
||
| 3434 | } |
||
| 3435 | |||
| 3436 | function admin_connect_notice() { |
||
| 3437 | // Don't show the connect notice anywhere but the plugins.php after activating |
||
| 3438 | $current = get_current_screen(); |
||
| 3439 | if ( 'plugins' !== $current->parent_base ) |
||
| 3440 | return; |
||
| 3441 | |||
| 3442 | if ( ! current_user_can( 'jetpack_connect' ) ) |
||
| 3443 | return; |
||
| 3444 | |||
| 3445 | $dismiss_and_deactivate_url = wp_nonce_url( Jetpack::admin_url( '?page=jetpack&jetpack-notice=dismiss' ), 'jetpack-deactivate' ); |
||
| 3446 | ?> |
||
| 3447 | <div id="message" class="updated jp-banner"> |
||
| 3448 | <a href="<?php echo esc_url( $dismiss_and_deactivate_url ); ?>" class="notice-dismiss" title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>"></a> |
||
| 3449 | <?php if ( in_array( Jetpack_Options::get_option( 'activated' ) , array( 1, 2, 3 ) ) ) : ?> |
||
| 3450 | <div class="jp-banner__description-container"> |
||
| 3451 | <h2 class="jp-banner__header"><?php _e( 'Your Jetpack is almost ready!', 'jetpack' ); ?></h2> |
||
| 3452 | <p class="jp-banner__description"><?php _e( 'Please connect to or create a WordPress.com account to enable Jetpack, including powerful security, traffic, and customization services.', 'jetpack' ); ?></p> |
||
| 3453 | <p class="jp-banner__button-container"> |
||
| 3454 | <a href="<?php echo $this->build_connect_url( false, false, 'banner' ) ?>" class="button button-primary" id="wpcom-connect"><?php _e( 'Connect to WordPress.com', 'jetpack' ); ?></a> |
||
| 3455 | <a href="<?php echo Jetpack::admin_url( 'admin.php?page=jetpack' ) ?>" class="button" title="<?php esc_attr_e( 'Learn about the benefits you receive when you connect Jetpack to WordPress.com', 'jetpack' ); ?>"><?php _e( 'Learn more', 'jetpack' ); ?></a> |
||
| 3456 | </p> |
||
| 3457 | </div> |
||
| 3458 | <?php else : ?> |
||
| 3459 | <div class="jp-banner__content"> |
||
| 3460 | <h2><?php _e( 'Jetpack is installed!', 'jetpack' ) ?></h2> |
||
| 3461 | <p><?php _e( 'It\'s ready to bring awesome, WordPress.com cloud-powered features to your site.', 'jetpack' ) ?></p> |
||
| 3462 | </div> |
||
| 3463 | <div class="jp-banner__action-container"> |
||
| 3464 | <a href="<?php echo Jetpack::admin_url() ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Learn More', 'jetpack' ); ?></a> |
||
| 3465 | </div> |
||
| 3466 | <?php endif; ?> |
||
| 3467 | </div> |
||
| 3468 | |||
| 3469 | <?php |
||
| 3470 | } |
||
| 3471 | |||
| 3472 | /** |
||
| 3473 | * This is the first banner |
||
| 3474 | * It should be visible only to user that can update the option |
||
| 3475 | * Are not connected |
||
| 3476 | * |
||
| 3477 | * @return null |
||
| 3478 | */ |
||
| 3479 | function admin_jetpack_manage_notice() { |
||
| 3480 | $screen = get_current_screen(); |
||
| 3481 | |||
| 3482 | // Don't show the connect notice on the jetpack settings page. |
||
| 3483 | if ( ! in_array( $screen->base, array( 'dashboard' ) ) || $screen->is_network || $screen->action ) |
||
| 3484 | return; |
||
| 3485 | |||
| 3486 | // Only show it if don't have the managment option set. |
||
| 3487 | // And not dismissed it already. |
||
| 3488 | if ( ! $this->can_display_jetpack_manage_notice() || Jetpack_Options::get_option( 'dismissed_manage_banner' ) ) { |
||
| 3489 | return; |
||
| 3490 | } |
||
| 3491 | |||
| 3492 | $opt_out_url = $this->opt_out_jetpack_manage_url(); |
||
| 3493 | $opt_in_url = $this->opt_in_jetpack_manage_url(); |
||
| 3494 | /** |
||
| 3495 | * I think it would be great to have different wordsing depending on where you are |
||
| 3496 | * for example if we show the notice on dashboard and a different one if we show it on Plugins screen |
||
| 3497 | * etc.. |
||
| 3498 | */ |
||
| 3499 | |||
| 3500 | ?> |
||
| 3501 | <div id="message" class="updated jp-banner"> |
||
| 3502 | <a href="<?php echo esc_url( $opt_out_url ); ?>" class="notice-dismiss" title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>"></a> |
||
| 3503 | <div class="jp-banner__description-container"> |
||
| 3504 | <h2 class="jp-banner__header"><?php esc_html_e( 'Jetpack Centralized Site Management', 'jetpack' ); ?></h2> |
||
| 3505 | <p class="jp-banner__description"><?php printf( __( 'Manage multiple Jetpack enabled sites from one single dashboard at wordpress.com. Allows all existing, connected Administrators to modify your site.', 'jetpack' ), 'https://jetpack.com/support/site-management' ); ?></p> |
||
| 3506 | <p class="jp-banner__button-container"> |
||
| 3507 | <a href="<?php echo esc_url( $opt_in_url ); ?>" class="button button-primary" id="wpcom-connect"><?php _e( 'Activate Jetpack Manage', 'jetpack' ); ?></a> |
||
| 3508 | <a href="https://jetpack.com/support/site-management" class="button" target="_blank" title="<?php esc_attr_e( 'Learn more about Jetpack Manage on Jetpack.com', 'jetpack' ); ?>"><?php _e( 'Learn more', 'jetpack' ); ?></a> |
||
| 3509 | </p> |
||
| 3510 | </div> |
||
| 3511 | </div> |
||
| 3512 | <?php |
||
| 3513 | } |
||
| 3514 | |||
| 3515 | /** |
||
| 3516 | * Returns the url that the user clicks to remove the notice for the big banner |
||
| 3517 | * @return (string) |
||
| 3518 | */ |
||
| 3519 | function opt_out_jetpack_manage_url() { |
||
| 3523 | /** |
||
| 3524 | * Returns the url that the user clicks to opt in to Jetpack Manage |
||
| 3525 | * @return (string) |
||
| 3526 | */ |
||
| 3527 | function opt_in_jetpack_manage_url() { |
||
| 3530 | |||
| 3531 | function opt_in_jetpack_manage_notice() { |
||
| 3541 | /** |
||
| 3542 | * Determines whether to show the notice of not true = display notice |
||
| 3543 | * @return (bool) |
||
| 3544 | */ |
||
| 3545 | function can_display_jetpack_manage_notice() { |
||
| 3567 | |||
| 3568 | function network_connect_notice() { |
||
| 3577 | |||
| 3578 | public static function jetpack_comment_notice() { |
||
| 3613 | |||
| 3614 | /* |
||
| 3615 | * Registration flow: |
||
| 3616 | * 1 - ::admin_page_load() action=register |
||
| 3617 | * 2 - ::try_registration() |
||
| 3618 | * 3 - ::register() |
||
| 3619 | * - Creates jetpack_register option containing two secrets and a timestamp |
||
| 3620 | * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with |
||
| 3621 | * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id |
||
| 3622 | * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's |
||
| 3623 | * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 |
||
| 3624 | * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 |
||
| 3625 | * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with |
||
| 3626 | * jetpack_id, jetpack_secret, jetpack_public |
||
| 3627 | * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret |
||
| 3628 | * 4 - redirect to https://wordpress.com/start/jetpack-connect |
||
| 3629 | * 5 - user logs in with WP.com account |
||
| 3630 | * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize |
||
| 3631 | * - Jetpack_Client_Server::authorize() |
||
| 3632 | * - Jetpack_Client_Server::get_token() |
||
| 3633 | * - GET https://jetpack.wordpress.com/jetpack.token/1/ with |
||
| 3634 | * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login |
||
| 3635 | * - which responds with access_token, token_type, scope |
||
| 3636 | * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id |
||
| 3637 | * - Jetpack::activate_default_modules() |
||
| 3638 | * - Deactivates deprecated plugins |
||
| 3639 | * - Activates all default modules |
||
| 3640 | * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users |
||
| 3641 | * 7 - For a new connection, user selects a Jetpack plan on wordpress.com |
||
| 3642 | * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized |
||
| 3643 | * Done! |
||
| 3644 | */ |
||
| 3645 | |||
| 3646 | /** |
||
| 3647 | * Handles the page load events for the Jetpack admin page |
||
| 3648 | */ |
||
| 3649 | function admin_page_load() { |
||
| 4102 | |||
| 4103 | function admin_notices() { |
||
| 4200 | |||
| 4201 | /** |
||
| 4202 | * Record a stat for later output. This will only currently output in the admin_footer. |
||
| 4203 | */ |
||
| 4204 | function stat( $group, $detail ) { |
||
| 4209 | |||
| 4210 | /** |
||
| 4211 | * Load stats pixels. $group is auto-prefixed with "x_jetpack-" |
||
| 4212 | */ |
||
| 4213 | function do_stats( $method = '' ) { |
||
| 4228 | |||
| 4229 | /** |
||
| 4230 | * Runs stats code for a one-off, server-side. |
||
| 4231 | * |
||
| 4232 | * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. |
||
| 4233 | * |
||
| 4234 | * @return bool If it worked. |
||
| 4235 | */ |
||
| 4236 | static function do_server_side_stat( $args ) { |
||
| 4246 | |||
| 4247 | /** |
||
| 4248 | * Builds the stats url. |
||
| 4249 | * |
||
| 4250 | * @param $args array|string The arguments to append to the URL. |
||
| 4251 | * |
||
| 4252 | * @return string The URL to be pinged. |
||
| 4253 | */ |
||
| 4254 | static function build_stats_url( $args ) { |
||
| 4274 | |||
| 4275 | function translate_current_user_to_role() { |
||
| 4284 | |||
| 4285 | function translate_role_to_cap( $role ) { |
||
| 4292 | |||
| 4293 | function sign_role( $role ) { |
||
| 4305 | |||
| 4306 | |||
| 4307 | /** |
||
| 4308 | * Builds a URL to the Jetpack connection auth page |
||
| 4309 | * |
||
| 4310 | * @since 3.9.5 |
||
| 4311 | * |
||
| 4312 | * @param bool $raw If true, URL will not be escaped. |
||
| 4313 | * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection. |
||
| 4314 | * If string, will be a custom redirect. |
||
| 4315 | * @param bool|string $from If not false, adds 'from=$from' param to the connect URL. |
||
| 4316 | * |
||
| 4317 | * @return string Connect URL |
||
| 4318 | */ |
||
| 4319 | function build_connect_url( $raw = false, $redirect = false, $from = false ) { |
||
| 4320 | if ( ! Jetpack_Options::get_option( 'blog_token' ) || ! Jetpack_Options::get_option( 'id' ) ) { |
||
| 4321 | $url = Jetpack::nonce_url_no_esc( Jetpack::admin_url( 'action=register' ), 'jetpack-register' ); |
||
| 4322 | if( is_network_admin() ) { |
||
| 4323 | $url = add_query_arg( 'is_multisite', network_admin_url( |
||
| 4324 | 'admin.php?page=jetpack-settings' ), $url ); |
||
| 4325 | } |
||
| 4326 | } else { |
||
| 4327 | require_once JETPACK__GLOTPRESS_LOCALES_PATH; |
||
| 4328 | $role = $this->translate_current_user_to_role(); |
||
| 4329 | $signed_role = $this->sign_role( $role ); |
||
| 4330 | |||
| 4331 | $user = wp_get_current_user(); |
||
| 4332 | |||
| 4333 | $jetpack_admin_page = esc_url_raw( admin_url( 'admin.php?page=jetpack' ) ); |
||
| 4334 | $redirect = $redirect |
||
| 4335 | ? wp_validate_redirect( esc_url_raw( $redirect ), $jetpack_admin_page ) |
||
| 4336 | : $jetpack_admin_page; |
||
| 4337 | |||
| 4338 | $gp_locale = GP_Locales::by_field( 'wp_locale', get_locale() ); |
||
| 4339 | |||
| 4340 | if( isset( $_REQUEST['is_multisite'] ) ) { |
||
| 4341 | $redirect = Jetpack_Network::init()->get_url( 'network_admin_page' ); |
||
| 4342 | } |
||
| 4343 | |||
| 4344 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 4345 | @list( $secret ) = explode( ':', $secrets ); |
||
| 4346 | |||
| 4347 | $args = urlencode_deep( |
||
| 4348 | array( |
||
| 4349 | 'response_type' => 'code', |
||
| 4350 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
| 4351 | 'redirect_uri' => add_query_arg( |
||
| 4352 | array( |
||
| 4353 | 'action' => 'authorize', |
||
| 4354 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
| 4355 | 'redirect' => urlencode( $redirect ), |
||
| 4356 | ), |
||
| 4357 | esc_url( admin_url( 'admin.php?page=jetpack' ) ) |
||
| 4358 | ), |
||
| 4359 | 'state' => $user->ID, |
||
| 4360 | 'scope' => $signed_role, |
||
| 4361 | 'user_email' => $user->user_email, |
||
| 4362 | 'user_login' => $user->user_login, |
||
| 4363 | 'is_active' => Jetpack::is_active(), |
||
| 4364 | 'jp_version' => JETPACK__VERSION, |
||
| 4365 | 'auth_type' => 'calypso', |
||
| 4366 | 'secret' => $secret, |
||
| 4367 | 'locale' => isset( $gp_locale->slug ) ? $gp_locale->slug : '', |
||
| 4368 | 'blogname' => get_option( 'blogname' ), |
||
| 4369 | ) |
||
| 4370 | ); |
||
| 4371 | |||
| 4372 | $url = add_query_arg( $args, Jetpack::api_url( 'authorize' ) ); |
||
| 4373 | } |
||
| 4374 | |||
| 4375 | if ( $from ) { |
||
| 4376 | $url = add_query_arg( 'from', $from, $url ); |
||
| 4377 | } |
||
| 4378 | |||
| 4379 | if ( isset( $_GET['calypso_env'] ) ) { |
||
| 4380 | $url = add_query_arg( 'calypso_env', sanitize_key( $_GET['calypso_env'] ), $url ); |
||
| 4381 | } |
||
| 4382 | |||
| 4383 | return $raw ? $url : esc_url( $url ); |
||
| 4384 | } |
||
| 4385 | |||
| 4386 | function build_reconnect_url( $raw = false ) { |
||
| 4390 | |||
| 4391 | public static function admin_url( $args = null ) { |
||
| 4396 | |||
| 4397 | public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { |
||
| 4401 | |||
| 4402 | function dismiss_jetpack_notice() { |
||
| 4476 | |||
| 4477 | function debugger_page() { |
||
| 4485 | |||
| 4486 | public static function admin_screen_configure_module( $module_id ) { |
||
| 4538 | |||
| 4539 | /** |
||
| 4540 | * Display link to activate the module to see the settings screen. |
||
| 4541 | * @param string $module_id |
||
| 4542 | * @return null |
||
| 4543 | */ |
||
| 4544 | public static function display_activate_module_link( $module_id ) { |
||
| 4596 | |||
| 4597 | public static function sort_modules( $a, $b ) { |
||
| 4603 | |||
| 4604 | View Code Duplication | function sync_reindex_trigger() { |
|
| 4612 | |||
| 4613 | View Code Duplication | function sync_reindex_status(){ |
|
| 4621 | |||
| 4622 | /* Client API */ |
||
| 4623 | |||
| 4624 | /** |
||
| 4625 | * Returns the requested Jetpack API URL |
||
| 4626 | * |
||
| 4627 | * @return string |
||
| 4628 | */ |
||
| 4629 | public static function api_url( $relative_url ) { |
||
| 4632 | |||
| 4633 | /** |
||
| 4634 | * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets |
||
| 4635 | */ |
||
| 4636 | public static function fix_url_for_bad_hosts( $url ) { |
||
| 4652 | |||
| 4653 | /** |
||
| 4654 | * Checks to see if the URL is using SSL to connect with Jetpack |
||
| 4655 | * |
||
| 4656 | * @since 2.3.3 |
||
| 4657 | * @return boolean |
||
| 4658 | */ |
||
| 4659 | public static function permit_ssl( $force_recheck = false ) { |
||
| 4701 | |||
| 4702 | /* |
||
| 4703 | * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working. |
||
| 4704 | */ |
||
| 4705 | public function alert_auto_ssl_fail() { |
||
| 4751 | |||
| 4752 | /** |
||
| 4753 | * Returns the Jetpack XML-RPC API |
||
| 4754 | * |
||
| 4755 | * @return string |
||
| 4756 | */ |
||
| 4757 | public static function xmlrpc_api_url() { |
||
| 4761 | |||
| 4762 | /** |
||
| 4763 | * Creates two secret tokens and the end of life timestamp for them. |
||
| 4764 | * |
||
| 4765 | * Note these tokens are unique per call, NOT static per site for connecting. |
||
| 4766 | * |
||
| 4767 | * @since 2.6 |
||
| 4768 | * @return array |
||
| 4769 | */ |
||
| 4770 | public function generate_secrets( $action, $exp = 600 ) { |
||
| 4771 | $secret = wp_generate_password( 32, false ) // secret_1 |
||
| 4772 | . ':' . wp_generate_password( 32, false ) // secret_2 |
||
| 4773 | . ':' . ( time() + $exp ) // eol ( End of Life ) |
||
| 4774 | . ':' . get_current_user_id(); // ties the secrets to the current user |
||
| 4775 | Jetpack_Options::update_option( $action, $secret ); |
||
| 4776 | return Jetpack_Options::get_option( $action ); |
||
| 4777 | } |
||
| 4778 | |||
| 4779 | /** |
||
| 4780 | * Builds the timeout limit for queries talking with the wpcom servers. |
||
| 4781 | * |
||
| 4782 | * Based on local php max_execution_time in php.ini |
||
| 4783 | * |
||
| 4784 | * @since 2.6 |
||
| 4785 | * @return int |
||
| 4786 | **/ |
||
| 4787 | public function get_remote_query_timeout_limit() { |
||
| 4793 | |||
| 4794 | |||
| 4795 | /** |
||
| 4796 | * Takes the response from the Jetpack register new site endpoint and |
||
| 4797 | * verifies it worked properly. |
||
| 4798 | * |
||
| 4799 | * @since 2.6 |
||
| 4800 | * @return true or Jetpack_Error |
||
| 4801 | **/ |
||
| 4802 | public function validate_remote_register_response( $response ) { |
||
| 4837 | /** |
||
| 4838 | * @return bool|WP_Error |
||
| 4839 | */ |
||
| 4840 | public static function register() { |
||
| 4841 | add_action( 'pre_update_jetpack_option_register', array( 'Jetpack_Options', 'delete_option' ) ); |
||
| 4842 | $secrets = Jetpack::init()->generate_secrets( 'register' ); |
||
| 4843 | |||
| 4844 | @list( $secret_1, $secret_2, $secret_eol ) = explode( ':', $secrets ); |
||
| 4845 | View Code Duplication | if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) { |
|
| 4846 | return new Jetpack_Error( 'missing_secrets' ); |
||
| 4847 | } |
||
| 4848 | |||
| 4849 | $timeout = Jetpack::init()->get_remote_query_timeout_limit(); |
||
| 4850 | |||
| 4851 | $gmt_offset = get_option( 'gmt_offset' ); |
||
| 4852 | if ( ! $gmt_offset ) { |
||
| 4853 | $gmt_offset = 0; |
||
| 4854 | } |
||
| 4855 | |||
| 4856 | $stats_options = get_option( 'stats_options' ); |
||
| 4857 | $stats_id = isset($stats_options['blog_id']) ? $stats_options['blog_id'] : null; |
||
| 4858 | |||
| 4859 | $args = array( |
||
| 4860 | 'method' => 'POST', |
||
| 4861 | 'body' => array( |
||
| 4862 | 'siteurl' => site_url(), |
||
| 4863 | 'home' => home_url(), |
||
| 4864 | 'gmt_offset' => $gmt_offset, |
||
| 4865 | 'timezone_string' => (string) get_option( 'timezone_string' ), |
||
| 4866 | 'site_name' => (string) get_option( 'blogname' ), |
||
| 4867 | 'secret_1' => $secret_1, |
||
| 4868 | 'secret_2' => $secret_2, |
||
| 4869 | 'site_lang' => get_locale(), |
||
| 4870 | 'timeout' => $timeout, |
||
| 4871 | 'stats_id' => $stats_id, |
||
| 4872 | 'state' => get_current_user_id(), |
||
| 4873 | ), |
||
| 4874 | 'headers' => array( |
||
| 4875 | 'Accept' => 'application/json', |
||
| 4876 | ), |
||
| 4877 | 'timeout' => $timeout, |
||
| 4878 | ); |
||
| 4879 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'register' ) ), $args, true ); |
||
| 4880 | |||
| 4881 | |||
| 4882 | // Make sure the response is valid and does not contain any Jetpack errors |
||
| 4883 | $valid_response = Jetpack::init()->validate_remote_register_response( $response ); |
||
| 4884 | if( is_wp_error( $valid_response ) || !$valid_response ) { |
||
| 4885 | return $valid_response; |
||
| 4886 | } |
||
| 4887 | |||
| 4888 | // Grab the response values to work with |
||
| 4889 | $code = wp_remote_retrieve_response_code( $response ); |
||
| 4890 | $entity = wp_remote_retrieve_body( $response ); |
||
| 4891 | |||
| 4892 | if ( $entity ) |
||
| 4893 | $json = json_decode( $entity ); |
||
| 4894 | else |
||
| 4895 | $json = false; |
||
| 4896 | |||
| 4897 | View Code Duplication | if ( empty( $json->jetpack_secret ) || ! is_string( $json->jetpack_secret ) ) |
|
| 4898 | return new Jetpack_Error( 'jetpack_secret', '', $code ); |
||
| 4899 | |||
| 4900 | if ( isset( $json->jetpack_public ) ) { |
||
| 4901 | $jetpack_public = (int) $json->jetpack_public; |
||
| 4902 | } else { |
||
| 4903 | $jetpack_public = false; |
||
| 4904 | } |
||
| 4905 | |||
| 4906 | Jetpack_Options::update_options( |
||
| 4907 | array( |
||
| 4908 | 'id' => (int) $json->jetpack_id, |
||
| 4909 | 'blog_token' => (string) $json->jetpack_secret, |
||
| 4910 | 'public' => $jetpack_public, |
||
| 4911 | ) |
||
| 4912 | ); |
||
| 4913 | |||
| 4914 | /** |
||
| 4915 | * Fires when a site is registered on WordPress.com. |
||
| 4916 | * |
||
| 4917 | * @since 3.7.0 |
||
| 4918 | * |
||
| 4919 | * @param int $json->jetpack_id Jetpack Blog ID. |
||
| 4920 | * @param string $json->jetpack_secret Jetpack Blog Token. |
||
| 4921 | * @param int|bool $jetpack_public Is the site public. |
||
| 4922 | */ |
||
| 4923 | do_action( 'jetpack_site_registered', $json->jetpack_id, $json->jetpack_secret, $jetpack_public ); |
||
| 4924 | |||
| 4925 | // Initialize Jump Start for the first and only time. |
||
| 4926 | if ( ! Jetpack_Options::get_option( 'jumpstart' ) ) { |
||
| 4927 | Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); |
||
| 4928 | |||
| 4929 | $jetpack = Jetpack::init(); |
||
| 4930 | |||
| 4931 | $jetpack->stat( 'jumpstart', 'unique-views' ); |
||
| 4932 | $jetpack->do_stats( 'server_side' ); |
||
| 4933 | }; |
||
| 4934 | |||
| 4935 | return true; |
||
| 4936 | } |
||
| 4937 | |||
| 4938 | /** |
||
| 4939 | * If the db version is showing something other that what we've got now, bump it to current. |
||
| 4940 | * |
||
| 4941 | * @return bool: True if the option was incorrect and updated, false if nothing happened. |
||
| 4942 | */ |
||
| 4943 | public static function maybe_set_version_option() { |
||
| 4951 | |||
| 4952 | /* Client Server API */ |
||
| 4953 | |||
| 4954 | /** |
||
| 4955 | * Loads the Jetpack XML-RPC client |
||
| 4956 | */ |
||
| 4957 | public static function load_xml_rpc_client() { |
||
| 4961 | |||
| 4962 | function verify_xml_rpc_signature() { |
||
| 5060 | |||
| 5061 | /** |
||
| 5062 | * Authenticates XML-RPC and other requests from the Jetpack Server |
||
| 5063 | */ |
||
| 5064 | function authenticate_jetpack( $user, $username, $password ) { |
||
| 5087 | |||
| 5088 | function add_nonce( $timestamp, $nonce ) { |
||
| 5126 | |||
| 5127 | /** |
||
| 5128 | * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. |
||
| 5129 | * Capture it here so we can verify the signature later. |
||
| 5130 | */ |
||
| 5131 | function xmlrpc_methods( $methods ) { |
||
| 5135 | |||
| 5136 | function public_xmlrpc_methods( $methods ) { |
||
| 5142 | |||
| 5143 | function jetpack_getOptions( $args ) { |
||
| 5183 | |||
| 5184 | function xmlrpc_options( $options ) { |
||
| 5202 | |||
| 5203 | public static function clean_nonces( $all = false ) { |
||
| 5228 | |||
| 5229 | /** |
||
| 5230 | * State is passed via cookies from one request to the next, but never to subsequent requests. |
||
| 5231 | * SET: state( $key, $value ); |
||
| 5232 | * GET: $value = state( $key ); |
||
| 5233 | * |
||
| 5234 | * @param string $key |
||
| 5235 | * @param string $value |
||
| 5236 | * @param bool $restate private |
||
| 5237 | */ |
||
| 5238 | public static function state( $key = null, $value = null, $restate = false ) { |
||
| 5288 | |||
| 5289 | public static function restate() { |
||
| 5292 | |||
| 5293 | public static function check_privacy( $file ) { |
||
| 5328 | |||
| 5329 | /** |
||
| 5330 | * Helper method for multicall XMLRPC. |
||
| 5331 | */ |
||
| 5332 | public static function xmlrpc_async_call() { |
||
| 5374 | |||
| 5375 | public static function staticize_subdomain( $url ) { |
||
| 5410 | |||
| 5411 | /* JSON API Authorization */ |
||
| 5412 | |||
| 5413 | /** |
||
| 5414 | * Handles the login action for Authorizing the JSON API |
||
| 5415 | */ |
||
| 5416 | function login_form_json_api_authorization() { |
||
| 5425 | |||
| 5426 | // Make sure the login form is POSTed to the signed URL so we can reverify the request |
||
| 5427 | function post_login_form_to_signed_url( $url, $path, $scheme ) { |
||
| 5440 | |||
| 5441 | // Make sure the POSTed request is handled by the same action |
||
| 5442 | function preserve_action_in_login_form_for_json_api_authorization() { |
||
| 5446 | |||
| 5447 | // If someone logs in to approve API access, store the Access Code in usermeta |
||
| 5448 | function store_json_api_authorization_token( $user_login, $user ) { |
||
| 5454 | |||
| 5455 | // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access |
||
| 5456 | function allow_wpcom_public_api_domain( $domains ) { |
||
| 5460 | |||
| 5461 | // Add the Access Code details to the public-api.wordpress.com redirect |
||
| 5462 | function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { |
||
| 5474 | |||
| 5475 | // Verifies the request by checking the signature |
||
| 5476 | function verify_json_api_authorization_request() { |
||
| 5555 | |||
| 5556 | function login_message_json_api_authorization( $message ) { |
||
| 5562 | |||
| 5563 | /** |
||
| 5564 | * Get $content_width, but with a <s>twist</s> filter. |
||
| 5565 | */ |
||
| 5566 | public static function get_content_width() { |
||
| 5577 | |||
| 5578 | /** |
||
| 5579 | * Centralize the function here until it gets added to core. |
||
| 5580 | * |
||
| 5581 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
||
| 5582 | * @param int $size Size of the avatar image |
||
| 5583 | * @param string $default URL to a default image to use if no avatar is available |
||
| 5584 | * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled |
||
| 5585 | * |
||
| 5586 | * @return array First element is the URL, second is the class. |
||
| 5587 | */ |
||
| 5588 | public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { |
||
| 5616 | |||
| 5617 | /** |
||
| 5618 | * Pings the WordPress.com Mirror Site for the specified options. |
||
| 5619 | * |
||
| 5620 | * @param string|array $option_names The option names to request from the WordPress.com Mirror Site |
||
| 5621 | * |
||
| 5622 | * @return array An associative array of the option values as stored in the WordPress.com Mirror Site |
||
| 5623 | */ |
||
| 5624 | public function get_cloud_site_options( $option_names ) { |
||
| 5640 | |||
| 5641 | /** |
||
| 5642 | * Fetch the filtered array of options that we should compare to determine an identity crisis. |
||
| 5643 | * |
||
| 5644 | * @return array An array of options to check. |
||
| 5645 | */ |
||
| 5646 | public static function identity_crisis_options_to_check() { |
||
| 5652 | |||
| 5653 | /** |
||
| 5654 | * Checks to make sure that local options have the same values as remote options. Will cache the results for up to 24 hours. |
||
| 5655 | * |
||
| 5656 | * @param bool $force_recheck Whether to ignore any cached transient and manually re-check. |
||
| 5657 | * |
||
| 5658 | * @return array An array of options that do not match. If everything is good, it will evaluate to false. |
||
| 5659 | */ |
||
| 5660 | public static function check_identity_crisis( $force_recheck = false ) { |
||
| 5726 | |||
| 5727 | /* |
||
| 5728 | * Resolve ID crisis |
||
| 5729 | * |
||
| 5730 | * If the URL has changed, but the rest of the options are the same (i.e. blog/user tokens) |
||
| 5731 | * The user has the option to update the shadow site with the new URL before a new |
||
| 5732 | * token is created. |
||
| 5733 | * |
||
| 5734 | * @param $key : Which option to sync. null defautlts to home and siteurl |
||
| 5735 | */ |
||
| 5736 | public static function resolve_identity_crisis( $key = null ) { |
||
| 5756 | |||
| 5757 | /* |
||
| 5758 | * Whitelist URL |
||
| 5759 | * |
||
| 5760 | * Ignore the URL differences between the blog and the shadow site. |
||
| 5761 | */ |
||
| 5762 | public static function whitelist_current_url() { |
||
| 5770 | |||
| 5771 | /* |
||
| 5772 | * Ajax callbacks for ID crisis resolutions |
||
| 5773 | * |
||
| 5774 | * Things that could happen here: |
||
| 5775 | * - site_migrated : Update the URL on the shadow blog to match new domain |
||
| 5776 | * - whitelist : Ignore the URL difference |
||
| 5777 | * - default : Error message |
||
| 5778 | */ |
||
| 5779 | public static function resolve_identity_crisis_ajax_callback() { |
||
| 5819 | |||
| 5820 | /** |
||
| 5821 | * Adds a value to the whitelist for the specified key. |
||
| 5822 | * |
||
| 5823 | * @param string $key The option name that we're whitelisting the value for. |
||
| 5824 | * @param string $value The value that we're intending to add to the whitelist. |
||
| 5825 | * |
||
| 5826 | * @return bool Whether the value was added to the whitelist, or false if it was already there. |
||
| 5827 | */ |
||
| 5828 | public static function whitelist_identity_crisis_value( $key, $value ) { |
||
| 5842 | |||
| 5843 | /** |
||
| 5844 | * Checks whether a value is already whitelisted. |
||
| 5845 | * |
||
| 5846 | * @param string $key The option name that we're checking the value for. |
||
| 5847 | * @param string $value The value that we're curious to see if it's on the whitelist. |
||
| 5848 | * |
||
| 5849 | * @return bool Whether the value is whitelisted. |
||
| 5850 | */ |
||
| 5851 | public static function is_identity_crisis_value_whitelisted( $key, $value ) { |
||
| 5858 | |||
| 5859 | /** |
||
| 5860 | * Checks whether the home and siteurl specifically are whitelisted |
||
| 5861 | * Written so that we don't have re-check $key and $value params every time |
||
| 5862 | * we want to check if this site is whitelisted, for example in footer.php |
||
| 5863 | * |
||
| 5864 | * @return bool True = already whitelsisted False = not whitelisted |
||
| 5865 | */ |
||
| 5866 | public static function is_staging_site() { |
||
| 5930 | |||
| 5931 | public function identity_crisis_js( $nonce ) { |
||
| 6001 | |||
| 6002 | /** |
||
| 6003 | * Displays an admin_notice, alerting the user to an identity crisis. |
||
| 6004 | */ |
||
| 6005 | public function alert_identity_crisis() { |
||
| 6130 | |||
| 6131 | /** |
||
| 6132 | * Maybe Use a .min.css stylesheet, maybe not. |
||
| 6133 | * |
||
| 6134 | * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. |
||
| 6135 | */ |
||
| 6136 | public static function maybe_min_asset( $url, $path, $plugin ) { |
||
| 6171 | |||
| 6172 | /** |
||
| 6173 | * Maybe inlines a stylesheet. |
||
| 6174 | * |
||
| 6175 | * If you'd like to inline a stylesheet instead of printing a link to it, |
||
| 6176 | * wp_style_add_data( 'handle', 'jetpack-inline', true ); |
||
| 6177 | * |
||
| 6178 | * Attached to `style_loader_tag` filter. |
||
| 6179 | * |
||
| 6180 | * @param string $tag The tag that would link to the external asset. |
||
| 6181 | * @param string $handle The registered handle of the script in question. |
||
| 6182 | * |
||
| 6183 | * @return string |
||
| 6184 | */ |
||
| 6185 | public static function maybe_inline_style( $tag, $handle ) { |
||
| 6235 | |||
| 6236 | /** |
||
| 6237 | * Loads a view file from the views |
||
| 6238 | * |
||
| 6239 | * Data passed in with the $data parameter will be available in the |
||
| 6240 | * template file as $data['value'] |
||
| 6241 | * |
||
| 6242 | * @param string $template - Template file to load |
||
| 6243 | * @param array $data - Any data to pass along to the template |
||
| 6244 | * @return boolean - If template file was found |
||
| 6245 | **/ |
||
| 6246 | public function load_view( $template, $data = array() ) { |
||
| 6257 | |||
| 6258 | /** |
||
| 6259 | * Throws warnings for deprecated hooks to be removed from Jetpack |
||
| 6260 | */ |
||
| 6261 | public function deprecated_hooks() { |
||
| 6262 | global $wp_filter; |
||
| 6263 | |||
| 6264 | /* |
||
| 6290 | |||
| 6291 | /** |
||
| 6292 | * Converts any url in a stylesheet, to the correct absolute url. |
||
| 6293 | * |
||
| 6294 | * Considerations: |
||
| 6295 | * - Normal, relative URLs `feh.png` |
||
| 6296 | * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==` |
||
| 6297 | * - Schema-agnostic URLs `//domain.com/feh.png` |
||
| 6298 | * - Absolute URLs `http://domain.com/feh.png` |
||
| 6299 | * - Domain root relative URLs `/feh.png` |
||
| 6300 | * |
||
| 6301 | * @param $css string: The raw CSS -- should be read in directly from the file. |
||
| 6302 | * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. |
||
| 6303 | * |
||
| 6304 | * @return mixed|string |
||
| 6305 | */ |
||
| 6306 | public static function absolutize_css_urls( $css, $css_file_url ) { |
||
| 6350 | |||
| 6351 | /** |
||
| 6352 | * This method checks to see if SSL is required by the site in |
||
| 6353 | * order to visit it in some way other than only setting the |
||
| 6354 | * https value in the home or siteurl values. |
||
| 6355 | * |
||
| 6356 | * @since 3.2 |
||
| 6357 | * @return boolean |
||
| 6358 | **/ |
||
| 6359 | private function is_ssl_required_to_visit_site() { |
||
| 6368 | |||
| 6369 | /** |
||
| 6370 | * This methods removes all of the registered css files on the frontend |
||
| 6371 | * from Jetpack in favor of using a single file. In effect "imploding" |
||
| 6372 | * all the files into one file. |
||
| 6373 | * |
||
| 6374 | * Pros: |
||
| 6375 | * - Uses only ONE css asset connection instead of 15 |
||
| 6376 | * - Saves a minimum of 56k |
||
| 6377 | * - Reduces server load |
||
| 6378 | * - Reduces time to first painted byte |
||
| 6379 | * |
||
| 6380 | * Cons: |
||
| 6381 | * - Loads css for ALL modules. However all selectors are prefixed so it |
||
| 6382 | * should not cause any issues with themes. |
||
| 6383 | * - Plugins/themes dequeuing styles no longer do anything. See |
||
| 6384 | * jetpack_implode_frontend_css filter for a workaround |
||
| 6385 | * |
||
| 6386 | * For some situations developers may wish to disable css imploding and |
||
| 6387 | * instead operate in legacy mode where each file loads seperately and |
||
| 6388 | * can be edited individually or dequeued. This can be accomplished with |
||
| 6389 | * the following line: |
||
| 6390 | * |
||
| 6391 | * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
||
| 6392 | * |
||
| 6393 | * @since 3.2 |
||
| 6394 | **/ |
||
| 6395 | public function implode_frontend_css( $travis_test = false ) { |
||
| 6447 | |||
| 6448 | function concat_remove_style_loader_tag( $tag, $handle ) { |
||
| 6458 | |||
| 6459 | /* |
||
| 6460 | * Check the heartbeat data |
||
| 6461 | * |
||
| 6462 | * Organizes the heartbeat data by severity. For example, if the site |
||
| 6463 | * is in an ID crisis, it will be in the $filtered_data['bad'] array. |
||
| 6464 | * |
||
| 6465 | * Data will be added to "caution" array, if it either: |
||
| 6466 | * - Out of date Jetpack version |
||
| 6467 | * - Out of date WP version |
||
| 6468 | * - Out of date PHP version |
||
| 6469 | * |
||
| 6470 | * $return array $filtered_data |
||
| 6471 | */ |
||
| 6472 | public static function jetpack_check_heartbeat_data() { |
||
| 6525 | |||
| 6526 | |||
| 6527 | /* |
||
| 6528 | * This method is used to organize all options that can be reset |
||
| 6529 | * without disconnecting Jetpack. |
||
| 6530 | * |
||
| 6531 | * It is used in class.jetpack-cli.php to reset options |
||
| 6532 | * |
||
| 6533 | * @return array of options to delete. |
||
| 6534 | */ |
||
| 6535 | public static function get_jetpack_options_for_reset() { |
||
| 6602 | |||
| 6603 | /** |
||
| 6604 | * Check if an option of a Jetpack module has been updated. |
||
| 6605 | * |
||
| 6606 | * If any module option has been updated before Jump Start has been dismissed, |
||
| 6607 | * update the 'jumpstart' option so we can hide Jump Start. |
||
| 6608 | * |
||
| 6609 | * @param string $option_name |
||
| 6610 | * |
||
| 6611 | * @return bool |
||
| 6612 | */ |
||
| 6613 | public static function jumpstart_has_updated_module_option( $option_name = '' ) { |
||
| 6634 | |||
| 6635 | /* |
||
| 6636 | * Strip http:// or https:// from a url, replaces forward slash with ::, |
||
| 6637 | * so we can bring them directly to their site in calypso. |
||
| 6638 | * |
||
| 6639 | * @param string | url |
||
| 6640 | * @return string | url without the guff |
||
| 6641 | */ |
||
| 6642 | public static function build_raw_urls( $url ) { |
||
| 6648 | |||
| 6649 | /** |
||
| 6650 | * Stores and prints out domains to prefetch for page speed optimization. |
||
| 6651 | * |
||
| 6652 | * @param mixed $new_urls |
||
| 6653 | */ |
||
| 6654 | public static function dns_prefetch( $new_urls = null ) { |
||
| 6671 | |||
| 6672 | public function wp_dashboard_setup() { |
||
| 6700 | |||
| 6701 | /** |
||
| 6702 | * @param mixed $result Value for the user's option |
||
| 6703 | * @return mixed |
||
| 6704 | */ |
||
| 6705 | function get_user_option_meta_box_order_dashboard( $sorted ) { |
||
| 6730 | |||
| 6731 | public static function dashboard_widget() { |
||
| 6739 | |||
| 6740 | public static function dashboard_widget_footer() { |
||
| 6773 | |||
| 6774 | public function dashboard_widget_connect_to_wpcom() { |
||
| 6796 | |||
| 6797 | /* |
||
| 6798 | * A graceful transition to using Core's site icon. |
||
| 6799 | * |
||
| 6800 | * All of the hard work has already been done with the image |
||
| 6801 | * in all_done_page(). All that needs to be done now is update |
||
| 6802 | * the option and display proper messaging. |
||
| 6803 | * |
||
| 6804 | * @todo remove when WP 4.3 is minimum |
||
| 6805 | * |
||
| 6806 | * @since 3.6.1 |
||
| 6807 | * |
||
| 6808 | * @return bool false = Core's icon not available || true = Core's icon is available |
||
| 6809 | */ |
||
| 6810 | public static function jetpack_site_icon_available_in_core() { |
||
| 6845 | |||
| 6846 | } |
||
| 6847 |
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.