/** * Add theme support for Infinite Scroll. * See: https://jetpack.me/support/infinite-scroll/ */ function ocin_lite_jetpack_setup() { add_theme_support('infinite-scroll', array('container' => 'main', 'render' => 'ocin_lite_infinite_scroll_render', 'footer' => 'page')); //Enable Custom CSS if (class_exists('Jetpack')) { Jetpack::activate_module('custom-css', false, false); } }
function activate_manage($request) { foreach (array('secret', 'state') as $required) { if (!isset($request[$required]) || empty($request[$required])) { return $this->error(new Jetpack_Error('missing_parameter', 'One or more parameters is missing from the request.', 400)); } } $verified = $this->verify_action(array('activate_manage', $request['secret'], $request['state'])); if (is_a($verified, 'IXR_Error')) { return $verified; } $activated = Jetpack::activate_module('manage', false, false); if (false === $activated || !Jetpack::is_module_active('manage')) { return $this->error(new Jetpack_Error('activation_error', 'There was an error while activating the module.', 500)); } return 'active'; }
protected function activate_module() { foreach ($this->modules as $module) { if (Jetpack::is_module_active($module)) { $error = $this->log[$module][] = __('The Jetpack Module is already activated.', 'jetpack'); continue; } $result = Jetpack::activate_module($module, false, false); if (false === $result || !Jetpack::is_module_active($module)) { $error = $this->log[$module][] = __('There was an error while activating the module.', 'jetpack'); } } if (!$this->bulk && isset($error)) { return new WP_Error('activation_error', $error, 400); } return true; }
/** * Manage Jetpack Modules * * ## OPTIONS * * list: View all available modules, and their status. * * activate <module_slug>: Activate a module. * * deactivate <module_slug>: Deactivate a module. * * toggle <module_slug>: Toggle a module on or off. * * ## EXAMPLES * * wp jetpack module list * wp jetpack module activate stats * wp jetpack module deactivate stats * wp jetpack module toggle stats * * @synopsis [list|activate|deactivate|toggle [<module_name>]] */ public function module($args, $assoc_args) { $action = isset($args[0]) ? $args[0] : 'list'; if (!in_array($action, array('list', 'activate', 'deactivate', 'toggle'))) { WP_CLI::error(sprintf(__('%s is not a valid command.', 'jetpack'), $action)); } if (in_array($action, array('activate', 'deactivate', 'toggle'))) { if (isset($args[1])) { $module_slug = $args[1]; if (!Jetpack::is_module($module_slug)) { WP_CLI::error(sprintf(__('%s is not a valid module.', 'jetpack'), $module_slug)); } if ('toggle' == $action) { $action = Jetpack::is_module_active($module_slug) ? 'deactivate' : 'activate'; } } else { WP_CLI::line(__('Please specify a valid module.', 'jetpack')); $action = 'list'; } } switch ($action) { case 'list': WP_CLI::line(__('Available Modules:', 'jetpack')); $modules = Jetpack::get_available_modules(); sort($modules); foreach ($modules as $module_slug) { $active = Jetpack::is_module_active($module_slug) ? __('Active', 'jetpack') : __('Inactive', 'jetpack'); WP_CLI::line("\t" . str_pad($module_slug, 24) . $active); } break; case 'activate': $module = Jetpack::get_module($module_slug); Jetpack::log('activate', $module_slug); Jetpack::activate_module($module_slug, false); WP_CLI::success(sprintf(__('%s has been activated.', 'jetpack'), $module['name'])); break; case 'deactivate': $module = Jetpack::get_module($module_slug); Jetpack::log('deactivate', $module_slug); Jetpack::deactivate_module($module_slug); WP_CLI::success(sprintf(__('%s has been deactivated.', 'jetpack'), $module['name'])); break; case 'toggle': // Will never happen, should have been handled above and changed to activate or deactivate. break; } }
function dismiss_jetpack_notice() { if (!isset($_GET['jetpack-notice'])) { return; } switch ($_GET['jetpack-notice']) { case 'dismiss': if (check_admin_referer('jetpack-deactivate') && !is_plugin_active_for_network(plugin_basename(JETPACK__PLUGIN_DIR . 'jetpack.php'))) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; deactivate_plugins(JETPACK__PLUGIN_DIR . 'jetpack.php', false, false); wp_safe_redirect(admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s='); } break; case 'jetpack-manage-opt-out': if (check_admin_referer('jetpack_manage_banner_opt_out')) { // Don't show the banner again Jetpack_Options::update_option('dismissed_manage_banner', true); // redirect back to the page that had the notice if (wp_get_referer()) { wp_safe_redirect(wp_get_referer()); } else { // Take me to Jetpack wp_safe_redirect(admin_url('admin.php?page=jetpack')); } } break; case 'jetpack-protect-multisite-opt-out': if (check_admin_referer('jetpack_protect_multisite_banner_opt_out')) { // Don't show the banner again update_site_option('jetpack_dismissed_protect_multisite_banner', true); // redirect back to the page that had the notice if (wp_get_referer()) { wp_safe_redirect(wp_get_referer()); } else { // Take me to Jetpack wp_safe_redirect(admin_url('admin.php?page=jetpack')); } } break; case 'jetpack-manage-opt-in': if (check_admin_referer('jetpack_manage_banner_opt_in')) { // This makes sure that we are redirect to jetpack home so that we can see the Success Message. $redirection_url = Jetpack::admin_url(); remove_action('jetpack_pre_activate_module', array(Jetpack_Admin::init(), 'fix_redirect')); // Don't redirect form the Jetpack Setting Page $referer_parsed = parse_url(wp_get_referer()); // check that we do have a wp_get_referer and the query paramater is set orderwise go to the Jetpack Home if (isset($referer_parsed['query']) && false !== strpos($referer_parsed['query'], 'page=jetpack_modules')) { // Take the user to Jetpack home except when on the setting page $redirection_url = wp_get_referer(); add_action('jetpack_pre_activate_module', array(Jetpack_Admin::init(), 'fix_redirect')); } // Also update the JSON API FULL MANAGEMENT Option Jetpack::activate_module('manage', false, false); // Special Message when option in. Jetpack::state('optin-manage', 'true'); // Activate the Module if not activated already // Redirect properly wp_safe_redirect($redirection_url); } break; } }
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['action'])) { switch ($_GET['action']) { case 'authorize': if (Jetpack::is_active()) { 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'); $this->disconnect(); wp_safe_redirect(Jetpack::admin_url()); exit; case 'deactivate': $module = stripslashes($_GET['module']); check_admin_referer("jetpack_deactivate-{$module}"); Jetpack::deactivate_module($module); Jetpack::state('message', 'module_deactivated'); Jetpack::state('module', $module); wp_safe_redirect(Jetpack::admin_url()); exit; } } if (!($error = $error ? $error : Jetpack::state('error'))) { Jetpack::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'), "<code>{$error}</code>"); break; case 'module_activation_failed': $module = Jetpack::state('module'); if (!empty($module) && ($mod = Jetpack::get_module($module))) { if ('sharedaddy' == $module && version_compare(PHP_VERSION, '5', '<')) { $this->error = sprintf(__('The %1$s module requires <strong>PHP version %2$s</strong> or higher.', 'jetpack'), '<strong>' . $mod['name'] . '</strong>', '5'); } else { $this->error = sprintf(__('%s could not be activated because it triggered a <strong>fatal error</strong>. 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 <strong>fatal error</strong>. Perhaps there is a conflict with another plugin you have installed?', 'jetpack'); } if ($php_errors = Jetpack::state('php_errors')) { $this->error .= "<br />\n"; $this->error .= $php_errors; } break; case 'not_public': $this->error = __("<strong>Your Jetpack has a glitch.</strong> 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'), "<code>{$error}</code>"); break; default: if (empty($error)) { break; } $error = trim(substr(strip_tags($error), 0, 20)); // no break: fall through // 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(__("<strong>Your Jetpack has a glitch.</strong> Something went wrong that’s never supposed to happen. Guess you’re just lucky: %s", 'jetpack'), "<code>{$error}</code>"); if (!Jetpack::is_active()) { $this->error .= '<br />'; $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 <strong>Jetpack %s</strong>!', 'jetpack'), JETPACK__VERSION); if ($active_state) { $titles = array(); foreach ($active_state as $mod) { if ($mod_headers = Jetpack::get_module($mod)) { $titles[] = '<strong>' . preg_replace('/\\s+(?![^<>]++>)/', ' ', $mod_headers['name']) . '</strong>'; } } if ($titles) { $this->message .= '<br /><br />' . 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[] = '<strong>' . preg_replace('/\\s+(?![^<>]++>)/', ' ', $mod_headers['name']) . '</strong>'; } } if ($titles) { $this->message .= '<br /><br />' . wp_sprintf(__('The following modules have been updated: %l.', 'jetpack'), $titles); } } break; case 'module_activated': if ($module = Jetpack::get_module(Jetpack::state('module'))) { $this->message = sprintf(__('<strong>%s Activated!</strong> 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': if ($module = Jetpack::get_module(Jetpack::state('module'))) { $this->message = sprintf(__('<strong>%s Deactivated!</strong> You can activate it again at any time using the activate button on the module card.', 'jetpack'), $module['name']); $this->stat('module-deactivated', Jetpack::state('module')); } break; case 'module_configured': $this->message = __('<strong>Module settings were saved.</strong> ', 'jetpack'); break; case 'already_authorized': $this->message = __('<strong>Your Jetpack is already connected.</strong> ', 'jetpack'); break; case 'authorized': $this->message = __("<strong>You’re fueled up and ready to go.</strong> ", 'jetpack'); $this->message .= "<br />\n"; $this->message .= __('The features below are now active. Click the learn more buttons to explore each feature.', 'jetpack'); 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[] = '<strong>' . str_replace(' ', ' ', $this->plugins_to_deactivate[$deactivated_plugin][1]) . '</strong>'; } if ($deactivated_titles) { if ($this->message) { $this->message .= "<br /><br />\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 .= "<br />\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'); } } if ($this->message || $this->error) { add_action('jetpack_notices', array(&$this, 'admin_notices')); } if (isset($_GET['configure']) && Jetpack::is_module($_GET['configure'])) { do_action('jetpack_module_configuration_load_' . $_GET['configure']); } add_filter('jetpack_short_module_description', 'wptexturize'); }
/** * Updates site settings for authorized users * * @return (array) */ public function update_settings() { // $this->input() retrieves posted arguments whitelisted and casted to the $request_format // specs that get passed in when this class is instantiated /** * Filters the settings to be updated on the site. * * @since 3.6.0 * * @param array $input Associative array of site settings to be updated. */ $input = apply_filters('rest_api_update_site_settings', $this->input()); $jetpack_relatedposts_options = array(); $sharing_options = array(); $updated = array(); foreach ($input as $key => $value) { if (!is_array($value)) { $value = trim($value); } $value = wp_unslash($value); switch ($key) { case 'default_ping_status': case 'default_comment_status': // settings are stored as closed|open $coerce_value = $value ? 'open' : 'closed'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'jetpack_protect_whitelist': if (function_exists('jetpack_protect_save_whitelist')) { $result = jetpack_protect_save_whitelist($value); if (is_wp_error($result)) { return $result; } $updated[$key] = jetpack_protect_format_whitelist(); } break; case 'jetpack_sync_non_public_post_stati': Jetpack_Options::update_option('sync_non_public_post_stati', $value); break; case 'jetpack_relatedposts_enabled': case 'jetpack_relatedposts_show_thumbnails': case 'jetpack_relatedposts_show_headline': if (!$this->jetpack_relatedposts_supported()) { break; } if ('jetpack_relatedposts_enabled' === $key && method_exists('Jetpack', 'is_module_active') && $this->jetpack_relatedposts_supported()) { $before_action = Jetpack::is_module_active('related-posts'); if ($value) { Jetpack::activate_module('related-posts', false, false); } else { Jetpack::deactivate_module('related-posts'); } $after_action = Jetpack::is_module_active('related-posts'); if ($after_action == $before_action) { break; } } $just_the_key = substr($key, 21); $jetpack_relatedposts_options[$just_the_key] = $value; break; case 'social_notifications_like': case 'social_notifications_reblog': case 'social_notifications_subscribe': // settings are stored as on|off $coerce_value = $value ? 'on' : 'off'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'wga': if (!isset($value['code']) || !preg_match('/^$|^UA-[\\d-]+$/i', $value['code'])) { return new WP_Error('invalid_code', 'Invalid UA ID'); } $wga = get_option('wga', array()); $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics if (update_option('wga', $wga)) { $updated[$key] = $value; } $enabled_or_disabled = $wga['code'] ? 'enabled' : 'disabled'; do_action('jetpack_bump_stats_extras', 'google-analytics', $enabled_or_disabled); $business_plugins = WPCOM_Business_Plugins::instance(); $business_plugins->activate_plugin('wp-google-analytics'); break; case 'jetpack_comment_likes_enabled': // settings are stored as 1|0 $coerce_value = (int) $value; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; // Sharing options // Sharing options case 'sharing_button_style': case 'sharing_show': case 'sharing_open_links': $sharing_options[preg_replace('/^sharing_/', '', $key)] = $value; break; case 'sharing_label': $sharing_options[$key] = $value; break; // Keyring token option // Keyring token option case 'eventbrite_api_token': // These options can only be updated for sites hosted on WordPress.com if (defined('IS_WPCOM') && IS_WPCOM) { if (empty($value) || WPCOM_JSON_API::is_falsy($value)) { if (delete_option($key)) { $updated[$key] = null; } } else { if (update_option($key, $value)) { $updated[$key] = (int) $value; } } } break; // no worries, we've already whitelisted and casted arguments above // no worries, we've already whitelisted and casted arguments above default: if (update_option($key, $value)) { $updated[$key] = $value; } } } if (count($jetpack_relatedposts_options)) { // track new jetpack_relatedposts options against old $old_relatedposts_options = Jetpack_Options::get_option('relatedposts'); if (Jetpack_Options::update_option('relatedposts', $jetpack_relatedposts_options)) { foreach ($jetpack_relatedposts_options as $key => $value) { if ($value !== $old_relatedposts_options[$key]) { $updated['jetpack_relatedposts_' . $key] = $value; } } } } if (!empty($sharing_options) && class_exists('Sharing_Service')) { $ss = new Sharing_Service(); // Merge current values with updated, since Sharing_Service expects // all values to be included when updating $current_sharing_options = $ss->get_global_options(); foreach ($current_sharing_options as $key => $val) { if (!isset($sharing_options[$key])) { $sharing_options[$key] = $val; } } $updated_social_options = $ss->set_global_options($sharing_options); if (isset($input['sharing_button_style'])) { $updated['sharing_button_style'] = (string) $updated_social_options['button_style']; } if (isset($input['sharing_label'])) { // Sharing_Service won't report label as updated if set to default $updated['sharing_label'] = (string) $sharing_options['sharing_label']; } if (isset($input['sharing_show'])) { $updated['sharing_show'] = (array) $updated_social_options['show']; } if (isset($input['sharing_open_links'])) { $updated['sharing_open_links'] = (string) $updated_social_options['open_links']; } } return array('updated' => $updated); }
static function activate_jetpack_modules() { check_ajax_referer(self::AJAX_NONCE, 'nonce'); // shamelessly copied from class.jetpack.php $modules = $_REQUEST['modules']; $modules = array_map('sanitize_key', $modules); // $modules_filtered = Jetpack::init()->filter_default_modules( $modules ); foreach ($modules as $module_slug) { Jetpack::log('activate', $module_slug); Jetpack::activate_module($module_slug, false, false); Jetpack::state('message', 'no_message'); } //XXX TODO: determine whether this is really useful // self::set_default_publicize_config(); wp_send_json_success($modules); }
function handle_unrecognized_action($action) { switch ($action) { case 'bulk-activate': if (!current_user_can('jetpack_activate_modules')) { break; } $modules = (array) $_GET['modules']; $modules = array_map('sanitize_key', $modules); check_admin_referer('bulk-jetpack_page_jetpack_modules'); foreach ($modules as $module) { Jetpack::log('activate', $module); Jetpack::activate_module($module, false); } // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. wp_safe_redirect(wp_get_referer()); exit; case 'bulk-deactivate': if (!current_user_can('jetpack_deactivate_modules')) { break; } $modules = (array) $_GET['modules']; $modules = array_map('sanitize_key', $modules); check_admin_referer('bulk-jetpack_page_jetpack_modules'); foreach ($modules as $module) { Jetpack::log('deactivate', $module); Jetpack::deactivate_module($module); Jetpack::state('message', 'module_deactivated'); } Jetpack::state('module', $modules); wp_safe_redirect(wp_get_referer()); exit; default: return; } }
/** * Activate a list of valid Jetpack modules. * * @since 4.3.0 * * @param WP_REST_Request $data { * Array of parameters received by request. * * @type string $slug Module slug. * } * * @return bool|WP_Error True if modules were activated. Otherwise, a WP_Error instance with the corresponding error. */ public static function activate_modules($data) { $params = $data->get_json_params(); if (!isset($params['modules']) || is_array($params['modules'])) { return new WP_Error('not_found', esc_html__('The requested Jetpack module was not found.', 'jetpack'), array('status' => 404)); } $activated = array(); $failed = array(); foreach ($params['modules'] as $module) { if (Jetpack::activate_module($module, false, false)) { $activated[] = $module; } else { $failed[] = $module; } } if (empty($failed)) { return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('All modules activated.', 'jetpack'))); } $error = ''; $activated_count = count($activated); if ($activated_count > 0) { $activated_last = array_pop($activated); $activated_text = $activated_count > 1 ? sprintf(__('%1$s and %2$s', 'jetpack'), join(', ', $activated), $activated_last) : $activated_last; $error = sprintf(_n('The module %s was activated.', 'The modules %s were activated.', $activated_count, 'jetpack'), $activated_text) . ' '; } $failed_count = count($failed); if (count($failed) > 0) { $failed_last = array_pop($failed); $failed_text = $failed_count > 1 ? sprintf(__('%1$s and %2$s', 'jetpack'), join(', ', $failed), $failed_last) : $failed_last; $error = sprintf(_n('The module %s failed to be activated.', 'The modules %s failed to be activated.', $failed_count, 'jetpack'), $failed_text) . ' '; } return new WP_Error('activation_failed', esc_html($error), array('status' => 424)); }
/** * Updates site settings for authorized users * * @return (array) */ public function update_settings() { // $this->input() retrieves posted arguments whitelisted and casted to the $request_format // specs that get passed in when this class is instantiated $input = $this->input(); $jetpack_relatedposts_options = array(); $sharing_options = array(); $updated = array(); foreach ($input as $key => $value) { if (!is_array($value)) { $value = trim($value); } $value = wp_unslash($value); switch ($key) { case 'default_ping_status': case 'default_comment_status': // settings are stored as closed|open $coerce_value = $value ? 'open' : 'closed'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'jetpack_sync_non_public_post_stati': Jetpack_Options::update_option('sync_non_public_post_stati', $value); break; case 'jetpack_relatedposts_enabled': case 'jetpack_relatedposts_show_thumbnails': case 'jetpack_relatedposts_show_headline': if (!$this->jetpack_relatedposts_supported()) { break; } if ('jetpack_relatedposts_enabled' === $key && method_exists('Jetpack', 'is_module_active') && $this->jetpack_relatedposts_supported()) { $before_action = Jetpack::is_module_active('related-posts'); if ($value) { Jetpack::activate_module('related-posts', false, false); } else { Jetpack::deactivate_module('related-posts'); } $after_action = Jetpack::is_module_active('related-posts'); if ($after_action == $before_action) { break; } } $just_the_key = substr($key, 21); $jetpack_relatedposts_options[$just_the_key] = $value; break; case 'social_notifications_like': case 'social_notifications_reblog': case 'social_notifications_subscribe': // settings are stored as on|off $coerce_value = $value ? 'on' : 'off'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'wga': if (!isset($value['code']) || !preg_match('/^UA-[\\d-]+$/', $value['code'])) { return new WP_Error('invalid_code', 'Invalid UA ID'); } $wga = get_option('wga', array()); $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics if (update_option('wga', $wga)) { $updated[$key] = $value; } break; case 'jetpack_comment_likes_enabled': // settings are stored as 1|0 $coerce_value = (int) $value; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; // Sharing options // Sharing options case 'sharing_button_style': case 'sharing_show': case 'sharing_open_links': $sharing_options[preg_replace('/^sharing_/', '', $key)] = $value; break; case 'sharing_label': $sharing_options[$key] = $value; break; // no worries, we've already whitelisted and casted arguments above // no worries, we've already whitelisted and casted arguments above default: if (update_option($key, $value)) { $updated[$key] = $value; } } } if (count($jetpack_relatedposts_options)) { // track new jetpack_relatedposts options against old $old_relatedposts_options = Jetpack_Options::get_option('relatedposts'); if (Jetpack_Options::update_option('relatedposts', $jetpack_relatedposts_options)) { foreach ($jetpack_relatedposts_options as $key => $value) { if ($value !== $old_relatedposts_options[$key]) { $updated['jetpack_relatedposts_' . $key] = $value; } } } } if (!empty($sharing_options) && class_exists('Sharing_Service')) { $ss = new Sharing_Service(); // Merge current values with updated, since Sharing_Service expects // all values to be included when updating $current_sharing_options = $ss->get_global_options(); foreach ($current_sharing_options as $key => $val) { if (!isset($sharing_options[$key])) { $sharing_options[$key] = $val; } } $updated_social_options = $ss->set_global_options($sharing_options); if (isset($input['sharing_button_style'])) { $updated['sharing_button_style'] = (string) $updated_social_options['button_style']; } if (isset($input['sharing_label'])) { // Sharing_Service won't report label as updated if set to default $updated['sharing_label'] = (string) $sharing_options['sharing_label']; } if (isset($input['sharing_show'])) { $updated['sharing_show'] = (array) $updated_social_options['show']; } if (isset($input['sharing_open_links'])) { $updated['sharing_open_links'] = (string) $updated_social_options['open_links']; } } return array('updated' => $updated); }
/** * Updates site settings for authorized users * * @return (array) */ public function update_settings() { // $this->input() retrieves posted arguments whitelisted and casted to the $request_format // specs that get passed in when this class is instantiated $input = $this->input(); $jetpack_relatedposts_options = array(); $updated = array(); foreach ($input as $key => $value) { $value = wp_unslash(trim($value)); switch ($key) { case 'default_ping_status': case 'default_comment_status': // settings are stored as closed|open $coerce_value = $value ? 'open' : 'closed'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'infinite_scroll': if (!current_theme_supports('infinite-scroll')) { continue; } if (update_option($key, $value)) { $updated[$key] = $value; } break; case 'jetpack_relatedposts_enabled': if (method_exists('Jetpack', 'is_module_active') && $this->jetpack_relatedposts_supported()) { if ($value) { Jetpack::activate_module('related-posts', false, false); } else { Jetpack::deactivate_module('related-posts'); } $updated[$key] = $value; unset($jetpack_relatedposts_options['enabled']); break; } case 'jetpack_relatedposts_show_thumbnails': case 'jetpack_relatedposts_show_headline': if (!$this->jetpack_relatedposts_supported()) { break; } $jetpack_relatedposts_options = Jetpack_Options::get_option('relatedposts'); $just_the_key = substr($key, 21); $jetpack_relatedposts_options[$just_the_key] = $value; if (Jetpack_Options::update_option('relatedposts', $jetpack_relatedposts_options)) { $updated[$key] = $value; } break; case 'social_notifications_like': case 'social_notifications_reblog': case 'social_notifications_subscribe': // settings are stored as on|off $coerce_value = $value ? 'on' : 'off'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; // no worries, we've already whitelisted and casted arguments above // no worries, we've already whitelisted and casted arguments above default: if (update_option($key, $value)) { $updated[$key] = $value; } } } return array('updated' => $updated); }
/** * Updates site settings for authorized users * * @return (array) */ public function update_settings() { // $this->input() retrieves posted arguments whitelisted and casted to the $request_format // specs that get passed in when this class is instantiated /** * Filters the settings to be updated on the site. * * @module json-api * * @since 3.6.0 * * @param array $input Associative array of site settings to be updated. */ $input = apply_filters('rest_api_update_site_settings', $this->input()); $jetpack_relatedposts_options = array(); $sharing_options = array(); $updated = array(); foreach ($input as $key => $value) { if (!is_array($value)) { $value = trim($value); } $value = wp_unslash($value); switch ($key) { case 'default_ping_status': case 'default_comment_status': // settings are stored as closed|open $coerce_value = $value ? 'open' : 'closed'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'jetpack_protect_whitelist': if (function_exists('jetpack_protect_save_whitelist')) { $result = jetpack_protect_save_whitelist($value); if (is_wp_error($result)) { return $result; } $updated[$key] = jetpack_protect_format_whitelist(); } break; case 'jetpack_sync_non_public_post_stati': Jetpack_Options::update_option('sync_non_public_post_stati', $value); break; case 'jetpack_relatedposts_enabled': case 'jetpack_relatedposts_show_thumbnails': case 'jetpack_relatedposts_show_headline': if (!$this->jetpack_relatedposts_supported()) { break; } if ('jetpack_relatedposts_enabled' === $key && method_exists('Jetpack', 'is_module_active') && $this->jetpack_relatedposts_supported()) { $before_action = Jetpack::is_module_active('related-posts'); if ($value) { Jetpack::activate_module('related-posts', false, false); } else { Jetpack::deactivate_module('related-posts'); } $after_action = Jetpack::is_module_active('related-posts'); if ($after_action == $before_action) { break; } } $just_the_key = substr($key, 21); $jetpack_relatedposts_options[$just_the_key] = $value; break; case 'social_notifications_like': case 'social_notifications_reblog': case 'social_notifications_subscribe': // settings are stored as on|off $coerce_value = $value ? 'on' : 'off'; if (update_option($key, $coerce_value)) { $updated[$key] = $value; } break; case 'wga': if (!isset($value['code']) || !preg_match('/^$|^UA-[\\d-]+$/i', $value['code'])) { return new WP_Error('invalid_code', 'Invalid UA ID'); } $wga = get_option('wga', array()); $wga['code'] = $value['code']; // maintain compatibility with wp-google-analytics if (update_option('wga', $wga)) { $updated[$key] = $value; } $enabled_or_disabled = $wga['code'] ? 'enabled' : 'disabled'; /** This action is documented in modules/widgets/social-media-icons.php */ do_action('jetpack_bump_stats_extras', 'google-analytics', $enabled_or_disabled); $business_plugins = WPCOM_Business_Plugins::instance(); $business_plugins->activate_plugin('wp-google-analytics'); break; case 'jetpack_testimonial': case 'jetpack_portfolio': case 'jetpack_comment_likes_enabled': // settings are stored as 1|0 $coerce_value = (int) $value; if (update_option($key, $coerce_value)) { $updated[$key] = (bool) $value; } break; case 'jetpack_testimonial_posts_per_page': case 'jetpack_portfolio_posts_per_page': // settings are stored as numeric $coerce_value = (int) $value; if (update_option($key, $coerce_value)) { $updated[$key] = $coerce_value; } break; // Sharing options // Sharing options case 'sharing_button_style': case 'sharing_show': case 'sharing_open_links': $sharing_options[preg_replace('/^sharing_/', '', $key)] = $value; break; case 'sharing_label': $sharing_options[$key] = $value; break; // Keyring token option // Keyring token option case 'eventbrite_api_token': // These options can only be updated for sites hosted on WordPress.com if (defined('IS_WPCOM') && IS_WPCOM) { if (empty($value) || WPCOM_JSON_API::is_falsy($value)) { if (delete_option($key)) { $updated[$key] = null; } } else { if (update_option($key, $value)) { $updated[$key] = (int) $value; } } } break; case 'holidaysnow': if (empty($value) || WPCOM_JSON_API::is_falsy($value)) { if (function_exists('jetpack_holiday_snow_option_name') && delete_option(jetpack_holiday_snow_option_name())) { $updated[$key] = false; } } else { if (function_exists('jetpack_holiday_snow_option_name') && update_option(jetpack_holiday_snow_option_name(), 'letitsnow')) { $updated[$key] = true; } } break; case 'timezone_string': // Map UTC+- timezones to gmt_offsets and set timezone_string to empty // https://github.com/WordPress/WordPress/blob/4.4.2/wp-admin/options.php#L175 if (!empty($value) && preg_match('/^UTC[+-]/', $value)) { $gmt_offset = preg_replace('/UTC\\+?/', '', $value); if (update_option('gmt_offset', $gmt_offset)) { $updated['gmt_offset'] = $gmt_offset; } $value = ''; } // Always set timezone_string either with the given value or with an // empty string if (update_option($key, $value)) { $updated[$key] = $value; } break; default: //allow future versions of this endpoint to support additional settings keys if (has_filter('site_settings_endpoint_update_' . $key)) { /** * Filter current site setting value to be updated. * * @module json-api * * @since 3.9.3 * * @param mixed $response_item A single site setting value. */ $value = apply_filters('site_settings_endpoint_update_' . $key, $value); $updated[$key] = $value; continue; } // no worries, we've already whitelisted and casted arguments above if (update_option($key, $value)) { $updated[$key] = $value; } } } if (count($jetpack_relatedposts_options)) { // track new jetpack_relatedposts options against old $old_relatedposts_options = Jetpack_Options::get_option('relatedposts'); if (Jetpack_Options::update_option('relatedposts', $jetpack_relatedposts_options)) { foreach ($jetpack_relatedposts_options as $key => $value) { if ($value !== $old_relatedposts_options[$key]) { $updated['jetpack_relatedposts_' . $key] = $value; } } } } if (!empty($sharing_options) && class_exists('Sharing_Service')) { $ss = new Sharing_Service(); // Merge current values with updated, since Sharing_Service expects // all values to be included when updating $current_sharing_options = $ss->get_global_options(); foreach ($current_sharing_options as $key => $val) { if (!isset($sharing_options[$key])) { $sharing_options[$key] = $val; } } $updated_social_options = $ss->set_global_options($sharing_options); if (isset($input['sharing_button_style'])) { $updated['sharing_button_style'] = (string) $updated_social_options['button_style']; } if (isset($input['sharing_label'])) { // Sharing_Service won't report label as updated if set to default $updated['sharing_label'] = (string) $sharing_options['sharing_label']; } if (isset($input['sharing_show'])) { $updated['sharing_show'] = (array) $updated_social_options['show']; } if (isset($input['sharing_open_links'])) { $updated['sharing_open_links'] = (string) $updated_social_options['open_links']; } } return array('updated' => $updated); }
/** * Manage Jetpack Modules * * ## OPTIONS * * list : View all available modules, and their status. * activate all : Activate all modules * deactivate all: Deactivate all modules * * activate <module_slug> : Activate a module. * deactivate <module_slug> : Deactivate a module. * toggle <module_slug> : Toggle a module on or off. * * ## EXAMPLES * * wp jetpack module list * wp jetpack module activate stats * wp jetpack module deactivate stats * wp jetpack module toggle stats * * wp jetpack module activate all * wp jetpack module deactivate all * * @synopsis <list|activate|deactivate|toggle> [<module_name>] */ public function module($args, $assoc_args) { $action = isset($args[0]) ? $args[0] : 'list'; if (!in_array($action, array('list', 'activate', 'deactivate', 'toggle'))) { WP_CLI::error(sprintf(__('%s is not a valid command.', 'jetpack'), $action)); } if (in_array($action, array('activate', 'deactivate', 'toggle'))) { if (isset($args[1])) { $module_slug = $args[1]; if ('all' !== $module_slug && !Jetpack::is_module($module_slug)) { WP_CLI::error(sprintf(__('%s is not a valid module.', 'jetpack'), $module_slug)); } if ('toggle' == $action) { $action = Jetpack::is_module_active($module_slug) ? 'deactivate' : 'activate'; } // Bulk actions if ('all' == $args[1]) { $action = 'deactivate' == $action ? 'deactivate_all' : 'activate_all'; } // VaultPress needs to be handled elsewhere. if (in_array($action, array('activate', 'deactivate', 'toggle')) && 'vaultpress' == $args[1]) { WP_CLI::error(sprintf(_x('Please visit %s to configure your VaultPress subscription.', '%s is a website', 'jetpack'), esc_url('https://vaultpress.com/jetpack/'))); } } else { WP_CLI::line(__('Please specify a valid module.', 'jetpack')); $action = 'list'; } } switch ($action) { case 'list': WP_CLI::line(__('Available Modules:', 'jetpack')); $modules = Jetpack::get_available_modules(); sort($modules); foreach ($modules as $module_slug) { if ('vaultpress' == $module_slug) { continue; } $active = Jetpack::is_module_active($module_slug) ? __('Active', 'jetpack') : __('Inactive', 'jetpack'); WP_CLI::line("\t" . str_pad($module_slug, 24) . $active); } break; case 'activate': $module = Jetpack::get_module($module_slug); Jetpack::log('activate', $module_slug); Jetpack::activate_module($module_slug, false, false); WP_CLI::success(sprintf(__('%s has been activated.', 'jetpack'), $module['name'])); break; case 'activate_all': $modules = Jetpack::get_available_modules(); Jetpack::update_active_modules($modules); WP_CLI::success(__('All modules activated!', 'jetpack')); break; case 'deactivate': $module = Jetpack::get_module($module_slug); Jetpack::log('deactivate', $module_slug); Jetpack::deactivate_module($module_slug); WP_CLI::success(sprintf(__('%s has been deactivated.', 'jetpack'), $module['name'])); break; case 'deactivate_all': Jetpack::delete_active_modules(); WP_CLI::success(__('All modules deactivated!', 'jetpack')); break; case 'toggle': // Will never happen, should have been handled above and changed to activate or deactivate. break; } }
/** * Activates a series of valid Jetpack modules and initializes some options. * * @since 4.3.0 * * @param WP_REST_Request $data { * Array of parameters received by request. * } * * @return bool|WP_Error True if Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error. */ public static function jumpstart_activate($data) { $modules = Jetpack::get_available_modules(); $activate_modules = array(); foreach ($modules as $module) { $module_info = Jetpack::get_module($module); if (isset($module_info['feature']) && is_array($module_info['feature']) && in_array('Jumpstart', $module_info['feature'])) { $activate_modules[] = $module; } } // Collect success/error messages like modules that are properly activated. $result = array('activated_modules' => array(), 'failed_modules' => array()); // Update the jumpstart option if ('new_connection' === Jetpack_Options::get_option('jumpstart')) { $result['jumpstart_activated'] = Jetpack_Options::update_option('jumpstart', 'jumpstart_activated'); } // Check for possible conflicting plugins $module_slugs_filtered = Jetpack::init()->filter_default_modules($activate_modules); foreach ($module_slugs_filtered as $module_slug) { Jetpack::log('activate', $module_slug); if (Jetpack::activate_module($module_slug, false, false)) { $result['activated_modules'][] = $module_slug; } else { $result['failed_modules'][] = $module_slug; } } // Set the default sharing buttons and set to display on posts if none have been set. $sharing_services = get_option('sharing-services'); $sharing_options = get_option('sharing-options'); if (empty($sharing_services['visible'])) { // Default buttons to set $visible = array('twitter', 'facebook', 'google-plus-1'); $hidden = array(); // Set some sharing settings $sharing = new Sharing_Service(); $sharing_options['global'] = array('button_style' => 'icon', 'sharing_label' => $sharing->default_sharing_label, 'open_links' => 'same', 'show' => array('post'), 'custom' => isset($sharing_options['global']['custom']) ? $sharing_options['global']['custom'] : array()); $result['sharing_options'] = update_option('sharing-options', $sharing_options); $result['sharing_services'] = update_option('sharing-services', array('visible' => $visible, 'hidden' => $hidden)); } // If all Jumpstart modules were activated if (empty($result['failed_modules'])) { return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('Jumpstart done.', 'jetpack'), 'data' => $result)); } return new WP_Error('jumpstart_failed', esc_html(sprintf(_n('Jumpstart failed activating this module: %s.', 'Jumpstart failed activating these modules: %s.', count($result['failed_modules']), 'jetpack'), join(', ', $result['failed_modules']))), array('status' => 400)); }
/** * Handles the page load events for the Jetpack admin page */ function admin_page_load() { $error = false; // Make sure we have the right body class to hook stylings for subpages off of. add_filter('admin_body_class', array(__CLASS__, 'add_jetpack_pagestyles')); 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; } Jetpack::log('authorize'); $client_server = new Jetpack_Client_Server(); $client_server->authorize(); exit; case 'register': check_admin_referer('jetpack-register'); Jetpack::log('register'); Jetpack::maybe_set_version_option(); $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': if (!current_user_can('jetpack_activate_modules')) { $error = 'cheatin'; break; } $module = stripslashes($_GET['module']); check_admin_referer("jetpack_activate-{$module}"); Jetpack::log('activate', $module); Jetpack::activate_module($module); // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. wp_safe_redirect(Jetpack::admin_url('page=jetpack')); exit; case 'activate_default_modules': check_admin_referer('activate_default_modules'); Jetpack::log('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('page=jetpack')); exit; case 'disconnect': if (!current_user_can('jetpack_disconnect')) { $error = 'cheatin'; break; } check_admin_referer('jetpack-disconnect'); Jetpack::log('disconnect'); Jetpack::disconnect(); wp_safe_redirect(Jetpack::admin_url()); exit; case 'reconnect': if (!current_user_can('jetpack_reconnect')) { $error = 'cheatin'; break; } check_admin_referer('jetpack-reconnect'); Jetpack::log('reconnect'); $this->disconnect(); wp_redirect($this->build_connect_url(true)); exit; case 'deactivate': if (!current_user_can('jetpack_deactivate_modules')) { $error = 'cheatin'; break; } $modules = stripslashes($_GET['module']); check_admin_referer("jetpack_deactivate-{$modules}"); foreach (explode(',', $modules) as $module) { Jetpack::log('deactivate', $module); Jetpack::deactivate_module($module); Jetpack::state('message', 'module_deactivated'); } Jetpack::state('module', $modules); wp_safe_redirect(Jetpack::admin_url('page=jetpack')); exit; case 'unlink': check_admin_referer('jetpack-unlink'); Jetpack::log('unlink'); $this->unlink_user(); Jetpack::state('message', 'unlinked'); wp_safe_redirect(Jetpack::admin_url()); exit; default: do_action('jetpack_unrecognized_action', sanitize_key($_GET['action'])); } } if (!($error = $error ? $error : Jetpack::state('error'))) { $this->activate_new_modules(); } switch ($error) { case 'cheatin': $this->error = __('Cheatin’ uh?', 'jetpack'); break; 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'), "<code>{$error}</code>"); 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 <strong>fatal error</strong>. 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 <strong>fatal error</strong>. Perhaps there is a conflict with another plugin you have installed?', 'jetpack'); } if ($php_errors = Jetpack::state('php_errors')) { $this->error .= "<br />\n"; $this->error .= $php_errors; } break; case 'master_user_required': $module = Jetpack::state('module'); $module_name = ''; if (!empty($module) && ($mod = Jetpack::get_module($module))) { $module_name = $mod['name']; } $master_user = Jetpack_Options::get_option('master_user'); $master_userdata = get_userdata($master_user); if ($master_userdata) { if (!in_array($module, Jetpack::get_active_modules())) { $this->error = sprintf(__('%s was not activated.', 'jetpack'), $module_name); } else { $this->error = sprintf(__('%s was not deactivated.', 'jetpack'), $module_name); } $this->error .= ' ' . sprintf(__('This module can only be altered by %s, the user who initiated the Jetpack connection on this site.', 'jetpack'), esc_html($master_userdata->display_name)); } else { $this->error = sprintf(__('Only the user who initiated the Jetpack connection on this site can toggle %s, but that user no longer exists. This should not happen.', 'jetpack'), $module_name); } break; case 'not_public': $this->error = __('<strong>Your Jetpack has a glitch.</strong> 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'), "<code>{$error}</code>"); break; default: if (empty($error)) { break; } $error = trim(substr(strip_tags($error), 0, 20)); // no break: fall through // 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(__('<strong>Your Jetpack has a glitch.</strong> Something went wrong that’s never supposed to happen. Guess you’re just lucky: %s', 'jetpack'), "<code>{$error}</code>"); if (!Jetpack::is_active()) { $this->error .= '<br />'; $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 <strong>Jetpack %s</strong>!', 'jetpack'), JETPACK__VERSION); if ($active_state) { $titles = array(); foreach ($active_state as $mod) { if ($mod_headers = Jetpack::get_module($mod)) { $titles[] = '<strong>' . preg_replace('/\\s+(?![^<>]++>)/', ' ', $mod_headers['name']) . '</strong>'; } } if ($titles) { $this->message .= '<br /><br />' . 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[] = '<strong>' . preg_replace('/\\s+(?![^<>]++>)/', ' ', $mod_headers['name']) . '</strong>'; } } if ($titles) { $this->message .= '<br /><br />' . 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(__('<strong>%s Activated!</strong> You can deactivate at any time by clicking the Deactivate link next to each module.', '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('<strong>%l Deactivated!</strong> You can activate it again at any time using the activate link next to each module.', '<strong>%l Deactivated!</strong> You can activate them again at any time using the activate links next to each module.', count($module_names), '%l = list of Jetpack module/feature names', 'jetpack'), $module_names); break; case 'module_configured': $this->message = __('<strong>Module settings were saved.</strong> ', 'jetpack'); break; case 'already_authorized': $this->message = __('<strong>Your Jetpack is already connected.</strong> ', 'jetpack'); break; case 'authorized': $this->message = __('<strong>You’re fueled up and ready to go.</strong> ', 'jetpack'); $this->message .= "<br />\n"; $this->message .= sprintf(__('Jetpack is now active. Browse through each Jetpack feature below. Visit the <a href="%s">settings page</a> to activate/deactivate features.', 'jetpack'), admin_url('admin.php?page=jetpack_modules')); $this->message .= Jetpack::jetpack_comment_notice(); break; case 'linked': $this->message = __('<strong>You’re fueled up and ready to go.</strong> ', 'jetpack'); $this->message .= Jetpack::jetpack_comment_notice(); break; case 'unlinked': $user = wp_get_current_user(); $this->message = sprintf(__('<strong>You have unlinked your account (%s) from WordPress.com.</strong>', '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[] = '<strong>' . str_replace(' ', ' ', $this->plugins_to_deactivate[$deactivated_plugin][1]) . '</strong>'; } if ($deactivated_titles) { if ($this->message) { $this->message .= "<br /><br />\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 .= "<br />\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'); }
protected function activate_module() { if (Jetpack::is_module_active($this->module_slug)) { return new WP_Error('jetpack_module_already_active', __('The Module is already active.', 'jetpack'), 400); } $result = Jetpack::activate_module($this->module_slug, false, false); // TODO return WP_Error instead of bool in order to forward the error message. if (false === $result || !Jetpack::is_module_active($this->module_slug)) { return new WP_Error('activation_error', sprintf(__('There was an error while activating the module `%s`.', 'jetpack'), $this->module_slug), 500); } return true; }