PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/class.jetpack.php

https://github.com/kwight/jetpack
PHP | 4426 lines | 3109 code | 626 blank | 691 comment | 571 complexity | 42ae87d59d543ef7935317c6f065841b MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. Options:
  4. jetpack_options (array)
  5. An array of options.
  6. @see Jetpack_Options::get_option_names()
  7. jetpack_register (string)
  8. Temporary verification secrets.
  9. jetpack_activated (int)
  10. 1: the plugin was activated normally
  11. 2: the plugin was activated on this site because of a network-wide activation
  12. 3: the plugin was auto-installed
  13. 4: the plugin was manually disconnected (but is still installed)
  14. jetpack_active_modules (array)
  15. Array of active module slugs.
  16. jetpack_do_activate (bool)
  17. Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
  18. */
  19. class Jetpack {
  20. var $xmlrpc_server = null;
  21. private $xmlrpc_verification = null;
  22. var $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA']
  23. var $plugins_to_deactivate = array(
  24. 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ),
  25. 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ),
  26. 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
  27. 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
  28. 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ),
  29. 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
  30. 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
  31. 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ),
  32. 'videopress' => array( 'video/video.php', 'VideoPress' ),
  33. 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),
  34. 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),
  35. 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),
  36. 'omnisearch' => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ),
  37. 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
  38. 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ),
  39. );
  40. var $capability_translations = array(
  41. 'administrator' => 'manage_options',
  42. 'editor' => 'edit_others_posts',
  43. 'author' => 'publish_posts',
  44. 'contributor' => 'edit_posts',
  45. 'subscriber' => 'read',
  46. );
  47. /**
  48. * Map of modules that have conflicts with plugins and should not be auto-activated
  49. * if the plugins are active. Used by filter_default_modules
  50. *
  51. * Plugin Authors: If you'd like to prevent a single module from auto-activating,
  52. * change `module-slug` and add this to your plugin:
  53. *
  54. * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' );
  55. * function my_jetpack_get_default_modules( $modules ) {
  56. * return array_diff( $modules, array( 'module-slug' ) );
  57. * }
  58. *
  59. * @var array
  60. */
  61. private $conflicting_plugins = array(
  62. 'comments' => array(
  63. 'Intense Debate' => 'intensedebate/intensedebate.php',
  64. 'Disqus' => 'disqus-comment-system/disqus.php',
  65. 'Livefyre' => 'livefyre-comments/livefyre.php',
  66. 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php',
  67. 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php',
  68. ),
  69. 'contact-form' => array(
  70. 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php',
  71. 'Gravity Forms' => 'gravityforms/gravityforms.php',
  72. 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php',
  73. 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php',
  74. 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php',
  75. ),
  76. 'gplus-authorship' => array(
  77. 'WP SEO by Yoast' => 'wordpress-seo/wp-seo.php',
  78. ),
  79. 'minileven' => array(
  80. 'WPtouch' => 'wptouch/wptouch.php',
  81. ),
  82. 'latex' => array(
  83. 'LaTeX for WordPress' => 'latex/latex.php',
  84. 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php',
  85. 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php',
  86. 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php',
  87. 'Enable Latex' => 'enable-latex/enable-latex.php',
  88. 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php',
  89. ),
  90. 'random-redirect' => array(
  91. 'Random Redirect 2' => 'random-redirect-2/random-redirect.php',
  92. ),
  93. 'related-posts' => array(
  94. 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php',
  95. 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php',
  96. 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php',
  97. 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php',
  98. 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php',
  99. 'outbrain' => 'outbrain/outbrain.php',
  100. 'Shareaholic' => 'shareaholic/shareaholic.php',
  101. 'Sexybookmarks' => 'sexybookmarks/shareaholic.php',
  102. ),
  103. 'sharedaddy' => array(
  104. 'AddThis' => 'addthis/addthis_social_widget.php',
  105. 'Add To Any' => 'add-to-any/add-to-any.php',
  106. 'ShareThis' => 'share-this/sharethis.php',
  107. 'Shareaholic' => 'shareaholic/shareaholic.php',
  108. ),
  109. 'verification-tools' => array(
  110. 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
  111. 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
  112. 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
  113. ),
  114. 'widget-visibility' => array(
  115. 'Widget Logic' => 'widget-logic/widget_logic.php',
  116. 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php',
  117. ),
  118. );
  119. /**
  120. * Plugins for which we turn off our Facebook OG Tags implementation.
  121. */
  122. private $open_graph_conflicting_plugins = array(
  123. 'facebook/facebook.php', // Official Facebook plugin
  124. 'wordpress-seo/wp-seo.php', // WordPress SEO by Yoast
  125. 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook
  126. 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one
  127. 'header-footer/plugin.php', // Header and Footer
  128. 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG
  129. 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments
  130. 'seo-ultimate/seo-ultimate.php', // SEO Ultimate
  131. 'sexybookmarks/sexy-bookmarks.php', // Shareaholic
  132. 'shareaholic/sexy-bookmarks.php', // Shareaholic
  133. 'social-discussions/social-discussions.php', // Social Discussions
  134. 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php',
  135. // NextScripts SNAP
  136. 'wordbooker/wordbooker.php', // Wordbooker
  137. 'socialize/socialize.php', // Socialize
  138. 'simple-facebook-connect/sfc.php', // Simple Facebook Connect
  139. 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit
  140. 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol
  141. 'opengraph/opengraph.php', // Open Graph
  142. 'sharepress/sharepress.php', // SharePress
  143. 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php',
  144. // WP Facebook Like Send & Open Graph Meta
  145. 'network-publisher/networkpub.php', // Network Publisher
  146. 'wp-ogp/wp-ogp.php', // WP-OGP
  147. 'open-graph-protocol-framework/open-graph-protocol-framework.php',
  148. // Open Graph Protocol Framework
  149. 'all-in-one-seo-pack/all_in_one_seo_pack.php', // All in One SEO Pack
  150. 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php',
  151. // Facebook Featured Image & OG Meta Tags
  152. 'add-meta-tags/add-meta-tags.php', // Add Meta Tags
  153. 'only-tweet-like-share-and-google-1/tweet-like-plusone.php',
  154. // Tweet, Like, Google +1 and Share
  155. 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail
  156. '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php',
  157. // 2 Click Social Media Buttons
  158. 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer
  159. 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin
  160. 'wp-caregiver/wp-caregiver.php', // WP Caregiver
  161. 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag
  162. 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php',
  163. // Fedmich's Facebook Open Graph Meta
  164. 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags
  165. 'wordpress-seo-premium/wp-seo-premium.php', // WordPress SEO Premium by Yoast
  166. );
  167. /**
  168. * Plugins for which we turn off our Twitter Cards Tags implementation.
  169. */
  170. private $twitter_cards_conflicting_plugins = array(
  171. 'twitter-cards/twitter-cards.php', // Twitter Cards
  172. 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta
  173. 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards
  174. 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards
  175. 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards
  176. 'eewee-twitter-card/index.php', // Eewee Twitter Card
  177. 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php',
  178. // Pure Web Brilliant's Social Graph Twitter Cards Extension
  179. );
  180. /**
  181. * Message to display in admin_notice
  182. * @var string
  183. */
  184. var $message = '';
  185. /**
  186. * Error to display in admin_notice
  187. * @var string
  188. */
  189. var $error = '';
  190. /**
  191. * Modules that need more privacy description.
  192. * @var string
  193. */
  194. var $privacy_checks = '';
  195. /**
  196. * Stats to record once the page loads
  197. *
  198. * @var array
  199. */
  200. var $stats = array();
  201. /**
  202. * Jetpack_Sync object
  203. */
  204. var $sync;
  205. /**
  206. * Verified data for JSON authorization request
  207. */
  208. var $json_api_authorization_request = array();
  209. /**
  210. * Holds the singleton instance of this class
  211. * @since 2.3.3
  212. * @var Jetpack
  213. */
  214. static $instance = false;
  215. /**
  216. * Singleton
  217. * @static
  218. */
  219. public static function init() {
  220. if ( ! self::$instance ) {
  221. if ( did_action( 'plugins_loaded' ) )
  222. self::plugin_textdomain();
  223. else
  224. add_action( 'plugins_loaded', array( __CLASS__, 'plugin_textdomain' ) );
  225. self::$instance = new Jetpack;
  226. self::$instance->plugin_upgrade();
  227. }
  228. return self::$instance;
  229. }
  230. /**
  231. * Must never be called statically
  232. */
  233. function plugin_upgrade() {
  234. // Upgrade: 1.1 -> 1.2
  235. if ( get_option( 'jetpack_id' ) ) {
  236. // Move individual jetpack options to single array of options
  237. $options = array();
  238. foreach ( Jetpack_Options::get_option_names() as $option ) {
  239. if ( false !== $value = get_option( "jetpack_$option" ) ) {
  240. $options[$option] = $value;
  241. }
  242. }
  243. if ( $options ) {
  244. Jetpack_Options::update_options( $options );
  245. foreach ( array_keys( $options ) as $option ) {
  246. delete_option( "jetpack_$option" );
  247. }
  248. }
  249. // Add missing version and old_version options
  250. if ( ! $version = Jetpack_Options::get_option( 'version' ) ) {
  251. $version = $old_version = '1.1:' . time();
  252. Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
  253. }
  254. }
  255. // Upgrade from a single user token to a user_id-indexed array and a master_user ID
  256. if ( ! Jetpack_Options::get_option( 'user_tokens' ) ) {
  257. if ( $user_token = Jetpack_Options::get_option( 'user_token' ) ) {
  258. $token_parts = explode( '.', $user_token );
  259. if ( isset( $token_parts[2] ) ) {
  260. $master_user = $token_parts[2];
  261. $user_tokens = array( $master_user => $user_token );
  262. Jetpack_Options::update_options( compact( 'master_user', 'user_tokens' ) );
  263. Jetpack_Options::delete_option( 'user_token' );
  264. } else {
  265. // @todo: is this even possible?
  266. trigger_error( sprintf( 'Jetpack::plugin_upgrade found no user_id in user_token "%s"', $user_token ), E_USER_WARNING );
  267. }
  268. }
  269. }
  270. }
  271. /**
  272. * Constructor. Initializes WordPress hooks
  273. */
  274. private function Jetpack() {
  275. /**
  276. * Do things that should run even in the network admin
  277. * here, before we potentially fail out.
  278. */
  279. add_filter( 'jetpack_require_lib_dir', array( $this, 'require_lib_dir' ) );
  280. /*
  281. * Load things that should only be in Network Admin.
  282. *
  283. * For now blow away everything else until a more full
  284. * understanding of what is needed at the network level is
  285. * available
  286. */
  287. if( is_multisite() ) {
  288. Jetpack_Network::init();
  289. if( is_network_admin() )
  290. return; // End here to prevent single site actions from firing
  291. }
  292. $this->sync = new Jetpack_Sync;
  293. // Modules should do Jetpack_Sync::sync_options( __FILE__, $option, ... ); instead
  294. // We access the "internal" method here only because the Jetpack object isn't instantiated yet
  295. $this->sync->options(
  296. JETPACK__PLUGIN_DIR . 'jetpack.php',
  297. 'home',
  298. 'siteurl',
  299. 'blogname',
  300. 'gmt_offset',
  301. 'timezone_string'
  302. );
  303. if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
  304. @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed.
  305. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
  306. $this->xmlrpc_server = new Jetpack_XMLRPC_Server();
  307. $this->require_jetpack_authentication();
  308. if ( Jetpack::is_active() ) {
  309. // Hack to preserve $HTTP_RAW_POST_DATA
  310. add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
  311. $signed = $this->verify_xml_rpc_signature();
  312. if ( $signed && ! is_wp_error( $signed ) ) {
  313. // The actual API methods.
  314. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) );
  315. } else {
  316. add_filter( 'xmlrpc_methods', '__return_empty_array' );
  317. }
  318. } else {
  319. // The bootstrap API methods.
  320. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) );
  321. }
  322. // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on.
  323. add_filter( 'pre_option_enable_xmlrpc', '__return_true' );
  324. } elseif ( is_admin() && isset( $_POST['action'] ) && 'jetpack_upload_file' == $_POST['action'] ) {
  325. $this->require_jetpack_authentication();
  326. $this->add_remote_request_handlers();
  327. } else {
  328. if ( Jetpack::is_active() ) {
  329. add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) );
  330. }
  331. }
  332. if ( Jetpack::is_active() ) {
  333. Jetpack_Heartbeat::init();
  334. }
  335. add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) );
  336. if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) {
  337. wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
  338. }
  339. add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) );
  340. add_action( 'admin_menu', array( $this, 'admin_menu' ), 999 ); // run late so that other plugins hooking into this menu don't get left out
  341. add_action( 'admin_init', array( $this, 'admin_init' ) );
  342. add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
  343. add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
  344. add_action( 'wp_ajax_jetpack-check-news-subscription', array( $this, 'check_news_subscription' ) );
  345. add_action( 'wp_ajax_jetpack-subscribe-to-news', array( $this, 'subscribe_to_news' ) );
  346. add_action( 'wp_ajax_jetpack-sync-reindex-trigger', array( $this, 'sync_reindex_trigger' ) );
  347. add_action( 'wp_ajax_jetpack-sync-reindex-status', array( $this, 'sync_reindex_status' ) );
  348. add_action( 'wp_loaded', array( $this, 'register_assets' ) );
  349. add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
  350. add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
  351. add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
  352. // add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_modules' ) );
  353. add_action( 'jetpack_activate_module', array( $this, 'activate_module_actions' ) );
  354. add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ) );
  355. /**
  356. * These actions run checks to load additional files.
  357. * They check for external files or plugins, so they need to run as late as possible.
  358. */
  359. add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 );
  360. add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 );
  361. add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
  362. add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 );
  363. add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
  364. add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
  365. }
  366. /**
  367. * If there are any stats that need to be pushed, but haven't been, push them now.
  368. */
  369. function __destruct() {
  370. if ( ! empty( $this->stats ) ) {
  371. $this->do_stats( 'server_side' );
  372. }
  373. }
  374. function jetpack_custom_caps( $caps, $cap, $user_id, $args ) {
  375. switch( $cap ) {
  376. case 'jetpack_connect' :
  377. case 'jetpack_reconnect' :
  378. if ( Jetpack::is_development_mode() ) {
  379. $caps = array( 'do_not_allow' );
  380. break;
  381. }
  382. /**
  383. * Pass through. If it's not development mode, these should match disconnect.
  384. * Let users disconnect if it's development mode, just in case things glitch.
  385. */
  386. case 'jetpack_disconnect' :
  387. /**
  388. * In multisite, can individual site admins manage their own connection?
  389. *
  390. * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class.
  391. */
  392. if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
  393. if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) {
  394. /**
  395. * We need to update the option name -- it's terribly unclear which
  396. * direction the override goes.
  397. *
  398. * @todo: Update the option name to `sub-sites-can-manage-own-connections`
  399. */
  400. $caps = array( 'do_not_allow' );
  401. break;
  402. }
  403. }
  404. $caps = array( 'manage_options' );
  405. break;
  406. case 'jetpack_manage_modules' :
  407. case 'jetpack_activate_modules' :
  408. case 'jetpack_deactivate_modules' :
  409. $caps = array( 'manage_options' );
  410. break;
  411. case 'jetpack_configure_modules' :
  412. $caps = array( 'manage_options' );
  413. break;
  414. case 'jetpack_admin_page' :
  415. if ( Jetpack::is_development_mode() ) {
  416. $caps = array( 'manage_options' );
  417. break;
  418. }
  419. /**
  420. * Pass through. If it's not development mode, these should match the admin page.
  421. * Let users disconnect if it's development mode, just in case things glitch.
  422. */
  423. case 'jetpack_connect_user' :
  424. if ( Jetpack::is_development_mode() ) {
  425. $caps = array( 'do_not_allow' );
  426. break;
  427. }
  428. $caps = array( 'read' );
  429. break;
  430. }
  431. return $caps;
  432. }
  433. function require_jetpack_authentication() {
  434. // Don't let anyone authenticate
  435. $_COOKIE = array();
  436. remove_all_filters( 'authenticate' );
  437. /**
  438. * For the moment, remove Limit Login Attempts if its xmlrpc for Jetpack.
  439. * If Limit Login Attempts is installed as a mu-plugin, it can occasionally
  440. * generate false-positives.
  441. */
  442. remove_filter( 'wp_login_failed', 'limit_login_failed' );
  443. if ( Jetpack::is_active() ) {
  444. // Allow Jetpack authentication
  445. add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 );
  446. }
  447. }
  448. /**
  449. * Load language files
  450. */
  451. public static function plugin_textdomain() {
  452. load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  453. }
  454. /**
  455. * Register assets for use in various modules and the Jetpack admin page.
  456. *
  457. * @uses wp_script_is, wp_register_script, plugins_url
  458. * @action wp_loaded
  459. * @return null
  460. */
  461. public function register_assets() {
  462. if ( ! wp_script_is( 'spin', 'registered' ) )
  463. wp_register_script( 'spin', plugins_url( '_inc/spin.js', __FILE__ ), false, '1.3' );
  464. if ( ! wp_script_is( 'jquery.spin', 'registered' ) )
  465. wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', __FILE__ ) , array( 'jquery', 'spin' ), '1.3' );
  466. if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) )
  467. wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', __FILE__ ), array( 'media-views' ), '20121225' );
  468. /**
  469. * As jetpack_register_genericons is by default fired off a hook,
  470. * the hook may have already fired by this point.
  471. * So, let's just trigger it manually.
  472. */
  473. require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' );
  474. jetpack_register_genericons();
  475. if ( ! wp_style_is( 'jetpack-icons', 'registered' ) )
  476. wp_register_style( 'jetpack-icons', plugins_url( '_inc/jetpack-icons/jetpack-icons.css', __FILE__ ), false, JETPACK__VERSION );
  477. }
  478. /**
  479. * Device Pixels support
  480. * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers.
  481. */
  482. function devicepx() {
  483. if ( Jetpack::is_active() ) {
  484. wp_enqueue_script( 'devicepx', set_url_scheme( 'http://s0.wp.com/wp-content/js/devicepx-jetpack.js' ), array(), gmdate( 'oW' ), true );
  485. }
  486. }
  487. /*
  488. * Returns the location of Jetpack's lib directory. This filter is applied
  489. * in require_lib().
  490. *
  491. * @filter require_lib_dir
  492. */
  493. function require_lib_dir( $lib_dir ) {
  494. return JETPACK__PLUGIN_DIR . '_inc/lib';
  495. }
  496. /**
  497. * Is Jetpack active?
  498. */
  499. public static function is_active() {
  500. return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  501. }
  502. /**
  503. * Is Jetpack in development (offline) mode?
  504. */
  505. public static function is_development_mode() {
  506. $development_mode = false;
  507. if ( defined( 'JETPACK_DEV_DEBUG' ) ) {
  508. $development_mode = JETPACK_DEV_DEBUG;
  509. }
  510. elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
  511. $development_mode = true;
  512. }
  513. return apply_filters( 'jetpack_development_mode', $development_mode );
  514. }
  515. /**
  516. * Is a given user (or the current user if none is specified) linked to a WordPress.com user?
  517. */
  518. public static function is_user_connected( $user_id = false ) {
  519. $user_id = false === $user_id ? get_current_user_id() : absint( $user_id );
  520. if ( ! $user_id ) {
  521. return false;
  522. }
  523. return (bool) Jetpack_Data::get_access_token( $user_id );
  524. }
  525. /**
  526. * Get the wpcom email of the current connected user.
  527. */
  528. public static function get_connected_user_email() {
  529. Jetpack::load_xml_rpc_client();
  530. $xml = new Jetpack_IXR_Client( array(
  531. 'user_id' => get_current_user_id()
  532. ) );
  533. $xml->query( 'wpcom.getUserEmail' );
  534. if ( ! $xml->isError() ) {
  535. return $xml->getResponse();
  536. }
  537. return false;
  538. }
  539. function current_user_is_connection_owner() {
  540. $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  541. return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id;
  542. }
  543. /**
  544. * Add any extra oEmbed providers that we know about and use on wpcom for feature parity.
  545. */
  546. function extra_oembed_providers() {
  547. // Cloudup: https://dev.cloudup.com/#oembed
  548. wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' );
  549. }
  550. /**
  551. * Synchronize connected user role changes
  552. */
  553. function user_role_change( $user_id ) {
  554. if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) {
  555. $current_user_id = get_current_user_id();
  556. wp_set_current_user( $user_id );
  557. $role = $this->translate_current_user_to_role();
  558. $signed_role = $this->sign_role( $role );
  559. wp_set_current_user( $current_user_id );
  560. $master_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  561. $master_user_id = absint( $master_token->external_user_id );
  562. if ( ! $master_user_id )
  563. return; // this shouldn't happen
  564. Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role );
  565. //@todo retry on failure
  566. //try to choose a new master if we're demoting the current one
  567. if ( $user_id == $master_user_id && 'administrator' != $role ) {
  568. $query = new WP_User_Query(
  569. array(
  570. 'fields' => array( 'id' ),
  571. 'role' => 'administrator',
  572. 'orderby' => 'id',
  573. 'exclude' => array( $master_user_id ),
  574. )
  575. );
  576. $new_master = false;
  577. foreach ( $query->results as $result ) {
  578. $uid = absint( $result->id );
  579. if ( $uid && Jetpack::is_user_connected( $uid ) ) {
  580. $new_master = $uid;
  581. break;
  582. }
  583. }
  584. if ( $new_master ) {
  585. Jetpack_Options::update_option( 'master_user', $new_master );
  586. }
  587. // else disconnect..?
  588. }
  589. }
  590. }
  591. /**
  592. * Loads the currently active modules.
  593. */
  594. public static function load_modules() {
  595. /*
  596. if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
  597. return;
  598. }
  599. */
  600. $version = Jetpack_Options::get_option( 'version' );
  601. if ( ! $version ) {
  602. $version = $old_version = JETPACK__VERSION . ':' . time();
  603. Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
  604. }
  605. list( $version ) = explode( ':', $version );
  606. $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) );
  607. $modules_data = array();
  608. // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check.
  609. if ( version_compare( $version, JETPACK__VERSION, '<' ) ) {
  610. $updated_modules = array();
  611. foreach ( $modules as $module ) {
  612. $modules_data[ $module ] = Jetpack::get_module( $module );
  613. if ( ! isset( $modules_data[ $module ]['changed'] ) ) {
  614. continue;
  615. }
  616. if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) {
  617. continue;
  618. }
  619. $updated_modules[] = $module;
  620. }
  621. $modules = array_diff( $modules, $updated_modules );
  622. }
  623. foreach ( $modules as $module ) {
  624. // If not connected and we're in dev mode, disable modules requiring a connection
  625. if ( ! Jetpack::is_active() ) {
  626. if ( empty( $modules_data[ $module ] ) ) {
  627. $modules_data[ $module ] = Jetpack::get_module( $module );
  628. }
  629. if ( $modules_data[ $module ]['requires_connection'] || ! Jetpack::is_development_mode() ) {
  630. continue;
  631. }
  632. }
  633. if ( did_action( 'jetpack_module_loaded_' . $module ) ) {
  634. continue;
  635. }
  636. require Jetpack::get_module_path( $module );
  637. do_action( 'jetpack_module_loaded_' . $module );
  638. }
  639. do_action( 'jetpack_modules_loaded' );
  640. // 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.
  641. if ( Jetpack::is_active() || Jetpack::is_development_mode() )
  642. require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' );
  643. }
  644. /**
  645. * Check if Jetpack's REST API compat file should be included
  646. * @action plugins_loaded
  647. * @return null
  648. */
  649. public function check_rest_api_compat() {
  650. $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() );
  651. if ( function_exists( 'bbpress' ) )
  652. $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php';
  653. foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include )
  654. require_once $_jetpack_rest_api_compat_include;
  655. }
  656. /**
  657. * Gets all plugins currently active in values, regardless of whether they're
  658. * traditionally activated or network activated.
  659. *
  660. * @todo Store the result in core's object cache maybe?
  661. */
  662. public static function get_active_plugins() {
  663. $active_plugins = (array) get_option( 'active_plugins', array() );
  664. if ( is_multisite() ) {
  665. // Due to legacy code, active_sitewide_plugins stores them in the keys,
  666. // whereas active_plugins stores them in the values.
  667. $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
  668. if ( $network_plugins ) {
  669. $active_plugins = array_merge( $active_plugins, $network_plugins );
  670. }
  671. }
  672. sort( $active_plugins );
  673. return $active_plugins;
  674. }
  675. /**
  676. * Checks whether a specific plugin is active.
  677. *
  678. * We don't want to store these in a static variable, in case
  679. * there are switch_to_blog() calls involved.
  680. */
  681. public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) {
  682. return in_array( $plugin, self::get_active_plugins() );
  683. }
  684. /**
  685. * Check if Jetpack's Open Graph tags should be used.
  686. * If certain plugins are active, Jetpack's og tags are suppressed.
  687. *
  688. * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
  689. * @action plugins_loaded
  690. * @return null
  691. */
  692. public function check_open_graph() {
  693. if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) {
  694. add_filter( 'jetpack_enable_open_graph', '__return_true', 0 );
  695. }
  696. $active_plugins = self::get_active_plugins();
  697. if ( ! empty( $active_plugins ) ) {
  698. foreach ( $this->open_graph_conflicting_plugins as $plugin ) {
  699. if ( in_array( $plugin, $active_plugins ) ) {
  700. add_filter( 'jetpack_enable_open_graph', '__return_false', 99 );
  701. break;
  702. }
  703. }
  704. }
  705. if ( apply_filters( 'jetpack_enable_open_graph', false ) ) {
  706. require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php';
  707. }
  708. }
  709. /**
  710. * Check if Jetpack's Twitter tags should be used.
  711. * If certain plugins are active, Jetpack's twitter tags are suppressed.
  712. *
  713. * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
  714. * @action plugins_loaded
  715. * @return null
  716. */
  717. public function check_twitter_tags() {
  718. $active_plugins = self::get_active_plugins();
  719. if ( ! empty( $active_plugins ) ) {
  720. foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) {
  721. if ( in_array( $plugin, $active_plugins ) ) {
  722. add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 );
  723. break;
  724. }
  725. }
  726. }
  727. if ( apply_filters( 'jetpack_disable_twitter_cards', true ) ) {
  728. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
  729. }
  730. }
  731. /* Jetpack Options API */
  732. public static function get_option_names( $type = 'compact' ) {
  733. return Jetpack_Options::get_option_names( $type );
  734. }
  735. /**
  736. * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate.
  737. *
  738. * @param string $name Option name
  739. * @param mixed $default (optional)
  740. */
  741. public static function get_option( $name, $default = false ) {
  742. return Jetpack_Options::get_option( $name, $default );
  743. }
  744. /**
  745. * Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action
  746. * Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted
  747. * $name must be a registered option name.
  748. */
  749. public static function create_nonce( $name ) {
  750. $secret = wp_generate_password( 32, false ) . ':' . wp_generate_password( 32, false ) . ':' . ( time() + 600 );
  751. Jetpack_Options::update_option( $name, $secret );
  752. @list( $secret_1, $secret_2, $eol ) = explode( ':', Jetpack_Options::get_option( $name ) );
  753. if ( empty( $secret_1 ) || empty( $secret_2 ) || $eol < time() )
  754. return new Jetpack_Error( 'missing_secrets' );
  755. return array(
  756. 'secret_1' => $secret_1,
  757. 'secret_2' => $secret_2,
  758. 'eol' => $eol,
  759. );
  760. }
  761. /**
  762. * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate.
  763. *
  764. * @param string $name Option name
  765. * @param mixed $value Option value
  766. */
  767. public static function update_option( $name, $value ) {
  768. return Jetpack_Options::update_option( $name, $value );
  769. }
  770. /**
  771. * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate.
  772. *
  773. * @param array $array array( option name => option value, ... )
  774. */
  775. public static function update_options( $array ) {
  776. return Jetpack_Options::update_options( $array );
  777. }
  778. /**
  779. * Deletes the given option. May be passed multiple option names as an array.
  780. * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
  781. *
  782. * @param string|array $names
  783. */
  784. public static function delete_option( $names ) {
  785. return Jetpack_Options::delete_option( $names );
  786. }
  787. /**
  788. * Enters a user token into the user_tokens option
  789. *
  790. * @param int $user_id
  791. * @param string $token
  792. * return bool
  793. */
  794. public static function update_user_token( $user_id, $token, $is_master_user ) {
  795. // not designed for concurrent updates
  796. $user_tokens = Jetpack_Options::get_option( 'user_tokens' );
  797. if ( ! is_array( $user_tokens ) )
  798. $user_tokens = array();
  799. $user_tokens[$user_id] = $token;
  800. if ( $is_master_user ) {
  801. $master_user = $user_id;
  802. $options = compact( 'user_tokens', 'master_user' );
  803. } else {
  804. $options = compact( 'user_tokens' );
  805. }
  806. return Jetpack_Options::update_options( $options );
  807. }
  808. /**
  809. * Returns an array of all PHP files in the specified absolute path.
  810. * Equivalent to glob( "$absolute_path/*.php" ).
  811. *
  812. * @param string $absolute_path The absolute path of the directory to search.
  813. * @return array Array of absolute paths to the PHP files.
  814. */
  815. public static function glob_php( $absolute_path ) {
  816. $absolute_path = untrailingslashit( $absolute_path );
  817. $files = array();
  818. if ( ! $dir = @opendir( $absolute_path ) ) {
  819. return $files;
  820. }
  821. while ( false !== $file = readdir( $dir ) ) {
  822. if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) {
  823. continue;
  824. }
  825. $file = "$absolute_path/$file";
  826. if ( ! is_file( $file ) ) {
  827. continue;
  828. }
  829. $files[] = $file;
  830. }
  831. closedir( $dir );
  832. return $files;
  833. }
  834. public function activate_new_modules() {
  835. if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
  836. return;
  837. }
  838. $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic]
  839. if ( ! $jetpack_old_version ) {
  840. $jetpack_old_version = $version = $old_version = '1.1:' . time();
  841. Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
  842. }
  843. list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic]
  844. if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) {
  845. return;
  846. }
  847. $active_modules = Jetpack::get_active_modules();
  848. $reactivate_modules = array();
  849. foreach ( $active_modules as $active_module ) {
  850. $module = Jetpack::get_module( $active_module );
  851. if ( ! isset( $module['changed'] ) ) {
  852. continue;
  853. }
  854. if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) {
  855. continue;
  856. }
  857. $reactivate_modules[] = $active_module;
  858. Jetpack::deactivate_module( $active_module );
  859. }
  860. if ( version_compare( $jetpack_version, '1.9.2', '<' ) && version_compare( '1.9-something', JETPACK__VERSION, '<' ) ) {
  861. add_action( 'jetpack_activate_default_modules', array( $this->sync, 'sync_all_registered_options' ), 1000 );
  862. }
  863. Jetpack_Options::update_options(
  864. array(
  865. 'version' => JETPACK__VERSION . ':' . time(),
  866. 'old_version' => $jetpack_old_version,
  867. )
  868. );
  869. Jetpack::state( 'message', 'modules_activated' );
  870. Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules );
  871. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  872. exit;
  873. }
  874. /**
  875. * List available Jetpack modules. Simply lists .php files in /modules/.
  876. * Make sure to tuck away module "library" files in a sub-directory.
  877. */
  878. public static function get_available_modules( $min_version = false, $max_version = false ) {
  879. static $modules = null;
  880. if ( ! isset( $modules ) ) {
  881. $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' );
  882. $modules = array();
  883. foreach ( $files as $file ) {
  884. if ( ! $headers = Jetpack::get_module( $file ) ) {
  885. continue;
  886. }
  887. $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced'];
  888. }
  889. }
  890. $modules = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version );
  891. if ( ! $min_version && ! $max_version ) {
  892. return array_keys( $modules );
  893. }
  894. $r = array();
  895. foreach ( $modules as $slug => $introduced ) {
  896. if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) {
  897. continue;
  898. }
  899. if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) {
  900. continue;
  901. }
  902. $r[] = $slug;
  903. }
  904. return $r;
  905. }
  906. /**
  907. * Default modules loaded on activation.
  908. */
  909. public static function get_default_modules( $min_version = false, $max_version = false ) {
  910. $return = array();
  911. foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) {
  912. $module_data = Jetpack::get_module( $module );
  913. switch ( strtolower( $module_data['auto_activate'] ) ) {
  914. case 'yes' :
  915. $return[] = $module;
  916. break;
  917. case 'public' :
  918. if ( Jetpack_Options::get_option( 'public' ) ) {
  919. $return[] = $module;
  920. }
  921. break;
  922. case 'no' :
  923. default :
  924. break;
  925. }
  926. }
  927. return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version );
  928. }
  929. /**
  930. * Checks activated modules during auto-activation to determine
  931. * if any of those modules are being deprecated. If so, close
  932. * them out, and add any replacement modules.
  933. *
  934. * Runs at priority 99 by default.
  935. *
  936. * This is run late, so that it can still activate a module if
  937. * the new module is a replacement for another that the user
  938. * currently has active, even if something at the normal priority
  939. * would kibosh everything.
  940. *
  941. * @since 2.6
  942. * @uses jetpack_get_default_modules filter
  943. * @param array $modules
  944. * @return array
  945. */
  946. function handle_deprecated_modules( $modules ) {
  947. $deprecated_modules = array(
  948. 'debug' => null, // Closed out and moved to ./class.jetpack-debugger.php
  949. 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
  950. );
  951. // Don't activate SSO if they never completed activating WPCC.
  952. if ( Jetpack::is_module_active( 'wpcc' ) ) {
  953. $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' );
  954. if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) {
  955. $deprecated_modules['wpcc'] = null;
  956. }
  957. }
  958. foreach ( $deprecated_modules as $module => $replacement ) {
  959. if ( Jetpack::is_module_active( $module ) ) {
  960. self::deactivate_module( $module );
  961. if ( $replacement ) {
  962. $modules[] = $replacement;
  963. }
  964. }
  965. }
  966. return array_unique( $modules );
  967. }
  968. /**
  969. * Checks activated plugins during auto-activation to determine
  970. * if any of those plugins are in the list with a corresponding module
  971. * that is not compatible with the plugin. The module will not be allowed
  972. * to auto-activate.
  973. *
  974. * @since 2.6
  975. * @uses jetpack_get_default_modules filter
  976. * @param array $modules
  977. * @return array
  978. */
  979. function filter_default_modules( $modules ) {
  980. $active_plugins = self::get_active_plugins();
  981. if ( ! empty( $active_plugins ) ) {
  982. // For each module we'd like to auto-activate...
  983. foreach ( $modules as $key => $module ) {
  984. // If there are potential conflicts for it...
  985. if ( ! empty( $this->conflicting_plugins[ $module ] ) ) {
  986. // For each potential conflict...
  987. foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) {
  988. // If that conflicting plugin is active...
  989. if ( in_array( $plugin, $active_plugins ) ) {
  990. // Remove that item from being auto-activated.
  991. unset( $modules[ $key ] );
  992. }
  993. }
  994. }
  995. }
  996. }
  997. return $modules;
  998. }
  999. /**
  1000. * Extract a module's slug from its full path.
  1001. */
  1002. public static function get_module_slug( $file ) {
  1003. return str_replace( '.php', '', basename( $file ) );
  1004. }
  1005. /**
  1006. * Generate a module's path from its slug.
  1007. */
  1008. public static function get_module_path( $slug ) {
  1009. return JETPACK__PLUGIN_DIR . "modules/$slug.php";
  1010. }
  1011. /**
  1012. * Load module data from module file. Headers differ from WordPress
  1013. * plugin headers to avoid them being identified as standalone
  1014. * plugins on the WordPress plugins page.
  1015. */
  1016. public static function get_module( $module ) {
  1017. $headers = array(
  1018. 'name' => 'Module Name',
  1019. 'description' => 'Module Description',
  1020. 'sort' => 'Sort Order',
  1021. 'introduced' => 'First Introduced',
  1022. 'changed' => 'Major Changes In',
  1023. 'deactivate' => 'Deactivate',
  1024. 'free' => 'Free',
  1025. 'requires_connection' => 'Requires Connection',
  1026. 'auto_activate' => 'Auto Activate',
  1027. 'module_tags' => 'Module Tags',
  1028. );
  1029. $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) );
  1030. if ( ! file_exists( $file ) )
  1031. return false;
  1032. $mod = get_file_data( $file, $headers );
  1033. if ( empty( $mod['name'] ) )
  1034. return false;
  1035. $mod['name'] = translate( $mod['name'], 'jetpack' );
  1036. $mod['description'] = translate( $mod['description'], 'jetpack' );
  1037. $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
  1038. $mod['deactivate'] = empty( $mod['deactivate'] );
  1039. $mod['free'] = empty( $mod['free'] );
  1040. $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true;
  1041. if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) {
  1042. $mod['auto_activate'] = 'No';
  1043. } else {
  1044. $mod['auto_activate'] = (string) $mod['auto_activate'];
  1045. }
  1046. if ( $mod['module_tags'] ) {
  1047. $mod['module_tags'] = explode( ',', $mod['module_tags'] );
  1048. $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] );
  1049. $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] );
  1050. } else {
  1051. $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) );
  1052. }
  1053. return $mod;
  1054. }
  1055. public static function translate_module_tag( $untranslated_tag ) {
  1056. return _x( $untranslated_tag, 'Module Tag', 'jetpack' );
  1057. // Calls here are to populate translation files.
  1058. _x( 'Photos and Videos', 'Module Tag', 'jetpack' );
  1059. _x( 'Social', 'Module Tag', 'jetpack' );
  1060. _x( 'WordPress.com Stats', 'Module Tag', 'jetpack' );
  1061. _x( 'Writing', 'Module Tag', 'jetpack' );
  1062. _x( 'Appearance', 'Module Tag', 'jetpack' );
  1063. _x( 'Developers', 'Module Tag', 'jetpack' );
  1064. _x( 'Mobile', 'Module Tag', 'jetpack' );
  1065. _x( 'Other', 'Module Tag', 'jetpack' );
  1066. }
  1067. /**
  1068. * Get a list of activated modules as an array of module slugs.
  1069. */
  1070. public static function get_active_modules() {
  1071. $active = Jetpack_Options::get_option( 'active_modules' );
  1072. if ( ! is_array( $active ) )
  1073. $active = array();
  1074. if ( is_admin() ) {
  1075. $active[] = 'vaultpress';
  1076. } else {
  1077. $active = array_diff( $active, array( 'vaultpress' ) );
  1078. }
  1079. return array_unique( $active );
  1080. }
  1081. /**
  1082. * Check whether or not a Jetpack module is active.
  1083. *
  1084. * @param string $module The slug of a Jetpack module.
  1085. * @return bool
  1086. *
  1087. * @static
  1088. */
  1089. public static function is_module_active( $module ) {
  1090. return in_array( $module, self::get_active_modules() );
  1091. }
  1092. public static function is_module( $module ) {
  1093. return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() );
  1094. }
  1095. /**
  1096. * Catches PHP errors. Must be used in conjunction with output buffering.
  1097. *
  1098. * @param bool $catch True to start catching, False to stop.
  1099. *
  1100. * @static
  1101. */
  1102. public static function catch_errors( $catch ) {
  1103. static $display_errors, $error_reporting;
  1104. if ( $catch ) {
  1105. $display_errors = @ini_set( 'display_errors', 1 );
  1106. $error_reporting = @error_reporting( E_ALL );
  1107. add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
  1108. } else {
  1109. @ini_set( 'display_errors', $display_errors );
  1110. @error_reporting( $error_reporting );
  1111. remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 1 );
  1112. }
  1113. }
  1114. /**
  1115. * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
  1116. */
  1117. public static function catch_errors_on_shutdown() {
  1118. Jetpack::state( 'php_errors', ob_get_clean() );
  1119. }
  1120. public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array() ) {
  1121. $jetpack = Jetpack::init();
  1122. $modules = Jetpack::get_default_modules( $min_version, $max_version );
  1123. $modules = array_merge( $other_modules, $modules );
  1124. // Look for standalone plugins and disable if active.
  1125. $to_deactivate = array();
  1126. foreach ( $modules as $module ) {
  1127. if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) {
  1128. $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module];
  1129. }
  1130. }
  1131. $deactivated = array();
  1132. foreach ( $to_deactivate as $module => $deactivate_me ) {
  1133. list( $probable_file, $probable_title ) = $deactivate_me;
  1134. if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) {
  1135. $deactivated[] = $module;
  1136. }
  1137. }
  1138. if ( $deactivated ) {
  1139. Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) );
  1140. $url = add_query_arg(
  1141. array(
  1142. 'action' => 'activate_default_modules',
  1143. '_wpnonce' => wp_create_nonce( 'activate_default_modules' ),
  1144. ),
  1145. add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) )
  1146. );
  1147. wp_safe_redirect( $url );
  1148. exit;
  1149. }
  1150. do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules );
  1151. // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating
  1152. Jetpack::restate();
  1153. Jetpack::catch_errors( true );
  1154. foreach ( $modules as $module ) {
  1155. if ( did_action( "jetpack_module_loaded_$module" ) ) {
  1156. $active[] = $module;
  1157. Jetpack_Options::update_option( 'active_modules', array_unique( $active ) );
  1158. continue;
  1159. }
  1160. $active = Jetpack::get_active_modules();
  1161. if ( in_array( $module, $active ) ) {
  1162. $module_info = Jetpack::get_module( $module );
  1163. if ( ! $module_info['deactivate'] ) {
  1164. $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
  1165. if ( $active_state = Jetpack::state( $state ) ) {
  1166. $active_state = explode( ',', $active_state );
  1167. } else {
  1168. $active_state = array();
  1169. }
  1170. $active_state[] = $module;
  1171. Jetpack::state( $state, implode( ',', $active_state ) );
  1172. }
  1173. continue;
  1174. }
  1175. $file = Jetpack::get_module_path( $module );
  1176. if ( ! file_exists( $file ) ) {
  1177. continue;
  1178. }
  1179. // we'll override this later if the plugin can be included without fatal error
  1180. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  1181. Jetpack::state( 'error', 'module_activation_failed' );
  1182. Jetpack::state( 'module', $module );
  1183. ob_start();
  1184. require $file;
  1185. do_action( 'jetpack_activate_module', $module );
  1186. $active[] = $module;
  1187. $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
  1188. if ( $active_state = Jetpack::state( $state ) ) {
  1189. $active_state = explode( ',', $active_state );
  1190. } else {
  1191. $active_state = array();
  1192. }
  1193. $active_state[] = $module;
  1194. Jetpack::state( $state, implode( ',', $active_state ) );
  1195. Jetpack_Options::update_option( 'active_modules', array_unique( $active ) );
  1196. ob_end_clean();
  1197. }
  1198. Jetpack::state( 'error', false );
  1199. Jetpack::state( 'module', false );
  1200. Jetpack::catch_errors( false );
  1201. do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules );
  1202. }
  1203. public static function activate_module( $module, $exit = true ) {
  1204. do_action( 'jetpack_pre_activate_module', $module, $exit );
  1205. $jetpack = Jetpack::init();
  1206. if ( ! strlen( $module ) )
  1207. return false;
  1208. if ( ! Jetpack::is_module( $module ) )
  1209. return false;
  1210. // If it's already active, then don't do it again
  1211. $active = Jetpack::get_active_modules();
  1212. foreach ( $active as $act ) {
  1213. if ( $act == $module )
  1214. return true;
  1215. }
  1216. $module_data = Jetpack::get_module( $module );
  1217. if ( ! Jetpack::is_active() ) {
  1218. if ( ! Jetpack::is_development_mode() )
  1219. return false;
  1220. // If we're not connected but in development mode, make sure the module doesn't require a connection
  1221. if ( Jetpack::is_development_mode() && $module_data['requires_connection'] )
  1222. return false;
  1223. }
  1224. // Check and see if the old plugin is active
  1225. if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) {
  1226. // Deactivate the old plugin
  1227. if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[$module][0], $jetpack->plugins_to_deactivate[$module][1] ) ) {
  1228. // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module
  1229. // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load.
  1230. Jetpack::state( 'deactivated_plugins',

Large files files are truncated, but you can click here to view the full file