Advanced Custom Fields: Extended - Version 0.8.5.5

Version Description

  • Field: Button - Added nominative JS hooks action('acfe/fields/button/before/name=my_field', response, $el, data);
  • Field: Button - Added nominative JS hooks action('acfe/fields/button/success/name=my_field', response, $el, data);
  • Field: Button - Added nominative JS hooks action('acfe/fields/button/complete/name=my_field', response, $el, data);
  • Field: Button - Deprecated JS hooks acfe/fields/button/before_ajax & acfe/fields/button/ajax_success. Replaced by acfe/fields/button/before & acfe/fields/button/success
  • Field: Flexible Content - Added generic acfe/flexible/thumbnail hook
  • Module: Dynamic Forms - Fixed a bug where 2 forms with 2 fields with the same name on the same page, will override default_value during render
  • Module: Dynamic Forms - Fixed a nasty bug with "Post Action" which could trigger an infinite loop when using Elementor & YOAST. See bug report: https://github.com/elementor/elementor/issues/10998
  • Module: Dynamic Forms - Fixed jQuery not recognized on form success in some specific case
  • Module: Dynamic Forms - Added "No form element" compatibility allowing validation settings to be applied when form tag isn't printed
  • Module: Dynamic Forms - Fixed typo in "Custom Action" code example
  • Module: Dynamic Forms - Added to the possibility to use get_field('my_field') to retrieve form input value inside acfe/form/load hooks
  • Module: Single Meta Save - Fixed hook arguments which could trigger a PHP error in some specific cases
  • General: Readme - Added Flexible Content Settings Modal example
Download this release

Release Info

Developer hwk-fr
Plugin Icon 128x128 Advanced Custom Fields: Extended
Version 0.8.5.5
Comparing to
See all releases

Code changes from version 0.8.5 to 0.8.5.5

acf-extended.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * Plugin Name: Advanced Custom Fields: Extended
4
  * Description: Enhancement Suite which improves Advanced Custom Fields administration
5
- * Version: 0.8.5
6
  * Author: ACF Extended
7
  * Author URI: https://www.acf-extended.com
8
  * Text Domain: acfe
@@ -16,7 +16,7 @@ if(!class_exists('ACFE')):
16
  class ACFE{
17
 
18
  // Version
19
- var $version = '0.8.5';
20
 
21
  // Settings
22
  var $settings = array();
2
  /**
3
  * Plugin Name: Advanced Custom Fields: Extended
4
  * Description: Enhancement Suite which improves Advanced Custom Fields administration
5
+ * Version: 0.8.5.5
6
  * Author: ACF Extended
7
  * Author URI: https://www.acf-extended.com
8
  * Text Domain: acfe
16
  class ACFE{
17
 
18
  // Version
19
+ var $version = '0.8.5.5';
20
 
21
  // Settings
22
  var $settings = array();
assets/acf-extended-fc-modal-select.js CHANGED
@@ -1,259 +1,259 @@
1
- (function($){
2
-
3
- if(typeof acf === 'undefined')
4
- return;
5
-
6
- /*
7
- * Init
8
- */
9
- var flexible = acf.getFieldType('flexible_content');
10
- var model = flexible.prototype;
11
-
12
- /*
13
- * Actions
14
- */
15
- model.acfeModalSelect = function(e, $el){
16
-
17
- // Get Flexible
18
- var flexible = this;
19
-
20
- // Validate
21
- if(!flexible.validateAdd())
22
- return false;
23
-
24
- // Layout
25
- var $layout_source = null;
26
-
27
- if($el.hasClass('acf-icon'))
28
- $layout_source = $el.closest('.layout');
29
-
30
- // Get Available Layouts
31
- var layouts = flexible.getPopupHTML();
32
-
33
- // Init Categories
34
- var categories = {
35
- array: [],
36
- html: ''
37
- };
38
-
39
- function SearchArray(element, array){
40
-
41
- var len = array.length,
42
- str = element.toString().toLowerCase();
43
-
44
- for(var i = 0; i < len; i++){
45
- if(array[i].toLowerCase() === str){
46
- return i;
47
- }
48
- }
49
-
50
- return -1;
51
-
52
- }
53
-
54
- // Get Categories
55
- $(layouts).find('li a span').each(function(){
56
-
57
- var $link = $(this);
58
-
59
- if(!$link.data('acfe-flexible-category'))
60
- return true;
61
-
62
- var category = $link.data('acfe-flexible-category');
63
-
64
- $.each(category, function(i, c){
65
-
66
- if(SearchArray(c, categories.array) !== -1)
67
- return true;
68
-
69
- categories.array.push(c);
70
-
71
- });
72
-
73
- });
74
-
75
- // Categories HTML
76
- if(categories.array.length){
77
-
78
- categories.array.sort();
79
-
80
- categories.html += '<h2 class="acfe-flexible-categories nav-tab-wrapper">';
81
-
82
- categories.html += '<a href="#" data-acfe-flexible-category="acfe-all" class="nav-tab nav-tab-active"><span class="dashicons dashicons-menu"></span></a>';
83
-
84
- $(categories.array).each(function(k, category){
85
-
86
- categories.html += '<a href="#" data-acfe-flexible-category="' + category + '" class="nav-tab">' + category + '</a>';
87
-
88
- });
89
-
90
- categories.html += '</h2>';
91
-
92
- }
93
-
94
- // Modal Title
95
- var $modal_title = 'Add Row';
96
-
97
- if(flexible.has('acfeFlexibleModalTitle'))
98
- $modal_title = flexible.get('acfeFlexibleModalTitle');
99
-
100
- // Create Modal
101
- var $modal = $('' +
102
- '<div class="acfe-modal">' +
103
-
104
- categories.html +
105
- '<div class="acfe-flex-container">' +
106
- layouts +
107
- '</div>' +
108
-
109
- '</div>'
110
-
111
- ).appendTo('body');
112
-
113
- // Open Modal
114
- var $modal = acfe.modal.open($modal, {
115
- title: $modal_title,
116
- size: 'full',
117
- destroy: true
118
- });
119
-
120
- // Modal: Columns
121
- if(flexible.has('acfeFlexibleModalCol'))
122
- $modal.find('.acfe-modal-content .acfe-flex-container').addClass('acfe-col-' + flexible.get('acfeFlexibleModalCol'));
123
-
124
- // Modal: ACF autofocus fix
125
- $modal.find('li:first-of-type a').blur();
126
-
127
- // count layouts
128
- var $layouts = flexible.$layouts();
129
- var countLayouts = function(name){
130
-
131
- return $layouts.filter(function(){
132
- return $(this).data('layout') === name;
133
- }).length;
134
-
135
- };
136
-
137
- $modal.find('a[data-layout]').each(function(){
138
-
139
- // vars
140
- var $a = $(this);
141
- var min = $a.data('min') || 0;
142
- var max = $a.data('max') || 0;
143
- var name = $a.data('layout') || '';
144
- var count = countLayouts( name );
145
-
146
- // max
147
- if(max && count >= max){
148
- $a.addClass('disabled');
149
- return;
150
- }
151
-
152
- // min
153
- if(min && count < min){
154
-
155
- // vars
156
- var required = min - count;
157
- var title = acf.__('{required} {label} {identifier} required (min {min})');
158
- var identifier = acf._n('layout', 'layouts', required);
159
-
160
- // translate
161
- title = title.replace('{required}', required);
162
- title = title.replace('{label}', name); // 5.5.0
163
- title = title.replace('{identifier}', identifier);
164
- title = title.replace('{min}', min);
165
-
166
- // badge
167
- $a.append('<span class="badge" title="' + title + '">' + required + '</span>');
168
-
169
- }
170
-
171
- });
172
-
173
- // Modal: Click Categories
174
- $modal.find('.acfe-flexible-categories a').click(function(e){
175
-
176
- e.preventDefault();
177
-
178
- var $link = $(this);
179
-
180
- $link.closest('.acfe-flexible-categories').find('a').removeClass('nav-tab-active');
181
- $link.addClass('nav-tab-active');
182
-
183
- var selected_category = $link.data('acfe-flexible-category');
184
-
185
- $modal.find('a[data-layout] span').each(function(){
186
-
187
- // Get span
188
- var $span = $(this);
189
-
190
- // Show All
191
- $span.closest('li').show();
192
-
193
- var category = $span.data('acfe-flexible-category');
194
-
195
- // Specific category
196
- if(selected_category !== 'acfe-all'){
197
-
198
- // Hide All
199
- $span.closest('li').hide();
200
-
201
- $.each(category, function(i, c){
202
-
203
- if(selected_category.toLowerCase() === c.toLowerCase()){
204
-
205
- $span.closest('li').show();
206
-
207
- return false;
208
-
209
- }
210
-
211
- });
212
-
213
- }
214
-
215
- });
216
-
217
- });
218
-
219
- // Modal: Click Add Layout
220
- $modal.on('click', 'a[data-layout]', function(e){
221
-
222
- e.preventDefault();
223
-
224
- // Close modal
225
- acfe.modal.close(true);
226
-
227
- // Add layout
228
- var $layout_added = flexible.add({
229
- layout: $(this).data('layout'),
230
- before: $layout_source
231
- });
232
-
233
- });
234
-
235
- }
236
-
237
- /*
238
- * Spawn
239
- */
240
- acf.addAction('new_field/type=flexible_content', function(flexible){
241
-
242
- if(!flexible.has('acfeFlexibleModal'))
243
- return;
244
-
245
- // Vars
246
- var $clones = flexible.$clones();
247
-
248
- if($clones.length <= 1)
249
- return;
250
-
251
- // Remove native ACF Tooltip action
252
- flexible.removeEvents({'click [data-name="add-layout"]': 'onClickAdd'});
253
-
254
- // Add ACF Extended Modal action
255
- flexible.addEvents({'click [data-name="add-layout"]': 'acfeModalSelect'});
256
-
257
- });
258
-
259
  })(jQuery);
1
+ (function($){
2
+
3
+ if(typeof acf === 'undefined')
4
+ return;
5
+
6
+ /*
7
+ * Init
8
+ */
9
+ var flexible = acf.getFieldType('flexible_content');
10
+ var model = flexible.prototype;
11
+
12
+ /*
13
+ * Actions
14
+ */
15
+ model.acfeModalSelect = function(e, $el){
16
+
17
+ // Get Flexible
18
+ var flexible = this;
19
+
20
+ // Validate
21
+ if(!flexible.validateAdd())
22
+ return false;
23
+
24
+ // Layout
25
+ var $layout_source = null;
26
+
27
+ if($el.hasClass('acf-icon'))
28
+ $layout_source = $el.closest('.layout');
29
+
30
+ // Get Available Layouts
31
+ var layouts = flexible.getPopupHTML();
32
+
33
+ // Init Categories
34
+ var categories = {
35
+ array: [],
36
+ html: ''
37
+ };
38
+
39
+ function SearchArray(element, array){
40
+
41
+ var len = array.length,
42
+ str = element.toString().toLowerCase();
43
+
44
+ for(var i = 0; i < len; i++){
45
+ if(array[i].toLowerCase() === str){
46
+ return i;
47
+ }
48
+ }
49
+
50
+ return -1;
51
+
52
+ }
53
+
54
+ // Get Categories
55
+ $(layouts).find('li a span').each(function(){
56
+
57
+ var $link = $(this);
58
+
59
+ if(!$link.data('acfe-flexible-category'))
60
+ return true;
61
+
62
+ var category = $link.data('acfe-flexible-category');
63
+
64
+ $.each(category, function(i, c){
65
+
66
+ if(SearchArray(c, categories.array) !== -1)
67
+ return true;
68
+
69
+ categories.array.push(c);
70
+
71
+ });
72
+
73
+ });
74
+
75
+ // Categories HTML
76
+ if(categories.array.length){
77
+
78
+ categories.array.sort();
79
+
80
+ categories.html += '<h2 class="acfe-flexible-categories nav-tab-wrapper">';
81
+
82
+ categories.html += '<a href="#" data-acfe-flexible-category="acfe-all" class="nav-tab nav-tab-active"><span class="dashicons dashicons-menu"></span></a>';
83
+
84
+ $(categories.array).each(function(k, category){
85
+
86
+ categories.html += '<a href="#" data-acfe-flexible-category="' + category + '" class="nav-tab">' + category + '</a>';
87
+
88
+ });
89
+
90
+ categories.html += '</h2>';
91
+
92
+ }
93
+
94
+ // Modal Title
95
+ var $modal_title = 'Add Row';
96
+
97
+ if(flexible.has('acfeFlexibleModalTitle'))
98
+ $modal_title = flexible.get('acfeFlexibleModalTitle');
99
+
100
+ // Create Modal
101
+ var $modal = $('' +
102
+ '<div class="acfe-modal">' +
103
+
104
+ categories.html +
105
+ '<div class="acfe-flex-container">' +
106
+ layouts +
107
+ '</div>' +
108
+
109
+ '</div>'
110
+
111
+ ).appendTo('body');
112
+
113
+ // Open Modal
114
+ var $modal = acfe.modal.open($modal, {
115
+ title: $modal_title,
116
+ size: 'full',
117
+ destroy: true
118
+ });
119
+
120
+ // Modal: Columns
121
+ if(flexible.has('acfeFlexibleModalCol'))
122
+ $modal.find('.acfe-modal-content .acfe-flex-container').addClass('acfe-col-' + flexible.get('acfeFlexibleModalCol'));
123
+
124
+ // Modal: ACF autofocus fix
125
+ $modal.find('li:first-of-type a').blur();
126
+
127
+ // count layouts
128
+ var $layouts = flexible.$layouts();
129
+ var countLayouts = function(name){
130
+
131
+ return $layouts.filter(function(){
132
+ return $(this).data('layout') === name;
133
+ }).length;
134
+
135
+ };
136
+
137
+ $modal.find('a[data-layout]').each(function(){
138
+
139
+ // vars
140
+ var $a = $(this);
141
+ var min = $a.data('min') || 0;
142
+ var max = $a.data('max') || 0;
143
+ var name = $a.data('layout') || '';
144
+ var count = countLayouts( name );
145
+
146
+ // max
147
+ if(max && count >= max){
148
+ $a.addClass('disabled');
149
+ return;
150
+ }
151
+
152
+ // min
153
+ if(min && count < min){
154
+
155
+ // vars
156
+ var required = min - count;
157
+ var title = acf.__('{required} {label} {identifier} required (min {min})');
158
+ var identifier = acf._n('layout', 'layouts', required);
159
+
160
+ // translate
161
+ title = title.replace('{required}', required);
162
+ title = title.replace('{label}', name); // 5.5.0
163
+ title = title.replace('{identifier}', identifier);
164
+ title = title.replace('{min}', min);
165
+
166
+ // badge
167
+ $a.append('<span class="badge" title="' + title + '">' + required + '</span>');
168
+
169
+ }
170
+
171
+ });
172
+
173
+ // Modal: Click Categories
174
+ $modal.find('.acfe-flexible-categories a').click(function(e){
175
+
176
+ e.preventDefault();
177
+
178
+ var $link = $(this);
179
+
180
+ $link.closest('.acfe-flexible-categories').find('a').removeClass('nav-tab-active');
181
+ $link.addClass('nav-tab-active');
182
+
183
+ var selected_category = $link.data('acfe-flexible-category');
184
+
185
+ $modal.find('a[data-layout] span').each(function(){
186
+
187
+ // Get span
188
+ var $span = $(this);
189
+
190
+ // Show All
191
+ $span.closest('li').show();
192
+
193
+ var category = $span.data('acfe-flexible-category');
194
+
195
+ // Specific category
196
+ if(selected_category !== 'acfe-all'){
197
+
198
+ // Hide All
199
+ $span.closest('li').hide();
200
+
201
+ $.each(category, function(i, c){
202
+
203
+ if(selected_category.toLowerCase() === c.toLowerCase()){
204
+
205
+ $span.closest('li').show();
206
+
207
+ return false;
208
+
209
+ }
210
+
211
+ });
212
+
213
+ }
214
+
215
+ });
216
+
217
+ });
218
+
219
+ // Modal: Click Add Layout
220
+ $modal.on('click', 'a[data-layout]', function(e){
221
+
222
+ e.preventDefault();
223
+
224
+ // Close modal
225
+ acfe.modal.close(true);
226
+
227
+ // Add layout
228
+ var $layout_added = flexible.add({
229
+ layout: $(this).data('layout'),
230
+ before: $layout_source
231
+ });
232
+
233
+ });
234
+
235
+ }
236
+
237
+ /*
238
+ * Spawn
239
+ */
240
+ acf.addAction('new_field/type=flexible_content', function(flexible){
241
+
242
+ if(!flexible.has('acfeFlexibleModal'))
243
+ return;
244
+
245
+ // Vars
246
+ var $clones = flexible.$clones();
247
+
248
+ if($clones.length <= 1)
249
+ return;
250
+
251
+ // Remove native ACF Tooltip action
252
+ flexible.removeEvents({'click [data-name="add-layout"]': 'onClickAdd'});
253
+
254
+ // Add ACF Extended Modal action
255
+ flexible.addEvents({'click [data-name="add-layout"]': 'acfeModalSelect'});
256
+
257
+ });
258
+
259
  })(jQuery);
assets/acf-extended-fields.js CHANGED
@@ -484,7 +484,13 @@ function acfe_recaptcha(){
484
  field_key: this.get('key')
485
  };
486
 
487
- acf.doAction('acfe/fields/button/before_ajax', this.$el, data);
 
 
 
 
 
 
488
 
489
  // ajax
490
  $.ajax({
@@ -493,11 +499,33 @@ function acfe_recaptcha(){
493
  type: 'post',
494
  dataType: 'json',
495
  context: this,
 
 
496
  success: function(response){
497
 
498
- acf.doAction('acfe/fields/button/ajax_success', response, this.$el, data);
 
 
 
 
 
 
499
 
 
 
 
 
 
 
 
 
 
 
 
 
500
  }
 
 
501
  });
502
 
503
  }
484
  field_key: this.get('key')
485
  };
486
 
487
+ // Deprecated
488
+ acf.doAction('acfe/fields/button/before_ajax', this.$el, data);
489
+
490
+ // Actions
491
+ acf.doAction('acfe/fields/button/before', this.$el, data);
492
+ acf.doAction('acfe/fields/button/before/key=' + this.get('key'), this.$el, data);
493
+ acf.doAction('acfe/fields/button/before/name=' + this.get('name'), this.$el, data);
494
 
495
  // ajax
496
  $.ajax({
499
  type: 'post',
500
  dataType: 'json',
501
  context: this,
502
+
503
+ // Success
504
  success: function(response){
505
 
506
+ // Deprecated
507
+ acf.doAction('acfe/fields/button/ajax_success', response, this.$el, data);
508
+
509
+ // Actions
510
+ acf.doAction('acfe/fields/button/success', response, this.$el, data);
511
+ acf.doAction('acfe/fields/button/success/key=' + this.get('key'), response, this.$el, data);
512
+ acf.doAction('acfe/fields/button/success/name=' + this.get('name'), response, this.$el, data);
513
 
514
+ },
515
+
516
+ // Complete
517
+ complete: function(xhr){
518
+
519
+ var response = xhr.responseText;
520
+
521
+ // Actions
522
+ acf.doAction('acfe/fields/button/complete', response, this.$el, data);
523
+ acf.doAction('acfe/fields/button/complete/key=' + this.get('key'), response, this.$el, data);
524
+ acf.doAction('acfe/fields/button/complete/name=' + this.get('name'), response, this.$el, data);
525
+
526
  }
527
+
528
+
529
  });
530
 
531
  }
assets/acf-extended-form.js CHANGED
@@ -3,14 +3,33 @@
3
  if(typeof acf === 'undefined')
4
  return;
5
 
6
- $(document).ready(function($){
7
-
8
  if($('.acfe-form[data-hide-unload="1"]').length){
9
-
10
  acf.unload.disable();
11
-
12
  }
13
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  });
15
 
16
  // Allow conditions to work within wrapped div
@@ -62,7 +81,7 @@
62
  // Datepicker: Add field class
63
  acf.addAction('new_field/type=date_picker', function(field){
64
 
65
- var $form = field.$el.closest('form.acfe-form');
66
 
67
  if(!$form.length)
68
  return;
@@ -77,7 +96,7 @@
77
  // Google Maps: Add field class
78
  acf.addAction('new_field/type=google_map', function(field){
79
 
80
- var $form = field.$el.closest('form.acfe-form');
81
 
82
  if(!$form.length)
83
  return;
@@ -92,7 +111,7 @@
92
  // Error: Move error
93
  acf.addAction('invalid_field', function(field){
94
 
95
- var $form = field.$el.closest('form.acfe-form');
96
 
97
  if(!$form.length)
98
  return;
3
  if(typeof acf === 'undefined')
4
  return;
5
 
6
+ acf.addAction('prepare', function(){
7
+
8
  if($('.acfe-form[data-hide-unload="1"]').length){
9
+
10
  acf.unload.disable();
11
+
12
  }
13
+
14
+ if($('.acfe-form-success').length){
15
+
16
+ if(window.history.replaceState){
17
+ window.history.replaceState(null, null, window.location.href);
18
+ }
19
+
20
+ $('.acfe-form-success').each(function(){
21
+
22
+ var form_name = $(this).data('form-name');
23
+ var form_id = $(this).data('form-id');
24
+
25
+ acf.doAction('acfe/form/submit/success');
26
+ acf.doAction('acfe/form/submit/success/id=' + form_id);
27
+ acf.doAction('acfe/form/submit/success/name=' + form_name);
28
+
29
+ });
30
+
31
+ }
32
+
33
  });
34
 
35
  // Allow conditions to work within wrapped div
81
  // Datepicker: Add field class
82
  acf.addAction('new_field/type=date_picker', function(field){
83
 
84
+ var $form = field.$el.closest('.acfe-form');
85
 
86
  if(!$form.length)
87
  return;
96
  // Google Maps: Add field class
97
  acf.addAction('new_field/type=google_map', function(field){
98
 
99
+ var $form = field.$el.closest('.acfe-form');
100
 
101
  if(!$form.length)
102
  return;
111
  // Error: Move error
112
  acf.addAction('invalid_field', function(field){
113
 
114
+ var $form = field.$el.closest('.acfe-form');
115
 
116
  if(!$form.length)
117
  return;
includes/admin/tools/dbt-import.php CHANGED
@@ -1,219 +1,219 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- // Check setting
7
- if(!acf_get_setting('acfe/modules/dynamic_block_types'))
8
- return;
9
-
10
- if(!class_exists('ACFE_Admin_Tool_Import_DBT')):
11
-
12
- class ACFE_Admin_Tool_Import_DBT extends ACF_Admin_Tool{
13
-
14
- function initialize(){
15
-
16
- // vars
17
- $this->name = 'acfe_tool_dbt_import';
18
- $this->title = __('Import Block Types');
19
- $this->icon = 'dashicons-upload';
20
-
21
- }
22
-
23
- function html(){
24
-
25
- ?>
26
- <p><?php _e('Import Block Types', 'acf'); ?></p>
27
-
28
- <div class="acf-fields">
29
- <?php
30
-
31
- acf_render_field_wrap(array(
32
- 'label' => __('Select File', 'acf'),
33
- 'type' => 'file',
34
- 'name' => 'acf_import_file',
35
- 'value' => false,
36
- 'uploader' => 'basic',
37
- ));
38
-
39
- ?>
40
- </div>
41
-
42
- <p class="acf-submit">
43
- <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
- </p>
45
- <?php
46
-
47
- }
48
-
49
- function submit(){
50
-
51
- // Check file size.
52
- if(empty($_FILES['acf_import_file']['size']))
53
- return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
-
55
- // Get file data.
56
- $file = $_FILES['acf_import_file'];
57
-
58
- // Check errors.
59
- if($file['error'])
60
- return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
-
62
- // Check file type.
63
- if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
- return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
-
66
- // Read JSON.
67
- $json = file_get_contents($file['tmp_name']);
68
- $json = json_decode($json, true);
69
-
70
- // Check if empty.
71
- if(!$json || !is_array($json))
72
- return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
-
74
- $ids = array();
75
-
76
- $dynamic_block_types = acfe_settings('modules.dynamic_block_type.data');
77
-
78
- // Loop over json
79
- foreach($json as $block_type_name => $args){
80
-
81
- // Check if already exists
82
- if(isset($dynamic_block_types[$block_type_name])){
83
-
84
- acf_add_admin_notice(__("Block type {$dynamic_block_types[$block_type_name]['title']} already exists. Import aborted."), 'warning');
85
- continue;
86
-
87
- }
88
-
89
- // Vars
90
- $title = $args['title'];
91
- $name = $block_type_name;
92
-
93
- // Insert post
94
- $post_id = wp_insert_post(array(
95
- 'post_title' => $title,
96
- 'post_name' => $name,
97
- 'post_type' => 'acfe-dbt',
98
- 'post_status' => 'publish'
99
- ));
100
-
101
- // Insert error
102
- if(is_wp_error($post_id)){
103
-
104
- acf_add_admin_notice(__("Something went wrong with the block type {$title}. Import aborted."), 'warning');
105
- continue;
106
-
107
- }
108
-
109
- // Register Args
110
- update_field('name', $name, $post_id);
111
- update_field('title', $args['title'], $post_id);
112
- update_field('description', $args['description'], $post_id);
113
- update_field('category', $args['category'], $post_id);
114
- update_field('keywords', acf_encode_choices($args['keywords'], false), $post_id);
115
- update_field('post_types', $args['post_types'], $post_id);
116
- update_field('mode', $args['mode'], $post_id);
117
- update_field('align', $args['align'], $post_id);
118
- update_field('render_callback', $args['render_callback'], $post_id);
119
- update_field('enqueue_assets', $args['enqueue_assets'], $post_id);
120
-
121
- // Render Template
122
- if(!empty($args['render_template']))
123
- update_field('render_template', str_replace(ACFE_THEME_PATH . '/', '', $args['render_template']), $post_id);
124
-
125
- // Enqueue Style
126
- if(!empty($args['enqueue_style']))
127
- update_field('enqueue_style', str_replace(ACFE_THEME_URL . '/', '', $args['enqueue_style']), $post_id);
128
-
129
- // Enqueue Script
130
- if(!empty($args['enqueue_script']))
131
- update_field('enqueue_script', str_replace(ACFE_THEME_URL . '/', '', $args['enqueue_script']), $post_id);
132
-
133
- // Align
134
- if(empty($args['align']))
135
- update_field('align', 'none', $post_id);
136
-
137
- // Icon
138
- if(!empty($args['icon'])){
139
-
140
- // Simple
141
- if(is_string($args['icon'])){
142
-
143
- update_field('icon_type', 'simple', $post_id);
144
-
145
- update_field('icon_text', $args['icon'], $post_id);
146
-
147
- }
148
-
149
- // Colors
150
- elseif(is_array($args['icon'])){
151
-
152
- update_field('icon_type', 'colors', $post_id);
153
-
154
- update_field('icon_background', $args['icon']['background'], $post_id);
155
- update_field('icon_foreground', $args['icon']['foreground'], $post_id);
156
- update_field('icon_src', $args['icon']['src'], $post_id);
157
-
158
- }
159
-
160
- }
161
-
162
- // Supports: Align
163
- update_field('supports_align', $args['supports']['align'], $post_id);
164
-
165
- if(is_array($args['supports']['align'])){
166
-
167
- update_field('supports_align_args', acf_encode_choices($args['supports']['align'], false), $post_id);
168
-
169
- }
170
-
171
- // Supports: Mode
172
- update_field('supports_mode', $args['supports']['mode'], $post_id);
173
-
174
- // Supports: Multiple
175
- update_field('supports_multiple', $args['supports']['multiple'], $post_id);
176
-
177
- // Create ACFE option
178
- $dynamic_block_types[$block_type_name] = $args;
179
-
180
- // Sort keys ASC
181
- ksort($dynamic_block_types);
182
-
183
- // Update ACFE option
184
- acfe_settings('modules.dynamic_block_type.data', $dynamic_block_types, true);
185
-
186
- // append message
187
- $ids[] = $post_id;
188
-
189
- }
190
-
191
- if(empty($ids))
192
- return;
193
-
194
- // Count total
195
- $total = count($ids);
196
-
197
- // Generate text
198
- $text = sprintf(_n('1 block type imported', '%s block types imported', $total, 'acf'), $total);
199
-
200
- // Add links to text
201
- $links = array();
202
- foreach($ids as $id){
203
-
204
- $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
205
-
206
- }
207
-
208
- $text .= ': ' . implode(', ', $links);
209
-
210
- // Add notice
211
- acf_add_admin_notice($text, 'success');
212
-
213
- }
214
-
215
- }
216
-
217
- acf_register_admin_tool('ACFE_Admin_Tool_Import_DBT');
218
-
219
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ // Check setting
7
+ if(!acf_get_setting('acfe/modules/dynamic_block_types'))
8
+ return;
9
+
10
+ if(!class_exists('ACFE_Admin_Tool_Import_DBT')):
11
+
12
+ class ACFE_Admin_Tool_Import_DBT extends ACF_Admin_Tool{
13
+
14
+ function initialize(){
15
+
16
+ // vars
17
+ $this->name = 'acfe_tool_dbt_import';
18
+ $this->title = __('Import Block Types');
19
+ $this->icon = 'dashicons-upload';
20
+
21
+ }
22
+
23
+ function html(){
24
+
25
+ ?>
26
+ <p><?php _e('Import Block Types', 'acf'); ?></p>
27
+
28
+ <div class="acf-fields">
29
+ <?php
30
+
31
+ acf_render_field_wrap(array(
32
+ 'label' => __('Select File', 'acf'),
33
+ 'type' => 'file',
34
+ 'name' => 'acf_import_file',
35
+ 'value' => false,
36
+ 'uploader' => 'basic',
37
+ ));
38
+
39
+ ?>
40
+ </div>
41
+
42
+ <p class="acf-submit">
43
+ <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
+ </p>
45
+ <?php
46
+
47
+ }
48
+
49
+ function submit(){
50
+
51
+ // Check file size.
52
+ if(empty($_FILES['acf_import_file']['size']))
53
+ return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
+
55
+ // Get file data.
56
+ $file = $_FILES['acf_import_file'];
57
+
58
+ // Check errors.
59
+ if($file['error'])
60
+ return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
+
62
+ // Check file type.
63
+ if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
+ return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
+
66
+ // Read JSON.
67
+ $json = file_get_contents($file['tmp_name']);
68
+ $json = json_decode($json, true);
69
+
70
+ // Check if empty.
71
+ if(!$json || !is_array($json))
72
+ return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
+
74
+ $ids = array();
75
+
76
+ $dynamic_block_types = acfe_settings('modules.dynamic_block_type.data');
77
+
78
+ // Loop over json
79
+ foreach($json as $block_type_name => $args){
80
+
81
+ // Check if already exists
82
+ if(isset($dynamic_block_types[$block_type_name])){
83
+
84
+ acf_add_admin_notice(__("Block type {$dynamic_block_types[$block_type_name]['title']} already exists. Import aborted."), 'warning');
85
+ continue;
86
+
87
+ }
88
+
89
+ // Vars
90
+ $title = $args['title'];
91
+ $name = $block_type_name;
92
+
93
+ // Insert post
94
+ $post_id = wp_insert_post(array(
95
+ 'post_title' => $title,
96
+ 'post_name' => $name,
97
+ 'post_type' => 'acfe-dbt',
98
+ 'post_status' => 'publish'
99
+ ));
100
+
101
+ // Insert error
102
+ if(is_wp_error($post_id)){
103
+
104
+ acf_add_admin_notice(__("Something went wrong with the block type {$title}. Import aborted."), 'warning');
105
+ continue;
106
+
107
+ }
108
+
109
+ // Register Args
110
+ update_field('name', $name, $post_id);
111
+ update_field('title', $args['title'], $post_id);
112
+ update_field('description', $args['description'], $post_id);
113
+ update_field('category', $args['category'], $post_id);
114
+ update_field('keywords', acf_encode_choices($args['keywords'], false), $post_id);
115
+ update_field('post_types', $args['post_types'], $post_id);
116
+ update_field('mode', $args['mode'], $post_id);
117
+ update_field('align', $args['align'], $post_id);
118
+ update_field('render_callback', $args['render_callback'], $post_id);
119
+ update_field('enqueue_assets', $args['enqueue_assets'], $post_id);
120
+
121
+ // Render Template
122
+ if(!empty($args['render_template']))
123
+ update_field('render_template', str_replace(ACFE_THEME_PATH . '/', '', $args['render_template']), $post_id);
124
+
125
+ // Enqueue Style
126
+ if(!empty($args['enqueue_style']))
127
+ update_field('enqueue_style', str_replace(ACFE_THEME_URL . '/', '', $args['enqueue_style']), $post_id);
128
+
129
+ // Enqueue Script
130
+ if(!empty($args['enqueue_script']))
131
+ update_field('enqueue_script', str_replace(ACFE_THEME_URL . '/', '', $args['enqueue_script']), $post_id);
132
+
133
+ // Align
134
+ if(empty($args['align']))
135
+ update_field('align', 'none', $post_id);
136
+
137
+ // Icon
138
+ if(!empty($args['icon'])){
139
+
140
+ // Simple
141
+ if(is_string($args['icon'])){
142
+
143
+ update_field('icon_type', 'simple', $post_id);
144
+
145
+ update_field('icon_text', $args['icon'], $post_id);
146
+
147
+ }
148
+
149
+ // Colors
150
+ elseif(is_array($args['icon'])){
151
+
152
+ update_field('icon_type', 'colors', $post_id);
153
+
154
+ update_field('icon_background', $args['icon']['background'], $post_id);
155
+ update_field('icon_foreground', $args['icon']['foreground'], $post_id);
156
+ update_field('icon_src', $args['icon']['src'], $post_id);
157
+
158
+ }
159
+
160
+ }
161
+
162
+ // Supports: Align
163
+ update_field('supports_align', $args['supports']['align'], $post_id);
164
+
165
+ if(is_array($args['supports']['align'])){
166
+
167
+ update_field('supports_align_args', acf_encode_choices($args['supports']['align'], false), $post_id);
168
+
169
+ }
170
+
171
+ // Supports: Mode
172
+ update_field('supports_mode', $args['supports']['mode'], $post_id);
173
+
174
+ // Supports: Multiple
175
+ update_field('supports_multiple', $args['supports']['multiple'], $post_id);
176
+
177
+ // Create ACFE option
178
+ $dynamic_block_types[$block_type_name] = $args;
179
+
180
+ // Sort keys ASC
181
+ ksort($dynamic_block_types);
182
+
183
+ // Update ACFE option
184
+ acfe_settings('modules.dynamic_block_type.data', $dynamic_block_types, true);
185
+
186
+ // append message
187
+ $ids[] = $post_id;
188
+
189
+ }
190
+
191
+ if(empty($ids))
192
+ return;
193
+
194
+ // Count total
195
+ $total = count($ids);
196
+
197
+ // Generate text
198
+ $text = sprintf(_n('1 block type imported', '%s block types imported', $total, 'acf'), $total);
199
+
200
+ // Add links to text
201
+ $links = array();
202
+ foreach($ids as $id){
203
+
204
+ $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
205
+
206
+ }
207
+
208
+ $text .= ': ' . implode(', ', $links);
209
+
210
+ // Add notice
211
+ acf_add_admin_notice($text, 'success');
212
+
213
+ }
214
+
215
+ }
216
+
217
+ acf_register_admin_tool('ACFE_Admin_Tool_Import_DBT');
218
+
219
  endif;
includes/admin/tools/dop-import.php CHANGED
@@ -1,209 +1,209 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- // Check setting
7
- if(!acf_get_setting('acfe/modules/dynamic_options_pages'))
8
- return;
9
-
10
- if(!class_exists('ACFE_Admin_Tool_Import_DOP')):
11
-
12
- class ACFE_Admin_Tool_Import_DOP extends ACF_Admin_Tool{
13
-
14
- function initialize(){
15
-
16
- // vars
17
- $this->name = 'acfe_tool_dop_import';
18
- $this->title = __('Import Options Pages');
19
- $this->icon = 'dashicons-upload';
20
-
21
- }
22
-
23
- function html(){
24
-
25
- ?>
26
- <p><?php _e('Import Options Pages', 'acf'); ?></p>
27
-
28
- <div class="acf-fields">
29
- <?php
30
-
31
- acf_render_field_wrap(array(
32
- 'label' => __('Select File', 'acf'),
33
- 'type' => 'file',
34
- 'name' => 'acf_import_file',
35
- 'value' => false,
36
- 'uploader' => 'basic',
37
- ));
38
-
39
- ?>
40
- </div>
41
-
42
- <p class="acf-submit">
43
- <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
- </p>
45
- <?php
46
-
47
- }
48
-
49
- function submit(){
50
-
51
- // Check file size.
52
- if(empty($_FILES['acf_import_file']['size']))
53
- return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
-
55
- // Get file data.
56
- $file = $_FILES['acf_import_file'];
57
-
58
- // Check errors.
59
- if($file['error'])
60
- return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
-
62
- // Check file type.
63
- if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
- return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
-
66
- // Read JSON.
67
- $json = file_get_contents($file['tmp_name']);
68
- $json = json_decode($json, true);
69
-
70
- // Check if empty.
71
- if(!$json || !is_array($json))
72
- return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
-
74
- $ids = array();
75
-
76
- $dynamic_options_pages = acfe_settings('modules.dynamic_option.data');
77
-
78
- $dynamic_options_sub_pages = array();
79
-
80
- // Loop over json
81
- foreach($json as $options_page_name => $args){
82
-
83
- // Check if already exists
84
- if(isset($dynamic_options_pages[$options_page_name])){
85
-
86
- acf_add_admin_notice(__("Options page {$dynamic_options_pages[$options_page_name]['page_title']} already exists. Import aborted."), 'warning');
87
- continue;
88
-
89
- }
90
-
91
- // Vars
92
- $title = $args['page_title'];
93
- $name = $options_page_name;
94
-
95
- // Insert post
96
- $post_id = wp_insert_post(array(
97
- 'post_title' => $title,
98
- 'post_name' => $name,
99
- 'post_type' => 'acfe-dop',
100
- 'post_status' => 'publish'
101
- ));
102
-
103
- // Insert error
104
- if(is_wp_error($post_id)){
105
-
106
- acf_add_admin_notice(__("Something went wrong with the options page {$title}. Import aborted."), 'warning');
107
- continue;
108
-
109
- }
110
-
111
- // Register Args
112
- update_field('page_title', $args['page_title'], $post_id);
113
- update_field('acfe_dop_name', $name, $post_id);
114
- update_field('menu_title', $args['menu_title'], $post_id);
115
- update_field('menu_slug', $args['menu_slug'], $post_id);
116
- update_field('capability', $args['capability'], $post_id);
117
- update_field('position', $args['position'], $post_id);
118
- update_field('parent_slug', $args['parent_slug'], $post_id);
119
- update_field('icon_url', $args['icon_url'], $post_id);
120
- update_field('redirect', $args['redirect'], $post_id);
121
- update_field('post_id', $args['post_id'], $post_id);
122
- update_field('autoload', $args['autoload'], $post_id);
123
- update_field('update_button', $args['update_button'], $post_id);
124
- update_field('updated_message', $args['updated_message'], $post_id);
125
-
126
- // Create ACFE option
127
- $dynamic_options_pages[$options_page_name] = $args;
128
-
129
- // Sort keys ASC
130
- ksort($dynamic_options_pages);
131
-
132
- // Update ACFE option
133
- acfe_settings('modules.dynamic_option.data', $dynamic_options_pages, true);
134
-
135
- // Append message
136
- $ids[] = $post_id;
137
-
138
- // Add Sub Page
139
- if(isset($args['parent_slug']) && !empty($args['parent_slug']))
140
- $dynamic_options_sub_pages[$post_id] = $args;
141
-
142
- }
143
-
144
- // Check if pages have been added
145
- if(empty($ids))
146
- return;
147
-
148
- // Update Options Sub Pages
149
- if(!empty($dynamic_options_sub_pages)){
150
-
151
- foreach($dynamic_options_sub_pages as $post_id => $args){
152
-
153
- // Get possible parent options pages
154
- $get_dop_parent = get_posts(array(
155
- 'post_type' => 'acfe-dop',
156
- 'posts_per_page' => 1,
157
- 'fields' => 'ids',
158
- 'meta_query' => array(
159
- array(
160
- 'key' => 'menu_slug',
161
- 'value' => $args['parent_slug']
162
- )
163
- )
164
- ));
165
-
166
- if(empty($get_dop_parent))
167
- continue;
168
-
169
- $parent = $get_dop_parent[0];
170
-
171
- // Update sub page post
172
- wp_update_post(array(
173
- 'ID' => $post_id,
174
- 'post_parent' => $parent,
175
- ));
176
-
177
- }
178
-
179
- }
180
-
181
- // Count total
182
- $total = count($ids);
183
-
184
- // Generate text
185
- $text = sprintf(_n('1 options page imported', '%s options pages imported', $total, 'acf'), $total);
186
-
187
- // Add links to text
188
- $links = array();
189
- foreach($ids as $id){
190
-
191
- $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
192
-
193
- }
194
-
195
- $text .= ': ' . implode(', ', $links);
196
-
197
- // Add notice
198
- acf_add_admin_notice($text, 'success');
199
-
200
- // Flush permalinks
201
- flush_rewrite_rules();
202
-
203
- }
204
-
205
- }
206
-
207
- acf_register_admin_tool('ACFE_Admin_Tool_Import_DOP');
208
-
209
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ // Check setting
7
+ if(!acf_get_setting('acfe/modules/dynamic_options_pages'))
8
+ return;
9
+
10
+ if(!class_exists('ACFE_Admin_Tool_Import_DOP')):
11
+
12
+ class ACFE_Admin_Tool_Import_DOP extends ACF_Admin_Tool{
13
+
14
+ function initialize(){
15
+
16
+ // vars
17
+ $this->name = 'acfe_tool_dop_import';
18
+ $this->title = __('Import Options Pages');
19
+ $this->icon = 'dashicons-upload';
20
+
21
+ }
22
+
23
+ function html(){
24
+
25
+ ?>
26
+ <p><?php _e('Import Options Pages', 'acf'); ?></p>
27
+
28
+ <div class="acf-fields">
29
+ <?php
30
+
31
+ acf_render_field_wrap(array(
32
+ 'label' => __('Select File', 'acf'),
33
+ 'type' => 'file',
34
+ 'name' => 'acf_import_file',
35
+ 'value' => false,
36
+ 'uploader' => 'basic',
37
+ ));
38
+
39
+ ?>
40
+ </div>
41
+
42
+ <p class="acf-submit">
43
+ <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
+ </p>
45
+ <?php
46
+
47
+ }
48
+
49
+ function submit(){
50
+
51
+ // Check file size.
52
+ if(empty($_FILES['acf_import_file']['size']))
53
+ return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
+
55
+ // Get file data.
56
+ $file = $_FILES['acf_import_file'];
57
+
58
+ // Check errors.
59
+ if($file['error'])
60
+ return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
+
62
+ // Check file type.
63
+ if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
+ return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
+
66
+ // Read JSON.
67
+ $json = file_get_contents($file['tmp_name']);
68
+ $json = json_decode($json, true);
69
+
70
+ // Check if empty.
71
+ if(!$json || !is_array($json))
72
+ return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
+
74
+ $ids = array();
75
+
76
+ $dynamic_options_pages = acfe_settings('modules.dynamic_option.data');
77
+
78
+ $dynamic_options_sub_pages = array();
79
+
80
+ // Loop over json
81
+ foreach($json as $options_page_name => $args){
82
+
83
+ // Check if already exists
84
+ if(isset($dynamic_options_pages[$options_page_name])){
85
+
86
+ acf_add_admin_notice(__("Options page {$dynamic_options_pages[$options_page_name]['page_title']} already exists. Import aborted."), 'warning');
87
+ continue;
88
+
89
+ }
90
+
91
+ // Vars
92
+ $title = $args['page_title'];
93
+ $name = $options_page_name;
94
+
95
+ // Insert post
96
+ $post_id = wp_insert_post(array(
97
+ 'post_title' => $title,
98
+ 'post_name' => $name,
99
+ 'post_type' => 'acfe-dop',
100
+ 'post_status' => 'publish'
101
+ ));
102
+
103
+ // Insert error
104
+ if(is_wp_error($post_id)){
105
+
106
+ acf_add_admin_notice(__("Something went wrong with the options page {$title}. Import aborted."), 'warning');
107
+ continue;
108
+
109
+ }
110
+
111
+ // Register Args
112
+ update_field('page_title', $args['page_title'], $post_id);
113
+ update_field('acfe_dop_name', $name, $post_id);
114
+ update_field('menu_title', $args['menu_title'], $post_id);
115
+ update_field('menu_slug', $args['menu_slug'], $post_id);
116
+ update_field('capability', $args['capability'], $post_id);
117
+ update_field('position', $args['position'], $post_id);
118
+ update_field('parent_slug', $args['parent_slug'], $post_id);
119
+ update_field('icon_url', $args['icon_url'], $post_id);
120
+ update_field('redirect', $args['redirect'], $post_id);
121
+ update_field('post_id', $args['post_id'], $post_id);
122
+ update_field('autoload', $args['autoload'], $post_id);
123
+ update_field('update_button', $args['update_button'], $post_id);
124
+ update_field('updated_message', $args['updated_message'], $post_id);
125
+
126
+ // Create ACFE option
127
+ $dynamic_options_pages[$options_page_name] = $args;
128
+
129
+ // Sort keys ASC
130
+ ksort($dynamic_options_pages);
131
+
132
+ // Update ACFE option
133
+ acfe_settings('modules.dynamic_option.data', $dynamic_options_pages, true);
134
+
135
+ // Append message
136
+ $ids[] = $post_id;
137
+
138
+ // Add Sub Page
139
+ if(isset($args['parent_slug']) && !empty($args['parent_slug']))
140
+ $dynamic_options_sub_pages[$post_id] = $args;
141
+
142
+ }
143
+
144
+ // Check if pages have been added
145
+ if(empty($ids))
146
+ return;
147
+
148
+ // Update Options Sub Pages
149
+ if(!empty($dynamic_options_sub_pages)){
150
+
151
+ foreach($dynamic_options_sub_pages as $post_id => $args){
152
+
153
+ // Get possible parent options pages
154
+ $get_dop_parent = get_posts(array(
155
+ 'post_type' => 'acfe-dop',
156
+ 'posts_per_page' => 1,
157
+ 'fields' => 'ids',
158
+ 'meta_query' => array(
159
+ array(
160
+ 'key' => 'menu_slug',
161
+ 'value' => $args['parent_slug']
162
+ )
163
+ )
164
+ ));
165
+
166
+ if(empty($get_dop_parent))
167
+ continue;
168
+
169
+ $parent = $get_dop_parent[0];
170
+
171
+ // Update sub page post
172
+ wp_update_post(array(
173
+ 'ID' => $post_id,
174
+ 'post_parent' => $parent,
175
+ ));
176
+
177
+ }
178
+
179
+ }
180
+
181
+ // Count total
182
+ $total = count($ids);
183
+
184
+ // Generate text
185
+ $text = sprintf(_n('1 options page imported', '%s options pages imported', $total, 'acf'), $total);
186
+
187
+ // Add links to text
188
+ $links = array();
189
+ foreach($ids as $id){
190
+
191
+ $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
192
+
193
+ }
194
+
195
+ $text .= ': ' . implode(', ', $links);
196
+
197
+ // Add notice
198
+ acf_add_admin_notice($text, 'success');
199
+
200
+ // Flush permalinks
201
+ flush_rewrite_rules();
202
+
203
+ }
204
+
205
+ }
206
+
207
+ acf_register_admin_tool('ACFE_Admin_Tool_Import_DOP');
208
+
209
  endif;
includes/admin/tools/dpt-import.php CHANGED
@@ -1,243 +1,243 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- // Check setting
7
- if(!acf_get_setting('acfe/modules/dynamic_post_types'))
8
- return;
9
-
10
- if(!class_exists('ACFE_Admin_Tool_Import_DPT')):
11
-
12
- class ACFE_Admin_Tool_Import_DPT extends ACF_Admin_Tool{
13
-
14
- function initialize(){
15
-
16
- // vars
17
- $this->name = 'acfe_tool_dpt_import';
18
- $this->title = __('Import Post Types');
19
- $this->icon = 'dashicons-upload';
20
-
21
- }
22
-
23
- function html(){
24
-
25
- ?>
26
- <p><?php _e('Import Post Types', 'acf'); ?></p>
27
-
28
- <div class="acf-fields">
29
- <?php
30
-
31
- acf_render_field_wrap(array(
32
- 'label' => __('Select File', 'acf'),
33
- 'type' => 'file',
34
- 'name' => 'acf_import_file',
35
- 'value' => false,
36
- 'uploader' => 'basic',
37
- ));
38
-
39
- ?>
40
- </div>
41
-
42
- <p class="acf-submit">
43
- <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
- </p>
45
- <?php
46
-
47
- }
48
-
49
- function submit(){
50
-
51
- // Check file size.
52
- if(empty($_FILES['acf_import_file']['size']))
53
- return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
-
55
- // Get file data.
56
- $file = $_FILES['acf_import_file'];
57
-
58
- // Check errors.
59
- if($file['error'])
60
- return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
-
62
- // Check file type.
63
- if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
- return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
-
66
- // Read JSON.
67
- $json = file_get_contents($file['tmp_name']);
68
- $json = json_decode($json, true);
69
-
70
- // Check if empty.
71
- if(!$json || !is_array($json))
72
- return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
-
74
- $ids = array();
75
-
76
- $dynamic_post_types = acfe_settings('modules.dynamic_post_type.data');
77
-
78
- // Loop over json
79
- foreach($json as $post_type_name => $args){
80
-
81
- // Check if already exists
82
- if(isset($dynamic_post_types[$post_type_name])){
83
-
84
- acf_add_admin_notice(__("Post type {$dynamic_post_types[$post_type_name]['label']} already exists. Import aborted."), 'warning');
85
- continue;
86
-
87
- }
88
-
89
- // Vars
90
- $title = $args['label'];
91
- $name = $post_type_name;
92
-
93
- // Insert post
94
- $post_id = wp_insert_post(array(
95
- 'post_title' => $title,
96
- 'post_name' => $name,
97
- 'post_type' => 'acfe-dpt',
98
- 'post_status' => 'publish'
99
- ));
100
-
101
- // Insert error
102
- if(is_wp_error($post_id)){
103
-
104
- acf_add_admin_notice(__("Something went wrong with the post type {$title}. Import aborted."), 'warning');
105
- continue;
106
-
107
- }
108
-
109
- // Register Args
110
- update_field('acfe_dpt_name', $post_type_name, $post_id);
111
- update_field('label', $args['label'], $post_id);
112
- update_field('description', $args['description'], $post_id);
113
- update_field('hierarchical', $args['hierarchical'], $post_id);
114
- update_field('supports', $args['supports'], $post_id);
115
- update_field('taxonomies', $args['taxonomies'], $post_id);
116
- update_field('public', $args['public'], $post_id);
117
- update_field('exclude_from_search', $args['exclude_from_search'], $post_id);
118
- update_field('publicly_queryable', $args['publicly_queryable'], $post_id);
119
- update_field('can_export', $args['can_export'], $post_id);
120
- update_field('delete_with_user', $args['delete_with_user'], $post_id);
121
-
122
- // Labels
123
- if(!empty($args['labels'])){
124
-
125
- foreach($args['labels'] as $label_key => $label_value){
126
-
127
- update_field('labels_' . $label_key, $label_value, $post_id);
128
-
129
- }
130
-
131
- }
132
-
133
- // Menu
134
- update_field('menu_position', $args['menu_position'], $post_id);
135
- update_field('menu_icon', $args['menu_icon'], $post_id);
136
- update_field('show_ui', $args['show_ui'], $post_id);
137
- update_field('show_in_menu', $args['show_in_menu'], $post_id);
138
- update_field('show_in_nav_menus', $args['show_in_nav_menus'], $post_id);
139
- update_field('show_in_admin_bar', $args['show_in_admin_bar'], $post_id);
140
-
141
- // Capability
142
- update_field('capability_type', acf_encode_choices($args['capability_type'], false), $post_id);
143
- update_field('map_meta_cap', $args['map_meta_cap'], $post_id);
144
-
145
- if(isset($args['capabilities']))
146
- update_field('capabilities', acf_encode_choices($args['capabilities'], false), $post_id);
147
-
148
- // Archive
149
- update_field('acfe_dpt_archive_template', $args['acfe_archive_template'], $post_id);
150
- update_field('acfe_dpt_archive_posts_per_page', $args['acfe_archive_ppp'], $post_id);
151
- update_field('acfe_dpt_archive_orderby', $args['acfe_archive_orderby'], $post_id);
152
- update_field('acfe_dpt_archive_order', $args['acfe_archive_order'], $post_id);
153
- update_field('has_archive', $args['has_archive'], $post_id);
154
-
155
- // Single
156
- update_field('acfe_dpt_single_template', $args['acfe_single_template'], $post_id);
157
- update_field('rewrite', $args['rewrite'], $post_id);
158
-
159
- // Admin
160
- update_field('acfe_dpt_admin_posts_per_page', $args['acfe_admin_ppp'], $post_id);
161
- update_field('acfe_dpt_admin_orderby', $args['acfe_admin_orderby'], $post_id);
162
- update_field('acfe_dpt_admin_order', $args['acfe_admin_order'], $post_id);
163
-
164
- // REST
165
- update_field('show_in_rest', $args['show_in_rest'], $post_id);
166
- update_field('rest_base', $args['rest_base'], $post_id);
167
- update_field('rest_controller_class', $args['rest_controller_class'], $post_id);
168
-
169
- // Has archive: override
170
- if($args['has_archive'])
171
- update_field('has_archive_slug', $args['has_archive'], $post_id);
172
-
173
- // Rewrite: override
174
- if($args['rewrite'] && is_array($args['rewrite'])){
175
-
176
- update_field('rewrite', true, $post_id);
177
-
178
- update_field('rewrite_args_select', true, $post_id);
179
-
180
- update_field('rewrite_args_acfe_dpt_rewrite_slug', $args['rewrite']['slug'], $post_id);
181
- update_field('rewrite_args_acfe_dpt_rewrite_with_front', $args['rewrite']['with_front'], $post_id);
182
- update_field('rewrite_args_feeds', $args['rewrite']['feeds'], $post_id);
183
- update_field('rewrite_args_pages', $args['rewrite']['pages'], $post_id);
184
-
185
- }
186
-
187
- // Show in menu (text)
188
- if($args['show_in_menu'] && is_string($args['show_in_menu']))
189
- update_field('show_in_menu_text', $args['show_in_menu'], $post_id);
190
-
191
- // Map meta cap
192
- if($args['map_meta_cap'] === false)
193
- update_field('map_meta_cap', 'false', $post_id);
194
-
195
- elseif($args['map_meta_cap'] === true)
196
- update_field('map_meta_cap', 'true', $post_id);
197
-
198
- // Create ACFE option
199
- $dynamic_post_types[$post_type_name] = $args;
200
-
201
- // Sort keys ASC
202
- ksort($dynamic_post_types);
203
-
204
- // Update ACFE option
205
- acfe_settings('modules.dynamic_post_type.data', $dynamic_post_types, true);
206
-
207
- // append message
208
- $ids[] = $post_id;
209
-
210
- }
211
-
212
- if(empty($ids))
213
- return;
214
-
215
- // Count total
216
- $total = count($ids);
217
-
218
- // Generate text
219
- $text = sprintf(_n('1 post type imported', '%s post types imported', $total, 'acf'), $total);
220
-
221
- // Add links to text
222
- $links = array();
223
- foreach($ids as $id){
224
-
225
- $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
226
-
227
- }
228
-
229
- $text .= ': ' . implode(', ', $links);
230
-
231
- // Add notice
232
- acf_add_admin_notice($text, 'success');
233
-
234
- // Flush permalinks
235
- flush_rewrite_rules();
236
-
237
- }
238
-
239
- }
240
-
241
- acf_register_admin_tool('ACFE_Admin_Tool_Import_DPT');
242
-
243
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ // Check setting
7
+ if(!acf_get_setting('acfe/modules/dynamic_post_types'))
8
+ return;
9
+
10
+ if(!class_exists('ACFE_Admin_Tool_Import_DPT')):
11
+
12
+ class ACFE_Admin_Tool_Import_DPT extends ACF_Admin_Tool{
13
+
14
+ function initialize(){
15
+
16
+ // vars
17
+ $this->name = 'acfe_tool_dpt_import';
18
+ $this->title = __('Import Post Types');
19
+ $this->icon = 'dashicons-upload';
20
+
21
+ }
22
+
23
+ function html(){
24
+
25
+ ?>
26
+ <p><?php _e('Import Post Types', 'acf'); ?></p>
27
+
28
+ <div class="acf-fields">
29
+ <?php
30
+
31
+ acf_render_field_wrap(array(
32
+ 'label' => __('Select File', 'acf'),
33
+ 'type' => 'file',
34
+ 'name' => 'acf_import_file',
35
+ 'value' => false,
36
+ 'uploader' => 'basic',
37
+ ));
38
+
39
+ ?>
40
+ </div>
41
+
42
+ <p class="acf-submit">
43
+ <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
+ </p>
45
+ <?php
46
+
47
+ }
48
+
49
+ function submit(){
50
+
51
+ // Check file size.
52
+ if(empty($_FILES['acf_import_file']['size']))
53
+ return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
+
55
+ // Get file data.
56
+ $file = $_FILES['acf_import_file'];
57
+
58
+ // Check errors.
59
+ if($file['error'])
60
+ return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
+
62
+ // Check file type.
63
+ if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
+ return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
+
66
+ // Read JSON.
67
+ $json = file_get_contents($file['tmp_name']);
68
+ $json = json_decode($json, true);
69
+
70
+ // Check if empty.
71
+ if(!$json || !is_array($json))
72
+ return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
+
74
+ $ids = array();
75
+
76
+ $dynamic_post_types = acfe_settings('modules.dynamic_post_type.data');
77
+
78
+ // Loop over json
79
+ foreach($json as $post_type_name => $args){
80
+
81
+ // Check if already exists
82
+ if(isset($dynamic_post_types[$post_type_name])){
83
+
84
+ acf_add_admin_notice(__("Post type {$dynamic_post_types[$post_type_name]['label']} already exists. Import aborted."), 'warning');
85
+ continue;
86
+
87
+ }
88
+
89
+ // Vars
90
+ $title = $args['label'];
91
+ $name = $post_type_name;
92
+
93
+ // Insert post
94
+ $post_id = wp_insert_post(array(
95
+ 'post_title' => $title,
96
+ 'post_name' => $name,
97
+ 'post_type' => 'acfe-dpt',
98
+ 'post_status' => 'publish'
99
+ ));
100
+
101
+ // Insert error
102
+ if(is_wp_error($post_id)){
103
+
104
+ acf_add_admin_notice(__("Something went wrong with the post type {$title}. Import aborted."), 'warning');
105
+ continue;
106
+
107
+ }
108
+
109
+ // Register Args
110
+ update_field('acfe_dpt_name', $post_type_name, $post_id);
111
+ update_field('label', $args['label'], $post_id);
112
+ update_field('description', $args['description'], $post_id);
113
+ update_field('hierarchical', $args['hierarchical'], $post_id);
114
+ update_field('supports', $args['supports'], $post_id);
115
+ update_field('taxonomies', $args['taxonomies'], $post_id);
116
+ update_field('public', $args['public'], $post_id);
117
+ update_field('exclude_from_search', $args['exclude_from_search'], $post_id);
118
+ update_field('publicly_queryable', $args['publicly_queryable'], $post_id);
119
+ update_field('can_export', $args['can_export'], $post_id);
120
+ update_field('delete_with_user', $args['delete_with_user'], $post_id);
121
+
122
+ // Labels
123
+ if(!empty($args['labels'])){
124
+
125
+ foreach($args['labels'] as $label_key => $label_value){
126
+
127
+ update_field('labels_' . $label_key, $label_value, $post_id);
128
+
129
+ }
130
+
131
+ }
132
+
133
+ // Menu
134
+ update_field('menu_position', $args['menu_position'], $post_id);
135
+ update_field('menu_icon', $args['menu_icon'], $post_id);
136
+ update_field('show_ui', $args['show_ui'], $post_id);
137
+ update_field('show_in_menu', $args['show_in_menu'], $post_id);
138
+ update_field('show_in_nav_menus', $args['show_in_nav_menus'], $post_id);
139
+ update_field('show_in_admin_bar', $args['show_in_admin_bar'], $post_id);
140
+
141
+ // Capability
142
+ update_field('capability_type', acf_encode_choices($args['capability_type'], false), $post_id);
143
+ update_field('map_meta_cap', $args['map_meta_cap'], $post_id);
144
+
145
+ if(isset($args['capabilities']))
146
+ update_field('capabilities', acf_encode_choices($args['capabilities'], false), $post_id);
147
+
148
+ // Archive
149
+ update_field('acfe_dpt_archive_template', $args['acfe_archive_template'], $post_id);
150
+ update_field('acfe_dpt_archive_posts_per_page', $args['acfe_archive_ppp'], $post_id);
151
+ update_field('acfe_dpt_archive_orderby', $args['acfe_archive_orderby'], $post_id);
152
+ update_field('acfe_dpt_archive_order', $args['acfe_archive_order'], $post_id);
153
+ update_field('has_archive', $args['has_archive'], $post_id);
154
+
155
+ // Single
156
+ update_field('acfe_dpt_single_template', $args['acfe_single_template'], $post_id);
157
+ update_field('rewrite', $args['rewrite'], $post_id);
158
+
159
+ // Admin
160
+ update_field('acfe_dpt_admin_posts_per_page', $args['acfe_admin_ppp'], $post_id);
161
+ update_field('acfe_dpt_admin_orderby', $args['acfe_admin_orderby'], $post_id);
162
+ update_field('acfe_dpt_admin_order', $args['acfe_admin_order'], $post_id);
163
+
164
+ // REST
165
+ update_field('show_in_rest', $args['show_in_rest'], $post_id);
166
+ update_field('rest_base', $args['rest_base'], $post_id);
167
+ update_field('rest_controller_class', $args['rest_controller_class'], $post_id);
168
+
169
+ // Has archive: override
170
+ if($args['has_archive'])
171
+ update_field('has_archive_slug', $args['has_archive'], $post_id);
172
+
173
+ // Rewrite: override
174
+ if($args['rewrite'] && is_array($args['rewrite'])){
175
+
176
+ update_field('rewrite', true, $post_id);
177
+
178
+ update_field('rewrite_args_select', true, $post_id);
179
+
180
+ update_field('rewrite_args_acfe_dpt_rewrite_slug', $args['rewrite']['slug'], $post_id);
181
+ update_field('rewrite_args_acfe_dpt_rewrite_with_front', $args['rewrite']['with_front'], $post_id);
182
+ update_field('rewrite_args_feeds', $args['rewrite']['feeds'], $post_id);
183
+ update_field('rewrite_args_pages', $args['rewrite']['pages'], $post_id);
184
+
185
+ }
186
+
187
+ // Show in menu (text)
188
+ if($args['show_in_menu'] && is_string($args['show_in_menu']))
189
+ update_field('show_in_menu_text', $args['show_in_menu'], $post_id);
190
+
191
+ // Map meta cap
192
+ if($args['map_meta_cap'] === false)
193
+ update_field('map_meta_cap', 'false', $post_id);
194
+
195
+ elseif($args['map_meta_cap'] === true)
196
+ update_field('map_meta_cap', 'true', $post_id);
197
+
198
+ // Create ACFE option
199
+ $dynamic_post_types[$post_type_name] = $args;
200
+
201
+ // Sort keys ASC
202
+ ksort($dynamic_post_types);
203
+
204
+ // Update ACFE option
205
+ acfe_settings('modules.dynamic_post_type.data', $dynamic_post_types, true);
206
+
207
+ // append message
208
+ $ids[] = $post_id;
209
+
210
+ }
211
+
212
+ if(empty($ids))
213
+ return;
214
+
215
+ // Count total
216
+ $total = count($ids);
217
+
218
+ // Generate text
219
+ $text = sprintf(_n('1 post type imported', '%s post types imported', $total, 'acf'), $total);
220
+
221
+ // Add links to text
222
+ $links = array();
223
+ foreach($ids as $id){
224
+
225
+ $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
226
+
227
+ }
228
+
229
+ $text .= ': ' . implode(', ', $links);
230
+
231
+ // Add notice
232
+ acf_add_admin_notice($text, 'success');
233
+
234
+ // Flush permalinks
235
+ flush_rewrite_rules();
236
+
237
+ }
238
+
239
+ }
240
+
241
+ acf_register_admin_tool('ACFE_Admin_Tool_Import_DPT');
242
+
243
  endif;
includes/admin/tools/dt-import.php CHANGED
@@ -1,241 +1,241 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- // Check setting
7
- if(!acf_get_setting('acfe/modules/dynamic_taxonomies'))
8
- return;
9
-
10
- if(!class_exists('ACFE_Admin_Tool_Import_DT')):
11
-
12
- class ACFE_Admin_Tool_Import_DT extends ACF_Admin_Tool{
13
-
14
- function initialize(){
15
-
16
- // vars
17
- $this->name = 'acfe_tool_dt_import';
18
- $this->title = __('Import Taxonomies');
19
- $this->icon = 'dashicons-upload';
20
-
21
- }
22
-
23
- function html(){
24
-
25
- ?>
26
- <p><?php _e('Import Taxonomies', 'acf'); ?></p>
27
-
28
- <div class="acf-fields">
29
- <?php
30
-
31
- acf_render_field_wrap(array(
32
- 'label' => __('Select File', 'acf'),
33
- 'type' => 'file',
34
- 'name' => 'acf_import_file',
35
- 'value' => false,
36
- 'uploader' => 'basic',
37
- ));
38
-
39
- ?>
40
- </div>
41
-
42
- <p class="acf-submit">
43
- <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
- </p>
45
- <?php
46
-
47
- }
48
-
49
- function submit(){
50
-
51
- // Check file size.
52
- if(empty($_FILES['acf_import_file']['size']))
53
- return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
-
55
- // Get file data.
56
- $file = $_FILES['acf_import_file'];
57
-
58
- // Check errors.
59
- if($file['error'])
60
- return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
-
62
- // Check file type.
63
- if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
- return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
-
66
- // Read JSON.
67
- $json = file_get_contents($file['tmp_name']);
68
- $json = json_decode($json, true);
69
-
70
- // Check if empty.
71
- if(!$json || !is_array($json))
72
- return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
-
74
- $ids = array();
75
-
76
- $dynamic_taxonomies = acfe_settings('modules.dynamic_taxonomy.data');
77
-
78
- // Loop over json
79
- foreach($json as $taxonomy_name => $args){
80
-
81
- // Check if already exists
82
- if(isset($dynamic_taxonomies[$taxonomy_name])){
83
-
84
- acf_add_admin_notice(__("Taxonomy {$dynamic_taxonomies[$taxonomy_name]['label']} already exists. Import aborted."), 'warning');
85
- continue;
86
-
87
- }
88
-
89
- // Vars
90
- $title = $args['label'];
91
- $name = $taxonomy_name;
92
-
93
- // Insert post
94
- $post_id = wp_insert_post(array(
95
- 'post_title' => $title,
96
- 'post_name' => $name,
97
- 'post_type' => 'acfe-dt',
98
- 'post_status' => 'publish'
99
- ));
100
-
101
- // Insert error
102
- if(is_wp_error($post_id)){
103
-
104
- acf_add_admin_notice(__("Something went wrong with the taxonomy {$title}. Import aborted."), 'warning');
105
- continue;
106
-
107
- }
108
-
109
-
110
- // Register Args
111
- update_field('acfe_dt_name', $taxonomy_name, $post_id);
112
- update_field('label', $args['label'], $post_id);
113
- update_field('description', $args['description'], $post_id);
114
- update_field('hierarchical', $args['hierarchical'], $post_id);
115
- update_field('post_types', $args['post_types'], $post_id);
116
- update_field('public', $args['public'], $post_id);
117
- update_field('publicly_queryable', $args['publicly_queryable'], $post_id);
118
- update_field('update_count_callback', $args['update_count_callback'], $post_id);
119
- update_field('sort', $args['sort'], $post_id);
120
-
121
- // Meta box callback
122
- if(!isset($args['meta_box_cb']) || $args['meta_box_cb'] === null){
123
-
124
- update_field('meta_box_cb', 'null', $post_id);
125
- update_field('meta_box_cb_custom', '', $post_id);
126
-
127
- }
128
-
129
- elseif($args['meta_box_cb'] === false){
130
-
131
- update_field('meta_box_cb', 'false', $post_id);
132
- update_field('meta_box_cb_custom', '', $post_id);
133
-
134
- }
135
-
136
- elseif(empty($args['meta_box_cb']) || is_string($args['meta_box_cb'])){
137
-
138
- update_field('meta_box_cb', 'custom', $post_id);
139
- update_field('meta_box_cb_custom', $args['meta_box_cb'], $post_id);
140
-
141
- }
142
-
143
- // Labels
144
- if(!empty($args['labels'])){
145
-
146
- foreach($args['labels'] as $label_key => $label_value){
147
-
148
- update_field('labels_' . $label_key, $label_value, $post_id);
149
-
150
- }
151
-
152
- }
153
-
154
- // Menu
155
- update_field('show_ui', $args['show_ui'], $post_id);
156
- update_field('show_in_menu', $args['show_in_menu'], $post_id);
157
- update_field('show_in_nav_menus', $args['show_in_nav_menus'], $post_id);
158
- update_field('show_tagcloud', $args['show_tagcloud'], $post_id);
159
- update_field('show_in_quick_edit', $args['show_in_quick_edit'], $post_id);
160
- update_field('show_admin_column', $args['show_admin_column'], $post_id);
161
-
162
- // Capability
163
- if(isset($args['capabilities']))
164
- update_field('capabilities', acf_encode_choices($args['capabilities'], false), $post_id);
165
-
166
- // Single
167
- update_field('acfe_dt_single_template', $args['acfe_single_template'], $post_id);
168
- update_field('acfe_dt_single_posts_per_page', $args['acfe_single_ppp'], $post_id);
169
- update_field('acfe_dt_single_orderby', $args['acfe_single_orderby'], $post_id);
170
- update_field('acfe_dt_single_order', $args['acfe_single_order'], $post_id);
171
- update_field('rewrite', $args['rewrite'], $post_id);
172
-
173
- // Admin
174
- update_field('acfe_dt_admin_terms_per_page', $args['acfe_admin_ppp'], $post_id);
175
- update_field('acfe_dt_admin_orderby', $args['acfe_admin_orderby'], $post_id);
176
- update_field('acfe_dt_admin_order', $args['acfe_admin_order'], $post_id);
177
-
178
- // REST
179
- update_field('show_in_rest', $args['show_in_rest'], $post_id);
180
- update_field('rest_base', $args['rest_base'], $post_id);
181
- update_field('rest_controller_class', $args['rest_controller_class'], $post_id);
182
-
183
- // Rewrite: override
184
- if($args['rewrite'] && is_array($args['rewrite'])){
185
-
186
- update_field('rewrite', true, $post_id);
187
-
188
- update_field('rewrite_args_select', true, $post_id);
189
-
190
- update_field('rewrite_args_acfe_dt_rewrite_slug', $args['rewrite']['slug'], $post_id);
191
- update_field('rewrite_args_acfe_dt_rewrite_with_front', $args['rewrite']['with_front'], $post_id);
192
- update_field('rewrite_args_hierarchical', $args['rewrite']['hierarchical'], $post_id);
193
-
194
- }
195
-
196
- // Create ACFE option
197
- $dynamic_taxonomies[$taxonomy_name] = $args;
198
-
199
- // Sort keys ASC
200
- ksort($dynamic_taxonomies);
201
-
202
- // Update ACFE option
203
- acfe_settings('modules.dynamic_taxonomy.data', $dynamic_taxonomies, true);
204
-
205
- // append message
206
- $ids[] = $post_id;
207
-
208
- }
209
-
210
- if(empty($ids))
211
- return;
212
-
213
- // Count total
214
- $total = count($ids);
215
-
216
- // Generate text
217
- $text = sprintf(_n('1 taxonomy imported', '%s taxonomies imported', $total, 'acf'), $total);
218
-
219
- // Add links to text
220
- $links = array();
221
- foreach($ids as $id){
222
-
223
- $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
224
-
225
- }
226
-
227
- $text .= ': ' . implode(', ', $links);
228
-
229
- // Add notice
230
- acf_add_admin_notice($text, 'success');
231
-
232
- // Flush permalinks
233
- flush_rewrite_rules();
234
-
235
- }
236
-
237
- }
238
-
239
- acf_register_admin_tool('ACFE_Admin_Tool_Import_DT');
240
-
241
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ // Check setting
7
+ if(!acf_get_setting('acfe/modules/dynamic_taxonomies'))
8
+ return;
9
+
10
+ if(!class_exists('ACFE_Admin_Tool_Import_DT')):
11
+
12
+ class ACFE_Admin_Tool_Import_DT extends ACF_Admin_Tool{
13
+
14
+ function initialize(){
15
+
16
+ // vars
17
+ $this->name = 'acfe_tool_dt_import';
18
+ $this->title = __('Import Taxonomies');
19
+ $this->icon = 'dashicons-upload';
20
+
21
+ }
22
+
23
+ function html(){
24
+
25
+ ?>
26
+ <p><?php _e('Import Taxonomies', 'acf'); ?></p>
27
+
28
+ <div class="acf-fields">
29
+ <?php
30
+
31
+ acf_render_field_wrap(array(
32
+ 'label' => __('Select File', 'acf'),
33
+ 'type' => 'file',
34
+ 'name' => 'acf_import_file',
35
+ 'value' => false,
36
+ 'uploader' => 'basic',
37
+ ));
38
+
39
+ ?>
40
+ </div>
41
+
42
+ <p class="acf-submit">
43
+ <button type="submit" name="action" class="button button-primary"><?php _e('Import File'); ?></button>
44
+ </p>
45
+ <?php
46
+
47
+ }
48
+
49
+ function submit(){
50
+
51
+ // Check file size.
52
+ if(empty($_FILES['acf_import_file']['size']))
53
+ return acf_add_admin_notice(__("No file selected", 'acf'), 'warning');
54
+
55
+ // Get file data.
56
+ $file = $_FILES['acf_import_file'];
57
+
58
+ // Check errors.
59
+ if($file['error'])
60
+ return acf_add_admin_notice(__("Error uploading file. Please try again", 'acf'), 'warning');
61
+
62
+ // Check file type.
63
+ if(pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json')
64
+ return acf_add_admin_notice(__("Incorrect file type", 'acf'), 'warning');
65
+
66
+ // Read JSON.
67
+ $json = file_get_contents($file['tmp_name']);
68
+ $json = json_decode($json, true);
69
+
70
+ // Check if empty.
71
+ if(!$json || !is_array($json))
72
+ return acf_add_admin_notice(__("Import file empty", 'acf'), 'warning');
73
+
74
+ $ids = array();
75
+
76
+ $dynamic_taxonomies = acfe_settings('modules.dynamic_taxonomy.data');
77
+
78
+ // Loop over json
79
+ foreach($json as $taxonomy_name => $args){
80
+
81
+ // Check if already exists
82
+ if(isset($dynamic_taxonomies[$taxonomy_name])){
83
+
84
+ acf_add_admin_notice(__("Taxonomy {$dynamic_taxonomies[$taxonomy_name]['label']} already exists. Import aborted."), 'warning');
85
+ continue;
86
+
87
+ }
88
+
89
+ // Vars
90
+ $title = $args['label'];
91
+ $name = $taxonomy_name;
92
+
93
+ // Insert post
94
+ $post_id = wp_insert_post(array(
95
+ 'post_title' => $title,
96
+ 'post_name' => $name,
97
+ 'post_type' => 'acfe-dt',
98
+ 'post_status' => 'publish'
99
+ ));
100
+
101
+ // Insert error
102
+ if(is_wp_error($post_id)){
103
+
104
+ acf_add_admin_notice(__("Something went wrong with the taxonomy {$title}. Import aborted."), 'warning');
105
+ continue;
106
+
107
+ }
108
+
109
+
110
+ // Register Args
111
+ update_field('acfe_dt_name', $taxonomy_name, $post_id);
112
+ update_field('label', $args['label'], $post_id);
113
+ update_field('description', $args['description'], $post_id);
114
+ update_field('hierarchical', $args['hierarchical'], $post_id);
115
+ update_field('post_types', $args['post_types'], $post_id);
116
+ update_field('public', $args['public'], $post_id);
117
+ update_field('publicly_queryable', $args['publicly_queryable'], $post_id);
118
+ update_field('update_count_callback', $args['update_count_callback'], $post_id);
119
+ update_field('sort', $args['sort'], $post_id);
120
+
121
+ // Meta box callback
122
+ if(!isset($args['meta_box_cb']) || $args['meta_box_cb'] === null){
123
+
124
+ update_field('meta_box_cb', 'null', $post_id);
125
+ update_field('meta_box_cb_custom', '', $post_id);
126
+
127
+ }
128
+
129
+ elseif($args['meta_box_cb'] === false){
130
+
131
+ update_field('meta_box_cb', 'false', $post_id);
132
+ update_field('meta_box_cb_custom', '', $post_id);
133
+
134
+ }
135
+
136
+ elseif(empty($args['meta_box_cb']) || is_string($args['meta_box_cb'])){
137
+
138
+ update_field('meta_box_cb', 'custom', $post_id);
139
+ update_field('meta_box_cb_custom', $args['meta_box_cb'], $post_id);
140
+
141
+ }
142
+
143
+ // Labels
144
+ if(!empty($args['labels'])){
145
+
146
+ foreach($args['labels'] as $label_key => $label_value){
147
+
148
+ update_field('labels_' . $label_key, $label_value, $post_id);
149
+
150
+ }
151
+
152
+ }
153
+
154
+ // Menu
155
+ update_field('show_ui', $args['show_ui'], $post_id);
156
+ update_field('show_in_menu', $args['show_in_menu'], $post_id);
157
+ update_field('show_in_nav_menus', $args['show_in_nav_menus'], $post_id);
158
+ update_field('show_tagcloud', $args['show_tagcloud'], $post_id);
159
+ update_field('show_in_quick_edit', $args['show_in_quick_edit'], $post_id);
160
+ update_field('show_admin_column', $args['show_admin_column'], $post_id);
161
+
162
+ // Capability
163
+ if(isset($args['capabilities']))
164
+ update_field('capabilities', acf_encode_choices($args['capabilities'], false), $post_id);
165
+
166
+ // Single
167
+ update_field('acfe_dt_single_template', $args['acfe_single_template'], $post_id);
168
+ update_field('acfe_dt_single_posts_per_page', $args['acfe_single_ppp'], $post_id);
169
+ update_field('acfe_dt_single_orderby', $args['acfe_single_orderby'], $post_id);
170
+ update_field('acfe_dt_single_order', $args['acfe_single_order'], $post_id);
171
+ update_field('rewrite', $args['rewrite'], $post_id);
172
+
173
+ // Admin
174
+ update_field('acfe_dt_admin_terms_per_page', $args['acfe_admin_ppp'], $post_id);
175
+ update_field('acfe_dt_admin_orderby', $args['acfe_admin_orderby'], $post_id);
176
+ update_field('acfe_dt_admin_order', $args['acfe_admin_order'], $post_id);
177
+
178
+ // REST
179
+ update_field('show_in_rest', $args['show_in_rest'], $post_id);
180
+ update_field('rest_base', $args['rest_base'], $post_id);
181
+ update_field('rest_controller_class', $args['rest_controller_class'], $post_id);
182
+
183
+ // Rewrite: override
184
+ if($args['rewrite'] && is_array($args['rewrite'])){
185
+
186
+ update_field('rewrite', true, $post_id);
187
+
188
+ update_field('rewrite_args_select', true, $post_id);
189
+
190
+ update_field('rewrite_args_acfe_dt_rewrite_slug', $args['rewrite']['slug'], $post_id);
191
+ update_field('rewrite_args_acfe_dt_rewrite_with_front', $args['rewrite']['with_front'], $post_id);
192
+ update_field('rewrite_args_hierarchical', $args['rewrite']['hierarchical'], $post_id);
193
+
194
+ }
195
+
196
+ // Create ACFE option
197
+ $dynamic_taxonomies[$taxonomy_name] = $args;
198
+
199
+ // Sort keys ASC
200
+ ksort($dynamic_taxonomies);
201
+
202
+ // Update ACFE option
203
+ acfe_settings('modules.dynamic_taxonomy.data', $dynamic_taxonomies, true);
204
+
205
+ // append message
206
+ $ids[] = $post_id;
207
+
208
+ }
209
+
210
+ if(empty($ids))
211
+ return;
212
+
213
+ // Count total
214
+ $total = count($ids);
215
+
216
+ // Generate text
217
+ $text = sprintf(_n('1 taxonomy imported', '%s taxonomies imported', $total, 'acf'), $total);
218
+
219
+ // Add links to text
220
+ $links = array();
221
+ foreach($ids as $id){
222
+
223
+ $links[] = '<a href="' . get_edit_post_link($id) . '">' . get_the_title($id) . '</a>';
224
+
225
+ }
226
+
227
+ $text .= ': ' . implode(', ', $links);
228
+
229
+ // Add notice
230
+ acf_add_admin_notice($text, 'success');
231
+
232
+ // Flush permalinks
233
+ flush_rewrite_rules();
234
+
235
+ }
236
+
237
+ }
238
+
239
+ acf_register_admin_tool('ACFE_Admin_Tool_Import_DT');
240
+
241
  endif;
includes/field-groups/field-groups-local.php CHANGED
@@ -1,198 +1,198 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- add_filter('views_edit-acf-field-group', 'acfe_field_groups_third_party_views', 99);
7
- function acfe_field_groups_third_party_views($views){
8
-
9
- // Total
10
- $total = count(acfe_get_third_party_field_groups());
11
-
12
- // Bail early if empty
13
- if($total === 0)
14
- return $views;
15
-
16
- // Class
17
- $class = '';
18
-
19
- // active
20
- if(acf_maybe_get_GET('post_status') === 'acfe-local'){
21
-
22
- // actions
23
- add_action('admin_footer', 'acfe_field_groups_third_party_footer', 5);
24
-
25
- // set active class
26
- $class = ' class="current"';
27
-
28
- // global
29
- global $wp_list_table;
30
-
31
- // update pagination
32
- $wp_list_table->set_pagination_args(array(
33
- 'total_items' => $total,
34
- 'total_pages' => 1,
35
- 'per_page' => $total
36
- ));
37
-
38
- }
39
-
40
- // add view
41
- $views['acfe-local'] = '<a' . $class . ' href="' . admin_url('edit.php?post_type=acf-field-group&post_status=acfe-local') . '">' . __('Local', 'acfe') . ' <span class="count">(' . $total . ')</span></a>';
42
-
43
- // return
44
- return $views;
45
-
46
- }
47
-
48
- function acfe_field_groups_third_party_footer(){
49
-
50
- // vars
51
- $i = -1;
52
- $columns = array(
53
- 'acfe-source',
54
- 'acfe-count',
55
- 'acfe-locations',
56
- 'acfe-local'
57
- );
58
-
59
- ?>
60
- <script type="text/html" id="tmpl-acfe-local-tbody">
61
- <?php
62
-
63
- foreach(acfe_get_third_party_field_groups() as $field_group ):
64
-
65
- // vars
66
- $i++;
67
- $key = $field_group['key'];
68
- $title = $field_group['title'];
69
- $local = $field_group['local'];
70
-
71
- ?>
72
- <tr <?php if($i%2 == 0): ?>class="alternate"<?php endif; ?>>
73
- <td class="post-title page-title column-title">
74
- <strong>
75
- <span class="row-title"><?php echo esc_html($title); ?></span>
76
- </strong>
77
- <div class="row-actions">
78
-
79
- <span>
80
- <a href="<?php echo add_query_arg(array('action' => 'php', 'keys' => $key), acf_get_admin_tool_url('acfe-fg-local')); ?>">PHP</a> |
81
- </span>
82
-
83
- <span>
84
- <a href="<?php echo add_query_arg(array('action' => 'json', 'keys' => $key), acf_get_admin_tool_url('acfe-fg-local')); ?>">Json</a> |
85
- </span>
86
-
87
- <span>
88
- <a href="<?php echo add_query_arg(array('action' => 'sync', 'keys' => $key), acf_get_admin_tool_url('acfe-fg-local')); ?>">Sync</a> |
89
- </span>
90
-
91
- <span class="acfe-key">
92
- <span style="color:#555;">
93
- <code style="font-size: 12px;"><?php echo esc_html($key); ?></code>
94
- </span>
95
- </span>
96
-
97
- </div>
98
- </td>
99
- <?php foreach($columns as $column): ?>
100
- <td class="column-<?php echo esc_attr($column); ?>">
101
- <?php echo acfe_field_groups_column_html($column, $key); ?>
102
- </td>
103
- <?php endforeach; ?>
104
- </tr>
105
- <?php endforeach; ?>
106
- </script>
107
-
108
- <script type="text/javascript">
109
- (function($){
110
-
111
- // update table HTML
112
- $('#the-list').html($('#tmpl-acfe-local-tbody').html());
113
-
114
- })(jQuery);
115
- </script>
116
- <?php
117
-
118
- }
119
-
120
- function acfe_get_third_party_field_groups(){
121
-
122
- $get_local_field_groups = acf_get_local_field_groups();
123
- if(empty($get_local_field_groups))
124
- return array();
125
-
126
- $locals = array();
127
-
128
- foreach($get_local_field_groups as $field_group){
129
-
130
- // Exclude ACFE Field Groups
131
-
132
- if(!acfe_is_super_dev()){
133
-
134
- if(stripos($field_group['key'], 'group_acfe_') === 0)
135
- continue;
136
-
137
- }
138
-
139
-
140
- $locals[] = $field_group;
141
-
142
- }
143
-
144
- // Bail early if no local fields
145
- if(empty($locals))
146
- return $locals;
147
-
148
- // Get DB field groups
149
- $get_db_field_groups = acfe_get_db_field_groups();
150
-
151
- // Bail early if no DB field groups
152
- if(empty($get_db_field_groups))
153
- return $locals;
154
-
155
- foreach($get_db_field_groups as $field_group){
156
-
157
- foreach($locals as $k => $local){
158
-
159
- if($local['key'] === $field_group['key'])
160
- unset($locals[$k]);
161
-
162
- }
163
-
164
- }
165
-
166
- return $locals;
167
-
168
- }
169
-
170
- function acfe_get_db_field_groups(){
171
-
172
- acf_disable_filters();
173
-
174
- $get_db_field_groups = acf_get_field_groups();
175
-
176
- acf_enable_filters();
177
-
178
- return $get_db_field_groups;
179
-
180
- }
181
-
182
- add_filter('bulk_actions-edit-acf-field-group', function($actions){
183
-
184
- if(acf_maybe_get_GET('post_status') === 'acfe-local')
185
- return array();
186
-
187
- return $actions;
188
-
189
- }, 99);
190
-
191
- add_filter('manage_edit-acf-field-group_sortable_columns', function($sortable_columns){
192
-
193
- if(acf_maybe_get_GET('post_status') === 'acfe-local')
194
- return array();
195
-
196
- return $sortable_columns;
197
-
198
  }, 99);
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ add_filter('views_edit-acf-field-group', 'acfe_field_groups_third_party_views', 99);
7
+ function acfe_field_groups_third_party_views($views){
8
+
9
+ // Total
10
+ $total = count(acfe_get_third_party_field_groups());
11
+
12
+ // Bail early if empty
13
+ if($total === 0)
14
+ return $views;
15
+
16
+ // Class
17
+ $class = '';
18
+
19
+ // active
20
+ if(acf_maybe_get_GET('post_status') === 'acfe-local'){
21
+
22
+ // actions
23
+ add_action('admin_footer', 'acfe_field_groups_third_party_footer', 5);
24
+
25
+ // set active class
26
+ $class = ' class="current"';
27
+
28
+ // global
29
+ global $wp_list_table;
30
+
31
+ // update pagination
32
+ $wp_list_table->set_pagination_args(array(
33
+ 'total_items' => $total,
34
+ 'total_pages' => 1,
35
+ 'per_page' => $total
36
+ ));
37
+
38
+ }
39
+
40
+ // add view
41
+ $views['acfe-local'] = '<a' . $class . ' href="' . admin_url('edit.php?post_type=acf-field-group&post_status=acfe-local') . '">' . __('Local', 'acfe') . ' <span class="count">(' . $total . ')</span></a>';
42
+
43
+ // return
44
+ return $views;
45
+
46
+ }
47
+
48
+ function acfe_field_groups_third_party_footer(){
49
+
50
+ // vars
51
+ $i = -1;
52
+ $columns = array(
53
+ 'acfe-source',
54
+ 'acfe-count',
55
+ 'acfe-locations',
56
+ 'acfe-local'
57
+ );
58
+
59
+ ?>
60
+ <script type="text/html" id="tmpl-acfe-local-tbody">
61
+ <?php
62
+
63
+ foreach(acfe_get_third_party_field_groups() as $field_group ):
64
+
65
+ // vars
66
+ $i++;
67
+ $key = $field_group['key'];
68
+ $title = $field_group['title'];
69
+ $local = $field_group['local'];
70
+
71
+ ?>
72
+ <tr <?php if($i%2 == 0): ?>class="alternate"<?php endif; ?>>
73
+ <td class="post-title page-title column-title">
74
+ <strong>
75
+ <span class="row-title"><?php echo esc_html($title); ?></span>
76
+ </strong>
77
+ <div class="row-actions">
78
+
79
+ <span>
80
+ <a href="<?php echo add_query_arg(array('action' => 'php', 'keys' => $key), acf_get_admin_tool_url('acfe-fg-local')); ?>">PHP</a> |
81
+ </span>
82
+
83
+ <span>
84
+ <a href="<?php echo add_query_arg(array('action' => 'json', 'keys' => $key), acf_get_admin_tool_url('acfe-fg-local')); ?>">Json</a> |
85
+ </span>
86
+
87
+ <span>
88
+ <a href="<?php echo add_query_arg(array('action' => 'sync', 'keys' => $key), acf_get_admin_tool_url('acfe-fg-local')); ?>">Sync</a> |
89
+ </span>
90
+
91
+ <span class="acfe-key">
92
+ <span style="color:#555;">
93
+ <code style="font-size: 12px;"><?php echo esc_html($key); ?></code>
94
+ </span>
95
+ </span>
96
+
97
+ </div>
98
+ </td>
99
+ <?php foreach($columns as $column): ?>
100
+ <td class="column-<?php echo esc_attr($column); ?>">
101
+ <?php echo acfe_field_groups_column_html($column, $key); ?>
102
+ </td>
103
+ <?php endforeach; ?>
104
+ </tr>
105
+ <?php endforeach; ?>
106
+ </script>
107
+
108
+ <script type="text/javascript">
109
+ (function($){
110
+
111
+ // update table HTML
112
+ $('#the-list').html($('#tmpl-acfe-local-tbody').html());
113
+
114
+ })(jQuery);
115
+ </script>
116
+ <?php
117
+
118
+ }
119
+
120
+ function acfe_get_third_party_field_groups(){
121
+
122
+ $get_local_field_groups = acf_get_local_field_groups();
123
+ if(empty($get_local_field_groups))
124
+ return array();
125
+
126
+ $locals = array();
127
+
128
+ foreach($get_local_field_groups as $field_group){
129
+
130
+ // Exclude ACFE Field Groups
131
+
132
+ if(!acfe_is_super_dev()){
133
+
134
+ if(stripos($field_group['key'], 'group_acfe_') === 0)
135
+ continue;
136
+
137
+ }
138
+
139
+
140
+ $locals[] = $field_group;
141
+
142
+ }
143
+
144
+ // Bail early if no local fields
145
+ if(empty($locals))
146
+ return $locals;
147
+
148
+ // Get DB field groups
149
+ $get_db_field_groups = acfe_get_db_field_groups();
150
+
151
+ // Bail early if no DB field groups
152
+ if(empty($get_db_field_groups))
153
+ return $locals;
154
+
155
+ foreach($get_db_field_groups as $field_group){
156
+
157
+ foreach($locals as $k => $local){
158
+
159
+ if($local['key'] === $field_group['key'])
160
+ unset($locals[$k]);
161
+
162
+ }
163
+
164
+ }
165
+
166
+ return $locals;
167
+
168
+ }
169
+
170
+ function acfe_get_db_field_groups(){
171
+
172
+ acf_disable_filters();
173
+
174
+ $get_db_field_groups = acf_get_field_groups();
175
+
176
+ acf_enable_filters();
177
+
178
+ return $get_db_field_groups;
179
+
180
+ }
181
+
182
+ add_filter('bulk_actions-edit-acf-field-group', function($actions){
183
+
184
+ if(acf_maybe_get_GET('post_status') === 'acfe-local')
185
+ return array();
186
+
187
+ return $actions;
188
+
189
+ }, 99);
190
+
191
+ add_filter('manage_edit-acf-field-group_sortable_columns', function($sortable_columns){
192
+
193
+ if(acf_maybe_get_GET('post_status') === 'acfe-local')
194
+ return array();
195
+
196
+ return $sortable_columns;
197
+
198
  }, 99);
includes/fields-settings/fields.php CHANGED
@@ -1,127 +1,127 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- /**
7
- * Field Wrapper Attributes
8
- */
9
- add_filter('acf/field_wrapper_attributes', 'acfe_field_wrapper_attributes', 10, 2);
10
- function acfe_field_wrapper_attributes($wrapper, $field){
11
-
12
- $wrapper = apply_filters('acfe/field_wrapper_attributes', $wrapper, $field);
13
-
14
- return $wrapper;
15
-
16
- }
17
-
18
- if(function_exists('acf_add_filter_variations')){
19
-
20
- acf_add_filter_variations('acfe/field_wrapper_attributes', array('type', 'name', 'key'), 1);
21
-
22
- }
23
-
24
- /**
25
- * Load Fields
26
- */
27
- add_filter('acf/load_fields', 'acfe_field_load_fields', 10, 2);
28
- function acfe_field_load_fields($fields, $parent){
29
-
30
- // check if field (fitler is also called on field groups)
31
- if(!acf_maybe_get($parent, 'type'))
32
- return $fields;
33
-
34
- $fields = apply_filters('acfe/load_fields', $fields, $parent);
35
-
36
- return $fields;
37
-
38
- }
39
-
40
- if(function_exists('acf_add_filter_variations')){
41
-
42
- acf_add_filter_variations('acfe/load_fields', array('type', 'name', 'key'), 1);
43
-
44
- }
45
-
46
- /**
47
- * Load Field
48
- */
49
- add_filter('acf/load_field', 'acfe_load_field');
50
- function acfe_load_field($field){
51
-
52
- if(acfe_is_admin_screen())
53
- return $field;
54
-
55
- // Everywhere
56
- $field = apply_filters('acfe/load_field', $field);
57
-
58
- // Admin
59
- if(acfe_form_is_admin()){
60
-
61
- $field = apply_filters('acfe/load_field_admin', $field);
62
-
63
- }
64
-
65
- // Front
66
- elseif(acfe_form_is_front()){
67
-
68
- $field = apply_filters('acfe/load_field_front', $field);
69
-
70
- }
71
-
72
- return $field;
73
-
74
- }
75
-
76
- if(function_exists('acf_add_filter_variations')){
77
-
78
- acf_add_filter_variations('acfe/load_field', array('type', 'name', 'key'), 0);
79
- acf_add_filter_variations('acfe/load_field_front', array('type', 'name', 'key'), 0);
80
- acf_add_filter_variations('acfe/load_field_admin', array('type', 'name', 'key'), 0);
81
-
82
- }
83
-
84
- add_filter('acf/pre_render_fields', 'acfe_fields_wrapper_instructions', 10, 2);
85
- function acfe_fields_wrapper_instructions($fields, $post_id){
86
-
87
- $tooltip = false;
88
-
89
- if(!isset($fields[0]))
90
- return $fields;
91
-
92
- $field_group = acfe_get_field_group_from_field($fields[0]);
93
-
94
- if(!$field_group)
95
- return $fields;
96
-
97
- if($field_group['instruction_placement'] !== 'acfe_instructions_tooltip')
98
- return $fields;
99
-
100
- foreach($fields as &$field){
101
-
102
- acfe_add_fields_instructions_tooltip($field);
103
-
104
- }
105
-
106
- return $fields;
107
-
108
- }
109
-
110
- add_filter('acf/field_wrapper_attributes', 'acfe_fields_wrapper', 10, 2);
111
- function acfe_fields_wrapper($wrapper, $field){
112
-
113
- if(!acf_maybe_get($field, 'label')){
114
-
115
- $wrapper['class'] .= ' acfe-no-label';
116
-
117
- }
118
-
119
- if(acf_maybe_get($field, 'acfe_instructions_tooltip')){
120
-
121
- $wrapper['data-acfe-instructions-tooltip'] = acf_esc_html($field['acfe_instructions_tooltip']);
122
-
123
- }
124
-
125
- return $wrapper;
126
-
127
  }
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ /**
7
+ * Field Wrapper Attributes
8
+ */
9
+ add_filter('acf/field_wrapper_attributes', 'acfe_field_wrapper_attributes', 10, 2);
10
+ function acfe_field_wrapper_attributes($wrapper, $field){
11
+
12
+ $wrapper = apply_filters('acfe/field_wrapper_attributes', $wrapper, $field);
13
+
14
+ return $wrapper;
15
+
16
+ }
17
+
18
+ if(function_exists('acf_add_filter_variations')){
19
+
20
+ acf_add_filter_variations('acfe/field_wrapper_attributes', array('type', 'name', 'key'), 1);
21
+
22
+ }
23
+
24
+ /**
25
+ * Load Fields
26
+ */
27
+ add_filter('acf/load_fields', 'acfe_field_load_fields', 10, 2);
28
+ function acfe_field_load_fields($fields, $parent){
29
+
30
+ // check if field (fitler is also called on field groups)
31
+ if(!acf_maybe_get($parent, 'type'))
32
+ return $fields;
33
+
34
+ $fields = apply_filters('acfe/load_fields', $fields, $parent);
35
+
36
+ return $fields;
37
+
38
+ }
39
+
40
+ if(function_exists('acf_add_filter_variations')){
41
+
42
+ acf_add_filter_variations('acfe/load_fields', array('type', 'name', 'key'), 1);
43
+
44
+ }
45
+
46
+ /**
47
+ * Load Field
48
+ */
49
+ add_filter('acf/load_field', 'acfe_load_field');
50
+ function acfe_load_field($field){
51
+
52
+ if(acfe_is_admin_screen())
53
+ return $field;
54
+
55
+ // Everywhere
56
+ $field = apply_filters('acfe/load_field', $field);
57
+
58
+ // Admin
59
+ if(acfe_form_is_admin()){
60
+
61
+ $field = apply_filters('acfe/load_field_admin', $field);
62
+
63
+ }
64
+
65
+ // Front
66
+ elseif(acfe_form_is_front()){
67
+
68
+ $field = apply_filters('acfe/load_field_front', $field);
69
+
70
+ }
71
+
72
+ return $field;
73
+
74
+ }
75
+
76
+ if(function_exists('acf_add_filter_variations')){
77
+
78
+ acf_add_filter_variations('acfe/load_field', array('type', 'name', 'key'), 0);
79
+ acf_add_filter_variations('acfe/load_field_front', array('type', 'name', 'key'), 0);
80
+ acf_add_filter_variations('acfe/load_field_admin', array('type', 'name', 'key'), 0);
81
+
82
+ }
83
+
84
+ add_filter('acf/pre_render_fields', 'acfe_fields_wrapper_instructions', 10, 2);
85
+ function acfe_fields_wrapper_instructions($fields, $post_id){
86
+
87
+ $tooltip = false;
88
+
89
+ if(!isset($fields[0]))
90
+ return $fields;
91
+
92
+ $field_group = acfe_get_field_group_from_field($fields[0]);
93
+
94
+ if(!$field_group)
95
+ return $fields;
96
+
97
+ if($field_group['instruction_placement'] !== 'acfe_instructions_tooltip')
98
+ return $fields;
99
+
100
+ foreach($fields as &$field){
101
+
102
+ acfe_add_fields_instructions_tooltip($field);
103
+
104
+ }
105
+
106
+ return $fields;
107
+
108
+ }
109
+
110
+ add_filter('acf/field_wrapper_attributes', 'acfe_fields_wrapper', 10, 2);
111
+ function acfe_fields_wrapper($wrapper, $field){
112
+
113
+ if(!acf_maybe_get($field, 'label')){
114
+
115
+ $wrapper['class'] .= ' acfe-no-label';
116
+
117
+ }
118
+
119
+ if(acf_maybe_get($field, 'acfe_instructions_tooltip')){
120
+
121
+ $wrapper['data-acfe-instructions-tooltip'] = acf_esc_html($field['acfe_instructions_tooltip']);
122
+
123
+ }
124
+
125
+ return $wrapper;
126
+
127
  }
includes/fields-settings/validation.php CHANGED
@@ -1,635 +1,635 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- if(!class_exists('acfe_field_validation')):
7
-
8
- class acfe_field_validation{
9
-
10
- public $functions = array();
11
-
12
- function __construct(){
13
-
14
- // Actions
15
- add_action('load-post.php', array($this, 'load'));
16
- add_action('wp_ajax_acf/field_group/render_field_settings', array($this, 'field_types_action'), 5);
17
-
18
- // Filters
19
- add_filter('acf/validate_value', array($this, 'validate_value'), 99, 4);
20
-
21
- // Validation functions
22
- $this->functions = array(
23
-
24
- 'General' => array(
25
- 'value' => 'If value',
26
- 'strlen' => 'If value length - strlen(value)',
27
- ),
28
-
29
- 'Exists' => array(
30
- 'email_exists' => 'If email exists - email_exists(value)',
31
- 'post_type_exists' => 'If post type exists - post_type_exists(value)',
32
- 'taxonomy_exists' => 'If taxonomy exists - taxonomy_exists(value)',
33
- 'term_exists' => 'If term exists - term_exists(value)',
34
- 'username_exists' => 'If username exists - username_exists(value)',
35
- ),
36
-
37
- 'Is' => array(
38
- 'is_email' => 'If is email - is_email(value)',
39
- 'is_user_logged_in' => 'If is user logged in - is_user_logged_in()',
40
- ),
41
-
42
- 'Sanitize' => array(
43
- 'sanitize_email' => 'If sanitize email - sanitize_email(value)',
44
- 'sanitize_file_name' => 'If sanitize file name - sanitize_file_name(value)',
45
- 'sanitize_html_class' => 'If sanitize html class - sanitize_html_class(value)',
46
- 'sanitize_key' => 'If sanitize key - sanitize_key(value)',
47
- 'sanitize_meta' => 'If sanitize meta - sanitize_meta(value)',
48
- 'sanitize_mime_type' => 'If sanitize mime type - sanitize_mime_type(value)',
49
- 'sanitize_option' => 'If sanitize option - sanitize_option(value)',
50
- 'sanitize_text_field' => 'If sanitize text field - sanitize_text_field(value)',
51
- 'sanitize_title' => 'If sanitize title - sanitize_title(value)',
52
- 'sanitize_user' => 'If sanitize user - sanitize_user(value)',
53
- ),
54
-
55
- 'User' => array(
56
- 'get_user_by_id' => 'If get user by id - get_user_by(\'id\', value)',
57
- 'get_user_by_slug' => 'If get user by slug - get_user_by(\'slug\', value)',
58
- 'get_user_by_email' => 'If get user by email - get_user_by(\'email\', value)',
59
- 'get_user_by_login' => 'If get user by login - get_user_by(\'login\', value)',
60
- )
61
-
62
- );
63
-
64
- }
65
-
66
- /**
67
- * Load
68
- */
69
- function load(){
70
-
71
- if(!acf_is_screen('acf-field-group'))
72
- return;
73
-
74
- $this->field_types_action();
75
-
76
- // Fix: Repeater
77
- add_filter('acf/prepare_field/name=acfe_validate', array($this, 'fix_repeater'));
78
- add_filter('acf/prepare_field/name=acfe_validate_location', array($this, 'fix_repeater'));
79
- add_filter('acf/prepare_field/name=acfe_validate_rules_and', array($this, 'fix_repeater'));
80
- add_filter('acf/prepare_field/name=acfe_validate_function', array($this, 'fix_repeater'));
81
- add_filter('acf/prepare_field/name=acfe_validate_operator', array($this, 'fix_repeater'));
82
- add_filter('acf/prepare_field/name=acfe_validate_match', array($this, 'fix_repeater'));
83
- add_filter('acf/prepare_field/name=acfe_validate_error', array($this, 'fix_repeater'));
84
-
85
- // Fix: Clone
86
- add_filter('acf/update_field', array($this, 'fix_clone'));
87
-
88
- }
89
-
90
- /**
91
- * Get field types
92
- */
93
- function field_types_action(){
94
-
95
- // Get Fields Types
96
- foreach(acf_get_field_types_info() as $field){
97
-
98
- // Field type
99
- $field_type = $field['name'];
100
-
101
- // Exclude
102
- if(in_array($field_type, array('message', 'accordion', 'tab', 'acfe_button', 'acfe_column', 'acfe_dynamic_message', 'group', 'repeater', 'flexible_content', 'clone')))
103
- continue;
104
-
105
- add_action('acf/render_field_settings/type=' . $field_type, array($this, 'render_field_settings'), 990);
106
-
107
- }
108
-
109
- }
110
-
111
- /**
112
- * Add Setting
113
- */
114
- function render_field_settings($field){
115
-
116
- $valid = false;
117
-
118
- // Ajax
119
- if(acf_verify_ajax()){
120
-
121
- $field_group = acfe_get_field_group_from_field($field);
122
-
123
- if(acf_maybe_get($field_group, 'acfe_form'))
124
- $valid = true;
125
-
126
- }
127
-
128
- // Display
129
- else{
130
-
131
- if(acf_maybe_get($field, 'acfe_form'))
132
- $valid = true;
133
-
134
- if(!$valid && acf_maybe_get($field, '_name') === 'new_field'){
135
-
136
- $field_group_id = get_the_ID();
137
-
138
- if($field_group_id){
139
-
140
- $field_group = acf_get_field_group($field_group_id);
141
-
142
- if(acf_maybe_get($field_group, 'acfe_form'))
143
- $valid = true;
144
-
145
- }
146
-
147
- }
148
-
149
- }
150
-
151
- if(!$valid)
152
- return;
153
-
154
- $choices = apply_filters('acfe/validate/functions', $this->functions, $field);
155
-
156
- if(empty($choices))
157
- return;
158
-
159
- // Settings
160
- acf_render_field_setting($field, array(
161
- 'label' => __('Advanced validation'),
162
- 'name' => 'acfe_validate',
163
- 'key' => 'acfe_validate',
164
- 'instructions' => __('Validate value against rules'),
165
- 'type' => 'repeater',
166
- 'button_label' => __('Add validation'),
167
- 'required' => false,
168
- 'layout' => 'row',
169
- 'sub_fields' => array(
170
- array(
171
- 'label' => 'Location',
172
- 'name' => 'acfe_validate_location',
173
- 'key' => 'acfe_validate_location',
174
- 'type' => 'select',
175
- 'instructions' => '',
176
- 'required' => 0,
177
- 'conditional_logic' => 0,
178
- 'wrapper' => array(
179
- 'width' => '',
180
- 'class' => '',
181
- 'id' => '',
182
- ),
183
- 'choices' => array(
184
- 'admin' => 'Administration',
185
- 'front' => 'Front-end',
186
- ),
187
- 'allow_null' => true,
188
- 'multiple' => 0,
189
- 'ui' => 0,
190
- 'return_format' => 'value',
191
- 'ajax' => 0,
192
- 'placeholder' => 'Everywhere',
193
- ),
194
- array(
195
- 'label' => __('Rules'),
196
- 'name' => 'acfe_validate_rules_and',
197
- 'key' => 'acfe_validate_rules_and',
198
- 'instructions' => '',
199
- 'type' => 'repeater',
200
- 'button_label' => __('+ AND'),
201
- 'required' => false,
202
- 'layout' => 'table',
203
- 'sub_fields' => array(
204
- array(
205
- 'label' => 'Function',
206
- 'name' => 'acfe_validate_function',
207
- 'key' => 'acfe_validate_function',
208
- 'prefix' => '',
209
- '_name' => '',
210
- '_prepare' => '',
211
- 'type' => 'select',
212
- 'choices' => $choices,
213
- 'instructions' => false,
214
- 'required' => false,
215
- 'wrapper' => array(
216
- 'width' => '',
217
- 'class' => '',
218
- 'id' => '',
219
- ),
220
- ),
221
- array(
222
- 'label' => 'Operator / Value',
223
- 'name' => 'acfe_validate_operator',
224
- 'key' => 'acfe_validate_operator',
225
- 'prefix' => '',
226
- '_name' => '',
227
- '_prepare' => '',
228
- 'type' => 'select',
229
- 'choices' => array(
230
- 'Operators' => array(
231
- '==' => '==',
232
- '!=' => '!=',
233
- '>' => '>',
234
- '>=' => '>=',
235
- '<' => '<',
236
- '<=' => '<=',
237
- 'contains' => 'Contains',
238
- '!contains' => 'Doesn\'t contain',
239
- 'starts' => 'Starts with',
240
- '!starts' => 'Doesn\'t start with',
241
- 'ends' => 'Ends with',
242
- '!ends' => 'Doesn\'t end with',
243
- ),
244
- 'Values' => array(
245
- 'true' => '== true',
246
- 'false' => '== false',
247
- 'null' => '== null',
248
- 'empty' => '== (empty)',
249
- '!empty' => '!= (empty)',
250
- )
251
- ),
252
- 'instructions' => false,
253
- 'required' => false,
254
- 'wrapper' => array(
255
- 'width' => '',
256
- 'class' => '',
257
- 'id' => '',
258
- ),
259
- ),
260
- array(
261
- 'label' => 'Value',
262
- 'name' => 'acfe_validate_match',
263
- 'key' => 'acfe_validate_match',
264
- 'prefix' => '',
265
- '_name' => '',
266
- '_prepare' => '',
267
- 'type' => 'text',
268
- 'instructions' => false,
269
- 'placeholder' => '',
270
- 'required' => false,
271
- 'wrapper' => array(
272
- 'width' => '',
273
- 'class' => '',
274
- 'id' => '',
275
- ),
276
- 'conditional_logic' => array(
277
- array(
278
- array(
279
- 'field' => 'acfe_validate_operator',
280
- 'operator' => '==',
281
- 'value' => '==',
282
- )
283
- ),
284
- array(
285
- array(
286
- 'field' => 'acfe_validate_operator',
287
- 'operator' => '==',
288
- 'value' => '!=',
289
- )
290
- ),
291
- array(
292
- array(
293
- 'field' => 'acfe_validate_operator',
294
- 'operator' => '==',
295
- 'value' => '>',
296
- )
297
- ),
298
- array(
299
- array(
300
- 'field' => 'acfe_validate_operator',
301
- 'operator' => '==',
302
- 'value' => '>=',
303
- )
304
- ),
305
- array(
306
- array(
307
- 'field' => 'acfe_validate_operator',
308
- 'operator' => '==',
309
- 'value' => '<',
310
- )
311
- ),
312
- array(
313
- array(
314
- 'field' => 'acfe_validate_operator',
315
- 'operator' => '==',
316
- 'value' => '<=',
317
- )
318
- ),
319
- array(
320
- array(
321
- 'field' => 'acfe_validate_operator',
322
- 'operator' => '==',
323
- 'value' => 'contains',
324
- )
325
- ),
326
- array(
327
- array(
328
- 'field' => 'acfe_validate_operator',
329
- 'operator' => '==',
330
- 'value' => '!contains',
331
- )
332
- ),
333
- array(
334
- array(
335
- 'field' => 'acfe_validate_operator',
336
- 'operator' => '==',
337
- 'value' => 'starts',
338
- )
339
- ),
340
- array(
341
- array(
342
- 'field' => 'acfe_validate_operator',
343
- 'operator' => '==',
344
- 'value' => '!starts',
345
- )
346
- ),
347
- array(
348
- array(
349
- 'field' => 'acfe_validate_operator',
350
- 'operator' => '==',
351
- 'value' => 'ends',
352
- )
353
- ),
354
- array(
355
- array(
356
- 'field' => 'acfe_validate_operator',
357
- 'operator' => '==',
358
- 'value' => '!ends',
359
- )
360
- ),
361
- )
362
- ),
363
- )
364
- ),
365
- array(
366
- 'label' => 'Error',
367
- 'name' => 'acfe_validate_error',
368
- 'key' => 'acfe_validate_error',
369
- 'prefix' => '',
370
- '_name' => '',
371
- '_prepare' => '',
372
- 'type' => 'text',
373
- 'instructions' => false,
374
- 'required' => false,
375
- 'wrapper' => array(
376
- 'width' => '',
377
- 'class' => '',
378
- 'id' => '',
379
- ),
380
- ),
381
- )
382
- ), false);
383
-
384
- }
385
-
386
- /**
387
- * Validate
388
- */
389
- function validate_value($valid, $value, $field, $input){
390
-
391
- if(!$valid)
392
- return $valid;
393
-
394
- if(!acf_maybe_get($field, 'acfe_validate'))
395
- return $valid;
396
-
397
- foreach($field['acfe_validate'] as $k => $rule){
398
-
399
- // Fix possible ACF Clone Index
400
- if($k === 'acfcloneindex')
401
- continue;
402
-
403
- // Screen
404
- $screen = isset($rule['acfe_validate_location']) ? $rule['acfe_validate_location'] : '';
405
- $screen_allow = false;
406
-
407
- // Screen: All
408
- if(empty($screen)){
409
-
410
- $screen_allow = true;
411
-
412
- }
413
-
414
- // Screen: Admin
415
- elseif($screen === 'admin' && acfe_form_is_admin()){
416
-
417
- $screen_allow = true;
418
-
419
- }
420
-
421
- // Screen: Front
422
- elseif($screen === 'front' && acfe_form_is_front()){
423
-
424
- $screen_allow = true;
425
-
426
- }
427
-
428
- if(!$screen_allow)
429
- continue;
430
-
431
- if(!acf_maybe_get($rule, 'acfe_validate_rules_and'))
432
- continue;
433
-
434
- $rule_match = true;
435
-
436
- foreach($rule['acfe_validate_rules_and'] as $k => $function){
437
-
438
- if(!$rule_match)
439
- break;
440
-
441
- $rule_match = false;
442
-
443
- // Check filters
444
- $filters = array(
445
- 'acfe/validate/function/' . $function['acfe_validate_function'] . '/key=' . $field['key'],
446
- 'acfe/validate/function/' . $function['acfe_validate_function'] . '/name=' . $field['name'],
447
- 'acfe/validate/function/' . $function['acfe_validate_function'] . '/type=' . $field['type'],
448
- 'acfe/validate/function/' . $function['acfe_validate_function'],
449
- );
450
-
451
- $filter_call = false;
452
- foreach($filters as $filter){
453
-
454
- if(has_filter($filter))
455
- $filter_call = $filter;
456
-
457
- }
458
-
459
- // Filter
460
- if($filter_call){
461
-
462
- $result = apply_filters($filter_call, false, $value, $field);
463
-
464
- }
465
-
466
- // Class
467
- elseif(is_callable(array($this, $function['acfe_validate_function']))){
468
-
469
- $result = call_user_func(array($this, $function['acfe_validate_function']), $value);
470
-
471
- }
472
-
473
- // Function
474
- elseif(is_callable($function['acfe_validate_function'])){
475
-
476
- $result = call_user_func($function['acfe_validate_function'], $value);
477
-
478
- }
479
-
480
- // Nothing
481
- else{
482
-
483
- continue;
484
-
485
- }
486
-
487
- // Vars
488
- $operator = $function['acfe_validate_operator'];
489
- $match = acf_maybe_get($function, 'acfe_validate_match');
490
-
491
- if($operator === '==' && $result == $match){
492
- $rule_match = true;
493
- }
494
-
495
- elseif($operator === '!=' && $result != $match){
496
- $rule_match = true;
497
- }
498
-
499
- elseif($operator === '>' && $result > $match){
500
- $rule_match = true;
501
- }
502
-
503
- elseif($operator === '>=' && $result >= $match){
504
- $rule_match = true;
505
- }
506
-
507
- elseif($operator === '<' && $result < $match){
508
- $rule_match = true;
509
- }
510
-
511
- elseif($operator === '<=' && $result <= $match){
512
- $rule_match = true;
513
- }
514
-
515
- elseif($operator === 'contains' && stripos($result, $match) !== false){
516
- $rule_match = true;
517
- }
518
-
519
- elseif($operator === '!contains' && stripos($result, $match) === false){
520
- $rule_match = true;
521
- }
522
-
523
- elseif($operator === 'starts' && stripos($result, $match) === 0){
524
- $rule_match = true;
525
- }
526
-
527
- elseif($operator === '!starts' && stripos($result, $match) !== 0){
528
- $rule_match = true;
529
- }
530
-
531
- elseif($operator === 'ends' && acfe_ends_with($result, $match)){
532
- $rule_match = true;
533
- }
534
-
535
- elseif($operator === '!ends' && !acfe_ends_with($result, $match)){
536
- $rule_match = true;
537
- }
538
-
539
- elseif($operator === 'true' && $result === true){
540
- $rule_match = true;
541
- }
542
-
543
- elseif($operator === 'false' && $result === false){
544
- $rule_match = true;
545
- }
546
-
547
- elseif($operator === 'null' && $result === null){
548
- $rule_match = true;
549
- }
550
-
551
- elseif($operator === 'empty' && empty($result)){
552
- $rule_match = true;
553
- }
554
-
555
- elseif($operator === '!empty' && !empty($result)){
556
- $rule_match = true;
557
- }
558
-
559
- }
560
-
561
- // Error
562
- $error = $rule['acfe_validate_error'];
563
-
564
- if($rule_match && !empty($error))
565
- $valid = $error;
566
-
567
- if(!$valid || is_string($valid))
568
- break;
569
-
570
- }
571
-
572
- return $valid;
573
-
574
- }
575
-
576
- function get_user_by_id($value){
577
-
578
- return get_user_by('id', $value);
579
-
580
- }
581
-
582
- function get_user_by_slug($value){
583
-
584
- return get_user_by('slug', $value);
585
-
586
- }
587
-
588
- function get_user_by_email($value){
589
-
590
- return get_user_by('email', $value);
591
-
592
- }
593
-
594
- function get_user_by_login($value){
595
-
596
- return get_user_by('login', $value);
597
-
598
- }
599
-
600
- function value($value){
601
-
602
- return $value;
603
-
604
- }
605
-
606
- /**
607
- * Process Setting
608
- */
609
- function fix_repeater($field){
610
-
611
- $field['prefix'] = str_replace('row-', '', $field['prefix']);
612
- $field['name'] = str_replace('row-', '', $field['name']);
613
-
614
- return $field;
615
-
616
- }
617
-
618
- /**
619
- * Setting: ACF Clone Index fix for flexible duplicate
620
- */
621
- function fix_clone($field){
622
-
623
- if(isset($field['acfe_validate']['acfcloneindex']))
624
- $field['acfe_validate'] = false;
625
-
626
- return $field;
627
-
628
- }
629
-
630
- }
631
-
632
- // initialize
633
- new acfe_field_validation();
634
-
635
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ if(!class_exists('acfe_field_validation')):
7
+
8
+ class acfe_field_validation{
9
+
10
+ public $functions = array();
11
+
12
+ function __construct(){
13
+
14
+ // Actions
15
+ add_action('load-post.php', array($this, 'load'));
16
+ add_action('wp_ajax_acf/field_group/render_field_settings', array($this, 'field_types_action'), 5);
17
+
18
+ // Filters
19
+ add_filter('acf/validate_value', array($this, 'validate_value'), 99, 4);
20
+
21
+ // Validation functions
22
+ $this->functions = array(
23
+
24
+ 'General' => array(
25
+ 'value' => 'If value',
26
+ 'strlen' => 'If value length - strlen(value)',
27
+ ),
28
+
29
+ 'Exists' => array(
30
+ 'email_exists' => 'If email exists - email_exists(value)',
31
+ 'post_type_exists' => 'If post type exists - post_type_exists(value)',
32
+ 'taxonomy_exists' => 'If taxonomy exists - taxonomy_exists(value)',
33
+ 'term_exists' => 'If term exists - term_exists(value)',
34
+ 'username_exists' => 'If username exists - username_exists(value)',
35
+ ),
36
+
37
+ 'Is' => array(
38
+ 'is_email' => 'If is email - is_email(value)',
39
+ 'is_user_logged_in' => 'If is user logged in - is_user_logged_in()',
40
+ ),
41
+
42
+ 'Sanitize' => array(
43
+ 'sanitize_email' => 'If sanitize email - sanitize_email(value)',
44
+ 'sanitize_file_name' => 'If sanitize file name - sanitize_file_name(value)',
45
+ 'sanitize_html_class' => 'If sanitize html class - sanitize_html_class(value)',
46
+ 'sanitize_key' => 'If sanitize key - sanitize_key(value)',
47
+ 'sanitize_meta' => 'If sanitize meta - sanitize_meta(value)',
48
+ 'sanitize_mime_type' => 'If sanitize mime type - sanitize_mime_type(value)',
49
+ 'sanitize_option' => 'If sanitize option - sanitize_option(value)',
50
+ 'sanitize_text_field' => 'If sanitize text field - sanitize_text_field(value)',
51
+ 'sanitize_title' => 'If sanitize title - sanitize_title(value)',
52
+ 'sanitize_user' => 'If sanitize user - sanitize_user(value)',
53
+ ),
54
+
55
+ 'User' => array(
56
+ 'get_user_by_id' => 'If get user by id - get_user_by(\'id\', value)',
57
+ 'get_user_by_slug' => 'If get user by slug - get_user_by(\'slug\', value)',
58
+ 'get_user_by_email' => 'If get user by email - get_user_by(\'email\', value)',
59
+ 'get_user_by_login' => 'If get user by login - get_user_by(\'login\', value)',
60
+ )
61
+
62
+ );
63
+
64
+ }
65
+
66
+ /**
67
+ * Load
68
+ */
69
+ function load(){
70
+
71
+ if(!acf_is_screen('acf-field-group'))
72
+ return;
73
+
74
+ $this->field_types_action();
75
+
76
+ // Fix: Repeater
77
+ add_filter('acf/prepare_field/name=acfe_validate', array($this, 'fix_repeater'));
78
+ add_filter('acf/prepare_field/name=acfe_validate_location', array($this, 'fix_repeater'));
79
+ add_filter('acf/prepare_field/name=acfe_validate_rules_and', array($this, 'fix_repeater'));
80
+ add_filter('acf/prepare_field/name=acfe_validate_function', array($this, 'fix_repeater'));
81
+ add_filter('acf/prepare_field/name=acfe_validate_operator', array($this, 'fix_repeater'));
82
+ add_filter('acf/prepare_field/name=acfe_validate_match', array($this, 'fix_repeater'));
83
+ add_filter('acf/prepare_field/name=acfe_validate_error', array($this, 'fix_repeater'));
84
+
85
+ // Fix: Clone
86
+ add_filter('acf/update_field', array($this, 'fix_clone'));
87
+
88
+ }
89
+
90
+ /**
91
+ * Get field types
92
+ */
93
+ function field_types_action(){
94
+
95
+ // Get Fields Types
96
+ foreach(acf_get_field_types_info() as $field){
97
+
98
+ // Field type
99
+ $field_type = $field['name'];
100
+
101
+ // Exclude
102
+ if(in_array($field_type, array('message', 'accordion', 'tab', 'acfe_button', 'acfe_column', 'acfe_dynamic_message', 'group', 'repeater', 'flexible_content', 'clone')))
103
+ continue;
104
+
105
+ add_action('acf/render_field_settings/type=' . $field_type, array($this, 'render_field_settings'), 990);
106
+
107
+ }
108
+
109
+ }
110
+
111
+ /**
112
+ * Add Setting
113
+ */
114
+ function render_field_settings($field){
115
+
116
+ $valid = false;
117
+
118
+ // Ajax
119
+ if(acf_verify_ajax()){
120
+
121
+ $field_group = acfe_get_field_group_from_field($field);
122
+
123
+ if(acf_maybe_get($field_group, 'acfe_form'))
124
+ $valid = true;
125
+
126
+ }
127
+
128
+ // Display
129
+ else{
130
+
131
+ if(acf_maybe_get($field, 'acfe_form'))
132
+ $valid = true;
133
+
134
+ if(!$valid && acf_maybe_get($field, '_name') === 'new_field'){
135
+
136
+ $field_group_id = get_the_ID();
137
+
138
+ if($field_group_id){
139
+
140
+ $field_group = acf_get_field_group($field_group_id);
141
+
142
+ if(acf_maybe_get($field_group, 'acfe_form'))
143
+ $valid = true;
144
+
145
+ }
146
+
147
+ }
148
+
149
+ }
150
+
151
+ if(!$valid)
152
+ return;
153
+
154
+ $choices = apply_filters('acfe/validate/functions', $this->functions, $field);
155
+
156
+ if(empty($choices))
157
+ return;
158
+
159
+ // Settings
160
+ acf_render_field_setting($field, array(
161
+ 'label' => __('Advanced validation'),
162
+ 'name' => 'acfe_validate',
163
+ 'key' => 'acfe_validate',
164
+ 'instructions' => __('Validate value against rules'),
165
+ 'type' => 'repeater',
166
+ 'button_label' => __('Add validation'),
167
+ 'required' => false,
168
+ 'layout' => 'row',
169
+ 'sub_fields' => array(
170
+ array(
171
+ 'label' => 'Location',
172
+ 'name' => 'acfe_validate_location',
173
+ 'key' => 'acfe_validate_location',
174
+ 'type' => 'select',
175
+ 'instructions' => '',
176
+ 'required' => 0,
177
+ 'conditional_logic' => 0,
178
+ 'wrapper' => array(
179
+ 'width' => '',
180
+ 'class' => '',
181
+ 'id' => '',
182
+ ),
183
+ 'choices' => array(
184
+ 'admin' => 'Administration',
185
+ 'front' => 'Front-end',
186
+ ),
187
+ 'allow_null' => true,
188
+ 'multiple' => 0,
189
+ 'ui' => 0,
190
+ 'return_format' => 'value',
191
+ 'ajax' => 0,
192
+ 'placeholder' => 'Everywhere',
193
+ ),
194
+ array(
195
+ 'label' => __('Rules'),
196
+ 'name' => 'acfe_validate_rules_and',
197
+ 'key' => 'acfe_validate_rules_and',
198
+ 'instructions' => '',
199
+ 'type' => 'repeater',
200
+ 'button_label' => __('+ AND'),
201
+ 'required' => false,
202
+ 'layout' => 'table',
203
+ 'sub_fields' => array(
204
+ array(
205
+ 'label' => 'Function',
206
+ 'name' => 'acfe_validate_function',
207
+ 'key' => 'acfe_validate_function',
208
+ 'prefix' => '',
209
+ '_name' => '',
210
+ '_prepare' => '',
211
+ 'type' => 'select',
212
+ 'choices' => $choices,
213
+ 'instructions' => false,
214
+ 'required' => false,
215
+ 'wrapper' => array(
216
+ 'width' => '',
217
+ 'class' => '',
218
+ 'id' => '',
219
+ ),
220
+ ),
221
+ array(
222
+ 'label' => 'Operator / Value',
223
+ 'name' => 'acfe_validate_operator',
224
+ 'key' => 'acfe_validate_operator',
225
+ 'prefix' => '',
226
+ '_name' => '',
227
+ '_prepare' => '',
228
+ 'type' => 'select',
229
+ 'choices' => array(
230
+ 'Operators' => array(
231
+ '==' => '==',
232
+ '!=' => '!=',
233
+ '>' => '>',
234
+ '>=' => '>=',
235
+ '<' => '<',
236
+ '<=' => '<=',
237
+ 'contains' => 'Contains',
238
+ '!contains' => 'Doesn\'t contain',
239
+ 'starts' => 'Starts with',
240
+ '!starts' => 'Doesn\'t start with',
241
+ 'ends' => 'Ends with',
242
+ '!ends' => 'Doesn\'t end with',
243
+ ),
244
+ 'Values' => array(
245
+ 'true' => '== true',
246
+ 'false' => '== false',
247
+ 'null' => '== null',
248
+ 'empty' => '== (empty)',
249
+ '!empty' => '!= (empty)',
250
+ )
251
+ ),
252
+ 'instructions' => false,
253
+ 'required' => false,
254
+ 'wrapper' => array(
255
+ 'width' => '',
256
+ 'class' => '',
257
+ 'id' => '',
258
+ ),
259
+ ),
260
+ array(
261
+ 'label' => 'Value',
262
+ 'name' => 'acfe_validate_match',
263
+ 'key' => 'acfe_validate_match',
264
+ 'prefix' => '',
265
+ '_name' => '',
266
+ '_prepare' => '',
267
+ 'type' => 'text',
268
+ 'instructions' => false,
269
+ 'placeholder' => '',
270
+ 'required' => false,
271
+ 'wrapper' => array(
272
+ 'width' => '',
273
+ 'class' => '',
274
+ 'id' => '',
275
+ ),
276
+ 'conditional_logic' => array(
277
+ array(
278
+ array(
279
+ 'field' => 'acfe_validate_operator',
280
+ 'operator' => '==',
281
+ 'value' => '==',
282
+ )
283
+ ),
284
+ array(
285
+ array(
286
+ 'field' => 'acfe_validate_operator',
287
+ 'operator' => '==',
288
+ 'value' => '!=',
289
+ )
290
+ ),
291
+ array(
292
+ array(
293
+ 'field' => 'acfe_validate_operator',
294
+ 'operator' => '==',
295
+ 'value' => '>',
296
+ )
297
+ ),
298
+ array(
299
+ array(
300
+ 'field' => 'acfe_validate_operator',
301
+ 'operator' => '==',
302
+ 'value' => '>=',
303
+ )
304
+ ),
305
+ array(
306
+ array(
307
+ 'field' => 'acfe_validate_operator',
308
+ 'operator' => '==',
309
+ 'value' => '<',
310
+ )
311
+ ),
312
+ array(
313
+ array(
314
+ 'field' => 'acfe_validate_operator',
315
+ 'operator' => '==',
316
+ 'value' => '<=',
317
+ )
318
+ ),
319
+ array(
320
+ array(
321
+ 'field' => 'acfe_validate_operator',
322
+ 'operator' => '==',
323
+ 'value' => 'contains',
324
+ )
325
+ ),
326
+ array(
327
+ array(
328
+ 'field' => 'acfe_validate_operator',
329
+ 'operator' => '==',
330
+ 'value' => '!contains',
331
+ )
332
+ ),
333
+ array(
334
+ array(
335
+ 'field' => 'acfe_validate_operator',
336
+ 'operator' => '==',
337
+ 'value' => 'starts',
338
+ )
339
+ ),
340
+ array(
341
+ array(
342
+ 'field' => 'acfe_validate_operator',
343
+ 'operator' => '==',
344
+ 'value' => '!starts',
345
+ )
346
+ ),
347
+ array(
348
+ array(
349
+ 'field' => 'acfe_validate_operator',
350
+ 'operator' => '==',
351
+ 'value' => 'ends',
352
+ )
353
+ ),
354
+ array(
355
+ array(
356
+ 'field' => 'acfe_validate_operator',
357
+ 'operator' => '==',
358
+ 'value' => '!ends',
359
+ )
360
+ ),
361
+ )
362
+ ),
363
+ )
364
+ ),
365
+ array(
366
+ 'label' => 'Error',
367
+ 'name' => 'acfe_validate_error',
368
+ 'key' => 'acfe_validate_error',
369
+ 'prefix' => '',
370
+ '_name' => '',
371
+ '_prepare' => '',
372
+ 'type' => 'text',
373
+ 'instructions' => false,
374
+ 'required' => false,
375
+ 'wrapper' => array(
376
+ 'width' => '',
377
+ 'class' => '',
378
+ 'id' => '',
379
+ ),
380
+ ),
381
+ )
382
+ ), false);
383
+
384
+ }
385
+
386
+ /**
387
+ * Validate
388
+ */
389
+ function validate_value($valid, $value, $field, $input){
390
+
391
+ if(!$valid)
392
+ return $valid;
393
+
394
+ if(!acf_maybe_get($field, 'acfe_validate'))
395
+ return $valid;
396
+
397
+ foreach($field['acfe_validate'] as $k => $rule){
398
+
399
+ // Fix possible ACF Clone Index
400
+ if($k === 'acfcloneindex')
401
+ continue;
402
+
403
+ // Screen
404
+ $screen = isset($rule['acfe_validate_location']) ? $rule['acfe_validate_location'] : '';
405
+ $screen_allow = false;
406
+
407
+ // Screen: All
408
+ if(empty($screen)){
409
+
410
+ $screen_allow = true;
411
+
412
+ }
413
+
414
+ // Screen: Admin
415
+ elseif($screen === 'admin' && acfe_form_is_admin()){
416
+
417
+ $screen_allow = true;
418
+
419
+ }
420
+
421
+ // Screen: Front
422
+ elseif($screen === 'front' && acfe_form_is_front()){
423
+
424
+ $screen_allow = true;
425
+
426
+ }
427
+
428
+ if(!$screen_allow)
429
+ continue;
430
+
431
+ if(!acf_maybe_get($rule, 'acfe_validate_rules_and'))
432
+ continue;
433
+
434
+ $rule_match = true;
435
+
436
+ foreach($rule['acfe_validate_rules_and'] as $k => $function){
437
+
438
+ if(!$rule_match)
439
+ break;
440
+
441
+ $rule_match = false;
442
+
443
+ // Check filters
444
+ $filters = array(
445
+ 'acfe/validate/function/' . $function['acfe_validate_function'] . '/key=' . $field['key'],
446
+ 'acfe/validate/function/' . $function['acfe_validate_function'] . '/name=' . $field['name'],
447
+ 'acfe/validate/function/' . $function['acfe_validate_function'] . '/type=' . $field['type'],
448
+ 'acfe/validate/function/' . $function['acfe_validate_function'],
449
+ );
450
+
451
+ $filter_call = false;
452
+ foreach($filters as $filter){
453
+
454
+ if(has_filter($filter))
455
+ $filter_call = $filter;
456
+
457
+ }
458
+
459
+ // Filter
460
+ if($filter_call){
461
+
462
+ $result = apply_filters($filter_call, false, $value, $field);
463
+
464
+ }
465
+
466
+ // Class
467
+ elseif(is_callable(array($this, $function['acfe_validate_function']))){
468
+
469
+ $result = call_user_func(array($this, $function['acfe_validate_function']), $value);
470
+
471
+ }
472
+
473
+ // Function
474
+ elseif(is_callable($function['acfe_validate_function'])){
475
+
476
+ $result = call_user_func($function['acfe_validate_function'], $value);
477
+
478
+ }
479
+
480
+ // Nothing
481
+ else{
482
+
483
+ continue;
484
+
485
+ }
486
+
487
+ // Vars
488
+ $operator = $function['acfe_validate_operator'];
489
+ $match = acf_maybe_get($function, 'acfe_validate_match');
490
+
491
+ if($operator === '==' && $result == $match){
492
+ $rule_match = true;
493
+ }
494
+
495
+ elseif($operator === '!=' && $result != $match){
496
+ $rule_match = true;
497
+ }
498
+
499
+ elseif($operator === '>' && $result > $match){
500
+ $rule_match = true;
501
+ }
502
+
503
+ elseif($operator === '>=' && $result >= $match){
504
+ $rule_match = true;
505
+ }
506
+
507
+ elseif($operator === '<' && $result < $match){
508
+ $rule_match = true;
509
+ }
510
+
511
+ elseif($operator === '<=' && $result <= $match){
512
+ $rule_match = true;
513
+ }
514
+
515
+ elseif($operator === 'contains' && stripos($result, $match) !== false){
516
+ $rule_match = true;
517
+ }
518
+
519
+ elseif($operator === '!contains' && stripos($result, $match) === false){
520
+ $rule_match = true;
521
+ }
522
+
523
+ elseif($operator === 'starts' && stripos($result, $match) === 0){
524
+ $rule_match = true;
525
+ }
526
+
527
+ elseif($operator === '!starts' && stripos($result, $match) !== 0){
528
+ $rule_match = true;
529
+ }
530
+
531
+ elseif($operator === 'ends' && acfe_ends_with($result, $match)){
532
+ $rule_match = true;
533
+ }
534
+
535
+ elseif($operator === '!ends' && !acfe_ends_with($result, $match)){
536
+ $rule_match = true;
537
+ }
538
+
539
+ elseif($operator === 'true' && $result === true){
540
+ $rule_match = true;
541
+ }
542
+
543
+ elseif($operator === 'false' && $result === false){
544
+ $rule_match = true;
545
+ }
546
+
547
+ elseif($operator === 'null' && $result === null){
548
+ $rule_match = true;
549
+ }
550
+
551
+ elseif($operator === 'empty' && empty($result)){
552
+ $rule_match = true;
553
+ }
554
+
555
+ elseif($operator === '!empty' && !empty($result)){
556
+ $rule_match = true;
557
+ }
558
+
559
+ }
560
+
561
+ // Error
562
+ $error = $rule['acfe_validate_error'];
563
+
564
+ if($rule_match && !empty($error))
565
+ $valid = $error;
566
+
567
+ if(!$valid || is_string($valid))
568
+ break;
569
+
570
+ }
571
+
572
+ return $valid;
573
+
574
+ }
575
+
576
+ function get_user_by_id($value){
577
+
578
+ return get_user_by('id', $value);
579
+
580
+ }
581
+
582
+ function get_user_by_slug($value){
583
+
584
+ return get_user_by('slug', $value);
585
+
586
+ }
587
+
588
+ function get_user_by_email($value){
589
+
590
+ return get_user_by('email', $value);
591
+
592
+ }
593
+
594
+ function get_user_by_login($value){
595
+
596
+ return get_user_by('login', $value);
597
+
598
+ }
599
+
600
+ function value($value){
601
+
602
+ return $value;
603
+
604
+ }
605
+
606
+ /**
607
+ * Process Setting
608
+ */
609
+ function fix_repeater($field){
610
+
611
+ $field['prefix'] = str_replace('row-', '', $field['prefix']);
612
+ $field['name'] = str_replace('row-', '', $field['name']);
613
+
614
+ return $field;
615
+
616
+ }
617
+
618
+ /**
619
+ * Setting: ACF Clone Index fix for flexible duplicate
620
+ */
621
+ function fix_clone($field){
622
+
623
+ if(isset($field['acfe_validate']['acfcloneindex']))
624
+ $field['acfe_validate'] = false;
625
+
626
+ return $field;
627
+
628
+ }
629
+
630
+ }
631
+
632
+ // initialize
633
+ new acfe_field_validation();
634
+
635
  endif;
includes/fields/field-button.php CHANGED
@@ -132,20 +132,33 @@ function my_acf_button_ajax($field, $post_id){
132
  * @bool/string $post_id Current post ID
133
  */
134
 
135
- echo 'Hello World';
136
 
137
  }
138
  </pre>
139
  <br />
140
  You can get access to Javascript ajax call using the following JS hooks:<br /><br />
141
  <pre>
142
- acf.addAction('acfe/fields/button/before_ajax', function($el){
 
 
 
 
 
 
 
 
143
  // $el
 
 
144
  });
145
 
146
- acf.addAction('acfe/fields/button/ajax_success', function(response, $el){
 
147
  // response
148
  // $el
 
 
149
  });
150
  </pre>
151
  <?php
132
  * @bool/string $post_id Current post ID
133
  */
134
 
135
+ wp_send_json('Success!');
136
 
137
  }
138
  </pre>
139
  <br />
140
  You can get access to Javascript ajax call using the following JS hooks:<br /><br />
141
  <pre>
142
+ acf.addAction('acfe/fields/button/before/name=my_button', function($el, data){
143
+
144
+ // $el
145
+
146
+ });
147
+
148
+ acf.addAction('acfe/fields/button/success/name=my_button', function(response, $el, data){
149
+
150
+ // response
151
  // $el
152
+ // data
153
+
154
  });
155
 
156
+ acf.addAction('acfe/fields/button/complete/name=my_button', function(response, $el, data){
157
+
158
  // response
159
  // $el
160
+ // data
161
+
162
  });
163
  </pre>
164
  <?php
includes/fields/field-file.php CHANGED
@@ -1,56 +1,56 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- add_filter('gettext', 'acfe_field_file_text', 99, 3);
7
- function acfe_field_file_text($translated_text, $text, $domain){
8
-
9
- if($domain !== 'acf')
10
- return $translated_text;
11
-
12
- if($text === 'No file selected')
13
- return '';
14
-
15
- return $translated_text;
16
-
17
- }
18
-
19
- add_action('acf/render_field_settings/type=file', 'acfe_field_file_settings', 0);
20
- function acfe_field_file_settings($field){
21
-
22
- acf_render_field_setting($field, array(
23
- 'label' => __('Uploader type'),
24
- 'name' => 'acfe_uploader',
25
- 'key' => 'acfe_uploader',
26
- 'instructions' => __('Choose the uploader type'),
27
- 'type' => 'radio',
28
- 'choices' => array(
29
- 'wp' => 'Media',
30
- 'basic' => 'Basic',
31
- ),
32
- 'allow_null' => 0,
33
- 'other_choice' => 0,
34
- 'default_value' => '',
35
- 'layout' => 'horizontal',
36
- 'return_format' => 'value',
37
- 'save_other_choice' => 0,
38
- ));
39
-
40
- }
41
-
42
- add_filter('acf/prepare_field/type=file', 'acfe_field_file_uploader_type');
43
- function acfe_field_file_uploader_type($field){
44
-
45
- if(!acf_maybe_get($field, 'acfe_uploader'))
46
- return $field;
47
-
48
- // ACFE Form force uploader type
49
- if(acf_is_filter_enabled('acfe/form/uploader'))
50
- return $field;
51
-
52
- acf_update_setting('uploader', $field['acfe_uploader']);
53
-
54
- return $field;
55
-
56
  }
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ add_filter('gettext', 'acfe_field_file_text', 99, 3);
7
+ function acfe_field_file_text($translated_text, $text, $domain){
8
+
9
+ if($domain !== 'acf')
10
+ return $translated_text;
11
+
12
+ if($text === 'No file selected')
13
+ return '';
14
+
15
+ return $translated_text;
16
+
17
+ }
18
+
19
+ add_action('acf/render_field_settings/type=file', 'acfe_field_file_settings', 0);
20
+ function acfe_field_file_settings($field){
21
+
22
+ acf_render_field_setting($field, array(
23
+ 'label' => __('Uploader type'),
24
+ 'name' => 'acfe_uploader',
25
+ 'key' => 'acfe_uploader',
26
+ 'instructions' => __('Choose the uploader type'),
27
+ 'type' => 'radio',
28
+ 'choices' => array(
29
+ 'wp' => 'Media',
30
+ 'basic' => 'Basic',
31
+ ),
32
+ 'allow_null' => 0,
33
+ 'other_choice' => 0,
34
+ 'default_value' => '',
35
+ 'layout' => 'horizontal',
36
+ 'return_format' => 'value',
37
+ 'save_other_choice' => 0,
38
+ ));
39
+
40
+ }
41
+
42
+ add_filter('acf/prepare_field/type=file', 'acfe_field_file_uploader_type');
43
+ function acfe_field_file_uploader_type($field){
44
+
45
+ if(!acf_maybe_get($field, 'acfe_uploader'))
46
+ return $field;
47
+
48
+ // ACFE Form force uploader type
49
+ if(acf_is_filter_enabled('acfe/form/uploader'))
50
+ return $field;
51
+
52
+ acf_update_setting('uploader', $field['acfe_uploader']);
53
+
54
+ return $field;
55
+
56
  }
includes/fields/field-flexible-content.php CHANGED
@@ -39,6 +39,7 @@ function acfe_flexible_layout_preview($args = array()){
39
  $layout['acfe_flexible_thumbnail'] = false;
40
 
41
  // Flexible Thumbnails
 
42
  $layout['acfe_flexible_thumbnail'] = apply_filters('acfe/flexible/thumbnail/name=' . $field['_name'], $layout['acfe_flexible_thumbnail'], $field, $layout);
43
  $layout['acfe_flexible_thumbnail'] = apply_filters('acfe/flexible/thumbnail/key=' . $field['key'], $layout['acfe_flexible_thumbnail'], $field, $layout);
44
 
@@ -1205,6 +1206,7 @@ class acfe_field_flexible_content extends acf_field_flexible_content{
1205
  $thumbnail = $layout['acfe_flexible_thumbnail'] ? $layout['acfe_flexible_thumbnail'] : false;
1206
 
1207
  // Filters
 
1208
  $thumbnail = apply_filters('acfe/flexible/thumbnail/name=' . $field['_name'], $thumbnail, $field, $layout);
1209
  $thumbnail = apply_filters('acfe/flexible/thumbnail/key=' . $field['key'], $thumbnail, $field, $layout);
1210
 
39
  $layout['acfe_flexible_thumbnail'] = false;
40
 
41
  // Flexible Thumbnails
42
+ $layout['acfe_flexible_thumbnail'] = apply_filters('acfe/flexible/thumbnail', $layout['acfe_flexible_thumbnail'], $field, $layout);
43
  $layout['acfe_flexible_thumbnail'] = apply_filters('acfe/flexible/thumbnail/name=' . $field['_name'], $layout['acfe_flexible_thumbnail'], $field, $layout);
44
  $layout['acfe_flexible_thumbnail'] = apply_filters('acfe/flexible/thumbnail/key=' . $field['key'], $layout['acfe_flexible_thumbnail'], $field, $layout);
45
 
1206
  $thumbnail = $layout['acfe_flexible_thumbnail'] ? $layout['acfe_flexible_thumbnail'] : false;
1207
 
1208
  // Filters
1209
+ $thumbnail = apply_filters('acfe/flexible/thumbnail', $thumbnail, $field, $layout);
1210
  $thumbnail = apply_filters('acfe/flexible/thumbnail/name=' . $field['_name'], $thumbnail, $field, $layout);
1211
  $thumbnail = apply_filters('acfe/flexible/thumbnail/key=' . $field['key'], $thumbnail, $field, $layout);
1212
 
includes/fields/field-image.php CHANGED
@@ -1,84 +1,84 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- add_filter('gettext', 'acfe_field_image_text', 99, 3);
7
- function acfe_field_image_text($translated_text, $text, $domain){
8
-
9
- if($domain !== 'acf')
10
- return $translated_text;
11
-
12
- if($text === 'No image selected')
13
- return '';
14
-
15
- return $translated_text;
16
-
17
- }
18
-
19
- add_action('acf/render_field_settings/type=image', 'acfe_field_image_settings', 0);
20
- function acfe_field_image_settings($field){
21
-
22
- acf_render_field_setting($field, array(
23
- 'label' => __('Uploader type'),
24
- 'name' => 'acfe_uploader',
25
- 'key' => 'acfe_uploader',
26
- 'instructions' => __('Choose the uploader type'),
27
- 'type' => 'radio',
28
- 'choices' => array(
29
- 'wp' => 'Media',
30
- 'basic' => 'Basic',
31
- ),
32
- 'allow_null' => 0,
33
- 'other_choice' => 0,
34
- 'default_value' => '',
35
- 'layout' => 'horizontal',
36
- 'return_format' => 'value',
37
- 'save_other_choice' => 0,
38
- ));
39
-
40
- acf_render_field_setting($field, array(
41
- 'label' => __('Featured thumbnail'),
42
- 'name' => 'acfe_thumbnail',
43
- 'key' => 'acfe_thumbnail',
44
- 'instructions' => __('Make this image the featured thumbnail'),
45
- 'type' => 'true_false',
46
- 'default_value' => false,
47
- 'ui' => true,
48
- 'ui_on_text' => '',
49
- 'ui_off_text' => '',
50
- 'required' => false,
51
- ));
52
-
53
- }
54
-
55
- add_filter('acf/prepare_field/type=image', 'acfe_field_image_uploader_type');
56
- function acfe_field_image_uploader_type($field){
57
-
58
- if(!acf_maybe_get($field, 'acfe_uploader'))
59
- return $field;
60
-
61
- // ACFE Form force uploader type
62
- if(acf_is_filter_enabled('acfe/form/uploader'))
63
- return $field;
64
-
65
- acf_update_setting('uploader', $field['acfe_uploader']);
66
-
67
- return $field;
68
-
69
- }
70
-
71
- /**
72
- * Field Value Update
73
- */
74
- add_filter('acf/update_value/type=image', 'acfe_thumbnail_update', 10, 3);
75
- function acfe_thumbnail_update($value, $post_id, $field){
76
-
77
- if(!isset($field['acfe_thumbnail']) || empty($field['acfe_thumbnail']) || empty($value) || empty(get_post_type($post_id)))
78
- return $value;
79
-
80
- update_post_meta($post_id, '_thumbnail_id', $value);
81
-
82
- return $value;
83
-
84
  }
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ add_filter('gettext', 'acfe_field_image_text', 99, 3);
7
+ function acfe_field_image_text($translated_text, $text, $domain){
8
+
9
+ if($domain !== 'acf')
10
+ return $translated_text;
11
+
12
+ if($text === 'No image selected')
13
+ return '';
14
+
15
+ return $translated_text;
16
+
17
+ }
18
+
19
+ add_action('acf/render_field_settings/type=image', 'acfe_field_image_settings', 0);
20
+ function acfe_field_image_settings($field){
21
+
22
+ acf_render_field_setting($field, array(
23
+ 'label' => __('Uploader type'),
24
+ 'name' => 'acfe_uploader',
25
+ 'key' => 'acfe_uploader',
26
+ 'instructions' => __('Choose the uploader type'),
27
+ 'type' => 'radio',
28
+ 'choices' => array(
29
+ 'wp' => 'Media',
30
+ 'basic' => 'Basic',
31
+ ),
32
+ 'allow_null' => 0,
33
+ 'other_choice' => 0,
34
+ 'default_value' => '',
35
+ 'layout' => 'horizontal',
36
+ 'return_format' => 'value',
37
+ 'save_other_choice' => 0,
38
+ ));
39
+
40
+ acf_render_field_setting($field, array(
41
+ 'label' => __('Featured thumbnail'),
42
+ 'name' => 'acfe_thumbnail',
43
+ 'key' => 'acfe_thumbnail',
44
+ 'instructions' => __('Make this image the featured thumbnail'),
45
+ 'type' => 'true_false',
46
+ 'default_value' => false,
47
+ 'ui' => true,
48
+ 'ui_on_text' => '',
49
+ 'ui_off_text' => '',
50
+ 'required' => false,
51
+ ));
52
+
53
+ }
54
+
55
+ add_filter('acf/prepare_field/type=image', 'acfe_field_image_uploader_type');
56
+ function acfe_field_image_uploader_type($field){
57
+
58
+ if(!acf_maybe_get($field, 'acfe_uploader'))
59
+ return $field;
60
+
61
+ // ACFE Form force uploader type
62
+ if(acf_is_filter_enabled('acfe/form/uploader'))
63
+ return $field;
64
+
65
+ acf_update_setting('uploader', $field['acfe_uploader']);
66
+
67
+ return $field;
68
+
69
+ }
70
+
71
+ /**
72
+ * Field Value Update
73
+ */
74
+ add_filter('acf/update_value/type=image', 'acfe_thumbnail_update', 10, 3);
75
+ function acfe_thumbnail_update($value, $post_id, $field){
76
+
77
+ if(!isset($field['acfe_thumbnail']) || empty($field['acfe_thumbnail']) || empty($value) || empty(get_post_type($post_id)))
78
+ return $value;
79
+
80
+ update_post_meta($post_id, '_thumbnail_id', $value);
81
+
82
+ return $value;
83
+
84
  }
includes/modules/form/actions/custom.php CHANGED
@@ -1,205 +1,205 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- if(!class_exists('acfe_form_custom')):
7
-
8
- class acfe_form_custom{
9
-
10
- function __construct(){
11
-
12
- add_filter('acf/validate_value/name=acfe_form_custom_action', array($this, 'validate'), 10, 4);
13
-
14
- add_action('acf/render_field/name=acfe_form_custom_action_advanced_load', array($this, 'advanced_load'));
15
- add_action('acf/render_field/name=acfe_form_custom_action_advanced_validation', array($this, 'advanced_validation'));
16
- add_action('acf/render_field/name=acfe_form_custom_action_advanced_submit', array($this, 'advanced_submit'));
17
-
18
- }
19
-
20
- function validate($valid, $value, $field, $input){
21
-
22
- if(!$valid)
23
- return $valid;
24
-
25
- $reserved = array(
26
- 'custom',
27
- 'email',
28
- 'post',
29
- 'term',
30
- );
31
-
32
- if(in_array($value, $reserved))
33
- $valid = 'This action name is not authorized';
34
-
35
- return $valid;
36
- }
37
-
38
- function advanced_load($field){
39
-
40
- $form_name = 'my_form';
41
-
42
- if(acf_maybe_get($field, 'value'))
43
- $form_name = get_field('acfe_form_name', $field['value']);
44
-
45
- ?>You may use the following hooks:<br /><br />
46
- <pre data-codemirror="php-plain">
47
- add_filter('acfe/form/load/my-custom-action', 'my_form_custom_action_load', 10, 3);
48
- add_filter('acfe/form/load/my-custom-action/form=<?php echo $form_name; ?>', 'my_form_custom_action_load', 10, 3);</pre>
49
- <br />
50
- <pre>
51
- /**
52
- * @array $form The form settings
53
- * @int $post_id Current post ID
54
- * @string $alias Action alias (Empty for custom actions)
55
- */
56
- add_filter('acfe/form/submit/my-custom-action', 'my_form_custom_action_load', 10, 3);
57
- function my_form_custom_action_load($form, $post_id, $alias){
58
-
59
- /**
60
- * Set a custom query var
61
- * The value '145' can be retrieved in an another action using the template tag:
62
- * {query_var:my_tag}
63
- */
64
- set_query_var('my_tag', 145);
65
-
66
-
67
- /**
68
- * Set a custom query var array
69
- * The values can be retrieved in an another action using the template tags:
70
- * {query_var:my_tag:target} {query_var:my_tag:load}
71
- */
72
- set_query_var('my_tag', array(
73
- 'target' => 145,
74
- 'load' => 12,
75
- );
76
-
77
- /**
78
- * Change form success message dynamically
79
- */
80
- $form['updated_message'] = 'New success message!';
81
-
82
- /**
83
- * Change form redirection URL
84
- */
85
- $form['field_acfe_form_return'] = '/thank-you';
86
-
87
- /**
88
- * Return arguments
89
- * Note: Return false will hide the form
90
- */
91
- return $form;
92
-
93
- }
94
- </pre><?php
95
-
96
- }
97
-
98
- function advanced_validation($field){
99
-
100
- $form_name = 'my_form';
101
-
102
- if(acf_maybe_get($field, 'value'))
103
- $form_name = get_field('acfe_form_name', $field['value']);
104
-
105
- ?>You may use the following hooks:<br /><br />
106
- <pre data-codemirror="php-plain">
107
- add_action('acfe/form/validation/my-custom-action', 'my_form_custom_action_validation', 10, 3);
108
- add_filter('acfe/form/validation/my-custom-action/form=<?php echo $form_name; ?>', 'my_form_custom_action_validation', 10, 3);</pre>
109
- <br />
110
- <pre>
111
- /**
112
- * @array $form The form settings
113
- * @int $post_id Current post ID
114
- * @string $alias Action alias (Empty for custom actions)
115
- */
116
- add_action('acfe/form/validation/my-custom-action', 'my_form_custom_action_validation', 10, 3);
117
- function my_form_custom_action_validation($form, $post_id, $alias){
118
-
119
- /**
120
- * Get the form input value named 'my_field'
121
- * This is the value entered by the user during the form submission
122
- */
123
- $my_field = get_field('my_field');
124
- $my_field_unformatted = get_field('my_field', false, false);
125
-
126
- if($my_field === 'Hello'){
127
-
128
- // Add validation error
129
- acfe_add_validation_error('my_field', 'Hello is not allowed');
130
-
131
- }
132
-
133
-
134
- /**
135
- * Get the field value 'my_field' from the post ID 145
136
- */
137
- $post_my_field = get_field('my_field', 145);
138
- $post_my_field_unformatted = get_field('my_field', 145, false);
139
-
140
- }
141
- </pre><?php
142
-
143
- }
144
-
145
- function advanced_submit($field){
146
-
147
- $form_name = 'my_form';
148
-
149
- if(acf_maybe_get($field, 'value'))
150
- $form_name = get_field('acfe_form_name', $field['value']);
151
-
152
- ?>You may use the following hooks:<br /><br />
153
- <pre data-codemirror="php-plain">
154
- add_action('acfe/form/submit/my-custom-action', 'my_form_custom_action', 10, 2);
155
- add_action('acfe/form/submit/my-custom-action/form=<?php echo $form_name; ?>', 'my_form_custom_action', 10, 2);</pre>
156
- <br />
157
- <pre>
158
- /**
159
- * @array $form The form settings
160
- * @int $post_id Current post ID
161
- */
162
- add_action('acfe/form/submit/my-custom-action', 'my_form_custom_action', 10, 2);
163
- function my_form_custom_action($form, $post_id){
164
-
165
- /**
166
- * Get the value from the form input named 'my_field'
167
- * This is the value entered by the user during the form submission
168
- */
169
- $my_field = get_field('my_field');
170
-
171
-
172
- /**
173
- * Get the field value 'my_field' from the post ID 145
174
- */
175
- $my_post_field = get_field('my_field', 145);
176
-
177
-
178
- /**
179
- * Set a custom query var
180
- * The value '145' can be retrieved in an another action using the template tag:
181
- * {query_var:my_tag}
182
- */
183
- set_query_var('my_tag', 145);
184
-
185
-
186
- /**
187
- * Set a custom query var array
188
- * The values can be retrieved in an another action using the template tags:
189
- * {query_var:my_tag:post_id} {query_var:my_tag:user}
190
- */
191
- set_query_var('my_tag', array(
192
- 'post_id' => 145,
193
- 'user' => 12,
194
- );
195
-
196
- }
197
- </pre><?php
198
-
199
- }
200
-
201
- }
202
-
203
- new acfe_form_custom();
204
-
205
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ if(!class_exists('acfe_form_custom')):
7
+
8
+ class acfe_form_custom{
9
+
10
+ function __construct(){
11
+
12
+ add_filter('acf/validate_value/name=acfe_form_custom_action', array($this, 'validate'), 10, 4);
13
+
14
+ add_action('acf/render_field/name=acfe_form_custom_action_advanced_load', array($this, 'advanced_load'));
15
+ add_action('acf/render_field/name=acfe_form_custom_action_advanced_validation', array($this, 'advanced_validation'));
16
+ add_action('acf/render_field/name=acfe_form_custom_action_advanced_submit', array($this, 'advanced_submit'));
17
+
18
+ }
19
+
20
+ function validate($valid, $value, $field, $input){
21
+
22
+ if(!$valid)
23
+ return $valid;
24
+
25
+ $reserved = array(
26
+ 'custom',
27
+ 'email',
28
+ 'post',
29
+ 'term',
30
+ );
31
+
32
+ if(in_array($value, $reserved))
33
+ $valid = 'This action name is not authorized';
34
+
35
+ return $valid;
36
+ }
37
+
38
+ function advanced_load($field){
39
+
40
+ $form_name = 'my_form';
41
+
42
+ if(acf_maybe_get($field, 'value'))
43
+ $form_name = get_field('acfe_form_name', $field['value']);
44
+
45
+ ?>You may use the following hooks:<br /><br />
46
+ <pre data-codemirror="php-plain">
47
+ add_filter('acfe/form/load/my-custom-action', 'my_form_custom_action_load', 10, 3);
48
+ add_filter('acfe/form/load/my-custom-action/form=<?php echo $form_name; ?>', 'my_form_custom_action_load', 10, 3);</pre>
49
+ <br />
50
+ <pre>
51
+ /**
52
+ * @array $form The form settings
53
+ * @int $post_id Current post ID
54
+ * @string $alias Action alias (Empty for custom actions)
55
+ */
56
+ add_filter('acfe/form/load/my-custom-action', 'my_form_custom_action_load', 10, 3);
57
+ function my_form_custom_action_load($form, $post_id, $alias){
58
+
59
+ /**
60
+ * Set a custom query var
61
+ * The value '145' can be retrieved in an another action using the template tag:
62
+ * {query_var:my_tag}
63
+ */
64
+ set_query_var('my_tag', 145);
65
+
66
+
67
+ /**
68
+ * Set a custom query var array
69
+ * The values can be retrieved in an another action using the template tags:
70
+ * {query_var:my_tag:target} {query_var:my_tag:load}
71
+ */
72
+ set_query_var('my_tag', array(
73
+ 'target' => 145,
74
+ 'load' => 12,
75
+ );
76
+
77
+ /**
78
+ * Change form success message dynamically
79
+ */
80
+ $form['updated_message'] = 'New success message!';
81
+
82
+ /**
83
+ * Change form redirection URL
84
+ */
85
+ $form['return'] = '/thank-you';
86
+
87
+ /**
88
+ * Return arguments
89
+ * Note: Return false will hide the form
90
+ */
91
+ return $form;
92
+
93
+ }
94
+ </pre><?php
95
+
96
+ }
97
+
98
+ function advanced_validation($field){
99
+
100
+ $form_name = 'my_form';
101
+
102
+ if(acf_maybe_get($field, 'value'))
103
+ $form_name = get_field('acfe_form_name', $field['value']);
104
+
105
+ ?>You may use the following hooks:<br /><br />
106
+ <pre data-codemirror="php-plain">
107
+ add_action('acfe/form/validation/my-custom-action', 'my_form_custom_action_validation', 10, 3);
108
+ add_filter('acfe/form/validation/my-custom-action/form=<?php echo $form_name; ?>', 'my_form_custom_action_validation', 10, 3);</pre>
109
+ <br />
110
+ <pre>
111
+ /**
112
+ * @array $form The form settings
113
+ * @int $post_id Current post ID
114
+ * @string $alias Action alias (Empty for custom actions)
115
+ */
116
+ add_action('acfe/form/validation/my-custom-action', 'my_form_custom_action_validation', 10, 3);
117
+ function my_form_custom_action_validation($form, $post_id, $alias){
118
+
119
+ /**
120
+ * Get the form input value named 'my_field'
121
+ * This is the value entered by the user during the form submission
122
+ */
123
+ $my_field = get_field('my_field');
124
+ $my_field_unformatted = get_field('my_field', false, false);
125
+
126
+ if($my_field === 'Hello'){
127
+
128
+ // Add validation error
129
+ acfe_add_validation_error('my_field', 'Hello is not allowed');
130
+
131
+ }
132
+
133
+
134
+ /**
135
+ * Get the field value 'my_field' from the post ID 145
136
+ */
137
+ $post_my_field = get_field('my_field', 145);
138
+ $post_my_field_unformatted = get_field('my_field', 145, false);
139
+
140
+ }
141
+ </pre><?php
142
+
143
+ }
144
+
145
+ function advanced_submit($field){
146
+
147
+ $form_name = 'my_form';
148
+
149
+ if(acf_maybe_get($field, 'value'))
150
+ $form_name = get_field('acfe_form_name', $field['value']);
151
+
152
+ ?>You may use the following hooks:<br /><br />
153
+ <pre data-codemirror="php-plain">
154
+ add_action('acfe/form/submit/my-custom-action', 'my_form_custom_action', 10, 2);
155
+ add_action('acfe/form/submit/my-custom-action/form=<?php echo $form_name; ?>', 'my_form_custom_action', 10, 2);</pre>
156
+ <br />
157
+ <pre>
158
+ /**
159
+ * @array $form The form settings
160
+ * @int $post_id Current post ID
161
+ */
162
+ add_action('acfe/form/submit/my-custom-action', 'my_form_custom_action', 10, 2);
163
+ function my_form_custom_action($form, $post_id){
164
+
165
+ /**
166
+ * Get the value from the form input named 'my_field'
167
+ * This is the value entered by the user during the form submission
168
+ */
169
+ $my_field = get_field('my_field');
170
+
171
+
172
+ /**
173
+ * Get the field value 'my_field' from the post ID 145
174
+ */
175
+ $my_post_field = get_field('my_field', 145);
176
+
177
+
178
+ /**
179
+ * Set a custom query var
180
+ * The value '145' can be retrieved in an another action using the template tag:
181
+ * {query_var:my_tag}
182
+ */
183
+ set_query_var('my_tag', 145);
184
+
185
+
186
+ /**
187
+ * Set a custom query var array
188
+ * The values can be retrieved in an another action using the template tags:
189
+ * {query_var:my_tag:post_id} {query_var:my_tag:user}
190
+ */
191
+ set_query_var('my_tag', array(
192
+ 'post_id' => 145,
193
+ 'user' => 12,
194
+ );
195
+
196
+ }
197
+ </pre><?php
198
+
199
+ }
200
+
201
+ }
202
+
203
+ new acfe_form_custom();
204
+
205
  endif;
includes/modules/form/actions/post.php CHANGED
@@ -1,828 +1,834 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- if(!class_exists('acfe_form_post')):
7
-
8
- class acfe_form_post{
9
-
10
- function __construct(){
11
-
12
- /*
13
- * Form
14
- */
15
- add_filter('acfe/form/load/post', array($this, 'load'), 1, 3);
16
- add_action('acfe/form/prepare/post', array($this, 'prepare'), 1, 3);
17
- add_action('acfe/form/submit/post', array($this, 'submit'), 1, 5);
18
-
19
- /*
20
- * Admin
21
- */
22
- add_filter('acf/prepare_field/name=acfe_form_post_save_meta', array(acfe()->acfe_form, 'map_fields'));
23
- add_filter('acf/prepare_field/name=acfe_form_post_load_meta', array(acfe()->acfe_form, 'map_fields'));
24
-
25
- add_filter('acf/prepare_field/name=acfe_form_post_save_target', array(acfe()->acfe_form, 'map_fields_deep'));
26
- add_filter('acf/prepare_field/name=acfe_form_post_load_source', array(acfe()->acfe_form, 'map_fields_deep'));
27
-
28
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_type', array(acfe()->acfe_form, 'map_fields_deep'));
29
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_status', array(acfe()->acfe_form, 'map_fields_deep'));
30
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_title', array(acfe()->acfe_form, 'map_fields_deep'));
31
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_name', array(acfe()->acfe_form, 'map_fields_deep'));
32
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_content', array(acfe()->acfe_form, 'map_fields_deep'));
33
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_author', array(acfe()->acfe_form, 'map_fields_deep'));
34
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_parent', array(acfe()->acfe_form, 'map_fields_deep'));
35
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_terms', array(acfe()->acfe_form, 'map_fields_deep'));
36
-
37
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_type', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
38
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_status', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
39
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_title', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
40
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
41
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_content', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
42
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_author', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
43
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_parent', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
44
- add_filter('acf/prepare_field/name=acfe_form_post_map_post_terms', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
45
-
46
- add_filter('acf/prepare_field/name=acfe_form_post_save_target', array($this, 'prepare_choices'), 5);
47
- add_filter('acf/prepare_field/name=acfe_form_post_load_source', array($this, 'prepare_choices'), 5);
48
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_parent', array($this, 'prepare_choices'), 5);
49
- add_filter('acf/prepare_field/name=acfe_form_post_save_post_author', array($this, 'prepare_choices_users'), 5);
50
-
51
-
52
- add_action('acf/render_field/name=acfe_form_post_advanced_load', array($this, 'advanced_load'));
53
- add_action('acf/render_field/name=acfe_form_post_advanced_save_args', array($this, 'advanced_save_args'));
54
- add_action('acf/render_field/name=acfe_form_post_advanced_save', array($this, 'advanced_save'));
55
-
56
- }
57
-
58
- function load($form, $current_post_id, $action){
59
-
60
- // Form
61
- $form_name = acf_maybe_get($form, 'form_name');
62
- $form_id = acf_maybe_get($form, 'form_id');
63
-
64
- // Action
65
- $post_action = get_sub_field('acfe_form_post_action');
66
-
67
- // Load values
68
- $load_values = get_sub_field('acfe_form_post_load_values');
69
- $load_meta = get_sub_field('acfe_form_post_load_meta');
70
-
71
- // Load values
72
- if(!$load_values)
73
- return $form;
74
-
75
- $_post_id = get_sub_field('acfe_form_post_load_source');
76
- $_post_type = get_sub_field('acfe_form_post_map_post_type');
77
- $_post_status = get_sub_field('acfe_form_post_map_post_status');
78
- $_post_title = get_sub_field('acfe_form_post_map_post_title');
79
- $_post_name = get_sub_field('acfe_form_post_map_post_name');
80
- $_post_content = get_sub_field('acfe_form_post_map_post_content');
81
- $_post_author = get_sub_field('acfe_form_post_map_post_author');
82
- $_post_parent = get_sub_field('acfe_form_post_map_post_parent');
83
- $_post_terms = get_sub_field('acfe_form_post_map_post_terms');
84
-
85
- // Map {field:name} {get_field:name} {query_var:name}
86
- $_post_id = acfe_form_map_field_value_load($_post_id, $current_post_id, $form);
87
- $_post_type = acfe_form_map_field_value_load($_post_type, $current_post_id, $form);
88
- $_post_status = acfe_form_map_field_value_load($_post_status, $current_post_id, $form);
89
- $_post_title = acfe_form_map_field_value_load($_post_title, $current_post_id, $form);
90
- $_post_name = acfe_form_map_field_value_load($_post_name, $current_post_id, $form);
91
- $_post_content = acfe_form_map_field_value_load($_post_content, $current_post_id, $form);
92
- $_post_author = acfe_form_map_field_value_load($_post_author, $current_post_id, $form);
93
- $_post_parent = acfe_form_map_field_value_load($_post_parent, $current_post_id, $form);
94
- $_post_terms = acfe_form_map_field_value_load($_post_terms, $current_post_id, $form);
95
-
96
- // Filters
97
- $_post_id = apply_filters('acfe/form/load/post_id', $_post_id, $form, $action);
98
- $_post_id = apply_filters('acfe/form/load/post_id/form=' . $form_name, $_post_id, $form, $action);
99
-
100
- if(!empty($action))
101
- $_post_id = apply_filters('acfe/form/load/post_id/action=' . $action, $_post_id, $form, $action);
102
-
103
- // Invalid Post ID
104
- if(!$_post_id)
105
- return $form;
106
-
107
- // Post type
108
- if(acf_is_field_key($_post_type)){
109
-
110
- $key = array_search($_post_type, $load_meta);
111
-
112
- if($key !== false)
113
- unset($load_meta[$key]);
114
-
115
- $form['map'][$_post_type]['value'] = get_post_field('post_type', $_post_id);
116
-
117
- }
118
-
119
- // Post status
120
- if(acf_is_field_key($_post_status)){
121
-
122
- $key = array_search($_post_status, $load_meta);
123
-
124
- if($key !== false)
125
- unset($load_meta[$key]);
126
-
127
- $form['map'][$_post_status]['value'] = get_post_field('post_status', $_post_id);
128
-
129
- }
130
-
131
- // Post title
132
- if(acf_is_field_key($_post_title)){
133
-
134
- $key = array_search($_post_title, $load_meta);
135
-
136
- if($key !== false)
137
- unset($load_meta[$key]);
138
-
139
- $form['map'][$_post_title]['value'] = get_post_field('post_title', $_post_id);
140
-
141
- }
142
-
143
- // Post name
144
- if(acf_is_field_key($_post_name)){
145
-
146
- $key = array_search($_post_name, $load_meta);
147
-
148
- if($key !== false)
149
- unset($load_meta[$key]);
150
-
151
- $form['map'][$_post_name]['value'] = get_post_field('post_name', $_post_id);
152
-
153
- }
154
-
155
- // Post content
156
- if(acf_is_field_key($_post_content)){
157
-
158
- $key = array_search($_post_content, $load_meta);
159
-
160
- if($key !== false)
161
- unset($load_meta[$key]);
162
-
163
- $form['map'][$_post_content]['value'] = get_post_field('post_content', $_post_id);
164
-
165
- }
166
-
167
- // Post author
168
- if(acf_is_field_key($_post_author)){
169
-
170
- $key = array_search($_post_author, $load_meta);
171
-
172
- if($key !== false)
173
- unset($load_meta[$key]);
174
-
175
- $form['map'][$_post_author]['value'] = get_post_field('post_author', $_post_id);
176
-
177
- }
178
-
179
- // Post parent
180
- if(acf_is_field_key($_post_parent)){
181
-
182
- $key = array_search($_post_parent, $load_meta);
183
-
184
- if($key !== false)
185
- unset($load_meta[$key]);
186
-
187
- $form['map'][$_post_parent]['value'] = get_post_field('post_parent', $_post_id);
188
-
189
- }
190
-
191
- // Post terms
192
- if(acf_is_field_key($_post_terms)){
193
-
194
- $key = array_search($_post_terms, $load_meta);
195
-
196
- if($key !== false)
197
- unset($load_meta[$key]);
198
-
199
- $taxonomies = acf_get_taxonomies(array(
200
- 'post_type' => get_post_type($_post_id)
201
- ));
202
-
203
- if(!empty($taxonomies)){
204
-
205
- $terms = array();
206
-
207
- foreach($taxonomies as $taxonomy){
208
-
209
- $get_the_terms = get_the_terms($_post_id, $taxonomy);
210
- if(!$get_the_terms || is_wp_error($get_the_terms))
211
- continue;
212
-
213
- $terms = array_merge($terms, $get_the_terms);
214
-
215
- }
216
-
217
- $return = wp_list_pluck($terms, 'term_id');
218
-
219
- $form['map'][$_post_terms]['value'] = $return;
220
-
221
- }
222
-
223
- }
224
-
225
- // Load others values
226
- if(!empty($load_meta)){
227
-
228
- foreach($load_meta as $field_key){
229
-
230
- $field = acf_get_field($field_key);
231
-
232
- $form['map'][$field_key]['value'] = acf_get_value($_post_id, $field);
233
-
234
- }
235
-
236
- }
237
-
238
- return $form;
239
-
240
- }
241
-
242
- function prepare($form, $current_post_id, $action){
243
-
244
- // Form
245
- $form_name = acf_maybe_get($form, 'form_name');
246
- $form_id = acf_maybe_get($form, 'form_id');
247
-
248
- // Action
249
- $post_action = get_sub_field('acfe_form_post_action');
250
-
251
- // Load values
252
- $load_values = get_sub_field('acfe_form_post_load_values');
253
-
254
- // Pre-process
255
- $_post_content_group = get_sub_field('acfe_form_post_save_post_content_group');
256
- $_post_content = $_post_content_group['acfe_form_post_save_post_content'];
257
- $_post_content_custom = $_post_content_group['acfe_form_post_save_post_content_custom'];
258
-
259
- if($_post_content === 'custom')
260
- $_post_content = $_post_content_custom;
261
-
262
- $map = array();
263
-
264
- if($load_values){
265
-
266
- // Mapping
267
- $map = array(
268
- 'post_type' => get_sub_field('acfe_form_post_map_post_type'),
269
- 'post_status' => get_sub_field('acfe_form_post_map_post_status'),
270
- 'post_title' => get_sub_field('acfe_form_post_map_post_title'),
271
- 'post_name' => get_sub_field('acfe_form_post_map_post_name'),
272
- 'post_content' => get_sub_field('acfe_form_post_map_post_content'),
273
- 'post_author' => get_sub_field('acfe_form_post_map_post_author'),
274
- 'post_parent' => get_sub_field('acfe_form_post_map_post_parent'),
275
- 'post_terms' => get_sub_field('acfe_form_post_map_post_terms'),
276
- );
277
-
278
- }
279
-
280
- // Fields
281
- $fields = array(
282
- 'target' => get_sub_field('acfe_form_post_save_target'),
283
- 'post_type' => get_sub_field('acfe_form_post_save_post_type'),
284
- 'post_status' => get_sub_field('acfe_form_post_save_post_status'),
285
- 'post_title' => get_sub_field('acfe_form_post_save_post_title'),
286
- 'post_name' => get_sub_field('acfe_form_post_save_post_name'),
287
- 'post_content' => $_post_content,
288
- 'post_author' => get_sub_field('acfe_form_post_save_post_author'),
289
- 'post_parent' => get_sub_field('acfe_form_post_save_post_parent'),
290
- 'post_terms' => get_sub_field('acfe_form_post_save_post_terms'),
291
- );
292
-
293
- $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form);
294
-
295
- $_post_id = 0;
296
-
297
- // Insert Post
298
- if($post_action === 'insert_post'){
299
-
300
- $_post_id = wp_insert_post(array(
301
- 'post_title' => 'Post'
302
- ));
303
-
304
- }
305
-
306
- // Update Post
307
- elseif($post_action === 'update_post'){
308
-
309
- $_post_id = $data['target'];
310
-
311
- }
312
-
313
- // Invalid Post ID
314
- if(!$_post_id)
315
- return;
316
-
317
- $args = array();
318
-
319
- // ID
320
- $args['ID'] = $_post_id;
321
-
322
- // Post type
323
- if(!empty($data['post_type'])){
324
-
325
- if(is_array($data['post_type']))
326
- $data['post_type'] = acfe_array_to_string($data['post_type']);
327
-
328
- $args['post_type'] = $data['post_type'];
329
-
330
- }
331
-
332
- // Post status
333
- if(!empty($data['post_status'])){
334
-
335
- if(is_array($data['post_status']))
336
- $data['post_status'] = acfe_array_to_string($data['post_status']);
337
-
338
- $args['post_status'] = $data['post_status'];
339
-
340
- }
341
-
342
- // Post title
343
- if(!empty($data['post_title'])){
344
-
345
- if(is_array($data['post_title']))
346
- $data['post_title'] = acfe_array_to_string($data['post_title']);
347
-
348
- $args['post_title'] = $data['post_title'];
349
-
350
- if($data['post_title'] === 'generated_id')
351
- $args['post_title'] = $_post_id;
352
-
353
- }
354
-
355
- // Post name
356
- if(!empty($data['post_name'])){
357
-
358
- if(is_array($data['post_name']))
359
- $data['post_name'] = acfe_array_to_string($data['post_name']);
360
-
361
- $args['post_name'] = $data['post_name'];
362
-
363
- if($data['post_name'] === 'generated_id')
364
- $args['post_name'] = $_post_id;
365
-
366
- }
367
-
368
- // Post content
369
- if(!empty($data['post_content'])){
370
-
371
- if(is_array($data['post_content']))
372
- $data['post_content'] = acfe_array_to_string($data['post_content']);
373
-
374
- $args['post_content'] = $data['post_content'];
375
-
376
- }
377
-
378
- // Post author
379
- if(!empty($data['post_author'])){
380
-
381
- if(is_array($data['post_author']))
382
- $data['post_author'] = acfe_array_to_string($data['post_author']);
383
-
384
- $args['post_author'] = $data['post_author'];
385
-
386
- }
387
-
388
- // Post parent
389
- if(!empty($data['post_parent'])){
390
-
391
- if(is_array($data['post_author']))
392
- $data['post_author'] = acfe_array_to_string($data['post_author']);
393
-
394
- $args['post_parent'] = $data['post_parent'];
395
-
396
- }
397
-
398
- // Post terms
399
- if(!empty($data['post_terms'])){
400
-
401
- $terms = acf_array($data['post_terms']);
402
-
403
- // Tax input
404
- if(!empty($terms)){
405
-
406
- foreach($terms as $term){
407
-
408
- if(is_string($term) || is_numeric($term)){
409
-
410
- $args['acfe_form_terms'][] = $term;
411
-
412
- }elseif(is_array($term)){
413
-
414
- foreach($term as $sub_term){
415
-
416
- // String || Numeric
417
- if(is_string($sub_term) || is_numeric($sub_term)){
418
-
419
- $args['acfe_form_terms'][] = $sub_term;
420
-
421
- // Array
422
- }elseif(is_array($sub_term)){
423
-
424
- if(!acf_maybe_get($sub_term, 'term_id'))
425
- continue;
426
-
427
- $args['acfe_form_terms'][] = $sub_term['term_id'];
428
-
429
- // Object
430
- }elseif(is_object($sub_term) && is_a($sub_term, 'WP_Term')){
431
-
432
- if(!isset($sub_term->term_id) || empty($sub_term->term_id))
433
- continue;
434
-
435
- $args['acfe_form_terms'][] = $sub_term->term_id;
436
-
437
- }
438
-
439
-
440
- }
441
-
442
- }elseif(is_object($term) && is_a($term, 'WP_Term')){
443
-
444
- if(!isset($term->term_id) || empty($term->term_id))
445
- continue;
446
-
447
- $args['acfe_form_terms'][] = $term->term_id;
448
-
449
- }
450
-
451
-
452
-
453
- }
454
-
455
- }
456
-
457
- }
458
-
459
- // Args
460
- $args = apply_filters('acfe/form/submit/post_args', $args, $post_action, $form, $action);
461
- $args = apply_filters('acfe/form/submit/post_args/form=' . $form_name, $args, $post_action, $form, $action);
462
-
463
- if(!empty($action))
464
- $args = apply_filters('acfe/form/submit/post_args/action=' . $action, $args, $post_action, $form, $action);
465
-
466
- // Bail early if false
467
- if($args === false)
468
- return;
469
-
470
- // Post terms pre-process (let post update first, for post type)
471
- $terms = array();
472
-
473
- if(acf_maybe_get($args, 'acfe_form_terms')){
474
-
475
- $terms = acf_extract_var($args, 'acfe_form_terms');
476
-
477
- }
478
-
479
- // Update Post
480
- $_post_id = wp_update_post($args);
481
-
482
- // Post terms process
483
- if(!empty($terms)){
484
-
485
- $term_objects = array();
486
- $term_create = array();
487
-
488
- foreach($terms as $term){
489
-
490
- if(is_numeric($term)){
491
-
492
- $get_term = get_term($term);
493
-
494
- if(empty($get_term) || is_wp_error($get_term))
495
- continue;
496
-
497
- $term_objects[$get_term->taxonomy][] = $get_term->term_id;
498
-
499
- }elseif(is_string($term)){
500
-
501
- $explode = explode('|', $term);
502
-
503
- // No taxonomy found in input
504
- if(isset($explode[1])){
505
-
506
- $term_create[$explode[1]][] = $explode[0];
507
-
508
- }else{
509
-
510
- // Get post type
511
- $post_type = 'post';
512
-
513
- if(isset($args['post_type']))
514
- $post_type = $args['post_type'];
515
-
516
- $taxonomies = get_object_taxonomies($post_type);
517
-
518
- if(!empty($taxonomies)){
519
-
520
- $taxonomy = $taxonomies[0];
521
-
522
- $term_create[$taxonomy] = $explode[0];
523
-
524
- }
525
-
526
- }
527
-
528
- }
529
-
530
- }
531
-
532
- // Term Objects
533
- if(!empty($term_objects)){
534
-
535
- foreach($term_objects as $term_taxonomy => $term_ids){
536
-
537
- wp_set_object_terms($args['ID'], $term_ids, $term_taxonomy, true);
538
-
539
- }
540
-
541
- }
542
-
543
- // Create Terms (with slugs)
544
- if(!empty($term_create)){
545
-
546
- foreach($term_create as $term_taxonomy => $term_slugs){
547
-
548
- wp_set_object_terms($args['ID'], $term_slugs, $term_taxonomy, true);
549
-
550
- }
551
-
552
- }
553
-
554
- }
555
-
556
- // Submit
557
- do_action('acfe/form/submit/post', $_post_id, $post_action, $args, $form, $action);
558
- do_action('acfe/form/submit/post/form=' . $form_name, $_post_id, $post_action, $args, $form, $action);
559
-
560
- if(!empty($action))
561
- do_action('acfe/form/submit/post/action=' . $action, $_post_id, $post_action, $args, $form, $action);
562
-
563
- }
564
-
565
- function submit($_post_id, $post_action, $args, $form, $action){
566
-
567
- if(!empty($action)){
568
-
569
- // Custom Query Var
570
- $custom_query_var = get_sub_field('acfe_form_custom_query_var');
571
-
572
- if(!empty($custom_query_var)){
573
-
574
- // Form name
575
- $form_name = acf_maybe_get($form, 'form_name');
576
-
577
- // Get post array
578
- $post_object = get_post($_post_id, 'ARRAY_A');
579
-
580
- $post_object['permalink'] = get_permalink($_post_id);
581
- $post_object['admin_url'] = admin_url('post.php?post=' . $_post_id . '&action=edit');
582
-
583
- $post_object = apply_filters('acfe/form/query_var/post', $post_object, $_post_id, $post_action, $args, $form, $action);
584
- $post_object = apply_filters('acfe/form/query_var/post/form=' . $form_name, $post_object, $_post_id, $post_action, $args, $form, $action);
585
- $post_object = apply_filters('acfe/form/query_var/post/action=' . $action, $post_object, $_post_id, $post_action, $args, $form, $action);
586
-
587
- set_query_var($action, $post_object);
588
-
589
- }
590
-
591
- }
592
-
593
- // Meta save
594
- $save_meta = get_sub_field('acfe_form_post_save_meta');
595
-
596
- if(!empty($save_meta)){
597
-
598
- $meta = acfe_form_filter_meta($save_meta, $_POST['acf']);
599
-
600
- if(!empty($meta)){
601
-
602
- // Backup original acf post data
603
- $acf = $_POST['acf'];
604
-
605
- // Save meta fields
606
- acf_save_post($_post_id, $meta);
607
-
608
- // Restore original acf post data
609
- $_POST['acf'] = $acf;
610
-
611
- }
612
-
613
- }
614
-
615
- }
616
-
617
- /**
618
- * Post: Select2 Choices
619
- */
620
- function prepare_choices($field){
621
-
622
- $field['choices']['current_post'] = 'Current: Post';
623
- $field['choices']['current_post_parent'] = 'Current: Post Parent';
624
-
625
- if(acf_maybe_get($field, 'value')){
626
-
627
- $field_type = acf_get_field_type('post_object');
628
- $field['post_type'] = acf_get_post_types();
629
-
630
- // load posts
631
- $posts = $field_type->get_posts($field['value'], $field);
632
-
633
- if($posts){
634
-
635
- foreach(array_keys($posts) as $i){
636
-
637
- // vars
638
- $post = acf_extract_var($posts, $i);
639
-
640
- // append to choices
641
- $field['choices'][$post->ID] = $field_type->get_post_title($post, $field);
642
-
643
- }
644
-
645
- }
646
-
647
- }
648
-
649
- return $field;
650
-
651
- }
652
-
653
- /**
654
- * User: Select2 Choices
655
- */
656
- function prepare_choices_users($field){
657
-
658
- $field['choices']['current_user'] = 'Current User';
659
- $field['choices']['current_post_author'] = 'Current Post Author';
660
-
661
- if(acf_maybe_get($field, 'value')){
662
-
663
- $field_type = acf_get_field_type('user');
664
-
665
- // Clean value into an array of IDs.
666
- $user_ids = array_map('intval', acf_array($field['value']));
667
-
668
- // Find users in database (ensures all results are real).
669
- $users = acf_get_users(array(
670
- 'include' => $user_ids
671
- ));
672
-
673
- // Append.
674
- if($users){
675
-
676
- foreach($users as $user){
677
- $field['choices'][$user->ID] = $field_type->get_result($user, $field);
678
- }
679
-
680
- }
681
-
682
- }
683
-
684
- return $field;
685
-
686
- }
687
-
688
- function advanced_load($field){
689
-
690
- $form_name = 'my_form';
691
-
692
- if(acf_maybe_get($field, 'value'))
693
- $form_name = get_field('acfe_form_name', $field['value']);
694
-
695
- ?>You may use the following hooks:<br /><br />
696
- <pre data-codemirror="php-plain">
697
- add_filter('acfe/form/load/post_id', 'my_form_post_values_source', 10, 3);
698
- add_filter('acfe/form/load/post_id/form=<?php echo $form_name; ?>', 'my_form_post_values_source', 10, 3);
699
- add_filter('acfe/form/load/post_id/action=my-post-action', 'my_form_post_values_source', 10, 3);</pre>
700
- <br />
701
- <pre data-codemirror="php-plain">
702
- /**
703
- * @int $post_id Post ID used as source
704
- * @array $form The form settings
705
- * @string $action The action alias name
706
- */
707
- add_filter('acfe/form/load/post_id/form=<?php echo $form_name; ?>', 'my_form_post_values_source', 10, 3);
708
- function my_form_post_values_source($post_id, $form, $action){
709
-
710
- /**
711
- * Force to load values from the post ID 145
712
- */
713
- $post_id = 145;
714
-
715
-
716
- /**
717
- * Return
718
- */
719
- return $post_id;
720
-
721
- }</pre><?php
722
-
723
- }
724
-
725
- function advanced_save_args($field){
726
-
727
- $form_name = 'my_form';
728
-
729
- if(acf_maybe_get($field, 'value'))
730
-
731
- $form_name = get_field('acfe_form_name', $field['value']);
732
-
733
- ?>You may use the following hooks:<br /><br />
734
- <pre data-codemirror="php-plain">
735
- add_filter('acfe/form/submit/post_args', 'my_form_post_args', 10, 4);
736
- add_filter('acfe/form/submit/post_args/form=<?php echo $form_name; ?>', 'my_form_post_args', 10, 4);
737
- add_filter('acfe/form/submit/post_args/action=my-post-action', 'my_form_post_args', 10, 4);</pre>
738
- <br />
739
- <pre data-codemirror="php-plain">
740
- /**
741
- * @array $args The generated post arguments
742
- * @string $type Action type: 'insert_post' or 'update_post'
743
- * @array $form The form settings
744
- * @string $action The action alias name
745
- */
746
- add_filter('acfe/form/submit/post_args/form=<?php echo $form_name; ?>', 'my_form_post_args', 10, 4);
747
- function my_form_post_args($args, $type, $form, $action){
748
-
749
- /**
750
- * Force specific post title if the action type is 'insert_post'
751
- */
752
- if($type === 'insert_post'){
753
-
754
- $args['post_title'] = 'My title';
755
-
756
- }
757
-
758
-
759
- /**
760
- * Get the form input value named 'my_field'
761
- * This is the value entered by the user during the form submission
762
- */
763
- $my_field = get_field('my_field');
764
-
765
-
766
- /**
767
- * Get the field value 'my_field' from the post ID 145
768
- */
769
- $my_post_field = get_field('my_field', 145);
770
-
771
-
772
- /**
773
- * Return arguments
774
- * Note: Return false will stop post & meta insert/update
775
- */
776
- return $args;
777
-
778
- }</pre><?php
779
-
780
- }
781
-
782
- function advanced_save($field){
783
-
784
- $form_name = 'my_form';
785
-
786
- if(acf_maybe_get($field, 'value'))
787
- $form_name = get_field('acfe_form_name', $field['value']);
788
-
789
- ?>You may use the following hooks:<br /><br />
790
- <pre data-codemirror="php-plain">
791
- add_action('acfe/form/submit/post', 'my_form_post_save', 10, 5);
792
- add_action('acfe/form/submit/post/form=<?php echo $form_name; ?>', 'my_form_post_save', 10, 5);
793
- add_action('acfe/form/submit/post/action=my-post-action', 'my_form_post_save', 10, 5);</pre>
794
- <br />
795
- <pre data-codemirror="php-plain">
796
- /**
797
- * @int $post_id The targeted post ID
798
- * @string $type Action type: 'insert_post' or 'update_post'
799
- * @array $args The generated post arguments
800
- * @array $form The form settings
801
- * @string $action The action alias name
802
- *
803
- * Note: At this point the post & meta fields are already saved in the database
804
- */
805
- add_action('acfe/form/submit/post/form=<?php echo $form_name; ?>', 'my_form_post_save', 10, 5);
806
- function my_form_post_save($post_id, $type, $args, $form, $action){
807
-
808
- /**
809
- * Get the value from the form input named 'my_field'
810
- * This is the value entered by the user during the form submission
811
- */
812
- $my_field = get_field('my_field');
813
-
814
-
815
- /**
816
- * Get the field value 'my_field' from the currently saved post
817
- */
818
- $my_post_field = get_field('my_field', $post_id);
819
-
820
- }</pre><?php
821
-
822
- }
823
-
824
- }
825
-
826
- new acfe_form_post();
827
-
 
 
 
 
 
 
828
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ if(!class_exists('acfe_form_post')):
7
+
8
+ class acfe_form_post{
9
+
10
+ function __construct(){
11
+
12
+ /*
13
+ * Form
14
+ */
15
+ add_filter('acfe/form/load/post', array($this, 'load'), 1, 3);
16
+ add_action('acfe/form/prepare/post', array($this, 'prepare'), 1, 3);
17
+ add_action('acfe/form/submit/post', array($this, 'submit'), 1, 5);
18
+
19
+ /*
20
+ * Admin
21
+ */
22
+ add_filter('acf/prepare_field/name=acfe_form_post_save_meta', array(acfe()->acfe_form, 'map_fields'));
23
+ add_filter('acf/prepare_field/name=acfe_form_post_load_meta', array(acfe()->acfe_form, 'map_fields'));
24
+
25
+ add_filter('acf/prepare_field/name=acfe_form_post_save_target', array(acfe()->acfe_form, 'map_fields_deep'));
26
+ add_filter('acf/prepare_field/name=acfe_form_post_load_source', array(acfe()->acfe_form, 'map_fields_deep'));
27
+
28
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_type', array(acfe()->acfe_form, 'map_fields_deep'));
29
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_status', array(acfe()->acfe_form, 'map_fields_deep'));
30
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_title', array(acfe()->acfe_form, 'map_fields_deep'));
31
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_name', array(acfe()->acfe_form, 'map_fields_deep'));
32
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_content', array(acfe()->acfe_form, 'map_fields_deep'));
33
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_author', array(acfe()->acfe_form, 'map_fields_deep'));
34
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_parent', array(acfe()->acfe_form, 'map_fields_deep'));
35
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_terms', array(acfe()->acfe_form, 'map_fields_deep'));
36
+
37
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_type', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
38
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_status', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
39
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_title', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
40
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
41
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_content', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
42
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_author', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
43
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_parent', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
44
+ add_filter('acf/prepare_field/name=acfe_form_post_map_post_terms', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
45
+
46
+ add_filter('acf/prepare_field/name=acfe_form_post_save_target', array($this, 'prepare_choices'), 5);
47
+ add_filter('acf/prepare_field/name=acfe_form_post_load_source', array($this, 'prepare_choices'), 5);
48
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_parent', array($this, 'prepare_choices'), 5);
49
+ add_filter('acf/prepare_field/name=acfe_form_post_save_post_author', array($this, 'prepare_choices_users'), 5);
50
+
51
+
52
+ add_action('acf/render_field/name=acfe_form_post_advanced_load', array($this, 'advanced_load'));
53
+ add_action('acf/render_field/name=acfe_form_post_advanced_save_args', array($this, 'advanced_save_args'));
54
+ add_action('acf/render_field/name=acfe_form_post_advanced_save', array($this, 'advanced_save'));
55
+
56
+ }
57
+
58
+ function load($form, $current_post_id, $action){
59
+
60
+ // Form
61
+ $form_name = acf_maybe_get($form, 'form_name');
62
+ $form_id = acf_maybe_get($form, 'form_id');
63
+
64
+ // Action
65
+ $post_action = get_sub_field('acfe_form_post_action');
66
+
67
+ // Load values
68
+ $load_values = get_sub_field('acfe_form_post_load_values');
69
+ $load_meta = get_sub_field('acfe_form_post_load_meta');
70
+
71
+ // Load values
72
+ if(!$load_values)
73
+ return $form;
74
+
75
+ $_post_id = get_sub_field('acfe_form_post_load_source');
76
+ $_post_type = get_sub_field('acfe_form_post_map_post_type');
77
+ $_post_status = get_sub_field('acfe_form_post_map_post_status');
78
+ $_post_title = get_sub_field('acfe_form_post_map_post_title');
79
+ $_post_name = get_sub_field('acfe_form_post_map_post_name');
80
+ $_post_content = get_sub_field('acfe_form_post_map_post_content');
81
+ $_post_author = get_sub_field('acfe_form_post_map_post_author');
82
+ $_post_parent = get_sub_field('acfe_form_post_map_post_parent');
83
+ $_post_terms = get_sub_field('acfe_form_post_map_post_terms');
84
+
85
+ // Map {field:name} {get_field:name} {query_var:name}
86
+ $_post_id = acfe_form_map_field_value_load($_post_id, $current_post_id, $form);
87
+ $_post_type = acfe_form_map_field_value_load($_post_type, $current_post_id, $form);
88
+ $_post_status = acfe_form_map_field_value_load($_post_status, $current_post_id, $form);
89
+ $_post_title = acfe_form_map_field_value_load($_post_title, $current_post_id, $form);
90
+ $_post_name = acfe_form_map_field_value_load($_post_name, $current_post_id, $form);
91
+ $_post_content = acfe_form_map_field_value_load($_post_content, $current_post_id, $form);
92
+ $_post_author = acfe_form_map_field_value_load($_post_author, $current_post_id, $form);
93
+ $_post_parent = acfe_form_map_field_value_load($_post_parent, $current_post_id, $form);
94
+ $_post_terms = acfe_form_map_field_value_load($_post_terms, $current_post_id, $form);
95
+
96
+ // Filters
97
+ $_post_id = apply_filters('acfe/form/load/post_id', $_post_id, $form, $action);
98
+ $_post_id = apply_filters('acfe/form/load/post_id/form=' . $form_name, $_post_id, $form, $action);
99
+
100
+ if(!empty($action))
101
+ $_post_id = apply_filters('acfe/form/load/post_id/action=' . $action, $_post_id, $form, $action);
102
+
103
+ // Invalid Post ID
104
+ if(!$_post_id)
105
+ return $form;
106
+
107
+ // Post type
108
+ if(acf_is_field_key($_post_type)){
109
+
110
+ $key = array_search($_post_type, $load_meta);
111
+
112
+ if($key !== false)
113
+ unset($load_meta[$key]);
114
+
115
+ $form['map'][$_post_type]['value'] = get_post_field('post_type', $_post_id);
116
+
117
+ }
118
+
119
+ // Post status
120
+ if(acf_is_field_key($_post_status)){
121
+
122
+ $key = array_search($_post_status, $load_meta);
123
+
124
+ if($key !== false)
125
+ unset($load_meta[$key]);
126
+
127
+ $form['map'][$_post_status]['value'] = get_post_field('post_status', $_post_id);
128
+
129
+ }
130
+
131
+ // Post title
132
+ if(acf_is_field_key($_post_title)){
133
+
134
+ $key = array_search($_post_title, $load_meta);
135
+
136
+ if($key !== false)
137
+ unset($load_meta[$key]);
138
+
139
+ $form['map'][$_post_title]['value'] = get_post_field('post_title', $_post_id);
140
+
141
+ }
142
+
143
+ // Post name
144
+ if(acf_is_field_key($_post_name)){
145
+
146
+ $key = array_search($_post_name, $load_meta);
147
+
148
+ if($key !== false)
149
+ unset($load_meta[$key]);
150
+
151
+ $form['map'][$_post_name]['value'] = get_post_field('post_name', $_post_id);
152
+
153
+ }
154
+
155
+ // Post content
156
+ if(acf_is_field_key($_post_content)){
157
+
158
+ $key = array_search($_post_content, $load_meta);
159
+
160
+ if($key !== false)
161
+ unset($load_meta[$key]);
162
+
163
+ $form['map'][$_post_content]['value'] = get_post_field('post_content', $_post_id);
164
+
165
+ }
166
+
167
+ // Post author
168
+ if(acf_is_field_key($_post_author)){
169
+
170
+ $key = array_search($_post_author, $load_meta);
171
+
172
+ if($key !== false)
173
+ unset($load_meta[$key]);
174
+
175
+ $form['map'][$_post_author]['value'] = get_post_field('post_author', $_post_id);
176
+
177
+ }
178
+
179
+ // Post parent
180
+ if(acf_is_field_key($_post_parent)){
181
+
182
+ $key = array_search($_post_parent, $load_meta);
183
+
184
+ if($key !== false)
185
+ unset($load_meta[$key]);
186
+
187
+ $form['map'][$_post_parent]['value'] = get_post_field('post_parent', $_post_id);
188
+
189
+ }
190
+
191
+ // Post terms
192
+ if(acf_is_field_key($_post_terms)){
193
+
194
+ $key = array_search($_post_terms, $load_meta);
195
+
196
+ if($key !== false)
197
+ unset($load_meta[$key]);
198
+
199
+ $taxonomies = acf_get_taxonomies(array(
200
+ 'post_type' => get_post_type($_post_id)
201
+ ));
202
+
203
+ if(!empty($taxonomies)){
204
+
205
+ $terms = array();
206
+
207
+ foreach($taxonomies as $taxonomy){
208
+
209
+ $get_the_terms = get_the_terms($_post_id, $taxonomy);
210
+ if(!$get_the_terms || is_wp_error($get_the_terms))
211
+ continue;
212
+
213
+ $terms = array_merge($terms, $get_the_terms);
214
+
215
+ }
216
+
217
+ $return = wp_list_pluck($terms, 'term_id');
218
+
219
+ $form['map'][$_post_terms]['value'] = $return;
220
+
221
+ }
222
+
223
+ }
224
+
225
+ // Load others values
226
+ if(!empty($load_meta)){
227
+
228
+ foreach($load_meta as $field_key){
229
+
230
+ $field = acf_get_field($field_key);
231
+
232
+ $form['map'][$field_key]['value'] = acf_get_value($_post_id, $field);
233
+
234
+ }
235
+
236
+ }
237
+
238
+ return $form;
239
+
240
+ }
241
+
242
+ function prepare($form, $current_post_id, $action){
243
+
244
+ // Form
245
+ $form_name = acf_maybe_get($form, 'form_name');
246
+ $form_id = acf_maybe_get($form, 'form_id');
247
+
248
+ // Action
249
+ $post_action = get_sub_field('acfe_form_post_action');
250
+
251
+ // Load values
252
+ $load_values = get_sub_field('acfe_form_post_load_values');
253
+
254
+ // Pre-process
255
+ $_post_content_group = get_sub_field('acfe_form_post_save_post_content_group');
256
+ $_post_content = $_post_content_group['acfe_form_post_save_post_content'];
257
+ $_post_content_custom = $_post_content_group['acfe_form_post_save_post_content_custom'];
258
+
259
+ if($_post_content === 'custom')
260
+ $_post_content = $_post_content_custom;
261
+
262
+ $map = array();
263
+
264
+ if($load_values){
265
+
266
+ // Mapping
267
+ $map = array(
268
+ 'post_type' => get_sub_field('acfe_form_post_map_post_type'),
269
+ 'post_status' => get_sub_field('acfe_form_post_map_post_status'),
270
+ 'post_title' => get_sub_field('acfe_form_post_map_post_title'),
271
+ 'post_name' => get_sub_field('acfe_form_post_map_post_name'),
272
+ 'post_content' => get_sub_field('acfe_form_post_map_post_content'),
273
+ 'post_author' => get_sub_field('acfe_form_post_map_post_author'),
274
+ 'post_parent' => get_sub_field('acfe_form_post_map_post_parent'),
275
+ 'post_terms' => get_sub_field('acfe_form_post_map_post_terms'),
276
+ );
277
+
278
+ }
279
+
280
+ // Fields
281
+ $fields = array(
282
+ 'target' => get_sub_field('acfe_form_post_save_target'),
283
+ 'post_type' => get_sub_field('acfe_form_post_save_post_type'),
284
+ 'post_status' => get_sub_field('acfe_form_post_save_post_status'),
285
+ 'post_title' => get_sub_field('acfe_form_post_save_post_title'),
286
+ 'post_name' => get_sub_field('acfe_form_post_save_post_name'),
287
+ 'post_content' => $_post_content,
288
+ 'post_author' => get_sub_field('acfe_form_post_save_post_author'),
289
+ 'post_parent' => get_sub_field('acfe_form_post_save_post_parent'),
290
+ 'post_terms' => get_sub_field('acfe_form_post_save_post_terms'),
291
+ );
292
+
293
+ $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form);
294
+
295
+ $_post_id = 0;
296
+
297
+ // Insert Post
298
+ if($post_action === 'insert_post'){
299
+
300
+ // Fix nasty Elementor + YOAST infinite loop
301
+ // Elementor bug report: https://github.com/elementor/elementor/issues/10998
302
+ // YOAST bug report: https://github.com/Yoast/wordpress-seo/issues/14643
303
+
304
+ add_filter('wpseo_should_index_links', '__return_false');
305
+
306
+ $_post_id = wp_insert_post(array(
307
+ 'post_title' => 'Post'
308
+ ));
309
+
310
+ }
311
+
312
+ // Update Post
313
+ elseif($post_action === 'update_post'){
314
+
315
+ $_post_id = $data['target'];
316
+
317
+ }
318
+
319
+ // Invalid Post ID
320
+ if(!$_post_id)
321
+ return;
322
+
323
+ $args = array();
324
+
325
+ // ID
326
+ $args['ID'] = $_post_id;
327
+
328
+ // Post type
329
+ if(!empty($data['post_type'])){
330
+
331
+ if(is_array($data['post_type']))
332
+ $data['post_type'] = acfe_array_to_string($data['post_type']);
333
+
334
+ $args['post_type'] = $data['post_type'];
335
+
336
+ }
337
+
338
+ // Post status
339
+ if(!empty($data['post_status'])){
340
+
341
+ if(is_array($data['post_status']))
342
+ $data['post_status'] = acfe_array_to_string($data['post_status']);
343
+
344
+ $args['post_status'] = $data['post_status'];
345
+
346
+ }
347
+
348
+ // Post title
349
+ if(!empty($data['post_title'])){
350
+
351
+ if(is_array($data['post_title']))
352
+ $data['post_title'] = acfe_array_to_string($data['post_title']);
353
+
354
+ $args['post_title'] = $data['post_title'];
355
+
356
+ if($data['post_title'] === 'generated_id')
357
+ $args['post_title'] = $_post_id;
358
+
359
+ }
360
+
361
+ // Post name
362
+ if(!empty($data['post_name'])){
363
+
364
+ if(is_array($data['post_name']))
365
+ $data['post_name'] = acfe_array_to_string($data['post_name']);
366
+
367
+ $args['post_name'] = $data['post_name'];
368
+
369
+ if($data['post_name'] === 'generated_id')
370
+ $args['post_name'] = $_post_id;
371
+
372
+ }
373
+
374
+ // Post content
375
+ if(!empty($data['post_content'])){
376
+
377
+ if(is_array($data['post_content']))
378
+ $data['post_content'] = acfe_array_to_string($data['post_content']);
379
+
380
+ $args['post_content'] = $data['post_content'];
381
+
382
+ }
383
+
384
+ // Post author
385
+ if(!empty($data['post_author'])){
386
+
387
+ if(is_array($data['post_author']))
388
+ $data['post_author'] = acfe_array_to_string($data['post_author']);
389
+
390
+ $args['post_author'] = $data['post_author'];
391
+
392
+ }
393
+
394
+ // Post parent
395
+ if(!empty($data['post_parent'])){
396
+
397
+ if(is_array($data['post_author']))
398
+ $data['post_author'] = acfe_array_to_string($data['post_author']);
399
+
400
+ $args['post_parent'] = $data['post_parent'];
401
+
402
+ }
403
+
404
+ // Post terms
405
+ if(!empty($data['post_terms'])){
406
+
407
+ $terms = acf_array($data['post_terms']);
408
+
409
+ // Tax input
410
+ if(!empty($terms)){
411
+
412
+ foreach($terms as $term){
413
+
414
+ if(is_string($term) || is_numeric($term)){
415
+
416
+ $args['acfe_form_terms'][] = $term;
417
+
418
+ }elseif(is_array($term)){
419
+
420
+ foreach($term as $sub_term){
421
+
422
+ // String || Numeric
423
+ if(is_string($sub_term) || is_numeric($sub_term)){
424
+
425
+ $args['acfe_form_terms'][] = $sub_term;
426
+
427
+ // Array
428
+ }elseif(is_array($sub_term)){
429
+
430
+ if(!acf_maybe_get($sub_term, 'term_id'))
431
+ continue;
432
+
433
+ $args['acfe_form_terms'][] = $sub_term['term_id'];
434
+
435
+ // Object
436
+ }elseif(is_object($sub_term) && is_a($sub_term, 'WP_Term')){
437
+
438
+ if(!isset($sub_term->term_id) || empty($sub_term->term_id))
439
+ continue;
440
+
441
+ $args['acfe_form_terms'][] = $sub_term->term_id;
442
+
443
+ }
444
+
445
+
446
+ }
447
+
448
+ }elseif(is_object($term) && is_a($term, 'WP_Term')){
449
+
450
+ if(!isset($term->term_id) || empty($term->term_id))
451
+ continue;
452
+
453
+ $args['acfe_form_terms'][] = $term->term_id;
454
+
455
+ }
456
+
457
+
458
+
459
+ }
460
+
461
+ }
462
+
463
+ }
464
+
465
+ // Args
466
+ $args = apply_filters('acfe/form/submit/post_args', $args, $post_action, $form, $action);
467
+ $args = apply_filters('acfe/form/submit/post_args/form=' . $form_name, $args, $post_action, $form, $action);
468
+
469
+ if(!empty($action))
470
+ $args = apply_filters('acfe/form/submit/post_args/action=' . $action, $args, $post_action, $form, $action);
471
+
472
+ // Bail early if false
473
+ if($args === false)
474
+ return;
475
+
476
+ // Post terms pre-process (let post update first, for post type)
477
+ $terms = array();
478
+
479
+ if(acf_maybe_get($args, 'acfe_form_terms')){
480
+
481
+ $terms = acf_extract_var($args, 'acfe_form_terms');
482
+
483
+ }
484
+
485
+ // Update Post
486
+ $_post_id = wp_update_post($args);
487
+
488
+ // Post terms process
489
+ if(!empty($terms)){
490
+
491
+ $term_objects = array();
492
+ $term_create = array();
493
+
494
+ foreach($terms as $term){
495
+
496
+ if(is_numeric($term)){
497
+
498
+ $get_term = get_term($term);
499
+
500
+ if(empty($get_term) || is_wp_error($get_term))
501
+ continue;
502
+
503
+ $term_objects[$get_term->taxonomy][] = $get_term->term_id;
504
+
505
+ }elseif(is_string($term)){
506
+
507
+ $explode = explode('|', $term);
508
+
509
+ // No taxonomy found in input
510
+ if(isset($explode[1])){
511
+
512
+ $term_create[$explode[1]][] = $explode[0];
513
+
514
+ }else{
515
+
516
+ // Get post type
517
+ $post_type = 'post';
518
+
519
+ if(isset($args['post_type']))
520
+ $post_type = $args['post_type'];
521
+
522
+ $taxonomies = get_object_taxonomies($post_type);
523
+
524
+ if(!empty($taxonomies)){
525
+
526
+ $taxonomy = $taxonomies[0];
527
+
528
+ $term_create[$taxonomy] = $explode[0];
529
+
530
+ }
531
+
532
+ }
533
+
534
+ }
535
+
536
+ }
537
+
538
+ // Term Objects
539
+ if(!empty($term_objects)){
540
+
541
+ foreach($term_objects as $term_taxonomy => $term_ids){
542
+
543
+ wp_set_object_terms($args['ID'], $term_ids, $term_taxonomy, true);
544
+
545
+ }
546
+
547
+ }
548
+
549
+ // Create Terms (with slugs)
550
+ if(!empty($term_create)){
551
+
552
+ foreach($term_create as $term_taxonomy => $term_slugs){
553
+
554
+ wp_set_object_terms($args['ID'], $term_slugs, $term_taxonomy, true);
555
+
556
+ }
557
+
558
+ }
559
+
560
+ }
561
+
562
+ // Submit
563
+ do_action('acfe/form/submit/post', $_post_id, $post_action, $args, $form, $action);
564
+ do_action('acfe/form/submit/post/form=' . $form_name, $_post_id, $post_action, $args, $form, $action);
565
+
566
+ if(!empty($action))
567
+ do_action('acfe/form/submit/post/action=' . $action, $_post_id, $post_action, $args, $form, $action);
568
+
569
+ }
570
+
571
+ function submit($_post_id, $post_action, $args, $form, $action){
572
+
573
+ if(!empty($action)){
574
+
575
+ // Custom Query Var
576
+ $custom_query_var = get_sub_field('acfe_form_custom_query_var');
577
+
578
+ if(!empty($custom_query_var)){
579
+
580
+ // Form name
581
+ $form_name = acf_maybe_get($form, 'form_name');
582
+
583
+ // Get post array
584
+ $post_object = get_post($_post_id, 'ARRAY_A');
585
+
586
+ $post_object['permalink'] = get_permalink($_post_id);
587
+ $post_object['admin_url'] = admin_url('post.php?post=' . $_post_id . '&action=edit');
588
+
589
+ $post_object = apply_filters('acfe/form/query_var/post', $post_object, $_post_id, $post_action, $args, $form, $action);
590
+ $post_object = apply_filters('acfe/form/query_var/post/form=' . $form_name, $post_object, $_post_id, $post_action, $args, $form, $action);
591
+ $post_object = apply_filters('acfe/form/query_var/post/action=' . $action, $post_object, $_post_id, $post_action, $args, $form, $action);
592
+
593
+ set_query_var($action, $post_object);
594
+
595
+ }
596
+
597
+ }
598
+
599
+ // Meta save
600
+ $save_meta = get_sub_field('acfe_form_post_save_meta');
601
+
602
+ if(!empty($save_meta)){
603
+
604
+ $meta = acfe_form_filter_meta($save_meta, $_POST['acf']);
605
+
606
+ if(!empty($meta)){
607
+
608
+ // Backup original acf post data
609
+ $acf = $_POST['acf'];
610
+
611
+ // Save meta fields
612
+ acf_save_post($_post_id, $meta);
613
+
614
+ // Restore original acf post data
615
+ $_POST['acf'] = $acf;
616
+
617
+ }
618
+
619
+ }
620
+
621
+ }
622
+
623
+ /**
624
+ * Post: Select2 Choices
625
+ */
626
+ function prepare_choices($field){
627
+
628
+ $field['choices']['current_post'] = 'Current: Post';
629
+ $field['choices']['current_post_parent'] = 'Current: Post Parent';
630
+
631
+ if(acf_maybe_get($field, 'value')){
632
+
633
+ $field_type = acf_get_field_type('post_object');
634
+ $field['post_type'] = acf_get_post_types();
635
+
636
+ // load posts
637
+ $posts = $field_type->get_posts($field['value'], $field);
638
+
639
+ if($posts){
640
+
641
+ foreach(array_keys($posts) as $i){
642
+
643
+ // vars
644
+ $post = acf_extract_var($posts, $i);
645
+
646
+ // append to choices
647
+ $field['choices'][$post->ID] = $field_type->get_post_title($post, $field);
648
+
649
+ }
650
+
651
+ }
652
+
653
+ }
654
+
655
+ return $field;
656
+
657
+ }
658
+
659
+ /**
660
+ * User: Select2 Choices
661
+ */
662
+ function prepare_choices_users($field){
663
+
664
+ $field['choices']['current_user'] = 'Current User';
665
+ $field['choices']['current_post_author'] = 'Current Post Author';
666
+
667
+ if(acf_maybe_get($field, 'value')){
668
+
669
+ $field_type = acf_get_field_type('user');
670
+
671
+ // Clean value into an array of IDs.
672
+ $user_ids = array_map('intval', acf_array($field['value']));
673
+
674
+ // Find users in database (ensures all results are real).
675
+ $users = acf_get_users(array(
676
+ 'include' => $user_ids
677
+ ));
678
+
679
+ // Append.
680
+ if($users){
681
+
682
+ foreach($users as $user){
683
+ $field['choices'][$user->ID] = $field_type->get_result($user, $field);
684
+ }
685
+
686
+ }
687
+
688
+ }
689
+
690
+ return $field;
691
+
692
+ }
693
+
694
+ function advanced_load($field){
695
+
696
+ $form_name = 'my_form';
697
+
698
+ if(acf_maybe_get($field, 'value'))
699
+ $form_name = get_field('acfe_form_name', $field['value']);
700
+
701
+ ?>You may use the following hooks:<br /><br />
702
+ <pre data-codemirror="php-plain">
703
+ add_filter('acfe/form/load/post_id', 'my_form_post_values_source', 10, 3);
704
+ add_filter('acfe/form/load/post_id/form=<?php echo $form_name; ?>', 'my_form_post_values_source', 10, 3);
705
+ add_filter('acfe/form/load/post_id/action=my-post-action', 'my_form_post_values_source', 10, 3);</pre>
706
+ <br />
707
+ <pre data-codemirror="php-plain">
708
+ /**
709
+ * @int $post_id Post ID used as source
710
+ * @array $form The form settings
711
+ * @string $action The action alias name
712
+ */
713
+ add_filter('acfe/form/load/post_id/form=<?php echo $form_name; ?>', 'my_form_post_values_source', 10, 3);
714
+ function my_form_post_values_source($post_id, $form, $action){
715
+
716
+ /**
717
+ * Force to load values from the post ID 145
718
+ */
719
+ $post_id = 145;
720
+
721
+
722
+ /**
723
+ * Return
724
+ */
725
+ return $post_id;
726
+
727
+ }</pre><?php
728
+
729
+ }
730
+
731
+ function advanced_save_args($field){
732
+
733
+ $form_name = 'my_form';
734
+
735
+ if(acf_maybe_get($field, 'value'))
736
+
737
+ $form_name = get_field('acfe_form_name', $field['value']);
738
+
739
+ ?>You may use the following hooks:<br /><br />
740
+ <pre data-codemirror="php-plain">
741
+ add_filter('acfe/form/submit/post_args', 'my_form_post_args', 10, 4);
742
+ add_filter('acfe/form/submit/post_args/form=<?php echo $form_name; ?>', 'my_form_post_args', 10, 4);
743
+ add_filter('acfe/form/submit/post_args/action=my-post-action', 'my_form_post_args', 10, 4);</pre>
744
+ <br />
745
+ <pre data-codemirror="php-plain">
746
+ /**
747
+ * @array $args The generated post arguments
748
+ * @string $type Action type: 'insert_post' or 'update_post'
749
+ * @array $form The form settings
750
+ * @string $action The action alias name
751
+ */
752
+ add_filter('acfe/form/submit/post_args/form=<?php echo $form_name; ?>', 'my_form_post_args', 10, 4);
753
+ function my_form_post_args($args, $type, $form, $action){
754
+
755
+ /**
756
+ * Force specific post title if the action type is 'insert_post'
757
+ */
758
+ if($type === 'insert_post'){
759
+
760
+ $args['post_title'] = 'My title';
761
+
762
+ }
763
+
764
+
765
+ /**
766
+ * Get the form input value named 'my_field'
767
+ * This is the value entered by the user during the form submission
768
+ */
769
+ $my_field = get_field('my_field');
770
+
771
+
772
+ /**
773
+ * Get the field value 'my_field' from the post ID 145
774
+ */
775
+ $my_post_field = get_field('my_field', 145);
776
+
777
+
778
+ /**
779
+ * Return arguments
780
+ * Note: Return false will stop post & meta insert/update
781
+ */
782
+ return $args;
783
+
784
+ }</pre><?php
785
+
786
+ }
787
+
788
+ function advanced_save($field){
789
+
790
+ $form_name = 'my_form';
791
+
792
+ if(acf_maybe_get($field, 'value'))
793
+ $form_name = get_field('acfe_form_name', $field['value']);
794
+
795
+ ?>You may use the following hooks:<br /><br />
796
+ <pre data-codemirror="php-plain">
797
+ add_action('acfe/form/submit/post', 'my_form_post_save', 10, 5);
798
+ add_action('acfe/form/submit/post/form=<?php echo $form_name; ?>', 'my_form_post_save', 10, 5);
799
+ add_action('acfe/form/submit/post/action=my-post-action', 'my_form_post_save', 10, 5);</pre>
800
+ <br />
801
+ <pre data-codemirror="php-plain">
802
+ /**
803
+ * @int $post_id The targeted post ID
804
+ * @string $type Action type: 'insert_post' or 'update_post'
805
+ * @array $args The generated post arguments
806
+ * @array $form The form settings
807
+ * @string $action The action alias name
808
+ *
809
+ * Note: At this point the post & meta fields are already saved in the database
810
+ */
811
+ add_action('acfe/form/submit/post/form=<?php echo $form_name; ?>', 'my_form_post_save', 10, 5);
812
+ function my_form_post_save($post_id, $type, $args, $form, $action){
813
+
814
+ /**
815
+ * Get the value from the form input named 'my_field'
816
+ * This is the value entered by the user during the form submission
817
+ */
818
+ $my_field = get_field('my_field');
819
+
820
+
821
+ /**
822
+ * Get the field value 'my_field' from the currently saved post
823
+ */
824
+ $my_post_field = get_field('my_field', $post_id);
825
+
826
+ }</pre><?php
827
+
828
+ }
829
+
830
+ }
831
+
832
+ new acfe_form_post();
833
+
834
  endif;
includes/modules/form/actions/term.php CHANGED
@@ -1,562 +1,562 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- if(!class_exists('acfe_form_term')):
7
-
8
- class acfe_form_term{
9
-
10
- function __construct(){
11
-
12
- /*
13
- * Form
14
- */
15
- add_filter('acfe/form/load/term', array($this, 'load'), 1, 3);
16
- add_action('acfe/form/prepare/term', array($this, 'prepare'), 1, 3);
17
- add_action('acfe/form/submit/term', array($this, 'submit'), 1, 5);
18
-
19
- /*
20
- * Admin
21
- */
22
- add_filter('acf/prepare_field/name=acfe_form_term_save_meta', array(acfe()->acfe_form, 'map_fields'));
23
- add_filter('acf/prepare_field/name=acfe_form_term_load_meta', array(acfe()->acfe_form, 'map_fields'));
24
-
25
- add_filter('acf/prepare_field/name=acfe_form_term_save_target', array(acfe()->acfe_form, 'map_fields_deep'));
26
- add_filter('acf/prepare_field/name=acfe_form_term_load_source', array(acfe()->acfe_form, 'map_fields_deep'));
27
-
28
- add_filter('acf/prepare_field/name=acfe_form_term_save_name', array(acfe()->acfe_form, 'map_fields_deep'));
29
- add_filter('acf/prepare_field/name=acfe_form_term_save_slug', array(acfe()->acfe_form, 'map_fields_deep'));
30
- add_filter('acf/prepare_field/name=acfe_form_term_save_taxonomy', array(acfe()->acfe_form, 'map_fields_deep'));
31
- add_filter('acf/prepare_field/name=acfe_form_term_save_parent', array(acfe()->acfe_form, 'map_fields_deep'));
32
- add_filter('acf/prepare_field/name=acfe_form_term_save_description', array(acfe()->acfe_form, 'map_fields_deep'));
33
-
34
- add_filter('acf/prepare_field/name=acfe_form_term_map_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
35
- add_filter('acf/prepare_field/name=acfe_form_term_map_slug', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
36
- add_filter('acf/prepare_field/name=acfe_form_term_map_taxonomy', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
37
- add_filter('acf/prepare_field/name=acfe_form_term_map_parent', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
38
- add_filter('acf/prepare_field/name=acfe_form_term_map_description', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
39
-
40
- add_filter('acf/prepare_field/name=acfe_form_term_save_target', array($this, 'prepare_choices'), 5);
41
- add_filter('acf/prepare_field/name=acfe_form_term_load_source', array($this, 'prepare_choices'), 5);
42
- add_filter('acf/prepare_field/name=acfe_form_term_save_parent', array($this, 'prepare_choices'), 5);
43
-
44
- add_action('acf/render_field/name=acfe_form_term_advanced_load', array($this, 'advanced_load'));
45
- add_action('acf/render_field/name=acfe_form_term_advanced_save_args', array($this, 'advanced_save_args'));
46
- add_action('acf/render_field/name=acfe_form_term_advanced_save', array($this, 'advanced_save'));
47
-
48
- }
49
-
50
- function load($form, $current_post_id, $action){
51
-
52
- // Form
53
- $form_name = acf_maybe_get($form, 'form_name');
54
- $form_id = acf_maybe_get($form, 'form_id');
55
-
56
- // Action
57
- $term_action = get_sub_field('acfe_form_term_action');
58
-
59
- // Load values
60
- $load_values = get_sub_field('acfe_form_term_load_values');
61
- $load_meta = get_sub_field('acfe_form_term_load_meta');
62
-
63
- // Load values
64
- if(!$load_values)
65
- return $form;
66
-
67
- $_term_id = get_sub_field('acfe_form_term_load_source');
68
- $_name = get_sub_field('acfe_form_term_map_name');
69
- $_slug = get_sub_field('acfe_form_term_map_slug');
70
- $_taxonomy = get_sub_field('acfe_form_term_map_taxonomy');
71
- $_parent = get_sub_field('acfe_form_term_map_parent');
72
- $_description = get_sub_field('acfe_form_term_map_description');
73
-
74
- // Map {field:name} {get_field:name} {query_var:name}
75
- $_term_id = acfe_form_map_field_value_load($_term_id, $current_post_id, $form);
76
- $_name = acfe_form_map_field_value_load($_name, $current_post_id, $form);
77
- $_slug = acfe_form_map_field_value_load($_slug, $current_post_id, $form);
78
- $_taxonomy = acfe_form_map_field_value_load($_taxonomy, $current_post_id, $form);
79
- $_parent = acfe_form_map_field_value_load($_parent, $current_post_id, $form);
80
- $_description = acfe_form_map_field_value_load($_description, $current_post_id, $form);
81
-
82
- $_term_id = apply_filters('acfe/form/load/term_id', $_term_id, $form, $action);
83
- $_term_id = apply_filters('acfe/form/load/term_id/form=' . $form_name, $_term_id, $form, $action);
84
-
85
- if(!empty($action))
86
- $_term_id = apply_filters('acfe/form/load/term_id/action=' . $action, $_term_id, $form, $action);
87
-
88
- // Invalid Term ID
89
- if(!$_term_id)
90
- return $form;
91
-
92
- // Name
93
- if(acf_is_field_key($_name)){
94
-
95
- $key = array_search($_name, $load_meta);
96
-
97
- if($key !== false)
98
- unset($load_meta[$key]);
99
-
100
- $form['map'][$_name]['value'] = get_term_field('name', $_term_id);
101
-
102
- }
103
-
104
- // Slug
105
- if(acf_is_field_key($_slug)){
106
-
107
- $key = array_search($_slug, $load_meta);
108
-
109
- if($key !== false)
110
- unset($load_meta[$key]);
111
-
112
- $form['map'][$_slug]['value'] = get_term_field('slug', $_term_id);
113
-
114
- }
115
-
116
- // Taxonomy
117
- if(acf_is_field_key($_taxonomy)){
118
-
119
- $key = array_search($_taxonomy, $load_meta);
120
-
121
- if($key !== false)
122
- unset($load_meta[$key]);
123
-
124
- $form['map'][$_taxonomy]['value'] = get_term_field('taxonomy', $_term_id);
125
-
126
- }
127
-
128
- // Parent
129
- if(acf_is_field_key($_parent)){
130
-
131
- $key = array_search($_parent, $load_meta);
132
-
133
- if($key !== false)
134
- unset($load_meta[$key]);
135
-
136
- $form['map'][$_parent]['value'] = get_term_field('parent', $_term_id);
137
-
138
- }
139
-
140
- // Description
141
- if(acf_is_field_key($_description)){
142
-
143
- $key = array_search($_description, $load_meta);
144
-
145
- if($key !== false)
146
- unset($load_meta[$key]);
147
-
148
- $form['map'][$_description]['value'] = get_term_field('description', $_term_id);
149
-
150
- }
151
-
152
- // Load others values
153
- if(!empty($load_meta)){
154
-
155
- foreach($load_meta as $field_key){
156
-
157
- $field = acf_get_field($field_key);
158
-
159
- $form['map'][$field_key]['value'] = acf_get_value('term_' . $_term_id, $field);
160
-
161
- }
162
-
163
- }
164
-
165
- return $form;
166
-
167
- }
168
-
169
- function prepare($form, $current_post_id, $action){
170
-
171
- // Form
172
- $form_name = acf_maybe_get($form, 'form_name');
173
- $form_id = acf_maybe_get($form, 'form_id');
174
-
175
- // Action
176
- $term_action = get_sub_field('acfe_form_term_action');
177
-
178
- // Load values
179
- $load_values = get_sub_field('acfe_form_term_load_values');
180
-
181
- // Pre-process
182
- $_description_group = get_sub_field('acfe_form_term_save_description_group');
183
- $_description = $_description_group['acfe_form_term_save_description'];
184
- $_description_custom = $_description_group['acfe_form_term_save_description_custom'];
185
-
186
- if($_description === 'custom')
187
- $_description = $_description_custom;
188
-
189
- $map = array();
190
-
191
- if($load_values){
192
-
193
- // Mapping
194
- $map = array(
195
- 'name' => get_sub_field( 'acfe_form_term_map_name' ),
196
- 'slug' => get_sub_field( 'acfe_form_term_map_slug' ),
197
- 'taxonomy' => get_sub_field( 'acfe_form_term_map_taxonomy' ),
198
- 'parent' => get_sub_field( 'acfe_form_term_map_parent' ),
199
- 'description' => get_sub_field( 'acfe_form_term_map_description' ),
200
- );
201
-
202
- }
203
-
204
- // Fields
205
- $fields = array(
206
- 'target' => get_sub_field('acfe_form_term_save_target'),
207
- 'name' => get_sub_field('acfe_form_term_save_name'),
208
- 'slug' => get_sub_field('acfe_form_term_save_slug'),
209
- 'taxonomy' => get_sub_field('acfe_form_term_save_taxonomy'),
210
- 'parent' => get_sub_field('acfe_form_term_save_parent'),
211
- 'description' => $_description,
212
- );
213
-
214
- $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form);
215
-
216
- // args
217
- $args = array();
218
-
219
- // Insert term
220
- $_term_id = 0;
221
-
222
- // Update term
223
- if($term_action === 'update_term'){
224
-
225
- $_term_id = $data['target'];
226
-
227
- // Invalid Term ID
228
- if(!$_term_id)
229
- return;
230
-
231
- $args['ID'] = $_term_id;
232
-
233
- }
234
-
235
- // Name
236
- if(!empty($data['name'])){
237
-
238
- if(is_array($data['name']))
239
- $data['name'] = acfe_array_to_string($data['name']);
240
-
241
- $args['name'] = $data['name'];
242
-
243
- }
244
-
245
- // Slug
246
- if(!empty($data['slug'])){
247
-
248
- if(is_array($data['name']))
249
- $data['name'] = acfe_array_to_string($data['name']);
250
-
251
- $args['slug'] = $data['slug'];
252
-
253
- }
254
-
255
- // Taxonomy
256
- if(!empty($data['taxonomy'])){
257
-
258
- if(is_array($data['name']))
259
- $data['name'] = acfe_array_to_string($data['name']);
260
-
261
- $args['taxonomy'] = $data['taxonomy'];
262
-
263
- }
264
-
265
- // Parent
266
- if(!empty($data['parent'])){
267
-
268
- if(is_array($data['name']))
269
- $data['name'] = acfe_array_to_string($data['name']);
270
-
271
- $args['parent'] = $data['parent'];
272
-
273
- }
274
-
275
- // Description
276
- if(!empty($data['description'])){
277
-
278
- if(is_array($data['name']))
279
- $data['name'] = acfe_array_to_string($data['name']);
280
-
281
- $args['description'] = $data['description'];
282
-
283
- }
284
-
285
- $args = apply_filters('acfe/form/submit/term_args', $args, $term_action, $form, $action);
286
- $args = apply_filters('acfe/form/submit/term_args/form=' . $form_name, $args, $term_action, $form, $action);
287
-
288
- if(!empty($action))
289
- $args = apply_filters('acfe/form/submit/term_args/action=' . $action, $args, $term_action, $form, $action);
290
-
291
- // Insert Term
292
- if($term_action === 'insert_term'){
293
-
294
- if(!isset($args['name']) || !isset($args['taxonomy'])){
295
-
296
- $args = false;
297
-
298
- }
299
-
300
- }
301
-
302
- if($args === false)
303
- return;
304
-
305
- // Insert Term
306
- if($term_action === 'insert_term'){
307
-
308
- $_insert_term = wp_insert_term($args['name'], $args['taxonomy'], $args);
309
-
310
- }
311
-
312
- // Update Term
313
- elseif($term_action === 'update_term'){
314
-
315
- $_insert_term = wp_update_term($args['ID'], $args['taxonomy'], $args);
316
-
317
- }
318
-
319
- // Term Error
320
- if(is_wp_error($_insert_term))
321
- return;
322
-
323
- $_term_id = $_insert_term['term_id'];
324
-
325
- $args['ID'] = $_term_id;
326
-
327
- // Save meta
328
- do_action('acfe/form/submit/term', $_term_id, $term_action, $args, $form, $action);
329
- do_action('acfe/form/submit/term/name=' . $form_name, $_term_id, $term_action, $args, $form, $action);
330
-
331
- if(!empty($action))
332
- do_action('acfe/form/submit/term/action=' . $action, $_term_id, $term_action, $args, $form, $action);
333
-
334
- }
335
-
336
- function submit($_term_id, $term_action, $args, $form, $action){
337
-
338
- if(!empty($action)){
339
-
340
- // Custom Query Var
341
- $custom_query_var = get_sub_field('acfe_form_custom_query_var');
342
-
343
- if(!empty($custom_query_var)){
344
-
345
- // Form name
346
- $form_name = acf_maybe_get($form, 'form_name');
347
-
348
- // Get term array
349
- $term_object = get_term($_term_id, $args['taxonomy'], 'ARRAY_A');
350
-
351
- $term_object['permalink'] = get_term_link($_term_id, $term_object['taxonomy']);
352
- $term_object['admin_url'] = admin_url('term.php?tag_ID=' . $_term_id . '&taxonomy=' . $term_object['taxonomy']);
353
-
354
- $term_object = apply_filters('acfe/form/query_var/term', $term_object, $_term_id, $term_action, $args, $form, $action);
355
- $term_object = apply_filters('acfe/form/query_var/term/form=' . $form_name, $term_object, $_term_id, $term_action, $args, $form, $action);
356
- $term_object = apply_filters('acfe/form/query_var/term/action=' . $action, $term_object, $_term_id, $term_action, $args, $form, $action);
357
-
358
- set_query_var($action, $term_object);
359
-
360
- }
361
-
362
- }
363
-
364
- // Meta save
365
- $save_meta = get_sub_field('acfe_form_term_save_meta');
366
-
367
- if(!empty($save_meta)){
368
-
369
- $meta = acfe_form_filter_meta($save_meta, $_POST['acf']);
370
-
371
- if(!empty($meta)){
372
-
373
- // Backup original acf post data
374
- $acf = $_POST['acf'];
375
-
376
- // Save meta fields
377
- acf_save_post('term_' . $_term_id, $meta);
378
-
379
- // Restore original acf post data
380
- $_POST['acf'] = $acf;
381
-
382
- }
383
-
384
- }
385
-
386
- }
387
-
388
- /**
389
- * Term: Select2 Choices
390
- */
391
- function prepare_choices($field){
392
-
393
- $field['choices']['current_term'] = 'Current: Term';
394
- $field['choices']['current_term_parent'] = 'Current: Term Parent';
395
-
396
- if(acf_maybe_get($field, 'value')){
397
-
398
- $value = $field['value'];
399
-
400
- if(is_array($value))
401
- $value = $value[0];
402
-
403
- $term = get_term($value);
404
-
405
- if($term){
406
-
407
- $field['choices'][$term->term_id] = $term->name;
408
-
409
- }
410
-
411
- }
412
-
413
- return $field;
414
-
415
- }
416
-
417
- function advanced_load($field){
418
-
419
- $form_name = 'my_form';
420
-
421
- if(acf_maybe_get($field, 'value'))
422
- $form_name = get_field('acfe_form_name', $field['value']);
423
-
424
- ?>You may use the following hooks:<br /><br />
425
- <pre>
426
- add_filter('acfe/form/load/term_id', 'my_form_term_values_source', 10, 3);
427
- add_filter('acfe/form/load/term_id/form=<?php echo $form_name; ?>', 'my_form_term_values_source', 10, 3);
428
- add_filter('acfe/form/load/term_id/action=my-term-action', 'my_form_term_values_source', 10, 3);
429
- </pre>
430
- <br />
431
- <pre>
432
- /**
433
- * @int $term_id Term ID used as source
434
- * @array $form The form settings
435
- * @string $action The action alias name
436
- */
437
- add_filter('acfe/form/load/term_id/form=<?php echo $form_name; ?>', 'my_form_term_values_source', 10, 3);
438
- function my_form_term_values_source($term_id, $form, $action){
439
-
440
- /**
441
- * Force to load values from the term ID 45
442
- */
443
- $term_id = 45;
444
-
445
-
446
- /**
447
- * Return
448
- */
449
- return $term_id;
450
-
451
- }
452
- </pre><?php
453
-
454
- }
455
-
456
- function advanced_save_args($field){
457
-
458
- $form_name = 'my_form';
459
-
460
- if(acf_maybe_get($field, 'value'))
461
- $form_name = get_field('acfe_form_name', $field['value']);
462
-
463
- ?>You may use the following hooks:<br /><br />
464
- <pre>
465
- add_filter('acfe/form/submit/term_args', 'my_form_term_args', 10, 4);
466
- add_filter('acfe/form/submit/term_args/form=<?php echo $form_name; ?>', 'my_form_term_args', 10, 4);
467
- add_filter('acfe/form/submit/term_args/action=my-term-action', 'my_form_term_args', 10, 4);
468
- </pre>
469
- <br />
470
- <pre>
471
- /**
472
- * @array $args The generated term arguments
473
- * @string $type Action type: 'insert_term' or 'update_term'
474
- * @array $form The form settings
475
- * @string $action The action alias name
476
- */
477
- add_filter('acfe/form/submit/term_args/form=<?php echo $form_name; ?>', 'my_form_term_args', 10, 4);
478
- function my_form_term_args($args, $type, $form, $action){
479
-
480
- /**
481
- * Force specific description if the action type is 'insert_term'
482
- */
483
- if($type === 'insert_term'){
484
-
485
- $args['description'] = 'My term description';
486
-
487
- }
488
-
489
-
490
- /**
491
- * Get the form input value named 'my_field'
492
- * This is the value entered by the user during the form submission
493
- */
494
- $my_field = get_field('my_field');
495
-
496
-
497
- /**
498
- * Get the field value 'my_field' from the post ID 145
499
- */
500
- $my_post_field = get_field('my_field', 145);
501
-
502
-
503
- /**
504
- * Return arguments
505
- * Note: Return false will stop post & meta insert/update
506
- */
507
- return $args;
508
-
509
- }
510
- </pre><?php
511
-
512
- }
513
-
514
- function advanced_save($field){
515
-
516
- $form_name = 'my_form';
517
-
518
- if(acf_maybe_get($field, 'value'))
519
- $form_name = get_field('acfe_form_name', $field['value']);
520
-
521
- ?>You may use the following hooks:<br /><br />
522
- <pre>
523
- add_action('acfe/form/submit/term', 'my_form_term_save', 10, 5);
524
- add_action('acfe/form/submit/term/form=<?php echo $form_name; ?>', 'my_form_term_save', 10, 5);
525
- add_action('acfe/form/submit/term/action=my-term-action', 'my_form_term_save', 10, 5);
526
- </pre>
527
- <br />
528
- <pre>
529
- /**
530
- * @int $term_id The targeted term ID
531
- * @string $type Action type: 'insert_term' or 'update_term'
532
- * @array $args The generated term arguments
533
- * @array $form The form settings
534
- * @string $action The action alias name
535
- *
536
- * Note: At this point the term is already saved into the database
537
- */
538
- add_action('acfe/form/submit/term/form=<?php echo $form_name; ?>', 'my_form_term_save', 10, 5);
539
- function my_form_term_save($term_id, $type, $args, $form, $action){
540
-
541
- /**
542
- * Get the form input value named 'my_field'
543
- * This is the value entered by the user during the form submission
544
- */
545
- $my_field = get_field('my_field');
546
-
547
-
548
- /**
549
- * Get the field value 'my_field' from the currently saved term
550
- */
551
- $my_term_field = get_field('my_field', 'term_' . $term_id);
552
-
553
- }
554
- </pre><?php
555
-
556
- }
557
-
558
- }
559
-
560
- new acfe_form_term();
561
-
562
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ if(!class_exists('acfe_form_term')):
7
+
8
+ class acfe_form_term{
9
+
10
+ function __construct(){
11
+
12
+ /*
13
+ * Form
14
+ */
15
+ add_filter('acfe/form/load/term', array($this, 'load'), 1, 3);
16
+ add_action('acfe/form/prepare/term', array($this, 'prepare'), 1, 3);
17
+ add_action('acfe/form/submit/term', array($this, 'submit'), 1, 5);
18
+
19
+ /*
20
+ * Admin
21
+ */
22
+ add_filter('acf/prepare_field/name=acfe_form_term_save_meta', array(acfe()->acfe_form, 'map_fields'));
23
+ add_filter('acf/prepare_field/name=acfe_form_term_load_meta', array(acfe()->acfe_form, 'map_fields'));
24
+
25
+ add_filter('acf/prepare_field/name=acfe_form_term_save_target', array(acfe()->acfe_form, 'map_fields_deep'));
26
+ add_filter('acf/prepare_field/name=acfe_form_term_load_source', array(acfe()->acfe_form, 'map_fields_deep'));
27
+
28
+ add_filter('acf/prepare_field/name=acfe_form_term_save_name', array(acfe()->acfe_form, 'map_fields_deep'));
29
+ add_filter('acf/prepare_field/name=acfe_form_term_save_slug', array(acfe()->acfe_form, 'map_fields_deep'));
30
+ add_filter('acf/prepare_field/name=acfe_form_term_save_taxonomy', array(acfe()->acfe_form, 'map_fields_deep'));
31
+ add_filter('acf/prepare_field/name=acfe_form_term_save_parent', array(acfe()->acfe_form, 'map_fields_deep'));
32
+ add_filter('acf/prepare_field/name=acfe_form_term_save_description', array(acfe()->acfe_form, 'map_fields_deep'));
33
+
34
+ add_filter('acf/prepare_field/name=acfe_form_term_map_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
35
+ add_filter('acf/prepare_field/name=acfe_form_term_map_slug', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
36
+ add_filter('acf/prepare_field/name=acfe_form_term_map_taxonomy', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
37
+ add_filter('acf/prepare_field/name=acfe_form_term_map_parent', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
38
+ add_filter('acf/prepare_field/name=acfe_form_term_map_description', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
39
+
40
+ add_filter('acf/prepare_field/name=acfe_form_term_save_target', array($this, 'prepare_choices'), 5);
41
+ add_filter('acf/prepare_field/name=acfe_form_term_load_source', array($this, 'prepare_choices'), 5);
42
+ add_filter('acf/prepare_field/name=acfe_form_term_save_parent', array($this, 'prepare_choices'), 5);
43
+
44
+ add_action('acf/render_field/name=acfe_form_term_advanced_load', array($this, 'advanced_load'));
45
+ add_action('acf/render_field/name=acfe_form_term_advanced_save_args', array($this, 'advanced_save_args'));
46
+ add_action('acf/render_field/name=acfe_form_term_advanced_save', array($this, 'advanced_save'));
47
+
48
+ }
49
+
50
+ function load($form, $current_post_id, $action){
51
+
52
+ // Form
53
+ $form_name = acf_maybe_get($form, 'form_name');
54
+ $form_id = acf_maybe_get($form, 'form_id');
55
+
56
+ // Action
57
+ $term_action = get_sub_field('acfe_form_term_action');
58
+
59
+ // Load values
60
+ $load_values = get_sub_field('acfe_form_term_load_values');
61
+ $load_meta = get_sub_field('acfe_form_term_load_meta');
62
+
63
+ // Load values
64
+ if(!$load_values)
65
+ return $form;
66
+
67
+ $_term_id = get_sub_field('acfe_form_term_load_source');
68
+ $_name = get_sub_field('acfe_form_term_map_name');
69
+ $_slug = get_sub_field('acfe_form_term_map_slug');
70
+ $_taxonomy = get_sub_field('acfe_form_term_map_taxonomy');
71
+ $_parent = get_sub_field('acfe_form_term_map_parent');
72
+ $_description = get_sub_field('acfe_form_term_map_description');
73
+
74
+ // Map {field:name} {get_field:name} {query_var:name}
75
+ $_term_id = acfe_form_map_field_value_load($_term_id, $current_post_id, $form);
76
+ $_name = acfe_form_map_field_value_load($_name, $current_post_id, $form);
77
+ $_slug = acfe_form_map_field_value_load($_slug, $current_post_id, $form);
78
+ $_taxonomy = acfe_form_map_field_value_load($_taxonomy, $current_post_id, $form);
79
+ $_parent = acfe_form_map_field_value_load($_parent, $current_post_id, $form);
80
+ $_description = acfe_form_map_field_value_load($_description, $current_post_id, $form);
81
+
82
+ $_term_id = apply_filters('acfe/form/load/term_id', $_term_id, $form, $action);
83
+ $_term_id = apply_filters('acfe/form/load/term_id/form=' . $form_name, $_term_id, $form, $action);
84
+
85
+ if(!empty($action))
86
+ $_term_id = apply_filters('acfe/form/load/term_id/action=' . $action, $_term_id, $form, $action);
87
+
88
+ // Invalid Term ID
89
+ if(!$_term_id)
90
+ return $form;
91
+
92
+ // Name
93
+ if(acf_is_field_key($_name)){
94
+
95
+ $key = array_search($_name, $load_meta);
96
+
97
+ if($key !== false)
98
+ unset($load_meta[$key]);
99
+
100
+ $form['map'][$_name]['value'] = get_term_field('name', $_term_id);
101
+
102
+ }
103
+
104
+ // Slug
105
+ if(acf_is_field_key($_slug)){
106
+
107
+ $key = array_search($_slug, $load_meta);
108
+
109
+ if($key !== false)
110
+ unset($load_meta[$key]);
111
+
112
+ $form['map'][$_slug]['value'] = get_term_field('slug', $_term_id);
113
+
114
+ }
115
+
116
+ // Taxonomy
117
+ if(acf_is_field_key($_taxonomy)){
118
+
119
+ $key = array_search($_taxonomy, $load_meta);
120
+
121
+ if($key !== false)
122
+ unset($load_meta[$key]);
123
+
124
+ $form['map'][$_taxonomy]['value'] = get_term_field('taxonomy', $_term_id);
125
+
126
+ }
127
+
128
+ // Parent
129
+ if(acf_is_field_key($_parent)){
130
+
131
+ $key = array_search($_parent, $load_meta);
132
+
133
+ if($key !== false)
134
+ unset($load_meta[$key]);
135
+
136
+ $form['map'][$_parent]['value'] = get_term_field('parent', $_term_id);
137
+
138
+ }
139
+
140
+ // Description
141
+ if(acf_is_field_key($_description)){
142
+
143
+ $key = array_search($_description, $load_meta);
144
+
145
+ if($key !== false)
146
+ unset($load_meta[$key]);
147
+
148
+ $form['map'][$_description]['value'] = get_term_field('description', $_term_id);
149
+
150
+ }
151
+
152
+ // Load others values
153
+ if(!empty($load_meta)){
154
+
155
+ foreach($load_meta as $field_key){
156
+
157
+ $field = acf_get_field($field_key);
158
+
159
+ $form['map'][$field_key]['value'] = acf_get_value('term_' . $_term_id, $field);
160
+
161
+ }
162
+
163
+ }
164
+
165
+ return $form;
166
+
167
+ }
168
+
169
+ function prepare($form, $current_post_id, $action){
170
+
171
+ // Form
172
+ $form_name = acf_maybe_get($form, 'form_name');
173
+ $form_id = acf_maybe_get($form, 'form_id');
174
+
175
+ // Action
176
+ $term_action = get_sub_field('acfe_form_term_action');
177
+
178
+ // Load values
179
+ $load_values = get_sub_field('acfe_form_term_load_values');
180
+
181
+ // Pre-process
182
+ $_description_group = get_sub_field('acfe_form_term_save_description_group');
183
+ $_description = $_description_group['acfe_form_term_save_description'];
184
+ $_description_custom = $_description_group['acfe_form_term_save_description_custom'];
185
+
186
+ if($_description === 'custom')
187
+ $_description = $_description_custom;
188
+
189
+ $map = array();
190
+
191
+ if($load_values){
192
+
193
+ // Mapping
194
+ $map = array(
195
+ 'name' => get_sub_field( 'acfe_form_term_map_name' ),
196
+ 'slug' => get_sub_field( 'acfe_form_term_map_slug' ),
197
+ 'taxonomy' => get_sub_field( 'acfe_form_term_map_taxonomy' ),
198
+ 'parent' => get_sub_field( 'acfe_form_term_map_parent' ),
199
+ 'description' => get_sub_field( 'acfe_form_term_map_description' ),
200
+ );
201
+
202
+ }
203
+
204
+ // Fields
205
+ $fields = array(
206
+ 'target' => get_sub_field('acfe_form_term_save_target'),
207
+ 'name' => get_sub_field('acfe_form_term_save_name'),
208
+ 'slug' => get_sub_field('acfe_form_term_save_slug'),
209
+ 'taxonomy' => get_sub_field('acfe_form_term_save_taxonomy'),
210
+ 'parent' => get_sub_field('acfe_form_term_save_parent'),
211
+ 'description' => $_description,
212
+ );
213
+
214
+ $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form);
215
+
216
+ // args
217
+ $args = array();
218
+
219
+ // Insert term
220
+ $_term_id = 0;
221
+
222
+ // Update term
223
+ if($term_action === 'update_term'){
224
+
225
+ $_term_id = $data['target'];
226
+
227
+ // Invalid Term ID
228
+ if(!$_term_id)
229
+ return;
230
+
231
+ $args['ID'] = $_term_id;
232
+
233
+ }
234
+
235
+ // Name
236
+ if(!empty($data['name'])){
237
+
238
+ if(is_array($data['name']))
239
+ $data['name'] = acfe_array_to_string($data['name']);
240
+
241
+ $args['name'] = $data['name'];
242
+
243
+ }
244
+
245
+ // Slug
246
+ if(!empty($data['slug'])){
247
+
248
+ if(is_array($data['name']))
249
+ $data['name'] = acfe_array_to_string($data['name']);
250
+
251
+ $args['slug'] = $data['slug'];
252
+
253
+ }
254
+
255
+ // Taxonomy
256
+ if(!empty($data['taxonomy'])){
257
+
258
+ if(is_array($data['name']))
259
+ $data['name'] = acfe_array_to_string($data['name']);
260
+
261
+ $args['taxonomy'] = $data['taxonomy'];
262
+
263
+ }
264
+
265
+ // Parent
266
+ if(!empty($data['parent'])){
267
+
268
+ if(is_array($data['name']))
269
+ $data['name'] = acfe_array_to_string($data['name']);
270
+
271
+ $args['parent'] = $data['parent'];
272
+
273
+ }
274
+
275
+ // Description
276
+ if(!empty($data['description'])){
277
+
278
+ if(is_array($data['name']))
279
+ $data['name'] = acfe_array_to_string($data['name']);
280
+
281
+ $args['description'] = $data['description'];
282
+
283
+ }
284
+
285
+ $args = apply_filters('acfe/form/submit/term_args', $args, $term_action, $form, $action);
286
+ $args = apply_filters('acfe/form/submit/term_args/form=' . $form_name, $args, $term_action, $form, $action);
287
+
288
+ if(!empty($action))
289
+ $args = apply_filters('acfe/form/submit/term_args/action=' . $action, $args, $term_action, $form, $action);
290
+
291
+ // Insert Term
292
+ if($term_action === 'insert_term'){
293
+
294
+ if(!isset($args['name']) || !isset($args['taxonomy'])){
295
+
296
+ $args = false;
297
+
298
+ }
299
+
300
+ }
301
+
302
+ if($args === false)
303
+ return;
304
+
305
+ // Insert Term
306
+ if($term_action === 'insert_term'){
307
+
308
+ $_insert_term = wp_insert_term($args['name'], $args['taxonomy'], $args);
309
+
310
+ }
311
+
312
+ // Update Term
313
+ elseif($term_action === 'update_term'){
314
+
315
+ $_insert_term = wp_update_term($args['ID'], $args['taxonomy'], $args);
316
+
317
+ }
318
+
319
+ // Term Error
320
+ if(is_wp_error($_insert_term))
321
+ return;
322
+
323
+ $_term_id = $_insert_term['term_id'];
324
+
325
+ $args['ID'] = $_term_id;
326
+
327
+ // Save meta
328
+ do_action('acfe/form/submit/term', $_term_id, $term_action, $args, $form, $action);
329
+ do_action('acfe/form/submit/term/name=' . $form_name, $_term_id, $term_action, $args, $form, $action);
330
+
331
+ if(!empty($action))
332
+ do_action('acfe/form/submit/term/action=' . $action, $_term_id, $term_action, $args, $form, $action);
333
+
334
+ }
335
+
336
+ function submit($_term_id, $term_action, $args, $form, $action){
337
+
338
+ if(!empty($action)){
339
+
340
+ // Custom Query Var
341
+ $custom_query_var = get_sub_field('acfe_form_custom_query_var');
342
+
343
+ if(!empty($custom_query_var)){
344
+
345
+ // Form name
346
+ $form_name = acf_maybe_get($form, 'form_name');
347
+
348
+ // Get term array
349
+ $term_object = get_term($_term_id, $args['taxonomy'], 'ARRAY_A');
350
+
351
+ $term_object['permalink'] = get_term_link($_term_id, $term_object['taxonomy']);
352
+ $term_object['admin_url'] = admin_url('term.php?tag_ID=' . $_term_id . '&taxonomy=' . $term_object['taxonomy']);
353
+
354
+ $term_object = apply_filters('acfe/form/query_var/term', $term_object, $_term_id, $term_action, $args, $form, $action);
355
+ $term_object = apply_filters('acfe/form/query_var/term/form=' . $form_name, $term_object, $_term_id, $term_action, $args, $form, $action);
356
+ $term_object = apply_filters('acfe/form/query_var/term/action=' . $action, $term_object, $_term_id, $term_action, $args, $form, $action);
357
+
358
+ set_query_var($action, $term_object);
359
+
360
+ }
361
+
362
+ }
363
+
364
+ // Meta save
365
+ $save_meta = get_sub_field('acfe_form_term_save_meta');
366
+
367
+ if(!empty($save_meta)){
368
+
369
+ $meta = acfe_form_filter_meta($save_meta, $_POST['acf']);
370
+
371
+ if(!empty($meta)){
372
+
373
+ // Backup original acf post data
374
+ $acf = $_POST['acf'];
375
+
376
+ // Save meta fields
377
+ acf_save_post('term_' . $_term_id, $meta);
378
+
379
+ // Restore original acf post data
380
+ $_POST['acf'] = $acf;
381
+
382
+ }
383
+
384
+ }
385
+
386
+ }
387
+
388
+ /**
389
+ * Term: Select2 Choices
390
+ */
391
+ function prepare_choices($field){
392
+
393
+ $field['choices']['current_term'] = 'Current: Term';
394
+ $field['choices']['current_term_parent'] = 'Current: Term Parent';
395
+
396
+ if(acf_maybe_get($field, 'value')){
397
+
398
+ $value = $field['value'];
399
+
400
+ if(is_array($value))
401
+ $value = $value[0];
402
+
403
+ $term = get_term($value);
404
+
405
+ if($term){
406
+
407
+ $field['choices'][$term->term_id] = $term->name;
408
+
409
+ }
410
+
411
+ }
412
+
413
+ return $field;
414
+
415
+ }
416
+
417
+ function advanced_load($field){
418
+
419
+ $form_name = 'my_form';
420
+
421
+ if(acf_maybe_get($field, 'value'))
422
+ $form_name = get_field('acfe_form_name', $field['value']);
423
+
424
+ ?>You may use the following hooks:<br /><br />
425
+ <pre>
426
+ add_filter('acfe/form/load/term_id', 'my_form_term_values_source', 10, 3);
427
+ add_filter('acfe/form/load/term_id/form=<?php echo $form_name; ?>', 'my_form_term_values_source', 10, 3);
428
+ add_filter('acfe/form/load/term_id/action=my-term-action', 'my_form_term_values_source', 10, 3);
429
+ </pre>
430
+ <br />
431
+ <pre>
432
+ /**
433
+ * @int $term_id Term ID used as source
434
+ * @array $form The form settings
435
+ * @string $action The action alias name
436
+ */
437
+ add_filter('acfe/form/load/term_id/form=<?php echo $form_name; ?>', 'my_form_term_values_source', 10, 3);
438
+ function my_form_term_values_source($term_id, $form, $action){
439
+
440
+ /**
441
+ * Force to load values from the term ID 45
442
+ */
443
+ $term_id = 45;
444
+
445
+
446
+ /**
447
+ * Return
448
+ */
449
+ return $term_id;
450
+
451
+ }
452
+ </pre><?php
453
+
454
+ }
455
+
456
+ function advanced_save_args($field){
457
+
458
+ $form_name = 'my_form';
459
+
460
+ if(acf_maybe_get($field, 'value'))
461
+ $form_name = get_field('acfe_form_name', $field['value']);
462
+
463
+ ?>You may use the following hooks:<br /><br />
464
+ <pre>
465
+ add_filter('acfe/form/submit/term_args', 'my_form_term_args', 10, 4);
466
+ add_filter('acfe/form/submit/term_args/form=<?php echo $form_name; ?>', 'my_form_term_args', 10, 4);
467
+ add_filter('acfe/form/submit/term_args/action=my-term-action', 'my_form_term_args', 10, 4);
468
+ </pre>
469
+ <br />
470
+ <pre>
471
+ /**
472
+ * @array $args The generated term arguments
473
+ * @string $type Action type: 'insert_term' or 'update_term'
474
+ * @array $form The form settings
475
+ * @string $action The action alias name
476
+ */
477
+ add_filter('acfe/form/submit/term_args/form=<?php echo $form_name; ?>', 'my_form_term_args', 10, 4);
478
+ function my_form_term_args($args, $type, $form, $action){
479
+
480
+ /**
481
+ * Force specific description if the action type is 'insert_term'
482
+ */
483
+ if($type === 'insert_term'){
484
+
485
+ $args['description'] = 'My term description';
486
+
487
+ }
488
+
489
+
490
+ /**
491
+ * Get the form input value named 'my_field'
492
+ * This is the value entered by the user during the form submission
493
+ */
494
+ $my_field = get_field('my_field');
495
+
496
+
497
+ /**
498
+ * Get the field value 'my_field' from the post ID 145
499
+ */
500
+ $my_post_field = get_field('my_field', 145);
501
+
502
+
503
+ /**
504
+ * Return arguments
505
+ * Note: Return false will stop post & meta insert/update
506
+ */
507
+ return $args;
508
+
509
+ }
510
+ </pre><?php
511
+
512
+ }
513
+
514
+ function advanced_save($field){
515
+
516
+ $form_name = 'my_form';
517
+
518
+ if(acf_maybe_get($field, 'value'))
519
+ $form_name = get_field('acfe_form_name', $field['value']);
520
+
521
+ ?>You may use the following hooks:<br /><br />
522
+ <pre>
523
+ add_action('acfe/form/submit/term', 'my_form_term_save', 10, 5);
524
+ add_action('acfe/form/submit/term/form=<?php echo $form_name; ?>', 'my_form_term_save', 10, 5);
525
+ add_action('acfe/form/submit/term/action=my-term-action', 'my_form_term_save', 10, 5);
526
+ </pre>
527
+ <br />
528
+ <pre>
529
+ /**
530
+ * @int $term_id The targeted term ID
531
+ * @string $type Action type: 'insert_term' or 'update_term'
532
+ * @array $args The generated term arguments
533
+ * @array $form The form settings
534
+ * @string $action The action alias name
535
+ *
536
+ * Note: At this point the term is already saved into the database
537
+ */
538
+ add_action('acfe/form/submit/term/form=<?php echo $form_name; ?>', 'my_form_term_save', 10, 5);
539
+ function my_form_term_save($term_id, $type, $args, $form, $action){
540
+
541
+ /**
542
+ * Get the form input value named 'my_field'
543
+ * This is the value entered by the user during the form submission
544
+ */
545
+ $my_field = get_field('my_field');
546
+
547
+
548
+ /**
549
+ * Get the field value 'my_field' from the currently saved term
550
+ */
551
+ $my_term_field = get_field('my_field', 'term_' . $term_id);
552
+
553
+ }
554
+ </pre><?php
555
+
556
+ }
557
+
558
+ }
559
+
560
+ new acfe_form_term();
561
+
562
  endif;
includes/modules/form/actions/user.php CHANGED
@@ -1,1036 +1,1036 @@
1
- <?php
2
-
3
- if(!defined('ABSPATH'))
4
- exit;
5
-
6
- if(!class_exists('acfe_form_user')):
7
-
8
- class acfe_form_user{
9
-
10
- function __construct(){
11
-
12
- /*
13
- * Form
14
- */
15
- add_filter('acfe/form/load/user', array($this, 'load'), 1, 3);
16
- add_action('acfe/form/validation/user', array($this, 'validation'), 1, 3);
17
- add_action('acfe/form/prepare/user', array($this, 'prepare'), 1, 3);
18
- add_action('acfe/form/submit/user', array($this, 'submit'), 1, 5);
19
-
20
- /*
21
- * Admin
22
- */
23
- add_filter('acf/prepare_field/name=acfe_form_user_save_meta', array(acfe()->acfe_form, 'map_fields'));
24
- add_filter('acf/prepare_field/name=acfe_form_user_load_meta', array(acfe()->acfe_form, 'map_fields'));
25
-
26
- add_filter('acf/prepare_field/name=acfe_form_user_save_login_user', array(acfe()->acfe_form, 'map_fields_deep'));
27
- add_filter('acf/prepare_field/name=acfe_form_user_save_login_pass', array(acfe()->acfe_form, 'map_fields_deep'));
28
- add_filter('acf/prepare_field/name=acfe_form_user_save_login_remember', array(acfe()->acfe_form, 'map_fields_deep'));
29
-
30
- add_filter('acf/prepare_field/name=acfe_form_user_save_target', array(acfe()->acfe_form, 'map_fields_deep'));
31
- add_filter('acf/prepare_field/name=acfe_form_user_load_source', array(acfe()->acfe_form, 'map_fields_deep'));
32
-
33
- add_filter('acf/prepare_field/name=acfe_form_user_save_email', array(acfe()->acfe_form, 'map_fields_deep'));
34
- add_filter('acf/prepare_field/name=acfe_form_user_save_username', array(acfe()->acfe_form, 'map_fields_deep'));
35
- add_filter('acf/prepare_field/name=acfe_form_user_save_password', array(acfe()->acfe_form, 'map_fields_deep'));
36
- add_filter('acf/prepare_field/name=acfe_form_user_save_first_name', array(acfe()->acfe_form, 'map_fields_deep'));
37
- add_filter('acf/prepare_field/name=acfe_form_user_save_last_name', array(acfe()->acfe_form, 'map_fields_deep'));
38
- add_filter('acf/prepare_field/name=acfe_form_user_save_nickname', array(acfe()->acfe_form, 'map_fields_deep'));
39
- add_filter('acf/prepare_field/name=acfe_form_user_save_display_name', array(acfe()->acfe_form, 'map_fields_deep'));
40
- add_filter('acf/prepare_field/name=acfe_form_user_save_website', array(acfe()->acfe_form, 'map_fields_deep'));
41
- add_filter('acf/prepare_field/name=acfe_form_user_save_description', array(acfe()->acfe_form, 'map_fields_deep'));
42
- add_filter('acf/prepare_field/name=acfe_form_user_save_role', array(acfe()->acfe_form, 'map_fields_deep'));
43
-
44
- add_filter('acf/prepare_field/name=acfe_form_user_map_email', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
45
- add_filter('acf/prepare_field/name=acfe_form_user_map_username', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
46
- add_filter('acf/prepare_field/name=acfe_form_user_map_password', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
47
- add_filter('acf/prepare_field/name=acfe_form_user_map_first_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
48
- add_filter('acf/prepare_field/name=acfe_form_user_map_last_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
49
- add_filter('acf/prepare_field/name=acfe_form_user_map_nickname', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
50
- add_filter('acf/prepare_field/name=acfe_form_user_map_display_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
51
- add_filter('acf/prepare_field/name=acfe_form_user_map_website', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
52
- add_filter('acf/prepare_field/name=acfe_form_user_map_description', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
53
- add_filter('acf/prepare_field/name=acfe_form_user_map_role', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
54
-
55
- add_filter('acf/prepare_field/name=acfe_form_user_save_target', array($this, 'prepare_choices'), 5);
56
- add_filter('acf/prepare_field/name=acfe_form_user_load_source', array($this, 'prepare_choices'), 5);
57
-
58
- add_action('acf/render_field/name=acfe_form_user_advanced_load', array($this, 'advanced_load'));
59
- add_action('acf/render_field/name=acfe_form_user_advanced_save_args', array($this, 'advanced_save_args'));
60
- add_action('acf/render_field/name=acfe_form_user_advanced_save', array($this, 'advanced_save'));
61
-
62
- }
63
-
64
- function load($form, $current_post_id, $action){
65
-
66
- // Form
67
- $form_name = acf_maybe_get($form, 'form_name');
68
- $form_id = acf_maybe_get($form, 'form_id');
69
-
70
- // Action
71
- $user_action = get_sub_field('acfe_form_user_action');
72
-
73
- // Bail early if Log user
74
- if($user_action === 'log_user')
75
- return $form;
76
-
77
- // Load values
78
- $load_values = get_sub_field('acfe_form_user_load_values');
79
- $load_meta = get_sub_field('acfe_form_user_load_meta');
80
-
81
- // Load values
82
- if(!$load_values)
83
- return $form;
84
-
85
- $_user_id = get_sub_field('acfe_form_user_load_source');
86
- $_email = get_sub_field('acfe_form_user_map_email');
87
- $_username = get_sub_field('acfe_form_user_map_username');
88
- $_password = get_sub_field('acfe_form_user_map_password');
89
- $_first_name = get_sub_field('acfe_form_user_map_first_name');
90
- $_last_name = get_sub_field('acfe_form_user_map_last_name');
91
- $_nickname = get_sub_field('acfe_form_user_map_nickname');
92
- $_display_name = get_sub_field('acfe_form_user_map_display_name');
93
- $_website = get_sub_field('acfe_form_user_map_website');
94
- $_description = get_sub_field('acfe_form_user_map_description');
95
- $_role = get_sub_field('acfe_form_user_map_role');
96
-
97
- // Map {field:name} {get_field:name} {query_var:name}
98
- $_user_id = acfe_form_map_field_value_load($_user_id, $current_post_id, $form);
99
- $_email = acfe_form_map_field_value_load($_email, $current_post_id, $form);
100
- $_username = acfe_form_map_field_value_load($_username, $current_post_id, $form);
101
- $_password = acfe_form_map_field_value_load($_password, $current_post_id, $form);
102
- $_first_name = acfe_form_map_field_value_load($_first_name, $current_post_id, $form);
103
- $_last_name = acfe_form_map_field_value_load($_last_name, $current_post_id, $form);
104
- $_nickname = acfe_form_map_field_value_load($_nickname, $current_post_id, $form);
105
- $_display_name = acfe_form_map_field_value_load($_display_name, $current_post_id, $form);
106
- $_website = acfe_form_map_field_value_load($_website, $current_post_id, $form);
107
- $_description = acfe_form_map_field_value_load($_description, $current_post_id, $form);
108
- $_role = acfe_form_map_field_value_load($_role, $current_post_id, $form);
109
-
110
- $_user_id = apply_filters('acfe/form/load/user_id', $_user_id, $form, $action);
111
- $_user_id = apply_filters('acfe/form/load/user_id/form=' . $form_name, $_user_id, $form, $action);
112
-
113
- if(!empty($action))
114
- $_user_id = apply_filters('acfe/form/load/user_id/action=' . $action, $_user_id, $form, $action);
115
-
116
- // Invalid User ID
117
- if(!$_user_id)
118
- return $form;
119
-
120
- $user_data = get_userdata($_user_id);
121
-
122
- // Check if userdata has been found
123
- if(!$user_data)
124
- return $form;
125
-
126
- // Email
127
- if(acf_is_field_key($_email)){
128
-
129
- $key = array_search($_email, $load_meta);
130
-
131
- if($key !== false)
132
- unset($load_meta[$key]);
133
-
134
- $form['map'][$_email]['value'] = $user_data->user_email;
135
-
136
- }
137
-
138
- // Username
139
- if(acf_is_field_key($_username)){
140
-
141
- $key = array_search($_username, $load_meta);
142
-
143
- if($key !== false)
144
- unset($load_meta[$key]);
145
-
146
- $form['map'][$_username]['value'] = $user_data->user_login;
147
- $form['map'][$_username]['maxlength'] = 60;
148
-
149
- }
150
-
151
- // Password
152
- if(acf_is_field_key($_password)){
153
-
154
- $key = array_search($_password, $load_meta);
155
-
156
- if($key !== false)
157
- unset($load_meta[$key]);
158
-
159
- //$form['map'][$_password]['value'] = $user_data->user_pass;
160
-
161
- }
162
-
163
- // First name
164
- if(acf_is_field_key($_first_name)){
165
-
166
- $key = array_search($_first_name, $load_meta);
167
-
168
- if($key !== false)
169
- unset($load_meta[$key]);
170
-
171
- $form['map'][$_first_name]['value'] = $user_data->first_name;
172
-
173
- }
174
-
175
- // Last name
176
- if(acf_is_field_key($_last_name)){
177
-
178
- $key = array_search($_last_name, $load_meta);
179
-
180
- if($key !== false)
181
- unset($load_meta[$key]);
182
-
183
- $form['map'][$_last_name]['value'] = $user_data->last_name;
184
-
185
- }
186
-
187
- // Nickname
188
- if(acf_is_field_key($_nickname)){
189
-
190
- $key = array_search($_nickname, $load_meta);
191
-
192
- if($key !== false)
193
- unset($load_meta[$key]);
194
-
195
- $form['map'][$_nickname]['value'] = $user_data->nickname;
196
-
197
- }
198
-
199
- // Display name
200
- if(acf_is_field_key($_display_name)){
201
-
202
- $key = array_search($_display_name, $load_meta);
203
-
204
- if($key !== false)
205
- unset($load_meta[$key]);
206
-
207
- $form['map'][$_display_name]['value'] = $user_data->display_name;
208
-
209
- }
210
-
211
- // Website
212
- if(acf_is_field_key($_website)){
213
-
214
- $key = array_search($_website, $load_meta);
215
-
216
- if($key !== false)
217
- unset($load_meta[$key]);
218
-
219
- $form['map'][$_website]['value'] = $user_data->website;
220
-
221
- }
222
-
223
- // Description
224
- if(acf_is_field_key($_description)){
225
-
226
- $key = array_search($_description, $load_meta);
227
-
228
- if($key !== false)
229
- unset($load_meta[$key]);
230
-
231
- $form['map'][$_description]['value'] = $user_data->description;
232
-
233
- }
234
-
235
- // Role
236
- if(acf_is_field_key($_role)){
237
-
238
- $key = array_search($_role, $load_meta);
239
-
240
- if($key !== false)
241
- unset($load_meta[$key]);
242
-
243
- $form['map'][$_role]['value'] = implode(', ', $user_data->roles);
244
-
245
- }
246
-
247
- // Load others values
248
- if(!empty($load_meta)){
249
-
250
- foreach($load_meta as $field_key){
251
-
252
- $field = acf_get_field($field_key);
253
-
254
- $form['map'][$field_key]['value'] = acf_get_value('user_' . $_user_id, $field);
255
-
256
- }
257
-
258
- }
259
-
260
- return $form;
261
-
262
- }
263
-
264
- function validation($form, $current_post_id, $action){
265
-
266
- // Action
267
- $user_action = get_sub_field('acfe_form_user_action');
268
-
269
- if($user_action !== 'log_user')
270
- return;
271
-
272
- // Form
273
- $form_name = acf_maybe_get($form, 'form_name');
274
- $form_id = acf_maybe_get($form, 'form_id');
275
-
276
- // Fields
277
- $data = array(
278
- 'type' => get_sub_field('acfe_form_user_log_type'),
279
- 'login' => get_sub_field('acfe_form_user_save_login_user'),
280
- 'pass' => get_sub_field('acfe_form_user_save_login_pass'),
281
- );
282
-
283
- $data['login'] = acfe_form_map_field_value($data['login'], $current_post_id, $form);
284
- $data['pass'] = acfe_form_map_field_value($data['pass'], $current_post_id, $form);
285
-
286
- $login = false;
287
- $pass = false;
288
-
289
- // Email
290
- if(!empty($data['login'])){
291
-
292
- $login = $data['login'];
293
-
294
- }
295
-
296
- // Password
297
- if(!empty($data['pass'])){
298
-
299
- $pass = $data['pass'];
300
-
301
- }
302
-
303
- $pass = wp_specialchars_decode($pass);
304
-
305
- if(empty($login) || empty($pass)){
306
-
307
- acfe_add_validation_error('', 'An error has occured. Please try again');
308
- return;
309
-
310
- }
311
-
312
- // Email
313
- if($data['type'] === 'email'){
314
-
315
- $this->validate_user_login('email', $login, $pass);
316
-
317
- }
318
-
319
- // Username
320
- elseif($data['type'] === 'username'){
321
-
322
- $this->validate_user_login('username', $login, $pass);
323
-
324
- }
325
-
326
- // Email || Username
327
- elseif($data['type'] === 'email_username'){
328
-
329
- // Email
330
- if(is_email($login)){
331
-
332
- $this->validate_user_login('email', $login, $pass);
333
-
334
- // Username
335
- }else{
336
-
337
- $this->validate_user_login('username', $login, $pass);
338
-
339
- }
340
-
341
- }
342
-
343
- }
344
-
345
- function validate_user_login($type = 'email', $login, $pass){
346
-
347
- if($type === 'email'){
348
-
349
- $login = sanitize_email($login);
350
-
351
- if(empty($login) || !is_email($login)){
352
-
353
- acfe_add_validation_error('', 'Invalid e-mail');
354
- return;
355
-
356
- }
357
-
358
- $user = get_user_by('email', $login);
359
-
360
- if(!$user || !wp_check_password($pass, $user->data->user_pass, $user->ID)){
361
-
362
- acfe_add_validation_error('', 'Invalid e-mail or password');
363
- return;
364
-
365
- }
366
-
367
- }elseif($type === 'username'){
368
-
369
- $login = sanitize_user($login);
370
-
371
- if(empty($login)){
372
-
373
- acfe_add_validation_error('', 'Invalid username');
374
- return;
375
-
376
- }
377
-
378
- $user = get_user_by('login', $login);
379
-
380
- if(!$user || !wp_check_password($pass, $user->data->user_pass, $user->ID)){
381
-
382
- acfe_add_validation_error('', 'Invalid username or password');
383
- return;
384
-
385
- }
386
-
387
- }
388
-
389
- }
390
-
391
- function prepare($form, $current_post_id, $action){
392
-
393
- // Form
394
- $form_name = acf_maybe_get($form, 'form_name');
395
- $form_id = acf_maybe_get($form, 'form_id');
396
-
397
- // Action
398
- $user_action = get_sub_field('acfe_form_user_action');
399
-
400
- // Load values
401
- $load_values = get_sub_field('acfe_form_user_load_values');
402
-
403
- // Pre-process
404
- $_description_group = get_sub_field('acfe_form_user_save_description_group');
405
- $_description = $_description_group['acfe_form_user_save_description'];
406
- $_description_custom = $_description_group['acfe_form_user_save_description_custom'];
407
-
408
- if($_description === 'custom')
409
- $_description = $_description_custom;
410
-
411
- $map = array();
412
-
413
- if($load_values){
414
-
415
- // Mapping
416
- $map = array(
417
- 'user_email' => get_sub_field( 'acfe_form_user_map_email' ),
418
- 'user_login' => get_sub_field( 'acfe_form_user_map_username' ),
419
- 'user_pass' => get_sub_field( 'acfe_form_user_map_password' ),
420
- 'first_name' => get_sub_field( 'acfe_form_user_map_first_name' ),
421
- 'last_name' => get_sub_field( 'acfe_form_user_map_last_name' ),
422
- 'nickname' => get_sub_field( 'acfe_form_user_map_nickname' ),
423
- 'display_name' => get_sub_field( 'acfe_form_user_map_display_name' ),
424
- 'user_url' => get_sub_field( 'acfe_form_user_map_website' ),
425
- 'description' => get_sub_field( 'acfe_form_user_map_description' ),
426
- 'role' => get_sub_field( 'acfe_form_user_map_role' ),
427
- );
428
-
429
- }
430
-
431
- // Fields
432
- $fields = array(
433
- 'target' => get_sub_field('acfe_form_user_save_target'),
434
-
435
- 'login_type' => get_sub_field('acfe_form_user_log_type'),
436
- 'login_user' => get_sub_field('acfe_form_user_save_login_user'),
437
- 'login_pass' => get_sub_field('acfe_form_user_save_login_pass'),
438
- 'login_remember' => get_sub_field('acfe_form_user_save_login_remember'),
439
-
440
- 'user_email' => get_sub_field('acfe_form_user_save_email'),
441
- 'user_login' => get_sub_field('acfe_form_user_save_username'),
442
- 'user_pass' => get_sub_field('acfe_form_user_save_password'),
443
- 'first_name' => get_sub_field('acfe_form_user_save_first_name'),
444
- 'last_name' => get_sub_field('acfe_form_user_save_last_name'),
445
- 'nickname' => get_sub_field('acfe_form_user_save_nickname'),
446
- 'display_name' => get_sub_field('acfe_form_user_save_display_name'),
447
- 'user_url' => get_sub_field('acfe_form_user_save_website'),
448
- 'description' => $_description,
449
- 'role' => get_sub_field('acfe_form_user_save_role'),
450
- );
451
-
452
- $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form);
453
-
454
- // args
455
- $args = array();
456
-
457
- // Insert user
458
- $_user_id = 0;
459
-
460
- // Insert || Update
461
- if($user_action === 'insert_user' || $user_action === 'update_user'){
462
-
463
- // Update user
464
- if($user_action === 'update_user'){
465
-
466
- $_user_id = $data['target'];
467
-
468
- // Invalid User ID
469
- if(!$_user_id)
470
- return;
471
-
472
- // ID
473
- $args['ID'] = $_user_id;
474
-
475
- }
476
-
477
- // Email
478
- if(!empty($data['user_email'])){
479
-
480
- if(is_array($data['user_email']))
481
- $data['user_email'] = acfe_array_to_string($data['user_email']);
482
-
483
- $args['user_email'] = $data['user_email'];
484
-
485
- }
486
-
487
- // Username
488
- if(!empty($data['user_login'])){
489
-
490
- if(is_array($data['user_login']))
491
- $data['user_login'] = acfe_array_to_string($data['user_login']);
492
-
493
- $args['user_login'] = $data['user_login'];
494
-
495
- }
496
-
497
- // Password
498
- if(!empty($data['user_pass'])){
499
-
500
- if(is_array($data['user_pass']))
501
- $data['user_pass'] = acfe_array_to_string($data['user_pass']);
502
-
503
- $args['user_pass'] = $data['user_pass'];
504
-
505
- }
506
-
507
- // First name
508
- if(!empty($data['first_name'])){
509
-
510
- if(is_array($data['first_name']))
511
- $data['first_name'] = acfe_array_to_string($data['first_name']);
512
-
513
- $args['first_name'] = $data['first_name'];
514
-
515
- }
516
-
517
- // Last name
518
- if(!empty($data['last_name'])){
519
-
520
- if(is_array($data['last_name']))
521
- $data['last_name'] = acfe_array_to_string($data['last_name']);
522
-
523
- $args['last_name'] = $data['last_name'];
524
-
525
- }
526
-
527
- // Nickname
528
- if(!empty($data['nickname'])){
529
-
530
- if(is_array($data['nickname']))
531
- $data['nickname'] = acfe_array_to_string($data['nickname']);
532
-
533
- $args['nickname'] = $data['nickname'];
534
-
535
- }
536
-
537
- // Display name
538
- if(!empty($data['display_name'])){
539
-
540
- if(is_array($data['display_name']))
541
- $data['display_name'] = acfe_array_to_string($data['display_name']);
542
-
543
- $args['display_name'] = $data['display_name'];
544
-
545
- }
546
-
547
- // Website
548
- if(!empty($data['user_url'])){
549
-
550
- if(is_array($data['user_url']))
551
- $data['user_url'] = acfe_array_to_string($data['user_url']);
552
-
553
- $args['user_url'] = $data['user_url'];
554
-
555
- }
556
-
557
- // Description
558
- if(!empty($data['description'])){
559
-
560
- if(is_array($data['description']))
561
- $data['description'] = acfe_array_to_string($data['description']);
562
-
563
- $args['description'] = $data['description'];
564
-
565
- }
566
-
567
- // Role
568
- if(!empty($data['role'])){
569
-
570
- if(is_array($data['role']))
571
- $data['role'] = acfe_array_to_string($data['role']);
572
-
573
- $args['role'] = $data['role'];
574
-
575
- }
576
-
577
- $args = apply_filters('acfe/form/submit/user_args', $args, $user_action, $form, $action);
578
- $args = apply_filters('acfe/form/submit/user_args/form=' . $form_name, $args, $user_action, $form, $action);
579
-
580
- if(!empty($action))
581
- $args = apply_filters('acfe/form/submit/user_args/action=' . $action, $args, $user_action, $form, $action);
582
-
583
- if($args === false)
584
- return false;
585
-
586
- // Insert User
587
- if($user_action === 'insert_user'){
588
-
589
- // Bail early if no e-mail
590
- if(!isset($args['user_email']))
591
- return false;
592
-
593
- // No login? Fallback to e-mail
594
- if(!isset($args['user_login']))
595
- $args['user_login'] = $args['user_email'];
596
-
597
- // No password? Fallback to generated password
598
- if(!isset($args['user_pass']))
599
- $args['user_pass'] = wp_generate_password(8, false);
600
-
601
- }
602
-
603
- // Insert User
604
- if($user_action === 'insert_user'){
605
-
606
- $_insert_user = wp_insert_user($args);
607
-
608
- }
609
-
610
- // Update User
611
- elseif($user_action === 'update_user'){
612
-
613
- $_insert_user = wp_update_user($args);
614
-
615
- if(!is_wp_error($_insert_user)){
616
-
617
- $_user_id = $_insert_user;
618
-
619
- // Update User Login + Nicename
620
- if(acf_maybe_get($args, 'user_login')){
621
-
622
- // Sanitize
623
- $sanitized_user_login = sanitize_user($args['user_login'], true);
624
-
625
- // Filter
626
- $pre_user_login = apply_filters('pre_user_login', $sanitized_user_login);
627
-
628
- // Trim
629
- $user_login = trim($pre_user_login);
630
-
631
- $error = false;
632
-
633
- if(empty($user_login)){
634
-
635
- $error = new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.'));
636
-
637
- }elseif(mb_strlen($user_login) > 60){
638
-
639
- $error = new WP_Error('user_login_too_long', __('Username may not be longer than 60 characters.'));
640
-
641
- }
642
-
643
- if(username_exists($user_login)){
644
-
645
- $error = new WP_Error('existing_user_login', __('Sorry, that username already exists!'));
646
-
647
- }
648
-
649
- $user_nicename = sanitize_user($user_login, true);
650
-
651
- if(mb_strlen($user_nicename) > 50){
652
-
653
- $error = new WP_Error('user_nicename_too_long', __('Nicename may not be longer than 50 characters.'));
654
-
655
- }
656
-
657
- $user_nicename = sanitize_title($user_nicename);
658
-
659
- $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
660
-
661
- if(!is_wp_error($error)){
662
-
663
- global $wpdb;
664
-
665
- $wpdb->update($wpdb->users,
666
- array(
667
- 'user_login' => $user_login,
668
- 'user_nicename' => $user_nicename,
669
- ),
670
- array(
671
- 'ID' => $_user_id
672
- )
673
- );
674
-
675
- }
676
-
677
- }
678
-
679
- }
680
-
681
- }
682
-
683
- }
684
-
685
- // Log User
686
- elseif($user_action === 'log_user'){
687
-
688
- $_insert_user = false;
689
-
690
- $_login_user = false;
691
- $_login_pass = false;
692
- $_login_remember = false;
693
-
694
- // Email
695
- if(!empty($data['login_user'])){
696
-
697
- $_login_user = $data['login_user'];
698
-
699
- }
700
-
701
- // Password
702
- if(!empty($data['login_pass'])){
703
-
704
- $_login_pass = $data['login_pass'];
705
-
706
- }
707
-
708
- // Remember me
709
- if(!empty($data['login_remember'])){
710
-
711
- $_login_remember = $data['login_remember'];
712
-
713
- }
714
-
715
- $_login_pass = wp_specialchars_decode($_login_pass);
716
-
717
- // Email
718
- if($data['login_type'] === 'email'){
719
-
720
- $_login_user = sanitize_email($_login_user);
721
- $user = get_user_by('email', $_login_user);
722
-
723
- }
724
-
725
- // Username
726
- elseif($data['login_type'] === 'username'){
727
-
728
- $_login_user = sanitize_user($_login_user);
729
- $user = get_user_by('login', $_login_user);
730
-
731
- }
732
-
733
- // Email || Username
734
- elseif($data['login_type'] === 'email_username'){
735
-
736
- // Email
737
- if(is_email($_login_user)){
738
-
739
- $user = get_user_by('email', $_login_user);
740
-
741
- // Username
742
- }else{
743
-
744
- $user = get_user_by('login', $_login_user);
745
-
746
- }
747
-
748
- }
749
-
750
- $_login_remember = boolval($_login_remember);
751
-
752
- // Login
753
- $_insert_user = wp_signon(array(
754
- 'user_login' => $user->user_login,
755
- 'user_password' => $_login_pass,
756
- 'remember' => $_login_remember
757
- ), is_ssl());
758
-
759
- // User Error
760
- if(is_wp_error($_insert_user))
761
- return;
762
-
763
- $_insert_user = $_insert_user->ID;
764
-
765
- }
766
-
767
- // User Error
768
- if(is_wp_error($_insert_user))
769
- return;
770
-
771
- $_user_id = $_insert_user;
772
-
773
- // Save meta
774
- do_action('acfe/form/submit/user', $_user_id, $user_action, $args, $form, $action);
775
- do_action('acfe/form/submit/user/form=' . $form_name, $_user_id, $user_action, $args, $form, $action);
776
-
777
- if(!empty($action))
778
- do_action('acfe/form/submit/user/action=' . $action, $_user_id, $user_action, $args, $form, $action);
779
-
780
- }
781
-
782
- function submit($_user_id, $user_action, $args, $form, $action){
783
-
784
- if(!empty($action)){
785
-
786
- // Custom Query Var
787
- $custom_query_var = get_sub_field('acfe_form_custom_query_var');
788
-
789
- if(!empty($custom_query_var)){
790
-
791
- // Form name
792
- $form_name = acf_maybe_get($form, 'form_name');
793
-
794
- // Get user array
795
- $user_object = get_user_by('ID', $_user_id);
796
-
797
- if(isset($user_object->data)){
798
-
799
- // return array
800
- $user = json_decode(json_encode($user_object->data), true);
801
-
802
- $user_object_meta = get_user_meta($user['ID']);
803
-
804
- $user_meta = array();
805
-
806
- foreach($user_object_meta as $k => $v){
807
-
808
- if(!isset($v[0]))
809
- continue;
810
-
811
- $user_meta[$k] = $v[0];
812
-
813
- }
814
-
815
- $user_array = array_merge($user, $user_meta);
816
-
817
- $user_array['permalink'] = get_author_posts_url($_user_id);
818
- $user_array['admin_url'] = admin_url('user-edit.php?user_id=' . $_user_id);
819
-
820
- $user_array = apply_filters('acfe/form/query_var/user', $user_array, $_user_id, $user_action, $args, $form, $action);
821
- $user_array = apply_filters('acfe/form/query_var/user/form=' . $form_name, $user_array, $_user_id, $user_action, $args, $form, $action);
822
- $user_array = apply_filters('acfe/form/query_var/user/action=' . $action, $user_array, $_user_id, $user_action, $args, $form, $action);
823
-
824
- set_query_var($action, $user_array);
825
-
826
- }
827
-
828
- }
829
-
830
- }
831
-
832
- // Meta save
833
- $save_meta = get_sub_field('acfe_form_user_save_meta');
834
-
835
- if(!empty($save_meta)){
836
-
837
- $meta = acfe_form_filter_meta($save_meta, $_POST['acf']);
838
-
839
- if(!empty($meta)){
840
-
841
- // Backup original acf post data
842
- $acf = $_POST['acf'];
843
-
844
- // Save meta fields
845
- acf_save_post('user_' . $_user_id, $meta);
846
-
847
- // Restore original acf post data
848
- $_POST['acf'] = $acf;
849
-
850
- }
851
-
852
- }
853
-
854
- }
855
-
856
- /**
857
- * User: Select2 Choices
858
- */
859
- function prepare_choices($field){
860
-
861
- $field['choices']['current_user'] = 'Current User';
862
- $field['choices']['current_post_author'] = 'Current Post Author';
863
-
864
- if(acf_maybe_get($field, 'value')){
865
-
866
- $field_type = acf_get_field_type('user');
867
-
868
- // Clean value into an array of IDs.
869
- $user_ids = array_map('intval', acf_array($field['value']));
870
-
871
- // Find users in database (ensures all results are real).
872
- $users = acf_get_users(array(
873
- 'include' => $user_ids
874
- ));
875
-
876
- // Append.
877
- if($users){
878
-
879
- foreach($users as $user){
880
- $field['choices'][$user->ID] = $field_type->get_result($user, $field);
881
- }
882
-
883
- }
884
-
885
- }
886
-
887
- return $field;
888
-
889
- }
890
-
891
- function advanced_load($field){
892
-
893
- $form_name = 'my_form';
894
-
895
- if(acf_maybe_get($field, 'value'))
896
- $form_name = get_field('acfe_form_name', $field['value']);
897
-
898
- ?>You may use the following hooks:<br /><br />
899
- <pre>
900
- add_filter('acfe/form/load/user_id', 'my_form_user_values_source', 10, 3);
901
- add_filter('acfe/form/load/user_id/form=<?php echo $form_name; ?>', 'my_form_user_values_source', 10, 3);
902
- add_filter('acfe/form/load/user_id/action=my-user-action', 'my_form_user_values_source', 10, 3);
903
- </pre>
904
- <br />
905
- <pre>
906
- /**
907
- * @int $user_id User ID used as source
908
- * @array $form The form settings
909
- * @string $action The action alias name
910
- */
911
- add_filter('acfe/form/load/user_id/form=<?php echo $form_name; ?>', 'my_form_user_values_source', 10, 3);
912
- function my_form_user_values_source($user_id, $form, $action){
913
-
914
- /**
915
- * Force to load values from the user ID 12
916
- */
917
- $user_id = 12;
918
-
919
-
920
- /**
921
- * Return
922
- */
923
- return $user_id;
924
-
925
- }
926
- </pre><?php
927
-
928
- }
929
-
930
- function advanced_save_args($field){
931
-
932
- $form_name = 'my_form';
933
-
934
- if(acf_maybe_get($field, 'value'))
935
- $form_name = get_field('acfe_form_name', $field['value']);
936
-
937
- ?>You may use the following hooks:<br /><br />
938
- <pre>
939
- add_filter('acfe/form/submit/user_args', 'my_form_user_args', 10, 4);
940
- add_filter('acfe/form/submit/user_args/form=<?php echo $form_name; ?>', 'my_form_user_args', 10, 4);
941
- add_filter('acfe/form/submit/user_args/action=my-user-action', 'my_form_user_args', 10, 4);
942
- </pre>
943
- <br />
944
- <pre>
945
- /**
946
- * @array $args The generated user arguments
947
- * @string $type Action type: 'insert_user' or 'update_user'
948
- * @array $form The form settings
949
- * @string $action The action alias name
950
- */
951
- add_filter('acfe/form/submit/user_args/form=<?php echo $form_name; ?>', 'my_form_user_args', 10, 4);
952
- function my_form_user_args($args, $type, $form, $action){
953
-
954
- /**
955
- * Force specific first name if the action type is 'insert_user'
956
- */
957
- if($type === 'insert_user'){
958
-
959
- $args['first_name'] = 'My name';
960
-
961
- }
962
-
963
-
964
- /**
965
- * Get the form input value named 'my_field'
966
- * This is the value entered by the user during the form submission
967
- */
968
- $my_field = get_field('my_field');
969
-
970
-
971
- /**
972
- * Get the field value 'my_field' from the post ID 145
973
- */
974
- $my_post_field = get_field('my_field', 145);
975
-
976
-
977
- /**
978
- * Return arguments
979
- * Note: Return false will stop post & meta insert/update
980
- */
981
- return $args;
982
-
983
- }
984
- </pre><?php
985
-
986
- }
987
-
988
- function advanced_save($field){
989
-
990
- $form_name = 'my_form';
991
-
992
- if(acf_maybe_get($field, 'value'))
993
- $form_name = get_field('acfe_form_name', $field['value']);
994
-
995
- ?>You may use the following hooks:<br /><br />
996
- <pre>
997
- add_action('acfe/form/submit/user', 'my_form_user_save', 10, 5);
998
- add_action('acfe/form/submit/user/form=<?php echo $form_name; ?>', 'my_form_user_save', 10, 5);
999
- add_action('acfe/form/submit/user/action=my-user-action', 'my_form_user_save', 10, 5);
1000
- </pre>
1001
- <br />
1002
- <pre>
1003
- /**
1004
- * @int $user_id The targeted user ID
1005
- * @string $type Action type: 'insert_user' or 'update_user'
1006
- * @array $args The generated user arguments
1007
- * @array $form The form settings
1008
- * @string $action The action alias name
1009
- *
1010
- * Note: At this point the user is already saved into the database
1011
- */
1012
- add_action('acfe/form/submit/user/form=<?php echo $form_name; ?>', 'my_form_user_save', 10, 5);
1013
- function my_form_user_save($user_id, $type, $args, $form, $action){
1014
-
1015
- /**
1016
- * Get the form input value named 'my_field'
1017
- * This is the value entered by the user during the form submission
1018
- */
1019
- $my_field = get_field('my_field');
1020
-
1021
-
1022
- /**
1023
- * Get the field value 'my_field' from the currently saved user
1024
- */
1025
- $my_user_field = get_field('my_field', 'user_' . $user_id);
1026
-
1027
- }
1028
- </pre><?php
1029
-
1030
- }
1031
-
1032
- }
1033
-
1034
- new acfe_form_user();
1035
-
1036
  endif;
1
+ <?php
2
+
3
+ if(!defined('ABSPATH'))
4
+ exit;
5
+
6
+ if(!class_exists('acfe_form_user')):
7
+
8
+ class acfe_form_user{
9
+
10
+ function __construct(){
11
+
12
+ /*
13
+ * Form
14
+ */
15
+ add_filter('acfe/form/load/user', array($this, 'load'), 1, 3);
16
+ add_action('acfe/form/validation/user', array($this, 'validation'), 1, 3);
17
+ add_action('acfe/form/prepare/user', array($this, 'prepare'), 1, 3);
18
+ add_action('acfe/form/submit/user', array($this, 'submit'), 1, 5);
19
+
20
+ /*
21
+ * Admin
22
+ */
23
+ add_filter('acf/prepare_field/name=acfe_form_user_save_meta', array(acfe()->acfe_form, 'map_fields'));
24
+ add_filter('acf/prepare_field/name=acfe_form_user_load_meta', array(acfe()->acfe_form, 'map_fields'));
25
+
26
+ add_filter('acf/prepare_field/name=acfe_form_user_save_login_user', array(acfe()->acfe_form, 'map_fields_deep'));
27
+ add_filter('acf/prepare_field/name=acfe_form_user_save_login_pass', array(acfe()->acfe_form, 'map_fields_deep'));
28
+ add_filter('acf/prepare_field/name=acfe_form_user_save_login_remember', array(acfe()->acfe_form, 'map_fields_deep'));
29
+
30
+ add_filter('acf/prepare_field/name=acfe_form_user_save_target', array(acfe()->acfe_form, 'map_fields_deep'));
31
+ add_filter('acf/prepare_field/name=acfe_form_user_load_source', array(acfe()->acfe_form, 'map_fields_deep'));
32
+
33
+ add_filter('acf/prepare_field/name=acfe_form_user_save_email', array(acfe()->acfe_form, 'map_fields_deep'));
34
+ add_filter('acf/prepare_field/name=acfe_form_user_save_username', array(acfe()->acfe_form, 'map_fields_deep'));
35
+ add_filter('acf/prepare_field/name=acfe_form_user_save_password', array(acfe()->acfe_form, 'map_fields_deep'));
36
+ add_filter('acf/prepare_field/name=acfe_form_user_save_first_name', array(acfe()->acfe_form, 'map_fields_deep'));
37
+ add_filter('acf/prepare_field/name=acfe_form_user_save_last_name', array(acfe()->acfe_form, 'map_fields_deep'));
38
+ add_filter('acf/prepare_field/name=acfe_form_user_save_nickname', array(acfe()->acfe_form, 'map_fields_deep'));
39
+ add_filter('acf/prepare_field/name=acfe_form_user_save_display_name', array(acfe()->acfe_form, 'map_fields_deep'));
40
+ add_filter('acf/prepare_field/name=acfe_form_user_save_website', array(acfe()->acfe_form, 'map_fields_deep'));
41
+ add_filter('acf/prepare_field/name=acfe_form_user_save_description', array(acfe()->acfe_form, 'map_fields_deep'));
42
+ add_filter('acf/prepare_field/name=acfe_form_user_save_role', array(acfe()->acfe_form, 'map_fields_deep'));
43
+
44
+ add_filter('acf/prepare_field/name=acfe_form_user_map_email', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
45
+ add_filter('acf/prepare_field/name=acfe_form_user_map_username', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
46
+ add_filter('acf/prepare_field/name=acfe_form_user_map_password', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
47
+ add_filter('acf/prepare_field/name=acfe_form_user_map_first_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
48
+ add_filter('acf/prepare_field/name=acfe_form_user_map_last_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
49
+ add_filter('acf/prepare_field/name=acfe_form_user_map_nickname', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
50
+ add_filter('acf/prepare_field/name=acfe_form_user_map_display_name', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
51
+ add_filter('acf/prepare_field/name=acfe_form_user_map_website', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
52
+ add_filter('acf/prepare_field/name=acfe_form_user_map_description', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
53
+ add_filter('acf/prepare_field/name=acfe_form_user_map_role', array(acfe()->acfe_form, 'map_fields_deep_no_custom'));
54
+
55
+ add_filter('acf/prepare_field/name=acfe_form_user_save_target', array($this, 'prepare_choices'), 5);
56
+ add_filter('acf/prepare_field/name=acfe_form_user_load_source', array($this, 'prepare_choices'), 5);
57
+
58
+ add_action('acf/render_field/name=acfe_form_user_advanced_load', array($this, 'advanced_load'));
59
+ add_action('acf/render_field/name=acfe_form_user_advanced_save_args', array($this, 'advanced_save_args'));
60
+ add_action('acf/render_field/name=acfe_form_user_advanced_save', array($this, 'advanced_save'));
61
+
62
+ }
63
+
64
+ function load($form, $current_post_id, $action){
65
+
66
+ // Form
67
+ $form_name = acf_maybe_get($form, 'form_name');
68
+ $form_id = acf_maybe_get($form, 'form_id');
69
+
70
+ // Action
71
+ $user_action = get_sub_field('acfe_form_user_action');
72
+
73
+ // Bail early if Log user
74
+ if($user_action === 'log_user')
75
+ return $form;
76
+
77
+ // Load values
78
+ $load_values = get_sub_field('acfe_form_user_load_values');
79
+ $load_meta = get_sub_field('acfe_form_user_load_meta');
80
+
81
+ // Load values
82
+ if(!$load_values)
83
+ return $form;
84
+
85
+ $_user_id = get_sub_field('acfe_form_user_load_source');
86
+ $_email = get_sub_field('acfe_form_user_map_email');
87
+ $_username = get_sub_field('acfe_form_user_map_username');
88
+ $_password = get_sub_field('acfe_form_user_map_password');
89
+ $_first_name = get_sub_field('acfe_form_user_map_first_name');
90
+ $_last_name = get_sub_field('acfe_form_user_map_last_name');
91
+ $_nickname = get_sub_field('acfe_form_user_map_nickname');
92
+ $_display_name = get_sub_field('acfe_form_user_map_display_name');
93
+ $_website = get_sub_field('acfe_form_user_map_website');
94
+ $_description = get_sub_field('acfe_form_user_map_description');
95
+ $_role = get_sub_field('acfe_form_user_map_role');
96
+
97
+ // Map {field:name} {get_field:name} {query_var:name}
98
+ $_user_id = acfe_form_map_field_value_load($_user_id, $current_post_id, $form);
99
+ $_email = acfe_form_map_field_value_load($_email, $current_post_id, $form);
100
+ $_username = acfe_form_map_field_value_load($_username, $current_post_id, $form);
101
+ $_password = acfe_form_map_field_value_load($_password, $current_post_id, $form);
102
+ $_first_name = acfe_form_map_field_value_load($_first_name, $current_post_id, $form);
103
+ $_last_name = acfe_form_map_field_value_load($_last_name, $current_post_id, $form);
104
+ $_nickname = acfe_form_map_field_value_load($_nickname, $current_post_id, $form);
105
+ $_display_name = acfe_form_map_field_value_load($_display_name, $current_post_id, $form);
106
+ $_website = acfe_form_map_field_value_load($_website, $current_post_id, $form);
107
+ $_description = acfe_form_map_field_value_load($_description, $current_post_id, $form);
108
+ $_role = acfe_form_map_field_value_load($_role, $current_post_id, $form);
109
+
110
+ $_user_id = apply_filters('acfe/form/load/user_id', $_user_id, $form, $action);
111
+ $_user_id = apply_filters('acfe/form/load/user_id/form=' . $form_name, $_user_id, $form, $action);
112
+
113
+ if(!empty($action))
114
+ $_user_id = apply_filters('acfe/form/load/user_id/action=' . $action, $_user_id, $form, $action);
115
+
116
+ // Invalid User ID
117
+ if(!$_user_id)
118
+ return $form;
119
+
120
+ $user_data = get_userdata($_user_id);
121
+
122
+ // Check if userdata has been found
123
+ if(!$user_data)
124
+ return $form;
125
+
126
+ // Email
127
+ if(acf_is_field_key($_email)){
128
+
129
+ $key = array_search($_email, $load_meta);
130
+
131
+ if($key !== false)
132
+ unset($load_meta[$key]);
133
+
134
+ $form['map'][$_email]['value'] = $user_data->user_email;
135
+
136
+ }
137
+
138
+ // Username
139
+ if(acf_is_field_key($_username)){
140
+
141
+ $key = array_search($_username, $load_meta);
142
+
143
+ if($key !== false)
144
+ unset($load_meta[$key]);
145
+
146
+ $form['map'][$_username]['value'] = $user_data->user_login;
147
+ $form['map'][$_username]['maxlength'] = 60;
148
+
149
+ }
150
+
151
+ // Password
152
+ if(acf_is_field_key($_password)){
153
+
154
+ $key = array_search($_password, $load_meta);
155
+
156
+ if($key !== false)
157
+ unset($load_meta[$key]);
158
+
159
+ //$form['map'][$_password]['value'] = $user_data->user_pass;
160
+
161
+ }
162
+
163
+ // First name
164
+ if(acf_is_field_key($_first_name)){
165
+
166
+ $key = array_search($_first_name, $load_meta);
167
+
168
+ if($key !== false)
169
+ unset($load_meta[$key]);
170
+
171
+ $form['map'][$_first_name]['value'] = $user_data->first_name;
172
+
173
+ }
174
+
175
+ // Last name
176
+ if(acf_is_field_key($_last_name)){
177
+
178
+ $key = array_search($_last_name, $load_meta);
179
+
180
+ if($key !== false)
181
+ unset($load_meta[$key]);
182
+
183
+ $form['map'][$_last_name]['value'] = $user_data->last_name;
184
+
185
+ }
186
+
187
+ // Nickname
188
+ if(acf_is_field_key($_nickname)){
189
+
190
+ $key = array_search($_nickname, $load_meta);
191
+
192
+ if($key !== false)
193
+ unset($load_meta[$key]);
194
+
195
+ $form['map'][$_nickname]['value'] = $user_data->nickname;
196
+
197
+ }
198
+
199
+ // Display name
200
+ if(acf_is_field_key($_display_name)){
201
+
202
+ $key = array_search($_display_name, $load_meta);
203
+
204
+ if($key !== false)
205
+ unset($load_meta[$key]);
206
+
207
+ $form['map'][$_display_name]['value'] = $user_data->display_name;
208
+
209
+ }
210
+
211
+ // Website
212
+ if(acf_is_field_key($_website)){
213
+
214
+ $key = array_search($_website, $load_meta);
215
+
216
+ if($key !== false)
217
+ unset($load_meta[$key]);
218
+
219
+ $form['map'][$_website]['value'] = $user_data->website;
220
+
221
+ }
222
+
223
+ // Description
224
+ if(acf_is_field_key($_description)){
225
+
226
+ $key = array_search($_description, $load_meta);
227
+
228
+ if($key !== false)
229
+ unset($load_meta[$key]);
230
+
231
+ $form['map'][$_description]['value'] = $user_data->description;
232
+
233
+ }
234
+
235
+ // Role
236
+ if(acf_is_field_key($_role)){
237
+
238
+ $key = array_search($_role, $load_meta);
239
+
240
+ if($key !== false)
241
+ unset($load_meta[$key]);
242
+
243
+ $form['map'][$_role]['value'] = implode(', ', $user_data->roles);
244
+
245
+ }
246
+
247
+ // Load others values
248
+ if(!empty($load_meta)){
249
+
250
+ foreach($load_meta as $field_key){
251
+
252
+ $field = acf_get_field($field_key);
253
+
254
+ $form['map'][$field_key]['value'] = acf_get_value('user_' . $_user_id, $field);
255
+
256
+ }
257
+
258
+ }
259
+
260
+ return $form;
261
+
262
+ }
263
+
264
+ function validation($form, $current_post_id, $action){
265
+
266
+ // Action
267
+ $user_action = get_sub_field('acfe_form_user_action');
268
+
269
+ if($user_action !== 'log_user')
270
+ return;
271
+
272
+ // Form
273
+ $form_name = acf_maybe_get($form, 'form_name');
274
+ $form_id = acf_maybe_get($form, 'form_id');
275
+
276
+ // Fields
277
+ $data = array(
278
+ 'type' => get_sub_field('acfe_form_user_log_type'),
279
+ 'login' => get_sub_field('acfe_form_user_save_login_user'),
280
+ 'pass' => get_sub_field('acfe_form_user_save_login_pass'),
281
+ );
282
+
283
+ $data['login'] = acfe_form_map_field_value($data['login'], $current_post_id, $form);
284
+ $data['pass'] = acfe_form_map_field_value($data['pass'], $current_post_id, $form);
285
+
286
+ $login = false;
287
+ $pass = false;
288
+
289
+ // Email
290
+ if(!empty($data['login'])){
291
+
292
+ $login = $data['login'];
293
+
294
+ }
295
+
296
+ // Password
297
+ if(!empty($data['pass'])){
298
+
299
+ $pass = $data['pass'];
300
+
301
+ }
302
+
303
+ $pass = wp_specialchars_decode($pass);
304
+
305
+ if(empty($login) || empty($pass)){
306
+
307
+ acfe_add_validation_error('', 'An error has occured. Please try again');
308
+ return;
309
+
310
+ }
311
+
312
+ // Email
313
+ if($data['type'] === 'email'){
314
+
315
+ $this->validate_user_login('email', $login, $pass);
316
+
317
+ }
318
+
319
+ // Username
320
+ elseif($data['type'] === 'username'){
321
+
322
+ $this->validate_user_login('username', $login, $pass);
323
+
324
+ }
325
+
326
+ // Email || Username
327
+ elseif($data['type'] === 'email_username'){
328
+
329
+ // Email
330
+ if(is_email($login)){
331
+
332
+ $this->validate_user_login('email', $login, $pass);
333
+
334
+ // Username
335
+ }else{
336
+
337
+ $this->validate_user_login('username', $login, $pass);
338
+
339
+ }
340
+
341
+ }
342
+
343
+ }
344
+
345
+ function validate_user_login($type = 'email', $login, $pass){
346
+
347
+ if($type === 'email'){
348
+
349
+ $login = sanitize_email($login);
350
+
351
+ if(empty($login) || !is_email($login)){
352
+
353
+ acfe_add_validation_error('', 'Invalid e-mail');
354
+ return;
355
+
356
+ }
357
+
358
+ $user = get_user_by('email', $login);
359
+
360
+ if(!$user || !wp_check_password($pass, $user->data->user_pass, $user->ID)){
361
+
362
+ acfe_add_validation_error('', 'Invalid e-mail or password');
363
+ return;
364
+
365
+ }
366
+
367
+ }elseif($type === 'username'){
368
+
369
+ $login = sanitize_user($login);
370
+
371
+ if(empty($login)){
372
+
373
+ acfe_add_validation_error('', 'Invalid username');
374
+ return;
375
+
376
+ }
377
+
378
+ $user = get_user_by('login', $login);
379
+
380
+ if(!$user || !wp_check_password($pass, $user->data->user_pass, $user->ID)){
381
+
382
+ acfe_add_validation_error('', 'Invalid username or password');
383
+ return;
384
+
385
+ }
386
+
387
+ }
388
+
389
+ }
390
+
391
+ function prepare($form, $current_post_id, $action){
392
+
393
+ // Form
394
+ $form_name = acf_maybe_get($form, 'form_name');
395
+ $form_id = acf_maybe_get($form, 'form_id');
396
+
397
+ // Action
398
+ $user_action = get_sub_field('acfe_form_user_action');
399
+
400
+ // Load values
401
+ $load_values = get_sub_field('acfe_form_user_load_values');
402
+
403
+ // Pre-process
404
+ $_description_group = get_sub_field('acfe_form_user_save_description_group');
405
+ $_description = $_description_group['acfe_form_user_save_description'];
406
+ $_description_custom = $_description_group['acfe_form_user_save_description_custom'];
407
+
408
+ if($_description === 'custom')
409
+ $_description = $_description_custom;
410
+
411
+ $map = array();
412
+
413
+ if($load_values){
414
+
415
+ // Mapping
416
+ $map = array(
417
+ 'user_email' => get_sub_field( 'acfe_form_user_map_email' ),
418
+ 'user_login' => get_sub_field( 'acfe_form_user_map_username' ),
419
+ 'user_pass' => get_sub_field( 'acfe_form_user_map_password' ),
420
+ 'first_name' => get_sub_field( 'acfe_form_user_map_first_name' ),
421
+ 'last_name' => get_sub_field( 'acfe_form_user_map_last_name' ),
422
+ 'nickname' => get_sub_field( 'acfe_form_user_map_nickname' ),
423
+ 'display_name' => get_sub_field( 'acfe_form_user_map_display_name' ),
424
+ 'user_url' => get_sub_field( 'acfe_form_user_map_website' ),
425
+ 'description' => get_sub_field( 'acfe_form_user_map_description' ),
426
+ 'role' => get_sub_field( 'acfe_form_user_map_role' ),
427
+ );
428
+
429
+ }
430
+
431
+ // Fields
432
+ $fields = array(
433
+ 'target' => get_sub_field('acfe_form_user_save_target'),
434
+
435
+ 'login_type' => get_sub_field('acfe_form_user_log_type'),
436
+ 'login_user' => get_sub_field('acfe_form_user_save_login_user'),
437
+ 'login_pass' => get_sub_field('acfe_form_user_save_login_pass'),
438
+ 'login_remember' => get_sub_field('acfe_form_user_save_login_remember'),
439
+
440
+ 'user_email' => get_sub_field('acfe_form_user_save_email'),
441
+ 'user_login' => get_sub_field('acfe_form_user_save_username'),
442
+ 'user_pass' => get_sub_field('acfe_form_user_save_password'),
443
+ 'first_name' => get_sub_field('acfe_form_user_save_first_name'),
444
+ 'last_name' => get_sub_field('acfe_form_user_save_last_name'),
445
+ 'nickname' => get_sub_field('acfe_form_user_save_nickname'),
446
+ 'display_name' => get_sub_field('acfe_form_user_save_display_name'),
447
+ 'user_url' => get_sub_field('acfe_form_user_save_website'),
448
+ 'description' => $_description,
449
+ 'role' => get_sub_field('acfe_form_user_save_role'),
450
+ );
451
+
452
+ $data = acfe_form_map_vs_fields($map, $fields, $current_post_id, $form);
453
+
454
+ // args
455
+ $args = array();
456
+
457
+ // Insert user
458
+ $_user_id = 0;
459
+
460
+ // Insert || Update
461
+ if($user_action === 'insert_user' || $user_action === 'update_user'){
462
+
463
+ // Update user
464
+ if($user_action === 'update_user'){
465
+
466
+ $_user_id = $data['target'];
467
+
468
+ // Invalid User ID
469
+ if(!$_user_id)
470
+ return;
471
+
472
+ // ID
473
+ $args['ID'] = $_user_id;
474
+
475
+ }
476
+
477
+ // Email
478
+ if(!empty($data['user_email'])){
479
+
480
+ if(is_array($data['user_email']))
481
+ $data['user_email'] = acfe_array_to_string($data['user_email']);
482
+
483
+ $args['user_email'] = $data['user_email'];
484
+
485
+ }
486
+
487
+ // Username
488
+ if(!empty($data['user_login'])){
489
+
490
+ if(is_array($data['user_login']))
491
+ $data['user_login'] = acfe_array_to_string($data['user_login']);
492
+
493
+ $args['user_login'] = $data['user_login'];
494
+
495
+ }
496
+
497
+ // Password
498
+ if(!empty($data['user_pass'])){
499
+
500
+ if(is_array($data['user_pass']))
501
+ $data['user_pass'] = acfe_array_to_string($data['user_pass']);
502
+
503
+ $args['user_pass'] = $data['user_pass'];
504
+
505
+ }
506
+
507
+ // First name
508
+ if(!empty($data['first_name'])){
509
+
510
+ if(is_array($data['first_name']))
511
+ $data['first_name'] = acfe_array_to_string($data['first_name']);
512
+
513
+ $args['first_name'] = $data['first_name'];
514
+
515
+ }
516
+
517
+ // Last name
518
+ if(!empty($data['last_name'])){
519
+
520
+ if(is_array($data['last_name']))
521
+ $data['last_name'] = acfe_array_to_string($data['last_name']);
522
+
523
+ $args['last_name'] = $data['last_name'];
524
+
525
+ }
526
+
527
+ // Nickname
528
+ if(!empty($data['nickname'])){
529
+
530
+ if(is_array($data['nickname']))
531
+ $data['nickname'] = acfe_array_to_string($data['nickname']);
532
+
533
+ $args['nickname'] = $data['nickname'];
534
+
535
+ }
536
+
537
+ // Display name
538
+ if(!empty($data['display_name'])){
539
+
540
+ if(is_array($data['display_name']))
541
+ $data['display_name'] = acfe_array_to_string($data['display_name']);
542
+
543
+ $args['display_name'] = $data['display_name'];
544
+
545
+ }
546
+
547
+ // Website
548
+ if(!empty($data['user_url'])){
549
+
550
+ if(is_array($data['user_url']))
551
+ $data['user_url'] = acfe_array_to_string($data['user_url']);
552
+
553
+ $args['user_url'] = $data['user_url'];
554
+
555
+ }
556
+
557
+ // Description
558
+ if(!empty($data['description'])){
559
+
560
+ if(is_array($data['description']))
561
+ $data['description'] = acfe_array_to_string($data['description']);
562
+
563
+ $args['description'] = $data['description'];
564
+
565
+ }
566
+
567
+ // Role
568
+ if(!empty($data['role'])){
569
+
570
+ if(is_array($data['role']))
571
+ $data['role'] = acfe_array_to_string($data['role']);
572
+
573
+ $args['role'] = $data['role'];
574
+
575
+ }
576
+
577
+ $args = apply_filters('acfe/form/submit/user_args', $args, $user_action, $form, $action);
578
+ $args = apply_filters('acfe/form/submit/user_args/form=' . $form_name, $args, $user_action, $form, $action);
579
+
580
+ if(!empty($action))
581
+ $args = apply_filters('acfe/form/submit/user_args/action=' . $action, $args, $user_action, $form, $action);
582
+
583
+ if($args === false)
584
+ return false;
585
+
586
+ // Insert User
587
+ if($user_action === 'insert_user'){
588
+
589
+ // Bail early if no e-mail
590
+ if(!isset($args['user_email']))
591
+ return false;
592
+
593
+ // No login? Fallback to e-mail
594
+ if(!isset($args['user_login']))
595
+ $args['user_login'] = $args['user_email'];
596
+
597
+ // No password? Fallback to generated password
598
+ if(!isset($args['user_pass']))
599
+ $args['user_pass'] = wp_generate_password(8, false);
600
+
601
+ }
602
+
603
+ // Insert User
604
+ if($user_action === 'insert_user'){
605
+
606
+ $_insert_user = wp_insert_user($args);
607
+
608
+ }
609
+
610
+ // Update User
611
+ elseif($user_action === 'update_user'){
612
+
613
+ $_insert_user = wp_update_user($args);
614
+
615
+ if(!is_wp_error($_insert_user)){
616
+
617
+ $_user_id = $_insert_user;
618
+
619
+ // Update User Login + Nicename
620
+ if(acf_maybe_get($args, 'user_login')){
621
+
622
+ // Sanitize
623
+ $sanitized_user_login = sanitize_user($args['user_login'], true);
624
+
625
+ // Filter
626
+ $pre_user_login = apply_filters('pre_user_login', $sanitized_user_login);
627
+
628
+ // Trim
629
+ $user_login = trim($pre_user_login);
630
+
631
+ $error = false;
632
+
633
+ if(empty($user_login)){
634
+
635
+ $error = new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.'));
636
+
637
+ }elseif(mb_strlen($user_login) > 60){
638
+
639
+ $error = new WP_Error('user_login_too_long', __('Username may not be longer than 60 characters.'));
640
+
641
+ }
642
+
643
+ if(username_exists($user_login)){
644
+
645
+ $error = new WP_Error('existing_user_login', __('Sorry, that username already exists!'));
646
+
647
+ }
648
+
649
+ $user_nicename = sanitize_user($user_login, true);
650
+
651
+ if(mb_strlen($user_nicename) > 50){
652
+
653
+ $error = new WP_Error('user_nicename_too_long', __('Nicename may not be longer than 50 characters.'));
654
+
655
+ }
656
+
657
+ $user_nicename = sanitize_title($user_nicename);
658
+
659
+ $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
660
+
661
+ if(!is_wp_error($error)){
662
+
663
+ global $wpdb;
664
+
665
+ $wpdb->update($wpdb->users,
666
+ array(
667
+ 'user_login' => $user_login,
668
+ 'user_nicename' => $user_nicename,
669
+ ),
670
+ array(
671
+ 'ID' => $_user_id
672
+ )
673
+ );
674
+
675
+ }
676
+
677
+ }
678
+
679
+ }
680
+
681
+ }
682
+
683
+ }
684
+
685
+ // Log User
686
+ elseif($user_action === 'log_user'){
687
+
688
+ $_insert_user = false;
689
+
690
+ $_login_user = false;
691
+ $_login_pass = false;
692
+ $_login_remember = false;
693
+
694
+ // Email
695
+ if(!empty($data['login_user'])){
696
+
697
+ $_login_user = $data['login_user'];
698
+
699
+ }
700
+
701
+ // Password
702
+ if(!empty($data['login_pass'])){
703
+
704
+ $_login_pass = $data['login_pass'];
705
+
706
+ }
707
+
708
+ // Remember me
709
+ if(!empty($data['login_remember'])){
710
+
711
+ $_login_remember = $data['login_remember'];
712
+
713
+ }
714
+
715
+ $_login_pass = wp_specialchars_decode($_login_pass);
716
+
717
+ // Email
718
+ if($data['login_type'] === 'email'){
719
+
720
+ $_login_user = sanitize_email($_login_user);
721
+ $user = get_user_by('email', $_login_user);
722
+
723
+ }
724
+
725
+ // Username
726
+ elseif($data['login_type'] === 'username'){
727
+
728
+ $_login_user = sanitize_user($_login_user);
729
+ $user = get_user_by('login', $_login_user);
730
+
731
+ }
732
+
733
+ // Email || Username
734
+ elseif($data['login_type'] === 'email_username'){
735
+
736
+ // Email
737
+ if(is_email($_login_user)){
738
+
739
+ $user = get_user_by('email', $_login_user);
740
+
741
+ // Username
742
+ }else{
743
+
744
+ $user = get_user_by('login', $_login_user);
745
+
746
+ }
747
+
748
+ }
749
+
750
+ $_login_remember = boolval($_login_remember);
751
+
752
+ // Login
753
+ $_insert_user = wp_signon(array(
754
+ 'user_login' => $user->user_login,
755
+ 'user_password' => $_login_pass,
756
+ 'remember' => $_login_remember
757
+ ), is_ssl());
758
+
759
+ // User Error
760
+ if(is_wp_error($_insert_user))
761
+ return;
762
+
763
+ $_insert_user = $_insert_user->ID;
764
+
765
+ }
766
+
767
+ // User Error
768
+ if(is_wp_error($_insert_user))
769
+ return;
770
+
771
+ $_user_id = $_insert_user;
772
+
773
+ // Save meta
774
+ do_action('acfe/form/submit/user', $_user_id, $user_action, $args, $form, $action);
775
+ do_action('acfe/form/submit/user/form=' . $form_name, $_user_id, $user_action, $args, $form, $action);
776
+
777
+ if(!empty($action))
778
+ do_action('acfe/form/submit/user/action=' . $action, $_user_id, $user_action, $args, $form, $action);
779
+
780
+ }
781
+
782
+ function submit($_user_id, $user_action, $args, $form, $action){
783
+
784
+ if(!empty($action)){
785
+
786
+ // Custom Query Var
787
+ $custom_query_var = get_sub_field('acfe_form_custom_query_var');
788
+
789
+ if(!empty($custom_query_var)){
790
+
791
+ // Form name
792
+ $form_name = acf_maybe_get($form, 'form_name');
793
+
794
+ // Get user array
795
+ $user_object = get_user_by('ID', $_user_id);
796
+
797
+ if(isset($user_object->data)){
798
+
799
+ // return array
800
+ $user = json_decode(json_encode($user_object->data), true);
801
+
802
+ $user_object_meta = get_user_meta($user['ID']);
803
+
804
+ $user_meta = array();
805
+
806
+ foreach($user_object_meta as $k => $v){
807
+
808
+ if(!isset($v[0]))
809
+ continue;
810
+
811
+ $user_meta[$k] = $v[0];
812
+
813
+ }
814
+
815
+ $user_array = array_merge($user, $user_meta);
816
+
817
+ $user_array['permalink'] = get_author_posts_url($_user_id);
818
+ $user_array['admin_url'] = admin_url('user-edit.php?user_id=' . $_user_id);
819
+
820
+ $user_array = apply_filters('acfe/form/query_var/user', $user_array, $_user_id, $user_action, $args, $form, $action);
821
+ $user_array = apply_filters('acfe/form/query_var/user/form=' . $form_name, $user_array, $_user_id, $user_action, $args, $form, $action);
822
+ $user_array = apply_filters('acfe/form/query_var/user/action=' . $action, $user_array, $_user_id, $user_action, $args, $form, $action);
823
+
824
+ set_query_var($action, $user_array);
825
+
826
+ }
827
+
828
+ }
829
+
830
+ }
831
+
832
+ // Meta save
833
+ $save_meta = get_sub_field('acfe_form_user_save_meta');
834
+
835
+ if(!empty($save_meta)){
836
+
837
+ $meta = acfe_form_filter_meta($save_meta, $_POST['acf']);
838
+
839
+ if(!empty($meta)){
840
+
841
+ // Backup original acf post data
842
+ $acf = $_POST['acf'];
843
+
844
+ // Save meta fields
845
+ acf_save_post('user_' . $_user_id, $meta);
846
+
847
+ // Restore original acf post data
848
+ $_POST['acf'] = $acf;
849
+
850
+ }
851
+
852
+ }
853
+
854
+ }
855
+
856
+ /**
857
+ * User: Select2 Choices
858
+ */
859
+ function prepare_choices($field){
860
+
861
+ $field['choices']['current_user'] = 'Current User';
862
+ $field['choices']['current_post_author'] = 'Current Post Author';
863
+
864
+ if(acf_maybe_get($field, 'value')){
865
+
866
+ $field_type = acf_get_field_type('user');
867
+
868
+ // Clean value into an array of IDs.
869
+ $user_ids = array_map('intval', acf_array($field['value']));
870
+
871
+ // Find users in database (ensures all results are real).
872
+ $users = acf_get_users(array(
873
+ 'include' => $user_ids
874
+ ));
875
+
876
+ // Append.
877
+ if($users){
878
+
879
+ foreach($users as $user){
880
+ $field['choices'][$user->ID] = $field_type->get_result($user, $field);
881
+ }
882
+
883
+ }
884
+
885
+ }
886
+
887
+ return $field;
888
+
889
+ }
890
+
891
+ function advanced_load($field){
892
+
893
+ $form_name = 'my_form';
894
+
895
+ if(acf_maybe_get($field, 'value'))
896
+ $form_name = get_field('acfe_form_name', $field['value']);
897
+
898
+ ?>You may use the following hooks:<br /><br />
899
+ <pre>
900
+ add_filter('acfe/form/load/user_id', 'my_form_user_values_source', 10, 3);
901
+ add_filter('acfe/form/load/user_id/form=<?php echo $form_name; ?>', 'my_form_user_values_source', 10, 3);
902
+ add_filter('acfe/form/load/user_id/action=my-user-action', 'my_form_user_values_source', 10, 3);
903
+ </pre>
904
+ <br />
905
+ <pre>
906
+ /**
907
+ * @int $user_id User ID used as source
908
+ * @array $form The form settings
909
+ * @string $action The action alias name
910
+ */
911
+ add_filter('acfe/form/load/user_id/form=<?php echo $form_name; ?>', 'my_form_user_values_source', 10, 3);
912
+ function my_form_user_values_source($user_id, $form, $action){
913
+
914
+ /**
915
+ * Force to load values from the user ID 12
916
+ */
917
+ $user_id = 12;
918
+
919
+
920
+ /**
921
+ * Return
922
+ */
923
+ return $user_id;
924
+
925
+ }
926
+ </pre><?php
927
+
928
+ }
929
+
930
+ function advanced_save_args($field){
931
+
932
+ $form_name = 'my_form';
933
+
934
+ if(acf_maybe_get($field, 'value'))
935
+ $form_name = get_field('acfe_form_name', $field['value']);
936
+
937
+ ?>You may use the following hooks:<br /><br />
938
+ <pre>
939
+ add_filter('acfe/form/submit/user_args', 'my_form_user_args', 10, 4);
940
+ add_filter('acfe/form/submit/user_args/form=<?php echo $form_name; ?>', 'my_form_user_args', 10, 4);
941
+ add_filter('acfe/form/submit/user_args/action=my-user-action', 'my_form_user_args', 10, 4);
942
+ </pre>
943
+ <br />
944
+ <pre>
945
+ /**
946
+ * @array $args The generated user arguments
947
+ * @string $type Action type: 'insert_user' or 'update_user'
948
+ * @array $form The form settings
949
+ * @string $action The action alias name
950
+ */
951
+ add_filter('acfe/form/submit/user_args/form=<?php echo $form_name; ?>', 'my_form_user_args', 10, 4);
952
+ function my_form_user_args($args, $type, $form, $action){
953
+
954
+ /**
955
+ * Force specific first name if the action type is 'insert_user'
956
+ */
957
+ if($type === 'insert_user'){
958
+
959
+ $args['first_name'] = 'My name';
960
+
961
+ }
962
+
963
+
964
+ /**
965
+ * Get the form input value named 'my_field'
966
+ * This is the value entered by the user during the form submission
967
+ */
968
+ $my_field = get_field('my_field');
969
+
970
+
971
+ /**
972
+ * Get the field value 'my_field' from the post ID 145
973
+ */
974
+ $my_post_field = get_field('my_field', 145);
975
+
976
+
977
+ /**
978
+ * Return arguments
979
+ * Note: Return false will stop post & meta insert/update
980
+ */
981
+ return $args;
982
+
983
+ }
984
+ </pre><?php
985
+
986
+ }
987
+
988
+ function advanced_save($field){
989
+
990
+ $form_name = 'my_form';
991
+
992
+ if(acf_maybe_get($field, 'value'))
993
+ $form_name = get_field('acfe_form_name', $field['value']);
994
+
995
+ ?>You may use the following hooks:<br /><br />
996
+ <pre>
997
+ add_action('acfe/form/submit/user', 'my_form_user_save', 10, 5);
998
+ add_action('acfe/form/submit/user/form=<?php echo $form_name; ?>', 'my_form_user_save', 10, 5);
999
+ add_action('acfe/form/submit/user/action=my-user-action', 'my_form_user_save', 10, 5);
1000
+ </pre>
1001
+ <br />
1002
+ <pre>
1003
+ /**
1004
+ * @int $user_id The targeted user ID
1005
+ * @string $type Action type: 'insert_user' or 'update_user'
1006
+ * @array $args The generated user arguments
1007
+ * @array $form The form settings
1008
+ * @string $action The action alias name
1009
+ *
1010
+ * Note: At this point the user is already saved into the database
1011
+ */
1012
+ add_action('acfe/form/submit/user/form=<?php echo $form_name; ?>', 'my_form_user_save', 10, 5);
1013
+ function my_form_user_save($user_id, $type, $args, $form, $action){
1014
+
1015
+ /**
1016
+ * Get the form input value named 'my_field'
1017
+ * This is the value entered by the user during the form submission
1018
+ */
1019
+ $my_field = get_field('my_field');
1020
+
1021
+
1022
+ /**
1023
+ * Get the field value 'my_field' from the currently saved user
1024
+ */
1025
+ $my_user_field = get_field('my_field', 'user_' . $user_id);
1026
+
1027
+ }
1028
+ </pre><?php
1029
+
1030
+ }
1031
+
1032
+ }
1033
+
1034
+ new acfe_form_user();
1035
+
1036
  endif;
includes/modules/form/admin.php CHANGED
@@ -1142,7 +1142,7 @@ class acfe_form{
1142
 
1143
  ob_start();
1144
 
1145
- acf_render_fields($fields, false, $args['field_el'], $args['instruction_placement']);
1146
 
1147
  $render_field = ob_get_clean();
1148
 
@@ -1195,7 +1195,7 @@ class acfe_form{
1195
 
1196
  ob_start();
1197
 
1198
- acf_render_fields($fields, false, $args['field_el'], $args['instruction_placement']);
1199
 
1200
  $render_fields = ob_get_clean();
1201
 
1142
 
1143
  ob_start();
1144
 
1145
+ acf_render_fields($fields, acf_uniqid('acfe_form'), $args['field_el'], $args['instruction_placement']);
1146
 
1147
  $render_field = ob_get_clean();
1148
 
1195
 
1196
  ob_start();
1197
 
1198
+ acf_render_fields($fields, acf_uniqid('acfe_form'), $args['field_el'], $args['instruction_placement']);
1199
 
1200
  $render_fields = ob_get_clean();
1201
 
includes/modules/form/form-front.php CHANGED
@@ -374,6 +374,12 @@ class acfe_form_front{
374
  if(!empty($args['errors_class']))
375
  $args['form_attributes']['data-errors-class'] = $args['errors_class'];
376
 
 
 
 
 
 
 
377
  // Args
378
  $args = apply_filters('acfe/form/load', $args, $args['post_id']);
379
  $args = apply_filters('acfe/form/load/form=' . $form_name, $args, $args['post_id']);
@@ -406,6 +412,12 @@ class acfe_form_front{
406
  endwhile;
407
  endif;
408
 
 
 
 
 
 
 
409
  return $args;
410
 
411
  }
@@ -446,23 +458,7 @@ class acfe_form_front{
446
 
447
  if(acf_maybe_get($form, 'form_name') === $args['form_name']){
448
 
449
- ?>
450
- <script>
451
- (function($){
452
-
453
- if(typeof acf === 'undefined')
454
- return;
455
-
456
- acf.addAction('prepare', function(){
457
-
458
- acf.doAction('acfe/form/submit/success');
459
- acf.doAction('acfe/form/submit/success/name=<?php echo $args['form_name']; ?>');
460
-
461
- });
462
-
463
- })(jQuery);
464
- </script>
465
- <?php
466
 
467
  if(!empty($args['updated_message'])){
468
 
@@ -488,14 +484,6 @@ class acfe_form_front{
488
 
489
  if($args['updated_hide_form']){
490
 
491
- ?>
492
- <script>
493
- if(window.history.replaceState){
494
- window.history.replaceState(null, null, window.location.href);
495
- }
496
- </script>
497
- <?php
498
-
499
  return;
500
 
501
  }
@@ -554,12 +542,16 @@ class acfe_form_front{
554
 
555
  }
556
 
557
- // display form
558
- if($args['form']): ?>
559
 
560
- <form <?php acf_esc_attr_e($args['form_attributes']); ?>>
 
 
 
 
 
561
 
562
- <?php endif;
563
 
564
  // render post data
565
  acf_form_data(array(
@@ -675,7 +667,7 @@ class acfe_form_front{
675
 
676
  }
677
 
678
- acf_render_fields($fields, false, $args['field_el'], $args['instruction_placement']);
679
 
680
  }
681
 
@@ -697,15 +689,9 @@ class acfe_form_front{
697
 
698
  <?php endif; ?>
699
 
700
- <?php if($args['form']): ?>
701
- </form>
702
- <?php endif; ?>
703
- <script>
704
- if(window.history.replaceState){
705
- window.history.replaceState(null, null, window.location.href);
706
- }
707
- </script>
708
- <?php
709
 
710
  }
711
 
374
  if(!empty($args['errors_class']))
375
  $args['form_attributes']['data-errors-class'] = $args['errors_class'];
376
 
377
+ if(acf_maybe_get_POST('acf')){
378
+
379
+ acf_setup_meta($_POST['acf'], 'acfe_form_load', true);
380
+
381
+ }
382
+
383
  // Args
384
  $args = apply_filters('acfe/form/load', $args, $args['post_id']);
385
  $args = apply_filters('acfe/form/load/form=' . $form_name, $args, $args['post_id']);
412
  endwhile;
413
  endif;
414
 
415
+ if(acf_maybe_get_POST('acf')){
416
+
417
+ acf_reset_meta('acfe_form_load');
418
+
419
+ }
420
+
421
  return $args;
422
 
423
  }
458
 
459
  if(acf_maybe_get($form, 'form_name') === $args['form_name']){
460
 
461
+ ?><div class="acfe-form-success" data-form-name="<?php echo $args['form_name']; ?>" data-form-id="<?php echo $args['form_id']; ?>"></div><?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
462
 
463
  if(!empty($args['updated_message'])){
464
 
484
 
485
  if($args['updated_hide_form']){
486
 
 
 
 
 
 
 
 
 
487
  return;
488
 
489
  }
542
 
543
  }
544
 
545
+ $wrapper = 'div';
 
546
 
547
+ if($args['form'])
548
+ $wrapper = 'form';
549
+
550
+ ?>
551
+
552
+ <<?php echo $wrapper; ?> <?php acf_esc_attr_e($args['form_attributes']); ?>>
553
 
554
+ <?php
555
 
556
  // render post data
557
  acf_form_data(array(
667
 
668
  }
669
 
670
+ acf_render_fields($fields, acf_uniqid('acfe_form'), $args['field_el'], $args['instruction_placement']);
671
 
672
  }
673
 
689
 
690
  <?php endif; ?>
691
 
692
+ </<?php echo $wrapper; ?>>
693
+
694
+ <?php
 
 
 
 
 
 
695
 
696
  }
697
 
includes/modules/single-meta.php CHANGED
@@ -26,7 +26,7 @@ class acfe_single_meta{
26
  add_filter('acf/pre_load_metadata', array($this, 'load_reference'), 10, 4);
27
 
28
  // Save
29
- add_filter('acf/update_value', array($this, 'update_value'), 999, 4);
30
  add_action('acf/save_post', array($this, 'save_post'), 999);
31
 
32
  // Settings
@@ -50,7 +50,7 @@ class acfe_single_meta{
50
  /*
51
  * Update Value
52
  */
53
- function update_value($value, $post_id, $field, $_value){
54
 
55
  // Do not save empty values
56
  //if(empty($value) && !is_numeric($value) && $field['type'] !== 'flexible_content' && $field['type'] !== 'clone' && $field['type'] !== 'group')
26
  add_filter('acf/pre_load_metadata', array($this, 'load_reference'), 10, 4);
27
 
28
  // Save
29
+ add_filter('acf/update_value', array($this, 'update_value'), 999, 3);
30
  add_action('acf/save_post', array($this, 'save_post'), 999);
31
 
32
  // Settings
50
  /*
51
  * Update Value
52
  */
53
+ function update_value($value, $post_id, $field){
54
 
55
  // Do not save empty values
56
  //if(empty($value) && !is_numeric($value) && $field['type'] !== 'flexible_content' && $field['type'] !== 'clone' && $field['type'] !== 'group')
readme.txt CHANGED
@@ -3,9 +3,9 @@ Contributors: hwk-fr
3
  Donate link: https://ko-fi.com/acfextended
4
  Tags: acf, custom fields, meta, admin, fields, form, repeater, content
5
  Requires at least: 4.9
6
- Tested up to: 5.3
7
  Requires PHP: 5.6
8
- Stable tag: 0.8.5
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -332,7 +332,7 @@ Manage ACF Block Types from ACF > Block Types.
332
  * Hide Empty Message: Hide the native Flexible Content 'Empty' message
333
  * Empty Message: Change the native Flexible Content 'Click the Add Row button below...' message
334
  * Layouts Thumbnails: Add thumbnails for each layout in the layout selection
335
- * Layouts Settings: Choose a field group to clone and to be used as a layout configuration modal in the administration
336
  * Layouts Render: Add `template.php`, `style.css` & `script.js` files settings for each layout. Those settings can be then accessed in the front-end ([More information in the FAQ](#faq))
337
  * Layouts Dynamic Preview: Edit & Preview Layouts on-the-fly from your WordPress administration, just like in Gutenberg (Layouts Render must be turned ON)
338
  * Modal Edition: Edit layouts in a modal
@@ -689,6 +689,21 @@ function my_acfe_modules(){
689
 
690
  == Changelog ==
691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
692
  = 0.8.5 =
693
  * General: Added Settings/Options API
694
  * General: Improved modules performance
3
  Donate link: https://ko-fi.com/acfextended
4
  Tags: acf, custom fields, meta, admin, fields, form, repeater, content
5
  Requires at least: 4.9
6
+ Tested up to: 5.4
7
  Requires PHP: 5.6
8
+ Stable tag: 0.8.5.5
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
332
  * Hide Empty Message: Hide the native Flexible Content 'Empty' message
333
  * Empty Message: Change the native Flexible Content 'Click the Add Row button below...' message
334
  * Layouts Thumbnails: Add thumbnails for each layout in the layout selection
335
+ * Layouts Settings: Choose a field group to clone and to be used as a layout configuration modal in the administration. Data can be retrieved using `while(have_settings()): the_setting(); get_sub_field('my_setting');`
336
  * Layouts Render: Add `template.php`, `style.css` & `script.js` files settings for each layout. Those settings can be then accessed in the front-end ([More information in the FAQ](#faq))
337
  * Layouts Dynamic Preview: Edit & Preview Layouts on-the-fly from your WordPress administration, just like in Gutenberg (Layouts Render must be turned ON)
338
  * Modal Edition: Edit layouts in a modal
689
 
690
  == Changelog ==
691
 
692
+ = 0.8.5.5 =
693
+ * Field: Button - Added nominative JS hooks `action('acfe/fields/button/before/name=my_field', response, $el, data);`
694
+ * Field: Button - Added nominative JS hooks `action('acfe/fields/button/success/name=my_field', response, $el, data);`
695
+ * Field: Button - Added nominative JS hooks `action('acfe/fields/button/complete/name=my_field', response, $el, data);`
696
+ * Field: Button - Deprecated JS hooks `acfe/fields/button/before_ajax` & `acfe/fields/button/ajax_success`. Replaced by `acfe/fields/button/before` & `acfe/fields/button/success`
697
+ * Field: Flexible Content - Added generic `acfe/flexible/thumbnail` hook
698
+ * Module: Dynamic Forms - Fixed a bug where 2 forms with 2 fields with the same name on the same page, will override `default_value` during render
699
+ * Module: Dynamic Forms - Fixed a nasty bug with "Post Action" which could trigger an infinite loop when using Elementor & YOAST. See bug report: https://github.com/elementor/elementor/issues/10998
700
+ * Module: Dynamic Forms - Fixed jQuery not recognized on form success in some specific case
701
+ * Module: Dynamic Forms - Added "No form element" compatibility allowing validation settings to be applied when form tag isn't printed
702
+ * Module: Dynamic Forms - Fixed typo in "Custom Action" code example
703
+ * Module: Dynamic Forms - Added to the possibility to use `get_field('my_field')` to retrieve form input value inside `acfe/form/load` hooks
704
+ * Module: Single Meta Save - Fixed hook arguments which could trigger a PHP error in some specific cases
705
+ * General: Readme - Added Flexible Content Settings Modal example
706
+
707
  = 0.8.5 =
708
  * General: Added Settings/Options API
709
  * General: Improved modules performance