array( 'stats/stats.php', 'WordPress.com Stats' ),
'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ),
'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ),
'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
);
var $capability_translations = array(
'administrator' => 'manage_options',
'editor' => 'edit_others_posts',
'author' => 'publish_posts',
'contributor' => 'edit_posts',
'subscriber' => 'read',
);
/**
* Message to display in admin_notice
* @var string
*/
var $message = '';
/**
* Error to display in admin_notice
* @var string
*/
var $error = '';
/**
* Modules that need more privacy description.
* @var string
*/
var $privacy_checks = '';
/**
* Stats to record once the page loads
*
* @var array
*/
var $stats = array();
/**
* Jetpack_Sync object
*/
var $sync;
/**
* Verified data for JSON authorization request
*/
var $json_api_authorization_request = array();
/**
* Singleton
* @static
*/
public static function init() {
static $instance = false;
if ( !$instance ) {
load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
$instance = new Jetpack;
$instance->plugin_upgrade();
}
return $instance;
}
/**
* Must never be called statically
*/
function plugin_upgrade() {
// Upgrade: 1.1 -> 1.2
if ( get_option( 'jetpack_id' ) ) {
// Move individual jetpack options to single array of options
$options = array();
foreach ( Jetpack::get_option_names() as $option ) {
if ( false !== $value = get_option( "jetpack_$option" ) ) {
$options[$option] = $value;
}
}
if ( $options ) {
Jetpack::update_options( $options );
foreach ( array_keys( $options ) as $option ) {
delete_option( "jetpack_$option" );
}
}
// Add missing version and old_version options
if ( !$version = Jetpack::get_option( 'version' ) ) {
$version = $old_version = '1.1:' . time();
Jetpack::update_options( compact( 'version', 'old_version' ) );
}
}
// Upgrade from a single user token to a user_id-indexed array and a master_user ID
if ( !Jetpack::get_option( 'user_tokens' ) ) {
if ( $user_token = Jetpack::get_option( 'user_token' ) ) {
$token_parts = explode( '.', $user_token );
if ( isset( $token_parts[2] ) ) {
$master_user = $token_parts[2];
$user_tokens = array( $master_user => $user_token );
Jetpack::update_options( compact( 'master_user', 'user_tokens' ) );
Jetpack::delete_option( 'user_token' );
} else {
// @todo: is this even possible?
trigger_error( sprintf( 'Jetpack::plugin_upgrade found no user_id in user_token "%s"', $user_token ), E_USER_WARNING );
}
}
}
}
/**
* Constructor. Initializes WordPress hooks
*/
function Jetpack() {
$this->sync = new Jetpack_Sync;
// Modules should do Jetpack_Sync::sync_options( __FILE__, $option, ... ); instead
// We access the "internal" method here only because the Jetpack object isn't instantiated yet
$this->sync->options( __FILE__,
'home',
'siteurl',
'blogname',
'gmt_offset',
'timezone_string'
);
if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
@ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed.
require_once dirname( __FILE__ ) . '/class.jetpack-xmlrpc-server.php';
$this->xmlrpc_server = new Jetpack_XMLRPC_Server();
$this->require_jetpack_authentication();
if ( Jetpack::is_active() ) {
// Hack to preserve $HTTP_RAW_POST_DATA
add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
// The actual API methods.
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) );
} else {
// The bootstrap API methods.
add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) );
}
// Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on.
add_filter( 'pre_option_enable_xmlrpc', '__return_true' );
} elseif ( is_admin() && isset( $_POST['action'] ) && 'jetpack_upload_file' == $_POST['action'] ) {
$this->require_jetpack_authentication();
$this->add_remote_request_handlers();
} else {
if ( Jetpack::is_active() ) {
add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) );
}
}
add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) );
if ( !wp_next_scheduled( 'jetpack_clean_nonces' ) ) {
wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
}
add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 999 ); // run late so that other plugins hooking into this menu don't get left out
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
add_action( 'wp_ajax_jetpack-check-news-subscription', array( $this, 'check_news_subscription' ) );
add_action( 'wp_ajax_jetpack-subscribe-to-news', array( $this, 'subscribe_to_news' ) );
add_action( 'wp_loaded', array( $this, 'register_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
add_action( 'jetpack_activate_module', array( $this, 'activate_module_actions' ) );
/**
* These actions run checks to load additional files.
* They check for external files or plugins, so thef need to run as late as possible.
*/
add_action( 'plugins_loaded', array( $this, 'check_open_graph' ), 999 );
add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
}
function require_jetpack_authentication() {
// Don't let anyone authenticate
$_COOKIE = array();
remove_all_filters( 'authenticate' );
if ( Jetpack::is_active() ) {
// Allow Jetpack authentication
add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 );
}
}
/**
* Register assets for use in various modules and the Jetpack admin page.
*
* @uses wp_script_is, wp_register_script, plugins_url
* @action wp_loaded
* @return null
*/
public function register_assets() {
if ( ! wp_script_is( 'spin', 'registered' ) )
wp_register_script( 'spin', plugins_url( '_inc/spin.js', __FILE__ ), false, '1.2.4' );
if ( ! wp_script_is( 'jquery.spin', 'registered' ) )
wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', __FILE__ ) , array( 'jquery', 'spin' ) );
if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) )
wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', __FILE__ ), array( 'media-views' ), '20121225' );
}
/**
* Device Pixels support
* This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers.
*/
function devicepx() {
wp_enqueue_script( 'devicepx', ( is_ssl() ? 'https' : 'http' ) . '://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate('oW'), true );
}
/**
* Is Jetpack active?
*/
public static function is_active() {
return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
}
/**
* Is Jetpack in development (offline) mode?
*/
public static function is_development_mode() {
$development_mode = false;
if ( defined( 'JETPACK_DEV_DEBUG' ) ) {
$development_mode = JETPACK_DEV_DEBUG;
}
elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
$development_mode = true;
}
return apply_filters( 'jetpack_development_mode', $development_mode );
}
/**
* Is a given user (or the current user if none is specified) linked to a WordPress.com user?
*/
public static function is_user_connected( $user_id = false ) {
$user_id = false === $user_id ? get_current_user_id() : absint( $user_id );
if ( !$user_id ) {
return false;
}
return (bool) Jetpack_Data::get_access_token( $user_id );
}
function current_user_is_connection_owner() {
$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id;
}
/**
* Synchronize connected user role changes
*/
function user_role_change( $user_id ) {
if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) {
$current_user_id = get_current_user_id();
wp_set_current_user( $user_id );
$role = $this->translate_current_user_to_role();
$signed_role = $this->sign_role( $role );
wp_set_current_user( $current_user_id );
$master_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
$master_user_id = absint( $master_token->external_user_id );
if ( !$master_user_id )
return; // this shouldn't happen
Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role );
//@todo retry on failure
//try to choose a new master if we're demoting the current one
if ( $user_id == $master_user_id && 'administrator' != $role ) {
$query = new WP_User_Query( array(
'fields' => array( 'id' ),
'role' => 'administrator',
'orderby' => 'id',
'exclude' => array( $master_user_id ),
)
);
$new_master = false;
foreach ( $query->results as $result ) {
$uid = absint( $result->id );
if ( $uid && Jetpack::is_user_connected( $uid ) ) {
$new_master = $uid;
break;
}
}
if ( $new_master ) {
Jetpack::update_option( 'master_user', $new_master );
}
// else disconnect..?
}
}
}
/**
* Loads the currently active modules.
*/
public static function load_modules() {
if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
return;
}
$version = Jetpack::get_option( 'version' );
if ( !$version ) {
$version = $old_version = JETPACK__VERSION . ':' . time();
Jetpack::update_options( compact( 'version', 'old_version' ) );
}
list( $version ) = explode( ':', $version );
$modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) );
$modules_data = array();
// Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check.
if ( version_compare( $version, JETPACK__VERSION, '<' ) ) {
$updated_modules = array();
foreach ( $modules as $module ) {
$modules_data[ $module ] = Jetpack::get_module( $module );
if ( ! isset( $modules_data[ $module ]['changed'] ) ) {
continue;
}
if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) {
continue;
}
$updated_modules[] = $module;
}
$modules = array_diff( $modules, $updated_modules );
}
foreach ( $modules as $module ) {
// If not connected and we're in dev mode, disable modules requiring a connection
if ( ! Jetpack::is_active() && Jetpack::is_development_mode() ) {
if ( empty( $modules_data[ $module ] ) ) {
$modules_data[ $module ] = Jetpack::get_module( $module );
}
if ( $modules_data[ $module ]['requires_connection'] ) {
Jetpack::deactivate_module( $module );
continue;
}
}
if ( did_action( 'jetpack_module_loaded_' . $module ) ) {
continue;
}
require Jetpack::get_module_path( $module );
do_action( 'jetpack_module_loaded_' . $module );
}
do_action( 'jetpack_modules_loaded' );
// 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.
require_once( dirname( __FILE__ ) . '/modules/module-extras.php' );
}
/**
* Check if Jetpack's REST API compat file should be included
* @action plugins_loaded
* @return null
*/
public function check_rest_api_compat() {
$_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() );
if ( function_exists( 'bbpress' ) )
$_jetpack_rest_api_compat_includes[] = dirname( __FILE__ ) . '/class.jetpack-bbpress-json-api-compat.php';
foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include )
require_once $_jetpack_rest_api_compat_include;
}
/**
* Check if Jetpack's Open Graph tags should be used.
* If certain plugins are active, Jetpack's og tags are suppressed.
*
* @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
* @action plugins_loaded
* @return null
*/
public function check_open_graph() {
if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) )
add_filter( 'jetpack_enable_open_graph', '__return_true', 0 );
$active_plugins = get_option( 'active_plugins', array() );
$conflicting_plugins = array(
'facebook/facebook.php', // Official Facebook plugin
'wordpress-seo/wp-seo.php', // WordPress SEO by Yoast
'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook
'facebook-awd/AWD_facebook.php', // Facebook AWD All in one
'header-footer/plugin.php', // Header and Footer
'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG
'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments
'seo-ultimate/seo-ultimate.php', // SEO Ultimate
'sexybookmarks/sexy-bookmarks.php', // Shareaholic
'shareaholic/sexy-bookmarks.php', // Shareaholic
'social-discussions/social-discussions.php', // Social Discussions
'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', // NextScripts SNAP
'wordbooker/wordbooker.php', // Wordbooker
'socialize/socialize.php', // Socialize
'simple-facebook-connect/sfc.php', // Simple Facebook Connect
'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit
'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol
'opengraph/opengraph.php', // Open Graph
'sharepress/sharepress.php', // SharePress
);
foreach ( $conflicting_plugins as $plugin ) {
if ( in_array( $plugin, $active_plugins ) ) {
add_filter( 'jetpack_enable_open_graph', '__return_false', 99 );
break;
}
}
if ( apply_filters( 'jetpack_enable_open_graph', false ) )
require_once dirname( __FILE__ ) . '/functions.opengraph.php';
}
/* Jetpack Options API */
public static function get_option_names( $type = 'compact' ) {
switch ( $type ) {
case 'non-compact' :
case 'non_compact' :
return array(
'register',
'activated',
'active_modules',
'do_activate',
'publicize',
'widget_twitter',
);
}
return array(
'id', // (int) The Client ID/WP.com Blog ID of this site.
'blog_token', // (string) The Client Secret/Blog Token of this site.
'user_token', // (string) The User Token of this site. (deprecated)
'publicize_connections', // (array) An array of Publicize connections from WordPress.com
'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com.
'user_tokens', // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com.
'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time
'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time
'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL.
'time_diff', // (int) Offset between Jetpack server's clocks and this server's clocks. Jetpack Server Time = time() + (int) Jetpack::get_option( 'time_diff' )
'public', // (int|bool) If we think this site is public or not (1, 0), false if we haven't yet tried to figure it out.
);
}
/**
* Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate.
*
* @param string $name Option name
* @param mixed $default (optional)
*/
public static function get_option( $name, $default = false ) {
if ( in_array( $name, Jetpack::get_option_names( 'non_compact' ) ) ) {
return get_option( "jetpack_$name" );
} else if ( !in_array( $name, Jetpack::get_option_names() ) ) {
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
return false;
}
$options = get_option( 'jetpack_options' );
if ( is_array( $options ) && isset( $options[$name] ) ) {
return $options[$name];
}
return $default;
}
/**
* Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action
* Does some extra verification so urls (such as those to public-api, register, etc) cant just be crafted
* $name must be a registered option name.
*/
public static function create_nonce( $name ) {
$secret = wp_generate_password( 32, false ) . ':' . wp_generate_password( 32, false ) . ':' . ( time() + 600 );
Jetpack::update_option( $name, $secret );
@list( $secret_1, $secret_2, $eol ) = explode( ':', Jetpack::get_option( $name ) );
if ( empty( $secret_1 ) || empty( $secret_2 ) || $eol < time() )
return new Jetpack_Error( 'missing_secrets' );
return array(
'secret_1' => $secret_1,
'secret_2' => $secret_2,
'eol' => $eol,
);
}
/**
* Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate.
*
* @param string $name Option name
* @param mixed $value Option value
*/
public static function update_option( $name, $value ) {
if ( in_array( $name, Jetpack::get_option_names( 'non_compact' ) ) ) {
return update_option( "jetpack_$name", $value );
} else if ( !in_array( $name, Jetpack::get_option_names() ) ) {
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
return false;
}
$options = get_option( 'jetpack_options' );
if ( !is_array( $options ) ) {
$options = array();
}
$options[$name] = $value;
return update_option( 'jetpack_options', $options );
}
/**
* Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate.
*
* @param array $array array( option name => option value, ... )
*/
public static function update_options( $array ) {
$names = array_keys( $array );
foreach ( array_diff( $names, Jetpack::get_option_names(), Jetpack::get_option_names( 'non_compact' ) ) as $unknown_name ) {
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
unset( $array[$unknown_name] );
}
foreach ( array_intersect( $names, Jetpack::get_option_names( 'non_compact' ) ) as $name ) {
update_option( "jetpack_$name", $array[$name] );
unset( $array[$name] );
}
$options = get_option( 'jetpack_options' );
if ( !is_array( $options ) ) {
$options = array();
}
return update_option( 'jetpack_options', array_merge( $options, $array ) );
}
/**
* Deletes the given option. May be passed multiple option names as an array.
* Updates jetpack_options and/or deletes jetpack_$name as appropriate.
*
* @param string|array $names
*/
public static function delete_option( $names ) {
$names = (array) $names;
foreach ( array_diff( $names, Jetpack::get_option_names(), Jetpack::get_option_names( 'non_compact' ) ) as $unknown_name ) {
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
}
foreach ( array_intersect( $names, Jetpack::get_option_names( 'non_compact' ) ) as $name ) {
delete_option( "jetpack_$name" );
}
$options = get_option( 'jetpack_options' );
if ( !is_array( $options ) ) {
$options = array();
}
$to_delete = array_intersect( $names, Jetpack::get_option_names(), array_keys( $options ) );
if ( $to_delete ) {
foreach ( $to_delete as $name ) {
unset( $options[$name] );
}
return update_option( 'jetpack_options', $options );
}
return true;
}
/**
* Enters a user token into the user_tokens option
*
* @param int $user_id
* @param string $token
* return bool
*/
public static function update_user_token( $user_id, $token, $is_master_user ) {
// not designed for concurrent updates
$user_tokens = Jetpack::get_option( 'user_tokens' );
if ( ! is_array( $user_tokens ) )
$user_tokens = array();
$user_tokens[$user_id] = $token;
if ( $is_master_user ) {
$master_user = $user_id;
$options = compact('user_tokens', 'master_user');
} else {
$options = compact('user_tokens');
}
return Jetpack::update_options( $options );
}
/**
* Returns an array of all PHP files in the specified absolute path.
* Equivalent to glob( "$absolute_path/*.php" ).
*
* @param string $absolute_path The absolute path of the directory to search.
* @return array Array of absolute paths to the PHP files.
*/
public static function glob_php( $absolute_path ) {
$absolute_path = untrailingslashit( $absolute_path );
$files = array();
if ( !$dir = @opendir( $absolute_path ) ) {
return $files;
}
while ( false !== $file = readdir( $dir ) ) {
if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) {
continue;
}
$file = "$absolute_path/$file";
if ( !is_file( $file ) ) {
continue;
}
$files[] = $file;
}
closedir( $dir );
return $files;
}
public function activate_new_modules() {
if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
return;
}
$jetpack_old_version = Jetpack::get_option( 'version' ); // [sic]
if ( !$jetpack_old_version ) {
$jetpack_old_version = $version = $old_version = '1.1:' . time();
Jetpack::update_options( compact( 'version', 'old_version' ) );
}
list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic]
if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) {
return;
}
$active_modules = Jetpack::get_active_modules();
$reactivate_modules = array();
foreach ( $active_modules as $active_module ) {
$module = Jetpack::get_module( $active_module );
if ( !isset( $module['changed'] ) ) {
continue;
}
if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) {
continue;
}
$reactivate_modules[] = $active_module;
Jetpack::deactivate_module( $active_module );
}
if ( version_compare( $jetpack_version, '1.9.2', '<' ) && version_compare( '1.9-something', JETPACK__VERSION, '<' ) ) {
add_action( 'jetpack_activate_default_modules', array( $this->sync, 'sync_all_registered_options' ), 1000 );
}
Jetpack::update_options( array(
'version' => JETPACK__VERSION . ':' . time(),
'old_version' => $jetpack_old_version,
) );
Jetpack::state( 'message', 'modules_activated' );
Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules );
wp_safe_redirect( Jetpack::admin_url() );
exit;
}
/**
* List available Jetpack modules. Simply lists .php files in /modules/.
* Make sure to tuck away module "library" files in a sub-directory.
*/
public static function get_available_modules( $min_version = false, $max_version = false ) {
static $modules = null;
if ( !isset( $modules ) ) {
$files = Jetpack::glob_php( dirname( __FILE__ ) . '/modules' );
$modules = array();
foreach ( $files as $file ) {
if ( !$headers = Jetpack::get_module( $file ) ) {
continue;
}
$modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced'];
}
}
if ( !$min_version && !$max_version ) {
return array_keys( $modules );
}
$r = array();
foreach ( $modules as $slug => $introduced ) {
if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) {
continue;
}
if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) {
continue;
}
$r[] = $slug;
}
return $r;
}
/**
* Default modules loaded on activation.
*/
public static function get_default_modules( $min_version = false, $max_version = false ) {
$return = array();
foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) {
// Add special cases here for modules to avoid auto-activation
switch ( $module ) {
// These modules are default off: they change things blog-side
case 'comments' :
case 'carousel' :
case 'minileven':
case 'infinite-scroll' :
case 'photon' :
case 'tiled-gallery' :
case 'likes' :
break;
// These modules are default off if we think the site is a private one
case 'enhanced-distribution' :
case 'json-api' :
if ( !Jetpack::get_option( 'public' ) ) {
break;
}
// else no break
// The rest are default on
default :
$return[] = $module;
}
}
return $return;
}
/**
* Extract a module's slug from its full path.
*/
public static function get_module_slug( $file ) {
return str_replace( '.php', '', basename( $file ) );
}
/**
* Generate a module's path from its slug.
*/
public static function get_module_path( $slug ) {
return dirname( __FILE__ ) . "/modules/$slug.php";
}
/**
* Load module data from module file. Headers differ from WordPress
* plugin headers to avoid them being identified as standalone
* plugins on the WordPress plugins page.
*/
public static function get_module( $module ) {
$headers = array(
'name' => 'Module Name',
'description' => 'Module Description',
'sort' => 'Sort Order',
'introduced' => 'First Introduced',
'changed' => 'Major Changes In',
'deactivate' => 'Deactivate',
'free' => 'Free',
'requires_connection' => 'Requires Connection',
);
$file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) );
if ( !file_exists( $file ) )
return false;
$mod = get_file_data( $file, $headers );
if ( empty( $mod['name'] ) )
return false;
$mod['name'] = translate( $mod['name'], 'jetpack' );
$mod['description'] = translate( $mod['description'], 'jetpack' );
if ( empty( $mod['sort'] ) )
$mod['sort'] = 10;
$mod['deactivate'] = empty( $mod['deactivate'] );
$mod['free'] = empty( $mod['free'] );
$mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true;
return $mod;
}
/**
* Get a list of activated modules as an array of module slugs.
*/
public static function get_active_modules() {
$active = Jetpack::get_option( 'active_modules' );
if ( !is_array( $active ) )
$active = array();
if ( is_admin() ) {
$active[] = 'vaultpress';
} else {
$active = array_diff( $active, array( 'vaultpress' ) );
}
return array_unique( $active );
}
public static function is_module( $module ) {
return !empty( $module ) && !validate_file( $module, Jetpack::get_available_modules() );
}
/**
* Catches PHP errors. Must be used in conjunction with output buffering.
*
* @param bool $catch True to start catching, False to stop.
*
* @static
*/
public static function catch_errors( $catch ) {
static $display_errors, $error_reporting;
if ( $catch ) {
$display_errors = @ini_set( 'display_errors', 1 );
$error_reporting = @error_reporting( E_ALL );
add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
} else {
@ini_set( 'display_errors', $display_errors );
@error_reporting( $error_reporting );
remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 1 );
}
}
/**
* Saves any generated PHP errors in ::state( 'php_errors', {errors} )
*/
public static function catch_errors_on_shutdown() {
Jetpack::state( 'php_errors', ob_get_clean() );
}
public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array() ) {
$jetpack = Jetpack::init();
$modules = Jetpack::get_default_modules( $min_version, $max_version );
$modules = array_merge( $other_modules, $modules );
// Look for standalone plugins and disable if active.
$to_deactivate = array();
foreach ( $modules as $module ) {
if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) {
$to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module];
}
}
$deactivated = array();
foreach ( $to_deactivate as $module => $deactivate_me ) {
list( $probable_file, $probable_title ) = $deactivate_me;
if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) {
$deactivated[] = $module;
}
}
if ( $deactivated ) {
Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) );
$url = add_query_arg( array(
'action' => 'activate_default_modules',
'_wpnonce' => wp_create_nonce( 'activate_default_modules' ),
), add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url() ) );
wp_safe_redirect( $url );
exit;
}
do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules );
// Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating
$redirect = menu_page_url( 'jetpack', false );
Jetpack::restate();
Jetpack::catch_errors( true );
foreach ( $modules as $module ) {
$active = Jetpack::get_active_modules();
if ( in_array( $module, $active ) ) {
$module_info = Jetpack::get_module( $module );
if ( !$module_info['deactivate'] ) {
$state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
if ( $active_state = Jetpack::state( $state ) ) {
$active_state = explode( ',', $active_state );
} else {
$active_state = array();
}
$active_state[] = $module;
Jetpack::state( $state, implode( ',', $active_state ) );
}
continue;
}
$file = Jetpack::get_module_path( $module );
if ( !file_exists( $file ) ) {
continue;
}
// we'll override this later if the plugin can be included without fatal error
wp_safe_redirect( Jetpack::admin_url() );
Jetpack::state( 'error', 'module_activation_failed' );
Jetpack::state( 'module', $module );
ob_start();
require $file;
do_action( 'jetpack_activate_module', $module );
$active[] = $module;
$state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
if ( $active_state = Jetpack::state( $state ) ) {
$active_state = explode( ',', $active_state );
} else {
$active_state = array();
}
$active_state[] = $module;
Jetpack::state( $state, implode( ',', $active_state ) );
Jetpack::update_option( 'active_modules', array_unique( $active ) );
ob_end_clean();
}
Jetpack::state( 'error', false );
Jetpack::state( 'module', false );
Jetpack::catch_errors( false );
do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules );
}
public static function activate_module( $module ) {
$jetpack = Jetpack::init();
if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() )
return false;
if ( ! strlen( $module ) )
return false;
if ( ! Jetpack::is_module( $module ) )
return false;
// If it's already active, then don't do it again
$active = Jetpack::get_active_modules();
foreach ( $active as $act ) {
if ( $act == $module )
return true;
}
// If we're not connected but in development mode, make sure the module doesn't require a connection
if ( ! Jetpack::is_active() && Jetpack::is_development_mode() ) {
$module_data = Jetpack::get_module( $module );
if ( $module_data['requires_connection'] ) {
return false;
}
}
// Check and see if the old plugin is active
if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) {
// Deactivate the old plugin
if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[$module][0], $jetpack->plugins_to_deactivate[$module][1] ) ) {
// If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module
// We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load.
Jetpack::state( 'deactivated_plugins', $module );
wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
exit;
}
}
// Check the file for fatal errors, a la wp-admin/plugins.php::activate
Jetpack::state( 'module', $module );
Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error
wp_safe_redirect( Jetpack::admin_url() );
Jetpack::catch_errors( true );
ob_start();
require Jetpack::get_module_path( $module );
do_action( 'jetpack_activate_module', $module );
$active[] = $module;
Jetpack::update_option( 'active_modules', array_unique( $active ) );
Jetpack::state( 'error', false ); // the override
Jetpack::state( 'message', 'module_activated' );
Jetpack::state( 'module', $module );
ob_end_clean();
Jetpack::catch_errors( false );
exit;
}
function activate_module_actions( $module ) {
do_action( "jetpack_activate_module_$module" );
$this->sync->sync_all_module_options( $module );
}
public static function deactivate_module( $module ) {
$active = Jetpack::get_active_modules();
$new = array();
foreach ( $active as $check ) {
if ( !empty( $check ) && $module != $check )
$new[] = $check;
}
do_action( "jetpack_deactivate_module_$module" );
return Jetpack::update_option( 'active_modules', array_unique( $new ) );
}
public static function enable_module_configurable( $module ) {
$module = Jetpack::get_module_slug( $module );
add_filter( 'jetpack_module_configurable_' . $module, '__return_true' );
}
public static function module_configuration_url( $module ) {
$module = Jetpack::get_module_slug( $module );
return Jetpack::admin_url( array( 'configure' => $module ) );
}
public static function module_configuration_load( $module, $method ) {
$module = Jetpack::get_module_slug( $module );
add_action( 'jetpack_module_configuration_load_' . $module, $method );
}
public static function module_configuration_head( $module, $method ) {
$module = Jetpack::get_module_slug( $module );
add_action( 'jetpack_module_configuration_head_' . $module, $method );
}
public static function module_configuration_screen( $module, $method ) {
$module = Jetpack::get_module_slug( $module );
add_action( 'jetpack_module_configuration_screen_' . $module, $method );
}
/* Installation */
public static function bail_on_activation( $message, $deactivate = true ) {
?>
$plugin ) {
if ( $plugin === $jetpack ) {
$plugins[$i] = false;
$update = true;
}
}
if ( $update ) {
update_option( 'active_plugins', array_filter( $plugins ) );
}
}
exit;
}
/**
* Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
* @static
*/
public static function plugin_activation( $network_wide ) {
Jetpack::update_option( 'activated', 1 );
if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) );
}
if ( $network_wide )
Jetpack::state( 'network_nag', true );
Jetpack::plugin_initialize();
}
/**
* Sets the internal version number and activation state.
* @static
*/
public static function plugin_initialize() {
if ( !Jetpack::get_option( 'activated' ) ) {
Jetpack::update_option( 'activated', 2 );
}
if ( !Jetpack::get_option( 'version' ) ) {
$version = $old_version = JETPACK__VERSION . ':' . time();
Jetpack::update_options( compact( 'version', 'old_version' ) );
}
Jetpack::load_modules();
Jetpack::delete_option( 'do_activate' );
}
/**
* Removes all connection options
* @static
*/
public static function plugin_deactivation( $network_wide ) {
Jetpack::disconnect( false );
}
/**
* Disconnects from the Jetpack servers.
* Forgets all connection details and tells the Jetpack servers to do the same.
* @static
*/
public static function disconnect( $update_activated_state = true ) {
wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
Jetpack::clean_nonces( true );
Jetpack::load_xml_rpc_client();
$xml = new Jetpack_IXR_Client();
$xml->query( 'jetpack.deregister' );
Jetpack::delete_option( array(
'register',
'blog_token',
'user_token',
'user_tokens',
'master_user',
'time_diff',
'fallback_no_verify_ssl_certs',
) );
if ( $update_activated_state ) {
Jetpack::update_option( 'activated', 4 );
}
}
/**
* Unlinks the current user from the linked WordPress.com user
*/
function unlink_user() {
if ( !$tokens = Jetpack::get_option( 'user_tokens' ) )
return false;
$user_id = get_current_user_id();
if ( Jetpack::get_option( 'master_user' ) == $user_id )
return false;
if ( !isset( $tokens[$user_id] ) )
return false;
Jetpack::load_xml_rpc_client();
$xml = new Jetpack_IXR_Client( compact( 'user_id' ) );
$xml->query( 'jetpack.unlink_user', $user_id );
unset( $tokens[$user_id] );
Jetpack::update_option( 'user_tokens', $tokens );
return true;
}
/**
* Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load()
*/
public static function try_registration() {
$result = Jetpack::register();
// If there was an error with registration and the site was not registered, record this so we can show a message.
if ( !$result || is_wp_error( $result ) ) {
return $result;
} else {
return true;
}
}
/* Admin Pages */
function admin_init() {
// If the plugin is not connected, display a connect message.
if (
// the plugin was auto-activated and needs its candy
Jetpack::get_option( 'do_activate' )
||
// the plugin is active, but was never activated. Probably came from a site-wide network activation
!Jetpack::get_option( 'activated' )
) {
Jetpack::plugin_initialize();
}
if ( !Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
if ( 4 != Jetpack::get_option( 'activated' ) ) {
// Show connect notice on dashboard and plugins pages
add_action( 'load-index.php', array( $this, 'prepare_connect_notice' ) );
add_action( 'load-plugins.php', array( $this, 'prepare_connect_notice' ) );
}
} elseif ( false === Jetpack::get_option( 'fallback_no_verify_ssl_certs' ) ) {
// Upgrade: 1.1 -> 1.1.1
// Check and see if host can verify the Jetpack servers' SSL certificate
$args = array();
Jetpack_Client::_wp_remote_request(
Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ), $args ),
$args,
true
);
}
add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
add_action( 'admin_head', array( $this, 'admin_menu_css' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
add_action( 'wp_ajax_jetpack_debug', array( $this, 'ajax_debug' ) );
if ( Jetpack::is_active() || Jetpack::is_development_mode() ) {
// Artificially throw errors in certain whitelisted cases during plugin activation
add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
// Kick off synchronization of user role when it changes
add_action( 'set_user_role', array( $this, 'user_role_change' ) );
// Add retina images hotfix to admin
global $wp_db_version;
if ( $wp_db_version > 19470 ) {
// WP 3.4.x
// TODO will need to add && $wp_db_version < xxxxx when 3.5 comes out.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_retina_scripts' ) );
// /wp-admin/customize.php omits the action above.
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_retina_scripts' ) );
}
}
}
function prepare_connect_notice() {
add_action( 'admin_print_styles', array( $this, 'admin_styles' ) );
add_action( 'admin_notices', array( $this, 'admin_connect_notice' ) );
if ( Jetpack::state( 'network_nag' ) )
add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
}
/**
* Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
* This function artificially throws errors for such cases (whitelisted).
*
* @param string $plugin The activated plugin.
*/
function throw_error_on_activate_plugin( $plugin ) {
$active_modules = Jetpack::get_active_modules();
// The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks.
if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) {
$throw = false;
// Try and make sure it really was the stats plugin
if ( !class_exists( 'ReflectionFunction' ) ) {
if ( 'stats.php' == basename( $plugin ) ) {
$throw = true;
}
} else {
$reflection = new ReflectionFunction( 'stats_get_api_key' );
if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) {
$throw = true;
}
}
if ( $throw ) {
trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR );
}
}
}
function intercept_plugin_error_scrape_init() {
add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 );
}
function intercept_plugin_error_scrape( $action, $result ) {
if ( !$result ) {
return;
}
foreach ( $this->plugins_to_deactivate as $module => $deactivate_me ) {
if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) {
Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old “%1$s” plugin.', 'jetpack' ), $deactivate_me[1] ), false );
}
}
}
function admin_menu() {
list( $jetpack_version ) = explode( ':', Jetpack::get_option( 'version' ) );
if (
$jetpack_version
&&
$jetpack_version != JETPACK__VERSION
&&
( $new_modules = Jetpack::get_default_modules( $jetpack_version, JETPACK__VERSION ) )
&&
is_array( $new_modules )
&&
( $new_modules_count = count( $new_modules ) )
&&
( Jetpack::is_active() || Jetpack::is_development_mode() )
) {
$new_modules_count_i18n = number_format_i18n( $new_modules_count );
$span_title = esc_attr( sprintf( _n( 'One New Jetpack Module', '%s New Jetpack Modules', $new_modules_count, 'jetpack' ), $new_modules_count_i18n ) );
$title = sprintf( 'Jetpack %s', "$new_modules_count_i18n" );
} else {
$title = __( 'Jetpack', 'jetpack' );
}
$hook = add_menu_page( 'Jetpack', $title, 'read', 'jetpack', array( $this, 'admin_page' ), 'div' );
add_action( "load-$hook", array( $this, 'admin_page_load' ) );
if ( version_compare( $GLOBALS['wp_version'], '3.3', '<' ) ) {
if ( isset( $_GET['page'] ) && 'jetpack' == $_GET['page'] ) {
add_contextual_help( $hook, $this->jetpack_help() );
}
} else {
add_action( "load-$hook", array( $this, 'admin_help' ) );
}
add_action( "admin_head-$hook", array( $this, 'admin_head' ) );
add_filter( 'custom_menu_order', array( $this, 'admin_menu_order' ) );
add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) );
add_action( "admin_print_styles-$hook", array( $this, 'admin_styles' ) );
add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts' ) );
do_action( 'jetpack_admin_menu' );
}
function add_remote_request_handlers() {
add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) );
}
function remote_request_handlers() {
switch ( current_filter() ) {
case 'wp_ajax_nopriv_jetpack_upload_file' :
$response = $this->upload_handler();
break;
default :
$response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 );
break;
}
if ( !$response ) {
$response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 );
}
if ( is_wp_error( $response ) ) {
$status_code = $response->get_error_data();
$error = $response->get_error_code();
$error_description = $response->get_error_message();
if ( !is_int( $status_code ) ) {
$status_code = 400;
}
status_header( $status_code );
die( json_encode( (object) compact( 'error', 'error_description' ) ) );
}
status_header( 200 );
if ( true === $response ) {
exit;
}
die( json_encode( (object) $response ) );
}
function upload_handler() {
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 );
}
$user = wp_authenticate( '', '' );
if ( !$user || is_wp_error( $user ) ) {
return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 );
}
wp_set_current_user( $user->ID );
if ( !current_user_can( 'upload_files' ) ) {
return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 );
}
if ( empty( $_FILES ) ) {
return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 );
}
foreach ( array_keys( $_FILES ) as $files_key ) {
if ( !isset( $_POST["_jetpack_file_hmac_{$files_key}"] ) ) {
return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 );
}
}
$media_keys = array_keys( $_FILES['media'] );
$token = Jetpack_Data::get_access_token( get_current_user_id() );
if ( !$token || is_wp_error( $token ) ) {
return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 );
}
$uploaded_files = array();
$global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
unset( $GLOBALS['post'] );
foreach ( $_FILES['media']['name'] as $index => $name ) {
$file = array();
foreach ( $media_keys as $media_key ) {
$file[$media_key] = $_FILES['media'][$media_key][$index];
}
list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][$index] );
$hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret );
if ( $hmac_provided !== $hmac_file ) {
$uploaded_files[$index] = (object) array( 'error' => 'invalid_hmac', 'error_description' => 'The corresponding HMAC for this file does not match' );
continue;
}
$_FILES['.jetpack.upload.'] = $file;
$post_id = isset( $_POST['post_id'][$index] ) ? absint( $_POST['post_id'][$index] ) : 0;
if ( !current_user_can( 'edit_post', $post_id ) ) {
$post_id = 0;
}
$attachment_id = media_handle_upload( '.jetpack.upload.', $post_id, array(), array(
'action' => 'jetpack_upload_file',
) );
if ( !$attachment_id ) {
$uploaded_files[$index] = (object) array( 'error' => 'unknown', 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site' );
} elseif ( is_wp_error( $attachment_id ) ) {
$uploaded_files[$index] = (object) array( 'error' => 'attachment_' . $attachment_id->get_error_code(), 'error_description' => $attachment_id->get_error_message() );
} else {
$attachment = get_post( $attachment_id );
$uploaded_files[$index] = (object) array(
'id' => (string) $attachment_id,
'file' => $attachment->post_title,
'url' => wp_get_attachment_url( $attachment_id ),
'type' => $attachment->post_mime_type,
'meta' => wp_get_attachment_metadata( $attachment_id ),
);
}
}
if ( !is_null( $global_post ) ) {
$GLOBALS['post'] = $global_post;
}
return $uploaded_files;
}
/**
* Add help to the Jetpack page
*
* Deprecated. Remove when Jetpack requires WP 3.3+
*/
function jetpack_help() {
return
'
' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '
' .
'
' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '
' .
'
' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '
' .
'
' . __( 'Jetpack Module Options', 'jetpack' ) . '
' .
'
' . __( 'To Activate/Deactivate a Module - Click on Learn More. An Activate or Deactivate button will now appear next to the Learn More button. Click the Activate/Deactivate button.', 'jetpack' ) . '
';
}
/**
* Add help to the Jetpack page
*
* @since Jetpack (1.2.3)
* @return false if not the Jetpack page
*/
function admin_help() {
$current_screen = get_current_screen();
// Overview
$current_screen->add_help_tab( array(
'id' => 'overview',
'title' => __( 'Overview', 'jetpack' ),
'content' =>
'
' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '
' .
'
' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '
' .
'
' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '
'
);
}
function admin_menu_css() { ?>
$item ) {
if ( $item != 'jetpack' )
$jp_menu_order[] = $item;
if ( $index == 0 )
$jp_menu_order[] = 'jetpack';
}
return $jp_menu_order;
}
function admin_head() {
if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) )
do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
}
function admin_styles() {
global $wp_styles;
wp_enqueue_style( 'jetpack', plugins_url( basename( dirname( __FILE__ ) ) . '/_inc/jetpack.css' ), false, JETPACK__VERSION . '-20121016' );
$wp_styles->add_data( 'jetpack', 'rtl', true );
}
function admin_scripts() {
wp_enqueue_script( 'jetpack-js', plugins_url( basename( dirname( __FILE__ ) ) ) . '/_inc/jetpack.js', array( 'jquery' ), JETPACK__VERSION . '-20121111' );
wp_localize_script( 'jetpack-js', 'jetpackL10n', array(
'ays_disconnect' => "This will deactivate all Jetpack modules.\nAre you sure you want to disconnect?",
'ays_unlink' => "This will prevent user-specific modules such as Publicize, Notifications and Post By Email from working.\nAre you sure you want to unlink?",
'ays_dismiss' => "This will deactivate Jetpack.\nAre you sure you want to deactivate Jetpack?",
) );
add_action( 'admin_footer', array( $this, 'do_stats' ) );
}
function enqueue_retina_scripts() {
wp_enqueue_style( 'jetpack-retina', plugins_url( basename( dirname( __FILE__ ) ) . '/_inc/jetpack-retina.css' ), false, JETPACK__VERSION . '-20120730' );
}
function plugin_action_links( $actions ) {
return array_merge(
array( 'settings' => sprintf( '%s', Jetpack::admin_url(), __( 'Settings', 'jetpack' ) ) ),
$actions
);
return $actions;
}
function admin_connect_notice() {
// Don't show the connect notice on the jetpack settings page. @todo: must be a better way?
if ( false !== strpos( $_SERVER['QUERY_STRING'], 'page=jetpack' ) )
return;
if ( !current_user_can( 'manage_options' ) )
return;
?>
Your Jetpack is almost ready – A connection to WordPress.com is needed to enable features like Stats, Contact Forms, and Subscriptions. Connect now to get fueled up!', 'jetpack' ); ?>
Jetpack is installed and ready to bring awesome, WordPress.com cloud-powered features to your site.', 'jetpack' ) ?>
Jetpack is activated! Each site on your network must be connected individually by an admin on that site.', 'jetpack' ) ?>
' . sprintf(
__( 'Jetpack now includes Jetpack Comments, which enables your visitors to use their WordPress.com, Twitter, or Facebook accounts when commenting on your site. To activate Jetpack Comments, %s.', 'jetpack' ),
wp_nonce_url(
Jetpack::admin_url( array(
'action' => 'activate',
'module' => 'comments',
) ),
"jetpack_activate-comments"
),
__( 'click here', 'jetpack' )
);
}
/*
* Registration flow:
* 1 - ::admin_page_load() action=register
* 2 - ::try_registration()
* 3 - ::register()
* - Creates jetpack_register option containing two secrets and a timestamp
* - Calls https://jetpack.wordpress.com/jetpack.register/1/ with
* siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id
* - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's
* xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1
* - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2
* - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with
* jetpack_id, jetpack_secret, jetpack_public
* - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret
* 4 - redirect to https://jetpack.wordpress.com/jetpack.authorize/1/
* 5 - user logs in with WP.com account
* 6 - redirect to this site's wp-admin/index.php?page=jetpack&action=authorize with
* code <-- OAuth2 style authorization code
* 7 - ::admin_page_load() action=authorize
* 8 - Jetpack_Client_Server::authorize()
* 9 - Jetpack_Client_Server::get_token()
* 10- GET https://jetpack.wordpress.com/jetpack.token/1/ with
* client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login
* 11- which responds with
* access_token, token_type, scope
* 12- Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id
* 13- Jetpack::activate_default_modules()
* Deactivates deprecated plugins
* Activates all default modules
* Catches errors: redirects to wp-admin/index.php?page=jetpack state:error=something
* 14- redirect to this site's wp-admin/index.php?page=jetpack with state:message=authorized
* Done!
*/
/**
* Handles the page load events for the Jetpack admin page
*/
function admin_page_load() {
$error = false;
if ( !empty( $_GET['jetpack_restate'] ) ) {
// Should only be used in intermediate redirects to preserve state across redirects
Jetpack::restate();
}
if ( isset( $_GET['connect_url_redirect'] ) ) {
// User clicked in the iframe to link their accounts
if ( ! Jetpack::is_user_connected() ) {
$connect_url = $this->build_connect_url( true );
if ( isset( $_GET['notes_iframe'] ) )
$connect_url .= '¬es_iframe';
wp_redirect( $connect_url );
exit;
} else {
Jetpack::state( 'message', 'already_authorized' );
wp_safe_redirect( Jetpack::admin_url() );
exit;
}
}
if ( isset( $_GET['action'] ) ) {
switch ( $_GET['action'] ) {
case 'authorize' :
if ( Jetpack::is_active() && Jetpack::is_user_connected() ) {
Jetpack::state( 'message', 'already_authorized' );
wp_safe_redirect( Jetpack::admin_url() );
exit;
}
$client_server = new Jetpack_Client_Server;
$client_server->authorize();
exit;
case 'register' :
check_admin_referer( 'jetpack-register' );
$registered = Jetpack::try_registration();
if ( is_wp_error( $registered ) ) {
$error = $registered->get_error_code();
Jetpack::state( 'error_description', $registered->get_error_message() );
break;
}
wp_redirect( $this->build_connect_url( true ) );
exit;
case 'activate' :
$module = stripslashes( $_GET['module'] );
check_admin_referer( "jetpack_activate-$module" );
Jetpack::activate_module( $module );
wp_safe_redirect( Jetpack::admin_url() );
exit;
case 'activate_default_modules' :
check_admin_referer( 'activate_default_modules' );
Jetpack::restate();
$min_version = isset( $_GET['min_version'] ) ? $_GET['min_version'] : false;
$max_version = isset( $_GET['max_version'] ) ? $_GET['max_version'] : false;
$other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? $_GET['other_modules'] : array();
Jetpack::activate_default_modules( $min_version, $max_version, $other_modules );
wp_safe_redirect( Jetpack::admin_url() );
exit;
case 'disconnect' :
check_admin_referer( 'jetpack-disconnect' );
Jetpack::disconnect();
wp_safe_redirect( Jetpack::admin_url() );
exit;
case 'deactivate' :
$modules = stripslashes( $_GET['module'] );
check_admin_referer( "jetpack_deactivate-$modules" );
foreach ( explode( ',', $modules ) as $module ) {
Jetpack::deactivate_module( $module );
Jetpack::state( 'message', 'module_deactivated' );
}
Jetpack::state( 'module', $modules );
wp_safe_redirect( Jetpack::admin_url() );
exit;
case 'unlink' :
check_admin_referer( 'jetpack-unlink' );
$this->unlink_user();
Jetpack::state( 'message', 'unlinked' );
wp_safe_redirect( Jetpack::admin_url() );
exit;
}
}
if ( !$error = $error ? $error : Jetpack::state( 'error' ) ) {
$this->activate_new_modules();
}
switch ( $error ) {
case 'access_denied' :
$this->error = __( 'You need to authorize the Jetpack connection between your site and WordPress.com to enable the awesome features.', 'jetpack' );
break;
case 'wrong_state' :
$this->error = __( "Don’t cross the streams! You need to stay logged in to your WordPress blog while you authorize Jetpack.", 'jetpack' );
break;
case 'invalid_client' :
// @todo re-register instead of deactivate/reactivate
$this->error = __( 'Return to sender. Whoops! It looks like you got the wrong Jetpack in the mail; deactivate then reactivate the Jetpack plugin to get a new one.', 'jetpack' );
break;
case 'invalid_grant' :
$this->error = __( "Wrong size. Hm… it seems your Jetpack doesn’t quite fit. Have you lost weight? Click “Connect to WordPress.com” again to get your Jetpack adjusted.", 'jetpack' );
break;
case 'site_inaccessible' :
case 'site_requires_authorization' :
$this->error = sprintf( __( 'Your website needs to be publicly accessible to use Jetpack: %s', 'jetpack' ), "$error" );
break;
case 'module_activation_failed' :
$module = Jetpack::state( 'module' );
if ( !empty( $module ) && $mod = Jetpack::get_module( $module ) ) {
$this->error = sprintf( __( '%s could not be activated because it triggered a fatal error. Perhaps there is a conflict with another plugin you have installed?', 'jetpack' ), $mod['name'] );
if ( isset( $this->plugins_to_deactivate[$module] ) ) {
$this->error .= ' ' . sprintf( __( 'Do you still have the %s plugin installed?', 'jetpack' ), $this->plugins_to_deactivate[$module][1] );
}
} else {
$this->error = __( 'Module could not be activated because it triggered a fatal error. Perhaps there is a conflict with another plugin you have installed?', 'jetpack' );
}
if ( $php_errors = Jetpack::state( 'php_errors' ) ) {
$this->error .= " \n";
$this->error .= $php_errors;
}
break;
case 'not_public' :
$this->error = __( "Your Jetpack has a glitch. Connecting this site with WordPress.com is not possible. This usually means your site is not publicly accessible (localhost).", 'jetpack' );
break;
case 'wpcom_408' :
case 'wpcom_5??' :
case 'wpcom_bad_response' :
case 'wpcom_outage' :
$this->error = __( 'WordPress.com is currently having problems and is unable to fuel up your Jetpack. Please try again later.', 'jetpack' );
break;
case 'register_http_request_failed' :
case 'token_http_request_failed' :
$this->error = sprintf( __( 'Jetpack could not contact WordPress.com: %s. This usually means something is incorrectly configured on your web host.', 'jetpack' ), "$error" );
break;
default :
if ( empty( $error ) ) {
break;
}
$error = trim( substr( strip_tags( $error ), 0, 20 ) );
// no break: fall through
case 'no_role' :
case 'no_cap' :
case 'no_code' :
case 'no_state' :
case 'invalid_state' :
case 'invalid_request' :
case 'invalid_scope' :
case 'unsupported_response_type' :
case 'invalid_token' :
case 'no_token' :
case 'missing_secrets' :
case 'home_missing' :
case 'siteurl_missing' :
case 'gmt_offset_missing' :
case 'site_name_missing' :
case 'secret_1_missing' :
case 'secret_2_missing' :
case 'site_lang_missing' :
case 'home_malformed' :
case 'siteurl_malformed' :
case 'gmt_offset_malformed' :
case 'timezone_string_malformed' :
case 'site_name_malformed' :
case 'secret_1_malformed' :
case 'secret_2_malformed' :
case 'site_lang_malformed' :
case 'secrets_mismatch' :
case 'verify_secret_1_missing' :
case 'verify_secret_1_malformed' :
case 'verify_secrets_missing' :
case 'verify_secrets_mismatch' :
$error = esc_html( $error );
$this->error = sprintf( __( "Your Jetpack has a glitch. Something went wrong that’s never supposed to happen. Guess you’re just lucky: %s", 'jetpack' ), "$error" );
if ( !Jetpack::is_active() ) {
$this->error .= ' ';
$this->error .= sprintf( __( 'Try connecting again.', 'jetpack' ) );
}
break;
}
$message_code = Jetpack::state( 'message' );
$active_state = Jetpack::state( 'activated_modules' );
if ( !empty( $active_state ) ) {
$available = Jetpack::get_available_modules();
$active_state = explode( ',', $active_state );
$active_state = array_intersect( $active_state, $available );
if ( count( $active_state ) ) {
foreach ( $active_state as $mod ) {
$this->stat( 'module-activated', $mod );
}
} else {
$active_state = false;
}
}
switch ( $message_code ) {
case 'modules_activated' :
$this->message = sprintf(
__( 'Welcome to Jetpack %s!', 'jetpack' ),
JETPACK__VERSION
);
if ( $active_state ) {
$titles = array();
foreach ( $active_state as $mod ) {
if ( $mod_headers = Jetpack::get_module( $mod ) ) {
$titles[] = '' . preg_replace( '/\s+(?![^<>]++>)/', ' ', $mod_headers['name'] ) . '';
}
}
if ( $titles ) {
$this->message .= '
' . wp_sprintf( __( 'The following new modules have been activated: %l.', 'jetpack' ), $titles );
}
}
if ( $reactive_state = Jetpack::state( 'reactivated_modules' ) ) {
$titles = array();
foreach ( explode( ',', $reactive_state ) as $mod ) {
if ( $mod_headers = Jetpack::get_module( $mod ) ) {
$titles[] = '' . preg_replace( '/\s+(?![^<>]++>)/', ' ', $mod_headers['name'] ) . '';
}
}
if ( $titles ) {
$this->message .= '
' . wp_sprintf( __( 'The following modules have been updated: %l.', 'jetpack' ), $titles );
}
}
$this->message .= Jetpack::jetpack_comment_notice();
break;
case 'module_activated' :
if ( $module = Jetpack::get_module( Jetpack::state( 'module' ) ) ) {
$this->message = sprintf( __( '%s Activated! You can deactivate at any time by clicking Learn More and then Deactivate on the module card.', 'jetpack' ), $module['name'] );
$this->stat( 'module-activated', Jetpack::state( 'module' ) );
}
break;
case 'module_deactivated' :
$modules = Jetpack::state( 'module' );
if ( !$modules ) {
break;
}
$module_names = array();
foreach ( explode( ',', $modules ) as $module_slug ) {
$module = Jetpack::get_module( $module_slug );
if ( $module ) {
$module_names[] = $module['name'];
}
$this->stat( 'module-deactivated', $module_slug );
}
if ( !$module_names ) {
break;
}
$this->message = wp_sprintf(
_nx(
'%l Deactivated! You can activate it again at any time using the activate button on the module card.',
'%l Deactivated! You can activate them again at any time using the activate buttons on their module cards.',
count( $module_names ),
'%l = list of Jetpack module/feature names',
'jetpack'
),
$module_names
);
break;
case 'module_configured' :
$this->message = __( 'Module settings were saved. ', 'jetpack' );
break;
case 'already_authorized' :
$this->message = __( 'Your Jetpack is already connected. ', 'jetpack' );
break;
case 'authorized' :
$this->message = __( "You’re fueled up and ready to go. ", 'jetpack' );
$this->message .= " \n";
$this->message .= __( 'The features below are now active. Click the learn more buttons to explore each feature.', 'jetpack' );
$this->message .= Jetpack::jetpack_comment_notice();
break;
case 'linked' :
$this->message = __( "You’re fueled up and ready to go. ", 'jetpack' );
$this->message .= Jetpack::jetpack_comment_notice();
break;
case 'unlinked' :
$user = wp_get_current_user();
$this->message = sprintf( __( 'You have unlinked your account (%s) from WordPress.com.', 'jetpack' ), $user->user_login );
break;
}
$deactivated_plugins = Jetpack::state( 'deactivated_plugins' );
if ( !empty( $deactivated_plugins ) ) {
$deactivated_plugins = explode( ',', $deactivated_plugins );
$deactivated_titles = array();
foreach ( $deactivated_plugins as $deactivated_plugin ) {
if ( !isset( $this->plugins_to_deactivate[$deactivated_plugin] ) ) {
continue;
}
$deactivated_titles[] = '' . str_replace( ' ', ' ', $this->plugins_to_deactivate[$deactivated_plugin][1] ) . '';
}
if ( $deactivated_titles ) {
if ( $this->message ) {
$this->message .= "
\n";
}
$this->message .= wp_sprintf( _n(
'Jetpack contains the most recent version of the old %l plugin.',
'Jetpack contains the most recent versions of the old %l plugins.',
count( $deactivated_titles ),
'jetpack'
), $deactivated_titles );
$this->message .= " \n";
$this->message .= _n(
'The old version has been deactivated and can be removed from your site.',
'The old versions have been deactivated and can be removed from your site.',
count( $deactivated_titles ),
'jetpack'
);
}
}
$this->privacy_checks = Jetpack::state( 'privacy_checks' );
if ( $this->message || $this->error || $this->privacy_checks ) {
add_action( 'jetpack_notices', array( $this, 'admin_notices' ) );
}
if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
do_action( 'jetpack_module_configuration_load_' . $_GET['configure'] );
}
add_filter( 'jetpack_short_module_description', 'wptexturize' );
}
function admin_notices() {
if ( $this->error ) {
?>