Jetpack

The WordPress Core Jetpack class.

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