-
-
Save mwangepatrick/ede54d0868dafb294789a04ec403a856 to your computer and use it in GitHub Desktop.
Load predefined JSON files by their name, then make the list accessible to child themes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class craft_child_features { | |
static function setup() { | |
add_filter( 'craft_acf_parent_features', __CLASS__ . '::craft_child_remove_acf_groups' ); | |
} | |
/** | |
* [craft_child_remove_acf_groups description] | |
* @param [type] $features [description] | |
* @return [type] [description] | |
*/ | |
static function craft_child_remove_acf_groups( $features ) { | |
// Remove ACF Groups from parent | |
// var_export( $features ); // Uncomment to see the available features | |
$ignore_list = [ | |
'variation-gallery', | |
]; | |
// For each item in the array, remove the key from features | |
foreach ( $ignore_list as $ignored ) { | |
if ( ( $key = array_search( $ignored, $features ) ) !== false ) { | |
unset( $features[ $key ] ); | |
} | |
} | |
// Add new groups / files | |
$add_list = []; | |
// If there files available, merge that with the features list | |
if ( ! empty( $add_list ) ) { | |
$features = array_merge( $features, $add_list ); | |
} | |
// Return the available features for this theme. | |
return $features; | |
} | |
} | |
} | |
craft_child_features::setup(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class craft_features { | |
static function setup() { | |
add_action( 'acf/include_fields', __CLASS__ . '::craft_acf_add_features' ); | |
} | |
/** | |
* [craft_acf_parent_features list all features in parent theme] | |
* @return [array] [list of filenames for acf-json to use] | |
*/ | |
static function craft_acf_parent_features() { | |
// A list of the file names and their features | |
$features = [ | |
'content-builder', // → Layout and content builder | |
'menu-builder', // → Main / mega menu builder | |
'theme-settings', // → Theme settings | |
'banner-builder', // → Banner builder for banner CPT | |
'banner-layouts', // → Banner layouts for banner CPT | |
'banner-groups', // → Banner Groups for banner group taxonomy | |
'brand-logo', // → Brand logo on Brand Taxonomy | |
'variation-gallery', // → Variation gallery on single product variations | |
]; | |
// Give me that list | |
return apply_filters( 'craft_acf_parent_features', $features ); | |
} | |
/** | |
* [craft_acf_add_features Add all the features to the current theme] | |
* @return [acf-json] [Add the ACF group to the website] | |
*/ | |
static function craft_acf_add_features() { | |
// is ACF loaded? Does it even exist?! | |
if ( ! function_exists( 'acf_add_local_field_group' ) ) { | |
return; | |
} | |
// Craft path for acf json | |
$child_path = untrailingslashit( get_stylesheet_directory() . '/acf-json' ); | |
$parent_path = untrailingslashit( get_template_directory() . '/acf-json' ); | |
// A list of the file names and their features | |
$features = self::craft_acf_parent_features(); | |
// For all these features, load it all in. | |
foreach ( $features as $feature ) { | |
// Create the file paths + add .json to it | |
$file = $child_path . '/' . $feature . '.json'; | |
// If the file doesn't exist in the child | |
if ( ! file_exists( $file ) ) { | |
$file = $parent_path . '/' . $feature . '.json'; | |
} | |
// Check the parent, skip if not found | |
if ( ! file_exists( $file ) ) { | |
continue; | |
} | |
// Is this a json? Lets double check. | |
$json = file_get_contents( $file ); | |
if ( empty( $json ) ) { | |
continue; | |
} | |
// Load the JSON into the theme | |
$json = json_decode( $json, true ); | |
$json['local'] = 'json'; | |
// Check if we haven't loaded this already | |
if ( acf_get_field_group( $json['key'] ) ) { | |
continue; | |
} | |
// Create ACF field group based on json | |
acf_add_local_field_group( $json ); | |
} // end foreach | |
} | |
} | |
craft_features::setup(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment