diff --git a/src/Uplink/Admin/Ajax.php b/src/Uplink/Admin/Ajax.php index f4e263b7..14687c41 100644 --- a/src/Uplink/Admin/Ajax.php +++ b/src/Uplink/Admin/Ajax.php @@ -3,6 +3,7 @@ namespace StellarWP\Uplink\Admin; use StellarWP\ContainerContract\ContainerInterface; +use StellarWP\Uplink\Auth\Auth_Url_Builder; use StellarWP\Uplink\Config; use StellarWP\Uplink\Resources\Collection; use StellarWP\Uplink\Utils; @@ -55,12 +56,17 @@ public function validate_license(): void { ] ); } - $results = $plugin->validate_license( $submission['key'] ); - $message = is_plugin_active_for_network( $plugin->get_path() ) ? $results->get_network_message()->get() : $results->get_message()->get(); + $results = $plugin->validate_license( $submission['key'] ); + $message = is_plugin_active_for_network( $plugin->get_path() ) ? $results->get_network_message()->get() : $results->get_message()->get(); + $auth_url = Config::get_container() + ->get( Auth_Url_Builder::class ) + ->set_license( $submission['key'] ) + ->build( $submission['slug'], get_site_url() ); wp_send_json( [ 'status' => absint( $results->is_valid() ), 'message' => $message, + 'auth_url' => $auth_url, ] ); } diff --git a/src/Uplink/Admin/Fields/Field.php b/src/Uplink/Admin/Fields/Field.php index 56fe77eb..ede06230 100644 --- a/src/Uplink/Admin/Fields/Field.php +++ b/src/Uplink/Admin/Fields/Field.php @@ -237,22 +237,11 @@ public function get_render_html(): string { $args = [ 'field' => $this, + 'resource' => $this->resource, 'group' => $this->group->get_name( $this->get_slug() ), ]; - if ( $this->resource->is_using_oauth() ) { - ob_start(); - - if ( $this->resource->oauth_requires_license_key() ) { - echo $this->view->render( self::VIEW, $args ); - } - - UplinkNamespace\render_authorize_button( $this->get_slug() ); - - $html = (string) ob_get_clean(); - } else { - $html = $this->view->render( self::VIEW, $args ); - } + $html = $this->view->render( self::VIEW, $args ); /** * Filters the field HTML. diff --git a/src/Uplink/Auth/Auth_Url_Builder.php b/src/Uplink/Auth/Auth_Url_Builder.php index 7e48ed37..7064cf50 100644 --- a/src/Uplink/Auth/Auth_Url_Builder.php +++ b/src/Uplink/Auth/Auth_Url_Builder.php @@ -50,6 +50,13 @@ public function build( string $slug, string $domain = '' ): string { return ''; } + $callback_url = admin_url( $pagenow ); + + // If building the URL in an ajax context, use the referring URL. + if ( wp_parse_url( $pagenow, PHP_URL_PATH ) === 'admin-ajax.php' ) { + $callback_url = wp_get_referer(); + } + $auth_url = $this->auth_url_manager->get( $slug ); if ( ! $auth_url ) { @@ -69,7 +76,7 @@ public function build( string $slug, string $domain = '' ): string { $url = add_query_arg( array_filter( array_merge( $_GET, $args ) ), - admin_url( $pagenow ) + $callback_url ); return sprintf( '%s?%s', diff --git a/src/Uplink/Components/Admin/Authorize_Button_Controller.php b/src/Uplink/Components/Admin/Authorize_Button_Controller.php index 7390e520..27191f5d 100644 --- a/src/Uplink/Components/Admin/Authorize_Button_Controller.php +++ b/src/Uplink/Components/Admin/Authorize_Button_Controller.php @@ -212,6 +212,7 @@ public function render( array $args = [] ): void { 'target' => $target, 'tag' => $tag, 'classes' => $this->classes( $classes ), + 'slug' => $slug, ] ); } diff --git a/src/Uplink/functions.php b/src/Uplink/functions.php index 37f2e56c..bc74f581 100644 --- a/src/Uplink/functions.php +++ b/src/Uplink/functions.php @@ -113,13 +113,15 @@ function is_user_authorized(): bool { * * @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 An optional license key to pass along. * * @return string */ -function build_auth_url( string $slug, string $domain = '' ): string { +function build_auth_url( string $slug, string $domain = '', string $license = ''): string { try { return Config::get_container()->get( Auth_Url_Builder::class ) - ->build( $slug, $domain ); + ->set_license( $license ) + ->build( $slug, $domain, $license ); } catch ( Throwable $e ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( "Error building auth URL: {$e->getMessage()} {$e->getFile()}:{$e->getLine()} {$e->getTraceAsString()}" ); diff --git a/src/assets/css/main.css b/src/assets/css/main.css index 9344e611..4fbb148a 100644 --- a/src/assets/css/main.css +++ b/src/assets/css/main.css @@ -1,24 +1,31 @@ .license-test-results { - margin-top: 10px; + margin-top: 10px; } .key-validity { - display: inline-block; + display: inline-block; } .valid-key { - color: green; + color: green; } .invalid-key { - color: red; + color: red; +} + +.uplink-authorize-container { + display: inline-block; } .uplink-authorize { - margin-bottom: 16px !important; transition: border-color 300ms ease-in-out; } +.uplink-authorize.button { + vertical-align: initial; +} + .uplink-authorize.not-authorized { background: #b8e6bf; border-color: #00a32a; diff --git a/src/assets/js/key-admin.js b/src/assets/js/key-admin.js index cef4a7ba..3470bcbb 100644 --- a/src/assets/js/key-admin.js +++ b/src/assets/js/key-admin.js @@ -1,11 +1,21 @@ ( function( $, obj ) { obj.init = function() { + + $('a.uplink-authorize').on( 'click', function (e) { + if ( $( this ).attr('disabled') === 'disabled' ) { + e.preventDefault(); + } + } ); + $( '.stellarwp-uplink-license-key-field' ).each( function() { var $el = $( this ); + var slug = $el.data( 'plugin-slug' ); var $field = $el.find( 'input[type="text"]' ); + var $oauth = $el.find( `a[data-plugin-slug="${slug}"]`); if ( '' === $field.val().trim() ) { $el.find( '.license-test-results' ).hide(); + obj.disableAuthorizeButton( $oauth ); } obj.validateKey( $el ); @@ -17,13 +27,25 @@ } ); }; + obj.disableAuthorizeButton = function( $button ) { + $button.attr( 'aria-disabled', 'true' ); + $button.attr( 'disabled', 'disabled' ); + } + + obj.enableAuthorizeButton = function( $button ) { + $button.removeAttr( 'aria-disabled' ); + $button.removeAttr( 'disabled' ); + } + obj.validateKey = function( $el ) { const field = $el.find( 'input[type="text"]' ) const action = $el.data( 'action' ); const slug = $el.data( 'plugin-slug' ); + const $oauth = $el.find( `a[data-plugin-slug="${slug}"]`); let $validityMessage = $el.find( '.key-validity' ); if ( '' === field.val().trim() ) { + obj.disableAuthorizeButton( $oauth ); return; } @@ -53,17 +75,27 @@ switch (response.status) { case 1: $validityMessage.addClass('valid-key').removeClass('invalid-key'); + obj.enableAuthorizeButton( $oauth ); + if ( $oauth.hasClass( 'not-authorized' ) ) { + $oauth.attr( 'href', response.auth_url ); + } break; case 2: $validityMessage.addClass('valid-key service-msg'); + obj.enableAuthorizeButton( $oauth ); + if ( $oauth.hasClass( 'not-authorized' ) ) { + $oauth.attr( 'href', response.auth_url ); + } break; default: $validityMessage.addClass('invalid-key').removeClass('valid-key'); + obj.disableAuthorizeButton( $oauth ); break; } }).fail(function(error) { $validityMessage.show(); $validityMessage.html(error.message); + obj.disableAuthorizeButton( $oauth ); }).always(function() { $($el).find('.ajax-loading-license').hide(); }); diff --git a/src/views/admin/authorize-button.php b/src/views/admin/authorize-button.php index 0f5983c1..3eccd3af 100644 --- a/src/views/admin/authorize-button.php +++ b/src/views/admin/authorize-button.php @@ -10,6 +10,7 @@ * @var string $target The link target. * @var string $tag The HTML tag to use for the wrapper. * @var string $classes The CSS classes for the hyperlink. + * @var string $slug The slug of the product the authorize button is for. */ defined( 'ABSPATH' ) || exit; @@ -19,6 +20,7 @@ + data-plugin-slug="" > diff --git a/src/views/admin/fields/field.php b/src/views/admin/fields/field.php index ef5134f9..4eda4190 100644 --- a/src/views/admin/fields/field.php +++ b/src/views/admin/fields/field.php @@ -1,11 +1,14 @@ should_show_label() ) : ?> - - - + + - + + [ 'expiration' => 0, - 'value' => 'https://licensing.stellarwp.com/account-auth?uplink_domain=&uplink_slug=service-oauth-with-license-key-field-1&_uplink_nonce=535281edcd', + 'value' => 'https://licensing.stellarwp.com/account-auth', ] ] ); $collection = $this->container->get( Collection::class ); diff --git a/tests/wpunit/Admin/Fields/__snapshots__/FieldTest__it_should_render_correct_html_for_oauth_resource_with_license_key__0.snapshot.html b/tests/wpunit/Admin/Fields/__snapshots__/FieldTest__it_should_render_correct_html_for_oauth_resource_with_license_key__0.snapshot.html index df61d908..5b395d63 100644 --- a/tests/wpunit/Admin/Fields/__snapshots__/FieldTest__it_should_render_correct_html_for_oauth_resource_with_license_key__0.snapshot.html +++ b/tests/wpunit/Admin/Fields/__snapshots__/FieldTest__it_should_render_correct_html_for_oauth_resource_with_license_key__0.snapshot.html @@ -1,37 +1,38 @@ - - +