Skip to content

Commit

Permalink
Move out initial code of GoogleListingsAndAdsPlugin file
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemd24 committed Nov 6, 2023
1 parent 05ab472 commit 6975f77
Showing 1 changed file with 0 additions and 102 deletions.
102 changes: 0 additions & 102 deletions src/Infrastructure/GoogleListingsAndAdsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ final class GoogleListingsAndAdsPlugin implements Plugin {
*/
private $registered_services;

/**
* The client ID.
* @var string
*/
private $client_id;

/**
* GoogleListingsAndAdsPlugin constructor.
*
Expand Down Expand Up @@ -121,12 +115,6 @@ function() {
}
);

add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) );

add_filter('jetpack_xmlrpc_test_connection_response', function (){
return '1.40';
});

}

/**
Expand Down Expand Up @@ -155,94 +143,4 @@ protected function maybe_register_services(): void {

$registered = true;
}

/**
* Handles the login action for Authorizing the JSON API
*/
public function login_form_json_api_authorization() {
add_action( 'wp_login', array( $this, 'store_json_api_authorization_token' ), 10, 2 );
add_action( 'login_form', array( $this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
add_filter( 'site_url', array( $this, 'post_login_form_to_signed_url' ), 10, 3 );
}

/**
* If someone logs in to approve API access, store the Access Code in usermeta.
*
* @param string $user_login Unused.
* @param WP_User $user User logged in.
*/
public function store_json_api_authorization_token( $user_login, $user ) {
$data = json_decode( base64_decode( stripslashes( $_REQUEST['data'] ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$this->client_id = $data->client_id;
add_filter( 'login_redirect', array( $this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_public_api_domain' ) );
$token = wp_generate_password( 32, false );
update_user_meta( $user->ID, 'jetpack_json_api_' . $this->client_id, $token );
}

/**
* Make sure the POSTed request is handled by the same action.
*/
public function preserve_action_in_login_form_for_json_api_authorization() {
$http_host = isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- escaped with esc_url below.
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- escaped with esc_url below.
echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n";
echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $http_host . $request_uri ) ) . "' />\n";
}

/**
* Make sure the login form is POSTed to the signed URL so we can reverify the request.
*
* @param string $url Redirect URL.
* @param string $path Path.
* @param string $scheme URL Scheme.
*/
public function post_login_form_to_signed_url( $url, $path, $scheme ) {
if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) {
return $url;
}
$query_string = isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$parsed_url = wp_parse_url( $url );
$url = strtok( $url, '?' );
$url = "$url?{$query_string}";
if ( ! empty( $parsed_url['query'] ) ) {
$url .= "&{$parsed_url['query']}";
}

return $url;
}

/**
* Add the Access Code details to the public-api.wordpress.com redirect.
*
* @param string $redirect_to URL.
* @param string $original_redirect_to URL.
* @param WP_User $user WP_User for the redirect.
*
* @return string
*/
public function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
return add_query_arg(
urlencode_deep(
array(
'jetpack-code' => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->client_id, true ),
'jetpack-user-id' => (int) $user->ID,
'jetpack-state' => '',
)
),
$redirect_to
);
}

/**
* Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
*
* To be used with a filter of allowed domains for a redirect.
*
* @param array $domains Allowed WP.com Environments.
*/
public function allow_wpcom_public_api_domain( $domains ) {
$domains[] = 'public-api.wordpress.com';
return $domains;
}
}

0 comments on commit 6975f77

Please sign in to comment.