installed plugin Jetpack Protect
version 1.0.2
This commit is contained in:
@ -0,0 +1,30 @@
|
||||
#wpadminbar #wp-admin-bar-jetpack-idc {
|
||||
margin-right: 5px;
|
||||
|
||||
.jp-idc-admin-bar {
|
||||
border-radius: 2px;
|
||||
font-weight: 500;
|
||||
font-size: 14px;
|
||||
line-height: 20px;
|
||||
color: #EFEFF0;
|
||||
padding: 6px 8px;
|
||||
}
|
||||
|
||||
&.hide {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.dashicons {
|
||||
font-family: 'dashicons';
|
||||
margin-top: -6px;
|
||||
|
||||
&:before {
|
||||
font-size: 18px;
|
||||
}
|
||||
}
|
||||
|
||||
.ab-item {
|
||||
padding: 0;
|
||||
background: #E68B28;
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
import { IDCScreen } from '@automattic/jetpack-idc';
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
|
||||
import './admin-bar.scss';
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* The initial renderer function.
|
||||
*/
|
||||
function render() {
|
||||
const container = document.getElementById( 'jp-identity-crisis-container' );
|
||||
|
||||
if ( null === container || ! window.hasOwnProperty( 'JP_IDENTITY_CRISIS__INITIAL_STATE' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
WP_API_root,
|
||||
WP_API_nonce,
|
||||
wpcomHomeUrl,
|
||||
currentUrl,
|
||||
redirectUri,
|
||||
tracksUserData,
|
||||
tracksEventData,
|
||||
isSafeModeConfirmed,
|
||||
consumerData,
|
||||
isAdmin,
|
||||
possibleDynamicSiteUrlDetected,
|
||||
} = window.JP_IDENTITY_CRISIS__INITIAL_STATE;
|
||||
|
||||
if ( ! isSafeModeConfirmed ) {
|
||||
ReactDOM.render(
|
||||
<IDCScreen
|
||||
wpcomHomeUrl={ wpcomHomeUrl }
|
||||
currentUrl={ currentUrl }
|
||||
apiRoot={ WP_API_root }
|
||||
apiNonce={ WP_API_nonce }
|
||||
redirectUri={ redirectUri }
|
||||
tracksUserData={ tracksUserData || {} }
|
||||
tracksEventData={ tracksEventData }
|
||||
customContent={
|
||||
consumerData.hasOwnProperty( 'customContent' ) ? consumerData.customContent : {}
|
||||
}
|
||||
isAdmin={ isAdmin }
|
||||
logo={ consumerData.hasOwnProperty( 'logo' ) ? consumerData.logo : undefined }
|
||||
possibleDynamicSiteUrlDetected={ possibleDynamicSiteUrlDetected }
|
||||
/>,
|
||||
container
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
render();
|
@ -0,0 +1,9 @@
|
||||
#jp-identity-crisis-container .jp-idc__idc-screen {
|
||||
margin-top: 40px;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
#jp-identity-crisis-container.notice {
|
||||
background: none;
|
||||
border: none;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,188 @@
|
||||
<?php
|
||||
/**
|
||||
* Identity_Crisis package.
|
||||
*
|
||||
* @package automattic/jetpack-identity-crisis
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack\IdentityCrisis;
|
||||
|
||||
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
|
||||
use Jetpack_Options;
|
||||
use WP_Error;
|
||||
use WP_REST_Server;
|
||||
|
||||
/**
|
||||
* This class will handle Identity Crisis Endpoints
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
class REST_Endpoints {
|
||||
|
||||
/**
|
||||
* Initialize REST routes.
|
||||
*/
|
||||
public static function initialize_rest_api() {
|
||||
|
||||
// Confirm that a site in identity crisis should be in staging mode.
|
||||
register_rest_route(
|
||||
'jetpack/v4',
|
||||
'/identity-crisis/confirm-safe-mode',
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => __CLASS__ . '::confirm_safe_mode',
|
||||
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
|
||||
)
|
||||
);
|
||||
|
||||
// Handles the request to migrate stats and subscribers during an identity crisis.
|
||||
register_rest_route(
|
||||
'jetpack/v4',
|
||||
'identity-crisis/migrate',
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => __CLASS__ . '::migrate_stats_and_subscribers',
|
||||
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
|
||||
)
|
||||
);
|
||||
|
||||
// IDC resolve: create an entirely new shadow site for this URL.
|
||||
register_rest_route(
|
||||
'jetpack/v4',
|
||||
'/identity-crisis/start-fresh',
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => __CLASS__ . '::start_fresh_connection',
|
||||
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
|
||||
'args' => array(
|
||||
'redirect_uri' => array(
|
||||
'description' => __( 'URI of the admin page where the user should be redirected after connection flow', 'jetpack-idc' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles identity crisis mitigation, confirming safe mode for this site.
|
||||
*
|
||||
* @since 0.2.0
|
||||
* @since-jetpack 4.4.0
|
||||
*
|
||||
* @return bool | WP_Error True if option is properly set.
|
||||
*/
|
||||
public static function confirm_safe_mode() {
|
||||
$updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true );
|
||||
if ( $updated ) {
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'code' => 'success',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return new WP_Error(
|
||||
'error_setting_jetpack_safe_mode',
|
||||
esc_html__( 'Could not confirm safe mode.', 'jetpack-idc' ),
|
||||
array( 'status' => 500 )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles identity crisis mitigation, migrating stats and subscribers from old url to this, new url.
|
||||
*
|
||||
* @since 0.2.0
|
||||
* @since-jetpack 4.4.0
|
||||
*
|
||||
* @return bool | WP_Error True if option is properly set.
|
||||
*/
|
||||
public static function migrate_stats_and_subscribers() {
|
||||
if ( Jetpack_Options::get_option( 'sync_error_idc' ) && ! Jetpack_Options::delete_option( 'sync_error_idc' ) ) {
|
||||
return new WP_Error(
|
||||
'error_deleting_sync_error_idc',
|
||||
esc_html__( 'Could not delete sync error option.', 'jetpack-idc' ),
|
||||
array( 'status' => 500 )
|
||||
);
|
||||
}
|
||||
|
||||
if ( Jetpack_Options::get_option( 'migrate_for_idc' ) || Jetpack_Options::update_option( 'migrate_for_idc', true ) ) {
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'code' => 'success',
|
||||
)
|
||||
);
|
||||
}
|
||||
return new WP_Error(
|
||||
'error_setting_jetpack_migrate',
|
||||
esc_html__( 'Could not confirm migration.', 'jetpack-idc' ),
|
||||
array( 'status' => 500 )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This IDC resolution will disconnect the site and re-connect to a completely new
|
||||
* and separate shadow site than the original.
|
||||
*
|
||||
* It will first will disconnect the site without phoning home as to not disturb the production site.
|
||||
* It then builds a fresh connection URL and sends it back along with the response.
|
||||
*
|
||||
* @since 0.2.0
|
||||
* @since-jetpack 4.4.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The request sent to the WP REST API.
|
||||
*
|
||||
* @return \WP_REST_Response|WP_Error
|
||||
*/
|
||||
public static function start_fresh_connection( $request ) {
|
||||
/**
|
||||
* Fires when Users have requested through Identity Crisis for the connection to be reset.
|
||||
* Should be used to disconnect any connections and reset options.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*/
|
||||
do_action( 'jetpack_idc_disconnect' );
|
||||
|
||||
$connection = new Connection_Manager();
|
||||
$result = $connection->try_registration( true );
|
||||
|
||||
// early return if site registration fails.
|
||||
if ( ! $result || is_wp_error( $result ) ) {
|
||||
return rest_ensure_response( $result );
|
||||
}
|
||||
|
||||
$redirect_uri = $request->get_param( 'redirect_uri' ) ? admin_url( $request->get_param( 'redirect_uri' ) ) : null;
|
||||
|
||||
/**
|
||||
* Filters the connection url that users should be redirected to for re-establishing their connection.
|
||||
*
|
||||
* @since 0.2.0
|
||||
*
|
||||
* @param \WP_REST_Response|WP_Error $connection_url Connection URL user should be redirected to.
|
||||
*/
|
||||
return apply_filters( 'jetpack_idc_authorization_url', rest_ensure_response( $connection->get_authorization_url( null, $redirect_uri ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that user can mitigate an identity crisis.
|
||||
*
|
||||
* @since 0.2.0
|
||||
* @since-jetpack 4.4.0
|
||||
*
|
||||
* @return true|WP_Error True if the user has capability 'jetpack_disconnect', an error object otherwise.
|
||||
*/
|
||||
public static function identity_crisis_mitigation_permission_check() {
|
||||
if ( current_user_can( 'jetpack_disconnect' ) ) {
|
||||
return true;
|
||||
}
|
||||
$error_msg = esc_html__(
|
||||
'You do not have the correct user permissions to perform this action.
|
||||
Please contact your site admin if you think this is a mistake.',
|
||||
'jetpack-idc'
|
||||
);
|
||||
|
||||
return new WP_Error( 'invalid_user_permission_identity_crisis', $error_msg, array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,193 @@
|
||||
<?php
|
||||
/**
|
||||
* Identity_Crisis package.
|
||||
*
|
||||
* @package automattic/jetpack-identity-crisis
|
||||
*/
|
||||
|
||||
namespace Automattic\Jetpack\IdentityCrisis;
|
||||
|
||||
use Automattic\Jetpack\Assets;
|
||||
use Automattic\Jetpack\Identity_Crisis;
|
||||
use Automattic\Jetpack\Status\Host;
|
||||
use Automattic\Jetpack\Tracking as Tracking;
|
||||
use Jetpack_Options;
|
||||
use Jetpack_Tracks_Client;
|
||||
|
||||
/**
|
||||
* The Identity Crisis UI handling.
|
||||
*/
|
||||
class UI {
|
||||
|
||||
/**
|
||||
* Temporary storage for consumer data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $consumers;
|
||||
|
||||
/**
|
||||
* Initialization.
|
||||
*/
|
||||
public static function init() {
|
||||
if ( did_action( 'jetpack_identity_crisis_ui_init' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Action called after initializing Identity Crisis UI.
|
||||
*
|
||||
* @since 0.6.0
|
||||
*/
|
||||
do_action( 'jetpack_identity_crisis_ui_init' );
|
||||
|
||||
$idc_data = Identity_Crisis::check_identity_crisis();
|
||||
|
||||
if ( false === $idc_data ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array( static::class, 'enqueue_scripts' ) );
|
||||
|
||||
Tracking::register_tracks_functions_scripts( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts!
|
||||
*/
|
||||
public static function enqueue_scripts() {
|
||||
if ( is_admin() ) {
|
||||
Assets::register_script(
|
||||
'jp_identity_crisis_banner',
|
||||
'../build/index.js',
|
||||
__FILE__,
|
||||
array(
|
||||
'in_footer' => true,
|
||||
'textdomain' => 'jetpack-idc',
|
||||
)
|
||||
);
|
||||
Assets::enqueue_script( 'jp_identity_crisis_banner' );
|
||||
wp_add_inline_script( 'jp_identity_crisis_banner', static::get_initial_state(), 'before' );
|
||||
|
||||
add_action( 'admin_notices', array( static::class, 'render_container' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the container element for the IDC banner.
|
||||
*/
|
||||
public static function render_container() {
|
||||
?>
|
||||
<div id="jp-identity-crisis-container" class="notice"></div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the rendered initial state JavaScript code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function get_initial_state() {
|
||||
return 'var JP_IDENTITY_CRISIS__INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( static::get_initial_state_data() ) ) . '"));';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the initial state data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_initial_state_data() {
|
||||
$idc_urls = Identity_Crisis::get_mismatched_urls();
|
||||
$current_screen = get_current_screen();
|
||||
$is_admin = current_user_can( 'jetpack_disconnect' );
|
||||
$possible_dynamic_site_url_detected = (bool) Identity_Crisis::detect_possible_dynamic_site_url();
|
||||
|
||||
return array(
|
||||
'WP_API_root' => esc_url_raw( rest_url() ),
|
||||
'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
|
||||
'wpcomHomeUrl' => ( is_array( $idc_urls ) && array_key_exists( 'wpcom_url', $idc_urls ) ) ? $idc_urls['wpcom_url'] : null,
|
||||
'currentUrl' => ( is_array( $idc_urls ) && array_key_exists( 'current_url', $idc_urls ) ) ? $idc_urls['current_url'] : null,
|
||||
'redirectUri' => isset( $_SERVER['REQUEST_URI'] ) ? str_replace( '/wp-admin/', '/', filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '',
|
||||
'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
|
||||
'tracksEventData' => array(
|
||||
'isAdmin' => $is_admin,
|
||||
'currentScreen' => $current_screen ? $current_screen->id : false,
|
||||
'blogID' => Jetpack_Options::get_option( 'id' ),
|
||||
'platform' => static::get_platform(),
|
||||
),
|
||||
'isSafeModeConfirmed' => Identity_Crisis::$is_safe_mode_confirmed,
|
||||
'consumerData' => static::get_consumer_data(),
|
||||
'isAdmin' => $is_admin,
|
||||
'possibleDynamicSiteUrlDetected' => $possible_dynamic_site_url_detected,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the package consumer data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_consumer_data() {
|
||||
if ( null !== static::$consumers ) {
|
||||
return static::$consumers;
|
||||
}
|
||||
|
||||
$consumers = apply_filters( 'jetpack_idc_consumers', array() );
|
||||
|
||||
if ( ! $consumers ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
usort(
|
||||
$consumers,
|
||||
function ( $c1, $c2 ) {
|
||||
$priority1 = ( array_key_exists( 'priority', $c1 ) && (int) $c1['priority'] ) ? (int) $c1['priority'] : 10;
|
||||
$priority2 = ( array_key_exists( 'priority', $c2 ) && (int) $c2['priority'] ) ? (int) $c2['priority'] : 10;
|
||||
|
||||
return $priority1 > $priority2 ? 1 : -1;
|
||||
}
|
||||
);
|
||||
|
||||
$consumer_chosen = null;
|
||||
$consumer_url_length = 0;
|
||||
|
||||
foreach ( $consumers as $consumer ) {
|
||||
if ( empty( $consumer['admin_page'] ) || ! is_string( $consumer['admin_page'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( isset( $_SERVER['REQUEST_URI'] ) && 0 === strpos( filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ) ), $consumer['admin_page'] ) && strlen( $consumer['admin_page'] ) > $consumer_url_length ) {
|
||||
$consumer_chosen = $consumer;
|
||||
$consumer_url_length = strlen( $consumer['admin_page'] );
|
||||
}
|
||||
}
|
||||
|
||||
static::$consumers = $consumer_chosen ? $consumer_chosen : array_shift( $consumers );
|
||||
|
||||
return static::$consumers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the site platform.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function get_platform() {
|
||||
$host = new Host();
|
||||
|
||||
if ( $host->is_woa_site() ) {
|
||||
return 'woa';
|
||||
}
|
||||
|
||||
if ( $host->is_vip_site() ) {
|
||||
return 'vip';
|
||||
}
|
||||
|
||||
if ( $host->is_newspack_site() ) {
|
||||
return 'newspack';
|
||||
}
|
||||
|
||||
return 'self-hosted';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user