Skip to content

Commit f2fb5f3

Browse files
author
Jerome Barbato
committed
add acf cleaner
1 parent 68534e6 commit f2fb5f3

File tree

6 files changed

+264
-6
lines changed

6 files changed

+264
-6
lines changed

acf-extensions.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ function include_acf_extensions_plugin() {
5151
include_once('fields/class-acf-field-latest_posts.php');
5252
include_once('fields/class-acf-field-link.php');
5353
include_once('fields/class-acf-field-map.php');
54+
include_once('fields/class-acf-field-dynamic_select.php');
5455

5556
include_once('rules/class-acf-rule-multisite.php');
5657
include_once('rules/class-acf-rule-tax-type.php');
@@ -63,6 +64,30 @@ function include_acf_extensions_plugin() {
6364

6465
add_action('acf/include_field_types', 'include_acf_extensions_plugin');
6566

67+
add_action('acf/init', function (){
68+
69+
if( isset($_GET['clean-acf']) && $_GET['clean-acf'] && is_admin() && current_user_can('administrator')){
70+
71+
$folder = apply_filters('acf/settings/save_json', '');
72+
73+
foreach (glob($folder."/*.*") as $filename) {
74+
75+
$content = json_decode(file_get_contents($filename), true);
76+
foreach($content['fields'] as $i=>$field){
77+
78+
if(!($field['key']??false) || empty($field))
79+
unset($content['fields'][$i]);
80+
}
81+
file_put_contents($filename, json_encode($content, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
82+
}
83+
84+
global $wpdb;
85+
86+
$wpdb->query("DELETE pm FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL");
87+
$wpdb->query("DELETE FROM $wpdb->posts WHERE post_type='acf-field'");
88+
}
89+
});
90+
6691
add_filter( 'mce_external_plugins', function ( $plugins ) {
6792
$plugins['table'] = content_url() . '/plugins/acf-extensions/js/tinymce/table/plugin.min.js';
6893
return $plugins;

changelog.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
CHANGELOG
22
---------
33

4+
##1.1.8
5+
#### Feature
6+
- added fields cleaning function, add ?clean-acf=1 in a backend url
7+
8+
##1.1.7
9+
#### Bugfix
10+
- invalid `dynamic_select` component value
11+
12+
##1.1.6
13+
#### Feature
14+
- added `dynamic_select` component, list values from other field in post or in options
15+
16+
##1.1.5
17+
#### Feature
18+
- Latest post component allow multiple post_type
19+
420
##1.1.4
521
#### Bugfix
622
- Allow empty tag

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name":"metabolism/acf-extensions",
3-
"version": "1.1.4",
3+
"version": "1.1.8",
44
"type": "wordpress-plugin",
5-
"description": "ACF Extensions plugin with component, components, hidden field ans latest posts field",
5+
"description": "ACF Extensions plugin with component, components, hidden field, dynamic select and latest posts field",
66
"license": "GPL-3.0-or-later"
77
}

css/input.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.acf-field-hidden, .acf-field-latest-posts{ display: none; }
22
.acf-field .acf-label:empty{ display: none }
33
.acf-tag-selector{ position: absolute; right: 0; bottom: 100%; width: auto; margin-bottom: 6px }
4-
.acf-tag-selector select{ width: auto!important; padding: 0 8px!important; padding-right: 30px!important; min-height: 26px; font-size: 12px }
4+
.acf-tag-selector select{ width: auto!important; padding: 0 8px!important; padding-right: 30px!important; min-height: 22px; font-size: 11px!important; }
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
3+
if( ! class_exists('acf_field_dynamic_select_extension') ) :
4+
5+
class acf_field_dynamic_select_extension extends acf_field_select {
6+
7+
function render_field( $field ) {
8+
9+
// convert
10+
$value = acf_get_array($field['value']);
11+
12+
$field_key = explode('.', $field['field_key']);
13+
14+
if( $field_key[0] == 'options'){
15+
16+
$other_field_values = get_field($field_key[1], 'options');
17+
array_shift($field_key);
18+
}
19+
else{
20+
21+
$other_field_values = get_field($field_key[0]);
22+
}
23+
24+
$choices = array();
25+
26+
if( count($field_key) == 2 ){
27+
28+
foreach ($other_field_values as $other_field_value){
29+
30+
$choice = $other_field_value[$field_key[1]]??'';
31+
32+
if( strlen($choice) ){
33+
34+
$choice = $other_field_value[$field_key[1]];
35+
$choices[sanitize_title($choice)] = $choice;
36+
}
37+
}
38+
}
39+
40+
// placeholder
41+
if( empty($field['placeholder']) ) {
42+
$field['placeholder'] = _x('Select', 'verb', 'acf');
43+
}
44+
45+
46+
// add empty value (allows '' to be selected)
47+
if( empty($value) ) {
48+
$value = array('');
49+
}
50+
51+
52+
// prepend empty choice
53+
// - only for single selects
54+
// - have tried array_merge but this causes keys to re-index if is numeric (post ID's)
55+
if( $field['allow_null'] && !$field['multiple'] ) {
56+
$choices = array( '' => "- {$field['placeholder']} -" ) + $choices;
57+
}
58+
59+
60+
// vars
61+
$select = array(
62+
'id' => $field['id'],
63+
'class' => $field['class'],
64+
'name' => $field['name'],
65+
'data-ui' => $field['ui'],
66+
'data-ajax' => $field['ajax'],
67+
'data-multiple' => $field['multiple'],
68+
'data-placeholder' => $field['placeholder'],
69+
'data-allow_null' => $field['allow_null']
70+
);
71+
72+
73+
// multiple
74+
if( $field['multiple'] ) {
75+
76+
$select['multiple'] = 'multiple';
77+
$select['size'] = 5;
78+
$select['name'] .= '[]';
79+
80+
// Reduce size to single line if UI.
81+
if( $field['ui'] ) {
82+
$select['size'] = 1;
83+
}
84+
}
85+
86+
87+
// special atts
88+
if( !empty($field['readonly']) ) $select['readonly'] = 'readonly';
89+
if( !empty($field['disabled']) ) $select['disabled'] = 'disabled';
90+
if( !empty($field['ajax_action']) ) $select['data-ajax_action'] = $field['ajax_action'];
91+
92+
93+
// hidden input is needed to allow validation to see <select> element with no selected value
94+
if( $field['multiple'] || $field['ui'] ) {
95+
acf_hidden_input(array(
96+
'id' => $field['id'] . '-input',
97+
'name' => $field['name']
98+
));
99+
}
100+
101+
102+
// append
103+
$select['value'] = $value;
104+
$select['choices'] = $choices;
105+
106+
107+
// render
108+
acf_select_input( $select );
109+
110+
}
111+
112+
/*
113+
* initialize
114+
*
115+
* This function will setup the field type data
116+
*
117+
* @type function
118+
* @date 10/01/2019
119+
* @since 5.0.0
120+
*
121+
* @param n/a
122+
* @return n/a
123+
*/
124+
125+
function initialize() {
126+
127+
// vars
128+
$this->name = 'dynamic_select';
129+
$this->label = __("Dynamic select",'acf');
130+
$this->category = 'choice';
131+
132+
$this->defaults = array(
133+
'multiple' => 0,
134+
'allow_null' => 0,
135+
'choices' => array(),
136+
'default_value' => '',
137+
'ui' => 0,
138+
'ajax' => 0,
139+
'placeholder' => '',
140+
'return_format' => 'value'
141+
);
142+
}
143+
144+
145+
/*
146+
* render_field_settings()
147+
*
148+
* Create extra options for your field. This is rendered when editing a field.
149+
* The value of $field['name'] can be used (like bellow) to save extra data to the $field
150+
*
151+
* @type action
152+
* @since 3.6
153+
* @date 23/01/13
154+
*
155+
* @param $field - an array holding all the field's data
156+
*/
157+
158+
function render_field_settings( $field ) {
159+
160+
// allow_null
161+
acf_render_field_setting( $field, array(
162+
'label' => __('Allow Null?','acf'),
163+
'instructions' => '',
164+
'name' => 'allow_null',
165+
'type' => 'true_false',
166+
'ui' => 1,
167+
));
168+
169+
// multiple
170+
acf_render_field_setting( $field, array(
171+
'label' => __('Select multiple values?','acf'),
172+
'instructions' => '',
173+
'name' => 'multiple',
174+
'type' => 'true_false',
175+
'ui' => 1,
176+
));
177+
178+
179+
// ui
180+
acf_render_field_setting( $field, array(
181+
'label' => __('Stylised UI','acf'),
182+
'instructions' => '',
183+
'name' => 'ui',
184+
'type' => 'true_false',
185+
'ui' => 1,
186+
));
187+
188+
189+
// return_format
190+
acf_render_field_setting( $field, array(
191+
'label' => __('Return Format','acf'),
192+
'instructions' => __('Specify the value returned','acf'),
193+
'type' => 'select',
194+
'name' => 'return_format',
195+
'choices' => array(
196+
'value' => __('Value','acf'),
197+
'label' => __('Label','acf'),
198+
'array' => __('Both (Array)','acf')
199+
)
200+
));
201+
202+
203+
// max
204+
acf_render_field_setting( $field, array(
205+
'label' => __('Field key','acf'),
206+
'instructions' => __('Specify field key using dot notation, max 2 levels, options supported','acf'),
207+
'type' => 'text',
208+
'placeholder' => 'repeater.title or options.repeater.title',
209+
'name' => 'field_key',
210+
));
211+
212+
}
213+
}
214+
215+
acf_register_field_type( 'acf_field_dynamic_select_extension' );
216+
217+
endif; // class_exists check

fields/class-acf-field-latest_posts.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ function render_field_settings( $field ) {
7272
'type' => 'select',
7373
'name' => 'post_type',
7474
'choices' => acf_get_pretty_post_types(),
75-
'multiple' => 0,
76-
'ui' => 0,
77-
'allow_null' => 1,
75+
'multiple' => 1,
76+
'ui' => 1,
77+
'allow_null' => 0,
7878
'placeholder' => __("All post types",'acf'),
7979
));
8080

0 commit comments

Comments
 (0)