Last active
December 20, 2019 11:08
-
-
Save toscani/a77e3c08bdc25491059dd77c5b3f07c6 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Plugin Name: MJSP 2019-10-17 12:30 GMT by @Brugman | |
* Description: Multiple JSON Save Points plugin, work in progress. | |
* | |
* Notes: | |
* 2019-12-20 | |
* Use at own risk. I've integrated parts of this code in | |
* https://github.com/Brugman/acf-agency-workflow and will be working | |
* on that instead. | |
*/ | |
new acf_custom_json_save_path_setting(); | |
class acf_custom_json_save_path_setting { | |
// This will store the preferred save path for the group for later retrieval. | |
private $preferred_save_path; | |
public function __construct() { | |
// Add the "JSON Save Path" setting to all field groups. | |
add_action('acf/render_field_group_settings', [$this, 'add_json_save_path_setting']); | |
// Call an early bird (priority 1) action before saving the field group. | |
add_action('acf/update_field_group', [$this, 'set_up_save_path'], 1, 1); | |
} | |
public function add_json_save_path_setting($field_group) { | |
$load_dir_current = false; | |
$choices = []; | |
$load_dirs = acf_get_setting('load_json'); | |
$path_to_plugins = dirname( dirname( __FILE__ ) ); | |
$path_to_themes = dirname( get_stylesheet_directory() ); | |
foreach ( $load_dirs as $load_dir ) | |
{ | |
$display_title = $load_dir; | |
if ( strpos( $load_dir, $path_to_plugins ) !== false ) | |
$display_title = 'Plugin: '.substr( str_replace( $path_to_plugins, '', $load_dir ), 1 ); | |
if ( strpos( $load_dir, $path_to_themes ) !== false ) | |
$display_title = 'Theme: '.substr( str_replace( $path_to_themes, '', $load_dir ), 1 ); | |
$choices[ $load_dir ] = $display_title; | |
if ( file_exists( $load_dir.'/'.$field_group['key'].'.json' ) ) | |
$load_dir_current = $load_dir; | |
} | |
$load_dir_selected = false; | |
if ( isset( $load_dirs[0] ) ) | |
$load_dir_selected = $load_dirs[0]; | |
if ( $load_dir_current ) | |
$load_dir_selected = $load_dir_current; | |
if ( !$load_dir_selected ) | |
return; | |
acf_render_field_wrap([ | |
'label' => 'JSON Save Path', | |
'instructions' => 'Determines where the field group\'s JSON file will be saved.', | |
'type' => 'select', | |
'name' => 'json_save_path', | |
'prefix' => 'acf_field_group', | |
'value' => $load_dir_selected, | |
'choices' => $choices, | |
]); | |
} | |
public function set_up_save_path($group) { | |
// Get the preferred save path, if set. | |
$preferred_save_path = $group['json_save_path'] ?? null; | |
// If not set (or set to an empty string), do nothing. | |
if (!$preferred_save_path) { | |
return $group; | |
} | |
// Set aside the preferred path and add an override action. | |
$this->preferred_save_path = $preferred_save_path; | |
add_action('acf/settings/save_json', [$this, 'save_to_preferred_save_path'], 9999); | |
// Return the group for updating as usual. | |
return $group; | |
} | |
public function save_to_preferred_save_path($path) { | |
// Ensure this field group is saved to the preferred save path. | |
$path = $this->preferred_save_path; | |
return $path; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment