├── screenshot.png ├── composer.json ├── README.md └── acf-local-json-manager.php /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/khromov/acf-local-json-manager/HEAD/screenshot.png -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "khromov/acf-local-json-manager", 3 | "type" : "wordpress-plugin", 4 | "require" : { 5 | "composer/installers": "~1.0" 6 | } 7 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ACF Local JSON Manager 2 | **Manages plugins and themes that use ACF Local JSON.** 3 | 4 | ![Screenshot](screenshot.png) 5 | 6 | 7 | Are you working multiple plugins and themes that utilize [ACF Local JSON](https://www.advancedcustomfields.com/resources/local-json/)? 8 | 9 | Since Local JSON files can only save in one place, there is an issue if multiple plugins/themes try to hook on `acf/settings/save_json`. 10 | (Any field groups that you edit will be saved to the last plugin that hooks on the filter.) That's the problem ACF Local JSON Manager 11 | tries to solve. 12 | 13 | With ACF Local JSON Manager you can select which plugin or theme the Local JSON files should be saved to from the comfort of the admin bar. 14 | 15 | #### Adding support for ACF Local JSON Manager 16 | 17 | You have to add a filter in order to make your plugin/theme compatible with the ACF Local JSON Manager. The snippet looks like this: 18 | 19 | ```php 20 | add_filter('aljm_save_json', function($folders) { 21 | $folders['My plugin'] = dirname(__FILE__) . '/acf'; 22 | return $folders; 23 | }); 24 | ``` 25 | 26 | The hook `aljm_save_json` provides you with a key/value array `$folders` that lists all the folders currently registered in the manager. 27 | The array key is your plugin or theme name and the value is the path to the folder. 28 | 29 | **Adding support to an existing plugin or theme** 30 | 31 | If your code currently looks like this: 32 | 33 | ```php 34 | add_filter('acf/settings/save_json', function() { 35 | return dirname(__FILE__) . '/acf'; 36 | }); 37 | ``` 38 | 39 | Simply add the `aljm_save_json` hook underneath it and your code will be compatible with the ACF Local JSON Manager, but it will also 40 | continue to work as before if the manager is not enabled: 41 | 42 | ```php 43 | //Old hook 44 | add_filter('acf/settings/save_json', function() { 45 | return dirname(__FILE__) . '/acf'; 46 | }); 47 | 48 | //New added hook 49 | add_filter('aljm_save_json', function($folders) { 50 | $folders['My plugin'] = dirname(__FILE__) . '/acf'; 51 | return $folders; 52 | }); 53 | ``` 54 | 55 | #### Changelog 56 | 57 | * 1.2 - [GitHub Updater](https://github.com/afragen/github-updater) support 58 | * 1.1 - Simplified activation mechanism. If you see "Local JSON: _none" in your admin bar please open the submenu and select Disable Overrides to reset it. 59 | * 1.0 - Initial release 60 | -------------------------------------------------------------------------------- /acf-local-json-manager.php: -------------------------------------------------------------------------------- 1 | register_endpoints(); 32 | 33 | //TODO: Register acf/settings/save_json at very late priority 34 | } 35 | 36 | /** 37 | * Adds a menu item to the admin bar via the admin_bar_menu hook 38 | * 39 | * @param $wp_admin_bar WP_Admin_Bar object 40 | */ 41 | function admin_bar($wp_admin_bar) 42 | { 43 | //We're in admin and this is not a WPML install 44 | if(is_admin() && current_user_can('manage_options') === true) { 45 | 46 | $top_id = 'acf-local-json-manager-icon-' . ($this->override_activated() ? 'active' : 'inactive'); 47 | 48 | //Add main menu 49 | $main_bar = array( 50 | 'id' => $top_id, 51 | 'title' => 'Local JSON' . ($this->override_activated() ? ': ' . esc_html(get_option('aljm_current_plugin')) : ''), 52 | 'href' => '#', 53 | 'meta' => array( 54 | 'class' => 'acf-local-json-manager' 55 | ) 56 | ); 57 | 58 | //List all submenus 59 | $submenus = array(); 60 | 61 | foreach(apply_filters('aljm_save_json', array()) as $plugin => $path) { 62 | 63 | $submenus[] = array( 64 | 'id' => 'acf-local-json-manager-'. esc_attr($plugin), 65 | 'title' => esc_html($plugin), 66 | 'href' => $this->build_switch_url($plugin), 67 | 'parent' => $top_id, 68 | 'meta' => array( 69 | 'target' => '_self' 70 | ) 71 | ); 72 | } 73 | 74 | //Add sub menu 75 | if($this->override_activated()) { 76 | $submenus[] = array( 77 | 'id' => 'acf-local-json-manager-disable', 78 | 'title' => 'Disable overrides', 79 | 'href' => $this->build_switch_url('_none'), 80 | 'parent' => $top_id, 81 | 'meta' => array( 82 | 'target' => '_self' 83 | ) 84 | ); 85 | } 86 | 87 | $wp_admin_bar->add_node($main_bar); 88 | 89 | foreach($submenus as $submenu) { 90 | $wp_admin_bar->add_node($submenu); 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * Builds URL to switch between plugins 97 | * 98 | * @param $plugin_slug 99 | * 100 | * @return string|void 101 | */ 102 | function build_switch_url($plugin_slug) { 103 | return admin_url('?aljm_select_plugin=' . urlencode_deep($plugin_slug). '&aljm_nonce=' . wp_create_nonce( 'aljm' ) . '&aljm_return_url=' . urlencode_deep((is_ssl() ? 'https' : 'http') . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"])); 104 | } 105 | 106 | /** 107 | * Adds a little icon to the admin bar for later WordPress versions 108 | */ 109 | function admin_css() 110 | { 111 | ?> 112 | 134 |