Skip to content

Commit

Permalink
User Authentication & ACF Blocks (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
bd-viget authored Dec 15, 2023
1 parent 019d699 commit 30d901a
Show file tree
Hide file tree
Showing 17 changed files with 804 additions and 35 deletions.
16 changes: 16 additions & 0 deletions client-mu-plugins/goodbids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ Returns the Auction's Goal value. If `$auction_id` is not provided, the current
`goodbids()->auctions->get_expected_high_bid( int $auction_id )`
Returns the Auction's Expected High Bid value. If `$auction_id` is not provided, the current post ID will be used.

### ACF Block Functions

`goodbids()->acf->blocks()->get_all_blocks()`
Get all custom registered blocks.

`goodbids()->acf->blocks()->get_block( string $block_name )`
Get block array by block name.

`goodbids()->acf->blocks()->block_attr()`
_Use the global `block_attr()` helper function instead._ This will render the block attributes for the current block.

`goodbids()->acf->blocks()->get_block_location( string $block_name, string $return )`
Get the location of a block. Return values can be: "directory" (Default) or "json" (Returns the path to block.json. _Can also be found in the `path` key of the block array._

`goodbids()->acf->blocks()->get_block_locations()`
Get all directories where blocks can be found.
14 changes: 14 additions & 0 deletions client-mu-plugins/goodbids/blocks/authentication/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "authentication",
"title": "User Authentication Form",
"description": "Displays a user login and registration form for GoodBids.",
"icon": "feedback",
"category": "goodbids",
"keywords": ["custom", "sign", "up", "register", "registration", "form", "users", "authentication"],
"acf": {
"mode": "preview"
},
"supports": {
"jsx": false
}
}
21 changes: 21 additions & 0 deletions client-mu-plugins/goodbids/blocks/authentication/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Block: Authentication
*
* @global array $block
*
* @since 1.0.0
* @package GoodBids
*/

if ( is_admin() ) :
printf(
'<p style="text-align: center;">%s</p>',
esc_html__( 'This will render the Login and Registration form if the user is currently not signed in.', 'goodbids' )
);
return;
endif;
?>
<section <?php block_attr( $block ); ?>>
<?php echo do_shortcode( '[woocommerce_my_account]' ); ?>
</section>
1 change: 1 addition & 0 deletions client-mu-plugins/goodbids/blocks/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php // Silence is golden
4 changes: 2 additions & 2 deletions client-mu-plugins/goodbids/goodbids.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
/**
* Plugin Function Shortcode.
*
* @return \GoodBids\Core|null
* @return ?\GoodBids\Core
*/
function goodbids() {
function goodbids() : ?\GoodBids\Core {
if ( class_exists( '\GoodBids\Core' ) ) {
return \GoodBids\Core::get_instance();
}
Expand Down
20 changes: 10 additions & 10 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct() {
*
* @return void
*/
private function register_post_type() : void {
private function register_post_type(): void {
add_action(
'init',
function () {
Expand Down Expand Up @@ -157,7 +157,7 @@ function () {
*
* @return string
*/
public function get_post_type() : string {
public function get_post_type(): string {
return self::POST_TYPE;
}

Expand All @@ -174,7 +174,7 @@ public function get_rewards_category_id() : ?int {
if ( ! $rewards_category ) {
$rewards_category = wp_insert_term( 'Rewards', 'product_cat' );

if ( ! $rewards_category ) {
if ( is_wp_error( $rewards_category ) ) {
// TODO: Log error.
return null;
}
Expand All @@ -194,7 +194,7 @@ public function get_rewards_category_id() : ?int {
*
* @return bool
*/
public function has_bid_product( int $auction_id ) : bool {
public function has_bid_product( int $auction_id ): bool {
return boolval( $this->get_bid_product_id( $auction_id ) );
}

Expand All @@ -207,7 +207,7 @@ public function has_bid_product( int $auction_id ) : bool {
*
* @return int
*/
public function get_bid_product_id( int $auction_id ) : int {
public function get_bid_product_id( int $auction_id ): int {
return intval( get_post_meta( $auction_id, Bids::AUCTION_BID_META_KEY, true ) );
}

Expand All @@ -221,7 +221,7 @@ public function get_bid_product_id( int $auction_id ) : int {
*
* @return void
*/
public function set_bid_product_id( int $auction_id, int $bid_product_id ) : void {
public function set_bid_product_id( int $auction_id, int $bid_product_id ): void {
update_post_meta( $auction_id, Bids::AUCTION_BID_META_KEY, $bid_product_id );
}

Expand All @@ -235,7 +235,7 @@ public function set_bid_product_id( int $auction_id, int $bid_product_id ) : voi
*
* @return mixed
*/
public function get_setting( string $meta_key, int $auction_id = null ) : mixed {
public function get_setting( string $meta_key, int $auction_id = null ): mixed {
if ( ! $auction_id ) {
$auction_id = get_the_ID();
}
Expand Down Expand Up @@ -278,7 +278,7 @@ public function get_estimated_value( int $auction_id = null ) : int {
*
* @return string
*/
public function get_start_date_time( int $auction_id = null ) : string {
public function get_start_date_time( int $auction_id = null ): string {
return $this->get_setting( 'auction_start', $auction_id );
}

Expand Down Expand Up @@ -309,7 +309,7 @@ public function has_started( int $auction_id = null ): bool {
*
* @return int
*/
public function get_bid_increment( int $auction_id = null ) : int {
public function get_bid_increment( int $auction_id = null ): int {
return intval( $this->get_setting( 'bid_increment', $auction_id ) );
}

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

Expand Down
28 changes: 24 additions & 4 deletions client-mu-plugins/goodbids/src/classes/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use GoodBids\Auctions\Auctions;
use GoodBids\Network\Sites;
use GoodBids\Plugins\ACF;
use GoodBids\Plugins\WooCommerce;

/**
* Core Class
Expand Down Expand Up @@ -60,6 +61,12 @@ class Core {
*/
public Auctions $auctions;

/**
* @since 1.0.0
* @var WooCommerce
*/
public WooCommerce $woocommerce;

/**
* Constructor
*
Expand Down Expand Up @@ -111,6 +118,7 @@ public function init(): void {
return;
}

$this->load_dependencies();
$this->load_plugins();
$this->load_modules();

Expand Down Expand Up @@ -152,6 +160,17 @@ public function get_config( string $key ): mixed {
return $this->config[ $key ] ?? null;
}

/**
* Load plugin dependencies.
*
* @since 1.0.0
*
* @return void
*/
private function load_dependencies() : void {
require_once GOODBIDS_PLUGIN_PATH . '/src/helpers.php';
}

/**
* Load 3rd Party Plugins.
*
Expand Down Expand Up @@ -200,10 +219,11 @@ private function load_modules(): void {
add_action(
'mu_plugin_loaded',
function () {
$this->acf = new ACF();
$this->sites = new Sites();
$this->admin = new Admin();
$this->auctions = new Auctions();
$this->acf = new ACF();
$this->sites = new Sites();
$this->admin = new Admin();
$this->auctions = new Auctions();
$this->woocommerce = new WooCommerce();
}
);
}
Expand Down
53 changes: 36 additions & 17 deletions client-mu-plugins/goodbids/src/classes/Network/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ class Sites {
*/
public function __construct() {
$this->init_np_fields();

// Process New Site Custom Meta Fields.
$this->new_site_form_fields();
$this->validate_new_site_fields();
$this->save_new_site_fields();
$this->save_edit_site_fields();
$this->new_site_form_fields();

// Process Edit Site Custom Meta Fields.
$this->edit_site_form_fields();
$this->save_edit_site_fields();

// New Site Actions
$this->activate_child_theme_on_new_site();
}

Expand Down Expand Up @@ -185,7 +191,7 @@ function () {
return;
}

check_admin_referer( 'add-blog', '_wpnonce_add-blog' );
check_admin_referer( 'add-np-site', '_wpnonce_add-np-site' );

if ( empty( $_POST[ self::OPTION_SLUG ] ) || ! is_array( $_POST[ self::OPTION_SLUG ] ) ) {
wp_die( esc_html__( 'Missing required Nonprofit data.' ) );
Expand Down Expand Up @@ -298,6 +304,8 @@ function ( WP_Site $new_site, array $args ) {
$meta_value = sanitize_text_field( $data[ $key ] );
update_site_meta( $new_site->id, $meta_key, $meta_value );
}

$this->init_site_defaults( $new_site->id );
},
10,
2
Expand All @@ -313,12 +321,13 @@ function ( WP_Site $new_site, array $args ) {
*/
private function save_edit_site_fields(): void {
add_action(
'wp_initialize_site',
'wp_update_site',

/**
* @param WP_Site $new_site New site object.
* @param WP_Site $old_site Old site object.
*/
function ( WP_Site $new_site, WP_Site $old_site ) {
function ( WP_Site $new_site, WP_Site $old_site ): void {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
Expand All @@ -328,7 +337,7 @@ function ( WP_Site $new_site, WP_Site $old_site ) {
return;
}

check_admin_referer( 'edit-site' );
check_admin_referer( 'edit-np-site', '_wpnonce_edit-np-site' );

$data = $_POST[ self::OPTION_SLUG ]; // phpcs:ignore

Expand Down Expand Up @@ -356,22 +365,32 @@ function ( WP_Site $new_site, WP_Site $old_site ) {
*/
private function activate_child_theme_on_new_site(): void {
add_action(
'wp_initialize_site',
'goodbids_init_site',
function ( $site_id ) {
$stylesheet = 'goodbids-nonprofit';

// Switch to the new site
switch_to_blog( $site_id );

// Check if the Goodbids child theme exists
if ( ! wp_get_theme( $stylesheet )->exists() ) {
return;
// Check if the Goodbids child theme exists first.
if ( wp_get_theme( $stylesheet )->exists() ) {
switch_theme( $stylesheet );
}

switch_theme( $stylesheet );

restore_current_blog();
}
);
}

/**
* Initialize new site defaults.
*
* @since 1.0.0
*
* @param int $site_id
*
* @return void
*/
private function init_site_defaults( int $site_id ): void {
switch_to_blog( $site_id );

do_action( 'goodbids_init_site', $site_id );

restore_current_blog();
}
}
23 changes: 23 additions & 0 deletions client-mu-plugins/goodbids/src/classes/Plugins/ACF.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
/**
* ACF Functionality
*
* @since 1.0.0
* @package GoodBids
*/

namespace GoodBids\Plugins;

use GoodBids\Plugins\ACF\Blocks;

/**
* Class for Advanced Custom Fields Pro
*
Expand All @@ -20,6 +23,12 @@ class ACF {
*/
private string $slug = 'advanced-custom-fields-pro/acf.php';

/**
* @since 1.0.0
* @var Blocks
*/
private Blocks $blocks;

/**
* Initialize ACF Functionality
*
Expand All @@ -30,12 +39,26 @@ public function __construct() {
return;
}

// Initialize Submodules.
$this->blocks = new Blocks();

$this->disable_admin();
$this->discourage_the_field_usage();
$this->modify_save_directory();
$this->disable_database_storage();
}

/**
* Return the Blocks submodule
*
* @since 1.0.0
*
* @return Blocks
*/
public function blocks() : Blocks {
return $this->blocks;
}

/**
* Disable ACF Admin per WP VIP Documentation
*
Expand Down
Loading

0 comments on commit 30d901a

Please sign in to comment.