PageRenderTime 72ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/wordpress/wp-content/plugins/jetpack/class.jetpack.php

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

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