Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#39] Auction index #103

Merged
merged 8 commits into from
Dec 19, 2023
97 changes: 88 additions & 9 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public function __construct() {

// Update Bid Product when Auction is updated.
$this->update_bid_product_on_auction_update();

// Set the auction archive page in the main navigation
$this->set_auction_archive_posts_per_page();

// Adds the auction archive page to the pages
$this->add_auction_archive_page();

// Sets a default image
$this->set_default_feature_image();
}

/**
Expand Down Expand Up @@ -160,7 +169,7 @@ private function get_template(): array {
*
* @return void
*/
private function init_rewards_category() : void {
private function init_rewards_category(): void {
add_action(
'init',
function () {
Expand All @@ -187,7 +196,7 @@ public function get_post_type(): string {
*
* @return ?int
*/
public function get_auction_id() : ?int {
public function get_auction_id(): ?int {
$auction_id = is_singular( $this->get_post_type() ) ? get_queried_object_id() : get_the_ID();

if ( ! $auction_id && is_admin() && ! empty( $_GET['post'] ) ) {
Expand All @@ -208,7 +217,7 @@ public function get_auction_id() : ?int {
*
* @return ?int
*/
public function get_rewards_category_id() : ?int {
public function get_rewards_category_id(): ?int {
$rewards_category = get_term_by( 'slug', 'rewards', 'product_cat' );

if ( ! $rewards_category ) {
Expand Down Expand Up @@ -270,8 +279,8 @@ public function set_bid_product_id( int $auction_id, int $bid_product_id ): void
*
* @since 1.0.0
*
* @param string $meta_key
* @param ?int $auction_id
* @param string $meta_key
* @param ?int $auction_id
*
* @return mixed
*/
Expand All @@ -292,7 +301,7 @@ public function get_setting( string $meta_key, int $auction_id = null ): mixed {
*
* @return int
*/
public function get_reward_product_id( int $auction_id = null ) : int {
public function get_reward_product_id( int $auction_id = null ): int {
return intval( $this->get_setting( 'auction_product', $auction_id ) );
}

Expand All @@ -305,7 +314,7 @@ public function get_reward_product_id( int $auction_id = null ) : int {
*
* @return int
*/
public function get_estimated_value( int $auction_id = null ) : int {
public function get_estimated_value( int $auction_id = null ): int {
return intval( $this->get_setting( 'estimated_value', $auction_id ) );
}

Expand Down Expand Up @@ -362,7 +371,7 @@ public function get_bid_increment( int $auction_id = null ): int {
*
* @return int
*/
public function get_starting_bid( int $auction_id = null ) : int {
public function get_starting_bid( int $auction_id = null ): int {
return intval( $this->get_setting( 'starting_bid', $auction_id ) );
}

Expand Down Expand Up @@ -406,7 +415,7 @@ public function get_goal( int $auction_id = null ): int {
*
* @return int
*/
public function get_expected_high_bid( int $auction_id = null ) : int {
public function get_expected_high_bid( int $auction_id = null ): int {
return intval( $this->get_setting( 'expected_high_bid', $auction_id ) );
}

Expand Down Expand Up @@ -454,4 +463,74 @@ function ( int $post_id ) {
}
);
}

/**
* Set the Auction archive page to show nine posts per pagination
*
* @since 1.0.0
*
* @return void
*/
private function set_auction_archive_posts_per_page(): void {
nathan-schmidt-viget marked this conversation as resolved.
Show resolved Hide resolved
add_action(
'pre_get_posts',
function ( $query ) {
nathan-schmidt-viget marked this conversation as resolved.
Show resolved Hide resolved
if ( ! is_admin() && is_post_type_archive( 'gb-auction' ) ) {
nathan-schmidt-viget marked this conversation as resolved.
Show resolved Hide resolved
$query->set( 'posts_per_page', 9 );
}
}
);
}

/**
* Adds the Auction archive page the pages section
*
* @since 1.0.0
*
* @return void
*/
private function add_auction_archive_page(): void {
add_action(
'goodbids_init_site',
function () {
$page = array(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use square brackets instead?

[

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it totally should! We are going to change directions a bit for the navigation so I am going to remove this second and see if we need it in the navigation PR.

'post_type' => 'page',
'post_title' => __( 'Auctions', 'goodbids' ),
'post_status' => 'publish',
'post_author' => 1,
'post_name' => self::ARCHIVE_SLUG,
);

wp_insert_post( $page );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a page here with the same slug as the Archive, have you looked into customizing the Auction Archive Template (Appearance > Editor > Templates > Archive Gb Auction)?

Full disclosure, this is just speculation from me, I've never actually done it yet with FSE, but in theory, I would think we could do it. We can customize the Archive template from the FSE, so we should be able to use a custom template instead of building a new page.

Ping me if you want to chat about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct everything is set up to use Appearance > Editor > Templates > Archive Gb Auction and that is how you edit the Archive page. This is the only way to dynamically add a page to the main navigation as the main nav block pulls from the pages. The downside to this is we need to remove Cart/Account/Shop from the navigation. If we can figure out a way to update the navigation then we don't need this at all.

All of this has been in churn last evening and this morning as I keep finding "solutions" to the navigation, but they don't really fix our bigger problem.

}
);
}


/**
* Set the default feature image for Auction
*
* @since 1.0.0
*
* @return void
*/
private function set_default_feature_image(): void {
add_filter(
'post_thumbnail_html',
function ( string $html, int $post_id ) {
if ( ! is_post_type_archive( $this->get_post_type() ) ) {
return $html;
}

$reward_id = goodbids()->auctions->get_reward_product_id( $post_id );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the update to your Auction page PR, this is a great example of when we would need to pass the ID into the method. 👍

$product = wc_get_product( $reward_id );
$image_html = $product->get_image();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$image_html = $product->get_image();
return $product->get_image();

I can't remember if the sprintf was doing something before, but if we no longer have anything to pass into the $image_html var, this can go ahead and return here.

return sprintf(
$image_html,
);
},
10,
2
);
}
}
12 changes: 6 additions & 6 deletions client-mu-plugins/goodbids/src/classes/Frontend/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ function (): void {
]
);

$sample_pattern = [
'name' => 'sample-pattern',
'path' => GOODBIDS_PLUGIN_PATH . 'views/patterns/sample-pattern.php',
'title' => __( 'GoodBids Sample Pattern', 'goodbids' ),
$auction_archive = [
'name' => 'template-archive-auction',
'path' => GOODBIDS_PLUGIN_PATH . 'views/patterns/template-archive-auction.php',
'title' => __( 'Archive Auction', 'goodbids' ),
'categories' => [ 'goodbids' ],
'keywords' => [ 'example', 'template', 'demo' ],
'keywords' => [ 'non-profit', 'starter', 'archive' ],
'inserter' => true,
];

$this->patterns = apply_filters(
'goodbids_block_patterns',
[
$sample_pattern,
$auction_archive,
]
Comment on lines -58 to 71
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moves this from the theme pattern into the plugin pattern

);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Pattern: Auction Archive
*
* @since 1.0.0
* @package GoodBids
*/
?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
?>
?>

Just need to add a line here.


<!-- wp:group {"tagName":"main","style":{"spacing":{"blockGap":"0","margin":{"top":"0"}}},"layout":{"type":"constrained","justifyContent":"left"}} -->
<main class="wp-block-group" style="margin-top:0">
<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:heading {"level":1,"style":{"typography":{"textTransform":"capitalize"}}} -->
<h1 class="wp-block-heading" style="text-transform:capitalize">
<?php esc_html_e( 'Our Auctions', 'goodbids-nonprofit' ); ?>
</h1>
<!-- /wp:heading -->

<!-- wp:query {"query":{"perPage":9,"pages":0,"offset":"0","postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"align":"wide","layout":{"type":"default"}} -->
<div class="wp-block-query alignwide">
<!-- wp:query-no-results -->
<!-- wp:pattern {"slug":"twentytwentyfour/hidden-no-results"} /-->
<!-- /wp:query-no-results -->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|50","bottom":"var:preset|spacing|50","left":"0","right":"0"},"margin":{"top":"0","bottom":"0"}}},"layout":{"type":"default"}} -->
<div class="wp-block-group" style="margin-top:0;margin-bottom:0;padding-top:var(--wp--preset--spacing--50);padding-right:0;padding-bottom:var(--wp--preset--spacing--50);padding-left:0">

<!-- wp:post-template {"align":"full","style":{"spacing":{"blockGap":"var:preset|spacing|30"}},"layout":{"type":"grid","columnCount":3}} -->

<!-- wp:post-featured-image {"isLink":true,"aspectRatio":"1","style":{"spacing":{"margin":{"bottom":"0"},"padding":{"bottom":"var:preset|spacing|20"}}}} /-->

<!-- wp:group {"style":{"spacing":{"blockGap":"10px","margin":{"top":"var:preset|spacing|20"},"padding":{"top":"0"}}},"layout":{"type":"flex","orientation":"vertical","flexWrap":"nowrap"}} -->
<div class="wp-block-group" style="margin-top:var(--wp--preset--spacing--20);padding-top:0">
<!-- wp:post-title {"isLink":true,"style":{"layout":{"flexSize":"min(2.5rem, 3vw)","selfStretch":"fixed"}},"fontSize":"large"} /-->

<!-- wp:spacer {"height":"0px","style":{"layout":{"flexSize":"min(1.5rem, 3vw)","selfStretch":"fixed"}}} -->
<div style="height:0px" aria-hidden="true" class="wp-block-spacer">
</div>
<!-- /wp:spacer -->
</div>
<!-- /wp:group -->

<!-- /wp:post-template -->

<!-- wp:spacer {"height":"var:preset|spacing|40","style":{"spacing":{"margin":{"top":"0","bottom":"0"}}}} -->
<div style="margin-top:0;margin-bottom:0;height:var(--wp--preset--spacing--40)" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->

<!-- wp:query-pagination {"paginationArrow":"arrow","layout":{"type":"flex","justifyContent":"space-between"}} -->
<!-- wp:query-pagination-previous /-->
<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->

</div>
<!-- /wp:group -->
</div>
<!-- /wp:query -->
</main>
<!-- /wp:group -->
3 changes: 3 additions & 0 deletions themes/goodbids-nonprofit/templates/archive-gb-auction.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:template-part {"slug":"header","area":"header","tagName":"header"} /-->
<!-- wp:pattern {"slug":"goodbids/template-archive-auction"} /-->
<!-- wp:template-part {"slug":"footer","area":"footer","tagName":"footer"} /-->