Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active May 13, 2019 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/cd130b4ea5f1eff034dc42429ab13826 to your computer and use it in GitHub Desktop.
Save billerickson/cd130b4ea5f1eff034dc42429ab13826 to your computer and use it in GitHub Desktop.
<?php
/**
* Advanced Custom Fields
*
* @package CoreFunctionality
* @version 2.0
* @author Bill Erickson <bill@billerickson.net>
* @copyright Copyright (c) 2018, Bill Erickson
* @license GPL-2.0+
*/
class BE_ACF_Customizations {
public function __construct() {
// Only allow fields to be edited on development
if ( ! defined( 'WP_LOCAL_DEV' ) || ! WP_LOCAL_DEV ) {
add_filter( 'acf/settings/show_admin', '__return_false' );
}
// Save and sync fields in functionality plugin
add_filter( 'acf/settings/save_json', array( $this, 'get_local_json_path' ) );
add_filter( 'acf/settings/load_json', array( $this, 'add_local_json_path' ) );
add_action( 'admin_init', array( $this, 'sync_fields_with_json' ) );
// Register options page
//add_action( 'init', array( $this, 'register_options_page' ) );
// Register Blocks
//add_action('acf/init', array( $this, 'register_blocks' ) );
}
/**
* Define where the local JSON is saved
*
* @return string
*/
public function get_local_json_path() {
return EA_DIR . '/acf-json';
}
/**
* Add our path for the local JSON
*
* @param array $paths
*
* @return array
*/
public function add_local_json_path( $paths ) {
$paths[] = EA_DIR . '/acf-json';
return $paths;
}
/**
* Automatically sync any JSON field configuration.
*/
public function sync_fields_with_json() {
if ( defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
return;
}
if ( ! function_exists( 'acf_get_field_groups' ) ) {
return;
}
$version = get_option( 'be_acf_json_version' );
if( defined( 'CORE_FUNCTIONALITY_VERSION' ) && version_compare( CORE_FUNCTIONALITY_VERSION, $version ) ) {
update_option( 'be_acf_json_version', CORE_FUNCTIONALITY_VERSION );
$groups = acf_get_field_groups();
if ( empty( $groups ) ) {
return;
}
$sync = array();
foreach ( $groups as $group ) {
$local = acf_maybe_get( $group, 'local', false );
$modified = acf_maybe_get( $group, 'modified', 0 );
$private = acf_maybe_get( $group, 'private', false );
if ( $local !== 'json' || $private ) {
// ignore DB / PHP / private field groups
continue;
}
if ( ! $group['ID'] ) {
$sync[ $group['key'] ] = $group;
} elseif ( $modified && $modified > get_post_modified_time( 'U', true, $group['ID'], true ) ) {
$sync[ $group['key'] ] = $group;
}
}
if ( empty( $sync ) ) {
return;
}
foreach ( $sync as $key => $v ) {
if ( acf_have_local_fields( $key ) ) {
$sync[ $key ]['fields'] = acf_get_local_fields( $key );
}
acf_import_field_group( $sync[ $key ] );
}
}
}
/**
* Register Options Page
*
*/
function register_options_page() {
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( array(
'title' => __( 'Site Options', 'core-functionality' ),
'capability' => 'manage_options',
) );
}
}
/**
* Register Blocks
* @see https://www.billerickson.net/building-gutenberg-block-acf/#register-block
*
* Categories: common, formatting, layout, widgets, embed
* Dashicons: https://developer.wordpress.org/resource/dashicons/
* ACF Settings: https://www.advancedcustomfields.com/resources/acf_register_block/
*/
function register_blocks() {
if( ! function_exists('acf_register_block_type') )
return;
acf_register_block_type( array(
'name' => 'features',
'title' => __( 'Features', 'core-functionality' ),
'render_template' => 'partials/block-features.php',
'category' => 'formatting',
'icon' => 'awards',
'mode' => 'auto',
'keywords' => array(),
));
}
}
new BE_ACF_Customizations();
<?php
/**
* Plugin Name: Core Functionality
*
*/
define( 'CORE_FUNCTIONALITY_VERSION', '1.0.0' );
define( 'EA_DIR' , plugin_dir_path( __FILE__ ) );
<?php
define( 'WP_LOCAL_DEV', true );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment