ensure( 'connection', array( 'slug' => JETPACK_PROTECT_SLUG, 'name' => JETPACK_PROTECT_NAME, 'url_info' => JETPACK_PROTECT_URI, ) ); // Sync package. $config->ensure( 'sync', array( 'jetpack_sync_modules' => array( 'Automattic\\Jetpack\\Sync\\Modules\\Options', 'Automattic\\Jetpack\\Sync\\Modules\\Callables', ), 'jetpack_sync_callable_whitelist' => array( 'get_plugins' => array( 'Automattic\\Jetpack\\Sync\\Functions', 'get_plugins' ), 'get_themes' => array( 'Automattic\\Jetpack\\Sync\\Functions', 'get_themes' ), 'wp_version' => array( 'Automattic\\Jetpack\\Sync\\Functions', 'wp_version' ), ), 'jetpack_sync_options_contentless' => array(), 'jetpack_sync_options_whitelist' => array( 'active_plugins', 'stylesheet', ), ) ); // Identity crisis package. $config->ensure( 'identity_crisis' ); }, 1 ); } /** * Initialize the plugin * * @return void */ public function init() { // Set up the REST authentication hooks. Connection_Rest_Authentication::init(); $total_vuls = Protect_Status::get_total_vulnerabilities(); $menu_label = _x( 'Protect', 'The Jetpack Protect product name, without the Jetpack prefix', 'jetpack-protect' ); if ( $total_vuls ) { $menu_label .= sprintf( ' %d', $total_vuls ); } $page_suffix = Admin_Menu::add_menu( __( 'Jetpack Protect', 'jetpack-protect' ), $menu_label, 'manage_options', 'jetpack-protect', array( $this, 'plugin_settings_page' ), 99 ); add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) ); add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 65 ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); // Add custom WP REST API endoints. add_action( 'rest_api_init', array( __CLASS__, 'register_rest_endpoints' ) ); My_Jetpack_Initializer::init(); Site_Health::init(); // Sets up JITMS. JITM::configure(); } /** * Initialize the admin resources. */ public function admin_init() { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); } /** * Enqueues the wp-admin styles (used outside the React app) */ public function enqueue_admin_styles() { wp_enqueue_style( 'jetpack-protect-wpadmin', JETPACK_PROTECT_BASE_PLUGIN_URL . '/assets/jetpack-protect.css', array(), JETPACK_PROTECT_VERSION ); } /** * Enqueue plugin admin scripts and styles. */ public function enqueue_admin_scripts() { Assets::register_script( 'jetpack-protect', 'build/index.js', JETPACK_PROTECT_ROOT_FILE, array( 'in_footer' => true, 'textdomain' => 'jetpack-protect', ) ); Assets::enqueue_script( 'jetpack-protect' ); // Required for Analytics. wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); // Initial JS state including JP Connection data. wp_add_inline_script( 'jetpack-protect', Connection_Initial_State::render(), 'before' ); wp_add_inline_script( 'jetpack-protect', $this->render_initial_state(), 'before' ); } /** * Render the initial state into a JavaScript variable. * * @return string */ public function render_initial_state() { return 'var jetpackProtectInitialState=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->initial_state() ) ) . '"));'; } /** * Get the initial state data for hydrating the React UI. * * @return array */ public function initial_state() { global $wp_version; return array( 'apiRoot' => esc_url_raw( rest_url() ), 'apiNonce' => wp_create_nonce( 'wp_rest' ), 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ), 'status' => Protect_Status::get_status(), 'installedPlugins' => Plugins_Installer::get_plugins(), 'installedThemes' => Sync_Functions::get_themes(), 'wpVersion' => $wp_version, 'adminUrl' => admin_url( 'admin.php?page=jetpack-protect' ), 'securityBundle' => My_Jetpack_Products::get_product( 'security' ), 'productData' => My_Jetpack_Products::get_product( 'protect' ), ); } /** * Main plugin settings page. */ public function plugin_settings_page() { ?>
uninstall(); $manager = new Connection_Manager( 'jetpack-protect' ); $manager->remove_connection(); Protect_Status::delete_option(); } /** * Create a shortcut on Admin Bar to show the total of vulnerabilities found. * * @param object $wp_admin_bar The Admin Bar object. * @return void */ public function admin_bar( $wp_admin_bar ) { if ( ! current_user_can( 'manage_options' ) ) { return; } $total = Protect_Status::get_total_vulnerabilities(); if ( $total > 0 ) { $args = array( 'id' => 'jetpack-protect', 'title' => '' . $total . '', 'href' => admin_url( 'admin.php?page=jetpack-protect' ), 'meta' => array( // translators: %d is the number of vulnerabilities found. 'title' => sprintf( _n( '%d vulnerability found by Jetpack Protect', '%d vulnerabilities found by Jetpack Protect', $total, 'jetpack-protect' ), $total ), ), ); $wp_admin_bar->add_node( $args ); } } /** * Register the REST API routes. * * @return void */ public static function register_rest_endpoints() { register_rest_route( 'jetpack-protect/v1', 'status', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => __CLASS__ . '::api_get_status', 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, ) ); } /** * Return Protect Status for the API endpoint * * @return WP_REST_Response */ public static function api_get_status() { $status = Protect_Status::get_status(); return rest_ensure_response( $status, 200 ); } }