-
Notifications
You must be signed in to change notification settings - Fork 2
/
acf-extensions.php
164 lines (126 loc) · 4.78 KB
/
acf-extensions.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
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
Plugin Name: Advanced Custom Fields Extensions
Description: Advanced Custom Fields add on. Create components, component field, hidden field and latest post field
Version: 1.3.11
Author: Metabolism
License: MIT
*/
defined('ABSPATH') or die('No script kiddies please!');
define('ACF_EXTENSIONS_VERSION', '1.3.11');
/**
* Load up the translation files
*
* @since 1.0.4
* @return void
*/
function acf_extensions_load_textdomain() {
load_plugin_textdomain('acf-component_field', false, dirname(plugin_basename(__FILE__)).'/lang/');
}
add_action('plugins_loaded', 'acf_extensions_load_textdomain');
/**
* Load the main field class after core acf init
*
* @since 1.0.0
* @return void
*/
function include_acf_extensions_plugin() {
$required_version = '5.8.0';
if( !function_exists('acf') )
return;
$acf = acf();
if( is_admin() && version_compare($acf->version, $required_version, '<') ) {
add_action('admin_notices', function() use($required_version){
echo '<div class="error"><p>'.__('ACF Extensions plugin requires ACF PRO '.$required_version).'</p></div>';
});
}
include_once('fields/class-acf-field-component.php');
include_once('fields/class-acf-field-components.php');
include_once('fields/class-acf-field-hidden.php');
include_once('fields/class-acf-field-children.php');
include_once('fields/class-acf-field-latest_posts.php');
include_once('fields/class-acf-field-link.php');
include_once('fields/class-acf-field-map.php');
include_once('fields/class-acf-field-dynamic_select.php');
include_once('fields/class-acf-field-id.php');
include_once('fields/class-acf-field-instagram_post.php');
include_once('fields/class-acf-field-inline-editor.php');
include_once('rules/class-acf-rule-multisite.php');
include_once('rules/class-acf-rule-parent-post.php');
include_once('rules/class-acf-location-term-type.php');
include_once('rules/class-acf-rule-tax-type.php');
include_once('rules/class-acf-rule-nav-menu-item-depth.php');
include_once('settings/class-acf-field-tag.php');
include_once('plugins/class-export.php');
include_once('plugins/class-counter.php');
}
add_action('acf/include_field_types', 'include_acf_extensions_plugin');
add_action('acf/init', function (){
if( isset($_GET['clean-acf']) && $_GET['clean-acf'] && is_admin() && current_user_can('administrator')){
$folder = apply_filters('acf/settings/save_json', '');
foreach (glob($folder."/*.*") as $filename) {
$content = json_decode(file_get_contents($filename), true);
foreach($content['fields'] as $i=>$field){
if(!($field['key']??false) || empty($field))
unset($content['fields'][$i]);
}
file_put_contents($filename, json_encode($content, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
}
global $wpdb;
$wpdb->query("DELETE pm FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL");
$wpdb->query("DELETE FROM $wpdb->posts WHERE post_type='acf-field'");
}
});
add_filter( 'mce_external_plugins', function ( $plugins ) {
$plugins['table'] = content_url() . '/plugins/acf-extensions/js/tinymce/table/plugin.min.js';
return $plugins;
});
/**
* Change acf-component status back to acf-diabled when deactivated
*
* @since 1.0.0
* @return void
*/
function deactivate_acf_extensions_plugin() {
// convert all the acf-component to acf-disabled
$args = array(
'posts_per_page' => -1,
'post_type' => 'acf-field-group',
'post_status' => array('acf-component')
);
$field_groups = get_posts($args);
foreach ($field_groups as $field_group) {
$field_group->post_status = 'acf-disabled';
wp_update_post($field_group);
}
}
register_deactivation_hook(__FILE__, 'deactivate_acf_extensions_plugin');
/**
* Loop through the inactive acf field group and tag them as acf component
* don't touch the enable ones even its is_acf_component is true.
* Because the means user manually enable it,
* thus might not be a component anymore
*
* @since 1.0.7
* @return void
*/
function activate_acf_extensions_plugin() {
if( !function_exists('acf_get_field_group') )
return;
$args = array(
'posts_per_page' => -1,
'post_type' => 'acf-field-group',
'post_status' => array('acf-component', 'acf-disabled')
);
$field_groups = get_posts($args);
foreach ($field_groups as $field_group) {
$acf = acf_get_field_group($field_group);
if (acf_maybe_get($acf, 'is_acf_component', 0) == 1) {
wp_update_post(array(
'ID' => $field_group->ID,
'post_status' => 'acf-component'
));
}
}
}
register_activation_hook(__FILE__, 'activate_acf_extensions_plugin');