PageRenderTime 152ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 2ms

/class.jetpack.php

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

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