updated plugin Jetpack Protect version 5.0.0
This commit is contained in:
@ -20,7 +20,7 @@ class ExPlat {
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const PACKAGE_VERSION = '0.2.13';
|
||||
const PACKAGE_VERSION = '0.4.20';
|
||||
|
||||
/**
|
||||
* Initializer.
|
||||
|
||||
@ -64,6 +64,11 @@ class REST_Controller {
|
||||
'as_connected_user' => array(
|
||||
'type' => 'boolean',
|
||||
),
|
||||
'platform' => array(
|
||||
'type' => 'string',
|
||||
'enum' => array( 'jetpack', 'calypso', 'wpcom' ),
|
||||
'default' => 'jetpack',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
@ -78,16 +83,22 @@ class REST_Controller {
|
||||
public function get_assignments( $request ) {
|
||||
$response = null;
|
||||
$is_user_connected = ( new Jetpack_Connection() )->is_user_connected();
|
||||
$request_path = '/experiments/' . self::EXPLAT_API_VERSION . '/assignments/jetpack';
|
||||
$platform = $request->get_param( 'platform' );
|
||||
$request_path = '/experiments/' . self::EXPLAT_API_VERSION . '/assignments/' . $platform;
|
||||
$args = array(
|
||||
'experiment_name' => $request['experiment_name'],
|
||||
'anon_id' => $request['anon_id'],
|
||||
'experiment_names' => $request['experiment_name'],
|
||||
'anon_id' => $request['anon_id'],
|
||||
);
|
||||
|
||||
if ( $request['as_connected_user'] && $is_user_connected ) {
|
||||
$response = Client::wpcom_json_api_request_as_user(
|
||||
add_query_arg( $args, $request_path ),
|
||||
'v2'
|
||||
'v2',
|
||||
array(
|
||||
'headers' => array(
|
||||
'User-Agent' => 'Jetpack MU WPCOM Plugin Experiment Assignment',
|
||||
),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$response = wp_remote_get(
|
||||
|
||||
@ -1,61 +0,0 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { parse as cookieParse } from 'cookie';
|
||||
|
||||
let initializeAnonIdPromise: Promise< string | null > | null = null;
|
||||
const anonIdPollingIntervalMilliseconds = 50;
|
||||
const anonIdPollingIntervalMaxAttempts = 100; // 50 * 100 = 5000 = 5 seconds
|
||||
|
||||
/**
|
||||
* Gather w.js anonymous cookie, tk_ai
|
||||
*
|
||||
* @return {?string} The anonymous cookie value, or null if it doesn't exist
|
||||
*/
|
||||
export const readAnonCookie = (): string | null => {
|
||||
return cookieParse( document.cookie ).tk_ai || null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initializes the anonId:
|
||||
* - Polls for AnonId receival
|
||||
* - Should only be called once at startup
|
||||
* - Happens to be safe to call multiple times if it is necessary to reset the anonId - something like this was necessary for testing.
|
||||
*
|
||||
* This purely for boot-time initialization, in usual circumstances it will be retrieved within 100-300ms, it happens in parallel booting
|
||||
* so should only delay experiment loading that much for boot-time experiments. In some circumstances such as a very slow connection this
|
||||
* can take a lot longer.
|
||||
*
|
||||
* The state of initializeAnonIdPromise should be used rather than the return of this function.
|
||||
* The return is only avaliable to make this easier to test.
|
||||
*
|
||||
* Throws on error.
|
||||
*
|
||||
* @return {Promise<string | null>} The anonymous cookie value, or null if it doesn't exist
|
||||
*/
|
||||
export const initializeAnonId = async (): Promise< string | null > => {
|
||||
let attempt = 0;
|
||||
initializeAnonIdPromise = new Promise( res => {
|
||||
const poll = () => {
|
||||
const anonId = readAnonCookie();
|
||||
if ( typeof anonId === 'string' && anonId !== '' ) {
|
||||
res( anonId );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( anonIdPollingIntervalMaxAttempts - 1 <= attempt ) {
|
||||
res( null );
|
||||
return;
|
||||
}
|
||||
attempt += 1;
|
||||
setTimeout( poll, anonIdPollingIntervalMilliseconds );
|
||||
};
|
||||
poll();
|
||||
} );
|
||||
|
||||
return initializeAnonIdPromise;
|
||||
};
|
||||
|
||||
export const getAnonId = async (): Promise< string | null > => {
|
||||
return await initializeAnonIdPromise;
|
||||
};
|
||||
@ -1,29 +0,0 @@
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
import { addQueryArgs } from '@wordpress/url';
|
||||
|
||||
const fetchExperimentAssignment =
|
||||
( asConnectedUser = false ) =>
|
||||
async ( {
|
||||
experimentName,
|
||||
anonId,
|
||||
}: {
|
||||
experimentName: string;
|
||||
anonId: string | null;
|
||||
} ): Promise< unknown > => {
|
||||
if ( ! anonId ) {
|
||||
throw new Error( `Tracking is disabled, can't fetch experimentAssignment` );
|
||||
}
|
||||
|
||||
const params = {
|
||||
experiment_name: experimentName,
|
||||
anon_id: anonId ?? undefined,
|
||||
as_connected_user: asConnectedUser,
|
||||
};
|
||||
|
||||
const assignmentsRequestUrl = addQueryArgs( 'jetpack/v4/explat/assignments', params );
|
||||
|
||||
return await apiFetch( { path: assignmentsRequestUrl } );
|
||||
};
|
||||
|
||||
export const fetchExperimentAssignmentAnonymously = fetchExperimentAssignment( false );
|
||||
export const fetchExperimentAssignmentWithAuth = fetchExperimentAssignment( true );
|
||||
@ -1,39 +0,0 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { isDevelopmentMode } from './utils';
|
||||
|
||||
export const logError = ( error: Record< string, string > & { message: string } ): void => {
|
||||
const onLoggingError = ( e: unknown ) => {
|
||||
if ( isDevelopmentMode ) {
|
||||
console.error( '[ExPlat] Unable to send error to server:', e ); // eslint-disable-line no-console
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
const { message, ...properties } = error;
|
||||
const logStashError = {
|
||||
message,
|
||||
properties: {
|
||||
...properties,
|
||||
context: 'explat',
|
||||
explat_client: 'jetpack',
|
||||
},
|
||||
};
|
||||
|
||||
if ( isDevelopmentMode ) {
|
||||
console.error( '[ExPlat] ', error.message, error ); // eslint-disable-line no-console
|
||||
} else {
|
||||
const body = new window.FormData();
|
||||
body.append( 'error', JSON.stringify( logStashError ) );
|
||||
window
|
||||
.fetch( 'https://public-api.wordpress.com/rest/v1.1/js-error', {
|
||||
method: 'POST',
|
||||
body,
|
||||
} )
|
||||
.catch( onLoggingError );
|
||||
}
|
||||
} catch ( e ) {
|
||||
onLoggingError( e );
|
||||
}
|
||||
};
|
||||
@ -1,51 +0,0 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { createExPlatClient } from '@automattic/explat-client';
|
||||
import createExPlatClientReactHelpers from '@automattic/explat-client-react-helpers';
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { getAnonId, initializeAnonId } from './anon';
|
||||
import {
|
||||
fetchExperimentAssignmentAnonymously,
|
||||
fetchExperimentAssignmentWithAuth,
|
||||
} from './assignment';
|
||||
import { logError } from './error';
|
||||
import { isDevelopmentMode } from './utils';
|
||||
|
||||
export const initializeExPlat = (): void => {
|
||||
initializeAnonId().catch( e => logError( { message: e.message } ) );
|
||||
};
|
||||
|
||||
initializeExPlat();
|
||||
|
||||
const exPlatClient = createExPlatClient( {
|
||||
fetchExperimentAssignment: fetchExperimentAssignmentAnonymously,
|
||||
getAnonId,
|
||||
logError,
|
||||
isDevelopmentMode,
|
||||
} );
|
||||
|
||||
export const { loadExperimentAssignment, dangerouslyGetExperimentAssignment } = exPlatClient;
|
||||
|
||||
export const { useExperiment, Experiment, ProvideExperimentData } =
|
||||
createExPlatClientReactHelpers( exPlatClient );
|
||||
|
||||
const exPlatClientWithAuth = createExPlatClient( {
|
||||
fetchExperimentAssignment: fetchExperimentAssignmentWithAuth,
|
||||
getAnonId,
|
||||
logError,
|
||||
isDevelopmentMode,
|
||||
} );
|
||||
|
||||
export const {
|
||||
loadExperimentAssignment: loadExperimentAssignmentWithAuth,
|
||||
dangerouslyGetExperimentAssignment: dangerouslyGetExperimentAssignmentWithAuth,
|
||||
} = exPlatClientWithAuth;
|
||||
|
||||
export const {
|
||||
useExperiment: useExperimentWithAuth,
|
||||
Experiment: ExperimentWithAuth,
|
||||
ProvideExperimentData: ProvideExperimentDataWithAuth,
|
||||
} = createExPlatClientReactHelpers( exPlatClientWithAuth );
|
||||
@ -1,4 +0,0 @@
|
||||
/**
|
||||
* Boolean determining if environment is development.
|
||||
*/
|
||||
export const isDevelopmentMode = process.env.NODE_ENV === 'development';
|
||||
Reference in New Issue
Block a user