Skip to content

Commit

Permalink
[#567] Setup Guide Tweaks / Publish Endpoint (#587)
Browse files Browse the repository at this point in the history
* [#555] Another Stripe Onboarding Redirect Handler

* [#555] FIx a Stripe error trying to trim null values.

* [#555] Prevent onboarding from being updated again.

* [#567] Adjust the site status value via REST API

* [#567] Updated vars sent to JS

* [#567] Removed unused JS Vars

* [#567] Changed to local var.

* [#567] JS Vars (#589)

- Adds JS variables for site id

---------

Co-authored-by: Nick <[email protected]>
  • Loading branch information
bd-viget and nick-telsan authored Mar 6, 2024
1 parent 3a1da1a commit f1e1756
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 65 deletions.
3 changes: 1 addition & 2 deletions client-mu-plugins/goodbids/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ declare const gbAuctionWizard: PHPVariables;
// These are only defined for the nonprofit setup page
declare const gbNonprofitSetupGuide: {
appID: string;
siteId: number;
siteStatus: string;
siteStatusOptions: ['pending', 'live', 'inactive'];
ajaxUrl: string;
optionsGeneralURL: string;
createWooCommerceURL: string;
setUpPaymentURL: string;
configureShippingURL: string;
jetpackURL: string;
akismetURL: string;
Expand Down
14 changes: 12 additions & 2 deletions client-mu-plugins/goodbids/src/classes/Network/API/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

defined( 'ABSPATH' ) || exit;

use GoodBids\Network\Nonprofit;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
Expand Down Expand Up @@ -75,13 +76,22 @@ public function register_routes(): void {
* @return WP_Error|WP_REST_Response
*/
public function publish_site( WP_REST_Request $request ): WP_Error|WP_REST_Response {
$site_id = $request->get_param( 'site_id' );
$site_id = intval( $request->get_param( 'site_id' ) );

if ( ! $site_id ) {
return new WP_Error( 'goodbids_missing_required_parameters', __( 'The required `site_id` parameter was not found.', 'goodbids' ) );
}

// TODO: If site not found, return a 404 error.
$nonprofit = new Nonprofit( $site_id );

if ( ! $nonprofit->is_valid() ) {
return new WP_Error( 'goodbids_invalid_site', __( 'The provided `site_id` does not belong to a valid site.', 'goodbids' ) );
}

// Make it live!
if ( ! $nonprofit->set_status( Nonprofit::STATUS_LIVE ) ) {
return new WP_Error( 'goodbids_status_update_failed', __( 'There was a problem adjusting the Site Status.', 'goodbids' ) );
}

return new WP_REST_Response( $site_id, 200 );
}
Expand Down
44 changes: 29 additions & 15 deletions client-mu-plugins/goodbids/src/classes/Network/Nonprofit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use GoodBids\Auctions\Auction;
use GoodBids\Nonprofits\Invoice;
use GoodBids\Nonprofits\Verification;
use WP_Site;

/**
Expand Down Expand Up @@ -65,6 +66,17 @@ public function get_id(): int {
return $this->site_id;
}

/**
* Check to see if the site is valid.
*
* @since 1.0.0
*
* @return bool
*/
public function is_valid(): bool {
return (bool) $this->site;
}

/**
* Get the Site URL
*
Expand Down Expand Up @@ -115,6 +127,23 @@ public function get_status(): string {
return goodbids()->verification->get_nonprofit_data( $this->get_id(), 'status' );
}

/**
* Update the Site's Status
*
* @since 1.0.0
*
* @param string $status
*
* @return bool|int
*/
public function set_status( string $status ): bool|int {
if ( ! in_array( $status, goodbids()->network->nonprofits->get_site_status_options(), true ) ) {
return false;
}

return update_site_meta( $this->get_id(), Verification::STATUS_OPTION, $status );
}

/**
* Get Nonprofit Site Standing
*
Expand Down Expand Up @@ -251,19 +280,4 @@ public function get_registered_date( string $format = 'n/j/Y' ): string {
public function is_verified(): bool {
return goodbids()->verification->is_verified( $this->get_id() );
}

/**
* Returns Site Status options
*
* @since 1.0.0
*
* @return bool
*/
public function get_site_status_options(): array {
return [
self::STATUS_PENDING,
self::STATUS_LIVE,
self::STATUS_INACTIVE,
];
}
}
21 changes: 8 additions & 13 deletions client-mu-plugins/goodbids/src/classes/Network/Nonprofits.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,17 @@ public function is_onboarded( ?int $site_id = null ): bool {
}

/**
* Check if onboarding is completed.
* Returns Site Status options
*
* @since 1.0.0
*
* @param ?int $site_id
*
* @return bool
* @return array
*/
public function is_onboarded( ?int $site_id = null ): bool {
if ( ! $site_id ) {
$site_id = get_current_blog_id();
}

return goodbids()->sites->swap(
fn () => boolval( get_option( 'goodbids_onboarded' ) ),
$site_id
);
public function get_site_status_options(): array {
return [
Nonprofit::STATUS_PENDING,
Nonprofit::STATUS_LIVE,
Nonprofit::STATUS_INACTIVE,
];
}
}
61 changes: 34 additions & 27 deletions client-mu-plugins/goodbids/src/classes/Nonprofits/Guide.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function __construct() {
return;
}

$this->nonprofit = new Nonprofit( get_current_blog_id() );

// Remove any admin notices for this page.
$this->disable_admin_notices();

Expand Down Expand Up @@ -167,32 +165,41 @@ function( array $dependencies ): array {
* @return array
*/
private function get_js_vars(): array {
$nonprofit = new Nonprofit( get_current_blog_id() );

return [
'appID' => self::PAGE_SLUG,
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'optionsGeneralURL' => admin_url( 'options-general.php' ),
'createWooCommerceURL' => admin_url( 'admin.php?page=wc-admin&path=/setup-wizard&step=skip-guided-setup' ),
'setUpPaymentURL' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ),
'configureShippingURL' => admin_url( 'admin.php?page=wc-settings&tab=shipping' ),
'jetpackURL' => admin_url( 'admin.php?page=jetpack#/dashboard' ),
'akismetURL' => admin_url( 'admin.php?page=akismet-key-config' ),
'woocommerceSettingsURL' => admin_url( 'admin.php?page=wc-settings&tab=general' ),
'styleURL' => admin_url( 'site-editor.php?path=/wp_global_styles' ),
'updateLogoURL' => admin_url( 'site-editor.php?postType=wp_template_part&postId=goodbids-nonprofit//header&categoryId=header&categoryType=wp_template_part' ),
'customizeHomepageURL' => admin_url( 'site-editor.php?postType=wp_template_part&postId=goodbids-nonprofit//header&categoryId=header&categoryType=wp_template_part' ),
'pagesURL' => admin_url( 'edit.php?post_type=page' ),
'patternsURL' => admin_url( 'site-editor.php?path=/patterns' ),
'auctionWizardURL' => admin_url( Wizard::BASE_URL . goodbids()->auctions->get_post_type() . '&page=' . Wizard::PAGE_SLUG ),
'addUsersURL' => admin_url( 'user-new.php' ),
'accessibilityCheckerURL' => admin_url( 'admin.php?page=accessibility_checker' ),
'homeURL' => home_url(),
'auctionsURL' => admin_url( 'edit.php?post_type=' . goodbids()->auctions->get_post_type() ),
'orderMetricsURL' => admin_url( 'admin.php?page=wc-admin&path=/analytics/categories' ),
'revenueMetricsURL' => admin_url( 'admin.php?page=wc-admin&path=/analytics/revenue&chart=net_revenue&orderby=net_revenue' ),
'invoicesURL' => admin_url( 'edit.php?post_type=' . goodbids()->invoices->get_post_type() ),
'commentsURL' => admin_url( 'edit-comments.php' ),
'siteStatus' => $this->nonprofit->get_status(),
'siteStatusOptions' => $this->nonprofit->get_site_status_options(),
'appID' => self::PAGE_SLUG,
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'homeURL' => home_url(),

'optionsGeneralURL' => admin_url( 'options-general.php' ),
'commentsURL' => admin_url( 'edit-comments.php' ),
'jetpackURL' => admin_url( 'admin.php?page=jetpack#/dashboard' ),
'akismetURL' => admin_url( 'admin.php?page=akismet-key-config' ),

'woocommerceSettingsURL' => admin_url( 'admin.php?page=wc-settings&tab=general' ),
'configureShippingURL' => admin_url( 'admin.php?page=wc-settings&tab=shipping' ),
'orderMetricsURL' => admin_url( 'admin.php?page=wc-admin&path=/analytics/categories' ),
'revenueMetricsURL' => admin_url( 'admin.php?page=wc-admin&path=/analytics/revenue&chart=net_revenue&orderby=net_revenue' ),

'styleURL' => admin_url( 'site-editor.php?path=/wp_global_styles' ),
'updateLogoURL' => admin_url( 'site-editor.php?postType=wp_template_part&postId=goodbids-nonprofit//header&categoryId=header&categoryType=wp_template_part' ),
'customizeHomepageURL' => admin_url( 'site-editor.php?postType=wp_template_part&postId=goodbids-nonprofit//header&categoryId=header&categoryType=wp_template_part' ),

'pagesURL' => admin_url( 'edit.php?post_type=page' ),
'patternsURL' => admin_url( 'site-editor.php?path=/patterns' ),
'addUsersURL' => admin_url( 'user-new.php' ),

'accessibilityCheckerLicenseURL' => admin_url( 'admin.php?page=accessibility_checker_settings&tab=license' ),
'accessibilityCheckerURL' => admin_url( 'admin.php?page=accessibility_checker' ),

'auctionWizardURL' => admin_url( Wizard::BASE_URL . goodbids()->auctions->get_post_type() . '&page=' . Wizard::PAGE_SLUG ),
'auctionsURL' => admin_url( 'edit.php?post_type=' . goodbids()->auctions->get_post_type() ),
'invoicesURL' => admin_url( 'edit.php?post_type=' . goodbids()->invoices->get_post_type() ),

'siteId' => $nonprofit->get_id(),
'siteStatus' => $nonprofit->get_status(),
'siteStatusOptions' => goodbids()->network->nonprofits->get_site_status_options(),
];
}

Expand Down
29 changes: 26 additions & 3 deletions client-mu-plugins/goodbids/src/classes/Nonprofits/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ function () {
?>
<script>
jQuery( function( $ ) {
const gbOnboardingInterval = setInterval(
const gbStripeCloseInterval = setInterval(
function () {
const $button = $( '.components-modal__header button' );

Expand All @@ -690,6 +690,26 @@ function ( e ) {
},
100
);

let gbStripeModalOpened = false;
const gbStripeModalInterval = setInterval(
function () {
const $modal = $( '.wcstripe-confirmation-modal' );

if ( $modal.length ) {
if ( ! gbStripeModalOpened ) { // Modal was opened.
gbStripeModalOpened = true;
}
} else {
if ( gbStripeModalOpened ) { // Modal was closed automatically.
clearInterval( gbStripeModalInterval );
window.location.href = '<?php echo esc_js( $redirect ); ?>';
return false;
}
}
},
100
);
} );
</script>
<?php
Expand Down Expand Up @@ -750,10 +770,13 @@ private function mark_onboarding_completed(): void {

delete_transient( self::STEP_TRANSIENT );
delete_transient( self::REDIRECT_TRANSIENT );
update_option( 'goodbids_onboarded', current_time( 'mysql' ) );

$redirect = remove_query_arg( self::DONE_ONBOARDING_PARAM );
// Make sure Onboarding isn't already marked as completed.
if ( ! goodbids()->network->nonprofits->is_onboarded() ) {
update_option( 'goodbids_onboarded', current_time( 'mysql' ) );
}

$redirect = remove_query_arg( self::DONE_ONBOARDING_PARAM );
wp_safe_redirect( $redirect );
exit;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class Verification {
*/
const OPTION_SLUG = 'gbnp';

/**
* @since 1.0.0
* @var string
*/
const STATUS_OPTION = 'status';

/**
* Nonprofit custom fields
*
Expand Down Expand Up @@ -280,7 +286,7 @@ function ( $fields ): array {
$new_fields[ $key ] = $field;

// Insert after Status.
if ( 'status' === $key ) {
if ( self::STATUS_OPTION === $key ) {
$new_fields['verification'] = [
'label' => __( 'Verified', 'goodbids' ),
'type' => 'toggle',
Expand Down Expand Up @@ -326,7 +332,7 @@ private function init_custom_fields(): void {
'placeholder' => 'https://',
'required' => true,
],
'status' => [
self::STATUS_OPTION => [
'label' => __( 'Site Status', 'goodbids' ),
'type' => 'select',
'default' => Nonprofit::STATUS_PENDING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use GoodBids\Plugins\WooCommerce\Emails\AuctionSummaryAdmin;
use GoodBids\Plugins\WooCommerce\Emails\AuctionWinnerConfirmation;
use GoodBids\Plugins\WooCommerce\Orders;
use GoodBids\Plugins\WooCommerce\Stripe;
use GoodBids\Plugins\WooCommerce\Taxes;
use WC_Email;
use WC_Product;
Expand Down Expand Up @@ -120,6 +121,9 @@ public function __construct() {
$this->checkout = new Checkout();
$this->taxes = new Taxes();

// Some Stripe Tweaks.
new Stripe();

// Init API Endpoints.
$this->setup_api_endpoints();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* WooCommerce Stripe Tweaks
*
* @since 1.0.0
* @package GoodBids
*/

namespace GoodBids\Plugins\WooCommerce;

/**
* Stripe Tweaks
*
* @since 1.0.0
*/
class Stripe {

/**
* Constructor
*
* @since 1.0.0
*/
public function __construct() {
// Change nulls to empty strings.
$this->clear_stripe_nulls();
}

/**
* Fix an error where nulls are trimmed.
*
* @since 1.0.0
*
* @return void
*/
private function clear_stripe_nulls(): void {
$clear_nulls = function ( array $settings ) : array {
foreach ( $settings as $key => $value ) {
if ( is_null( $value ) ) {
$settings[ $key ] = '';
}
}
return $settings;
};

add_filter( 'option_woocommerce_stripe_settings', $clear_nulls, 50 );
add_filter( 'default_option_woocommerce_stripe_settings', $clear_nulls, 50 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function Pending({ manuallySetToLive }: PendingProps) {
const publishSite = usePublishSite();

const handlePublishSite = () => {
publishSite.mutate({ site_id: 1 });
publishSite.mutate({ site_id: gbNonprofitSetupGuide.siteId });
};

return (
Expand Down

0 comments on commit f1e1756

Please sign in to comment.