Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of application passwords. #934

Merged
merged 8 commits into from
Sep 13, 2022
25 changes: 16 additions & 9 deletions assets/js/admin-external-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,22 @@ jQuery( authorizeConnectionButton ).on( 'click', ( event ) => {
? 'authorize-application.php'
: 'admin.php?page=auth_app';

const authURL = addQueryArgs(
`${ siteURL }wp-admin/${ auth_page }`,
{
app_name: dt.distributor_from /*eslint camelcase: 0*/,
success_url:
encodeURI( successURL ) /*eslint camelcase: 0*/,
reject_url: encodeURI( failureURL ) /*eslint camelcase: 0*/,
}
);
let auth_url;

if (
'core_application_passwords_endpoint' in response.data &&
response.data.core_application_passwords_available
) {
auth_url = response.data.core_application_passwords_endpoint;
} else {
auth_url = `${ siteURL }wp-admin/${ auth_page }`;
}

const authURL = addQueryArgs( auth_url, {
app_name: dt.distributor_from /*eslint camelcase: 0*/,
success_url: encodeURI( successURL ) /*eslint camelcase: 0*/,
reject_url: encodeURI( failureURL ) /*eslint camelcase: 0*/,
} );
document.location = authURL;
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,13 @@ public function check_connections() {
'endpoint_suggestion' => false,
);

$response = Utils\remote_http_request( untrailingslashit( $this->base_url ), $this->auth_handler->format_get_args( array( 'timeout' => self::$timeout ) ) );
$remote_request_url = untrailingslashit( $this->base_url );
if ( str_ends_with( $remote_request_url, '?rest_route=' ) ) {
// It's a request to get the REST API index using plain permalinks and needs a trailing slash.
$remote_request_url = trailingslashit( $remote_request_url );
}

$response = Utils\remote_http_request( $remote_request_url, $this->auth_handler->format_get_args( array( 'timeout' => self::$timeout ) ) );

$body = wp_remote_retrieve_body( $response );

Expand Down
67 changes: 65 additions & 2 deletions includes/global-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
/**
* Functions in the global namespace.
*
* These functions are required in the global namespace for the coding standards
* sniffs to recognize them as custom escaping or sanitization functions.
* These functions are required in the global namespace.
*
* @package distributor
*/
Expand Down Expand Up @@ -49,3 +48,67 @@ function distributor_sanitize_connection( $connection ) {
);
return $sanitized_connection;
}

if ( ! function_exists( 'str_contains' ) ) {
/**
* Polyfill for `str_contains()` function added in PHP 8.0/WP 5.9.0.
*
* Performs a case-sensitive check indicating if needle is
* contained in haystack.
*
* @since x.x.x
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the haystack.
* @return bool True if `$needle` is in `$haystack`, otherwise false.
*/
function str_contains( $haystack, $needle ) {
return ( '' === $needle || false !== strpos( $haystack, $needle ) );
}
}

if ( ! function_exists( 'str_starts_with' ) ) {
/**
* Polyfill for `str_starts_with()` function added in PHP 8.0/WP 5.9.0.
*
* Performs a case-sensitive check indicating if
* the haystack begins with needle.
*
* @since x.x.x
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the `$haystack`.
* @return bool True if `$haystack` starts with `$needle`, otherwise false.
*/
function str_starts_with( $haystack, $needle ) {
if ( '' === $needle ) {
return true;
}

return 0 === strpos( $haystack, $needle );
}
}

if ( ! function_exists( 'str_ends_with' ) ) {
/**
* Polyfill for `str_ends_with()` function added in PHP 8.0/WP 5.9.0.
*
* Performs a case-sensitive check indicating if
* the haystack ends with needle.
*
* @since x.x.x
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the `$haystack`.
* @return bool True if `$haystack` ends with `$needle`, otherwise false.
*/
function str_ends_with( $haystack, $needle ) {
if ( '' === $haystack && '' !== $needle ) {
return false;
}

$len = strlen( $needle );

return 0 === substr_compare( $haystack, $needle, -$len, $len );
}
}
3 changes: 3 additions & 0 deletions tests/php/bootstrap.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ WP_Mock::setUsePatchwork( true );
WP_Mock::bootstrap();

require_once __DIR__ . '/includes/common.php';

require_once( __DIR__ . '/../../includes/utils.php' );
require_once( __DIR__ . '/../../includes/debug-info.php' );
require_once( __DIR__ . '/../../includes/subscriptions.php' );
require_once( __DIR__ . '/../../includes/global-functions.php' );

require_once __DIR__ . '/includes/TestCase.php';