Skip to content

Commit

Permalink
Add optional domain to pass along with the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctl committed Nov 10, 2023
1 parent 3f50036 commit 9dc39a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/Uplink/Components/Admin/Authorize_Button_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function __construct(
/**
* Renders the authorize-button view.
*
* @param array{slug?: string} $args The Product slug.
* @param array{slug?: string, domain?: string} $args The Product slug and license domain.
*
* @see src/views/admin/authorize-button.php
*
Expand All @@ -81,7 +81,8 @@ public function __construct(
public function render( array $args = [] ): void {
global $pagenow;

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

if ( empty ( $slug ) ) {
throw new InvalidArgumentException( __( 'The Product slug cannot be empty', '%TEXTDOMAIN%' ) );
Expand All @@ -96,7 +97,7 @@ public function render( array $args = [] ): void {
$authenticated = false;
$target = '_blank';
$link_text = __( 'Connect', '%TEXTDOMAIN%' );
$url = $this->build_auth_url();
$url = $this->build_auth_url( $domain );
$classes = [
'uplink-authorize',
'not-authorized',
Expand Down Expand Up @@ -200,7 +201,7 @@ public function render( array $args = [] ): void {
*
* Build the callback URL with the current URL the user is on.
*/
private function build_auth_url(): string {
private function build_auth_url( string $domain ): string {
global $pagenow;

if ( empty( $pagenow ) ) {
Expand All @@ -211,9 +212,10 @@ private function build_auth_url(): string {

return sprintf( '%s?%s',
$this->auth_url,
http_build_query( [
http_build_query( array_filter( [
'uplink_domain' => $domain,
'uplink_callback' => $this->nonce->create_url( $url ),
] )
] ) )
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Uplink/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
* token locally by disconnecting.
*
* @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.
*/
function render_authorize_button( string $slug ): void {
function render_authorize_button( string $slug, string $domain = '' ): void {
try {
Config::get_container()->get( Authorize_Button_Controller::class )
->render( [ 'slug' => $slug ] );
->render( [
'slug' => $slug,
'domain' => $domain,
] );
} catch ( Throwable $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "Unable to render authorize button: {$e->getMessage()} {$e->getFile()}:{$e->getLine()} {$e->getTraceAsString()}" );
Expand Down

0 comments on commit 9dc39a2

Please sign in to comment.