Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into slr/sl-142
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Sep 2, 2024
2 parents 8918eea + 7fe3413 commit dcd6de7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/Uplink/Admin/Package_Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use StellarWP\Uplink\Resources;
use WP_Error;
use WP_Upgrader;
use WP_Filesystem_Base;

class Package_Handler {

Expand All @@ -15,6 +16,11 @@ class Package_Handler {
*/
public $upgrader;

/**
* @var WP_Filesystem_Base|null
*/
public $filesystem;

/**
* Filters the package download step to store the downloaded file with a shorter file name.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Uplink/Auth/Auth_Url_Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class Auth_Url_Builder {
*/
private $auth_url_manager;

/**
* @var string
*/
private $license_key;

/**
* @param Nonce $nonce The Nonce creator.
* @param Auth_Url $auth_url_manager The auth URL manager.
Expand Down Expand Up @@ -57,6 +62,11 @@ public function build( string $slug, string $domain = '' ): string {
'uplink_slug' => $slug,
];

// Optionally include a license key if set.
if ( ! empty( $this->license_key ) ) {
$args['uplink_license'] = $this->license_key;
}

$url = add_query_arg(
array_filter( array_merge( $_GET, $args ) ),
admin_url( $pagenow )
Expand All @@ -70,4 +80,16 @@ public function build( string $slug, string $domain = '' ): string {
);
}

/**
* Optionally set a license key to provide in uplink_callback query arg.
*
* @param string $key The license key to pass in the auth url.
*
* @return self
*/
public function set_license( string $key ): self {
$this->license_key = $key;

return $this;
}
}
9 changes: 5 additions & 4 deletions src/Uplink/Components/Admin/Authorize_Button_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(
/**
* Renders the authorize-button view.
*
* @param array{slug?: string, domain?: string} $args The Product slug and license domain.
* @param array{slug?: string, domain?: string, license?: string} $args The Product slug and license domain.
*
* @see src/views/admin/authorize-button.php
*
Expand All @@ -79,14 +79,15 @@ public function __construct(
public function render( array $args = [] ): void {
global $pagenow;

$slug = $args['slug'] ?? '';
$domain = $args['domain'] ?? '';
$slug = $args['slug'] ?? '';
$domain = $args['domain'] ?? '';
$license = $args['license'] ?? '';

if ( empty ( $slug ) ) {
throw new InvalidArgumentException( __( 'The Product slug cannot be empty', '%TEXTDOMAIN%' ) );
}

$url = $this->url_builder->build( $slug, $domain );
$url = $this->url_builder->set_license( $license )->build( $slug, $domain );

if ( ! $url ) {
return;
Expand Down
8 changes: 5 additions & 3 deletions src/Uplink/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ function get_container(): ContainerInterface {
*
* @param string $slug The Product slug to render the button for.
* @param string $domain An optional domain associated with a license key to pass along.
* @param string $license The license that should be authenticated before token generation.
*/
function render_authorize_button( string $slug, string $domain = '' ): void {
function render_authorize_button( string $slug, string $domain = '', string $license = '' ): void {
try {
get_container()->get( Authorize_Button_Controller::class )
->render( [
'slug' => $slug,
'domain' => $domain,
'slug' => $slug,
'domain' => $domain,
'license' => $license,
] );
} catch ( Throwable $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
Expand Down
4 changes: 2 additions & 2 deletions tests/_support/Helper/Http_API/Http_API_Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ public function will_reply_to_request( string $method, string $uri, $response ):
/**
* Hooked on the `pre_http_request` filter to prefill the mocked HTTP responses.
*
* @param bool $preempt Whether to preempt an HTTP request's return value. Default `false`.
* @param bool|array|WP_Error $preempt Whether to preempt an HTTP request's return value. Default `false`.
* @param array<string,mixed> $parsed_args The HTTP request arguments.
* @param string $url The full request URL.
*
* @return false|mixed Either the mocked response or `false` to let the request go through.
*/
public function mock_http_response( bool $preempt, array $parsed_args, string $url ) {
public function mock_http_response( $preempt, array $parsed_args, string $url ) {
$uri = '/' . ltrim( str_replace( $this->get_url(), '', $url ), '/' );
$method = $parsed_args['method'] ?? 'GET';
$key = "$method $uri";
Expand Down
63 changes: 63 additions & 0 deletions tests/wpunit/Auth/AuthUrlBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare( strict_types=1 );

namespace StellarWP\Uplink\Tests\Auth;

use StellarWP\Uplink\API\V3\Auth\Auth_Url;
use StellarWP\Uplink\Auth\Auth_Url_Builder;
use StellarWP\Uplink\Auth\Nonce;
use StellarWP\Uplink\Config;
use StellarWP\Uplink\Tests\Traits\With_Uopz;
use StellarWP\Uplink\Tests\UplinkTestCase;
use StellarWP\Uplink\Uplink;

final class AuthUrlBuilderTest extends UplinkTestCase {

use With_Uopz;

/**
* @var Auth_Url_Builder
*/
private $auth_url_builder;

protected function setUp(): void {
parent::setUp();

Config::set_token_auth_prefix( 'kadence_' );

// Run init again to reload all providers.
Uplink::init();

$this->set_class_fn_return( Auth_Url::class, 'get', 'https://theeventscalendar.com/account-auth' );
$this->set_class_fn_return( Nonce::class, 'create', 'abcd1234' );

$this->auth_url_builder = $this->container->get( Auth_Url_Builder::class );
}

public function test_it_builds_url(): void {
$url = $this->auth_url_builder->build( 'the-events-calendar', 'theeventscalendar.com' );

$callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' );
$expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback );

$this->assertSame( $expected, $url );
}

public function test_it_builds_url_with_license(): void {
$url = $this->auth_url_builder->set_license('some-license-key')->build( 'the-events-calendar', 'theeventscalendar.com' );

$callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&uplink_license=some-license-key&_uplink_nonce=abcd1234' );
$expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback );

$this->assertSame( $expected, $url );
}

public function test_it_builds_url_with_empty_license(): void {
$url = $this->auth_url_builder->set_license('')->build( 'the-events-calendar', 'theeventscalendar.com' );

$callback = base64_encode( 'http://wordpress.test/wp-admin/index.php?uplink_domain=theeventscalendar.com&uplink_slug=the-events-calendar&_uplink_nonce=abcd1234' );
$expected = 'https://theeventscalendar.com/account-auth?uplink_callback=' . rawurlencode( $callback );

$this->assertSame( $expected, $url );
}

}

0 comments on commit dcd6de7

Please sign in to comment.