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