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