PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 2ms

/class.jetpack.php

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

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