forked from hoppinger/advanced-custom-fields-wpcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced-custom-fields-wpcli.php
75 lines (62 loc) · 2.18 KB
/
advanced-custom-fields-wpcli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*
Plugin Name: Advanced Custom Fields wp-cli Extension
Plugin URI: http://www.advancedcustomfields.com/add-ons/acf-wpcli/
Description: This extension for Advanced Custom Fields makes it possible to manage your field_groups through the console of wp-cli.
Version: 0.1
Author: Hoppinger
Author URI: http://www.hoppinger.com/
License: GPL
Copyright: Hoppinger
*/
if ( defined('WP_CLI') && WP_CLI ) {
// Include and register the class as the 'example' command handler
include('ACFCommand.php');
WP_CLI::addCommand( 'acf', 'ACFCommand' );
}
/*
* add the php field_groups to our wordpress installation on runtime
*/
if (!defined('WP_CLI') ) {
function acf_wpcli_register_groups() {
global $blog_id;
if(function_exists("register_field_group")) :
$db_field_groups = get_posts(array(
'post_type' => 'acf',
));
$db_field_group_titles = array();
foreach($db_field_groups as $db_group ) :
$db_field_group_titles[] = $db_group->post_title;
endforeach;
$path_pattern = ABSPATH . 'field_groups/' . $blog_id . '/*/data.php';
$shared_childs_pattern = ABSPATH . 'field_groups/shared-childs/*/data.php';
$added_groups = array();
function get_data($f)
{
if (!is_readable($f) || !is_file($f))
return false;
include $f;
return $group;
}
// register the field groups specific for this subsite
foreach (glob($path_pattern) as $file) {
$group = get_data($file);
// Don't register group when the group is already in the DB
if(!in_array($group['title'] , $db_field_group_titles))
register_field_group($group);
$added_groups[] = $group['title'];
}
if( $blog_id != 1 ){
// register the field groups that are shared for all child websites
foreach (glob($shared_childs_pattern) as $file) {
$group = get_data($file);
// 1. Don't register group when the group is already in the DB
// 2. Don't register group when the group has been added from a blog_id specific group
if(!in_array($group['title'] , $db_field_group_titles) && !in_array($group['title'] , $added_groups))
register_field_group($group);
}
}
endif;
}
add_action('plugins_loaded', 'acf_wpcli_register_groups');
}