This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
acfd.php
executable file
·153 lines (122 loc) · 3.76 KB
/
acfd.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
Plugin Name: Advanced Custom Fields Development
Plugin URI: https://github.com/mirkoferraro/advanced-custom-fields-dev
Description: Makes the ACF registration via PHP easier
Version: 1.0.7
Author: Mirko Ferraro
Author URI: http://www.mirkoferraro.it
Copyright: Mirko Ferraro
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class ACFD {
static private $menu_order = 1;
static private $defaults = array();
static private $queue = array();
static function init() {
add_action( 'plugins_loaded', array( get_called_class(), 'check_dependencies' ) );
}
static function check_dependencies() {
if ( ! class_exists('acf') ) {
add_action( 'admin_notices', array( get_called_class(), 'admin_notice_acf_required' ) );
return;
}
include( 'class/custom_field.php' );
include( 'class/custom_field_container.php' );
include( 'class/custom_group.php' );
include( 'class/custom_module.php' );
include( 'class/widget.php' );
include( 'includes/hide_on_screen_fix.php' );
add_action( 'acf/init', array( get_called_class(), 'registerGroups' ) );
add_action( 'init', array( get_called_class(), 'registerGroups' ) );
}
static function isActive() {
return class_exists( 'acf' );
}
static function admin_notice_acf_required() {
?>
<div class="notice notice-error is-dismissible">
<p><?= __( 'Advanced Custom Fields Easy Development require Advanced Custom Field in order to work', 'acfed' ); ?></p>
</div>
<?php
}
static function getContainerFieldName( $type ) {
switch ( $type ) {
case 'repeater': return 'sub_fields';
case 'flexible_content': return 'layouts';
case 'flexible_item': return 'sub_fields';
default: return 'fields';
}
}
static function getDefaults( $type ) {
return isset( self::$defaults[ $type ] ) ? self::$defaults[ $type ] : array();
}
static function setDefaults( $type, $data ) {
self::$defaults[ $type ] = $data;
}
static function field( $name, $label, $type ) {
return new CustomField( $name, $label, $type );
}
static function container( $name, $label, $type, $field_name = 'fields' ) {
return new CustomFieldContainer( $name, $label, $type, $field_name );
}
static function group( $name, $location = 'options_page == acf-options' ) {
return new CustomGroup( $name, $location );
}
static function module() {
return new CustomModule();
}
static function addGroup( $group ) {
self::$queue[] = $group;
}
private static function _acfLocalGroup( $group ) {
if ( ! count( $group->getFields() ) ) {
return false;
}
$group_data = $group->get();
if ( ! isset( $group_data['menu_order'] ) ) {
$group_data['menu_order'] = self::$menu_order++;
}
// Throws exception if ACF is not loaded
if ( ! function_exists( 'acf_add_local_field_group' ) ) {
throw new Exception( 'Advanced Custom Field not loaded' );
}
// Create ACF Options page if used
foreach ( $group_data['location'] as &$group ) {
foreach ($group as &$rule) {
if ( $rule['param'] == 'options_page' && $rule['operator'] == '==' ) {
if ( strpos( $rule['value'], 'acf-options-' ) === 0 ) {
acf_add_options_sub_page( substr( $rule['value'], 12 ) );
$rule['value'] = strtolower( $rule['value'] );
} else {
acf_add_options_page();
}
}
}
}
acf_add_local_field_group( $group_data );
return true;
}
static function registerGroup( $key ) {
foreach ( self::$queue as $key => $group ) {
if ( $key == $group->get( 'key' ) ) {
if ( self::_acfLocalGroup( $group ) ) {
unset( self::$queue[$key] );
return true;
}
return false;
}
}
return false;
}
static function registerGroups() {
foreach ( self::$queue as $key => $group ) {
if ( self::_acfLocalGroup( $group ) ) {
unset( self::$queue[$key] );
}
}
}
}
ACFD::init();