forked from hoppinger/advanced-custom-fields-wpcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-custom-fields-wpcli.php
83 lines (70 loc) · 2.58 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
76
77
78
79
80
81
82
83
<?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 ) {
//Since version 5 uses json instead of xml, we have a new ACF command for it.
if ( substr( get_option( 'acf_version' ), 0, 1 ) == 5 ) {
// Include and register the class as the 'example' command handler
include 'ACF5_Command.php';
WP_CLI::add_command( 'acf', 'ACF5_Command' );
} else {
// Include and register the class as the 'example' command handler
include 'ACFCommand.php';
WP_CLI::add_command( '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" ) ) :
global $wpdb;
$db_field_groups = $wpdb->get_results( "SELECT post_title FROM {$wpdb->posts} WHERE post_type='acf' AND post_status='publish';" );
$db_field_group_titles = array();
foreach ( $db_field_groups as $db_group ) :
$db_field_group_titles[] = $db_group->post_title;
endforeach;
$paths = array(
'active_theme' => get_template_directory() . '/field-groups/',
'active_child_theme' => get_stylesheet_directory() . '/field-groups/',
'child_themes_shared' => ABSPATH . 'field-groups/shared-childs/',
);
$paths = apply_filters( 'acfwpcli_fieldgroup_paths', $paths );
$patterns = array();
foreach ( $paths as $key => $value ) {
if( ! is_dir($value) ){
continue;
}
$patterns[ $key ] = trailingslashit( $value ) . '*/data.php';
}
$added_groups = array();
foreach ( $patterns as $pattern ) {
// register the field groups specific for this subsite
foreach ( glob( $pattern ) as $file ) {
$group = acf_wpcli_get_file_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'];
}
}
endif;
}
add_action( 'plugins_loaded', 'acf_wpcli_register_groups' );
function acf_wpcli_get_file_data( $file ) {
if ( ! is_readable( $file ) || ! is_file( $file ) )
return false;
include $file;
return $group;
}
}