From bcf64c22572f4a178c257e1205a83eacc5ab3ce1 Mon Sep 17 00:00:00 2001 From: Nathan Schmidt <91974372+nathan-schmidt-viget@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:54:04 -0700 Subject: [PATCH] [#495] pulling in the created pages into the new nav --- .../blocks/nonprofit-navigation/block.json | 13 - .../blocks/nonprofit-navigation/block.php | 23 -- .../blocks/nonprofit-navigation/render.php | 15 - .../src/classes/Frontend/Patterns.php | 12 - .../goodbids/src/classes/Network/Sites.php | 303 ++++++++++-------- .../views/parts/nonprofit-navigation.php | 14 + .../views/patterns/nonprofit-navigation.php | 17 - .../patterns/header-nonprofit.php | 3 +- 8 files changed, 188 insertions(+), 212 deletions(-) delete mode 100644 client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.json delete mode 100644 client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.php delete mode 100644 client-mu-plugins/goodbids/blocks/nonprofit-navigation/render.php create mode 100644 client-mu-plugins/goodbids/views/parts/nonprofit-navigation.php delete mode 100644 client-mu-plugins/goodbids/views/patterns/nonprofit-navigation.php diff --git a/client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.json b/client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.json deleted file mode 100644 index 9a90082de..000000000 --- a/client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "nonprofit-navigation", - "title": "Nonprofit Navigation", - "description": "Displays the Nonprofit Navigation", - "icon": "admin-links", - "textdomain": "goodbids", - "acf": { - "mode": "preview" - }, - "supports": { - "jsx": false - } -} diff --git a/client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.php b/client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.php deleted file mode 100644 index 364414ffb..000000000 --- a/client-mu-plugins/goodbids/blocks/nonprofit-navigation/block.php +++ /dev/null @@ -1,23 +0,0 @@ - - - - - diff --git a/client-mu-plugins/goodbids/src/classes/Frontend/Patterns.php b/client-mu-plugins/goodbids/src/classes/Frontend/Patterns.php index ab9ad8663..9dd6937f4 100644 --- a/client-mu-plugins/goodbids/src/classes/Frontend/Patterns.php +++ b/client-mu-plugins/goodbids/src/classes/Frontend/Patterns.php @@ -108,17 +108,6 @@ function (): void { 'inserter' => true, ]; - $nonprofit_navigation = [ - 'name' => 'nonprofit-navigation', - 'path' => goodbids()->get_view_path( 'patterns/nonprofit-navigation.php' ), - 'title' => __( 'Nonprofit Navigation', 'goodbids' ), - 'description' => _x( 'Default Nonprofit Navigation', 'Block pattern description', 'goodbids' ), - 'categories' => [ 'goodbids' ], - 'keywords' => [ 'navigation' ], - 'source' => 'plugin', - 'inserter' => true, - ]; - $section_sidebar_chapters = [ 'name' => 'section-sidebar-chapters', 'path' => goodbids()->get_view_path( 'patterns/section-sidebar-chapters.php' ), @@ -178,7 +167,6 @@ function (): void { $hero_banner, $logo_grid, $nonprofit_interest_form, - $nonprofit_navigation, $section_sidebar_chapters, $template_about, $template_auction, diff --git a/client-mu-plugins/goodbids/src/classes/Network/Sites.php b/client-mu-plugins/goodbids/src/classes/Network/Sites.php index 378d2a102..8134675a4 100644 --- a/client-mu-plugins/goodbids/src/classes/Network/Sites.php +++ b/client-mu-plugins/goodbids/src/classes/Network/Sites.php @@ -17,6 +17,7 @@ use WP_Block_Type_Registry; use WP_Post; use WP_Site; +use WP_Query; /** * Network Sites Class @@ -31,6 +32,18 @@ class Sites { */ const ALL_AUCTIONS_TRANSIENT = '_goodbids_all_auctions'; + /** + * @since 1.0.0 + * @var array + */ + const ABOUT_OPTION = 'gb_about_page'; + + /** + * @since 1.0.0 + * @var array + */ + const AUCTIONS_OPTION = 'gb_auctions_page'; + /** * @since 1.0.0 */ @@ -63,6 +76,8 @@ public function __construct() { // Refresh transients when Auctions change status. $this->maybe_clear_transients(); + + $this->set_nonprofit_navigation(); } /** @@ -101,12 +116,12 @@ private function redirect_on_save_new_site(): void { add_action( 'wp_initialize_site', /** - * @param WP_Site $new_site New site object. - */ + * @param WP_Site $new_site New site object. + */ function ( WP_Site $new_site ) { global $pagenow; - if ( 'site-new.php' !== $pagenow || empty( $_POST ) ) { // phpcs:ignore + if ( 'site-new.php' !== $pagenow || empty( $_POST ) ) { // phpcs:ignore return; } @@ -258,13 +273,13 @@ public function loop( callable|array $callback, array $site_args = [] ): array { } return collect( get_sites( $site_args ) ) - ->flatMap( - fn( WP_Site $site ) => $this->swap( - fn ( int $site_id ) => call_user_func( $callback, $site_id ), - $site->blog_id - ) + ->flatMap( + fn( WP_Site $site ) => $this->swap( + fn ( int $site_id ) => call_user_func( $callback, $site_id ), + $site->blog_id ) - ->all(); + ) + ->all(); } /** @@ -392,6 +407,8 @@ function (): void { if ( is_wp_error( $about_id ) ) { Log::error( $about_id->get_error_message() ); } + + update_option( self::ABOUT_OPTION, $about_id ); } ); } @@ -432,6 +449,8 @@ function (): void { if ( is_wp_error( $auctions_id ) ) { Log::error( $auctions_id->get_error_message() ); } + + update_option( self::AUCTIONS_OPTION, $auctions_id ); } ); } @@ -542,40 +561,40 @@ public function get_all_auctions( array $query_args = [] ): array { $auctions = $this->loop( fn ( int $site_id ) => collect( ( goodbids()->auctions->get_all( $query_args ) )->posts ) - ->map( - fn ( int $post_id ) => [ - 'post_id' => $post_id, - 'site_id' => $site_id, - ] - ) - ->filter( - fn ( array $auction_data ) => goodbids()->sites->swap( - function () use ( $auction_data ) { - $auction = goodbids()->auctions->get( $auction_data['post_id'] ); - return ! $auction->has_ended(); - }, - $auction_data['site_id'] + ->map( + fn ( int $post_id ) => [ + 'post_id' => $post_id, + 'site_id' => $site_id, + ] ) - ) - ->sortByDesc( - fn ( array $auction_data ) => [ - 'bid_count' => $this->swap( - function () use ( $auction_data ) { - $auction = goodbids()->auctions->get( $auction_data['post_id'] ); - return $auction->get_bid_count(); - }, - $auction_data['site_id'] - ), - 'total_raised' => $this->swap( + ->filter( + fn ( array $auction_data ) => goodbids()->sites->swap( function () use ( $auction_data ) { - $auction = goodbids()->auctions->get( $auction_data['post_id'] ); - return $auction->get_total_raised(); - }, + $auction = goodbids()->auctions->get( $auction_data['post_id'] ); + return ! $auction->has_ended(); + }, $auction_data['site_id'] - ), - ] - ) - ->all() + ) + ) + ->sortByDesc( + fn ( array $auction_data ) => [ + 'bid_count' => $this->swap( + function () use ( $auction_data ) { + $auction = goodbids()->auctions->get( $auction_data['post_id'] ); + return $auction->get_bid_count(); + }, + $auction_data['site_id'] + ), + 'total_raised' => $this->swap( + function () use ( $auction_data ) { + $auction = goodbids()->auctions->get( $auction_data['post_id'] ); + return $auction->get_total_raised(); + }, + $auction_data['site_id'] + ), + ] + ) + ->all() ); if ( empty( $query_args ) ) { @@ -597,9 +616,9 @@ function () use ( $auction_data ) { */ public function get_featured_auctions( array $query_args = [] ): array { return collect( $this->get_all_auctions( $query_args ) ) - ->slice( 0, 3 ) - ->values() - ->all(); + ->slice( 0, 3 ) + ->values() + ->all(); } /** @@ -746,15 +765,15 @@ public function get_user_total_bids( ?int $user_id = null ): int { */ public function get_user_total_donated( ?int $user_id = null ): float { return collect( $this->get_user_bid_orders( $user_id, [ 'processing', 'completed' ] ) ) - ->sum( - fn ( array $goodbids_order ) => $this->swap( - function () use ( $goodbids_order ) { - $order = wc_get_order( $goodbids_order['order_id'] ); - return $order->get_total(); - }, - $goodbids_order['site_id'] - ) - ); + ->sum( + fn ( array $goodbids_order ) => $this->swap( + function () use ( $goodbids_order ) { + $order = wc_get_order( $goodbids_order['order_id'] ); + return $order->get_total(); + }, + $goodbids_order['site_id'] + ) + ); } /** @@ -768,8 +787,8 @@ function () use ( $goodbids_order ) { */ public function get_user_nonprofits_supported( ?int $user_id = null ): int { return collect( $this->get_user_bid_orders( $user_id, [ 'processing', 'completed' ] ) ) - ->groupBy( 'site_id' ) - ->count(); + ->groupBy( 'site_id' ) + ->count(); } /** @@ -784,25 +803,25 @@ public function get_user_nonprofits_supported( ?int $user_id = null ): int { */ public function get_user_participating_auctions( ?int $user_id = null, ?int $limit = null ): array { $participating = collect( $this->get_user_bid_orders( $user_id, [ 'processing', 'completed' ] ) ) - ->map( - function ( array $item ) { - return $this->swap( - function () use ( &$item ) { - $item['auction_id'] = goodbids()->woocommerce->orders->get_auction_id( $item['order_id'] ); - return $item; - }, - $item['site_id'] - ); - } - ) - ->groupBy( 'auction_id' ) - ->map( - fn( Collection $group ) => [ - 'site_id' => $group->first()['site_id'], - 'auction_id' => $group->first()['auction_id'], - 'count' => $group->count(), - ] - ); + ->map( + function ( array $item ) { + return $this->swap( + function () use ( &$item ) { + $item['auction_id'] = goodbids()->woocommerce->orders->get_auction_id( $item['order_id'] ); + return $item; + }, + $item['site_id'] + ); + } + ) + ->groupBy( 'auction_id' ) + ->map( + fn( Collection $group ) => [ + 'site_id' => $group->first()['site_id'], + 'auction_id' => $group->first()['auction_id'], + 'count' => $group->count(), + ] + ); if ( $limit ) { $participating = $participating->slice( 0, $limit ); @@ -820,16 +839,16 @@ function () use ( &$item ) { */ public function get_user_live_participating_auctions(): array { return collect( $this->get_user_participating_auctions() ) - ->filter( - fn( array $item ) => $this->swap( - function () use ( &$item ) { - $auction = goodbids()->auctions->get( $item['auction_id'] ); - return Auction::STATUS_LIVE === $auction->get_status(); - }, - $item['site_id'] - ) + ->filter( + fn( array $item ) => $this->swap( + function () use ( &$item ) { + $auction = goodbids()->auctions->get( $item['auction_id'] ); + return Auction::STATUS_LIVE === $auction->get_status(); + }, + $item['site_id'] ) - ->all(); + ) + ->all(); } /** @@ -843,18 +862,18 @@ function () use ( &$item ) { */ public function get_user_auctions_won( ?int $user_id = null ): array { return collect( $this->get_user_participating_auctions( $user_id ) ) - ->filter( - function ( $auction_data ) { - return $this->swap( - function () use ( $auction_data ) { - $auction = goodbids()->auctions->get( $auction_data['auction_id'] ); - return $auction->is_current_user_winner(); - }, - $auction_data['site_id'] - ); - } - ) - ->all(); + ->filter( + function ( $auction_data ) { + return $this->swap( + function () use ( $auction_data ) { + $auction = goodbids()->auctions->get( $auction_data['auction_id'] ); + return $auction->is_current_user_winner(); + }, + $auction_data['site_id'] + ); + } + ) + ->all(); } /** @@ -933,30 +952,30 @@ function ( $site_id ) use ( &$auctions, $user_id ) { // Filter by started and not ended and sort by end date return collect( $auctions ) - ->unique( - function ( $auction_data ) { - return $auction_data['site_id'] . '|' . $auction_data['post_id']; - } - ) - ->filter( - fn ( array $auction_data ) => goodbids()->sites->swap( - function () use ( $auction_data ) { - $auction = goodbids()->auctions->get( $auction_data['post_id'] ); - return $auction->has_started() && ! $auction->has_ended(); - }, - $auction_data['site_id'] - ) + ->unique( + function ( $auction_data ) { + return $auction_data['site_id'] . '|' . $auction_data['post_id']; + } + ) + ->filter( + fn ( array $auction_data ) => goodbids()->sites->swap( + function () use ( $auction_data ) { + $auction = goodbids()->auctions->get( $auction_data['post_id'] ); + return $auction->has_started() && ! $auction->has_ended(); + }, + $auction_data['site_id'] ) - ->sortBy( - fn( array $auction_data ) => goodbids()->sites->swap( - function () use ( $auction_data ) { - $auction = goodbids()->auctions->get( $auction_data['post_id'] ); - return $auction->get_end_date_time(); - }, - $auction_data['site_id'] - ) + ) + ->sortBy( + fn( array $auction_data ) => goodbids()->sites->swap( + function () use ( $auction_data ) { + $auction = goodbids()->auctions->get( $auction_data['post_id'] ); + return $auction->get_end_date_time(); + }, + $auction_data['site_id'] ) - ->all(); + ) + ->all(); } /** @@ -1041,24 +1060,46 @@ private function get_page_path( string $path ): ?WP_Post { /** - * Return the nonprofit navigation + * Set the nonprofit navigation * - * @return array + * @return void * * @since 1.0.0 */ - public function get_nonprofit_navigation(): array { - return [ - [ - 'label' => 'Explore Auctions', - 'ID' => 4, - 'url' => '/explore-auctions', - ], - [ - 'label' => 'About GOODBIDS', - 'ID' => 3, - 'url' => '/about', - ], - ]; + public function set_nonprofit_navigation(): void { + add_action( + 'goodbids_nonprofit_verified', + function ( int $site_id ): void { + + $about_id = get_option( self::ABOUT_OPTION ); + $auctions_id = get_option( self::AUCTIONS_OPTION ); + $wp_navigation = new WP_Query( + [ + 'post_type' => 'wp_navigation', + 'post_status' => [ 'publish' ], + ] + ); + $nav_links = [ + get_post( $about_id ), + get_post( $auctions_id ), + ]; + + + ob_start(); + goodbids()->load_view( 'parts/nonprofit-navigation.php', compact( 'nav_links' ) ); + + $navigation_content = [ + 'ID' => $wp_navigation->posts[0]->ID, + 'post_content' => ob_get_clean(), + ]; + + // Update the post into the database + wp_update_post( $navigation_content ); + + if ( is_wp_error( $navigation_content ) ) { + Log::error( $navigation_content->get_error_message() ); + } + } + ); } } diff --git a/client-mu-plugins/goodbids/views/parts/nonprofit-navigation.php b/client-mu-plugins/goodbids/views/parts/nonprofit-navigation.php new file mode 100644 index 000000000..d51ac79ea --- /dev/null +++ b/client-mu-plugins/goodbids/views/parts/nonprofit-navigation.php @@ -0,0 +1,14 @@ + + + + + diff --git a/client-mu-plugins/goodbids/views/patterns/nonprofit-navigation.php b/client-mu-plugins/goodbids/views/patterns/nonprofit-navigation.php deleted file mode 100644 index ceaf0408e..000000000 --- a/client-mu-plugins/goodbids/views/patterns/nonprofit-navigation.php +++ /dev/null @@ -1,17 +0,0 @@ -sites->get_nonprofit_navigation(); -?> - - - - - - - diff --git a/themes/goodbids-nonprofit/patterns/header-nonprofit.php b/themes/goodbids-nonprofit/patterns/header-nonprofit.php index 60cfb564e..c62a1e31a 100644 --- a/themes/goodbids-nonprofit/patterns/header-nonprofit.php +++ b/themes/goodbids-nonprofit/patterns/header-nonprofit.php @@ -21,7 +21,8 @@
- + +