Skip to content

Commit

Permalink
[#15] setting the child theme when a new site is created
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-schmidt-viget committed Dec 11, 2023
1 parent 903cb99 commit a3b854f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
14 changes: 7 additions & 7 deletions client-mu-plugins/goodbids/src/classes/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct() {
*
* @return Core
*/
public static function get_instance() : Core {
public static function get_instance(): Core {
if ( null === self::$instance ) {
self::$instance = new self();
}
Expand All @@ -93,7 +93,7 @@ public static function get_instance() : Core {
*
* @return string
*/
public function get_version() : string {
public function get_version(): string {
$data = get_plugin_data( GOODBIDS_PLUGIN_FILE );
return $data['Version'];
}
Expand All @@ -105,7 +105,7 @@ public function get_version() : string {
*
* @return void
*/
public function init() : void {
public function init(): void {
if ( ! $this->load_config() ) {
// TODO: Log error.
return;
Expand All @@ -124,7 +124,7 @@ public function init() : void {
*
* @return bool
*/
private function load_config() : bool {
private function load_config(): bool {
$json_path = GOODBIDS_PLUGIN_PATH . 'config.json';
if ( ! file_exists( $json_path ) ) {
return false;
Expand Down Expand Up @@ -159,7 +159,7 @@ public function get_config( string $key ) : mixed {
*
* @return void
*/
private function load_plugins() : void {
private function load_plugins(): void {
if ( ! function_exists( 'wpcom_vip_load_plugin' ) ) {
return;
}
Expand All @@ -184,7 +184,7 @@ private function load_plugins() : void {
*
* @return bool
*/
public function is_plugin_active( string $plugin ) : bool {
public function is_plugin_active( string $plugin ): bool {
$plugins = $this->get_config( 'active-plugins' );
return in_array( $plugin, $plugins, true );
}
Expand All @@ -196,7 +196,7 @@ public function is_plugin_active( string $plugin ) : bool {
*
* @return void
*/
private function load_modules() : void {
private function load_modules(): void {
add_action(
'mu_plugin_loaded',
function () {
Expand Down
50 changes: 39 additions & 11 deletions client-mu-plugins/goodbids/src/classes/network/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct() {
$this->save_edit_site_fields();
$this->new_site_form_fields();
$this->edit_site_form_fields();
$this->activate_child_theme();
}

/**
Expand All @@ -50,7 +51,7 @@ public function __construct() {
*
* @return void
*/
private function init_np_fields() : void {
private function init_np_fields(): void {
$this->np_fields = apply_filters(
'goodbids_nonprofit_custom_fields',
[
Expand Down Expand Up @@ -78,7 +79,7 @@ private function init_np_fields() : void {
'required' => true,
'context' => 'both',
],
'status' => [
'status' => [
'label' => __( 'Site Status', 'goodbids' ),
'type' => 'select',
'default' => 'pending',
Expand Down Expand Up @@ -114,7 +115,7 @@ private function init_np_fields() : void {
*
* @return array
*/
public function get_np_fields( string $context = 'both' ) : array {
public function get_np_fields( string $context = 'both' ): array {
if ( empty( $this->np_fields ) ) {
$this->init_np_fields();
}
Expand Down Expand Up @@ -148,7 +149,7 @@ public function get_np_fields( string $context = 'both' ) : array {
*
* @return mixed
*/
public function get_np_data( int $site_id, string $field_id = '' ) : mixed {
public function get_np_data( int $site_id, string $field_id = '' ): mixed {
$data = [];

foreach ( $this->get_np_fields() as $key => $field ) {
Expand Down Expand Up @@ -176,7 +177,7 @@ public function get_np_data( int $site_id, string $field_id = '' ) : mixed {
*
* @return void
*/
private function validate_new_site_fields() : void {
private function validate_new_site_fields(): void {
add_action(
'admin_init',
function () {
Expand Down Expand Up @@ -236,7 +237,7 @@ function () {
*
* @return void
*/
private function new_site_form_fields() : void {
private function new_site_form_fields(): void {
add_action(
'network_site_new_form',
function () {
Expand All @@ -254,7 +255,7 @@ function () {
*
* @return void
*/
private function edit_site_form_fields() : void {
private function edit_site_form_fields(): void {
add_action(
'network_site_info_form',
function ( $site_id ) {
Expand All @@ -273,10 +274,9 @@ function ( $site_id ) {
*
* @return void
*/
private function save_new_site_fields() : void {
private function save_new_site_fields(): void {
add_action(
'wp_initialize_site',

/**
* @param WP_Site $new_site New site object.
* @param array $args Arguments for the initialization.
Expand Down Expand Up @@ -311,10 +311,9 @@ function ( WP_Site $new_site, array $args ) {
*
* @return void
*/
private function save_edit_site_fields() : void {
private function save_edit_site_fields(): void {
add_action(
'wp_update_site',

/**
* @param WP_Site $new_site New site object.
* @param WP_Site $old_site Old site object.
Expand Down Expand Up @@ -347,4 +346,33 @@ function ( WP_Site $new_site, WP_Site $old_site ) {
2
);
}

/**
* Activate nonprofit child theme.
*
* @since 1.0.0
*
* @param int $site_id Site ID.
*
* @return void
*/
private function activate_child_theme(): void {
add_action(
'wpmu_new_blog',
function ( $site_id ) {
$stylesheet = 'goodbids-nonprofit';

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

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

restore_current_blog();
}
);
}
}

0 comments on commit a3b854f

Please sign in to comment.