Skip to content

Commit

Permalink
Better fallbacks when saved default site template for group type is n…
Browse files Browse the repository at this point in the history
…o longer valid.

See #3227.
  • Loading branch information
boonebgorges committed Sep 12, 2023
1 parent 9b9e57b commit 2bebee4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,44 @@ public function get_label() {
}
}

/**
* Get the default site template ID for this group type.
*
* @return int
*/
public function get_site_template_id() {
$site_template_ids = get_option( 'openlab_group_type_default_site_template_ids' );

$saved_site_template_is_valid = false;

$site_template_id = null;

// Before returning the saved value, make sure the template exists
// and is associated with a category linked to this group type.
if ( isset( $site_template_ids[ $this->get_slug() ] ) ) {
return (int) $site_template_ids[ $this->get_slug() ];
$site_template_id = $site_template_ids[ $this->get_slug() ];

$site_template_categories = wp_get_post_terms(
$site_template_id,
'cboxol_template_category',
[
'fields' => 'ids',
]
);

$group_type_template_categories = $this->get_site_template_categories();
$group_type_template_category_ids = wp_list_pluck( $group_type_template_categories, 'term_id' );

foreach ( $site_template_categories as $site_template_category ) {
if ( in_array( $site_template_category, $group_type_template_category_ids, true ) ) {
$saved_site_template_is_valid = true;
break;
}
}
}

if ( $saved_site_template_is_valid ) {
return $site_template_id;
}

// Fall back on the first available.
Expand Down
6 changes: 3 additions & 3 deletions wp-content/themes/openlab/lib/site-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 1. Copy the following files:
* a. includes/site-template.php -> lib/site-template.php
* b. build/site-templates.css -> css/site-templates.css
* c. build/site-templates.js -> css/site-templates.js
* d. build/site-templates-admin.js -> css/site-templates-admin.js
* e. build/site-templates-default-category.js -> css/site-templates-default-category.js
* c. build/site-templates.js -> js/site-templates.js
* d. build/site-templates-admin.js -> js/site-templates-admin.js
* e. build/site-templates-default-category.js -> js/site-templates-default-category.js
* f. views/site-template/ -> parts/site-template/
* g. classes/API/Sites.php -> lib/cbox-polyfills/class-cbox-sites-endpoint.php
*
Expand Down

0 comments on commit 2bebee4

Please sign in to comment.