Jetpack

Options: jetpack_options (array) An array of options.

Defined (1)

The class is defined in the following location(s).

/class.jetpack.php  
  1. class Jetpack { 
  2. public $xmlrpc_server = null; 
  3.  
  4. private $xmlrpc_verification = null; 
  5.  
  6. public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA'] 
  7.  
  8. /** 
  9. * @var array The handles of styles that are concatenated into jetpack.css 
  10. */ 
  11. public $concatenated_style_handles = array( 
  12. 'jetpack-carousel',  
  13. 'grunion.css',  
  14. 'the-neverending-homepage',  
  15. 'jetpack_likes',  
  16. 'jetpack_related-posts',  
  17. 'sharedaddy',  
  18. 'jetpack-slideshow',  
  19. 'presentations',  
  20. 'jetpack-subscriptions',  
  21. 'tiled-gallery',  
  22. 'widget-conditions',  
  23. 'jetpack_display_posts_widget',  
  24. 'gravatar-profile-widget',  
  25. 'widget-grid-and-list',  
  26. 'jetpack-widgets',  
  27. 'goodreads-widget',  
  28. ); 
  29.  
  30. public $plugins_to_deactivate = array( 
  31. 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ),  
  32. 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ),  
  33. 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),  
  34. 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),  
  35. 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ),  
  36. 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),  
  37. 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),  
  38. 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),  
  39. 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ),  
  40. 'videopress' => array( 'video/video.php', 'VideoPress' ),  
  41. 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),  
  42. 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),  
  43. 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),  
  44. 'omnisearch' => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ),  
  45. 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),  
  46. 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ) 
  47. ); 
  48.  
  49. public $capability_translations = array( 
  50. 'administrator' => 'manage_options',  
  51. 'editor' => 'edit_others_posts',  
  52. 'author' => 'publish_posts',  
  53. 'contributor' => 'edit_posts',  
  54. 'subscriber' => 'read',  
  55. ); 
  56.  
  57. /** 
  58. * Map of modules that have conflicts with plugins and should not be auto-activated 
  59. * if the plugins are active. Used by filter_default_modules 
  60. * Plugin Authors: If you'd like to prevent a single module from auto-activating,  
  61. * change `module-slug` and add this to your plugin: 
  62. * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' ); 
  63. * function my_jetpack_get_default_modules( $modules ) { 
  64. * return array_diff( $modules, array( 'module-slug' ) ); 
  65. * } 
  66. * @var array 
  67. */ 
  68. private $conflicting_plugins = array( 
  69. 'comments' => array( 
  70. 'Intense Debate' => 'intensedebate/intensedebate.php',  
  71. 'Disqus' => 'disqus-comment-system/disqus.php',  
  72. 'Livefyre' => 'livefyre-comments/livefyre.php',  
  73. 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php',  
  74. 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php',  
  75. 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php',  
  76. ),  
  77. 'contact-form' => array( 
  78. 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php',  
  79. 'Gravity Forms' => 'gravityforms/gravityforms.php',  
  80. 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php',  
  81. 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php',  
  82. 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php',  
  83. ),  
  84. 'minileven' => array( 
  85. 'WPtouch' => 'wptouch/wptouch.php',  
  86. ),  
  87. 'latex' => array( 
  88. 'LaTeX for WordPress' => 'latex/latex.php',  
  89. 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php',  
  90. 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php',  
  91. 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php',  
  92. 'Enable Latex' => 'enable-latex/enable-latex.php',  
  93. 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php',  
  94. ),  
  95. 'protect' => array( 
  96. 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php',  
  97. 'Captcha' => 'captcha/captcha.php',  
  98. 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php',  
  99. 'Login Security Solution' => 'login-security-solution/login-security-solution.php',  
  100. 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php',  
  101. 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php',  
  102. 'SiteGuard WP Plugin' => 'siteguard/siteguard.php',  
  103. 'Security-protection' => 'security-protection/security-protection.php',  
  104. 'Login Security' => 'login-security/login-security.php',  
  105. 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php',  
  106. 'Wordfence Security' => 'wordfence/wordfence.php',  
  107. 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php',  
  108. 'iThemes Security' => 'better-wp-security/better-wp-security.php',  
  109. ),  
  110. 'random-redirect' => array( 
  111. 'Random Redirect 2' => 'random-redirect-2/random-redirect.php',  
  112. ),  
  113. 'related-posts' => array( 
  114. 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php',  
  115. 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php',  
  116. 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php',  
  117. 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php',  
  118. 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php',  
  119. 'outbrain' => 'outbrain/outbrain.php',  
  120. 'Shareaholic' => 'shareaholic/shareaholic.php',  
  121. 'Sexybookmarks' => 'sexybookmarks/shareaholic.php',  
  122. ),  
  123. 'sharedaddy' => array( 
  124. 'AddThis' => 'addthis/addthis_social_widget.php',  
  125. 'Add To Any' => 'add-to-any/add-to-any.php',  
  126. 'ShareThis' => 'share-this/sharethis.php',  
  127. 'Shareaholic' => 'shareaholic/shareaholic.php',  
  128. ),  
  129. 'verification-tools' => array( 
  130. 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',  
  131. 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',  
  132. 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',  
  133. ),  
  134. 'widget-visibility' => array( 
  135. 'Widget Logic' => 'widget-logic/widget_logic.php',  
  136. 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php',  
  137. ),  
  138. ); 
  139.  
  140. /** 
  141. * Plugins for which we turn off our Facebook OG Tags implementation. 
  142. * Note: WordPress SEO by Yoast and WordPress SEO Premium by Yoast automatically deactivate 
  143. * Jetpack's Open Graph tags via filter when their Social Meta modules are active. 
  144. * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter: 
  145. * add_filter( 'jetpack_enable_open_graph', '__return_false' ); 
  146. */ 
  147. private $open_graph_conflicting_plugins = array( 
  148. '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php',  
  149. // 2 Click Social Media Buttons 
  150. 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook 
  151. 'add-meta-tags/add-meta-tags.php', // Add Meta Tags 
  152. 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail 
  153. 'facebook/facebook.php', // Facebook (official plugin) 
  154. 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one 
  155. 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php',  
  156. // Facebook Featured Image & OG Meta Tags 
  157. 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags 
  158. 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php',  
  159. // Facebook Open Graph Meta Tags for WordPress 
  160. 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag 
  161. 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer 
  162. 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php',  
  163. // Fedmich's Facebook Open Graph Meta 
  164. 'header-footer/plugin.php', // Header and Footer 
  165. 'network-publisher/networkpub.php', // Network Publisher 
  166. 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG 
  167. 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php',  
  168. // NextScripts SNAP 
  169. 'opengraph/opengraph.php', // Open Graph 
  170. 'open-graph-protocol-framework/open-graph-protocol-framework.php',  
  171. // Open Graph Protocol Framework 
  172. 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments 
  173. 'seo-ultimate/seo-ultimate.php', // SEO Ultimate 
  174. 'sexybookmarks/sexy-bookmarks.php', // Shareaholic 
  175. 'shareaholic/sexy-bookmarks.php', // Shareaholic 
  176. 'sharepress/sharepress.php', // SharePress 
  177. 'simple-facebook-connect/sfc.php', // Simple Facebook Connect 
  178. 'social-discussions/social-discussions.php', // Social Discussions 
  179. 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit 
  180. 'socialize/socialize.php', // Socialize 
  181. 'only-tweet-like-share-and-google-1/tweet-like-plusone.php',  
  182. // Tweet, Like, Google +1 and Share 
  183. 'wordbooker/wordbooker.php', // Wordbooker 
  184. 'wpsso/wpsso.php', // WordPress Social Sharing Optimization 
  185. 'wp-caregiver/wp-caregiver.php', // WP Caregiver 
  186. 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php',  
  187. // WP Facebook Like Send & Open Graph Meta 
  188. 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol 
  189. 'wp-ogp/wp-ogp.php', // WP-OGP 
  190. 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin 
  191. 'wp-fb-share-like-button/wp_fb_share-like_widget.php' // WP Facebook Like Button 
  192. ); 
  193.  
  194. /** 
  195. * Plugins for which we turn off our Twitter Cards Tags implementation. 
  196. */ 
  197. private $twitter_cards_conflicting_plugins = array( 
  198. // 'twitter/twitter.php', // The official one handles this on its own. 
  199. // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php 
  200. 'eewee-twitter-card/index.php', // Eewee Twitter Card 
  201. 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards 
  202. 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards 
  203. 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php',  
  204. // Pure Web Brilliant's Social Graph Twitter Cards Extension 
  205. 'twitter-cards/twitter-cards.php', // Twitter Cards 
  206. 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta 
  207. 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards 
  208. ); 
  209.  
  210. /** 
  211. * Message to display in admin_notice 
  212. * @var string 
  213. */ 
  214. public $message = ''; 
  215.  
  216. /** 
  217. * Error to display in admin_notice 
  218. * @var string 
  219. */ 
  220. public $error = ''; 
  221.  
  222. /** 
  223. * Modules that need more privacy description. 
  224. * @var string 
  225. */ 
  226. public $privacy_checks = ''; 
  227.  
  228. /** 
  229. * Stats to record once the page loads 
  230. * @var array 
  231. */ 
  232. public $stats = array(); 
  233.  
  234. /** 
  235. * Allows us to build a temporary security report 
  236. * @var array 
  237. */ 
  238. static $security_report = array(); 
  239.  
  240. /** 
  241. * Jetpack_Sync object 
  242. */ 
  243. public $sync; 
  244.  
  245. /** 
  246. * Verified data for JSON authorization request 
  247. */ 
  248. public $json_api_authorization_request = array(); 
  249.  
  250. /** 
  251. * Holds the singleton instance of this class 
  252. * @since 2.3.3 
  253. * @var Jetpack 
  254. */ 
  255. static $instance = false; 
  256.  
  257. /** 
  258. * Singleton 
  259. * @static 
  260. */ 
  261. public static function init() { 
  262. if ( ! self::$instance ) { 
  263. if ( did_action( 'plugins_loaded' ) ) 
  264. self::plugin_textdomain(); 
  265. else 
  266. add_action( 'plugins_loaded', array( __CLASS__, 'plugin_textdomain' ), 99 ); 
  267.  
  268. self::$instance = new Jetpack; 
  269.  
  270. self::$instance->plugin_upgrade(); 
  271.  
  272. add_action( 'init', array( __CLASS__, 'perform_security_reporting' ) ); 
  273.  
  274.  
  275. return self::$instance; 
  276.  
  277. /** 
  278. * Must never be called statically 
  279. */ 
  280. function plugin_upgrade() { 
  281. // Upgrade: 1.1 -> 1.2 
  282. if ( get_option( 'jetpack_id' ) ) { 
  283. // Move individual jetpack options to single array of options 
  284. $options = array(); 
  285. foreach ( Jetpack_Options::get_option_names() as $option ) { 
  286. if ( false !== $value = get_option( "jetpack_$option" ) ) { 
  287. $options[$option] = $value; 
  288.  
  289. if ( $options ) { 
  290. Jetpack_Options::update_options( $options ); 
  291.  
  292. foreach ( array_keys( $options ) as $option ) { 
  293. delete_option( "jetpack_$option" ); 
  294.  
  295. // Add missing version and old_version options 
  296. if ( ! $version = Jetpack_Options::get_option( 'version' ) ) { 
  297. $version = $old_version = '1.1:' . time(); 
  298. /** 
  299. * Fires on update, before bumping version numbers up to a new version. 
  300. * @since 3.4.0 
  301. * @param string $version Jetpack version number. 
  302. * @param bool false Does an old version exist. Default is false. 
  303. */ 
  304. do_action( 'updating_jetpack_version', $version, false ); 
  305. Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); 
  306.  
  307. // Upgrade from a single user token to a user_id-indexed array and a master_user ID 
  308. if ( ! Jetpack_Options::get_option( 'user_tokens' ) ) { 
  309. if ( $user_token = Jetpack_Options::get_option( 'user_token' ) ) { 
  310. $token_parts = explode( '.', $user_token ); 
  311. if ( isset( $token_parts[2] ) ) { 
  312. $master_user = $token_parts[2]; 
  313. $user_tokens = array( $master_user => $user_token ); 
  314. Jetpack_Options::update_options( compact( 'master_user', 'user_tokens' ) ); 
  315. Jetpack_Options::delete_option( 'user_token' ); 
  316. } else { 
  317. // @todo: is this even possible? 
  318. trigger_error( sprintf( 'Jetpack::plugin_upgrade found no user_id in user_token "%s"', $user_token ), E_USER_WARNING ); 
  319.  
  320. // Clean up legacy G+ Authorship data. 
  321. if ( get_option( 'gplus_authors' ) ) { 
  322. delete_option( 'gplus_authors' ); 
  323. delete_option( 'hide_gplus' ); 
  324. delete_metadata( 'post', 0, 'gplus_authorship_disabled', null, true ); 
  325.  
  326. if ( ! get_option( 'jetpack_private_options' ) ) { 
  327. $jetpack_options = get_option( 'jetpack_options', array() ); 
  328. foreach( Jetpack_Options::get_option_names( 'private' ) as $option_name ) { 
  329. if ( isset( $jetpack_options[ $option_name ] ) ) { 
  330. Jetpack_Options::update_option( $option_name, $jetpack_options[ $option_name ] ); 
  331. unset( $jetpack_options[ $option_name ] ); 
  332. update_option( 'jetpack_options', $jetpack_options ); 
  333.  
  334. if ( Jetpack::is_active() ) { 
  335. list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); 
  336. if ( JETPACK__VERSION != $version ) { 
  337. add_action( 'init', array( __CLASS__, 'activate_new_modules' ) ); 
  338. /** 
  339. * Fires when synchronizing all registered options and constants. 
  340. * @since 3.3.0 
  341. */ 
  342. do_action( 'jetpack_sync_all_registered_options' ); 
  343.  
  344. //if Jetpack is connected check if jetpack_unique_connection exists and if not then set it 
  345. $jetpack_unique_connection = get_option( 'jetpack_unique_connection' ); 
  346. $is_unique_connection = $jetpack_unique_connection && array_key_exists( 'version', $jetpack_unique_connection ); 
  347. if ( ! $is_unique_connection ) { 
  348. $jetpack_unique_connection = array( 
  349. 'connected' => 1,  
  350. 'disconnected' => -1,  
  351. 'version' => '3.6.1' 
  352. ); 
  353. update_option( 'jetpack_unique_connection', $jetpack_unique_connection ); 
  354.  
  355. if ( get_option( 'jetpack_json_api_full_management' ) ) { 
  356. delete_option( 'jetpack_json_api_full_management' ); 
  357. self::activate_manage(); 
  358.  
  359.  
  360. static function activate_manage( ) { 
  361.  
  362. if ( did_action( 'init' ) || current_filter() == 'init' ) { 
  363. self::activate_module( 'manage', false, false ); 
  364. } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) { 
  365. add_action( 'init', array( __CLASS__, 'activate_manage' ) ); 
  366.  
  367.  
  368. /** 
  369. * Constructor. Initializes WordPress hooks 
  370. */ 
  371. private function __construct() { 
  372. /** 
  373. * Check for and alert any deprecated hooks 
  374. */ 
  375. add_action( 'init', array( $this, 'deprecated_hooks' ) ); 
  376.  
  377. /** 
  378. * Do things that should run even in the network admin 
  379. * here, before we potentially fail out. 
  380. */ 
  381. add_filter( 'jetpack_require_lib_dir', array( $this, 'require_lib_dir' ) ); 
  382.  
  383. /** 
  384. * We need sync object even in Multisite mode 
  385. */ 
  386. $this->sync = new Jetpack_Sync; 
  387.  
  388. /** 
  389. * Trigger a wp_version sync when updating WP versions 
  390. **/ 
  391. add_action( 'upgrader_process_complete', array( 'Jetpack', 'update_get_wp_version' ), 10, 2 ); 
  392. $this->sync->mock_option( 'wp_version', array( 'Jetpack', 'get_wp_version' ) ); 
  393.  
  394. add_action( 'init', array( $this, 'sync_update_data') ); 
  395. add_action( 'init', array( $this, 'sync_theme_data' ) ); 
  396.  
  397. /** 
  398. * Load things that should only be in Network Admin. 
  399. * For now blow away everything else until a more full 
  400. * understanding of what is needed at the network level is 
  401. * available 
  402. */ 
  403. if( is_multisite() ) { 
  404. Jetpack_Network::init(); 
  405.  
  406. // Only sync this info if we are on a multi site 
  407. // @since 3.7 
  408. $this->sync->mock_option( 'network_name', array( 'Jetpack', 'network_name' ) ); 
  409. $this->sync->mock_option( 'network_allow_new_registrations', array( 'Jetpack', 'network_allow_new_registrations' ) ); 
  410. $this->sync->mock_option( 'network_add_new_users', array( 'Jetpack', 'network_add_new_users' ) ); 
  411. $this->sync->mock_option( 'network_site_upload_space', array( 'Jetpack', 'network_site_upload_space' ) ); 
  412. $this->sync->mock_option( 'network_upload_file_types', array( 'Jetpack', 'network_upload_file_types' ) ); 
  413. $this->sync->mock_option( 'network_enable_administration_menus', array( 'Jetpack', 'network_enable_administration_menus' ) ); 
  414.  
  415. if( is_network_admin() ) { 
  416. // Sync network site data if it is updated or not. 
  417. add_action( 'update_wpmu_options', array( $this, 'update_jetpack_network_settings' ) ); 
  418. return; // End here to prevent single site actions from firing 
  419.  
  420.  
  421. $theme_slug = get_option( 'stylesheet' ); 
  422.  
  423.  
  424. // Modules should do Jetpack_Sync::sync_options( __FILE__, $option, ... ); instead 
  425. // We access the "internal" method here only because the Jetpack object isn't instantiated yet 
  426. $this->sync->options( 
  427. JETPACK__PLUGIN_DIR . 'jetpack.php',  
  428. 'home',  
  429. 'siteurl',  
  430. 'blogname',  
  431. 'gmt_offset',  
  432. 'timezone_string',  
  433. 'security_report',  
  434. 'stylesheet',  
  435. "theme_mods_{$theme_slug}",  
  436. 'jetpack_sync_non_public_post_stati',  
  437. 'jetpack_options',  
  438. 'site_icon', // (int) - ID of core's Site Icon attachment ID 
  439. 'default_post_format',  
  440. 'default_category',  
  441. 'large_size_w',  
  442. 'large_size_h',  
  443. 'thumbnail_size_w',  
  444. 'thumbnail_size_h',  
  445. 'medium_size_w',  
  446. 'medium_size_h',  
  447. 'thumbnail_crop',  
  448. 'image_default_link_type' 
  449. ); 
  450.  
  451. foreach( Jetpack_Options::get_option_names( 'non-compact' ) as $option ) { 
  452. $this->sync->options( __FILE__, 'jetpack_' . $option ); 
  453.  
  454. /** 
  455. * Sometimes you want to sync data to .com without adding options to .org sites. 
  456. * The mock option allows you to do just that. 
  457. */ 
  458. $this->sync->mock_option( 'is_main_network', array( $this, 'is_main_network_option' ) ); 
  459. $this->sync->mock_option( 'is_multi_site', array( $this, 'is_multisite' ) ); 
  460. $this->sync->mock_option( 'main_network_site', array( $this, 'jetpack_main_network_site_option' ) ); 
  461. $this->sync->mock_option( 'single_user_site', array( 'Jetpack', 'is_single_user_site' ) ); 
  462. $this->sync->mock_option( 'stat_data', array( $this, 'get_stat_data' ) ); 
  463.  
  464. $this->sync->mock_option( 'has_file_system_write_access', array( 'Jetpack', 'file_system_write_access' ) ); 
  465. $this->sync->mock_option( 'is_version_controlled', array( 'Jetpack', 'is_version_controlled' ) ); 
  466. $this->sync->mock_option( 'max_upload_size', 'wp_max_upload_size' ); 
  467. $this->sync->mock_option( 'content_width', array( 'Jetpack', 'get_content_width' ) ); 
  468.  
  469. /** 
  470. * Trigger an update to the main_network_site when we update the blogname of a site. 
  471. */ 
  472. add_action( 'update_option_siteurl', array( $this, 'update_jetpack_main_network_site_option' ) ); 
  473.  
  474. add_action( 'update_option', array( $this, 'log_settings_change' ), 10, 3 ); 
  475.  
  476. // Update the settings everytime the we register a new user to the site or we delete a user. 
  477. add_action( 'user_register', array( $this, 'is_single_user_site_invalidate' ) ); 
  478. add_action( 'deleted_user', array( $this, 'is_single_user_site_invalidate' ) ); 
  479.  
  480. // Unlink user before deleting the user from .com 
  481. add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 ); 
  482. add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 ); 
  483.  
  484. if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) { 
  485. @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed. 
  486.  
  487. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php'; 
  488. $this->xmlrpc_server = new Jetpack_XMLRPC_Server(); 
  489.  
  490. $this->require_jetpack_authentication(); 
  491.  
  492. if ( Jetpack::is_active() ) { 
  493. // Hack to preserve $HTTP_RAW_POST_DATA 
  494. add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); 
  495.  
  496. $signed = $this->verify_xml_rpc_signature(); 
  497. if ( $signed && ! is_wp_error( $signed ) ) { 
  498. // The actual API methods. 
  499. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) ); 
  500. } else { 
  501. add_filter( 'xmlrpc_methods', '__return_empty_array' ); 
  502. } else { 
  503. // The bootstrap API methods. 
  504. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) ); 
  505.  
  506. // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on. 
  507. add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); 
  508. } elseif ( is_admin() && isset( $_POST['action'] ) && 'jetpack_upload_file' == $_POST['action'] ) { 
  509. $this->require_jetpack_authentication(); 
  510. $this->add_remote_request_handlers(); 
  511. } else { 
  512. if ( Jetpack::is_active() ) { 
  513. add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) ); 
  514. add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) ); 
  515.  
  516. if ( Jetpack::is_active() ) { 
  517. Jetpack_Heartbeat::init(); 
  518.  
  519. add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) ); 
  520. if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) { 
  521. wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); 
  522.  
  523. add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) ); 
  524.  
  525. add_action( 'admin_init', array( $this, 'admin_init' ) ); 
  526. add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) ); 
  527.  
  528. add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); 
  529.  
  530. add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) ); 
  531. // Filter the dashboard meta box order to swap the new one in in place of the old one. 
  532. add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) ); 
  533.  
  534. add_action( 'wp_ajax_jetpack-sync-reindex-trigger', array( $this, 'sync_reindex_trigger' ) ); 
  535. add_action( 'wp_ajax_jetpack-sync-reindex-status', array( $this, 'sync_reindex_status' ) ); 
  536.  
  537. // Jump Start AJAX callback function 
  538. add_action( 'wp_ajax_jetpack_jumpstart_ajax', array( $this, 'jetpack_jumpstart_ajax_callback' ) ); 
  539. add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) ); 
  540.  
  541. // Identity Crisis AJAX callback function 
  542. add_action( 'wp_ajax_jetpack_resolve_identity_crisis', array( $this, 'resolve_identity_crisis_ajax_callback' ) ); 
  543.  
  544. // JITM AJAX callback function 
  545. add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) ); 
  546.  
  547. add_action( 'wp_ajax_jetpack_admin_ajax', array( $this, 'jetpack_admin_ajax_callback' ) ); 
  548. add_action( 'wp_ajax_jetpack_admin_ajax_refresh', array( $this, 'jetpack_admin_ajax_refresh_data' ) ); 
  549.  
  550. add_action( 'wp_loaded', array( $this, 'register_assets' ) ); 
  551. add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) ); 
  552. add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) ); 
  553. add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) ); 
  554.  
  555. add_action( 'jetpack_activate_module', array( $this, 'activate_module_actions' ) ); 
  556.  
  557. add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 ); 
  558.  
  559. add_action( 'jetpack_notices', array( $this, 'show_development_mode_notice' ) ); 
  560.  
  561. /** 
  562. * These actions run checks to load additional files. 
  563. * They check for external files or plugins, so they need to run as late as possible. 
  564. */ 
  565. add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 ); 
  566. add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 ); 
  567. add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 ); 
  568.  
  569. add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 ); 
  570. add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 ); 
  571.  
  572. add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 ); 
  573.  
  574. add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) ); 
  575. add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 ); 
  576.  
  577. // A filter to control all just in time messages 
  578. add_filter( 'jetpack_just_in_time_msgs', '__return_true' ); 
  579.  
  580. /** 
  581. * This is the hack to concatinate all css files into one. 
  582. * For description and reasoning see the implode_frontend_css method 
  583. * Super late priority so we catch all the registered styles 
  584. */ 
  585. if( !is_admin() ) { 
  586. add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first 
  587. add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles` 
  588.  
  589. // Sync Core Icon: Detect changes in Core's Site Icon and make it syncable. 
  590. add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); 
  591. add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); 
  592. add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); 
  593. add_action( 'jetpack_heartbeat', array( $this, 'jetpack_sync_core_icon' ) ); 
  594.  
  595.  
  596. /** 
  597. * Make sure any site icon added to core can get 
  598. * synced back to dotcom, so we can display it there. 
  599. */ 
  600. function jetpack_sync_core_icon() { 
  601. if ( function_exists( 'get_site_icon_url' ) ) { 
  602. $url = get_site_icon_url(); 
  603. } else { 
  604. return; 
  605.  
  606. require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' ); 
  607. // If there's a core icon, maybe update the option. If not, fall back to Jetpack's. 
  608. if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) { 
  609. // This is the option that is synced with dotcom 
  610. Jetpack_Options::update_option( 'site_icon_url', $url ); 
  611. } else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) { 
  612. Jetpack_Options::delete_option( 'site_icon_url' ); 
  613.  
  614. function jetpack_admin_ajax_callback() { 
  615. // Check for nonce 
  616. if ( ! isset( $_REQUEST['adminNonce'] ) || ! wp_verify_nonce( $_REQUEST['adminNonce'], 'jetpack-admin-nonce' ) || ! current_user_can( 'jetpack_manage_modules' ) ) { 
  617. wp_die( 'permissions check failed' ); 
  618.  
  619. if ( isset( $_REQUEST['toggleModule'] ) && 'nux-toggle-module' == $_REQUEST['toggleModule'] ) { 
  620. $slug = $_REQUEST['thisModuleSlug']; 
  621.  
  622. if ( ! in_array( $slug, Jetpack::get_available_modules() ) ) { 
  623. wp_die( 'That is not a Jetpack module slug' ); 
  624.  
  625. if ( Jetpack::is_module_active( $slug ) ) { 
  626. Jetpack::deactivate_module( $slug ); 
  627. } else { 
  628. Jetpack::activate_module( $slug, false, false ); 
  629.  
  630. $modules = Jetpack_Admin::init()->get_modules(); 
  631. echo json_encode( $modules[ $slug ] ); 
  632.  
  633. exit; 
  634.  
  635. wp_die(); 
  636.  
  637. /** 
  638. * Sometimes we need to refresh the data,  
  639. * especially if the page is visited via a 'history' 
  640. * event like back/forward 
  641. */ 
  642. function jetpack_admin_ajax_refresh_data() { 
  643. // Check for nonce 
  644. if ( ! isset( $_REQUEST['adminNonce'] ) || ! wp_verify_nonce( $_REQUEST['adminNonce'], 'jetpack-admin-nonce' ) ) { 
  645. wp_die( 'permissions check failed' ); 
  646.  
  647. if ( isset( $_REQUEST['refreshData'] ) && 'refresh' == $_REQUEST['refreshData'] ) { 
  648. $modules = Jetpack_Admin::init()->get_modules(); 
  649. echo json_encode( $modules ); 
  650. exit; 
  651.  
  652. wp_die(); 
  653.  
  654. /** 
  655. * The callback for the Jump Start ajax requests. 
  656. */ 
  657. function jetpack_jumpstart_ajax_callback() { 
  658. // Check for nonce 
  659. if ( ! isset( $_REQUEST['jumpstartNonce'] ) || ! wp_verify_nonce( $_REQUEST['jumpstartNonce'], 'jetpack-jumpstart-nonce' ) ) 
  660. wp_die( 'permissions check failed' ); 
  661.  
  662. if ( isset( $_REQUEST['jumpStartActivate'] ) && 'jump-start-activate' == $_REQUEST['jumpStartActivate'] ) { 
  663. // Update the jumpstart option 
  664. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { 
  665. Jetpack_Options::update_option( 'jumpstart', 'jumpstart_activated' ); 
  666.  
  667. // Loops through the requested "Jump Start" modules, and activates them. 
  668. // Custom 'no_message' state, so that no message will be shown on reload. 
  669. $modules = $_REQUEST['jumpstartModSlug']; 
  670. $module_slugs = array(); 
  671. foreach( $modules as $module => $value ) { 
  672. $module_slugs[] = $value['module_slug']; 
  673.  
  674. // Check for possible conflicting plugins 
  675. $module_slugs_filtered = $this->filter_default_modules( $module_slugs ); 
  676.  
  677. foreach ( $module_slugs_filtered as $module_slug ) { 
  678. Jetpack::log( 'activate', $module_slug ); 
  679. Jetpack::activate_module( $module_slug, false, false ); 
  680. Jetpack::state( 'message', 'no_message' ); 
  681.  
  682. // Set the default sharing buttons and set to display on posts if none have been set. 
  683. $sharing_services = get_option( 'sharing-services' ); 
  684. $sharing_options = get_option( 'sharing-options' ); 
  685. if ( empty( $sharing_services['visible'] ) ) { 
  686. // Default buttons to set 
  687. $visible = array( 
  688. 'twitter',  
  689. 'facebook',  
  690. 'google-plus-1',  
  691. ); 
  692. $hidden = array(); 
  693.  
  694. // Set some sharing settings 
  695. $sharing = new Sharing_Service(); 
  696. $sharing_options['global'] = array( 
  697. 'button_style' => 'icon',  
  698. 'sharing_label' => $sharing->default_sharing_label,  
  699. 'open_links' => 'same',  
  700. 'show' => array( 'post' ),  
  701. 'custom' => isset( $sharing_options['global']['custom'] ) ? $sharing_options['global']['custom'] : array() 
  702. ); 
  703.  
  704. update_option( 'sharing-options', $sharing_options ); 
  705.  
  706. // Send a success response so that we can display an error message. 
  707. $success = update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) ); 
  708. echo json_encode( $success ); 
  709. exit; 
  710.  
  711. } elseif ( isset( $_REQUEST['disableJumpStart'] ) && true == $_REQUEST['disableJumpStart'] ) { 
  712. // If dismissed, flag the jumpstart option as such. 
  713. // Send a success response so that we can display an error message. 
  714. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { 
  715. $success = Jetpack_Options::update_option( 'jumpstart', 'jumpstart_dismissed' ); 
  716. echo json_encode( $success ); 
  717. exit; 
  718.  
  719. } elseif ( isset( $_REQUEST['jumpStartDeactivate'] ) && 'jump-start-deactivate' == $_REQUEST['jumpStartDeactivate'] ) { 
  720.  
  721. // FOR TESTING ONLY 
  722. // @todo remove 
  723. $modules = (array) $_REQUEST['jumpstartModSlug']; 
  724. foreach( $modules as $module => $value ) { 
  725. if ( !in_array( $value['module_slug'], Jetpack::get_default_modules() ) ) { 
  726. Jetpack::log( 'deactivate', $value['module_slug'] ); 
  727. Jetpack::deactivate_module( $value['module_slug'] ); 
  728. Jetpack::state( 'message', 'no_message' ); 
  729. } else { 
  730. Jetpack::log( 'activate', $value['module_slug'] ); 
  731. Jetpack::activate_module( $value['module_slug'], false, false ); 
  732. Jetpack::state( 'message', 'no_message' ); 
  733.  
  734. Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); 
  735. echo "reload the page"; 
  736.  
  737. wp_die(); 
  738.  
  739. /** 
  740. * The callback for the JITM ajax requests. 
  741. */ 
  742. function jetpack_jitm_ajax_callback() { 
  743. // Check for nonce 
  744. if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) { 
  745. wp_die( 'Module activation failed due to lack of appropriate permissions' ); 
  746. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) { 
  747. $module_slug = $_REQUEST['jitmModule']; 
  748. Jetpack::log( 'activate', $module_slug ); 
  749. Jetpack::activate_module( $module_slug, false, false ); 
  750. Jetpack::state( 'message', 'no_message' ); 
  751.  
  752. //A Jetpack module is being activated through a JITM, track it 
  753. $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION ); 
  754. $this->do_stats( 'server_side' ); 
  755.  
  756. wp_send_json_success(); 
  757. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) { 
  758. // get the hide_jitm options array 
  759. $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' ); 
  760. $module_slug = $_REQUEST['jitmModule']; 
  761.  
  762. if( ! $jetpack_hide_jitm ) { 
  763. $jetpack_hide_jitm = array( 
  764. $module_slug => 'hide' 
  765. ); 
  766. } else { 
  767. $jetpack_hide_jitm[$module_slug] = 'hide'; 
  768.  
  769. Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm ); 
  770.  
  771. //jitm is being dismissed forever, track it 
  772. $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION ); 
  773. $this->do_stats( 'server_side' ); 
  774.  
  775. wp_send_json_success(); 
  776. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) { 
  777. $module_slug = $_REQUEST['jitmModule']; 
  778.  
  779. // User went to WordPress.com, track this 
  780. $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION ); 
  781. $this->do_stats( 'server_side' ); 
  782.  
  783. wp_send_json_success(); 
  784.  
  785. /** 
  786. * If there are any stats that need to be pushed, but haven't been, push them now. 
  787. */ 
  788. function __destruct() { 
  789. if ( ! empty( $this->stats ) ) { 
  790. $this->do_stats( 'server_side' ); 
  791.  
  792. function jetpack_custom_caps( $caps, $cap, $user_id, $args ) { 
  793. switch( $cap ) { 
  794. case 'jetpack_connect' : 
  795. case 'jetpack_reconnect' : 
  796. if ( Jetpack::is_development_mode() ) { 
  797. $caps = array( 'do_not_allow' ); 
  798. break; 
  799. /** 
  800. * Pass through. If it's not development mode, these should match disconnect. 
  801. * Let users disconnect if it's development mode, just in case things glitch. 
  802. */ 
  803. case 'jetpack_disconnect' : 
  804. /** 
  805. * In multisite, can individual site admins manage their own connection? 
  806. * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class. 
  807. */ 
  808. if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { 
  809. if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) { 
  810. /** 
  811. * We need to update the option name -- it's terribly unclear which 
  812. * direction the override goes. 
  813. * @todo: Update the option name to `sub-sites-can-manage-own-connections` 
  814. */ 
  815. $caps = array( 'do_not_allow' ); 
  816. break; 
  817.  
  818. $caps = array( 'manage_options' ); 
  819. break; 
  820. case 'jetpack_manage_modules' : 
  821. case 'jetpack_activate_modules' : 
  822. case 'jetpack_deactivate_modules' : 
  823. $caps = array( 'manage_options' ); 
  824. break; 
  825. case 'jetpack_configure_modules' : 
  826. $caps = array( 'manage_options' ); 
  827. break; 
  828. case 'jetpack_network_admin_page': 
  829. case 'jetpack_network_settings_page': 
  830. $caps = array( 'manage_network_plugins' ); 
  831. break; 
  832. case 'jetpack_network_sites_page': 
  833. $caps = array( 'manage_sites' ); 
  834. break; 
  835. case 'jetpack_admin_page' : 
  836. if ( Jetpack::is_development_mode() ) { 
  837. $caps = array( 'manage_options' ); 
  838. break; 
  839.  
  840. // Don't ever show to subscribers, but allow access to the page if they're trying to unlink. 
  841. if ( ! current_user_can( 'edit_posts' ) ) { 
  842. if ( isset( $_GET['redirect'] ) && 'sub-unlink' == $_GET['redirect'] ) { 
  843. // We need this in order to unlink the user. 
  844. $this->admin_page_load(); 
  845. if ( ! wp_verify_nonce( 'jetpack-unlink' ) ) { 
  846. $caps = array( 'do_not_allow' ); 
  847. break; 
  848.  
  849. if ( ! self::is_active() && ! current_user_can( 'jetpack_connect' ) ) { 
  850. $caps = array( 'do_not_allow' ); 
  851. break; 
  852. /** 
  853. * Pass through. If it's not development mode, these should match the admin page. 
  854. * Let users disconnect if it's development mode, just in case things glitch. 
  855. */ 
  856. case 'jetpack_connect_user' : 
  857. if ( Jetpack::is_development_mode() ) { 
  858. $caps = array( 'do_not_allow' ); 
  859. break; 
  860. $caps = array( 'read' ); 
  861. break; 
  862. return $caps; 
  863.  
  864. function require_jetpack_authentication() { 
  865. // Don't let anyone authenticate 
  866. $_COOKIE = array(); 
  867. remove_all_filters( 'authenticate' ); 
  868.  
  869. /** 
  870. * For the moment, remove Limit Login Attempts if its xmlrpc for Jetpack. 
  871. * If Limit Login Attempts is installed as a mu-plugin, it can occasionally 
  872. * generate false-positives. 
  873. */ 
  874. remove_filter( 'wp_login_failed', 'limit_login_failed' ); 
  875.  
  876. if ( Jetpack::is_active() ) { 
  877. // Allow Jetpack authentication 
  878. add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 ); 
  879.  
  880. /** 
  881. * Load language files 
  882. */ 
  883. public static function plugin_textdomain() { 
  884. // Note to self, the third argument must not be hardcoded, to account for relocated folders. 
  885. load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' ); 
  886.  
  887. /** 
  888. * Register assets for use in various modules and the Jetpack admin page. 
  889. * @uses wp_script_is, wp_register_script, plugins_url 
  890. * @action wp_loaded 
  891. * @return null 
  892. */ 
  893. public function register_assets() { 
  894. if ( ! wp_script_is( 'spin', 'registered' ) ) { 
  895. wp_register_script( 'spin', plugins_url( '_inc/spin.js', JETPACK__PLUGIN_FILE ), false, '1.3' ); 
  896.  
  897. if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) { 
  898. wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' ); 
  899.  
  900. if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) { 
  901. wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' ); 
  902.  
  903. /** 
  904. * As jetpack_register_genericons is by default fired off a hook,  
  905. * the hook may have already fired by this point. 
  906. * So, let's just trigger it manually. 
  907. */ 
  908. require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); 
  909. jetpack_register_genericons(); 
  910.  
  911. if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) 
  912. wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); 
  913.  
  914. /** 
  915. * Device Pixels support 
  916. * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers. 
  917. */ 
  918. function devicepx() { 
  919. if ( Jetpack::is_active() ) { 
  920. wp_enqueue_script( 'devicepx', set_url_scheme( 'http://s0.wp.com/wp-content/js/devicepx-jetpack.js' ), array(), gmdate( 'oW' ), true ); 
  921.  
  922. /** 
  923. * Returns the location of Jetpack's lib directory. This filter is applied 
  924. * in require_lib(). 
  925. * @filter require_lib_dir 
  926. */ 
  927. function require_lib_dir() { 
  928. return JETPACK__PLUGIN_DIR . '_inc/lib'; 
  929.  
  930. /** 
  931. * Return the network_site_url so that .com knows what network this site is a part of. 
  932. * @param bool $option 
  933. * @return string 
  934. */ 
  935. public function jetpack_main_network_site_option( $option ) { 
  936. return network_site_url(); 
  937. /** 
  938. * Network Name. 
  939. */ 
  940. static function network_name( $option = null ) { 
  941. global $current_site; 
  942. return $current_site->site_name; 
  943. /** 
  944. * Does the network allow new user and site registrations. 
  945. * @return string 
  946. */ 
  947. static function network_allow_new_registrations( $option = null ) { 
  948. return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' ); 
  949. /** 
  950. * Does the network allow admins to add new users. 
  951. * @return boolian 
  952. */ 
  953. static function network_add_new_users( $option = null ) { 
  954. return (bool) get_site_option( 'add_new_users' ); 
  955. /** 
  956. * File upload psace left per site in MB. 
  957. * -1 means NO LIMIT. 
  958. * @return number 
  959. */ 
  960. static function network_site_upload_space( $option = null ) { 
  961. // value in MB 
  962. return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() ); 
  963.  
  964. /** 
  965. * Network allowed file types. 
  966. * @return string 
  967. */ 
  968. static function network_upload_file_types( $option = null ) { 
  969. return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ); 
  970.  
  971. /** 
  972. * Maximum file upload size set by the network. 
  973. * @return number 
  974. */ 
  975. static function network_max_upload_file_size( $option = null ) { 
  976. // value in KB 
  977. return get_site_option( 'fileupload_maxk', 300 ); 
  978.  
  979. /** 
  980. * Lets us know if a site allows admins to manage the network. 
  981. * @return array 
  982. */ 
  983. static function network_enable_administration_menus( $option = null ) { 
  984. return get_site_option( 'menu_items' ); 
  985.  
  986. /** 
  987. * Return whether we are dealing with a multi network setup or not. 
  988. * The reason we are type casting this is because we want to avoid the situation where 
  989. * the result is false since when is_main_network_option return false it cases 
  990. * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the 
  991. * database which could be set to anything as opposed to what this function returns. 
  992. * @param bool $option 
  993. * @return boolean 
  994. */ 
  995. public function is_main_network_option( $option ) { 
  996. // return '1' or '' 
  997. return (string) (bool) Jetpack::is_multi_network(); 
  998.  
  999. /** 
  1000. * Return true if we are with multi-site or multi-network false if we are dealing with single site. 
  1001. * @param string $option 
  1002. * @return boolean 
  1003. */ 
  1004. public function is_multisite( $option ) { 
  1005. return (string) (bool) is_multisite(); 
  1006.  
  1007. /** 
  1008. * Implemented since there is no core is multi network function 
  1009. * Right now there is no way to tell if we which network is the dominant network on the system 
  1010. * @since 3.3 
  1011. * @return boolean 
  1012. */ 
  1013. public static function is_multi_network() { 
  1014. global $wpdb; 
  1015.  
  1016. // if we don't have a multi site setup no need to do any more 
  1017. if ( ! is_multisite() ) { 
  1018. return false; 
  1019.  
  1020. $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" ); 
  1021. if ( $num_sites > 1 ) { 
  1022. return true; 
  1023. } else { 
  1024. return false; 
  1025.  
  1026. /** 
  1027. * Trigger an update to the main_network_site when we update the siteurl of a site. 
  1028. * @return null 
  1029. */ 
  1030. function update_jetpack_main_network_site_option() { 
  1031. // do_action( 'add_option_$option', '$option', '$value-of-the-option' ); 
  1032. /** 
  1033. * Fires when the site URL is updated. 
  1034. * Determines if the site is the main site of a Mulitiste network. 
  1035. * @since 3.3.0 
  1036. * @param string jetpack_main_network_site. 
  1037. * @param string network_site_url() Site URL for the "main" site of the current Multisite network. 
  1038. */ 
  1039. do_action( 'add_option_jetpack_main_network_site', 'jetpack_main_network_site', network_site_url() ); 
  1040. /** 
  1041. * Fires when the site URL is updated. 
  1042. * Determines if the is part of a multi network. 
  1043. * @since 3.3.0 
  1044. * @param string jetpack_is_main_network. 
  1045. * @param bool Jetpack::is_multi_network() Is the site part of a multi network. 
  1046. */ 
  1047. do_action( 'add_option_jetpack_is_main_network', 'jetpack_is_main_network', (string) (bool) Jetpack::is_multi_network() ); 
  1048. /** 
  1049. * Fires when the site URL is updated. 
  1050. * Determines if the site is part of a multisite network. 
  1051. * @since 3.4.0 
  1052. * @param string jetpack_is_multi_site. 
  1053. * @param bool is_multisite() Is the site part of a mutlisite network. 
  1054. */ 
  1055. do_action( 'add_option_jetpack_is_multi_site', 'jetpack_is_multi_site', (string) (bool) is_multisite() ); 
  1056. /** 
  1057. * Triggered after a user updates the network settings via Network Settings Admin Page 
  1058. */ 
  1059. function update_jetpack_network_settings() { 
  1060. // Only sync this info for the main network site. 
  1061. do_action( 'add_option_jetpack_network_name', 'jetpack_network_name', Jetpack::network_name() ); 
  1062. do_action( 'add_option_jetpack_network_allow_new_registrations', 'jetpack_network_allow_new_registrations', Jetpack::network_allow_new_registrations() ); 
  1063. do_action( 'add_option_jetpack_network_add_new_users', 'jetpack_network_add_new_users', Jetpack::network_add_new_users() ); 
  1064. do_action( 'add_option_jetpack_network_site_upload_space', 'jetpack_network_site_upload_space', Jetpack::network_site_upload_space() ); 
  1065. do_action( 'add_option_jetpack_network_upload_file_types', 'jetpack_network_upload_file_types', Jetpack::network_upload_file_types() ); 
  1066. do_action( 'add_option_jetpack_network_enable_administration_menus', 'jetpack_network_enable_administration_menus', Jetpack::network_enable_administration_menus() ); 
  1067.  
  1068.  
  1069. /** 
  1070. * Get back if the current site is single user site. 
  1071. * @return bool 
  1072. */ 
  1073. public static function is_single_user_site() { 
  1074.  
  1075. $user_query = new WP_User_Query( array( 
  1076. 'blog_id' => get_current_blog_id(),  
  1077. 'fields' => 'ID',  
  1078. 'number' => 2 
  1079. ) ); 
  1080. return 1 === (int) $user_query->get_total(); 
  1081.  
  1082. /** 
  1083. * Returns true if the site has file write access false otherwise. 
  1084. * @return string ( '1' | '0' ) 
  1085. **/ 
  1086. public static function file_system_write_access() { 
  1087. if ( ! function_exists( 'get_filesystem_method' ) ) { 
  1088. require_once( ABSPATH . 'wp-admin/includes/file.php' ); 
  1089.  
  1090. require_once( ABSPATH . 'wp-admin/includes/template.php' ); 
  1091.  
  1092. $filesystem_method = get_filesystem_method(); 
  1093. if ( $filesystem_method === 'direct' ) { 
  1094. return 1; 
  1095.  
  1096. ob_start(); 
  1097. $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); 
  1098. ob_end_clean(); 
  1099. if ( $filesystem_credentials_are_stored ) { 
  1100. return 1; 
  1101. return 0; 
  1102.  
  1103. /** 
  1104. * Finds out if a site is using a version control system. 
  1105. * @return string ( '1' | '0' ) 
  1106. **/ 
  1107. public static function is_version_controlled() { 
  1108.  
  1109. if ( !class_exists( 'WP_Automatic_Updater' ) ) { 
  1110. require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  1111. $updater = new WP_Automatic_Updater(); 
  1112. $is_version_controlled = strval( $updater->is_vcs_checkout( $context = ABSPATH ) ); 
  1113. // transients should not be empty 
  1114. if ( empty( $is_version_controlled ) ) { 
  1115. $is_version_controlled = '0'; 
  1116. return $is_version_controlled; 
  1117.  
  1118. /** 
  1119. * Determines whether the current theme supports featured images or not. 
  1120. * @return string ( '1' | '0' ) 
  1121. */ 
  1122. public static function featured_images_enabled() { 
  1123. return current_theme_supports( 'post-thumbnails' ) ? '1' : '0'; 
  1124.  
  1125. /** 
  1126. * Sync back wp_version 
  1127. */ 
  1128. public static function get_wp_version() { 
  1129. global $wp_version; 
  1130. return $wp_version; 
  1131.  
  1132. /** 
  1133. * Keeps wp_version in sync with .com when WordPress core updates 
  1134. **/ 
  1135. public static function update_get_wp_version( $update, $meta_data ) { 
  1136. if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) { 
  1137. /** This action is documented in wp-includes/option.php */ 
  1138. /** 
  1139. * This triggers the sync for the jetpack version 
  1140. * See Jetpack_Sync options method for more info. 
  1141. */ 
  1142. do_action( 'add_option_jetpack_wp_version', 'jetpack_wp_version', (string) Jetpack::get_wp_version() ); 
  1143.  
  1144. /** 
  1145. * Triggers a sync of update counts and update details 
  1146. */ 
  1147. function sync_update_data() { 
  1148. // Anytime WordPress saves update data, we'll want to sync update data 
  1149. add_action( 'set_site_transient_update_plugins', array( 'Jetpack', 'refresh_update_data' ) ); 
  1150. add_action( 'set_site_transient_update_themes', array( 'Jetpack', 'refresh_update_data' ) ); 
  1151. add_action( 'set_site_transient_update_core', array( 'Jetpack', 'refresh_update_data' ) ); 
  1152. // Anytime a connection to jetpack is made, sync the update data 
  1153. add_action( 'jetpack_site_registered', array( 'Jetpack', 'refresh_update_data' ) ); 
  1154. // Anytime the Jetpack Version changes, sync the the update data 
  1155. add_action( 'updating_jetpack_version', array( 'Jetpack', 'refresh_update_data' ) ); 
  1156.  
  1157. if ( current_user_can( 'update_core' ) && current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' ) ) { 
  1158. $this->sync->mock_option( 'updates', array( 'Jetpack', 'get_updates' ) ); 
  1159.  
  1160. $this->sync->mock_option( 'update_details', array( 'Jetpack', 'get_update_details' ) ); 
  1161.  
  1162. /** 
  1163. * Triggers a sync of information specific to the current theme. 
  1164. */ 
  1165. function sync_theme_data() { 
  1166. add_action( 'switch_theme', array( 'Jetpack', 'refresh_theme_data' ) ); 
  1167. $this->sync->mock_option( 'featured_images_enabled', array( 'Jetpack', 'featured_images_enabled' ) ); 
  1168.  
  1169. /** 
  1170. * jetpack_updates is saved in the following schema: 
  1171. * array ( 
  1172. * 'plugins' => (int) Number of plugin updates available. 
  1173. * 'themes' => (int) Number of theme updates available. 
  1174. * 'wordpress' => (int) Number of WordPress core updates available. 
  1175. * 'translations' => (int) Number of translation updates available. 
  1176. * 'total' => (int) Total of all available updates. 
  1177. * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed. 
  1178. * ) 
  1179. * @return array 
  1180. */ 
  1181. public static function get_updates() { 
  1182. $update_data = wp_get_update_data(); 
  1183.  
  1184. // Stores the individual update counts as well as the total count. 
  1185. if ( isset( $update_data['counts'] ) ) { 
  1186. $updates = $update_data['counts']; 
  1187.  
  1188. // If we need to update WordPress core, let's find the latest version number. 
  1189. if ( ! empty( $updates['wordpress'] ) ) { 
  1190. $cur = get_preferred_from_update_core(); 
  1191. if ( isset( $cur->response ) && 'upgrade' === $cur->response ) { 
  1192. $updates['wp_update_version'] = $cur->current; 
  1193. return isset( $updates ) ? $updates : array(); 
  1194.  
  1195. public static function get_update_details() { 
  1196. $update_details = array( 
  1197. 'update_core' => get_site_transient( 'update_core' ),  
  1198. 'update_plugins' => get_site_transient( 'update_plugins' ),  
  1199. 'update_themes' => get_site_transient( 'update_themes' ),  
  1200. ); 
  1201. return $update_details; 
  1202.  
  1203. public static function refresh_update_data() { 
  1204. if ( current_user_can( 'update_core' ) && current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' ) ) { 
  1205. /** 
  1206. * Fires whenever the amount of updates needed for a site changes. 
  1207. * Syncs an array that includes the number of theme, plugin, and core updates available, as well as the latest core version available. 
  1208. * @since 3.7.0 
  1209. * @param string jetpack_updates 
  1210. * @param array Update counts calculated by Jetpack::get_updates 
  1211. */ 
  1212. do_action( 'add_option_jetpack_updates', 'jetpack_updates', Jetpack::get_updates() ); 
  1213. /** 
  1214. * Fires whenever the amount of updates needed for a site changes. 
  1215. * Syncs an array of core, theme, and plugin data, and which of each is out of date 
  1216. * @since 3.7.0 
  1217. * @param string jetpack_update_details 
  1218. * @param array Update details calculated by Jetpack::get_update_details 
  1219. */ 
  1220. do_action( 'add_option_jetpack_update_details', 'jetpack_update_details', Jetpack::get_update_details() ); 
  1221.  
  1222. public static function refresh_theme_data() { 
  1223. /** 
  1224. * Fires whenever a theme change is made. 
  1225. * @since 3.8.1 
  1226. * @param string featured_images_enabled 
  1227. * @param boolean Whether featured images are enabled or not 
  1228. */ 
  1229. do_action( 'add_option_jetpack_featured_images_enabled', 'jetpack_featured_images_enabled', Jetpack::featured_images_enabled() ); 
  1230.  
  1231. /** 
  1232. * Invalides the transient as well as triggers the update of the mock option. 
  1233. * @return null 
  1234. */ 
  1235. function is_single_user_site_invalidate() { 
  1236. /** 
  1237. * Fires when a user is added or removed from a site. 
  1238. * Determines if the site is a single user site. 
  1239. * @since 3.4.0 
  1240. * @param string jetpack_single_user_site. 
  1241. * @param bool Jetpack::is_single_user_site() Is the current site a single user site. 
  1242. */ 
  1243. do_action( 'update_option_jetpack_single_user_site', 'jetpack_single_user_site', (bool) Jetpack::is_single_user_site() ); 
  1244.  
  1245. /** 
  1246. * Is Jetpack active? 
  1247. */ 
  1248. public static function is_active() { 
  1249. return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); 
  1250.  
  1251. /** 
  1252. * Is Jetpack in development (offline) mode? 
  1253. */ 
  1254. public static function is_development_mode() { 
  1255. $development_mode = false; 
  1256.  
  1257. if ( defined( 'JETPACK_DEV_DEBUG' ) ) { 
  1258. $development_mode = JETPACK_DEV_DEBUG; 
  1259.  
  1260. elseif ( site_url() && false === strpos( site_url(), '.' ) ) { 
  1261. $development_mode = true; 
  1262. /** 
  1263. * Filters Jetpack's development mode. 
  1264. * @see http://jetpack.me/support/development-mode/ 
  1265. * @since 2.2.1 
  1266. * @param bool $development_mode Is Jetpack's development mode active. 
  1267. */ 
  1268. return apply_filters( 'jetpack_development_mode', $development_mode ); 
  1269.  
  1270. /** 
  1271. * Get Jetpack development mode notice text and notice class. 
  1272. * Mirrors the checks made in Jetpack::is_development_mode 
  1273. */ 
  1274. public static function show_development_mode_notice() { 
  1275. if ( Jetpack::is_development_mode() ) { 
  1276. if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) { 
  1277. $notice = sprintf( 
  1278. /** translators: %s is a URL */ 
  1279. __( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ),  
  1280. 'http://jetpack.me/support/development-mode/' 
  1281. ); 
  1282. } elseif ( site_url() && false === strpos( site_url(), '.' ) ) { 
  1283. $notice = sprintf( 
  1284. /** translators: %s is a URL */ 
  1285. __( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ),  
  1286. 'http://jetpack.me/support/development-mode/' 
  1287. ); 
  1288. } else { 
  1289. $notice = sprintf( 
  1290. /** translators: %s is a URL */ 
  1291. __( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ),  
  1292. 'http://jetpack.me/support/development-mode/' 
  1293. ); 
  1294.  
  1295. echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; 
  1296.  
  1297. // Throw up a notice if using a development version and as for feedback. 
  1298. if ( Jetpack::is_development_version() ) { 
  1299. /** translators: %s is a URL */ 
  1300. $notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), 'https://jetpack.me/contact-support/beta-group/' ); 
  1301.  
  1302. echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; 
  1303.  
  1304. /** 
  1305. * Whether Jetpack's version maps to a public release, or a development version. 
  1306. */ 
  1307. public static function is_development_version() { 
  1308. return ! preg_match( '/^\d+(\.\d+)+$/', JETPACK__VERSION ); 
  1309.  
  1310. /** 
  1311. * Is a given user (or the current user if none is specified) linked to a WordPress.com user? 
  1312. */ 
  1313. public static function is_user_connected( $user_id = false ) { 
  1314. $user_id = false === $user_id ? get_current_user_id() : absint( $user_id ); 
  1315. if ( ! $user_id ) { 
  1316. return false; 
  1317. return (bool) Jetpack_Data::get_access_token( $user_id ); 
  1318.  
  1319. /** 
  1320. * Get the wpcom user data of the current|specified connected user. 
  1321. */ 
  1322. public static function get_connected_user_data( $user_id = null ) { 
  1323. if ( ! $user_id ) { 
  1324. $user_id = get_current_user_id(); 
  1325. Jetpack::load_xml_rpc_client(); 
  1326. $xml = new Jetpack_IXR_Client( array( 
  1327. 'user_id' => $user_id,  
  1328. ) ); 
  1329. $xml->query( 'wpcom.getUser' ); 
  1330. if ( ! $xml->isError() ) { 
  1331. return $xml->getResponse(); 
  1332. return false; 
  1333.  
  1334. /** 
  1335. * Get the wpcom email of the current|specified connected user. 
  1336. */ 
  1337. public static function get_connected_user_email( $user_id = null ) { 
  1338. if ( ! $user_id ) { 
  1339. $user_id = get_current_user_id(); 
  1340. Jetpack::load_xml_rpc_client(); 
  1341. $xml = new Jetpack_IXR_Client( array( 
  1342. 'user_id' => $user_id,  
  1343. ) ); 
  1344. $xml->query( 'wpcom.getUserEmail' ); 
  1345. if ( ! $xml->isError() ) { 
  1346. return $xml->getResponse(); 
  1347. return false; 
  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. return ''; 
  1357.  
  1358. function current_user_is_connection_owner() { 
  1359. $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); 
  1360. return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id; 
  1361.  
  1362. /** 
  1363. * Add any extra oEmbed providers that we know about and use on wpcom for feature parity. 
  1364. */ 
  1365. function extra_oembed_providers() { 
  1366. // Cloudup: https://dev.cloudup.com/#oembed 
  1367. wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' ); 
  1368. wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' ); 
  1369. wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true ); 
  1370.  
  1371. /** 
  1372. * Synchronize connected user role changes 
  1373. */ 
  1374. function user_role_change( $user_id ) { 
  1375. if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) { 
  1376. $current_user_id = get_current_user_id(); 
  1377. wp_set_current_user( $user_id ); 
  1378. $role = $this->translate_current_user_to_role(); 
  1379. $signed_role = $this->sign_role( $role ); 
  1380. wp_set_current_user( $current_user_id ); 
  1381.  
  1382. $master_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); 
  1383. $master_user_id = absint( $master_token->external_user_id ); 
  1384.  
  1385. if ( ! $master_user_id ) 
  1386. return; // this shouldn't happen 
  1387.  
  1388. Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role ); 
  1389. //@todo retry on failure 
  1390.  
  1391. //try to choose a new master if we're demoting the current one 
  1392. if ( $user_id == $master_user_id && 'administrator' != $role ) { 
  1393. $query = new WP_User_Query( 
  1394. array( 
  1395. 'fields' => array( 'id' ),  
  1396. 'role' => 'administrator',  
  1397. 'orderby' => 'id',  
  1398. 'exclude' => array( $master_user_id ),  
  1399. ); 
  1400. $new_master = false; 
  1401. foreach ( $query->results as $result ) { 
  1402. $uid = absint( $result->id ); 
  1403. if ( $uid && Jetpack::is_user_connected( $uid ) ) { 
  1404. $new_master = $uid; 
  1405. break; 
  1406.  
  1407. if ( $new_master ) { 
  1408. Jetpack_Options::update_option( 'master_user', $new_master ); 
  1409. // else disconnect..? 
  1410.  
  1411. /** 
  1412. * Loads the currently active modules. 
  1413. */ 
  1414. public static function load_modules() { 
  1415. if ( ! self::is_active() && !self::is_development_mode() ) { 
  1416. if ( ! is_multisite() || ! get_site_option( 'jetpack_protect_active' ) ) { 
  1417. return; 
  1418.  
  1419. $version = Jetpack_Options::get_option( 'version' ); 
  1420. if ( ! $version ) { 
  1421. $version = $old_version = JETPACK__VERSION . ':' . time(); 
  1422. /** This action is documented in class.jetpack.php */ 
  1423. do_action( 'updating_jetpack_version', $version, false ); 
  1424. Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); 
  1425. list( $version ) = explode( ':', $version ); 
  1426.  
  1427. $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) ); 
  1428.  
  1429. $modules_data = array(); 
  1430.  
  1431. // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check. 
  1432. if ( version_compare( $version, JETPACK__VERSION, '<' ) ) { 
  1433. $updated_modules = array(); 
  1434. foreach ( $modules as $module ) { 
  1435. $modules_data[ $module ] = Jetpack::get_module( $module ); 
  1436. if ( ! isset( $modules_data[ $module ]['changed'] ) ) { 
  1437. continue; 
  1438.  
  1439. if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) { 
  1440. continue; 
  1441.  
  1442. $updated_modules[] = $module; 
  1443.  
  1444. $modules = array_diff( $modules, $updated_modules ); 
  1445.  
  1446. $is_development_mode = Jetpack::is_development_mode(); 
  1447.  
  1448. foreach ( $modules as $module ) { 
  1449. // If we're in dev mode, disable modules requiring a connection 
  1450. if ( $is_development_mode ) { 
  1451. // Prime the pump if we need to 
  1452. if ( empty( $modules_data[ $module ] ) ) { 
  1453. $modules_data[ $module ] = Jetpack::get_module( $module ); 
  1454. // If the module requires a connection, but we're in local mode, don't include it. 
  1455. if ( $modules_data[ $module ]['requires_connection'] ) { 
  1456. continue; 
  1457.  
  1458. if ( did_action( 'jetpack_module_loaded_' . $module ) ) { 
  1459. continue; 
  1460.  
  1461. require Jetpack::get_module_path( $module ); 
  1462. /** 
  1463. * Fires when a specific module is loaded. 
  1464. * The dynamic part of the hook, $module, is the module slug. 
  1465. * @since 1.1.0 
  1466. */ 
  1467. do_action( 'jetpack_module_loaded_' . $module ); 
  1468.  
  1469. /** 
  1470. * Fires when all the modules are loaded. 
  1471. * @since 1.1.0 
  1472. */ 
  1473. do_action( 'jetpack_modules_loaded' ); 
  1474.  
  1475. // 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. 
  1476. if ( Jetpack::is_active() || Jetpack::is_development_mode() ) 
  1477. require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' ); 
  1478.  
  1479. /** 
  1480. * Check if Jetpack's REST API compat file should be included 
  1481. * @action plugins_loaded 
  1482. * @return null 
  1483. */ 
  1484. public function check_rest_api_compat() { 
  1485. /** 
  1486. * Filters the list of REST API compat files to be included. 
  1487. * @since 2.2.5 
  1488. * @param array $args Array of REST API compat files to include. 
  1489. */ 
  1490. $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() ); 
  1491.  
  1492. if ( function_exists( 'bbpress' ) ) 
  1493. $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php'; 
  1494.  
  1495. foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) 
  1496. require_once $_jetpack_rest_api_compat_include; 
  1497.  
  1498. /** 
  1499. * Gets all plugins currently active in values, regardless of whether they're 
  1500. * traditionally activated or network activated. 
  1501. * @todo Store the result in core's object cache maybe? 
  1502. */ 
  1503. public static function get_active_plugins() { 
  1504. $active_plugins = (array) get_option( 'active_plugins', array() ); 
  1505.  
  1506. if ( is_multisite() ) { 
  1507. // Due to legacy code, active_sitewide_plugins stores them in the keys,  
  1508. // whereas active_plugins stores them in the values. 
  1509. $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); 
  1510. if ( $network_plugins ) { 
  1511. $active_plugins = array_merge( $active_plugins, $network_plugins ); 
  1512.  
  1513. sort( $active_plugins ); 
  1514.  
  1515. return array_unique( $active_plugins ); 
  1516.  
  1517. /** 
  1518. * Gets and parses additional plugin data to send with the heartbeat data 
  1519. * @since 3.8.1 
  1520. * @return array Array of plugin data 
  1521. */ 
  1522. public static function get_parsed_plugin_data() { 
  1523. if ( ! function_exists( 'get_plugins' ) ) { 
  1524. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  1525. $all_plugins = get_plugins(); 
  1526. $active_plugins = Jetpack::get_active_plugins(); 
  1527.  
  1528. $plugins = array(); 
  1529. foreach ( $all_plugins as $path => $plugin_data ) { 
  1530. $plugins[ $path ] = array( 
  1531. 'is_active' => in_array( $path, $active_plugins ),  
  1532. 'file' => $path,  
  1533. 'name' => $plugin_data['Name'],  
  1534. 'version' => $plugin_data['Version'],  
  1535. 'author' => $plugin_data['Author'],  
  1536. ); 
  1537.  
  1538. return $plugins; 
  1539.  
  1540. /** 
  1541. * Gets and parses theme data to send with the heartbeat data 
  1542. * @since 3.8.1 
  1543. * @return array Array of theme data 
  1544. */ 
  1545. public static function get_parsed_theme_data() { 
  1546. $all_themes = wp_get_themes( array( 'allowed' => true ) ); 
  1547. $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' ); 
  1548.  
  1549. $themes = array(); 
  1550. foreach ( $all_themes as $slug => $theme_data ) { 
  1551. $theme_headers = array(); 
  1552. foreach ( $header_keys as $header_key ) { 
  1553. $theme_headers[ $header_key ] = $theme_data->get( $header_key ); 
  1554.  
  1555. $themes[ $slug ] = array( 
  1556. 'is_active_theme' => $slug == wp_get_theme()->get_template(),  
  1557. 'slug' => $slug,  
  1558. 'theme_root' => $theme_data->get_theme_root_uri(),  
  1559. 'parent' => $theme_data->parent(),  
  1560. 'headers' => $theme_headers 
  1561. ); 
  1562.  
  1563. return $themes; 
  1564.  
  1565. /** 
  1566. * Checks whether a specific plugin is active. 
  1567. * We don't want to store these in a static variable, in case 
  1568. * there are switch_to_blog() calls involved. 
  1569. */ 
  1570. public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) { 
  1571. return in_array( $plugin, self::get_active_plugins() ); 
  1572.  
  1573. /** 
  1574. * Check if Jetpack's Open Graph tags should be used. 
  1575. * If certain plugins are active, Jetpack's og tags are suppressed. 
  1576. * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters 
  1577. * @action plugins_loaded 
  1578. * @return null 
  1579. */ 
  1580. public function check_open_graph() { 
  1581. if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) { 
  1582. add_filter( 'jetpack_enable_open_graph', '__return_true', 0 ); 
  1583.  
  1584. $active_plugins = self::get_active_plugins(); 
  1585.  
  1586. if ( ! empty( $active_plugins ) ) { 
  1587. foreach ( $this->open_graph_conflicting_plugins as $plugin ) { 
  1588. if ( in_array( $plugin, $active_plugins ) ) { 
  1589. add_filter( 'jetpack_enable_open_graph', '__return_false', 99 ); 
  1590. break; 
  1591.  
  1592. /** 
  1593. * Allow the addition of Open Graph Meta Tags to all pages. 
  1594. * @since 2.0.3 
  1595. * @param bool false Should Open Graph Meta tags be added. Default to false. 
  1596. */ 
  1597. if ( apply_filters( 'jetpack_enable_open_graph', false ) ) { 
  1598. require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php'; 
  1599.  
  1600. /** 
  1601. * Check if Jetpack's Twitter tags should be used. 
  1602. * If certain plugins are active, Jetpack's twitter tags are suppressed. 
  1603. * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters 
  1604. * @action plugins_loaded 
  1605. * @return null 
  1606. */ 
  1607. public function check_twitter_tags() { 
  1608.  
  1609. $active_plugins = self::get_active_plugins(); 
  1610.  
  1611. if ( ! empty( $active_plugins ) ) { 
  1612. foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) { 
  1613. if ( in_array( $plugin, $active_plugins ) ) { 
  1614. add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 ); 
  1615. break; 
  1616.  
  1617. /** 
  1618. * Allow Twitter Card Meta tags to be disabled. 
  1619. * @since 2.6.0 
  1620. * @param bool true Should Twitter Card Meta tags be disabled. Default to true. 
  1621. */ 
  1622. if ( apply_filters( 'jetpack_disable_twitter_cards', true ) ) { 
  1623. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php'; 
  1624.  
  1625.  
  1626.  
  1627.  
  1628. /** 
  1629. * Jetpack Security Reports 
  1630. * Allowed types: login_form, backup, file_scanning, spam 
  1631. * 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) 
  1632. * 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) 
  1633. * Example code to submit a security report: 
  1634. * function akismet_submit_jetpack_security_report() { 
  1635. * Jetpack::submit_security_report( 'spam', __FILE__, $args = array( 'blocked' => 138284, status => 'ok' ) ); 
  1636. * } 
  1637. * add_action( 'jetpack_security_report', 'akismet_submit_jetpack_security_report' ); 
  1638. */ 
  1639.  
  1640.  
  1641. /** 
  1642. * Calls for security report submissions. 
  1643. * @return null 
  1644. */ 
  1645. public static function perform_security_reporting() { 
  1646. $no_check_needed = get_site_transient( 'security_report_performed_recently' ); 
  1647.  
  1648. if ( $no_check_needed ) { 
  1649. return; 
  1650.  
  1651. /** 
  1652. * Fires before a security report is created. 
  1653. * @since 3.4.0 
  1654. */ 
  1655. do_action( 'jetpack_security_report' ); 
  1656.  
  1657. Jetpack_Options::update_option( 'security_report', self::$security_report ); 
  1658. set_site_transient( 'security_report_performed_recently', 1, 15 * MINUTE_IN_SECONDS ); 
  1659.  
  1660. /** 
  1661. * Allows plugins to submit security reports. 
  1662. * @param string $type Report type (login_form, backup, file_scanning, spam) 
  1663. * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data 
  1664. * @param array $args See definitions above 
  1665. */ 
  1666. public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) { 
  1667.  
  1668. if( !doing_action( 'jetpack_security_report' ) ) { 
  1669. return new WP_Error( 'not_collecting_report', 'Not currently collecting security reports. Please use the jetpack_security_report hook.' ); 
  1670.  
  1671. if( !is_string( $type ) || !is_string( $plugin_file ) ) { 
  1672. return new WP_Error( 'invalid_security_report', 'Invalid Security Report' ); 
  1673.  
  1674. if( !function_exists( 'get_plugin_data' ) ) { 
  1675. include( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  1676.  
  1677. //Get rid of any non-allowed args 
  1678. $args = array_intersect_key( $args, array_flip( array( 'blocked', 'last', 'next', 'status', 'message' ) ) ); 
  1679.  
  1680. $plugin = get_plugin_data( $plugin_file ); 
  1681.  
  1682. if ( !$plugin['Name'] ) { 
  1683. return new WP_Error( 'security_report_missing_plugin_name', 'Invalid Plugin File Provided' ); 
  1684.  
  1685. // Sanitize everything to make sure we're not syncing something wonky 
  1686. $type = sanitize_key( $type ); 
  1687.  
  1688. $args['plugin'] = $plugin; 
  1689.  
  1690. // Cast blocked, last and next as integers. 
  1691. // Last and next should be in unix timestamp format 
  1692. if ( isset( $args['blocked'] ) ) { 
  1693. $args['blocked'] = (int) $args['blocked']; 
  1694. if ( isset( $args['last'] ) ) { 
  1695. $args['last'] = (int) $args['last']; 
  1696. if ( isset( $args['next'] ) ) { 
  1697. $args['next'] = (int) $args['next']; 
  1698. if ( !in_array( $args['status'], array( 'ok', 'warning', 'error' ) ) ) { 
  1699. $args['status'] = 'ok'; 
  1700. if ( isset( $args['message'] ) ) { 
  1701.  
  1702. if( $args['status'] == 'ok' ) { 
  1703. unset( $args['message'] ); 
  1704.  
  1705. $allowed_html = array( 
  1706. 'a' => array( 
  1707. 'href' => array(),  
  1708. 'title' => array() 
  1709. ),  
  1710. 'em' => array(),  
  1711. 'strong' => array(),  
  1712. ); 
  1713.  
  1714. $args['message'] = wp_kses( $args['message'], $allowed_html ); 
  1715.  
  1716. $plugin_name = $plugin[ 'Name' ]; 
  1717.  
  1718. self::$security_report[ $type ][ $plugin_name ] = $args; 
  1719.  
  1720. /** 
  1721. * Collects a new report if needed, then returns it. 
  1722. */ 
  1723. public function get_security_report() { 
  1724. self::perform_security_reporting(); 
  1725. return Jetpack_Options::get_option( 'security_report' ); 
  1726.  
  1727.  
  1728. /** Jetpack Options API */ 
  1729.  
  1730. public static function get_option_names( $type = 'compact' ) { 
  1731. return Jetpack_Options::get_option_names( $type ); 
  1732.  
  1733. /** 
  1734. * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. 
  1735. * @param string $name Option name 
  1736. * @param mixed $default (optional) 
  1737. */ 
  1738. public static function get_option( $name, $default = false ) { 
  1739. return Jetpack_Options::get_option( $name, $default ); 
  1740.  
  1741. /** 
  1742. * Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action 
  1743. * Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted 
  1744. * $name must be a registered option name. 
  1745. */ 
  1746. public static function create_nonce( $name ) { 
  1747. $secret = wp_generate_password( 32, false ) . ':' . wp_generate_password( 32, false ) . ':' . ( time() + 600 ); 
  1748.  
  1749. Jetpack_Options::update_option( $name, $secret ); 
  1750. @list( $secret_1, $secret_2, $eol ) = explode( ':', Jetpack_Options::get_option( $name ) ); 
  1751. if ( empty( $secret_1 ) || empty( $secret_2 ) || $eol < time() ) 
  1752. return new Jetpack_Error( 'missing_secrets' ); 
  1753.  
  1754. return array( 
  1755. 'secret_1' => $secret_1,  
  1756. 'secret_2' => $secret_2,  
  1757. 'eol' => $eol,  
  1758. ); 
  1759.  
  1760. /** 
  1761. * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. 
  1762. * @deprecated 3.4 use Jetpack_Options::update_option() instead. 
  1763. * @param string $name Option name 
  1764. * @param mixed $value Option value 
  1765. */ 
  1766. public static function update_option( $name, $value ) { 
  1767. _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' ); 
  1768. return Jetpack_Options::update_option( $name, $value ); 
  1769.  
  1770. /** 
  1771. * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. 
  1772. * @deprecated 3.4 use Jetpack_Options::update_options() instead. 
  1773. * @param array $array array( option name => option value, ... ) 
  1774. */ 
  1775. public static function update_options( $array ) { 
  1776. _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' ); 
  1777. return Jetpack_Options::update_options( $array ); 
  1778.  
  1779. /** 
  1780. * Deletes the given option. May be passed multiple option names as an array. 
  1781. * Updates jetpack_options and/or deletes jetpack_$name as appropriate. 
  1782. * @deprecated 3.4 use Jetpack_Options::delete_option() instead. 
  1783. * @param string|array $names 
  1784. */ 
  1785. public static function delete_option( $names ) { 
  1786. _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' ); 
  1787. return Jetpack_Options::delete_option( $names ); 
  1788.  
  1789. /** 
  1790. * Enters a user token into the user_tokens option 
  1791. * @param int $user_id 
  1792. * @param string $token 
  1793. * return bool 
  1794. */ 
  1795. public static function update_user_token( $user_id, $token, $is_master_user ) { 
  1796. // not designed for concurrent updates 
  1797. $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); 
  1798. if ( ! is_array( $user_tokens ) ) 
  1799. $user_tokens = array(); 
  1800. $user_tokens[$user_id] = $token; 
  1801. if ( $is_master_user ) { 
  1802. $master_user = $user_id; 
  1803. $options = compact( 'user_tokens', 'master_user' ); 
  1804. } else { 
  1805. $options = compact( 'user_tokens' ); 
  1806. return Jetpack_Options::update_options( $options ); 
  1807.  
  1808. /** 
  1809. * Returns an array of all PHP files in the specified absolute path. 
  1810. * Equivalent to glob( "$absolute_path/**.php" ). 
  1811. * @param string $absolute_path The absolute path of the directory to search. 
  1812. * @return array Array of absolute paths to the PHP files. 
  1813. */ 
  1814. public static function glob_php( $absolute_path ) { 
  1815. if ( function_exists( 'glob' ) ) { 
  1816. return glob( "$absolute_path/*.php" ); 
  1817.  
  1818. $absolute_path = untrailingslashit( $absolute_path ); 
  1819. $files = array(); 
  1820. if ( ! $dir = @opendir( $absolute_path ) ) { 
  1821. return $files; 
  1822.  
  1823. while ( false !== $file = readdir( $dir ) ) { 
  1824. if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) { 
  1825. continue; 
  1826.  
  1827. $file = "$absolute_path/$file"; 
  1828.  
  1829. if ( ! is_file( $file ) ) { 
  1830. continue; 
  1831.  
  1832. $files[] = $file; 
  1833.  
  1834. closedir( $dir ); 
  1835.  
  1836. return $files; 
  1837.  
  1838. public static function activate_new_modules( $redirect = false ) { 
  1839. if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { 
  1840. return; 
  1841.  
  1842. $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic] 
  1843. if ( ! $jetpack_old_version ) { 
  1844. $jetpack_old_version = $version = $old_version = '1.1:' . time(); 
  1845. /** This action is documented in class.jetpack.php */ 
  1846. do_action( 'updating_jetpack_version', $version, false ); 
  1847. Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); 
  1848.  
  1849. list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic] 
  1850.  
  1851. if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) { 
  1852. return; 
  1853.  
  1854. $active_modules = Jetpack::get_active_modules(); 
  1855. $reactivate_modules = array(); 
  1856. foreach ( $active_modules as $active_module ) { 
  1857. $module = Jetpack::get_module( $active_module ); 
  1858. if ( ! isset( $module['changed'] ) ) { 
  1859. continue; 
  1860.  
  1861. if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) { 
  1862. continue; 
  1863.  
  1864. $reactivate_modules[] = $active_module; 
  1865. Jetpack::deactivate_module( $active_module ); 
  1866.  
  1867. $new_version = JETPACK__VERSION . ':' . time(); 
  1868. /** This action is documented in class.jetpack.php */ 
  1869. do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version ); 
  1870. Jetpack_Options::update_options( 
  1871. array( 
  1872. 'version' => $new_version,  
  1873. 'old_version' => $jetpack_old_version,  
  1874. ); 
  1875.  
  1876. Jetpack::state( 'message', 'modules_activated' ); 
  1877. Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules ); 
  1878.  
  1879. if ( $redirect ) { 
  1880. $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page 
  1881. if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) { 
  1882. $page = $_GET['page']; 
  1883.  
  1884. wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) ); 
  1885. exit; 
  1886.  
  1887. /** 
  1888. * List available Jetpack modules. Simply lists .php files in /modules/. 
  1889. * Make sure to tuck away module "library" files in a sub-directory. 
  1890. */ 
  1891. public static function get_available_modules( $min_version = false, $max_version = false ) { 
  1892. static $modules = null; 
  1893.  
  1894. if ( ! isset( $modules ) ) { 
  1895. $available_modules_option = Jetpack_Options::get_option( 'available_modules', array() ); 
  1896. // Use the cache if we're on the front-end and it's available... 
  1897. if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) { 
  1898. $modules = $available_modules_option[ JETPACK__VERSION ]; 
  1899. } else { 
  1900. $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' ); 
  1901.  
  1902. $modules = array(); 
  1903.  
  1904. foreach ( $files as $file ) { 
  1905. if ( ! $headers = Jetpack::get_module( $file ) ) { 
  1906. continue; 
  1907.  
  1908. $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced']; 
  1909.  
  1910. Jetpack_Options::update_option( 'available_modules', array( 
  1911. JETPACK__VERSION => $modules,  
  1912. ) ); 
  1913.  
  1914. /** 
  1915. * Filters the array of modules available to be activated. 
  1916. * @since 2.4.0 
  1917. * @param array $modules Array of available modules. 
  1918. * @param string $min_version Minimum version number required to use modules. 
  1919. * @param string $max_version Maximum version number required to use modules. 
  1920. */ 
  1921. $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version ); 
  1922.  
  1923. if ( ! $min_version && ! $max_version ) { 
  1924. return array_keys( $mods ); 
  1925.  
  1926. $r = array(); 
  1927. foreach ( $mods as $slug => $introduced ) { 
  1928. if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) { 
  1929. continue; 
  1930.  
  1931. if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) { 
  1932. continue; 
  1933.  
  1934. $r[] = $slug; 
  1935.  
  1936. return $r; 
  1937.  
  1938. /** 
  1939. * Default modules loaded on activation. 
  1940. */ 
  1941. public static function get_default_modules( $min_version = false, $max_version = false ) { 
  1942. $return = array(); 
  1943.  
  1944. foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) { 
  1945. $module_data = Jetpack::get_module( $module ); 
  1946.  
  1947. switch ( strtolower( $module_data['auto_activate'] ) ) { 
  1948. case 'yes' : 
  1949. $return[] = $module; 
  1950. break; 
  1951. case 'public' : 
  1952. if ( Jetpack_Options::get_option( 'public' ) ) { 
  1953. $return[] = $module; 
  1954. break; 
  1955. case 'no' : 
  1956. default : 
  1957. break; 
  1958. /** 
  1959. * Filters the array of default modules. 
  1960. * @since 2.5.0 
  1961. * @param array $return Array of default modules. 
  1962. * @param string $min_version Minimum version number required to use modules. 
  1963. * @param string $max_version Maximum version number required to use modules. 
  1964. */ 
  1965. return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version ); 
  1966.  
  1967. /** 
  1968. * Checks activated modules during auto-activation to determine 
  1969. * if any of those modules are being deprecated. If so, close 
  1970. * them out, and add any replacement modules. 
  1971. * Runs at priority 99 by default. 
  1972. * This is run late, so that it can still activate a module if 
  1973. * the new module is a replacement for another that the user 
  1974. * currently has active, even if something at the normal priority 
  1975. * would kibosh everything. 
  1976. * @since 2.6 
  1977. * @uses jetpack_get_default_modules filter 
  1978. * @param array $modules 
  1979. * @return array 
  1980. */ 
  1981. function handle_deprecated_modules( $modules ) { 
  1982. $deprecated_modules = array( 
  1983. 'debug' => null, // Closed out and moved to ./class.jetpack-debugger.php 
  1984. 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality. 
  1985. 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support. 
  1986. ); 
  1987.  
  1988. // Don't activate SSO if they never completed activating WPCC. 
  1989. if ( Jetpack::is_module_active( 'wpcc' ) ) { 
  1990. $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' ); 
  1991. if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) { 
  1992. $deprecated_modules['wpcc'] = null; 
  1993.  
  1994. foreach ( $deprecated_modules as $module => $replacement ) { 
  1995. if ( Jetpack::is_module_active( $module ) ) { 
  1996. self::deactivate_module( $module ); 
  1997. if ( $replacement ) { 
  1998. $modules[] = $replacement; 
  1999.  
  2000. return array_unique( $modules ); 
  2001.  
  2002. /** 
  2003. * Checks activated plugins during auto-activation to determine 
  2004. * if any of those plugins are in the list with a corresponding module 
  2005. * that is not compatible with the plugin. The module will not be allowed 
  2006. * to auto-activate. 
  2007. * @since 2.6 
  2008. * @uses jetpack_get_default_modules filter 
  2009. * @param array $modules 
  2010. * @return array 
  2011. */ 
  2012. function filter_default_modules( $modules ) { 
  2013.  
  2014. $active_plugins = self::get_active_plugins(); 
  2015.  
  2016. if ( ! empty( $active_plugins ) ) { 
  2017.  
  2018. // For each module we'd like to auto-activate... 
  2019. foreach ( $modules as $key => $module ) { 
  2020. // If there are potential conflicts for it... 
  2021. if ( ! empty( $this->conflicting_plugins[ $module ] ) ) { 
  2022. // For each potential conflict... 
  2023. foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) { 
  2024. // If that conflicting plugin is active... 
  2025. if ( in_array( $plugin, $active_plugins ) ) { 
  2026. // Remove that item from being auto-activated. 
  2027. unset( $modules[ $key ] ); 
  2028.  
  2029. return $modules; 
  2030.  
  2031. /** 
  2032. * Extract a module's slug from its full path. 
  2033. */ 
  2034. public static function get_module_slug( $file ) { 
  2035. return str_replace( '.php', '', basename( $file ) ); 
  2036.  
  2037. /** 
  2038. * Generate a module's path from its slug. 
  2039. */ 
  2040. public static function get_module_path( $slug ) { 
  2041. return JETPACK__PLUGIN_DIR . "modules/$slug.php"; 
  2042.  
  2043. /** 
  2044. * Load module data from module file. Headers differ from WordPress 
  2045. * plugin headers to avoid them being identified as standalone 
  2046. * plugins on the WordPress plugins page. 
  2047. */ 
  2048. public static function get_module( $module ) { 
  2049. $headers = array( 
  2050. 'name' => 'Module Name',  
  2051. 'description' => 'Module Description',  
  2052. 'jumpstart_desc' => 'Jumpstart Description',  
  2053. 'sort' => 'Sort Order',  
  2054. 'recommendation_order' => 'Recommendation Order',  
  2055. 'introduced' => 'First Introduced',  
  2056. 'changed' => 'Major Changes In',  
  2057. 'deactivate' => 'Deactivate',  
  2058. 'free' => 'Free',  
  2059. 'requires_connection' => 'Requires Connection',  
  2060. 'auto_activate' => 'Auto Activate',  
  2061. 'module_tags' => 'Module Tags',  
  2062. 'feature' => 'Feature',  
  2063. 'additional_search_queries' => 'Additional Search Queries',  
  2064. ); 
  2065.  
  2066. $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) ); 
  2067.  
  2068. $mod = Jetpack::get_file_data( $file, $headers ); 
  2069. if ( empty( $mod['name'] ) ) { 
  2070. return false; 
  2071.  
  2072. $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; 
  2073. $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; 
  2074. $mod['deactivate'] = empty( $mod['deactivate'] ); 
  2075. $mod['free'] = empty( $mod['free'] ); 
  2076. $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true; 
  2077.  
  2078. if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) { 
  2079. $mod['auto_activate'] = 'No'; 
  2080. } else { 
  2081. $mod['auto_activate'] = (string) $mod['auto_activate']; 
  2082.  
  2083. if ( $mod['module_tags'] ) { 
  2084. $mod['module_tags'] = explode( ', ', $mod['module_tags'] ); 
  2085. $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] ); 
  2086. $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] ); 
  2087. } else { 
  2088. $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) ); 
  2089.  
  2090. if ( $mod['feature'] ) { 
  2091. $mod['feature'] = explode( ', ', $mod['feature'] ); 
  2092. $mod['feature'] = array_map( 'trim', $mod['feature'] ); 
  2093. } else { 
  2094. $mod['feature'] = array( self::translate_module_tag( 'Other' ) ); 
  2095.  
  2096. /** 
  2097. * Filters the feature array on a module. 
  2098. * This filter allows you to control where each module is filtered: Recommended,  
  2099. * Jumpstart, and the default "Other" listing. 
  2100. * @since 3.5.0 
  2101. * @param array $mod['feature'] The areas to feature this module: 
  2102. * 'Jumpstart' adds to the "Jumpstart" option to activate many modules at once. 
  2103. * 'Recommended' shows on the main Jetpack admin screen. 
  2104. * 'Other' should be the default if no other value is in the array. 
  2105. * @param string $module The slug of the module, e.g. sharedaddy. 
  2106. * @param array $mod All the currently assembled module data. 
  2107. */ 
  2108. $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod ); 
  2109.  
  2110. /** 
  2111. * Filter the returned data about a module. 
  2112. * This filter allows overriding any info about Jetpack modules. It is dangerous,  
  2113. * so please be careful. 
  2114. * @since 3.6.0 
  2115. * @param array $mod The details of the requested module. 
  2116. * @param string $module The slug of the module, e.g. sharedaddy 
  2117. * @param string $file The path to the module source file. 
  2118. */ 
  2119. return apply_filters( 'jetpack_get_module', $mod, $module, $file ); 
  2120.  
  2121. /** 
  2122. * Like core's get_file_data implementation, but caches the result. 
  2123. */ 
  2124. public static function get_file_data( $file, $headers ) { 
  2125. //Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated 
  2126. $file_name = basename( $file ); 
  2127. $file_data_option = Jetpack_Options::get_option( 'file_data', array() ); 
  2128. $key = md5( $file_name . serialize( $headers ) ); 
  2129. $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 ); 
  2130.  
  2131. // If we don't need to refresh the cache, and already have the value, short-circuit! 
  2132. if ( ! $refresh_cache && isset( $file_data_option[ JETPACK__VERSION ][ $key ] ) ) { 
  2133. return $file_data_option[ JETPACK__VERSION ][ $key ]; 
  2134.  
  2135. $data = get_file_data( $file, $headers ); 
  2136.  
  2137. // Strip out any old Jetpack versions that are cluttering the option. 
  2138. $file_data_option = array_intersect_key( (array) $file_data_option, array( JETPACK__VERSION => null ) ); 
  2139. $file_data_option[ JETPACK__VERSION ][ $key ] = $data; 
  2140. Jetpack_Options::update_option( 'file_data', $file_data_option ); 
  2141.  
  2142. return $data; 
  2143.  
  2144. public static function translate_module_tag( $untranslated_tag ) { 
  2145. // Tags are aggregated by tools/build-module-headings-translations.php 
  2146. // and output in modules/module-headings.php 
  2147. return _x( $untranslated_tag, 'Module Tag', 'jetpack' ); 
  2148.  
  2149. /** 
  2150. * Get a list of activated modules as an array of module slugs. 
  2151. */ 
  2152. public static function get_active_modules() { 
  2153. $active = Jetpack_Options::get_option( 'active_modules' ); 
  2154. if ( ! is_array( $active ) ) 
  2155. $active = array(); 
  2156. if ( is_admin() && ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) ) { 
  2157. $active[] = 'vaultpress'; 
  2158. } else { 
  2159. $active = array_diff( $active, array( 'vaultpress' ) ); 
  2160.  
  2161. //If protect is active on the main site of a multisite, it should be active on all sites. 
  2162. if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { 
  2163. $active[] = 'protect'; 
  2164.  
  2165. return array_unique( $active ); 
  2166.  
  2167. /** 
  2168. * Check whether or not a Jetpack module is active. 
  2169. * @param string $module The slug of a Jetpack module. 
  2170. * @return bool 
  2171. * @static 
  2172. */ 
  2173. public static function is_module_active( $module ) { 
  2174. return in_array( $module, self::get_active_modules() ); 
  2175.  
  2176. public static function is_module( $module ) { 
  2177. return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() ); 
  2178.  
  2179. /** 
  2180. * Catches PHP errors. Must be used in conjunction with output buffering. 
  2181. * @param bool $catch True to start catching, False to stop. 
  2182. * @static 
  2183. */ 
  2184. public static function catch_errors( $catch ) { 
  2185. static $display_errors, $error_reporting; 
  2186.  
  2187. if ( $catch ) { 
  2188. $display_errors = @ini_set( 'display_errors', 1 ); 
  2189. $error_reporting = @error_reporting( E_ALL ); 
  2190. add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); 
  2191. } else { 
  2192. @ini_set( 'display_errors', $display_errors ); 
  2193. @error_reporting( $error_reporting ); 
  2194. remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 ); 
  2195.  
  2196. /** 
  2197. * Saves any generated PHP errors in ::state( 'php_errors', {errors} ) 
  2198. */ 
  2199. public static function catch_errors_on_shutdown() { 
  2200. Jetpack::state( 'php_errors', ob_get_clean() ); 
  2201.  
  2202. public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array() ) { 
  2203. $jetpack = Jetpack::init(); 
  2204.  
  2205. $modules = Jetpack::get_default_modules( $min_version, $max_version ); 
  2206. $modules = array_merge( $other_modules, $modules ); 
  2207.  
  2208. // Look for standalone plugins and disable if active. 
  2209.  
  2210. $to_deactivate = array(); 
  2211. foreach ( $modules as $module ) { 
  2212. if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) { 
  2213. $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module]; 
  2214.  
  2215. $deactivated = array(); 
  2216. foreach ( $to_deactivate as $module => $deactivate_me ) { 
  2217. list( $probable_file, $probable_title ) = $deactivate_me; 
  2218. if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) { 
  2219. $deactivated[] = $module; 
  2220.  
  2221. if ( $deactivated ) { 
  2222. Jetpack::state( 'deactivated_plugins', join( ', ', $deactivated ) ); 
  2223.  
  2224. $url = add_query_arg( 
  2225. array( 
  2226. 'action' => 'activate_default_modules',  
  2227. '_wpnonce' => wp_create_nonce( 'activate_default_modules' ),  
  2228. ),  
  2229. add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) ) 
  2230. ); 
  2231. wp_safe_redirect( $url ); 
  2232. exit; 
  2233.  
  2234. /** 
  2235. * Fires before default modules are activated. 
  2236. * @since 1.9.0 
  2237. * @param string $min_version Minimum version number required to use modules. 
  2238. * @param string $max_version Maximum version number required to use modules. 
  2239. * @param array $other_modules Array of other modules to activate alongside the default modules. 
  2240. */ 
  2241. do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules ); 
  2242.  
  2243. // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating 
  2244. Jetpack::restate(); 
  2245. Jetpack::catch_errors( true ); 
  2246.  
  2247. $active = Jetpack::get_active_modules(); 
  2248.  
  2249. foreach ( $modules as $module ) { 
  2250. if ( did_action( "jetpack_module_loaded_$module" ) ) { 
  2251. $active[] = $module; 
  2252. Jetpack_Options::update_option( 'active_modules', array_unique( $active ) ); 
  2253. continue; 
  2254.  
  2255. if ( in_array( $module, $active ) ) { 
  2256. $module_info = Jetpack::get_module( $module ); 
  2257. if ( ! $module_info['deactivate'] ) { 
  2258. $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; 
  2259. if ( $active_state = Jetpack::state( $state ) ) { 
  2260. $active_state = explode( ', ', $active_state ); 
  2261. } else { 
  2262. $active_state = array(); 
  2263. $active_state[] = $module; 
  2264. Jetpack::state( $state, implode( ', ', $active_state ) ); 
  2265. continue; 
  2266.  
  2267. $file = Jetpack::get_module_path( $module ); 
  2268. if ( ! file_exists( $file ) ) { 
  2269. continue; 
  2270.  
  2271. // we'll override this later if the plugin can be included without fatal error 
  2272. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); 
  2273. Jetpack::state( 'error', 'module_activation_failed' ); 
  2274. Jetpack::state( 'module', $module ); 
  2275. ob_start(); 
  2276. require $file; 
  2277. /** 
  2278. * Fires when a specific module is activated. 
  2279. * @since 1.9.0 
  2280. * @param string $module Module slug. 
  2281. */ 
  2282. do_action( 'jetpack_activate_module', $module ); 
  2283. $active[] = $module; 
  2284. $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules'; 
  2285. if ( $active_state = Jetpack::state( $state ) ) { 
  2286. $active_state = explode( ', ', $active_state ); 
  2287. } else { 
  2288. $active_state = array(); 
  2289. $active_state[] = $module; 
  2290. Jetpack::state( $state, implode( ', ', $active_state ) ); 
  2291. Jetpack_Options::update_option( 'active_modules', array_unique( $active ) ); 
  2292. ob_end_clean(); 
  2293. Jetpack::state( 'error', false ); 
  2294. Jetpack::state( 'module', false ); 
  2295. Jetpack::catch_errors( false ); 
  2296. /** 
  2297. * Fires when default modules are activated. 
  2298. * @since 1.9.0 
  2299. * @param string $min_version Minimum version number required to use modules. 
  2300. * @param string $max_version Maximum version number required to use modules. 
  2301. * @param array $other_modules Array of other modules to activate alongside the default modules. 
  2302. */ 
  2303. do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules ); 
  2304.  
  2305. public static function activate_module( $module, $exit = true, $redirect = true ) { 
  2306. /** 
  2307. * Fires before a module is activated. 
  2308. * @since 2.6.0 
  2309. * @param string $module Module slug. 
  2310. * @param bool $exit Should we exit after the module has been activated. Default to true. 
  2311. * @param bool $redirect Should the user be redirected after module activation? Default to true. 
  2312. */ 
  2313. do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect ); 
  2314.  
  2315. $jetpack = Jetpack::init(); 
  2316.  
  2317. if ( ! strlen( $module ) ) 
  2318. return false; 
  2319.  
  2320. if ( ! Jetpack::is_module( $module ) ) 
  2321. return false; 
  2322.  
  2323. // If it's already active, then don't do it again 
  2324. $active = Jetpack::get_active_modules(); 
  2325. foreach ( $active as $act ) { 
  2326. if ( $act == $module ) 
  2327. return true; 
  2328.  
  2329. $module_data = Jetpack::get_module( $module ); 
  2330.  
  2331. if ( ! Jetpack::is_active() ) { 
  2332. if ( !Jetpack::is_development_mode() ) 
  2333. return false; 
  2334.  
  2335. // If we're not connected but in development mode, make sure the module doesn't require a connection 
  2336. if ( Jetpack::is_development_mode() && $module_data['requires_connection'] ) 
  2337. return false; 
  2338.  
  2339. // Check and see if the old plugin is active 
  2340. if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { 
  2341. // Deactivate the old plugin 
  2342. if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) { 
  2343. // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module 
  2344. // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. 
  2345. Jetpack::state( 'deactivated_plugins', $module ); 
  2346. wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); 
  2347. exit; 
  2348.  
  2349. // Check the file for fatal errors, a la wp-admin/plugins.php::activate 
  2350. Jetpack::state( 'module', $module ); 
  2351. Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error 
  2352.  
  2353. Jetpack::catch_errors( true ); 
  2354. ob_start(); 
  2355. require Jetpack::get_module_path( $module ); 
  2356. /** This action is documented in class.jetpack.php */ 
  2357. do_action( 'jetpack_activate_module', $module ); 
  2358. $active[] = $module; 
  2359. Jetpack_Options::update_option( 'active_modules', array_unique( $active ) ); 
  2360. Jetpack::state( 'error', false ); // the override 
  2361. Jetpack::state( 'message', 'module_activated' ); 
  2362. Jetpack::state( 'module', $module ); 
  2363. ob_end_clean(); 
  2364. Jetpack::catch_errors( false ); 
  2365.  
  2366. // A flag for Jump Start so it's not shown again. Only set if it hasn't been yet. 
  2367. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { 
  2368. Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); 
  2369.  
  2370. //Jump start is being dismissed send data to MC Stats 
  2371. $jetpack->stat( 'jumpstart', 'manual, '.$module ); 
  2372.  
  2373. $jetpack->do_stats( 'server_side' ); 
  2374.  
  2375. if ( $redirect ) { 
  2376. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); 
  2377. if ( $exit ) { 
  2378. exit; 
  2379.  
  2380. function activate_module_actions( $module ) { 
  2381. /** 
  2382. * Fires when a module is activated. 
  2383. * The dynamic part of the filter, $module, is the module slug. 
  2384. * @since 1.9.0 
  2385. * @param string $module Module slug. 
  2386. */ 
  2387. do_action( "jetpack_activate_module_$module", $module ); 
  2388.  
  2389. $this->sync->sync_all_module_options( $module ); 
  2390.  
  2391. public static function deactivate_module( $module ) { 
  2392. /** 
  2393. * Fires when a module is deactivated. 
  2394. * @since 1.9.0 
  2395. * @param string $module Module slug. 
  2396. */ 
  2397. do_action( 'jetpack_pre_deactivate_module', $module ); 
  2398.  
  2399. $jetpack = Jetpack::init(); 
  2400.  
  2401. $active = Jetpack::get_active_modules(); 
  2402. $new = array_filter( array_diff( $active, (array) $module ) ); 
  2403.  
  2404. /** 
  2405. * Fires when a module is deactivated. 
  2406. * The dynamic part of the filter, $module, is the module slug. 
  2407. * @since 1.9.0 
  2408. * @param string $module Module slug. 
  2409. */ 
  2410. do_action( "jetpack_deactivate_module_$module", $module ); 
  2411.  
  2412. // A flag for Jump Start so it's not shown again. 
  2413. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) { 
  2414. Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); 
  2415.  
  2416. //Jump start is being dismissed send data to MC Stats 
  2417. $jetpack->stat( 'jumpstart', 'manual, deactivated-'.$module ); 
  2418.  
  2419. $jetpack->do_stats( 'server_side' ); 
  2420.  
  2421. return Jetpack_Options::update_option( 'active_modules', array_unique( $new ) ); 
  2422.  
  2423. public static function enable_module_configurable( $module ) { 
  2424. $module = Jetpack::get_module_slug( $module ); 
  2425. add_filter( 'jetpack_module_configurable_' . $module, '__return_true' ); 
  2426.  
  2427. public static function module_configuration_url( $module ) { 
  2428. $module = Jetpack::get_module_slug( $module ); 
  2429. return Jetpack::admin_url( array( 'page' => 'jetpack', 'configure' => $module ) ); 
  2430.  
  2431. public static function module_configuration_load( $module, $method ) { 
  2432. $module = Jetpack::get_module_slug( $module ); 
  2433. add_action( 'jetpack_module_configuration_load_' . $module, $method ); 
  2434.  
  2435. public static function module_configuration_head( $module, $method ) { 
  2436. $module = Jetpack::get_module_slug( $module ); 
  2437. add_action( 'jetpack_module_configuration_head_' . $module, $method ); 
  2438.  
  2439. public static function module_configuration_screen( $module, $method ) { 
  2440. $module = Jetpack::get_module_slug( $module ); 
  2441. add_action( 'jetpack_module_configuration_screen_' . $module, $method ); 
  2442.  
  2443. public static function module_configuration_activation_screen( $module, $method ) { 
  2444. $module = Jetpack::get_module_slug( $module ); 
  2445. add_action( 'display_activate_module_setting_' . $module, $method ); 
  2446.  
  2447. /** Installation */ 
  2448.  
  2449. public static function bail_on_activation( $message, $deactivate = true ) { 
  2450. ?> 
  2451. <!doctype html> 
  2452. <html> 
  2453. <head> 
  2454. <meta charset="<?php bloginfo( 'charset' ); ?>"> 
  2455. <style> 
  2456. * { 
  2457. text-align: center; 
  2458. margin: 0; 
  2459. padding: 0; 
  2460. font-family: "Lucida Grande", Verdana, Arial, "Bitstream Vera Sans", sans-serif; 
  2461. p { 
  2462. margin-top: 1em; 
  2463. font-size: 18px; 
  2464. </style> 
  2465. <body> 
  2466. <p><?php echo esc_html( $message ); ?></p> 
  2467. </body> 
  2468. </html> 
  2469. <?php 
  2470. if ( $deactivate ) { 
  2471. $plugins = get_option( 'active_plugins' ); 
  2472. $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ); 
  2473. $update = false; 
  2474. foreach ( $plugins as $i => $plugin ) { 
  2475. if ( $plugin === $jetpack ) { 
  2476. $plugins[$i] = false; 
  2477. $update = true; 
  2478.  
  2479. if ( $update ) { 
  2480. update_option( 'active_plugins', array_filter( $plugins ) ); 
  2481. exit; 
  2482.  
  2483. /** 
  2484. * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() 
  2485. * @static 
  2486. */ 
  2487. public static function plugin_activation( $network_wide ) { 
  2488. Jetpack_Options::update_option( 'activated', 1 ); 
  2489.  
  2490. if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) { 
  2491. Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) ); 
  2492.  
  2493. if ( $network_wide ) 
  2494. Jetpack::state( 'network_nag', true ); 
  2495.  
  2496. Jetpack::plugin_initialize(); 
  2497. /** 
  2498. * Runs before bumping version numbers up to a new version 
  2499. * @param (string) $version Version:timestamp 
  2500. * @param (string) $old_version Old Version:timestamp or false if not set yet. 
  2501. * @return null [description] 
  2502. */ 
  2503. public static function do_version_bump( $version, $old_version ) { 
  2504.  
  2505. if ( ! $old_version ) { // For new sites 
  2506. // Setting up jetpack manage 
  2507. Jetpack::activate_manage(); 
  2508.  
  2509. /** 
  2510. * Sets the internal version number and activation state. 
  2511. * @static 
  2512. */ 
  2513. public static function plugin_initialize() { 
  2514. if ( ! Jetpack_Options::get_option( 'activated' ) ) { 
  2515. Jetpack_Options::update_option( 'activated', 2 ); 
  2516.  
  2517. if ( ! Jetpack_Options::get_option( 'version' ) ) { 
  2518. $version = $old_version = JETPACK__VERSION . ':' . time(); 
  2519. /** This action is documented in class.jetpack.php */ 
  2520. do_action( 'updating_jetpack_version', $version, false ); 
  2521. Jetpack_Options::update_options( compact( 'version', 'old_version' ) ); 
  2522.  
  2523. Jetpack::load_modules(); 
  2524.  
  2525. Jetpack_Options::delete_option( 'do_activate' ); 
  2526.  
  2527. /** 
  2528. * Removes all connection options 
  2529. * @static 
  2530. */ 
  2531. public static function plugin_deactivation( ) { 
  2532. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  2533. if( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { 
  2534. Jetpack_Network::init()->deactivate(); 
  2535. } else { 
  2536. Jetpack::disconnect( false ); 
  2537. //Jetpack_Heartbeat::init()->deactivate(); 
  2538.  
  2539. /** 
  2540. * Disconnects from the Jetpack servers. 
  2541. * Forgets all connection details and tells the Jetpack servers to do the same. 
  2542. * @static 
  2543. */ 
  2544. public static function disconnect( $update_activated_state = true ) { 
  2545. wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); 
  2546. Jetpack::clean_nonces( true ); 
  2547.  
  2548. Jetpack::load_xml_rpc_client(); 
  2549. $xml = new Jetpack_IXR_Client(); 
  2550. $xml->query( 'jetpack.deregister' ); 
  2551.  
  2552. Jetpack_Options::delete_option( 
  2553. array( 
  2554. 'register',  
  2555. 'blog_token',  
  2556. 'user_token',  
  2557. 'user_tokens',  
  2558. 'master_user',  
  2559. 'time_diff',  
  2560. 'fallback_no_verify_ssl_certs',  
  2561. ); 
  2562.  
  2563. if ( $update_activated_state ) { 
  2564. Jetpack_Options::update_option( 'activated', 4 ); 
  2565.  
  2566. $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); 
  2567. // Check then record unique disconnection if site has never been disconnected previously 
  2568. if ( -1 == $jetpack_unique_connection['disconnected'] ) { 
  2569. $jetpack_unique_connection['disconnected'] = 1; 
  2570. else { 
  2571. if ( 0 == $jetpack_unique_connection['disconnected'] ) { 
  2572. //track unique disconnect 
  2573. $jetpack = Jetpack::init(); 
  2574.  
  2575. $jetpack->stat( 'connections', 'unique-disconnect' ); 
  2576. $jetpack->do_stats( 'server_side' ); 
  2577. // increment number of times disconnected 
  2578. $jetpack_unique_connection['disconnected'] += 1; 
  2579.  
  2580. Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); 
  2581.  
  2582. // Disable the Heartbeat cron 
  2583. Jetpack_Heartbeat::init()->deactivate(); 
  2584.  
  2585. /** 
  2586. * Unlinks the current user from the linked WordPress.com user 
  2587. */ 
  2588. public static function unlink_user( $user_id = null ) { 
  2589. if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) ) 
  2590. return false; 
  2591.  
  2592. $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id ); 
  2593.  
  2594. if ( Jetpack_Options::get_option( 'master_user' ) == $user_id ) 
  2595. return false; 
  2596.  
  2597. if ( ! isset( $tokens[ $user_id ] ) ) 
  2598. return false; 
  2599.  
  2600. Jetpack::load_xml_rpc_client(); 
  2601. $xml = new Jetpack_IXR_Client( compact( 'user_id' ) ); 
  2602. $xml->query( 'jetpack.unlink_user', $user_id ); 
  2603.  
  2604. unset( $tokens[ $user_id ] ); 
  2605.  
  2606. Jetpack_Options::update_option( 'user_tokens', $tokens ); 
  2607.  
  2608. return true; 
  2609.  
  2610. /** 
  2611. * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load() 
  2612. */ 
  2613. public static function try_registration() { 
  2614. // Let's get some testing in beta versions and such. 
  2615. if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) { 
  2616. // Before attempting to connect, let's make sure that the domains are viable. 
  2617. $domains_to_check = array_unique( array( 
  2618. 'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ),  
  2619. 'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ),  
  2620. ) ); 
  2621. foreach ( $domains_to_check as $domain ) { 
  2622. $result = Jetpack_Data::is_usable_domain( $domain ); 
  2623. if ( is_wp_error( $result ) ) { 
  2624. return $result; 
  2625.  
  2626. $result = Jetpack::register(); 
  2627.  
  2628. // If there was an error with registration and the site was not registered, record this so we can show a message. 
  2629. if ( ! $result || is_wp_error( $result ) ) { 
  2630. return $result; 
  2631. } else { 
  2632. return true; 
  2633.  
  2634. /** 
  2635. * Tracking an internal event log. Try not to put too much chaff in here. 
  2636. * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA) 
  2637. */ 
  2638. public static function log( $code, $data = null ) { 
  2639. // only grab the latest 200 entries 
  2640. $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 ); 
  2641.  
  2642. // Append our event to the log 
  2643. $log_entry = array( 
  2644. 'time' => time(),  
  2645. 'user_id' => get_current_user_id(),  
  2646. 'blog_id' => Jetpack_Options::get_option( 'id' ),  
  2647. 'code' => $code,  
  2648. ); 
  2649. // Don't bother storing it unless we've got some. 
  2650. if ( ! is_null( $data ) ) { 
  2651. $log_entry['data'] = $data; 
  2652. $log[] = $log_entry; 
  2653.  
  2654. // Try add_option first, to make sure it's not autoloaded. 
  2655. // @todo: Add an add_option method to Jetpack_Options 
  2656. if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) { 
  2657. Jetpack_Options::update_option( 'log', $log ); 
  2658.  
  2659. /** 
  2660. * Fires when Jetpack logs an internal event. 
  2661. * @since 3.0.0 
  2662. * @param array $log_entry { 
  2663. * Array of details about the log entry. 
  2664. * @param string time Time of the event. 
  2665. * @param int user_id ID of the user who trigerred the event. 
  2666. * @param int blog_id Jetpack Blog ID. 
  2667. * @param string code Unique name for the event. 
  2668. * @param string data Data about the event. 
  2669. * } 
  2670. */ 
  2671. do_action( 'jetpack_log_entry', $log_entry ); 
  2672.  
  2673. /** 
  2674. * Get the internal event log. 
  2675. * @param $event (string) - only return the specific log events 
  2676. * @param $num (int) - get specific number of latest results, limited to 200 
  2677. * @return array of log events || WP_Error for invalid params 
  2678. */ 
  2679. public static function get_log( $event = false, $num = false ) { 
  2680. if ( $event && ! is_string( $event ) ) { 
  2681. return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) ); 
  2682.  
  2683. if ( $num && ! is_numeric( $num ) ) { 
  2684. return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) ); 
  2685.  
  2686. $entire_log = Jetpack_Options::get_option( 'log', array() ); 
  2687.  
  2688. // If nothing set - act as it did before, otherwise let's start customizing the output 
  2689. if ( ! $num && ! $event ) { 
  2690. return $entire_log; 
  2691. } else { 
  2692. $entire_log = array_reverse( $entire_log ); 
  2693.  
  2694. $custom_log_output = array(); 
  2695.  
  2696. if ( $event ) { 
  2697. foreach ( $entire_log as $log_event ) { 
  2698. if ( $event == $log_event[ 'code' ] ) { 
  2699. $custom_log_output[] = $log_event; 
  2700. } else { 
  2701. $custom_log_output = $entire_log; 
  2702.  
  2703. if ( $num ) { 
  2704. $custom_log_output = array_slice( $custom_log_output, 0, $num ); 
  2705.  
  2706. return $custom_log_output; 
  2707.  
  2708. /** 
  2709. * Log modification of important settings. 
  2710. */ 
  2711. public static function log_settings_change( $option, $old_value, $value ) { 
  2712. switch( $option ) { 
  2713. case 'jetpack_sync_non_public_post_stati': 
  2714. self::log( $option, $value ); 
  2715. break; 
  2716.  
  2717. /** 
  2718. * Return stat data for WPCOM sync 
  2719. */ 
  2720. function get_stat_data() { 
  2721. $heartbeat_data = Jetpack_Heartbeat::generate_stats_array(); 
  2722. $additional_data = $this->get_additional_stat_data(); 
  2723.  
  2724. return json_encode( array_merge( $heartbeat_data, $additional_data ) ); 
  2725.  
  2726. /** 
  2727. * Get additional stat data to sync to WPCOM 
  2728. */ 
  2729. function get_additional_stat_data( $prefix = '' ) { 
  2730. $return["{$prefix}themes"] = Jetpack::get_parsed_theme_data(); 
  2731. $return["{$prefix}plugins-extra"] = Jetpack::get_parsed_plugin_data(); 
  2732. $return["{$prefix}users"] = count_users(); 
  2733. $return["{$prefix}site-count"] = 0; 
  2734. if ( function_exists( 'get_blog_count' ) ) { 
  2735. $return["{$prefix}site-count"] = get_blog_count(); 
  2736. return $return; 
  2737.  
  2738. /** Admin Pages */ 
  2739.  
  2740. function admin_init() { 
  2741. // If the plugin is not connected, display a connect message. 
  2742. if ( 
  2743. // the plugin was auto-activated and needs its candy 
  2744. Jetpack_Options::get_option( 'do_activate' ) 
  2745. || 
  2746. // the plugin is active, but was never activated. Probably came from a site-wide network activation 
  2747. ! Jetpack_Options::get_option( 'activated' ) 
  2748. ) { 
  2749. Jetpack::plugin_initialize(); 
  2750.  
  2751. if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { 
  2752. if ( 4 != Jetpack_Options::get_option( 'activated' ) ) { 
  2753. // Show connect notice on dashboard and plugins pages 
  2754. add_action( 'load-index.php', array( $this, 'prepare_connect_notice' ) ); 
  2755. add_action( 'load-plugins.php', array( $this, 'prepare_connect_notice' ) ); 
  2756. } elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) { 
  2757. // Upgrade: 1.1 -> 1.1.1 
  2758. // Check and see if host can verify the Jetpack servers' SSL certificate 
  2759. $args = array(); 
  2760. Jetpack_Client::_wp_remote_request( 
  2761. Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ) ),  
  2762. $args,  
  2763. true 
  2764. ); 
  2765. } else { 
  2766. // Show the notice on the Dashboard only for now 
  2767.  
  2768. add_action( 'load-index.php', array( $this, 'prepare_manage_jetpack_notice' ) ); 
  2769.  
  2770. // Identity crisis notices 
  2771. add_action( 'jetpack_notices', array( $this, 'alert_identity_crisis' ) ); 
  2772.  
  2773. // If the plugin has just been disconnected from WP.com, show the survey notice 
  2774. if ( isset( $_GET['disconnected'] ) && 'true' === $_GET['disconnected'] ) { 
  2775. add_action( 'jetpack_notices', array( $this, 'disconnect_survey_notice' ) ); 
  2776.  
  2777. if ( current_user_can( 'manage_options' ) && 'ALWAYS' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) { 
  2778. add_action( 'admin_notices', array( $this, 'alert_required_ssl_fail' ) ); 
  2779.  
  2780. add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) ); 
  2781. add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); 
  2782. add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) ); 
  2783.  
  2784. if ( Jetpack::is_active() || Jetpack::is_development_mode() ) { 
  2785. // Artificially throw errors in certain whitelisted cases during plugin activation 
  2786. add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) ); 
  2787.  
  2788. // Kick off synchronization of user role when it changes 
  2789. add_action( 'set_user_role', array( $this, 'user_role_change' ) ); 
  2790.  
  2791. // Jetpack Manage Activation Screen from .com 
  2792. Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) ); 
  2793.  
  2794. function admin_body_class( $admin_body_class = '' ) { 
  2795. $classes = explode( ' ', trim( $admin_body_class ) ); 
  2796.  
  2797. $classes[] = self::is_active() ? 'jetpack-connected' : 'jetpack-disconnected'; 
  2798.  
  2799. $admin_body_class = implode( ' ', array_unique( $classes ) ); 
  2800. return " $admin_body_class "; 
  2801.  
  2802. static function add_jetpack_pagestyles( $admin_body_class = '' ) { 
  2803. return $admin_body_class . ' jetpack-pagestyles '; 
  2804.  
  2805. function prepare_connect_notice() { 
  2806. add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) ); 
  2807.  
  2808. add_action( 'admin_notices', array( $this, 'admin_connect_notice' ) ); 
  2809.  
  2810. if ( Jetpack::state( 'network_nag' ) ) 
  2811. add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) ); 
  2812. /** 
  2813. * Call this function if you want the Big Jetpack Manage Notice to show up. 
  2814. * @return null 
  2815. */ 
  2816. function prepare_manage_jetpack_notice() { 
  2817.  
  2818. add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) ); 
  2819. add_action( 'admin_notices', array( $this, 'admin_jetpack_manage_notice' ) ); 
  2820.  
  2821. function manage_activate_screen() { 
  2822. include ( JETPACK__PLUGIN_DIR . 'modules/manage/activate-admin.php' ); 
  2823. /** 
  2824. * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load. 
  2825. * This function artificially throws errors for such cases (whitelisted). 
  2826. * @param string $plugin The activated plugin. 
  2827. */ 
  2828. function throw_error_on_activate_plugin( $plugin ) { 
  2829. $active_modules = Jetpack::get_active_modules(); 
  2830.  
  2831. // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks. 
  2832. if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) { 
  2833. $throw = false; 
  2834.  
  2835. // Try and make sure it really was the stats plugin 
  2836. if ( ! class_exists( 'ReflectionFunction' ) ) { 
  2837. if ( 'stats.php' == basename( $plugin ) ) { 
  2838. $throw = true; 
  2839. } else { 
  2840. $reflection = new ReflectionFunction( 'stats_get_api_key' ); 
  2841. if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) { 
  2842. $throw = true; 
  2843.  
  2844. if ( $throw ) { 
  2845. trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR ); 
  2846.  
  2847. function intercept_plugin_error_scrape_init() { 
  2848. add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 ); 
  2849.  
  2850. function intercept_plugin_error_scrape( $action, $result ) { 
  2851. if ( ! $result ) { 
  2852. return; 
  2853.  
  2854. foreach ( $this->plugins_to_deactivate as $deactivate_me ) { 
  2855. if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) { 
  2856. Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), $deactivate_me[1] ), false ); 
  2857.  
  2858. function add_remote_request_handlers() { 
  2859. add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) ); 
  2860.  
  2861. function remote_request_handlers() { 
  2862. switch ( current_filter() ) { 
  2863. case 'wp_ajax_nopriv_jetpack_upload_file' : 
  2864. $response = $this->upload_handler(); 
  2865. break; 
  2866. default : 
  2867. $response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 ); 
  2868. break; 
  2869.  
  2870. if ( ! $response ) { 
  2871. $response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 ); 
  2872.  
  2873. if ( is_wp_error( $response ) ) { 
  2874. $status_code = $response->get_error_data(); 
  2875. $error = $response->get_error_code(); 
  2876. $error_description = $response->get_error_message(); 
  2877.  
  2878. if ( ! is_int( $status_code ) ) { 
  2879. $status_code = 400; 
  2880.  
  2881. status_header( $status_code ); 
  2882. die( json_encode( (object) compact( 'error', 'error_description' ) ) ); 
  2883.  
  2884. status_header( 200 ); 
  2885. if ( true === $response ) { 
  2886. exit; 
  2887.  
  2888. die( json_encode( (object) $response ) ); 
  2889.  
  2890. function upload_handler() { 
  2891. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  2892. return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 ); 
  2893.  
  2894. $user = wp_authenticate( '', '' ); 
  2895. if ( ! $user || is_wp_error( $user ) ) { 
  2896. return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 ); 
  2897.  
  2898. wp_set_current_user( $user->ID ); 
  2899.  
  2900. if ( ! current_user_can( 'upload_files' ) ) { 
  2901. return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 ); 
  2902.  
  2903. if ( empty( $_FILES ) ) { 
  2904. return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 ); 
  2905.  
  2906. foreach ( array_keys( $_FILES ) as $files_key ) { 
  2907. if ( ! isset( $_POST["_jetpack_file_hmac_{$files_key}"] ) ) { 
  2908. return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 ); 
  2909.  
  2910. $media_keys = array_keys( $_FILES['media'] ); 
  2911.  
  2912. $token = Jetpack_Data::get_access_token( get_current_user_id() ); 
  2913. if ( ! $token || is_wp_error( $token ) ) { 
  2914. return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 ); 
  2915.  
  2916. $uploaded_files = array(); 
  2917. $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; 
  2918. unset( $GLOBALS['post'] ); 
  2919. foreach ( $_FILES['media']['name'] as $index => $name ) { 
  2920. $file = array(); 
  2921. foreach ( $media_keys as $media_key ) { 
  2922. $file[$media_key] = $_FILES['media'][$media_key][$index]; 
  2923.  
  2924. list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][$index] ); 
  2925.  
  2926. $hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret ); 
  2927. if ( $hmac_provided !== $hmac_file ) { 
  2928. $uploaded_files[$index] = (object) array( 'error' => 'invalid_hmac', 'error_description' => 'The corresponding HMAC for this file does not match' ); 
  2929. continue; 
  2930.  
  2931. $_FILES['.jetpack.upload.'] = $file; 
  2932. $post_id = isset( $_POST['post_id'][$index] ) ? absint( $_POST['post_id'][$index] ) : 0; 
  2933. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  2934. $post_id = 0; 
  2935. $attachment_id = media_handle_upload( 
  2936. '.jetpack.upload.',  
  2937. $post_id,  
  2938. array(),  
  2939. array( 
  2940. 'action' => 'jetpack_upload_file',  
  2941. ); 
  2942.  
  2943. if ( ! $attachment_id ) { 
  2944. $uploaded_files[$index] = (object) array( 'error' => 'unknown', 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site' ); 
  2945. } elseif ( is_wp_error( $attachment_id ) ) { 
  2946. $uploaded_files[$index] = (object) array( 'error' => 'attachment_' . $attachment_id->get_error_code(), 'error_description' => $attachment_id->get_error_message() ); 
  2947. } else { 
  2948. $attachment = get_post( $attachment_id ); 
  2949. $uploaded_files[$index] = (object) array( 
  2950. 'id' => (string) $attachment_id,  
  2951. 'file' => $attachment->post_title,  
  2952. 'url' => wp_get_attachment_url( $attachment_id ),  
  2953. 'type' => $attachment->post_mime_type,  
  2954. 'meta' => wp_get_attachment_metadata( $attachment_id ),  
  2955. ); 
  2956. if ( ! is_null( $global_post ) ) { 
  2957. $GLOBALS['post'] = $global_post; 
  2958.  
  2959. return $uploaded_files; 
  2960.  
  2961. /** 
  2962. * Add help to the Jetpack page 
  2963. * @since Jetpack (1.2.3) 
  2964. * @return false if not the Jetpack page 
  2965. */ 
  2966. function admin_help() { 
  2967. $current_screen = get_current_screen(); 
  2968.  
  2969. // Overview 
  2970. $current_screen->add_help_tab( 
  2971. array( 
  2972. 'id' => 'home',  
  2973. 'title' => __( 'Home', 'jetpack' ),  
  2974. 'content' => 
  2975. '<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' . 
  2976. '<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' . 
  2977. '<p>' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '</p>',  
  2978. ); 
  2979.  
  2980. // Screen Content 
  2981. if ( current_user_can( 'manage_options' ) ) { 
  2982. $current_screen->add_help_tab( 
  2983. array( 
  2984. 'id' => 'settings',  
  2985. 'title' => __( 'Settings', 'jetpack' ),  
  2986. 'content' => 
  2987. '<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' . 
  2988. '<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' . 
  2989. '<ol>' . 
  2990. '<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' . 
  2991. '<li>' . __( 'Using the checkboxes next to each module, you can select multiple modules to toggle via the Bulk Actions menu at the top of the list.', 'jetpack' ) . '</li>' . 
  2992. '</ol>' . 
  2993. '<p>' . __( 'Using the tools on the right, you can search for specific modules, filter by module categories or which are active, or change the sorting order.', 'jetpack' ) . '</p>' 
  2994. ); 
  2995.  
  2996. // Help Sidebar 
  2997. $current_screen->set_help_sidebar( 
  2998. '<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' . 
  2999. '<p><a href="http://jetpack.me/faq/" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' . 
  3000. '<p><a href="http://jetpack.me/support/" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' . 
  3001. '<p><a href="' . Jetpack::admin_url( array( 'page' => 'jetpack-debugger' ) ) .'">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>' 
  3002. ); 
  3003.  
  3004. function admin_menu_css() { 
  3005. wp_enqueue_style( 'jetpack-icons' ); 
  3006.  
  3007. function admin_menu_order() { 
  3008. return true; 
  3009.  
  3010. function jetpack_menu_order( $menu_order ) { 
  3011. $jp_menu_order = array(); 
  3012.  
  3013. foreach ( $menu_order as $index => $item ) { 
  3014. if ( $item != 'jetpack' ) { 
  3015. $jp_menu_order[] = $item; 
  3016.  
  3017. if ( $index == 0 ) { 
  3018. $jp_menu_order[] = 'jetpack'; 
  3019.  
  3020. return $jp_menu_order; 
  3021.  
  3022. function admin_head() { 
  3023. if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) 
  3024. /** This action is documented in class.jetpack-admin-page.php */ 
  3025. do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] ); 
  3026.  
  3027. function admin_banner_styles() { 
  3028. $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  3029.  
  3030. wp_enqueue_style( 'jetpack', plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-20121016' ); 
  3031. wp_style_add_data( 'jetpack', 'rtl', 'replace' ); 
  3032. wp_style_add_data( 'jetpack', 'suffix', $min ); 
  3033.  
  3034. function admin_scripts() { 
  3035. wp_enqueue_script( 'jetpack-js', plugins_url( '_inc/jp.js', JETPACK__PLUGIN_FILE ), array( 'jquery', 'wp-util' ), JETPACK__VERSION . '-20121111' ); 
  3036. wp_localize_script( 
  3037. 'jetpack-js',  
  3038. 'jetpackL10n',  
  3039. array( 
  3040. 'ays_disconnect' => "This will deactivate all Jetpack modules.\nAre you sure you want to disconnect?",  
  3041. '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?",  
  3042. 'ays_dismiss' => "This will deactivate Jetpack.\nAre you sure you want to deactivate Jetpack?",  
  3043. ); 
  3044. add_action( 'admin_footer', array( $this, 'do_stats' ) ); 
  3045.  
  3046. function plugin_action_links( $actions ) { 
  3047.  
  3048. $jetpack_home = array( 'jetpack-home' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack' ), __( 'Jetpack', 'jetpack' ) ) ); 
  3049.  
  3050. if( current_user_can( 'jetpack_manage_modules' ) && ( Jetpack::is_active() || Jetpack::is_development_mode() ) ) { 
  3051. return array_merge( 
  3052. $jetpack_home,  
  3053. array( 'settings' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack_modules' ), __( 'Settings', 'jetpack' ) ) ),  
  3054. array( 'support' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack-debugger '), __( 'Support', 'jetpack' ) ) ),  
  3055. $actions 
  3056. ); 
  3057.  
  3058. return array_merge( $jetpack_home, $actions ); 
  3059.  
  3060. function admin_connect_notice() { 
  3061. // Don't show the connect notice anywhere but the plugins.php after activating 
  3062. $current = get_current_screen(); 
  3063. if ( 'plugins' !== $current->parent_base ) 
  3064. return; 
  3065.  
  3066. if ( ! current_user_can( 'jetpack_connect' ) ) 
  3067. return; 
  3068.  
  3069. $dismiss_and_deactivate_url = wp_nonce_url( Jetpack::admin_url( '?page=jetpack&jetpack-notice=dismiss' ), 'jetpack-deactivate' ); 
  3070. ?> 
  3071. <div id="message" class="updated jetpack-message jp-banner" style="display:block !important;"> 
  3072. <a class="jp-banner__dismiss" href="<?php echo esc_url( $dismiss_and_deactivate_url ); ?>" title="<?php esc_attr_e( 'Dismiss this notice and deactivate Jetpack.', 'jetpack' ); ?>"></a> 
  3073. <?php if ( in_array( Jetpack_Options::get_option( 'activated' ) , array( 1, 2, 3 ) ) ) : ?> 
  3074. <div class="jp-banner__content is-connection"> 
  3075. <h2><?php _e( 'Your Jetpack is almost ready!', 'jetpack' ); ?></h2> 
  3076. <p><?php _e( 'Connect now to enable features like Stats, Likes, and Social Sharing.', 'jetpack' ); ?></p> 
  3077. </div> 
  3078. <div class="jp-banner__action-container is-connection"> 
  3079. <a href="<?php echo $this->build_connect_url() ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Connect to WordPress.com', 'jetpack' ); ?></a> 
  3080. </div> 
  3081. <?php else : ?> 
  3082. <div class="jp-banner__content"> 
  3083. <h2><?php _e( 'Jetpack is installed!', 'jetpack' ) ?></h2> 
  3084. <p><?php _e( 'It\'s ready to bring awesome, WordPress.com cloud-powered features to your site.', 'jetpack' ) ?></p> 
  3085. </div> 
  3086. <div class="jp-banner__action-container"> 
  3087. <a href="<?php echo Jetpack::admin_url() ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Learn More', 'jetpack' ); ?></a> 
  3088. </div> 
  3089. <?php endif; ?> 
  3090. </div> 
  3091.  
  3092. <?php 
  3093.  
  3094. /** 
  3095. * This is the first banner 
  3096. * It should be visible only to user that can update the option 
  3097. * Are not connected 
  3098. * @return null 
  3099. */ 
  3100. function admin_jetpack_manage_notice() { 
  3101. $screen = get_current_screen(); 
  3102.  
  3103. // Don't show the connect notice on the jetpack settings page. 
  3104. if ( ! in_array( $screen->base, array( 'dashboard' ) ) || $screen->is_network || $screen->action ) 
  3105. return; 
  3106.  
  3107. // Only show it if don't have the managment option set. 
  3108. // And not dismissed it already. 
  3109. if ( ! $this->can_display_jetpack_manage_notice() || Jetpack_Options::get_option( 'dismissed_manage_banner' ) ) { 
  3110. return; 
  3111.  
  3112. $opt_out_url = $this->opt_out_jetpack_manage_url(); 
  3113. $opt_in_url = $this->opt_in_jetpack_manage_url(); 
  3114. /** 
  3115. * I think it would be great to have different wordsing depending on where you are 
  3116. * for example if we show the notice on dashboard and a different one if we show it on Plugins screen 
  3117. * etc.. 
  3118. */ 
  3119.  
  3120. ?> 
  3121. <div id="message" class="updated jetpack-message jp-banner is-opt-in" style="display:block !important;"> 
  3122. <a class="jp-banner__dismiss" href="<?php echo esc_url( $opt_out_url ); ?>" title="<?php esc_attr_e( 'Dismiss this notice for now.', 'jetpack' ); ?>"></a> 
  3123. <div class="jp-banner__content"> 
  3124. <h2><?php esc_html_e( 'New in Jetpack: Centralized Site Management', 'jetpack' ); ?></h2> 
  3125. <p><?php printf( __( 'Manage multiple sites from one dashboard at wordpress.com/sites. Enabling allows all existing, connected Administrators to modify your site from WordPress.com. <a href="%s" target="_blank">Learn More</a>.', 'jetpack' ), 'http://jetpack.me/support/site-management' ); ?></p> 
  3126. </div> 
  3127. <div class="jp-banner__action-container is-opt-in"> 
  3128. <a href="<?php echo esc_url( $opt_in_url ); ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Activate now', 'jetpack' ); ?></a> 
  3129. </div> 
  3130. </div> 
  3131. <?php 
  3132.  
  3133. /** 
  3134. * Returns the url that the user clicks to remove the notice for the big banner 
  3135. * @return (string) 
  3136. */ 
  3137. function opt_out_jetpack_manage_url() { 
  3138. $referer = '&_wp_http_referer=' . add_query_arg( '_wp_http_referer', null ); 
  3139. return wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-manage-opt-out' . $referer ), 'jetpack_manage_banner_opt_out' ); 
  3140. /** 
  3141. * Returns the url that the user clicks to opt in to Jetpack Manage 
  3142. * @return (string) 
  3143. */ 
  3144. function opt_in_jetpack_manage_url() { 
  3145. return wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-manage-opt-in' ), 'jetpack_manage_banner_opt_in' ); 
  3146.  
  3147. function opt_in_jetpack_manage_notice() { 
  3148. ?> 
  3149. <div class="wrap"> 
  3150. <div id="message" class="jetpack-message is-opt-in"> 
  3151. <?php echo sprintf( __( '<p><a href="%1$s" title="Opt in to WordPress.com Site Management" >Activate Site Management</a> to manage multiple sites from our centralized dashboard at wordpress.com/sites. <a href="%2$s" target="_blank">Learn more</a>.</p><a href="%1$s" class="jp-button">Activate Now</a>', 'jetpack' ), $this->opt_in_jetpack_manage_url(), 'http://jetpack.me/support/site-management' ); ?> 
  3152. </div> 
  3153. </div> 
  3154. <?php 
  3155.  
  3156. /** 
  3157. * Determines whether to show the notice of not true = display notice 
  3158. * @return (bool) 
  3159. */ 
  3160. function can_display_jetpack_manage_notice() { 
  3161. // never display the notice to users that can't do anything about it anyways 
  3162. if( ! current_user_can( 'jetpack_manage_modules' ) ) 
  3163. return false; 
  3164.  
  3165. // don't display if we are in development more 
  3166. if( Jetpack::is_development_mode() ) { 
  3167. return false; 
  3168. // don't display if the site is private 
  3169. if( ! Jetpack_Options::get_option( 'public' ) ) 
  3170. return false; 
  3171.  
  3172. /** 
  3173. * Should the Jetpack Remote Site Management notice be displayed. 
  3174. * @since 3.3.0 
  3175. * @param bool ! self::is_module_active( 'manage' ) Is the Manage module inactive. 
  3176. */ 
  3177. return apply_filters( 'can_display_jetpack_manage_notice', ! self::is_module_active( 'manage' ) ); 
  3178.  
  3179. function network_connect_notice() { 
  3180. ?> 
  3181. <div id="message" class="updated jetpack-message"> 
  3182. <div class="squeezer"> 
  3183. <h2><?php _e( '<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.', 'jetpack' ) ?></h2> 
  3184. </div> 
  3185. </div> 
  3186. <?php 
  3187.  
  3188. public static function jetpack_comment_notice() { 
  3189. if ( in_array( 'comments', Jetpack::get_active_modules() ) ) { 
  3190. return ''; 
  3191.  
  3192. $jetpack_old_version = explode( ':', Jetpack_Options::get_option( 'old_version' ) ); 
  3193. $jetpack_new_version = explode( ':', Jetpack_Options::get_option( 'version' ) ); 
  3194.  
  3195. if ( $jetpack_old_version ) { 
  3196. if ( version_compare( $jetpack_old_version[0], '1.4', '>=' ) ) { 
  3197. return ''; 
  3198.  
  3199. if ( $jetpack_new_version ) { 
  3200. if ( version_compare( $jetpack_new_version[0], '1.4-something', '<' ) ) { 
  3201. return ''; 
  3202.  
  3203. return '<br /><br />' . sprintf( 
  3204. __( 'Jetpack now includes Comments, which enables your visitors to use their WordPress.com, Twitter, or Facebook accounts when commenting on your site. To activate Comments, <a href="%s">%s</a>.', 'jetpack' ),  
  3205. wp_nonce_url( 
  3206. Jetpack::admin_url( 
  3207. array( 
  3208. 'page' => 'jetpack',  
  3209. 'action' => 'activate',  
  3210. 'module' => 'comments',  
  3211. ),  
  3212. 'jetpack_activate-comments' 
  3213. ),  
  3214. __( 'click here', 'jetpack' ) 
  3215. ); 
  3216.  
  3217. /** 
  3218. * Show the survey link when the user has just disconnected Jetpack. 
  3219. */ 
  3220. function disconnect_survey_notice() { 
  3221. ?> 
  3222. <div class="wrap"> 
  3223. <div id="message" class="jetpack-message stay-visible"> 
  3224. <div class="squeezer"> 
  3225. <h2> 
  3226. <?php _e( 'You have successfully disconnected Jetpack.', 'jetpack' ); ?> 
  3227. <br /> 
  3228. <?php echo sprintf( 
  3229. __( 'Would you tell us why? Just <a href="%1$s" target="%2$s">answering two simple questions</a> would help us improve Jetpack.', 'jetpack' ),  
  3230. 'https://jetpack.me/survey-disconnected/',  
  3231. '_blank' 
  3232. ); ?> 
  3233. </h2> 
  3234. </div> 
  3235. </div> 
  3236. </div> 
  3237. <?php 
  3238.  
  3239. /** 
  3240. * Registration flow: 
  3241. * 1 - ::admin_page_load() action=register 
  3242. * 2 - ::try_registration() 
  3243. * 3 - ::register() 
  3244. * - Creates jetpack_register option containing two secrets and a timestamp 
  3245. * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with 
  3246. * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id 
  3247. * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's 
  3248. * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1 
  3249. * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2 
  3250. * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with 
  3251. * jetpack_id, jetpack_secret, jetpack_public 
  3252. * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret 
  3253. * 4 - redirect to https://jetpack.wordpress.com/jetpack.authorize/1/ 
  3254. * 5 - user logs in with WP.com account 
  3255. * 6 - redirect to this site's wp-admin/index.php?page=jetpack&action=authorize with 
  3256. * code <-- OAuth2 style authorization code 
  3257. * 7 - ::admin_page_load() action=authorize 
  3258. * 8 - Jetpack_Client_Server::authorize() 
  3259. * 9 - Jetpack_Client_Server::get_token() 
  3260. * 10- GET https://jetpack.wordpress.com/jetpack.token/1/ with 
  3261. * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login 
  3262. * 11- which responds with 
  3263. * access_token, token_type, scope 
  3264. * 12- Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id 
  3265. * 13- Jetpack::activate_default_modules() 
  3266. * Deactivates deprecated plugins 
  3267. * Activates all default modules 
  3268. * Catches errors: redirects to wp-admin/index.php?page=jetpack state:error=something 
  3269. * 14- redirect to this site's wp-admin/index.php?page=jetpack with state:message=authorized 
  3270. * Done! 
  3271. */ 
  3272.  
  3273. /** 
  3274. * Handles the page load events for the Jetpack admin page 
  3275. */ 
  3276. function admin_page_load() { 
  3277. $error = false; 
  3278.  
  3279. // Make sure we have the right body class to hook stylings for subpages off of. 
  3280. add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ) ); 
  3281.  
  3282. if ( ! empty( $_GET['jetpack_restate'] ) ) { 
  3283. // Should only be used in intermediate redirects to preserve state across redirects 
  3284. Jetpack::restate(); 
  3285.  
  3286. if ( isset( $_GET['connect_url_redirect'] ) ) { 
  3287. // User clicked in the iframe to link their accounts 
  3288. if ( ! Jetpack::is_user_connected() ) { 
  3289. $connect_url = $this->build_connect_url( true ); 
  3290. if ( isset( $_GET['notes_iframe'] ) ) 
  3291. $connect_url .= '¬es_iframe'; 
  3292. wp_redirect( $connect_url ); 
  3293. exit; 
  3294. } else { 
  3295. Jetpack::state( 'message', 'already_authorized' ); 
  3296. wp_safe_redirect( Jetpack::admin_url() ); 
  3297. exit; 
  3298.  
  3299.  
  3300. if ( isset( $_GET['action'] ) ) { 
  3301. switch ( $_GET['action'] ) { 
  3302. case 'authorize' : 
  3303. if ( Jetpack::is_active() && Jetpack::is_user_connected() ) { 
  3304. Jetpack::state( 'message', 'already_authorized' ); 
  3305. wp_safe_redirect( Jetpack::admin_url() ); 
  3306. exit; 
  3307. Jetpack::log( 'authorize' ); 
  3308. $client_server = new Jetpack_Client_Server; 
  3309. $client_server->authorize(); 
  3310. exit; 
  3311. case 'register' : 
  3312. if ( ! current_user_can( 'jetpack_connect' ) ) { 
  3313. $error = 'cheatin'; 
  3314. break; 
  3315. check_admin_referer( 'jetpack-register' ); 
  3316. Jetpack::log( 'register' ); 
  3317. Jetpack::maybe_set_version_option(); 
  3318. $registered = Jetpack::try_registration(); 
  3319. if ( is_wp_error( $registered ) ) { 
  3320. $error = $registered->get_error_code(); 
  3321. Jetpack::state( 'error_description', $registered->get_error_message() ); 
  3322. break; 
  3323.  
  3324. wp_redirect( $this->build_connect_url( true ) ); 
  3325. exit; 
  3326. case 'activate' : 
  3327. if ( ! current_user_can( 'jetpack_activate_modules' ) ) { 
  3328. $error = 'cheatin'; 
  3329. break; 
  3330.  
  3331. $module = stripslashes( $_GET['module'] ); 
  3332. check_admin_referer( "jetpack_activate-$module" ); 
  3333. Jetpack::log( 'activate', $module ); 
  3334. Jetpack::activate_module( $module ); 
  3335. // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. 
  3336. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); 
  3337. exit; 
  3338. case 'activate_default_modules' : 
  3339. check_admin_referer( 'activate_default_modules' ); 
  3340. Jetpack::log( 'activate_default_modules' ); 
  3341. Jetpack::restate(); 
  3342. $min_version = isset( $_GET['min_version'] ) ? $_GET['min_version'] : false; 
  3343. $max_version = isset( $_GET['max_version'] ) ? $_GET['max_version'] : false; 
  3344. $other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? $_GET['other_modules'] : array(); 
  3345. Jetpack::activate_default_modules( $min_version, $max_version, $other_modules ); 
  3346. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); 
  3347. exit; 
  3348. case 'disconnect' : 
  3349. if ( ! current_user_can( 'jetpack_disconnect' ) ) { 
  3350. $error = 'cheatin'; 
  3351. break; 
  3352.  
  3353. check_admin_referer( 'jetpack-disconnect' ); 
  3354. Jetpack::log( 'disconnect' ); 
  3355. Jetpack::disconnect(); 
  3356. wp_safe_redirect( Jetpack::admin_url( 'disconnected=true' ) ); 
  3357. exit; 
  3358. case 'reconnect' : 
  3359. if ( ! current_user_can( 'jetpack_reconnect' ) ) { 
  3360. $error = 'cheatin'; 
  3361. break; 
  3362.  
  3363. check_admin_referer( 'jetpack-reconnect' ); 
  3364. Jetpack::log( 'reconnect' ); 
  3365. $this->disconnect(); 
  3366. wp_redirect( $this->build_connect_url( true ) ); 
  3367. exit; 
  3368. case 'deactivate' : 
  3369. if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { 
  3370. $error = 'cheatin'; 
  3371. break; 
  3372.  
  3373. $modules = stripslashes( $_GET['module'] ); 
  3374. check_admin_referer( "jetpack_deactivate-$modules" ); 
  3375. foreach ( explode( ', ', $modules ) as $module ) { 
  3376. Jetpack::log( 'deactivate', $module ); 
  3377. Jetpack::deactivate_module( $module ); 
  3378. Jetpack::state( 'message', 'module_deactivated' ); 
  3379. Jetpack::state( 'module', $modules ); 
  3380. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); 
  3381. exit; 
  3382. case 'unlink' : 
  3383. $redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : ''; 
  3384. check_admin_referer( 'jetpack-unlink' ); 
  3385. Jetpack::log( 'unlink' ); 
  3386. $this->unlink_user(); 
  3387. Jetpack::state( 'message', 'unlinked' ); 
  3388. if ( 'sub-unlink' == $redirect ) { 
  3389. wp_safe_redirect( admin_url() ); 
  3390. } else { 
  3391. wp_safe_redirect( Jetpack::admin_url( array( 'page' => $redirect ) ) ); 
  3392. exit; 
  3393. default: 
  3394. /** 
  3395. * Fires when a Jetpack admin page is loaded with an unrecognized parameter. 
  3396. * @since 2.6.0 
  3397. * @param string sanitize_key( $_GET['action'] ) Unrecognized URL parameter. 
  3398. */ 
  3399. do_action( 'jetpack_unrecognized_action', sanitize_key( $_GET['action'] ) ); 
  3400.  
  3401. if ( ! $error = $error ? $error : Jetpack::state( 'error' ) ) { 
  3402. self::activate_new_modules( true ); 
  3403.  
  3404. switch ( $error ) { 
  3405. case 'cheatin' : 
  3406. $this->error = __( 'Cheatin’ uh?', 'jetpack' ); 
  3407. break; 
  3408. case 'access_denied' : 
  3409. $this->error = __( 'You need to authorize the Jetpack connection between your site and WordPress.com to enable the awesome features.', 'jetpack' ); 
  3410. break; 
  3411. case 'wrong_state' : 
  3412. $this->error = __( 'Don’t cross the streams! You need to stay logged in to your WordPress blog while you authorize Jetpack.', 'jetpack' ); 
  3413. break; 
  3414. case 'invalid_client' : 
  3415. // @todo re-register instead of deactivate/reactivate 
  3416. $this->error = __( 'Return to sender. Whoops! It looks like you got the wrong Jetpack in the mail; deactivate then reactivate the Jetpack plugin to get a new one.', 'jetpack' ); 
  3417. break; 
  3418. case 'invalid_grant' : 
  3419. $this->error = __( 'Wrong size. Hm… it seems your Jetpack doesn’t quite fit. Have you lost weight? Click “Connect to WordPress.com” again to get your Jetpack adjusted.', 'jetpack' ); 
  3420. break; 
  3421. case 'site_inaccessible' : 
  3422. case 'site_requires_authorization' : 
  3423. $this->error = sprintf( __( 'Your website needs to be publicly accessible to use Jetpack: %s', 'jetpack' ), "<code>$error</code>" ); 
  3424. break; 
  3425. case 'module_activation_failed' : 
  3426. $module = Jetpack::state( 'module' ); 
  3427. if ( ! empty( $module ) && $mod = Jetpack::get_module( $module ) ) { 
  3428. $this->error = sprintf( __( '%s could not be activated because it triggered a <strong>fatal error</strong>. Perhaps there is a conflict with another plugin you have installed?', 'jetpack' ), $mod['name'] ); 
  3429. if ( isset( $this->plugins_to_deactivate[$module] ) ) { 
  3430. $this->error .= ' ' . sprintf( __( 'Do you still have the %s plugin installed?', 'jetpack' ), $this->plugins_to_deactivate[$module][1] ); 
  3431. } else { 
  3432. $this->error = __( 'Module could not be activated because it triggered a <strong>fatal error</strong>. Perhaps there is a conflict with another plugin you have installed?', 'jetpack' ); 
  3433. if ( $php_errors = Jetpack::state( 'php_errors' ) ) { 
  3434. $this->error .= "<br />\n"; 
  3435. $this->error .= $php_errors; 
  3436. break; 
  3437. case 'master_user_required' : 
  3438. $module = Jetpack::state( 'module' ); 
  3439. $module_name = ''; 
  3440. if ( ! empty( $module ) && $mod = Jetpack::get_module( $module ) ) { 
  3441. $module_name = $mod['name']; 
  3442.  
  3443. $master_user = Jetpack_Options::get_option( 'master_user' ); 
  3444. $master_userdata = get_userdata( $master_user ) ; 
  3445. if ( $master_userdata ) { 
  3446. if ( ! in_array( $module, Jetpack::get_active_modules() ) ) { 
  3447. $this->error = sprintf( __( '%s was not activated.' , 'jetpack' ), $module_name ); 
  3448. } else { 
  3449. $this->error = sprintf( __( '%s was not deactivated.' , 'jetpack' ), $module_name ); 
  3450. $this->error .= ' ' . sprintf( __( 'This module can only be altered by %s, the user who initiated the Jetpack connection on this site.' , 'jetpack' ), esc_html( $master_userdata->display_name ) ); 
  3451.  
  3452. } else { 
  3453. $this->error = sprintf( __( 'Only the user who initiated the Jetpack connection on this site can toggle %s, but that user no longer exists. This should not happen.', 'jetpack' ), $module_name ); 
  3454. break; 
  3455. case 'not_public' : 
  3456. $this->error = __( '<strong>Your Jetpack has a glitch.</strong> Connecting this site with WordPress.com is not possible. This usually means your site is not publicly accessible (localhost).', 'jetpack' ); 
  3457. break; 
  3458. case 'wpcom_408' : 
  3459. case 'wpcom_5??' : 
  3460. case 'wpcom_bad_response' : 
  3461. case 'wpcom_outage' : 
  3462. $this->error = __( 'WordPress.com is currently having problems and is unable to fuel up your Jetpack. Please try again later.', 'jetpack' ); 
  3463. break; 
  3464. case 'register_http_request_failed' : 
  3465. case 'token_http_request_failed' : 
  3466. $this->error = sprintf( __( 'Jetpack could not contact WordPress.com: %s. This usually means something is incorrectly configured on your web host.', 'jetpack' ), "<code>$error</code>" ); 
  3467. break; 
  3468. default : 
  3469. if ( empty( $error ) ) { 
  3470. break; 
  3471. $error = trim( substr( strip_tags( $error ), 0, 20 ) ); 
  3472. // no break: fall through 
  3473. case 'no_role' : 
  3474. case 'no_cap' : 
  3475. case 'no_code' : 
  3476. case 'no_state' : 
  3477. case 'invalid_state' : 
  3478. case 'invalid_request' : 
  3479. case 'invalid_scope' : 
  3480. case 'unsupported_response_type' : 
  3481. case 'invalid_token' : 
  3482. case 'no_token' : 
  3483. case 'missing_secrets' : 
  3484. case 'home_missing' : 
  3485. case 'siteurl_missing' : 
  3486. case 'gmt_offset_missing' : 
  3487. case 'site_name_missing' : 
  3488. case 'secret_1_missing' : 
  3489. case 'secret_2_missing' : 
  3490. case 'site_lang_missing' : 
  3491. case 'home_malformed' : 
  3492. case 'siteurl_malformed' : 
  3493. case 'gmt_offset_malformed' : 
  3494. case 'timezone_string_malformed' : 
  3495. case 'site_name_malformed' : 
  3496. case 'secret_1_malformed' : 
  3497. case 'secret_2_malformed' : 
  3498. case 'site_lang_malformed' : 
  3499. case 'secrets_mismatch' : 
  3500. case 'verify_secret_1_missing' : 
  3501. case 'verify_secret_1_malformed' : 
  3502. case 'verify_secrets_missing' : 
  3503. case 'verify_secrets_mismatch' : 
  3504. $error = esc_html( $error ); 
  3505. $this->error = sprintf( __( '<strong>Your Jetpack has a glitch.</strong> Something went wrong that’s never supposed to happen. Guess you’re just lucky: %s', 'jetpack' ), "<code>$error</code>" ); 
  3506. if ( ! Jetpack::is_active() ) { 
  3507. $this->error .= '<br />'; 
  3508. $this->error .= sprintf( __( 'Try connecting again.', 'jetpack' ) ); 
  3509. break; 
  3510.  
  3511. $message_code = Jetpack::state( 'message' ); 
  3512.  
  3513. $active_state = Jetpack::state( 'activated_modules' ); 
  3514. if ( ! empty( $active_state ) ) { 
  3515. $available = Jetpack::get_available_modules(); 
  3516. $active_state = explode( ', ', $active_state ); 
  3517. $active_state = array_intersect( $active_state, $available ); 
  3518. if ( count( $active_state ) ) { 
  3519. foreach ( $active_state as $mod ) { 
  3520. $this->stat( 'module-activated', $mod ); 
  3521. } else { 
  3522. $active_state = false; 
  3523. if( Jetpack::state( 'optin-manage' ) ) { 
  3524. $activated_manage = $message_code; 
  3525. $message_code = 'jetpack-manage'; 
  3526.  
  3527. switch ( $message_code ) { 
  3528. case 'modules_activated' : 
  3529. $this->message = sprintf( 
  3530. __( 'Welcome to <strong>Jetpack %s</strong>!', 'jetpack' ),  
  3531. JETPACK__VERSION 
  3532. ); 
  3533.  
  3534. if ( $active_state ) { 
  3535. $titles = array(); 
  3536. foreach ( $active_state as $mod ) { 
  3537. if ( $mod_headers = Jetpack::get_module( $mod ) ) { 
  3538. $titles[] = '<strong>' . preg_replace( '/\s+(?![^<>]++>)/', ' ', $mod_headers['name'] ) . '</strong>'; 
  3539. if ( $titles ) { 
  3540. $this->message .= '<br /><br />' . wp_sprintf( __( 'The following new modules have been activated: %l.', 'jetpack' ), $titles ); 
  3541.  
  3542. if ( $reactive_state = Jetpack::state( 'reactivated_modules' ) ) { 
  3543. $titles = array(); 
  3544. foreach ( explode( ', ', $reactive_state ) as $mod ) { 
  3545. if ( $mod_headers = Jetpack::get_module( $mod ) ) { 
  3546. $titles[] = '<strong>' . preg_replace( '/\s+(?![^<>]++>)/', ' ', $mod_headers['name'] ) . '</strong>'; 
  3547. if ( $titles ) { 
  3548. $this->message .= '<br /><br />' . wp_sprintf( __( 'The following modules have been updated: %l.', 'jetpack' ), $titles ); 
  3549.  
  3550. $this->message .= Jetpack::jetpack_comment_notice(); 
  3551. break; 
  3552. case 'jetpack-manage': 
  3553. $this->message = '<strong>' . sprintf( __( 'You are all set! Your site can now be managed from <a href="%s" target="_blank">wordpress.com/sites</a>.', 'jetpack' ), 'https://wordpress.com/sites' ) . '</strong>'; 
  3554. if ( $activated_manage ) { 
  3555. $this->message .= '<br /><strong>' . __( 'Manage has been activated for you!', 'jetpack' ) . '</strong>'; 
  3556. break; 
  3557. case 'module_activated' : 
  3558. if ( $module = Jetpack::get_module( Jetpack::state( 'module' ) ) ) { 
  3559. $this->message = sprintf( __( '<strong>%s Activated!</strong> You can deactivate at any time by clicking the Deactivate link next to each module.', 'jetpack' ), $module['name'] ); 
  3560. $this->stat( 'module-activated', Jetpack::state( 'module' ) ); 
  3561. break; 
  3562.  
  3563. case 'module_deactivated' : 
  3564. $modules = Jetpack::state( 'module' ); 
  3565. if ( ! $modules ) { 
  3566. break; 
  3567.  
  3568. $module_names = array(); 
  3569. foreach ( explode( ', ', $modules ) as $module_slug ) { 
  3570. $module = Jetpack::get_module( $module_slug ); 
  3571. if ( $module ) { 
  3572. $module_names[] = $module['name']; 
  3573.  
  3574. $this->stat( 'module-deactivated', $module_slug ); 
  3575.  
  3576. if ( ! $module_names ) { 
  3577. break; 
  3578.  
  3579. $this->message = wp_sprintf( 
  3580. _nx( 
  3581. '<strong>%l Deactivated!</strong> You can activate it again at any time using the activate link next to each module.',  
  3582. '<strong>%l Deactivated!</strong> You can activate them again at any time using the activate links next to each module.',  
  3583. count( $module_names ),  
  3584. '%l = list of Jetpack module/feature names',  
  3585. 'jetpack' 
  3586. ),  
  3587. $module_names 
  3588. ); 
  3589. break; 
  3590.  
  3591. case 'module_configured' : 
  3592. $this->message = __( '<strong>Module settings were saved.</strong> ', 'jetpack' ); 
  3593. break; 
  3594.  
  3595. case 'already_authorized' : 
  3596. $this->message = __( '<strong>Your Jetpack is already connected.</strong> ', 'jetpack' ); 
  3597. break; 
  3598.  
  3599. case 'authorized' : 
  3600. $this->message = __( '<strong>You’re fueled up and ready to go, Jetpack is now active.</strong> ', 'jetpack' ); 
  3601. $this->message .= Jetpack::jetpack_comment_notice(); 
  3602. break; 
  3603.  
  3604. case 'linked' : 
  3605. $this->message = __( '<strong>You’re fueled up and ready to go.</strong> ', 'jetpack' ); 
  3606. $this->message .= Jetpack::jetpack_comment_notice(); 
  3607. break; 
  3608.  
  3609. case 'unlinked' : 
  3610. $user = wp_get_current_user(); 
  3611. $this->message = sprintf( __( '<strong>You have unlinked your account (%s) from WordPress.com.</strong>', 'jetpack' ), $user->user_login ); 
  3612. break; 
  3613.  
  3614. case 'switch_master' : 
  3615. global $current_user; 
  3616. $is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' ); 
  3617. $master_userdata = get_userdata( Jetpack_Options::get_option( 'master_user' ) ); 
  3618. if ( $is_master_user ) { 
  3619. $this->message = __( 'You have successfully set yourself as Jetpack*s primary user.', 'jetpack' ); 
  3620. } else { 
  3621. $this->message = sprintf( _x( 'You have successfully set %s as Jetpack*s primary user.', '%s is a username', 'jetpack' ), $master_userdata->user_login ); 
  3622. break; 
  3623.  
  3624. $deactivated_plugins = Jetpack::state( 'deactivated_plugins' ); 
  3625.  
  3626. if ( ! empty( $deactivated_plugins ) ) { 
  3627. $deactivated_plugins = explode( ', ', $deactivated_plugins ); 
  3628. $deactivated_titles = array(); 
  3629. foreach ( $deactivated_plugins as $deactivated_plugin ) { 
  3630. if ( ! isset( $this->plugins_to_deactivate[$deactivated_plugin] ) ) { 
  3631. continue; 
  3632.  
  3633. $deactivated_titles[] = '<strong>' . str_replace( ' ', ' ', $this->plugins_to_deactivate[$deactivated_plugin][1] ) . '</strong>'; 
  3634.  
  3635. if ( $deactivated_titles ) { 
  3636. if ( $this->message ) { 
  3637. $this->message .= "<br /><br />\n"; 
  3638.  
  3639. $this->message .= wp_sprintf( 
  3640. _n( 
  3641. 'Jetpack contains the most recent version of the old %l plugin.',  
  3642. 'Jetpack contains the most recent versions of the old %l plugins.',  
  3643. count( $deactivated_titles ),  
  3644. 'jetpack' 
  3645. ),  
  3646. $deactivated_titles 
  3647. ); 
  3648.  
  3649. $this->message .= "<br />\n"; 
  3650.  
  3651. $this->message .= _n( 
  3652. 'The old version has been deactivated and can be removed from your site.',  
  3653. 'The old versions have been deactivated and can be removed from your site.',  
  3654. count( $deactivated_titles ),  
  3655. 'jetpack' 
  3656. ); 
  3657.  
  3658. $this->privacy_checks = Jetpack::state( 'privacy_checks' ); 
  3659.  
  3660. if ( $this->message || $this->error || $this->privacy_checks || $this->can_display_jetpack_manage_notice() ) { 
  3661. add_action( 'jetpack_notices', array( $this, 'admin_notices' ) ); 
  3662.  
  3663. if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) { 
  3664. /** 
  3665. * Fires when a module configuration page is loaded. 
  3666. * The dynamic part of the hook is the configure parameter from the URL. 
  3667. * @since 1.1.0 
  3668. */ 
  3669. do_action( 'jetpack_module_configuration_load_' . $_GET['configure'] ); 
  3670.  
  3671. add_filter( 'jetpack_short_module_description', 'wptexturize' ); 
  3672.  
  3673. function admin_notices() { 
  3674.  
  3675. if ( $this->error ) { 
  3676. ?> 
  3677. <div id="message" class="jetpack-message jetpack-err"> 
  3678. <div class="squeezer"> 
  3679. <h2><?php echo wp_kses( $this->error, array( 'code' => true, 'strong' => true, 'br' => true, 'b' => true ) ); ?></h2> 
  3680. <?php if ( $desc = Jetpack::state( 'error_description' ) ) : ?> 
  3681. <p><?php echo esc_html( stripslashes( $desc ) ); ?></p> 
  3682. <?php endif; ?> 
  3683. </div> 
  3684. </div> 
  3685. <?php 
  3686.  
  3687. if ( $this->message ) { 
  3688. ?> 
  3689. <div id="message" class="jetpack-message"> 
  3690. <div class="squeezer"> 
  3691. <h2><?php echo wp_kses( $this->message, array( 'strong' => array(), 'a' => array( 'href' => true ), 'br' => true ) ); ?></h2> 
  3692. </div> 
  3693. </div> 
  3694. <?php 
  3695.  
  3696. if ( $this->privacy_checks ) : 
  3697. $module_names = $module_slugs = array(); 
  3698.  
  3699. $privacy_checks = explode( ', ', $this->privacy_checks ); 
  3700. $privacy_checks = array_filter( $privacy_checks, array( 'Jetpack', 'is_module' ) ); 
  3701. foreach ( $privacy_checks as $module_slug ) { 
  3702. $module = Jetpack::get_module( $module_slug ); 
  3703. if ( ! $module ) { 
  3704. continue; 
  3705.  
  3706. $module_slugs[] = $module_slug; 
  3707. $module_names[] = "<strong>{$module['name']}</strong>"; 
  3708.  
  3709. $module_slugs = join( ', ', $module_slugs ); 
  3710. ?> 
  3711. <div id="message" class="jetpack-message jetpack-err"> 
  3712. <div class="squeezer"> 
  3713. <h2><strong><?php esc_html_e( 'Is this site private?', 'jetpack' ); ?></strong></h2><br /> 
  3714. <p><?php 
  3715. echo wp_kses( 
  3716. wptexturize( 
  3717. wp_sprintf( 
  3718. _nx( 
  3719. "Like your site's RSS feeds, %l allows access to your posts and other content to third parties.",  
  3720. "Like your site's RSS feeds, %l allow access to your posts and other content to third parties.",  
  3721. count( $privacy_checks ),  
  3722. '%l = list of Jetpack module/feature names',  
  3723. 'jetpack' 
  3724. ),  
  3725. $module_names 
  3726. ),  
  3727. array( 'strong' => true ) 
  3728. ); 
  3729.  
  3730. echo "\n<br />\n"; 
  3731.  
  3732. echo wp_kses( 
  3733. sprintf( 
  3734. _nx( 
  3735. 'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating this feature</a>.',  
  3736. 'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating these features</a>.',  
  3737. count( $privacy_checks ),  
  3738. '%1$s = deactivation URL, %2$s = "Deactivate {list of Jetpack module/feature names}',  
  3739. 'jetpack' 
  3740. ),  
  3741. wp_nonce_url( 
  3742. Jetpack::admin_url( 
  3743. array( 
  3744. 'page' => 'jetpack',  
  3745. 'action' => 'deactivate',  
  3746. 'module' => urlencode( $module_slugs ),  
  3747. ),  
  3748. "jetpack_deactivate-$module_slugs" 
  3749. ),  
  3750. esc_attr( wp_kses( wp_sprintf( _x( 'Deactivate %l', '%l = list of Jetpack module/feature names', 'jetpack' ), $module_names ), array() ) ) 
  3751. ),  
  3752. array( 'a' => array( 'href' => true, 'title' => true ) ) 
  3753. ); 
  3754. ?></p> 
  3755. </div> 
  3756. </div> 
  3757. <?php endif; 
  3758. // only display the notice if the other stuff is not there 
  3759. if( $this->can_display_jetpack_manage_notice() && ! $this->error && ! $this->message && ! $this->privacy_checks ) { 
  3760. if( isset( $_GET['page'] ) && 'jetpack' != $_GET['page'] ) 
  3761. $this->opt_in_jetpack_manage_notice(); 
  3762.  
  3763. /** 
  3764. * Record a stat for later output. This will only currently output in the admin_footer. 
  3765. */ 
  3766. function stat( $group, $detail ) { 
  3767. if ( ! isset( $this->stats[ $group ] ) ) 
  3768. $this->stats[ $group ] = array(); 
  3769. $this->stats[ $group ][] = $detail; 
  3770.  
  3771. /** 
  3772. * Load stats pixels. $group is auto-prefixed with "x_jetpack-" 
  3773. */ 
  3774. function do_stats( $method = '' ) { 
  3775. if ( is_array( $this->stats ) && count( $this->stats ) ) { 
  3776. foreach ( $this->stats as $group => $stats ) { 
  3777. if ( is_array( $stats ) && count( $stats ) ) { 
  3778. $args = array( "x_jetpack-{$group}" => implode( ', ', $stats ) ); 
  3779. if ( 'server_side' === $method ) { 
  3780. self::do_server_side_stat( $args ); 
  3781. } else { 
  3782. echo '<img src="' . esc_url( self::build_stats_url( $args ) ) . '" width="1" height="1" style="display:none;" />'; 
  3783. unset( $this->stats[ $group ] ); 
  3784.  
  3785. /** 
  3786. * Runs stats code for a one-off, server-side. 
  3787. * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store. 
  3788. * @return bool If it worked. 
  3789. */ 
  3790. static function do_server_side_stat( $args ) { 
  3791. $response = wp_remote_get( esc_url_raw( self::build_stats_url( $args ) ) ); 
  3792. if ( is_wp_error( $response ) ) 
  3793. return false; 
  3794.  
  3795. if ( 200 !== wp_remote_retrieve_response_code( $response ) ) 
  3796. return false; 
  3797.  
  3798. return true; 
  3799.  
  3800. /** 
  3801. * Builds the stats url. 
  3802. * @param $args array|string The arguments to append to the URL. 
  3803. * @return string The URL to be pinged. 
  3804. */ 
  3805. static function build_stats_url( $args ) { 
  3806. $defaults = array( 
  3807. 'v' => 'wpcom2',  
  3808. 'rand' => md5( mt_rand( 0, 999 ) . time() ),  
  3809. ); 
  3810. $args = wp_parse_args( $args, $defaults ); 
  3811. /** 
  3812. * Filter the URL used as the Stats tracking pixel. 
  3813. * @since 2.3.2 
  3814. * @param string $url Base URL used as the Stats tracking pixel. 
  3815. */ 
  3816. $base_url = apply_filters( 
  3817. 'jetpack_stats_base_url',  
  3818. set_url_scheme( 'http://pixel.wp.com/g.gif' ) 
  3819. ); 
  3820. $url = add_query_arg( $args, $base_url ); 
  3821. return $url; 
  3822.  
  3823. function translate_current_user_to_role() { 
  3824. foreach ( $this->capability_translations as $role => $cap ) { 
  3825. if ( current_user_can( $role ) || current_user_can( $cap ) ) { 
  3826. return $role; 
  3827.  
  3828. return false; 
  3829.  
  3830. function translate_role_to_cap( $role ) { 
  3831. if ( ! isset( $this->capability_translations[$role] ) ) { 
  3832. return false; 
  3833.  
  3834. return $this->capability_translations[$role]; 
  3835.  
  3836. function sign_role( $role ) { 
  3837. if ( ! $user_id = (int) get_current_user_id() ) { 
  3838. return false; 
  3839.  
  3840. $token = Jetpack_Data::get_access_token(); 
  3841. if ( ! $token || is_wp_error( $token ) ) { 
  3842. return false; 
  3843.  
  3844. return $role . ':' . hash_hmac( 'md5', "{$role}|{$user_id}", $token->secret ); 
  3845.  
  3846. function build_connect_url( $raw = false, $redirect = false ) { 
  3847. if ( ! Jetpack_Options::get_option( 'blog_token' ) || ! Jetpack_Options::get_option( 'id' ) ) { 
  3848. $url = Jetpack::nonce_url_no_esc( Jetpack::admin_url( 'action=register' ), 'jetpack-register' ); 
  3849. if( is_network_admin() ) { 
  3850. $url = add_query_arg( 'is_multisite', network_admin_url( 
  3851. 'admin.php?page=jetpack-settings' ), $url ); 
  3852. } else { 
  3853. $role = $this->translate_current_user_to_role(); 
  3854. $signed_role = $this->sign_role( $role ); 
  3855.  
  3856. $user = wp_get_current_user(); 
  3857.  
  3858. $redirect = $redirect ? esc_url_raw( $redirect ) : ''; 
  3859.  
  3860. if( isset( $_REQUEST['is_multisite'] ) ) { 
  3861. $redirect = Jetpack_Network::init()->get_url( 'network_admin_page' ); 
  3862.  
  3863. $args = urlencode_deep( 
  3864. array( 
  3865. 'response_type' => 'code',  
  3866. 'client_id' => Jetpack_Options::get_option( 'id' ),  
  3867. 'redirect_uri' => add_query_arg( 
  3868. array( 
  3869. 'action' => 'authorize',  
  3870. '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),  
  3871. 'redirect' => $redirect ? urlencode( $redirect ) : false,  
  3872. ),  
  3873. menu_page_url( 'jetpack', false ) 
  3874. ),  
  3875. 'state' => $user->ID,  
  3876. 'scope' => $signed_role,  
  3877. 'user_email' => $user->user_email,  
  3878. 'user_login' => $user->user_login,  
  3879. 'is_active' => Jetpack::is_active(),  
  3880. 'jp_version' => JETPACK__VERSION,  
  3881. ); 
  3882.  
  3883. $url = add_query_arg( $args, Jetpack::api_url( 'authorize' ) ); 
  3884.  
  3885. return $raw ? $url : esc_url( $url ); 
  3886.  
  3887. function build_reconnect_url( $raw = false ) { 
  3888. $url = wp_nonce_url( Jetpack::admin_url( 'action=reconnect' ), 'jetpack-reconnect' ); 
  3889. return $raw ? $url : esc_url( $url ); 
  3890.  
  3891. public static function admin_url( $args = null ) { 
  3892. $args = wp_parse_args( $args, array( 'page' => 'jetpack' ) ); 
  3893. $url = add_query_arg( $args, admin_url( 'admin.php' ) ); 
  3894. return $url; 
  3895.  
  3896. public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) { 
  3897. $actionurl = str_replace( '&', '&', $actionurl ); 
  3898. return add_query_arg( $name, wp_create_nonce( $action ), $actionurl ); 
  3899.  
  3900. function dismiss_jetpack_notice() { 
  3901.  
  3902. if ( ! isset( $_GET['jetpack-notice'] ) ) { 
  3903. return; 
  3904.  
  3905. switch( $_GET['jetpack-notice'] ) { 
  3906. case 'dismiss': 
  3907. if ( check_admin_referer( 'jetpack-deactivate' ) && ! is_plugin_active_for_network( plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ) ) ) { 
  3908.  
  3909. require_once ABSPATH . 'wp-admin/includes/plugin.php'; 
  3910. deactivate_plugins( JETPACK__PLUGIN_DIR . 'jetpack.php', false, false ); 
  3911. wp_safe_redirect( admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s=' ); 
  3912. break; 
  3913. case 'jetpack-manage-opt-out': 
  3914.  
  3915. if ( check_admin_referer( 'jetpack_manage_banner_opt_out' ) ) { 
  3916. // Don't show the banner again 
  3917.  
  3918. Jetpack_Options::update_option( 'dismissed_manage_banner', true ); 
  3919. // redirect back to the page that had the notice 
  3920. if ( wp_get_referer() ) { 
  3921. wp_safe_redirect( wp_get_referer() ); 
  3922. } else { 
  3923. // Take me to Jetpack 
  3924. wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) ); 
  3925. break; 
  3926. case 'jetpack-protect-multisite-opt-out': 
  3927.  
  3928. if ( check_admin_referer( 'jetpack_protect_multisite_banner_opt_out' ) ) { 
  3929. // Don't show the banner again 
  3930.  
  3931. update_site_option( 'jetpack_dismissed_protect_multisite_banner', true ); 
  3932. // redirect back to the page that had the notice 
  3933. if ( wp_get_referer() ) { 
  3934. wp_safe_redirect( wp_get_referer() ); 
  3935. } else { 
  3936. // Take me to Jetpack 
  3937. wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) ); 
  3938. break; 
  3939. case 'jetpack-manage-opt-in': 
  3940. if ( check_admin_referer( 'jetpack_manage_banner_opt_in' ) ) { 
  3941. // This makes sure that we are redirect to jetpack home so that we can see the Success Message. 
  3942.  
  3943. $redirection_url = Jetpack::admin_url(); 
  3944. remove_action( 'jetpack_pre_activate_module', array( Jetpack_Admin::init(), 'fix_redirect' ) ); 
  3945.  
  3946. // Don't redirect form the Jetpack Setting Page 
  3947. $referer_parsed = parse_url ( wp_get_referer() ); 
  3948. // check that we do have a wp_get_referer and the query paramater is set orderwise go to the Jetpack Home 
  3949. if ( isset( $referer_parsed['query'] ) && false !== strpos( $referer_parsed['query'], 'page=jetpack_modules' ) ) { 
  3950. // Take the user to Jetpack home except when on the setting page 
  3951. $redirection_url = wp_get_referer(); 
  3952. add_action( 'jetpack_pre_activate_module', array( Jetpack_Admin::init(), 'fix_redirect' ) ); 
  3953. // Also update the JSON API FULL MANAGEMENT Option 
  3954. Jetpack::activate_module( 'manage', false, false ); 
  3955.  
  3956. // Special Message when option in. 
  3957. Jetpack::state( 'optin-manage', 'true' ); 
  3958. // Activate the Module if not activated already 
  3959.  
  3960. // Redirect properly 
  3961. wp_safe_redirect( $redirection_url ); 
  3962.  
  3963. break; 
  3964.  
  3965. function debugger_page() { 
  3966. nocache_headers(); 
  3967. if ( ! current_user_can( 'manage_options' ) ) { 
  3968. die( '-1' ); 
  3969. Jetpack_Debugger::jetpack_debug_display_handler(); 
  3970. exit; 
  3971.  
  3972. public static function admin_screen_configure_module( $module_id ) { 
  3973.  
  3974. // User that doesn't have 'jetpack_configure_modules' will never end up here since Jetpack Landing Page woun't let them. 
  3975. if ( ! in_array( $module_id, Jetpack::get_active_modules() ) && current_user_can( 'manage_options' ) ) { 
  3976. if ( has_action( 'display_activate_module_setting_' . $module_id ) ) { 
  3977. /** 
  3978. * Fires to diplay a custom module activation screen. 
  3979. * To add a module actionation screen use Jetpack::module_configuration_activation_screen method. 
  3980. * Example: Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) ); 
  3981. * @module manage 
  3982. * @since 3.8.0 
  3983. * @param int $module_id Module ID. 
  3984. */ 
  3985. do_action( 'display_activate_module_setting_' . $module_id ); 
  3986. } else { 
  3987. self::display_activate_module_link( $module_id ); 
  3988.  
  3989. return false; 
  3990. } ?> 
  3991.  
  3992. <div id="jp-settings-screen" style="position: relative"> 
  3993. <h3> 
  3994. <?php 
  3995. $module = Jetpack::get_module( $module_id ); 
  3996. echo '<a href="' . Jetpack::admin_url( 'page=jetpack_modules' ) . '">' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</a> → '; 
  3997. printf( __( 'Configure %s', 'jetpack' ), $module['name'] ); 
  3998. ?> 
  3999. </h3> 
  4000. <?php 
  4001. /** 
  4002. * Fires within the displayed message when a feature configuation is updated. 
  4003. * @since 3.4.0 
  4004. * @param int $module_id Module ID. 
  4005. */ 
  4006. do_action( 'jetpack_notices_update_settings', $module_id ); 
  4007. /** 
  4008. * Fires when a feature configuation screen is loaded. 
  4009. * The dynamic part of the hook, $module_id, is the module ID. 
  4010. * @since 1.1.0 
  4011. */ 
  4012. do_action( 'jetpack_module_configuration_screen_' . $module_id ); 
  4013. ?> 
  4014. </div><?php 
  4015.  
  4016. /** 
  4017. * Display link to activate the module to see the settings screen. 
  4018. * @param string $module_id 
  4019. * @return null 
  4020. */ 
  4021. public static function display_activate_module_link( $module_id ) { 
  4022.  
  4023. $info = Jetpack::get_module( $module_id ); 
  4024. $extra = ''; 
  4025. $activate_url = wp_nonce_url( 
  4026. Jetpack::admin_url( 
  4027. array( 
  4028. 'page' => 'jetpack',  
  4029. 'action' => 'activate',  
  4030. 'module' => $module_id,  
  4031. ),  
  4032. "jetpack_activate-$module_id" 
  4033. ); 
  4034.  
  4035. ?> 
  4036.  
  4037. <div class="wrap configure-module"> 
  4038. <div id="jp-settings-screen"> 
  4039. <?php 
  4040. if ( $module_id == 'json-api' ) { 
  4041.  
  4042. $info['name'] = esc_html__( 'Activate Site Management and JSON API', 'jetpack' ); 
  4043.  
  4044. $activate_url = Jetpack::init()->opt_in_jetpack_manage_url(); 
  4045.  
  4046. $info['description'] = sprintf( __( 'Manage your multiple Jetpack sites from our centralized dashboard at wordpress.com/sites. <a href="%s" target="_blank">Learn more</a>.', 'jetpack' ), 'http://jetpack.me/support/site-management' ); 
  4047.  
  4048. // $extra = __( 'To use Site Management, you need to first activate JSON API to allow remote management of your site. ', 'jetpack' ); 
  4049. } ?> 
  4050.  
  4051. <h3><?php echo esc_html( $info['name'] ); ?></h3> 
  4052. <div class="narrow"> 
  4053. <p><?php echo $info['description']; ?></p> 
  4054. <?php if( $extra ) { ?> 
  4055. <p><?php echo esc_html( $extra ); ?></p> 
  4056. <?php } ?> 
  4057. <p> 
  4058. <?php 
  4059. if( wp_get_referer() ) { 
  4060. printf( __( '<a class="button-primary" href="%s">Activate Now</a> or <a href="%s" >return to previous page</a>.', 'jetpack' ) , $activate_url, wp_get_referer() ); 
  4061. } else { 
  4062. printf( __( '<a class="button-primary" href="%s">Activate Now</a>', 'jetpack' ) , $activate_url ); 
  4063. } ?> 
  4064. </p> 
  4065. </div> 
  4066.  
  4067. </div> 
  4068. </div> 
  4069.  
  4070. <?php 
  4071.  
  4072. public static function sort_modules( $a, $b ) { 
  4073. if ( $a['sort'] == $b['sort'] ) 
  4074. return 0; 
  4075.  
  4076. return ( $a['sort'] < $b['sort'] ) ? -1 : 1; 
  4077.  
  4078. function sync_reindex_trigger() { 
  4079. if ( $this->current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) { 
  4080. echo json_encode( $this->sync->reindex_trigger() ); 
  4081. } else { 
  4082. echo '{"status":"ERROR"}'; 
  4083. exit; 
  4084.  
  4085. function sync_reindex_status() { 
  4086. if ( $this->current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) { 
  4087. echo json_encode( $this->sync->reindex_status() ); 
  4088. } else { 
  4089. echo '{"status":"ERROR"}'; 
  4090. exit; 
  4091.  
  4092. /** Client API */ 
  4093.  
  4094. /** 
  4095. * Returns the requested Jetpack API URL 
  4096. * @return string 
  4097. */ 
  4098. public static function api_url( $relative_url ) { 
  4099. return trailingslashit( JETPACK__API_BASE . $relative_url ) . JETPACK__API_VERSION . '/'; 
  4100.  
  4101. /** 
  4102. * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets 
  4103. */ 
  4104. public static function fix_url_for_bad_hosts( $url ) { 
  4105. if ( 0 !== strpos( $url, 'https://' ) ) { 
  4106. return $url; 
  4107.  
  4108. switch ( JETPACK_CLIENT__HTTPS ) { 
  4109. case 'ALWAYS' : 
  4110. return $url; 
  4111. case 'NEVER' : 
  4112. return set_url_scheme( $url, 'http' ); 
  4113. // default : case 'AUTO' : 
  4114.  
  4115. // Yay! Your host is good! 
  4116. if ( self::permit_ssl() && wp_http_supports( array( 'ssl' => true ) ) ) { 
  4117. return $url; 
  4118.  
  4119. // Boo! Your host is bad and makes Jetpack cry! 
  4120. return set_url_scheme( $url, 'http' ); 
  4121.  
  4122. /** 
  4123. * Checks to see if the URL is using SSL to connect with Jetpack 
  4124. * @since 2.3.3 
  4125. * @return boolean 
  4126. */ 
  4127. public static function permit_ssl( $force_recheck = false ) { 
  4128. // Do some fancy tests to see if ssl is being supported 
  4129. if ( $force_recheck || false === ( $ssl = get_transient( 'jetpack_https_test' ) ) ) { 
  4130. if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) { 
  4131. $ssl = 0; 
  4132. } else { 
  4133. switch ( JETPACK_CLIENT__HTTPS ) { 
  4134. case 'NEVER': 
  4135. $ssl = 0; 
  4136. break; 
  4137. case 'ALWAYS': 
  4138. case 'AUTO': 
  4139. default: 
  4140. $ssl = 1; 
  4141. break; 
  4142.  
  4143. // If it's not 'NEVER', test to see 
  4144. if ( $ssl ) { 
  4145. $response = wp_remote_get( JETPACK__API_BASE . 'test/1/' ); 
  4146. if ( is_wp_error( $response ) || ( 'OK' !== wp_remote_retrieve_body( $response ) ) ) { 
  4147. $ssl = 0; 
  4148. set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS ); 
  4149.  
  4150. return (bool) $ssl; 
  4151.  
  4152. /** 
  4153. * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'ALWAYS' but SSL isn't working. 
  4154. */ 
  4155. public function alert_required_ssl_fail() { 
  4156. if ( ! current_user_can( 'manage_options' ) ) 
  4157. return; 
  4158. ?> 
  4159.  
  4160. <div id="message" class="error jetpack-message jp-identity-crisis"> 
  4161. <div class="jp-banner__content"> 
  4162. <h2><?php _e( 'Something is being cranky!', 'jetpack' ); ?></h2> 
  4163. <p><?php _e( 'Your site is configured to only permit SSL connections to Jetpack, but SSL connections don\'t seem to be functional!', 'jetpack' ); ?></p> 
  4164. </div> 
  4165. </div> 
  4166.  
  4167. <?php 
  4168.  
  4169. /** 
  4170. * Returns the Jetpack XML-RPC API 
  4171. * @return string 
  4172. */ 
  4173. public static function xmlrpc_api_url() { 
  4174. $base = preg_replace( '#(https?://[^?/]+)(/?.*)?$#', '\\1', JETPACK__API_BASE ); 
  4175. return untrailingslashit( $base ) . '/xmlrpc.php'; 
  4176.  
  4177. /** 
  4178. * Creates two secret tokens and the end of life timestamp for them. 
  4179. * Note these tokens are unique per call, NOT static per site for connecting. 
  4180. * @since 2.6 
  4181. * @return array 
  4182. */ 
  4183. public function generate_secrets() { 
  4184. $secrets = array( 
  4185. wp_generate_password( 32, false ), // secret_1 
  4186. wp_generate_password( 32, false ), // secret_2 
  4187. ( time() + 600 ), // eol ( End of Life ) 
  4188. ); 
  4189.  
  4190. return $secrets; 
  4191.  
  4192. /** 
  4193. * Builds the timeout limit for queries talking with the wpcom servers. 
  4194. * Based on local php max_execution_time in php.ini 
  4195. * @since 2.6 
  4196. * @return int 
  4197. **/ 
  4198. public function get_remote_query_timeout_limit() { 
  4199. $timeout = (int) ini_get( 'max_execution_time' ); 
  4200. if ( ! $timeout ) // Ensure exec time set in php.ini 
  4201. $timeout = 30; 
  4202. return intval( $timeout / 2 ); 
  4203.  
  4204.  
  4205. /** 
  4206. * Takes the response from the Jetpack register new site endpoint and 
  4207. * verifies it worked properly. 
  4208. * @since 2.6 
  4209. * @return true or Jetpack_Error 
  4210. **/ 
  4211. public function validate_remote_register_response( $response ) { 
  4212. if ( is_wp_error( $response ) ) { 
  4213. return new Jetpack_Error( 'register_http_request_failed', $response->get_error_message() ); 
  4214.  
  4215. $code = wp_remote_retrieve_response_code( $response ); 
  4216. $entity = wp_remote_retrieve_body( $response ); 
  4217. if ( $entity ) 
  4218. $json = json_decode( $entity ); 
  4219. else 
  4220. $json = false; 
  4221.  
  4222. $code_type = intval( $code / 100 ); 
  4223. if ( 5 == $code_type ) { 
  4224. return new Jetpack_Error( 'wpcom_5??', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code ); 
  4225. } elseif ( 408 == $code ) { 
  4226. return new Jetpack_Error( 'wpcom_408', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code ); 
  4227. } elseif ( ! empty( $json->error ) ) { 
  4228. $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; 
  4229. return new Jetpack_Error( (string) $json->error, $error_description, $code ); 
  4230. } elseif ( 200 != $code ) { 
  4231. return new Jetpack_Error( 'wpcom_bad_response', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code ); 
  4232.  
  4233. // Jetpack ID error block 
  4234. if ( empty( $json->jetpack_id ) ) { 
  4235. return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is empty. Do not publicly post this error message! %s', 'jetpack' ), $entity ), $entity ); 
  4236. } elseif ( ! is_scalar( $json->jetpack_id ) ) { 
  4237. return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is not a scalar. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity ); 
  4238. } elseif ( preg_match( '/[^0-9]/', $json->jetpack_id ) ) { 
  4239. return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID begins with a numeral. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity ); 
  4240.  
  4241. return true; 
  4242. /** 
  4243. * @return bool|WP_Error 
  4244. */ 
  4245. public static function register() { 
  4246. add_action( 'pre_update_jetpack_option_register', array( 'Jetpack_Options', 'delete_option' ) ); 
  4247. $secrets = Jetpack::init()->generate_secrets(); 
  4248.  
  4249. Jetpack_Options::update_option( 'register', $secrets[0] . ':' . $secrets[1] . ':' . $secrets[2] ); 
  4250.  
  4251. @list( $secret_1, $secret_2, $secret_eol ) = explode( ':', Jetpack_Options::get_option( 'register' ) ); 
  4252. if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) { 
  4253. return new Jetpack_Error( 'missing_secrets' ); 
  4254.  
  4255. $timeout = Jetpack::init()->get_remote_query_timeout_limit(); 
  4256.  
  4257. $gmt_offset = get_option( 'gmt_offset' ); 
  4258. if ( ! $gmt_offset ) { 
  4259. $gmt_offset = 0; 
  4260.  
  4261. $stats_options = get_option( 'stats_options' ); 
  4262. $stats_id = isset($stats_options['blog_id']) ? $stats_options['blog_id'] : null; 
  4263.  
  4264. $args = array( 
  4265. 'method' => 'POST',  
  4266. 'body' => array( 
  4267. 'siteurl' => site_url(),  
  4268. 'home' => home_url(),  
  4269. 'gmt_offset' => $gmt_offset,  
  4270. 'timezone_string' => (string) get_option( 'timezone_string' ),  
  4271. 'site_name' => (string) get_option( 'blogname' ),  
  4272. 'secret_1' => $secret_1,  
  4273. 'secret_2' => $secret_2,  
  4274. 'site_lang' => get_locale(),  
  4275. 'timeout' => $timeout,  
  4276. 'stats_id' => $stats_id,  
  4277. ),  
  4278. 'headers' => array( 
  4279. 'Accept' => 'application/json',  
  4280. ),  
  4281. 'timeout' => $timeout,  
  4282. ); 
  4283. $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'register' ) ), $args, true ); 
  4284.  
  4285.  
  4286. // Make sure the response is valid and does not contain any Jetpack errors 
  4287. $valid_response = Jetpack::init()->validate_remote_register_response( $response ); 
  4288. if( is_wp_error( $valid_response ) || !$valid_response ) { 
  4289. return $valid_response; 
  4290.  
  4291.  
  4292. // Grab the response values to work with 
  4293. $code = wp_remote_retrieve_response_code( $response ); 
  4294. $entity = wp_remote_retrieve_body( $response ); 
  4295.  
  4296. if ( $entity ) 
  4297. $json = json_decode( $entity ); 
  4298. else 
  4299. $json = false; 
  4300.  
  4301. if ( empty( $json->jetpack_secret ) || ! is_string( $json->jetpack_secret ) ) 
  4302. return new Jetpack_Error( 'jetpack_secret', '', $code ); 
  4303.  
  4304. if ( isset( $json->jetpack_public ) ) { 
  4305. $jetpack_public = (int) $json->jetpack_public; 
  4306. } else { 
  4307. $jetpack_public = false; 
  4308.  
  4309. Jetpack_Options::update_options( 
  4310. array( 
  4311. 'id' => (int) $json->jetpack_id,  
  4312. 'blog_token' => (string) $json->jetpack_secret,  
  4313. 'public' => $jetpack_public,  
  4314. ); 
  4315.  
  4316. /** 
  4317. * Fires when a site is registered on WordPress.com. 
  4318. * @since 3.7.0 
  4319. * @param int $json->jetpack_id Jetpack Blog ID. 
  4320. * @param string $json->jetpack_secret Jetpack Blog Token. 
  4321. * @param int|bool $jetpack_public Is the site public. 
  4322. */ 
  4323. do_action( 'jetpack_site_registered', $json->jetpack_id, $json->jetpack_secret, $jetpack_public ); 
  4324.  
  4325. // Initialize Jump Start for the first and only time. 
  4326. if ( ! Jetpack_Options::get_option( 'jumpstart' ) ) { 
  4327. Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); 
  4328.  
  4329. $jetpack = Jetpack::init(); 
  4330.  
  4331. $jetpack->stat( 'jumpstart', 'unique-views' ); 
  4332. $jetpack->do_stats( 'server_side' ); 
  4333. }; 
  4334.  
  4335. return true; 
  4336.  
  4337. /** 
  4338. * If the db version is showing something other that what we've got now, bump it to current. 
  4339. * @return bool: True if the option was incorrect and updated, false if nothing happened. 
  4340. */ 
  4341. public static function maybe_set_version_option() { 
  4342. list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) ); 
  4343. if ( JETPACK__VERSION != $version ) { 
  4344. Jetpack_Options::update_option( 'version', JETPACK__VERSION . ':' . time() ); 
  4345. return true; 
  4346. return false; 
  4347.  
  4348. /** Client Server API */ 
  4349.  
  4350. /** 
  4351. * Loads the Jetpack XML-RPC client 
  4352. */ 
  4353. public static function load_xml_rpc_client() { 
  4354. require_once ABSPATH . WPINC . '/class-IXR.php'; 
  4355. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-ixr-client.php'; 
  4356.  
  4357. function verify_xml_rpc_signature() { 
  4358. if ( $this->xmlrpc_verification ) { 
  4359. return $this->xmlrpc_verification; 
  4360.  
  4361. // It's not for us 
  4362. if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) { 
  4363. return false; 
  4364.  
  4365. @list( $token_key, $version, $user_id ) = explode( ':', $_GET['token'] ); 
  4366. if ( 
  4367. empty( $token_key ) 
  4368. || 
  4369. empty( $version ) || strval( JETPACK__API_VERSION ) !== $version 
  4370. ) { 
  4371. return false; 
  4372.  
  4373. if ( '0' === $user_id ) { 
  4374. $token_type = 'blog'; 
  4375. $user_id = 0; 
  4376. } else { 
  4377. $token_type = 'user'; 
  4378. if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) { 
  4379. return false; 
  4380. $user_id = (int) $user_id; 
  4381.  
  4382. $user = new WP_User( $user_id ); 
  4383. if ( ! $user || ! $user->exists() ) { 
  4384. return false; 
  4385.  
  4386. $token = Jetpack_Data::get_access_token( $user_id ); 
  4387. if ( ! $token ) { 
  4388. return false; 
  4389.  
  4390. $token_check = "$token_key."; 
  4391. if ( ! hash_equals( substr( $token->secret, 0, strlen( $token_check ) ), $token_check ) ) { 
  4392. return false; 
  4393.  
  4394. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; 
  4395.  
  4396. $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) ); 
  4397. if ( isset( $_POST['_jetpack_is_multipart'] ) ) { 
  4398. $post_data = $_POST; 
  4399. $file_hashes = array(); 
  4400. foreach ( $post_data as $post_data_key => $post_data_value ) { 
  4401. if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) { 
  4402. continue; 
  4403. $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) ); 
  4404. $file_hashes[$post_data_key] = $post_data_value; 
  4405.  
  4406. foreach ( $file_hashes as $post_data_key => $post_data_value ) { 
  4407. unset( $post_data["_jetpack_file_hmac_{$post_data_key}"] ); 
  4408. $post_data[$post_data_key] = $post_data_value; 
  4409.  
  4410. ksort( $post_data ); 
  4411.  
  4412. $body = http_build_query( stripslashes_deep( $post_data ) ); 
  4413. } elseif ( is_null( $this->HTTP_RAW_POST_DATA ) ) { 
  4414. $body = file_get_contents( 'php://input' ); 
  4415. } else { 
  4416. $body = null; 
  4417. $signature = $jetpack_signature->sign_current_request( 
  4418. array( 'body' => is_null( $body ) ? $this->HTTP_RAW_POST_DATA : $body, ) 
  4419. ); 
  4420.  
  4421. if ( ! $signature ) { 
  4422. return false; 
  4423. } else if ( is_wp_error( $signature ) ) { 
  4424. return $signature; 
  4425. } else if ( ! hash_equals( $signature, $_GET['signature'] ) ) { 
  4426. return false; 
  4427.  
  4428. $timestamp = (int) $_GET['timestamp']; 
  4429. $nonce = stripslashes( (string) $_GET['nonce'] ); 
  4430.  
  4431. if ( ! $this->add_nonce( $timestamp, $nonce ) ) { 
  4432. return false; 
  4433.  
  4434. $this->xmlrpc_verification = array( 
  4435. 'type' => $token_type,  
  4436. 'user_id' => $token->external_user_id,  
  4437. ); 
  4438.  
  4439. return $this->xmlrpc_verification; 
  4440.  
  4441. /** 
  4442. * Authenticates XML-RPC and other requests from the Jetpack Server 
  4443. */ 
  4444. function authenticate_jetpack( $user, $username, $password ) { 
  4445. if ( is_a( $user, 'WP_User' ) ) { 
  4446. return $user; 
  4447.  
  4448. $token_details = $this->verify_xml_rpc_signature(); 
  4449.  
  4450. if ( ! $token_details || is_wp_error( $token_details ) ) { 
  4451. return $user; 
  4452.  
  4453. if ( 'user' !== $token_details['type'] ) { 
  4454. return $user; 
  4455.  
  4456. if ( ! $token_details['user_id'] ) { 
  4457. return $user; 
  4458.  
  4459. nocache_headers(); 
  4460.  
  4461. return new WP_User( $token_details['user_id'] ); 
  4462.  
  4463. function add_nonce( $timestamp, $nonce ) { 
  4464. global $wpdb; 
  4465. static $nonces_used_this_request = array(); 
  4466.  
  4467. if ( isset( $nonces_used_this_request["$timestamp:$nonce"] ) ) { 
  4468. return $nonces_used_this_request["$timestamp:$nonce"]; 
  4469.  
  4470. // This should always have gone through Jetpack_Signature::sign_request() first to check $timestamp an $nonce 
  4471. $timestamp = (int) $timestamp; 
  4472. $nonce = esc_sql( $nonce ); 
  4473.  
  4474. // Raw query so we can avoid races: add_option will also update 
  4475. $show_errors = $wpdb->show_errors( false ); 
  4476.  
  4477. $old_nonce = $wpdb->get_row( 
  4478. $wpdb->prepare( "SELECT * FROM `$wpdb->options` WHERE option_name = %s", "jetpack_nonce_{$timestamp}_{$nonce}" ) 
  4479. ); 
  4480.  
  4481. if ( is_null( $old_nonce ) ) { 
  4482. $return = $wpdb->query( 
  4483. $wpdb->prepare( 
  4484. "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)",  
  4485. "jetpack_nonce_{$timestamp}_{$nonce}",  
  4486. time(),  
  4487. 'no' 
  4488. ); 
  4489. } else { 
  4490. $return = false; 
  4491.  
  4492. $wpdb->show_errors( $show_errors ); 
  4493.  
  4494. $nonces_used_this_request["$timestamp:$nonce"] = $return; 
  4495.  
  4496. return $return; 
  4497.  
  4498. /** 
  4499. * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods. 
  4500. * Capture it here so we can verify the signature later. 
  4501. */ 
  4502. function xmlrpc_methods( $methods ) { 
  4503. $this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA']; 
  4504. return $methods; 
  4505.  
  4506. function public_xmlrpc_methods( $methods ) { 
  4507. if ( array_key_exists( 'wp.getOptions', $methods ) ) { 
  4508. $methods['wp.getOptions'] = array( $this, 'jetpack_getOptions' ); 
  4509. return $methods; 
  4510.  
  4511. function jetpack_getOptions( $args ) { 
  4512. global $wp_xmlrpc_server; 
  4513.  
  4514. $wp_xmlrpc_server->escape( $args ); 
  4515.  
  4516. $username = $args[1]; 
  4517. $password = $args[2]; 
  4518.  
  4519. if ( !$user = $wp_xmlrpc_server->login($username, $password) ) { 
  4520. return $wp_xmlrpc_server->error; 
  4521.  
  4522. $options = array(); 
  4523. $user_data = $this->get_connected_user_data(); 
  4524. if ( is_array( $user_data ) ) { 
  4525. $options['jetpack_user_id'] = array( 
  4526. 'desc' => __( 'The WP.com user ID of the connected user', 'jetpack' ),  
  4527. 'readonly' => true,  
  4528. 'value' => $user_data['ID'],  
  4529. ); 
  4530. $options['jetpack_user_login'] = array( 
  4531. 'desc' => __( 'The WP.com username of the connected user', 'jetpack' ),  
  4532. 'readonly' => true,  
  4533. 'value' => $user_data['login'],  
  4534. ); 
  4535. $options['jetpack_user_email'] = array( 
  4536. 'desc' => __( 'The WP.com user email of the connected user', 'jetpack' ),  
  4537. 'readonly' => true,  
  4538. 'value' => $user_data['email'],  
  4539. ); 
  4540. $options['jetpack_user_site_count'] = array( 
  4541. 'desc' => __( 'The number of sites of the connected WP.com user', 'jetpack' ),  
  4542. 'readonly' => true,  
  4543. 'value' => $user_data['site_count'],  
  4544. ); 
  4545. $wp_xmlrpc_server->blog_options = array_merge( $wp_xmlrpc_server->blog_options, $options ); 
  4546. $args = stripslashes_deep( $args ); 
  4547. return $wp_xmlrpc_server->wp_getOptions( $args ); 
  4548.  
  4549. function xmlrpc_options( $options ) { 
  4550. $jetpack_client_id = false; 
  4551. if ( self::is_active() ) { 
  4552. $jetpack_client_id = Jetpack_Options::get_option( 'id' ); 
  4553. $options['jetpack_version'] = array( 
  4554. 'desc' => __( 'Jetpack Plugin Version', 'jetpack' ),  
  4555. 'readonly' => true,  
  4556. 'value' => JETPACK__VERSION,  
  4557. ); 
  4558.  
  4559. $options['jetpack_client_id'] = array( 
  4560. 'desc' => __( 'The Client ID/WP.com Blog ID of this site', 'jetpack' ),  
  4561. 'readonly' => true,  
  4562. 'value' => $jetpack_client_id,  
  4563. ); 
  4564. return $options; 
  4565.  
  4566. public static function clean_nonces( $all = false ) { 
  4567. global $wpdb; 
  4568.  
  4569. $sql = "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s"; 
  4570. if ( method_exists ( $wpdb , 'esc_like' ) ) { 
  4571. $sql_args = array( $wpdb->esc_like( 'jetpack_nonce_' ) . '%' ); 
  4572. } else { 
  4573. $sql_args = array( like_escape( 'jetpack_nonce_' ) . '%' ); 
  4574.  
  4575. if ( true !== $all ) { 
  4576. $sql .= ' AND CAST( `option_value` AS UNSIGNED ) < %d'; 
  4577. $sql_args[] = time() - 3600; 
  4578.  
  4579. $sql .= ' ORDER BY `option_id` LIMIT 100'; 
  4580.  
  4581. $sql = $wpdb->prepare( $sql, $sql_args ); 
  4582.  
  4583. for ( $i = 0; $i < 1000; $i++ ) { 
  4584. if ( ! $wpdb->query( $sql ) ) { 
  4585. break; 
  4586.  
  4587. /** 
  4588. * State is passed via cookies from one request to the next, but never to subsequent requests. 
  4589. * SET: state( $key, $value ); 
  4590. * GET: $value = state( $key ); 
  4591. * @param string $key 
  4592. * @param string $value 
  4593. * @param bool $restate private 
  4594. */ 
  4595. public static function state( $key = null, $value = null, $restate = false ) { 
  4596. static $state = array(); 
  4597. static $path, $domain; 
  4598. if ( ! isset( $path ) ) { 
  4599. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  4600. $admin_url = Jetpack::admin_url(); 
  4601. $bits = parse_url( $admin_url ); 
  4602.  
  4603. if ( is_array( $bits ) ) { 
  4604. $path = ( isset( $bits['path'] ) ) ? dirname( $bits['path'] ) : null; 
  4605. $domain = ( isset( $bits['host'] ) ) ? $bits['host'] : null; 
  4606. } else { 
  4607. $path = $domain = null; 
  4608.  
  4609. // Extract state from cookies and delete cookies 
  4610. if ( isset( $_COOKIE[ 'jetpackState' ] ) && is_array( $_COOKIE[ 'jetpackState' ] ) ) { 
  4611. $yum = $_COOKIE[ 'jetpackState' ]; 
  4612. unset( $_COOKIE[ 'jetpackState' ] ); 
  4613. foreach ( $yum as $k => $v ) { 
  4614. if ( strlen( $v ) ) 
  4615. $state[ $k ] = $v; 
  4616. setcookie( "jetpackState[$k]", false, 0, $path, $domain ); 
  4617.  
  4618. if ( $restate ) { 
  4619. foreach ( $state as $k => $v ) { 
  4620. setcookie( "jetpackState[$k]", $v, 0, $path, $domain ); 
  4621. return; 
  4622.  
  4623. // Get a state variable 
  4624. if ( isset( $key ) && ! isset( $value ) ) { 
  4625. if ( array_key_exists( $key, $state ) ) 
  4626. return $state[ $key ]; 
  4627. return null; 
  4628.  
  4629. // Set a state variable 
  4630. if ( isset ( $key ) && isset( $value ) ) { 
  4631. if( is_array( $value ) && isset( $value[0] ) ) { 
  4632. $value = $value[0]; 
  4633. $state[ $key ] = $value; 
  4634. setcookie( "jetpackState[$key]", $value, 0, $path, $domain ); 
  4635.  
  4636. public static function restate() { 
  4637. Jetpack::state( null, null, true ); 
  4638.  
  4639. public static function check_privacy( $file ) { 
  4640. static $is_site_publicly_accessible = null; 
  4641.  
  4642. if ( is_null( $is_site_publicly_accessible ) ) { 
  4643. $is_site_publicly_accessible = false; 
  4644.  
  4645. Jetpack::load_xml_rpc_client(); 
  4646. $rpc = new Jetpack_IXR_Client(); 
  4647.  
  4648. $success = $rpc->query( 'jetpack.isSitePubliclyAccessible', home_url() ); 
  4649. if ( $success ) { 
  4650. $response = $rpc->getResponse(); 
  4651. if ( $response ) { 
  4652. $is_site_publicly_accessible = true; 
  4653.  
  4654. Jetpack_Options::update_option( 'public', (int) $is_site_publicly_accessible ); 
  4655.  
  4656. if ( $is_site_publicly_accessible ) { 
  4657. return; 
  4658.  
  4659. $module_slug = self::get_module_slug( $file ); 
  4660.  
  4661. $privacy_checks = Jetpack::state( 'privacy_checks' ); 
  4662. if ( ! $privacy_checks ) { 
  4663. $privacy_checks = $module_slug; 
  4664. } else { 
  4665. $privacy_checks .= ", $module_slug"; 
  4666.  
  4667. Jetpack::state( 'privacy_checks', $privacy_checks ); 
  4668.  
  4669. /** 
  4670. * Helper method for multicall XMLRPC. 
  4671. */ 
  4672. public static function xmlrpc_async_call() { 
  4673. global $blog_id; 
  4674. static $clients = array(); 
  4675.  
  4676. $client_blog_id = is_multisite() ? $blog_id : 0; 
  4677.  
  4678. if ( ! isset( $clients[$client_blog_id] ) ) { 
  4679. Jetpack::load_xml_rpc_client(); 
  4680. $clients[$client_blog_id] = new Jetpack_IXR_ClientMulticall( array( 'user_id' => JETPACK_MASTER_USER, ) ); 
  4681. if ( function_exists( 'ignore_user_abort' ) ) { 
  4682. ignore_user_abort( true ); 
  4683. add_action( 'shutdown', array( 'Jetpack', 'xmlrpc_async_call' ) ); 
  4684.  
  4685. $args = func_get_args(); 
  4686.  
  4687. if ( ! empty( $args[0] ) ) { 
  4688. call_user_func_array( array( $clients[$client_blog_id], 'addCall' ), $args ); 
  4689. } elseif ( is_multisite() ) { 
  4690. foreach ( $clients as $client_blog_id => $client ) { 
  4691. if ( ! $client_blog_id || empty( $client->calls ) ) { 
  4692. continue; 
  4693.  
  4694. $switch_success = switch_to_blog( $client_blog_id, true ); 
  4695. if ( ! $switch_success ) { 
  4696. continue; 
  4697.  
  4698. flush(); 
  4699. $client->query(); 
  4700.  
  4701. restore_current_blog(); 
  4702. } else { 
  4703. if ( isset( $clients[0] ) && ! empty( $clients[0]->calls ) ) { 
  4704. flush(); 
  4705. $clients[0]->query(); 
  4706.  
  4707. public static function staticize_subdomain( $url ) { 
  4708.  
  4709. // Extract hostname from URL 
  4710. $host = parse_url( $url, PHP_URL_HOST ); 
  4711.  
  4712. // Explode hostname on '.' 
  4713. $exploded_host = explode( '.', $host ); 
  4714.  
  4715. // Retrieve the name and TLD 
  4716. if ( count( $exploded_host ) > 1 ) { 
  4717. $name = $exploded_host[ count( $exploded_host ) - 2 ]; 
  4718. $tld = $exploded_host[ count( $exploded_host ) - 1 ]; 
  4719. // Rebuild domain excluding subdomains 
  4720. $domain = $name . '.' . $tld; 
  4721. } else { 
  4722. $domain = $host; 
  4723. // Array of Automattic domains 
  4724. $domain_whitelist = array( 'wordpress.com', 'wp.com' ); 
  4725.  
  4726. // Return $url if not an Automattic domain 
  4727. if ( ! in_array( $domain, $domain_whitelist ) ) { 
  4728. return $url; 
  4729.  
  4730. if ( is_ssl() ) { 
  4731. return preg_replace( '|https?://[^/]++/|', 'https://s-ssl.wordpress.com/', $url ); 
  4732.  
  4733. srand( crc32( basename( $url ) ) ); 
  4734. $static_counter = rand( 0, 2 ); 
  4735. srand(); // this resets everything that relies on this, like array_rand() and shuffle() 
  4736.  
  4737. return preg_replace( '|://[^/]+?/|', "://s$static_counter.wp.com/", $url ); 
  4738.  
  4739. /** JSON API Authorization */ 
  4740.  
  4741. /** 
  4742. * Handles the login action for Authorizing the JSON API 
  4743. */ 
  4744. function login_form_json_api_authorization() { 
  4745. $this->verify_json_api_authorization_request(); 
  4746.  
  4747. add_action( 'wp_login', array( &$this, 'store_json_api_authorization_token' ), 10, 2 ); 
  4748.  
  4749. add_action( 'login_message', array( &$this, 'login_message_json_api_authorization' ) ); 
  4750. add_action( 'login_form', array( &$this, 'preserve_action_in_login_form_for_json_api_authorization' ) ); 
  4751. add_filter( 'site_url', array( &$this, 'post_login_form_to_signed_url' ), 10, 3 ); 
  4752.  
  4753. // Make sure the login form is POSTed to the signed URL so we can reverify the request 
  4754. function post_login_form_to_signed_url( $url, $path, $scheme ) { 
  4755. if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) { 
  4756. return $url; 
  4757.  
  4758. $parsed_url = parse_url( $url ); 
  4759. $url = strtok( $url, '?' ); 
  4760. $url = "$url?{$_SERVER['QUERY_STRING']}"; 
  4761. if ( ! empty( $parsed_url['query'] ) ) 
  4762. $url .= "&{$parsed_url['query']}"; 
  4763.  
  4764. return $url; 
  4765.  
  4766. // Make sure the POSTed request is handled by the same action 
  4767. function preserve_action_in_login_form_for_json_api_authorization() { 
  4768. echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n"; 
  4769. echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) . "' />\n"; 
  4770.  
  4771. // If someone logs in to approve API access, store the Access Code in usermeta 
  4772. function store_json_api_authorization_token( $user_login, $user ) { 
  4773. add_filter( 'login_redirect', array( &$this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 ); 
  4774. add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_public_api_domain' ) ); 
  4775. $token = wp_generate_password( 32, false ); 
  4776. update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token ); 
  4777.  
  4778. // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access 
  4779. function allow_wpcom_public_api_domain( $domains ) { 
  4780. $domains[] = 'public-api.wordpress.com'; 
  4781. return $domains; 
  4782.  
  4783. // Add the Access Code details to the public-api.wordpress.com redirect 
  4784. function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) { 
  4785. return add_query_arg( 
  4786. urlencode_deep( 
  4787. array( 
  4788. 'jetpack-code' => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], true ),  
  4789. 'jetpack-user-id' => (int) $user->ID,  
  4790. 'jetpack-state' => $this->json_api_authorization_request['state'],  
  4791. ),  
  4792. $redirect_to 
  4793. ); 
  4794.  
  4795. // Verifies the request by checking the signature 
  4796. function verify_json_api_authorization_request() { 
  4797. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; 
  4798.  
  4799. $token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER ); 
  4800. if ( ! $token || empty( $token->secret ) ) { 
  4801. wp_die( __( 'You must connect your Jetpack plugin to WordPress.com to use this feature.' , 'jetpack' ) ); 
  4802.  
  4803. $die_error = __( 'Someone may be trying to trick you into giving them access to your site. Or it could be you just encountered a bug :). Either way, please close this window.', 'jetpack' ); 
  4804.  
  4805. $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) ); 
  4806.  
  4807. if ( isset( $_POST['jetpack_json_api_original_query'] ) ) { 
  4808. $signature = $jetpack_signature->sign_request( $_GET['token'], $_GET['timestamp'], $_GET['nonce'], '', 'GET', $_POST['jetpack_json_api_original_query'], null, true ); 
  4809. } else { 
  4810. $signature = $jetpack_signature->sign_current_request( array( 'body' => null, 'method' => 'GET' ) ); 
  4811.  
  4812. if ( ! $signature ) { 
  4813. wp_die( $die_error ); 
  4814. } else if ( is_wp_error( $signature ) ) { 
  4815. wp_die( $die_error ); 
  4816. } else if ( $signature !== $_GET['signature'] ) { 
  4817. if ( is_ssl() ) { 
  4818. // If we signed an HTTP request on the Jetpack Servers, but got redirected to HTTPS by the local blog, check the HTTP signature as well 
  4819. $signature = $jetpack_signature->sign_current_request( array( 'scheme' => 'http', 'body' => null, 'method' => 'GET' ) ); 
  4820. if ( ! $signature || is_wp_error( $signature ) || $signature !== $_GET['signature'] ) { 
  4821. wp_die( $die_error ); 
  4822. } else { 
  4823. wp_die( $die_error ); 
  4824.  
  4825. $timestamp = (int) $_GET['timestamp']; 
  4826. $nonce = stripslashes( (string) $_GET['nonce'] ); 
  4827.  
  4828. if ( ! $this->add_nonce( $timestamp, $nonce ) ) { 
  4829. // De-nonce the nonce, at least for 5 minutes. 
  4830. // We have to reuse this nonce at least once (used the first time when the initial request is made, used a second time when the login form is POSTed) 
  4831. $old_nonce_time = get_option( "jetpack_nonce_{$timestamp}_{$nonce}" ); 
  4832. if ( $old_nonce_time < time() - 300 ) { 
  4833. wp_die( __( 'The authorization process expired. Please go back and try again.' , 'jetpack' ) ); 
  4834.  
  4835. $data = json_decode( base64_decode( stripslashes( $_GET['data'] ) ) ); 
  4836. $data_filters = array( 
  4837. 'state' => 'opaque',  
  4838. 'client_id' => 'int',  
  4839. 'client_title' => 'string',  
  4840. 'client_image' => 'url',  
  4841. ); 
  4842.  
  4843. foreach ( $data_filters as $key => $sanitation ) { 
  4844. if ( ! isset( $data->$key ) ) { 
  4845. wp_die( $die_error ); 
  4846.  
  4847. switch ( $sanitation ) { 
  4848. case 'int' : 
  4849. $this->json_api_authorization_request[$key] = (int) $data->$key; 
  4850. break; 
  4851. case 'opaque' : 
  4852. $this->json_api_authorization_request[$key] = (string) $data->$key; 
  4853. break; 
  4854. case 'string' : 
  4855. $this->json_api_authorization_request[$key] = wp_kses( (string) $data->$key, array() ); 
  4856. break; 
  4857. case 'url' : 
  4858. $this->json_api_authorization_request[$key] = esc_url_raw( (string) $data->$key ); 
  4859. break; 
  4860.  
  4861. if ( empty( $this->json_api_authorization_request['client_id'] ) ) { 
  4862. wp_die( $die_error ); 
  4863.  
  4864. function login_message_json_api_authorization( $message ) { 
  4865. return '<p class="message">' . sprintf( 
  4866. esc_html__( '%s wants to access your site’s data. Log in to authorize that access.' , 'jetpack' ),  
  4867. '<strong>' . esc_html( $this->json_api_authorization_request['client_title'] ) . '</strong>' 
  4868. ) . '<img src="' . esc_url( $this->json_api_authorization_request['client_image'] ) . '" /></p>'; 
  4869.  
  4870. /** 
  4871. * Get $content_width, but with a <s>twist</s> filter. 
  4872. */ 
  4873. public static function get_content_width() { 
  4874. $content_width = isset( $GLOBALS['content_width'] ) ? $GLOBALS['content_width'] : false; 
  4875. /** 
  4876. * Filter the Content Width value. 
  4877. * @since 2.2.3 
  4878. * @param string $content_width Content Width value. 
  4879. */ 
  4880. return apply_filters( 'jetpack_content_width', $content_width ); 
  4881.  
  4882. /** 
  4883. * Centralize the function here until it gets added to core. 
  4884. * @param int|string|object $id_or_email A user ID, email address, or comment object 
  4885. * @param int $size Size of the avatar image 
  4886. * @param string $default URL to a default image to use if no avatar is available 
  4887. * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled 
  4888. * @return array First element is the URL, second is the class. 
  4889. */ 
  4890. public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) { 
  4891. // Don't bother adding the __return_true filter if it's already there. 
  4892. $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' ); 
  4893.  
  4894. if ( $force_display && ! $has_filter ) 
  4895. add_filter( 'pre_option_show_avatars', '__return_true' ); 
  4896.  
  4897. $avatar = get_avatar( $id_or_email, $size, $default ); 
  4898.  
  4899. if ( $force_display && ! $has_filter ) 
  4900. remove_filter( 'pre_option_show_avatars', '__return_true' ); 
  4901.  
  4902. // If no data, fail out. 
  4903. if ( is_wp_error( $avatar ) || ! $avatar ) 
  4904. return array( null, null ); 
  4905.  
  4906. // Pull out the URL. If it's not there, fail out. 
  4907. if ( ! preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $url_matches ) ) 
  4908. return array( null, null ); 
  4909. $url = wp_specialchars_decode( $url_matches[1], ENT_QUOTES ); 
  4910.  
  4911. // Pull out the class, but it's not a big deal if it's missing. 
  4912. $class = ''; 
  4913. if ( preg_match( '/class=["\']([^"\']+)["\']/', $avatar, $class_matches ) ) 
  4914. $class = wp_specialchars_decode( $class_matches[1], ENT_QUOTES ); 
  4915.  
  4916. return array( $url, $class ); 
  4917.  
  4918. /** 
  4919. * Pings the WordPress.com Mirror Site for the specified options. 
  4920. * @param string|array $option_names The option names to request from the WordPress.com Mirror Site 
  4921. * @return array An associative array of the option values as stored in the WordPress.com Mirror Site 
  4922. */ 
  4923. public function get_cloud_site_options( $option_names ) { 
  4924. $option_names = array_filter( (array) $option_names, 'is_string' ); 
  4925.  
  4926. Jetpack::load_xml_rpc_client(); 
  4927. $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) ); 
  4928. $xml->query( 'jetpack.fetchSiteOptions', $option_names ); 
  4929. if ( $xml->isError() ) { 
  4930. return array( 
  4931. 'error_code' => $xml->getErrorCode(),  
  4932. 'error_msg' => $xml->getErrorMessage(),  
  4933. ); 
  4934. $cloud_site_options = $xml->getResponse(); 
  4935.  
  4936. return $cloud_site_options; 
  4937.  
  4938. /** 
  4939. * Fetch the filtered array of options that we should compare to determine an identity crisis. 
  4940. * @return array An array of options to check. 
  4941. */ 
  4942. public static function identity_crisis_options_to_check() { 
  4943. $options = array( 
  4944. 'siteurl',  
  4945. 'home',  
  4946. ); 
  4947. /** 
  4948. * Filter the options that we should compare to determine an identity crisis. 
  4949. * @since 2.5.0 
  4950. * @param array $options Array of options to compare to determine an identity crisis. 
  4951. */ 
  4952. return apply_filters( 'jetpack_identity_crisis_options_to_check', $options ); 
  4953.  
  4954. /** 
  4955. * Checks to make sure that local options have the same values as remote options. Will cache the results for up to 24 hours. 
  4956. * @param bool $force_recheck Whether to ignore any cached transient and manually re-check. 
  4957. * @return array An array of options that do not match. If everything is good, it will evaluate to false. 
  4958. */ 
  4959. public static function check_identity_crisis( $force_recheck = false ) { 
  4960. if ( ! Jetpack::is_active() || Jetpack::is_development_mode() ) 
  4961. return false; 
  4962.  
  4963. if ( $force_recheck || false === ( $errors = get_transient( 'jetpack_has_identity_crisis' ) ) ) { 
  4964. $options_to_check = self::identity_crisis_options_to_check(); 
  4965. $cloud_options = Jetpack::init()->get_cloud_site_options( $options_to_check ); 
  4966. $errors = array(); 
  4967.  
  4968. foreach ( $cloud_options as $cloud_key => $cloud_value ) { 
  4969.  
  4970. // If it's not the same as the local value... 
  4971. if ( $cloud_value !== get_option( $cloud_key ) ) { 
  4972.  
  4973. // Break out if we're getting errors. We are going to check the error keys later when we alert. 
  4974. if ( 'error_code' == $cloud_key ) { 
  4975. $errors[ $cloud_key ] = $cloud_value; 
  4976. break; 
  4977.  
  4978. $parsed_cloud_value = parse_url( $cloud_value ); 
  4979. // If the current options is an IP address 
  4980. if ( filter_var( $parsed_cloud_value['host'], FILTER_VALIDATE_IP ) ) { 
  4981. // Give the new value a Jetpack to fly in to the clouds 
  4982. Jetpack::resolve_identity_crisis( $cloud_key ); 
  4983. continue; 
  4984.  
  4985. // And it's not been added to the whitelist... 
  4986. if ( ! self::is_identity_crisis_value_whitelisted( $cloud_key, $cloud_value ) ) { 
  4987. /** 
  4988. * This should be a temporary hack until a cleaner solution is found. 
  4989. * The siteurl and home can be set to use http in General > Settings 
  4990. * however some constants can be defined that can force https in wp-admin 
  4991. * when this happens wpcom can confuse wporg with a fake identity 
  4992. * crisis with a mismatch of http vs https when it should be allowed. 
  4993. * we need to check that here. 
  4994. * @see https://github.com/Automattic/jetpack/issues/1006 
  4995. */ 
  4996. if ( ( 'home' == $cloud_key || 'siteurl' == $cloud_key ) 
  4997. && ( substr( $cloud_value, 0, 8 ) == "https://" ) 
  4998. && Jetpack::init()->is_ssl_required_to_visit_site() ) { 
  4999. // Ok, we found a mismatch of http and https because of wp-config, not an invalid url 
  5000. continue; 
  5001.  
  5002.  
  5003. // Then kick an error! 
  5004. $errors[ $cloud_key ] = $cloud_value; 
  5005.  
  5006. /** 
  5007. * Filters the errors returned when checking for an Identity Crisis. 
  5008. * @since 2.3.2 
  5009. * @param array $errors Array of Identity Crisis errors. 
  5010. * @param bool $force_recheck Ignore any cached transient and manually re-check. Default to false. 
  5011. */ 
  5012. return apply_filters( 'jetpack_has_identity_crisis', $errors, $force_recheck ); 
  5013.  
  5014. /** 
  5015. * Resolve ID crisis 
  5016. * If the URL has changed, but the rest of the options are the same (i.e. blog/user tokens) 
  5017. * The user has the option to update the shadow site with the new URL before a new 
  5018. * token is created. 
  5019. * @param $key : Which option to sync. null defautlts to home and siteurl 
  5020. */ 
  5021. public static function resolve_identity_crisis( $key = null ) { 
  5022. if ( $key ) { 
  5023. $identity_options = array( $key ); 
  5024. } else { 
  5025. $identity_options = self::identity_crisis_options_to_check(); 
  5026.  
  5027. if ( is_array( $identity_options ) ) { 
  5028. foreach( $identity_options as $identity_option ) { 
  5029. Jetpack_Sync::sync_options( __FILE__, $identity_option ); 
  5030.  
  5031. /** 
  5032. * Fires when a shadow site option is updated. 
  5033. * These options are updated via the Identity Crisis UI. 
  5034. * $identity_option is the option that gets updated. 
  5035. * @since 3.7.0 
  5036. */ 
  5037. do_action( "update_option_{$identity_option}" ); 
  5038.  
  5039. /** 
  5040. * Whitelist URL 
  5041. * Ignore the URL differences between the blog and the shadow site. 
  5042. */ 
  5043. public static function whitelist_current_url() { 
  5044. $options_to_check = Jetpack::identity_crisis_options_to_check(); 
  5045. $cloud_options = Jetpack::init()->get_cloud_site_options( $options_to_check ); 
  5046.  
  5047. foreach ( $cloud_options as $cloud_key => $cloud_value ) { 
  5048. Jetpack::whitelist_identity_crisis_value( $cloud_key, $cloud_value ); 
  5049.  
  5050. /** 
  5051. * Ajax callbacks for ID crisis resolutions 
  5052. * Things that could happen here: 
  5053. * - site_migrated : Update the URL on the shadow blog to match new domain 
  5054. * - whitelist : Ignore the URL difference 
  5055. * - default : Error message 
  5056. */ 
  5057. public static function resolve_identity_crisis_ajax_callback() { 
  5058. check_ajax_referer( 'resolve-identity-crisis', 'ajax-nonce' ); 
  5059.  
  5060. switch ( $_POST[ 'crisis_resolution_action' ] ) { 
  5061. case 'site_migrated': 
  5062. Jetpack::resolve_identity_crisis(); 
  5063. echo 'resolved'; 
  5064. break; 
  5065.  
  5066. case 'whitelist': 
  5067. Jetpack::whitelist_current_url(); 
  5068. echo 'whitelisted'; 
  5069. break; 
  5070.  
  5071. case 'reset_connection': 
  5072. // Delete the options first so it doesn't get confused which site to disconnect dotcom-side 
  5073. Jetpack_Options::delete_option( 
  5074. array( 
  5075. 'register',  
  5076. 'blog_token',  
  5077. 'user_token',  
  5078. 'user_tokens',  
  5079. 'master_user',  
  5080. 'time_diff',  
  5081. 'fallback_no_verify_ssl_certs',  
  5082. 'id',  
  5083. ); 
  5084. delete_transient( 'jetpack_has_identity_crisis' ); 
  5085.  
  5086. echo 'reset-connection-success'; 
  5087. break; 
  5088.  
  5089. default: 
  5090. echo 'missing action'; 
  5091. break; 
  5092.  
  5093. wp_die(); 
  5094.  
  5095. /** 
  5096. * Adds a value to the whitelist for the specified key. 
  5097. * @param string $key The option name that we're whitelisting the value for. 
  5098. * @param string $value The value that we're intending to add to the whitelist. 
  5099. * @return bool Whether the value was added to the whitelist, or false if it was already there. 
  5100. */ 
  5101. public static function whitelist_identity_crisis_value( $key, $value ) { 
  5102. if ( Jetpack::is_identity_crisis_value_whitelisted( $key, $value ) ) { 
  5103. return false; 
  5104.  
  5105. $whitelist = Jetpack_Options::get_option( 'identity_crisis_whitelist', array() ); 
  5106. if ( empty( $whitelist[ $key ] ) || ! is_array( $whitelist[ $key ] ) ) { 
  5107. $whitelist[ $key ] = array(); 
  5108. array_push( $whitelist[ $key ], $value ); 
  5109.  
  5110. Jetpack_Options::update_option( 'identity_crisis_whitelist', $whitelist ); 
  5111. return true; 
  5112.  
  5113. /** 
  5114. * Checks whether a value is already whitelisted. 
  5115. * @param string $key The option name that we're checking the value for. 
  5116. * @param string $value The value that we're curious to see if it's on the whitelist. 
  5117. * @return bool Whether the value is whitelisted. 
  5118. */ 
  5119. public static function is_identity_crisis_value_whitelisted( $key, $value ) { 
  5120. $whitelist = Jetpack_Options::get_option( 'identity_crisis_whitelist', array() ); 
  5121. if ( ! empty( $whitelist[ $key ] ) && is_array( $whitelist[ $key ] ) && in_array( $value, $whitelist[ $key ] ) ) { 
  5122. return true; 
  5123. return false; 
  5124.  
  5125. /** 
  5126. * Checks whether the home and siteurl specifically are whitelisted 
  5127. * Written so that we don't have re-check $key and $value params every time 
  5128. * we want to check if this site is whitelisted, for example in footer.php 
  5129. * @return bool True = already whitelsisted False = not whitelisted 
  5130. */ 
  5131. public static function jetpack_is_staging_site() { 
  5132. $current_whitelist = Jetpack_Options::get_option( 'identity_crisis_whitelist' ); 
  5133. if ( ! $current_whitelist ) { 
  5134. return false; 
  5135.  
  5136. $options_to_check = Jetpack::identity_crisis_options_to_check(); 
  5137. $cloud_options = Jetpack::init()->get_cloud_site_options( $options_to_check ); 
  5138.  
  5139. foreach ( $cloud_options as $cloud_key => $cloud_value ) { 
  5140. if ( ! self::is_identity_crisis_value_whitelisted( $cloud_key, $cloud_value ) ) { 
  5141. return false; 
  5142. return true; 
  5143.  
  5144. public function identity_crisis_js( $nonce ) { 
  5145. ?> 
  5146. <script> 
  5147. (function( $ ) { 
  5148. var SECOND_IN_MS = 1000; 
  5149.  
  5150. function contactSupport( e ) { 
  5151. e.preventDefault(); 
  5152. $( '.jp-id-crisis-question' ).hide(); 
  5153. $( '#jp-id-crisis-contact-support' ).show(); 
  5154.  
  5155. function autodismissSuccessBanner() { 
  5156. $( '.jp-identity-crisis' ).fadeOut(600); //.addClass( 'dismiss' ); 
  5157.  
  5158. var data = { action: 'jetpack_resolve_identity_crisis', 'ajax-nonce': '<?php echo $nonce; ?>' }; 
  5159.  
  5160. $( document ).ready(function() { 
  5161.  
  5162. // Site moved: Update the URL on the shadow blog 
  5163. $( '.site-moved' ).click(function( e ) { 
  5164. e.preventDefault(); 
  5165. data.crisis_resolution_action = 'site_migrated'; 
  5166. $( '#jp-id-crisis-question-1 .spinner' ).show(); 
  5167. $.post( ajaxurl, data, function() { 
  5168. $( '.jp-id-crisis-question' ).hide(); 
  5169. $( '.banner-title' ).hide(); 
  5170. $( '#jp-id-crisis-success' ).show(); 
  5171. setTimeout( autodismissSuccessBanner, 6 * SECOND_IN_MS ); 
  5172. }); 
  5173.  
  5174. }); 
  5175.  
  5176. // URL hasn't changed, next question please. 
  5177. $( '.site-not-moved' ).click(function( e ) { 
  5178. e.preventDefault(); 
  5179. $( '.jp-id-crisis-question' ).hide(); 
  5180. $( '#jp-id-crisis-question-2' ).show(); 
  5181. }); 
  5182.  
  5183. // Reset connection: two separate sites. 
  5184. $( '.reset-connection' ).click(function( e ) { 
  5185. data.crisis_resolution_action = 'reset_connection'; 
  5186. $.post( ajaxurl, data, function( response ) { 
  5187. if ( 'reset-connection-success' === response ) { 
  5188. window.location.replace( '<?php echo Jetpack::admin_url(); ?>' ); 
  5189. }); 
  5190. }); 
  5191.  
  5192. // It's a dev environment. Ignore. 
  5193. $( '.is-dev-env' ).click(function( e ) { 
  5194. data.crisis_resolution_action = 'whitelist'; 
  5195. $( '#jp-id-crisis-question-2 .spinner' ).show(); 
  5196. $.post( ajaxurl, data, function() { 
  5197. $( '.jp-id-crisis-question' ).hide(); 
  5198. $( '.banner-title' ).hide(); 
  5199. $( '#jp-id-crisis-success' ).show(); 
  5200. setTimeout( autodismissSuccessBanner, 4 * SECOND_IN_MS ); 
  5201. }); 
  5202. }); 
  5203.  
  5204. $( '.not-reconnecting' ).click(contactSupport); 
  5205. $( '.not-staging-or-dev' ).click(contactSupport); 
  5206. }); 
  5207. })( jQuery ); 
  5208. </script> 
  5209. <?php 
  5210.  
  5211. /** 
  5212. * Displays an admin_notice, alerting the user to an identity crisis. 
  5213. */ 
  5214. public function alert_identity_crisis() { 
  5215. // @todo temporary killing of feature in 3.8.1 as it revealed a number of scenarios not foreseen. 
  5216. if ( ! Jetpack::is_development_version() ) { 
  5217. return; 
  5218.  
  5219. // @todo temporary copout for dealing with domain mapping 
  5220. // @see https://github.com/Automattic/jetpack/issues/2702 
  5221. if ( is_multisite() && defined( 'SUNRISE' ) && ! Jetpack::is_development_version() ) { 
  5222. return; 
  5223.  
  5224. if ( ! current_user_can( 'jetpack_disconnect' ) ) { 
  5225. return; 
  5226.  
  5227. if ( ! $errors = self::check_identity_crisis() ) { 
  5228. return; 
  5229.  
  5230. // Only show on dashboard and jetpack pages 
  5231. $screen = get_current_screen(); 
  5232. if ( 'dashboard' !== $screen->base && ! did_action( 'jetpack_notices' ) ) { 
  5233. return; 
  5234.  
  5235. // Include the js! 
  5236. $ajax_nonce = wp_create_nonce( 'resolve-identity-crisis' ); 
  5237. $this->identity_crisis_js( $ajax_nonce ); 
  5238.  
  5239. // Include the CSS! 
  5240. if ( ! wp_script_is( 'jetpack', 'done' ) ) { 
  5241. $this->admin_banner_styles(); 
  5242.  
  5243. if ( ! array_key_exists( 'error_code', $errors ) ) { 
  5244. $key = 'siteurl'; 
  5245. if ( ! $errors[ $key ] ) { 
  5246. $key = 'home'; 
  5247. } else { 
  5248. $key = 'error_code'; 
  5249. // 401 is the only error we care about. Any other errors should not trigger the alert. 
  5250. if ( 401 !== $errors[ $key ] ) { 
  5251. return; 
  5252.  
  5253. ?> 
  5254.  
  5255. <style> 
  5256. .jp-identity-crisis .jp-btn-group { 
  5257. margin: 15px 0; 
  5258. .jp-identity-crisis strong { 
  5259. color: #518d2a; 
  5260. .jp-identity-crisis.dismiss { 
  5261. display: none; 
  5262. .jp-identity-crisis .button { 
  5263. margin-right: 4px; 
  5264. </style> 
  5265.  
  5266. <div id="message" class="error jetpack-message jp-identity-crisis stay-visible"> 
  5267. <div class="service-mark"></div> 
  5268. <div class="jp-id-banner__content"> 
  5269. <!-- <h3 class="banner-title"><?php _e( 'Something\'s not quite right with your Jetpack connection! Let\'s fix that.', 'jetpack' ); ?></h3> --> 
  5270.  
  5271. <div class="jp-id-crisis-question" id="jp-id-crisis-question-1"> 
  5272. <?php 
  5273. // 401 means that this site has been disconnected from wpcom, but the remote site still thinks it's connected. 
  5274. if ( 'error_code' == $key && '401' == $errors[ $key ] ) : ?> 
  5275. <div class="banner-content"> 
  5276. <p><?php 
  5277. /** translators: %s is a URL */ 
  5278. printf( __( 'Our records show that this site does not have a valid connection to WordPress.com. Please reset your connection to fix this. <a href="%s" target="_blank">What caused this?</a>', 'jetpack' ), 'https://jetpack.me/support/no-valid-wordpress-com-connection/' ); 
  5279. ?></p> 
  5280. </div> 
  5281. <div class="jp-btn-group"> 
  5282. <a href="#" class="reset-connection"><?php _e( 'Reset the connection', 'jetpack' ); ?></a> 
  5283. <span class="idc-separator">|</span> 
  5284. <a href="<?php echo esc_url( wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=dismiss' ), 'jetpack-deactivate' ) ); ?>"><?php _e( 'Deactivate Jetpack', 'jetpack' ); ?></a> 
  5285. </div> 
  5286. <?php else : ?> 
  5287. <div class="banner-content"> 
  5288. <p><?php printf( __( 'It looks like you may have changed your domain. Is <strong>%1$s</strong> still your site\'s domain, or have you updated it to <strong> %2$s </strong>?', 'jetpack' ), $errors[ $key ], (string) get_option( $key ) ); ?></p> 
  5289. </div> 
  5290. <div class="jp-btn-group"> 
  5291. <a href="#" class="regular site-moved"><?php printf( __( '%s is now my domain.', 'jetpack' ), $errors[ $key ] ); ?></a> <span class="idc-separator">|</span> <a href="#" class="site-not-moved" ><?php printf( __( '%s is still my domain.', 'jetpack' ), (string) get_option( $key ) ); ?></a> 
  5292. <span class="spinner"></span> 
  5293. </div> 
  5294. <?php endif ; ?> 
  5295. </div> 
  5296.  
  5297. <div class="jp-id-crisis-question" id="jp-id-crisis-question-2" style="display: none;"> 
  5298. <div class="banner-content"> 
  5299. <p><?php printf( 
  5300. /** translators: %1$s, %2$s and %3$s are URLs */ 
  5301. __( 
  5302. 'Are <strong> %2$s </strong> and <strong> %1$s </strong> two completely separate websites? If so we should create a new connection, which will reset your followers and linked services. <a href="%3$s"><em>What does this mean?</em></a>',  
  5303. 'jetpack' 
  5304. ),  
  5305. $errors[ $key ],  
  5306. (string) get_option( $key ),  
  5307. 'https://jetpack.me/support/what-does-resetting-the-connection-mean/' 
  5308. ); ?></p> 
  5309. </div> 
  5310. <div class="jp-btn-group"> 
  5311. <a href="#" class="reset-connection"><?php _e( 'Reset the connection', 'jetpack' ); ?></a> <span class="idc-separator">|</span> 
  5312. <a href="#" class="is-dev-env"><?php _e( 'This is a development environment', 'jetpack' ); ?></a> <span class="idc-separator">|</span> 
  5313. <a href="https://jetpack.me/contact-support/" class="contact-support"><?php _e( 'Submit a support ticket', 'jetpack' ); ?></a> 
  5314. <span class="spinner"></span> 
  5315. </div> 
  5316. </div> 
  5317.  
  5318. <div class="jp-id-crisis-success" id="jp-id-crisis-success" style="display: none;"> 
  5319. <h3 class="success-notice"><?php printf( __( 'Thanks for taking the time to sort things out. We've updated our records accordingly!', 'jetpack' ) ); ?></h3> 
  5320. </div> 
  5321. </div> 
  5322. </div> 
  5323.  
  5324. <?php 
  5325.  
  5326. /** 
  5327. * Maybe Use a .min.css stylesheet, maybe not. 
  5328. * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args. 
  5329. */ 
  5330. public static function maybe_min_asset( $url, $path, $plugin ) { 
  5331. // Short out on things trying to find actual paths. 
  5332. if ( ! $path || empty( $plugin ) ) { 
  5333. return $url; 
  5334.  
  5335. // Strip out the abspath. 
  5336. $base = dirname( plugin_basename( $plugin ) ); 
  5337.  
  5338. // Short out on non-Jetpack assets. 
  5339. if ( 'jetpack/' !== substr( $base, 0, 8 ) ) { 
  5340. return $url; 
  5341.  
  5342. // File name parsing. 
  5343. $file = "{$base}/{$path}"; 
  5344. $full_path = JETPACK__PLUGIN_DIR . substr( $file, 8 ); 
  5345. $file_name = substr( $full_path, strrpos( $full_path, '/' ) + 1 ); 
  5346. $file_name_parts_r = array_reverse( explode( '.', $file_name ) ); 
  5347. $extension = array_shift( $file_name_parts_r ); 
  5348.  
  5349. if ( in_array( strtolower( $extension ), array( 'css', 'js' ) ) ) { 
  5350. // Already pointing at the minified version. 
  5351. if ( 'min' === $file_name_parts_r[0] ) { 
  5352. return $url; 
  5353.  
  5354. $min_full_path = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $full_path ); 
  5355. if ( file_exists( $min_full_path ) ) { 
  5356. $url = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $url ); 
  5357.  
  5358. return $url; 
  5359.  
  5360. /** 
  5361. * Maybe inlines a stylesheet. 
  5362. * If you'd like to inline a stylesheet instead of printing a link to it,  
  5363. * wp_style_add_data( 'handle', 'jetpack-inline', true ); 
  5364. * Attached to `style_loader_tag` filter. 
  5365. * @param string $tag The tag that would link to the external asset. 
  5366. * @param string $handle The registered handle of the script in question. 
  5367. * @return string 
  5368. */ 
  5369. public static function maybe_inline_style( $tag, $handle ) { 
  5370. global $wp_styles; 
  5371. $item = $wp_styles->registered[ $handle ]; 
  5372.  
  5373. if ( ! isset( $item->extra['jetpack-inline'] ) || ! $item->extra['jetpack-inline'] ) { 
  5374. return $tag; 
  5375.  
  5376. if ( preg_match( '# href=\'([^\']+)\' #i', $tag, $matches ) ) { 
  5377. $href = $matches[1]; 
  5378. // Strip off query string 
  5379. if ( $pos = strpos( $href, '?' ) ) { 
  5380. $href = substr( $href, 0, $pos ); 
  5381. // Strip off fragment 
  5382. if ( $pos = strpos( $href, '#' ) ) { 
  5383. $href = substr( $href, 0, $pos ); 
  5384. } else { 
  5385. return $tag; 
  5386.  
  5387. $plugins_dir = plugin_dir_url( JETPACK__PLUGIN_FILE ); 
  5388. if ( $plugins_dir !== substr( $href, 0, strlen( $plugins_dir ) ) ) { 
  5389. return $tag; 
  5390.  
  5391. // If this stylesheet has a RTL version, and the RTL version replaces normal... 
  5392. if ( isset( $item->extra['rtl'] ) && 'replace' === $item->extra['rtl'] && is_rtl() ) { 
  5393. // And this isn't the pass that actually deals with the RTL version... 
  5394. if ( false === strpos( $tag, " id='$handle-rtl-css' " ) ) { 
  5395. // Short out, as the RTL version will deal with it in a moment. 
  5396. return $tag; 
  5397.  
  5398. $file = JETPACK__PLUGIN_DIR . substr( $href, strlen( $plugins_dir ) ); 
  5399. $css = Jetpack::absolutize_css_urls( file_get_contents( $file ), $href ); 
  5400. if ( $css ) { 
  5401. $tag = "<!-- Inline {$item->handle} -->\r\n"; 
  5402. if ( empty( $item->extra['after'] ) ) { 
  5403. wp_add_inline_style( $handle, $css ); 
  5404. } else { 
  5405. array_unshift( $item->extra['after'], $css ); 
  5406. wp_style_add_data( $handle, 'after', $item->extra['after'] ); 
  5407.  
  5408. return $tag; 
  5409.  
  5410. /** 
  5411. * Loads a view file from the views 
  5412. * Data passed in with the $data parameter will be available in the 
  5413. * template file as $data['value'] 
  5414. * @param string $template - Template file to load 
  5415. * @param array $data - Any data to pass along to the template 
  5416. * @return boolean - If template file was found 
  5417. **/ 
  5418. public function load_view( $template, $data = array() ) { 
  5419. $views_dir = JETPACK__PLUGIN_DIR . 'views/'; 
  5420.  
  5421. if( file_exists( $views_dir . $template ) ) { 
  5422. require_once( $views_dir . $template ); 
  5423. return true; 
  5424.  
  5425. error_log( "Jetpack: Unable to find view file $views_dir$template" ); 
  5426. return false; 
  5427.  
  5428. /** 
  5429. * Sends a ping to the Jetpack servers to toggle on/off remote portions 
  5430. * required by some modules. 
  5431. * @param string $module_slug 
  5432. */ 
  5433. public function toggle_module_on_wpcom( $module_slug ) { 
  5434. Jetpack::init()->sync->register( 'noop' ); 
  5435.  
  5436. if ( false !== strpos( current_filter(), 'jetpack_activate_module_' ) ) { 
  5437. self::check_privacy( $module_slug ); 
  5438.  
  5439.  
  5440. /** 
  5441. * Throws warnings for deprecated hooks to be removed from Jetpack 
  5442. */ 
  5443. public function deprecated_hooks() { 
  5444. global $wp_filter; 
  5445.  
  5446. /** 
  5447. * Format: 
  5448. * deprecated_filter_name => replacement_name 
  5449. * If there is no replacement us null for replacement_name 
  5450. */ 
  5451. $deprecated_list = array( 
  5452. 'jetpack_bail_on_shortcode' => 'jetpack_shortcodes_to_include',  
  5453. 'wpl_sharing_2014_1' => null,  
  5454. ); 
  5455.  
  5456. // This is a silly loop depth. Better way? 
  5457. foreach( $deprecated_list AS $hook => $hook_alt ) { 
  5458. if( isset( $wp_filter[ $hook ] ) && is_array( $wp_filter[ $hook ] ) ) { 
  5459. foreach( $wp_filter[$hook] AS $func => $values ) { 
  5460. foreach( $values AS $hooked ) { 
  5461. _deprecated_function( $hook . ' used for ' . $hooked['function'], null, $hook_alt ); 
  5462.  
  5463. /** 
  5464. * Converts any url in a stylesheet, to the correct absolute url. 
  5465. * Considerations: 
  5466. * - Normal, relative URLs `feh.png` 
  5467. * - Data URLs `data:image/gif;base64, eh129ehiuehjdhsa==` 
  5468. * - Schema-agnostic URLs `//domain.com/feh.png` 
  5469. * - Absolute URLs `http://domain.com/feh.png` 
  5470. * - Domain root relative URLs `/feh.png` 
  5471. * @param $css string: The raw CSS -- should be read in directly from the file. 
  5472. * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from. 
  5473. * @return mixed|string 
  5474. */ 
  5475. public static function absolutize_css_urls( $css, $css_file_url ) { 
  5476. $pattern = '#url\((?P<path>[^)]*)\)#i'; 
  5477. $css_dir = dirname( $css_file_url ); 
  5478. $p = parse_url( $css_dir ); 
  5479. $domain = sprintf( 
  5480. '%1$s//%2$s%3$s%4$s',  
  5481. isset( $p['scheme'] ) ? "{$p['scheme']}:" : '',  
  5482. isset( $p['user'], $p['pass'] ) ? "{$p['user']}:{$p['pass']}@" : '',  
  5483. $p['host'],  
  5484. isset( $p['port'] ) ? ":{$p['port']}" : '' 
  5485. ); 
  5486.  
  5487. if ( preg_match_all( $pattern, $css, $matches, PREG_SET_ORDER ) ) { 
  5488. $find = $replace = array(); 
  5489. foreach ( $matches as $match ) { 
  5490. $url = trim( $match['path'], "'\" \t" ); 
  5491.  
  5492. // If this is a data url, we don't want to mess with it. 
  5493. if ( 'data:' === substr( $url, 0, 5 ) ) { 
  5494. continue; 
  5495.  
  5496. // If this is an absolute or protocol-agnostic url,  
  5497. // we don't want to mess with it. 
  5498. if ( preg_match( '#^(https?:)?//#i', $url ) ) { 
  5499. continue; 
  5500.  
  5501. switch ( substr( $url, 0, 1 ) ) { 
  5502. case '/': 
  5503. $absolute = $domain . $url; 
  5504. break; 
  5505. default: 
  5506. $absolute = $css_dir . '/' . $url; 
  5507.  
  5508. $find[] = $match[0]; 
  5509. $replace[] = sprintf( 'url("%s")', $absolute ); 
  5510. $css = str_replace( $find, $replace, $css ); 
  5511.  
  5512. return $css; 
  5513.  
  5514. /** 
  5515. * This method checks to see if SSL is required by the site in 
  5516. * order to visit it in some way other than only setting the 
  5517. * https value in the home or siteurl values. 
  5518. * @since 3.2 
  5519. * @return boolean 
  5520. **/ 
  5521. private function is_ssl_required_to_visit_site() { 
  5522. global $wp_version; 
  5523. $ssl = is_ssl(); 
  5524.  
  5525. if ( version_compare( $wp_version, '4.4-alpha', '<=' ) && force_ssl_login() ) { // force_ssl_login deprecated WP 4.4. 
  5526. $ssl = true; 
  5527. } else if ( force_ssl_admin() ) { 
  5528. $ssl = true; 
  5529. return $ssl; 
  5530.  
  5531. /** 
  5532. * This methods removes all of the registered css files on the frontend 
  5533. * from Jetpack in favor of using a single file. In effect "imploding" 
  5534. * all the files into one file. 
  5535. * Pros: 
  5536. * - Uses only ONE css asset connection instead of 15 
  5537. * - Saves a minimum of 56k 
  5538. * - Reduces server load 
  5539. * - Reduces time to first painted byte 
  5540. * Cons: 
  5541. * - Loads css for ALL modules. However all selectors are prefixed so it 
  5542. * should not cause any issues with themes. 
  5543. * - Plugins/themes dequeuing styles no longer do anything. See 
  5544. * jetpack_implode_frontend_css filter for a workaround 
  5545. * For some situations developers may wish to disable css imploding and 
  5546. * instead operate in legacy mode where each file loads seperately and 
  5547. * can be edited individually or dequeued. This can be accomplished with 
  5548. * the following line: 
  5549. * add_filter( 'jetpack_implode_frontend_css', '__return_false' ); 
  5550. * @since 3.2 
  5551. **/ 
  5552. public function implode_frontend_css( $travis_test = false ) { 
  5553. $do_implode = true; 
  5554. if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { 
  5555. $do_implode = false; 
  5556.  
  5557. /** 
  5558. * Allow CSS to be concatenated into a single jetpack.css file. 
  5559. * @since 3.2.0 
  5560. * @param bool $do_implode Should CSS be concatenated? Default to true. 
  5561. */ 
  5562. $do_implode = apply_filters( 'jetpack_implode_frontend_css', $do_implode ); 
  5563.  
  5564. // Do not use the imploded file when default behaviour was altered through the filter 
  5565. if ( ! $do_implode ) { 
  5566. return; 
  5567.  
  5568. // We do not want to use the imploded file in dev mode, or if not connected 
  5569. if ( Jetpack::is_development_mode() || ! self::is_active() ) { 
  5570. if ( ! $travis_test ) { 
  5571. return; 
  5572.  
  5573. // Do not use the imploded file if sharing css was dequeued via the sharing settings screen 
  5574. if ( get_option( 'sharedaddy_disable_resources' ) ) { 
  5575. return; 
  5576.  
  5577. /** 
  5578. * Now we assume Jetpack is connected and able to serve the single 
  5579. * file. 
  5580. * In the future there will be a check here to serve the file locally 
  5581. * or potentially from the Jetpack CDN 
  5582. * For now: 
  5583. * - Enqueue a single imploded css file 
  5584. * - Zero out the style_loader_tag for the bundled ones 
  5585. * - Be happy, drink scotch 
  5586. */ 
  5587.  
  5588. add_filter( 'style_loader_tag', array( $this, 'concat_remove_style_loader_tag' ), 10, 2 ); 
  5589.  
  5590. $version = Jetpack::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . 'css/jetpack.css' ) : JETPACK__VERSION; 
  5591.  
  5592. wp_enqueue_style( 'jetpack_css', plugins_url( 'css/jetpack.css', __FILE__ ), array(), $version ); 
  5593. wp_style_add_data( 'jetpack_css', 'rtl', 'replace' ); 
  5594.  
  5595. function concat_remove_style_loader_tag( $tag, $handle ) { 
  5596. if ( in_array( $handle, $this->concatenated_style_handles ) ) { 
  5597. $tag = ''; 
  5598. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  5599. $tag = "<!-- `" . esc_html( $handle ) . "` is included in the concatenated jetpack.css -->\r\n"; 
  5600.  
  5601. return $tag; 
  5602.  
  5603. /** 
  5604. * Check the heartbeat data 
  5605. * Organizes the heartbeat data by severity. For example, if the site 
  5606. * is in an ID crisis, it will be in the $filtered_data['bad'] array. 
  5607. * Data will be added to "caution" array, if it either: 
  5608. * - Out of date Jetpack version 
  5609. * - Out of date WP version 
  5610. * - Out of date PHP version 
  5611. * $return array $filtered_data 
  5612. */ 
  5613. public static function jetpack_check_heartbeat_data() { 
  5614. $raw_data = Jetpack_Heartbeat::generate_stats_array(); 
  5615.  
  5616. $good = array(); 
  5617. $caution = array(); 
  5618. $bad = array(); 
  5619.  
  5620. foreach ( $raw_data as $stat => $value ) { 
  5621.  
  5622. // Check jetpack version 
  5623. if ( 'version' == $stat ) { 
  5624. if ( version_compare( $value, JETPACK__VERSION, '<' ) ) { 
  5625. $caution[ $stat ] = $value . " - min supported is " . JETPACK__VERSION; 
  5626. continue; 
  5627.  
  5628. // Check WP version 
  5629. if ( 'wp-version' == $stat ) { 
  5630. if ( version_compare( $value, JETPACK__MINIMUM_WP_VERSION, '<' ) ) { 
  5631. $caution[ $stat ] = $value . " - min supported is " . JETPACK__MINIMUM_WP_VERSION; 
  5632. continue; 
  5633.  
  5634. // Check PHP version 
  5635. if ( 'php-version' == $stat ) { 
  5636. if ( version_compare( PHP_VERSION, '5.2.4', '<' ) ) { 
  5637. $caution[ $stat ] = $value . " - min supported is 5.2.4"; 
  5638. continue; 
  5639.  
  5640. // Check ID crisis 
  5641. if ( 'identitycrisis' == $stat ) { 
  5642. if ( 'yes' == $value ) { 
  5643. $bad[ $stat ] = $value; 
  5644. continue; 
  5645.  
  5646. // The rest are good :) 
  5647. $good[ $stat ] = $value; 
  5648.  
  5649. $filtered_data = array( 
  5650. 'good' => $good,  
  5651. 'caution' => $caution,  
  5652. 'bad' => $bad 
  5653. ); 
  5654.  
  5655. return $filtered_data; 
  5656.  
  5657.  
  5658. /** 
  5659. * This method is used to organize all options that can be reset 
  5660. * without disconnecting Jetpack. 
  5661. * It is used in class.jetpack-cli.php to reset options 
  5662. * @return array of options to delete. 
  5663. */ 
  5664. public static function get_jetpack_options_for_reset() { 
  5665. $jetpack_options = Jetpack_Options::get_option_names(); 
  5666. $jetpack_options_non_compat = Jetpack_Options::get_option_names( 'non_compact' ); 
  5667. $jetpack_options_private = Jetpack_Options::get_option_names( 'private' ); 
  5668.  
  5669. $all_jp_options = array_merge( $jetpack_options, $jetpack_options_non_compat, $jetpack_options_private ); 
  5670.  
  5671. // A manual build of the wp options 
  5672. $wp_options = array( 
  5673. 'sharing-options',  
  5674. 'disabled_likes',  
  5675. 'disabled_reblogs',  
  5676. 'jetpack_comments_likes_enabled',  
  5677. 'wp_mobile_excerpt',  
  5678. 'wp_mobile_featured_images',  
  5679. 'wp_mobile_app_promos',  
  5680. 'stats_options',  
  5681. 'stats_dashboard_widget',  
  5682. 'safecss_preview_rev',  
  5683. 'safecss_rev',  
  5684. 'safecss_revision_migrated',  
  5685. 'nova_menu_order',  
  5686. 'jetpack_portfolio',  
  5687. 'jetpack_portfolio_posts_per_page',  
  5688. 'jetpack_testimonial',  
  5689. 'jetpack_testimonial_posts_per_page',  
  5690. 'wp_mobile_custom_css',  
  5691. 'sharedaddy_disable_resources',  
  5692. 'sharing-options',  
  5693. 'sharing-services',  
  5694. 'site_icon_temp_data',  
  5695. 'featured-content',  
  5696. 'site_logo',  
  5697. ); 
  5698.  
  5699. // Flag some Jetpack options as unsafe 
  5700. $unsafe_options = array( 
  5701. 'id', // (int) The Client ID/WP.com Blog ID of this site. 
  5702. 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. 
  5703. 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time 
  5704. 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed. 
  5705.  
  5706. // non_compact 
  5707. 'activated',  
  5708.  
  5709. // private 
  5710. 'register',  
  5711. 'blog_token', // (string) The Client Secret/Blog Token of this site. 
  5712. 'user_token', // (string) The User Token of this site. (deprecated) 
  5713. 'user_tokens' 
  5714. ); 
  5715.  
  5716. // Remove the unsafe Jetpack options 
  5717. foreach ( $unsafe_options as $unsafe_option ) { 
  5718. if ( false !== ( $key = array_search( $unsafe_option, $all_jp_options ) ) ) { 
  5719. unset( $all_jp_options[ $key ] ); 
  5720.  
  5721. $options = array( 
  5722. 'jp_options' => $all_jp_options,  
  5723. 'wp_options' => $wp_options 
  5724. ); 
  5725.  
  5726. return $options; 
  5727.  
  5728. /** 
  5729. * Check if an option of a Jetpack module has been updated. 
  5730. * If any module option has been updated before Jump Start has been dismissed,  
  5731. * update the 'jumpstart' option so we can hide Jump Start. 
  5732. */ 
  5733. public static function jumpstart_has_updated_module_option( $option_name = '' ) { 
  5734. // Bail if Jump Start has already been dismissed 
  5735. if ( 'new_connection' !== Jetpack::get_option( 'jumpstart' ) ) { 
  5736. return false; 
  5737.  
  5738. $jetpack = Jetpack::init(); 
  5739.  
  5740.  
  5741. // Manual build of module options 
  5742. $option_names = self::get_jetpack_options_for_reset(); 
  5743.  
  5744. if ( in_array( $option_name, $option_names['wp_options'] ) ) { 
  5745. Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' ); 
  5746.  
  5747. //Jump start is being dismissed send data to MC Stats 
  5748. $jetpack->stat( 'jumpstart', 'manual, '.$option_name ); 
  5749.  
  5750. $jetpack->do_stats( 'server_side' ); 
  5751.  
  5752.  
  5753. /** 
  5754. * Strip http:// or https:// from a url, replaces forward slash with ::,  
  5755. * so we can bring them directly to their site in calypso. 
  5756. * @param string | url 
  5757. * @return string | url without the guff 
  5758. */ 
  5759. public static function build_raw_urls( $url ) { 
  5760. $strip_http = '/.*?:\/\//i'; 
  5761. $url = preg_replace( $strip_http, '', $url ); 
  5762. $url = str_replace( '/', '::', $url ); 
  5763. return $url; 
  5764.  
  5765. /** 
  5766. * Stores and prints out domains to prefetch for page speed optimization. 
  5767. * @param mixed $new_urls 
  5768. */ 
  5769. public static function dns_prefetch( $new_urls = null ) { 
  5770. static $prefetch_urls = array(); 
  5771. if ( empty( $new_urls ) && ! empty( $prefetch_urls ) ) { 
  5772. echo "\r\n"; 
  5773. foreach ( $prefetch_urls as $this_prefetch_url ) { 
  5774. printf( "<link rel='dns-prefetch' href='%s'>\r\n", esc_attr( $this_prefetch_url ) ); 
  5775. } elseif ( ! empty( $new_urls ) ) { 
  5776. if ( ! has_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) ) ) { 
  5777. add_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) ); 
  5778. foreach ( (array) $new_urls as $this_new_url ) { 
  5779. $prefetch_urls[] = strtolower( untrailingslashit( preg_replace( '#^https?://#i', '//', $this_new_url ) ) ); 
  5780. $prefetch_urls = array_unique( $prefetch_urls ); 
  5781.  
  5782. public function wp_dashboard_setup() { 
  5783. if ( self::is_active() ) { 
  5784. add_action( 'jetpack_dashboard_widget', array( __CLASS__, 'dashboard_widget_footer' ), 999 ); 
  5785. $widget_title = __( 'Site Stats', 'jetpack' ); 
  5786. } elseif ( ! self::is_development_mode() && current_user_can( 'jetpack_connect' ) ) { 
  5787. add_action( 'jetpack_dashboard_widget', array( $this, 'dashboard_widget_connect_to_wpcom' ) ); 
  5788. $widget_title = __( 'Please Connect Jetpack', 'jetpack' ); 
  5789.  
  5790. if ( has_action( 'jetpack_dashboard_widget' ) ) { 
  5791. wp_add_dashboard_widget( 
  5792. 'jetpack_summary_widget',  
  5793. $widget_title,  
  5794. array( __CLASS__, 'dashboard_widget' ) 
  5795. ); 
  5796. wp_enqueue_style( 'jetpack-dashboard-widget', plugins_url( 'css/dashboard-widget.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); 
  5797.  
  5798. // If we're inactive and not in development mode, sort our box to the top. 
  5799. if ( ! self::is_active() && ! self::is_development_mode() ) { 
  5800. global $wp_meta_boxes; 
  5801.  
  5802. $dashboard = $wp_meta_boxes['dashboard']['normal']['core']; 
  5803. $ours = array( 'jetpack_summary_widget' => $dashboard['jetpack_summary_widget'] ); 
  5804.  
  5805. $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard ); 
  5806.  
  5807. /** 
  5808. * @param mixed $result Value for the user's option 
  5809. * @return mixed 
  5810. */ 
  5811. function get_user_option_meta_box_order_dashboard( $sorted ) { 
  5812. if ( ! is_array( $sorted ) ) { 
  5813. return $sorted; 
  5814.  
  5815. foreach ( $sorted as $box_context => $ids ) { 
  5816. if ( false === strpos( $ids, 'dashboard_stats' ) ) { 
  5817. // If the old id isn't anywhere in the ids, don't bother exploding and fail out. 
  5818. continue; 
  5819.  
  5820. $ids_array = explode( ', ', $ids ); 
  5821. $key = array_search( 'dashboard_stats', $ids_array ); 
  5822.  
  5823. if ( false !== $key ) { 
  5824. // If we've found that exact value in the option (and not `google_dashboard_stats` for example) 
  5825. $ids_array[ $key ] = 'jetpack_summary_widget'; 
  5826. $sorted[ $box_context ] = implode( ', ', $ids_array ); 
  5827. // We've found it, stop searching, and just return. 
  5828. break; 
  5829.  
  5830. return $sorted; 
  5831.  
  5832. public static function dashboard_widget() { 
  5833. /** 
  5834. * Fires when the dashboard is loaded. 
  5835. * @since 3.4.0 
  5836. */ 
  5837. do_action( 'jetpack_dashboard_widget' ); 
  5838.  
  5839. public static function dashboard_widget_footer() { 
  5840. ?> 
  5841. <footer> 
  5842.  
  5843. <div class="protect"> 
  5844. <?php if ( Jetpack::is_module_active( 'protect' ) ) : ?> 
  5845. <h3><?php echo number_format_i18n( get_site_option( 'jetpack_protect_blocked_attempts', 0 ) ); ?></h3> 
  5846. <p><?php echo esc_html_x( 'Blocked malicious login attempts', '{#} Blocked malicious login attempts -- number is on a prior line, text is a caption.', 'jetpack' ); ?></p> 
  5847. <?php elseif ( current_user_can( 'jetpack_activate_modules' ) && ! self::is_development_mode() ) : ?> 
  5848. <a href="<?php echo esc_url( wp_nonce_url( Jetpack::admin_url( array( 'action' => 'activate', 'module' => 'protect' ) ), 'jetpack_activate-protect' ) ); ?>" class="button button-jetpack" title="<?php esc_attr_e( 'Protect helps to keep you secure from brute-force login attacks.', 'jetpack' ); ?>"> 
  5849. <?php esc_html_e( 'Activate Protect', 'jetpack' ); ?> 
  5850. </a> 
  5851. <?php else : ?> 
  5852. <?php esc_html_e( 'Protect is inactive.', 'jetpack' ); ?> 
  5853. <?php endif; ?> 
  5854. </div> 
  5855.  
  5856. <div class="akismet"> 
  5857. <?php if ( is_plugin_active( 'akismet/akismet.php' ) ) : ?> 
  5858. <h3><?php echo number_format_i18n( get_option( 'akismet_spam_count', 0 ) ); ?></h3> 
  5859. <p><?php echo esc_html_x( 'Spam comments blocked by Akismet.', '{#} Spam comments blocked by Akismet -- number is on a prior line, text is a caption.', 'jetpack' ); ?></p> 
  5860. <?php elseif ( current_user_can( 'activate_plugins' ) && ! is_wp_error( validate_plugin( 'akismet/akismet.php' ) ) ) : ?> 
  5861. <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'activate', 'plugin' => 'akismet/akismet.php' ), admin_url( 'plugins.php' ) ), 'activate-plugin_akismet/akismet.php' ) ); ?>" class="button button-jetpack"> 
  5862. <?php esc_html_e( 'Activate Akismet', 'jetpack' ); ?> 
  5863. </a> 
  5864. <?php else : ?> 
  5865. <p><a href="<?php echo esc_url( 'https://akismet.com/?utm_source=jetpack&utm_medium=link&utm_campaign=Jetpack%20Dashboard%20Widget%20Footer%20Link' ); ?>"><?php esc_html_e( 'Akismet can help to keep your blog safe from spam!', 'jetpack' ); ?></a></p> 
  5866. <?php endif; ?> 
  5867. </div> 
  5868.  
  5869.  
  5870. <?php if ( ! current_user_can( 'edit_posts' ) && self::is_user_connected() ) : ?> 
  5871. <div style="width: 100%; text-align: center; padding-top: 20px; clear: both;"><a class="button" title="<?php esc_attr_e( 'Unlink your account from WordPress.com', 'jetpack' ); ?>" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'unlink', 'redirect' => 'sub-unlink' ), admin_url( 'index.php' ) ), 'jetpack-unlink' ) ); ?>"><?php esc_html_e( 'Unlink your account from WordPress.com', 'jetpack' ); ?></a></div> 
  5872. <?php endif; ?> 
  5873.  
  5874. </footer> 
  5875. <?php 
  5876.  
  5877. public function dashboard_widget_connect_to_wpcom() { 
  5878. if ( Jetpack::is_active() || Jetpack::is_development_mode() || ! current_user_can( 'jetpack_connect' ) ) { 
  5879. return; 
  5880. ?> 
  5881. <div class="wpcom-connect"> 
  5882. <div class="jp-emblem"> 
  5883. <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve"> 
  5884. <path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z"/> 
  5885. </svg> 
  5886. </div> 
  5887. <h3><?php esc_html_e( 'Please Connect Jetpack', 'jetpack' ); ?></h3> 
  5888. <p><?php echo wp_kses( __( 'Connecting Jetpack will show you <strong>stats</strong> about your traffic, <strong>protect</strong> you from brute force attacks, <strong>speed up</strong> your images and photos, and enable other <strong>traffic and security</strong> features.', 'jetpack' ), 'jetpack' ) ?></p> 
  5889.  
  5890. <div class="actions"> 
  5891. <a href="<?php echo $this->build_connect_url() ?>" class="button button-primary"> 
  5892. <?php esc_html_e( 'Connect Jetpack', 'jetpack' ); ?> 
  5893. </a> 
  5894. </div> 
  5895. </div> 
  5896. <?php 
  5897.  
  5898. /** 
  5899. * A graceful transition to using Core's site icon. 
  5900. * All of the hard work has already been done with the image 
  5901. * in all_done_page(). All that needs to be done now is update 
  5902. * the option and display proper messaging. 
  5903. * @todo remove when WP 4.3 is minimum 
  5904. * @since 3.6.1 
  5905. * @return bool false = Core's icon not available || true = Core's icon is available 
  5906. */ 
  5907. public static function jetpack_site_icon_available_in_core() { 
  5908. global $wp_version; 
  5909. $core_icon_available = function_exists( 'has_site_icon' ) && version_compare( $wp_version, '4.3-beta' ) >= 0; 
  5910.  
  5911. if ( ! $core_icon_available ) { 
  5912. return false; 
  5913.  
  5914. // No need for Jetpack's site icon anymore if core's is already set 
  5915. if ( has_site_icon() ) { 
  5916. if ( Jetpack::is_module_active( 'site-icon' ) ) { 
  5917. Jetpack::log( 'deactivate', 'site-icon' ); 
  5918. Jetpack::deactivate_module( 'site-icon' ); 
  5919. return true; 
  5920.  
  5921. // Transfer Jetpack's site icon to use core. 
  5922. $site_icon_id = Jetpack::get_option( 'site_icon_id' ); 
  5923. if ( $site_icon_id ) { 
  5924. // Update core's site icon 
  5925. update_option( 'site_icon', $site_icon_id ); 
  5926.  
  5927. // Delete Jetpack's icon option. We still want the blavatar and attached data though. 
  5928. delete_option( 'site_icon_id' ); 
  5929.  
  5930. // No need for Jetpack's site icon anymore 
  5931. if ( Jetpack::is_module_active( 'site-icon' ) ) { 
  5932. Jetpack::log( 'deactivate', 'site-icon' ); 
  5933. Jetpack::deactivate_module( 'site-icon' ); 
  5934.  
  5935. return true; 
  5936.