-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathacf-fields-in-custom-table.php
370 lines (311 loc) · 12.6 KB
/
acf-fields-in-custom-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/*
Plugin Name: ACF: Fields in Custom Table
Description: Stores ACF custom fields in a custom table instead of WordPress core meta tables.
Version: 0.5
Author: Eduardo Marcolino
Author URI: https://eduardomarcolino.com
Text Domain: acfict
Domain Path: /languages
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
GitHub Plugin URI: https://github.com/eduardo-marcolino/acf-fields-in-custom-table
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'ACF_FICT' ) )
{
defined( 'ACF_FICT_PLUGIN_FILE' ) or define( 'ACF_FICT_PLUGIN_FILE', __FILE__ );
include_once( plugin_dir_path( __FILE__ ).'includes/acfict-utility-functions.php' );
acfict_include( 'includes/types/class-acfict-type.php' );
acfict_include( 'includes/types/class-acfict-type-column.php' );
acfict_include( 'includes/fields/class-acfict-field.php' );
acfict_include( 'includes/fields/class-acfict-field-text.php' );
acfict_include( 'includes/fields/class-acfict-field-email.php' );
acfict_include( 'includes/fields/class-acfict-field-url.php' );
acfict_include( 'includes/fields/class-acfict-field-password.php' );
acfict_include( 'includes/fields/class-acfict-field-radio.php' );
acfict_include( 'includes/fields/class-acfict-field-button_group.php' );
acfict_include( 'includes/fields/class-acfict-field-oembed.php' );
acfict_include( 'includes/fields/class-acfict-field-number.php' );
acfict_include( 'includes/fields/class-acfict-field-range.php' );
acfict_include( 'includes/fields/class-acfict-field-file.php' );
acfict_include( 'includes/fields/class-acfict-field-image.php' );
acfict_include( 'includes/fields/class-acfict-field-true_false.php' );
acfict_include( 'includes/fields/class-acfict-field-color_picker.php' );
acfict_include( 'includes/fields/class-acfict-field-date_picker.php' );
acfict_include( 'includes/fields/class-acfict-field-date_time_picker.php' );
acfict_include( 'includes/fields/class-acfict-field-time_picker.php' );
acfict_include( 'includes/fields/class-acfict-field-textarea.php' );
acfict_include( 'includes/fields/class-acfict-field-wysiwyg.php' );
acfict_include( 'includes/fields/class-acfict-field-select.php' );
acfict_include( 'includes/fields/class-acfict-field-checkbox.php' );
acfict_include( 'includes/fields/class-acfict-field-link.php' );
acfict_include( 'includes/fields/class-acfict-field-relationship.php' );
acfict_include( 'includes/fields/class-acfict-field-post_object.php' );
acfict_include( 'includes/fields/class-acfict-field-page_link.php' );
acfict_include( 'includes/fields/class-acfict-field-taxonomy.php' );
acfict_include( 'includes/fields/class-acfict-field-user.php' );
final class ACF_FICT
{
private static $instance = null;
const SETTINGS_TABLE_NAME = 'acfict_table_name';
const SETTINGS_ENABLED = 'acfict_enabled';
const SETTINGS_USE_PREFIX = 'acfict_use_prefix';
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}
private function __wakeup() {}
private function __construct()
{
add_action( 'acf/field_group/admin_head', [$this, 'register_meta_box'] );
add_action( 'acf/update_field_group', [$this, 'create_or_update_table'] );
add_action( 'acf/save_post', [$this, 'store_fields_in_custom_table'], 1 );
add_action( 'delete_post', [$this, 'delete_fields_in_custom_table'] );
add_action( 'admin_notices', [$this, 'display_admin_notices'] );
add_filter( 'acf/load_field', [$this, 'add_settings'] );
add_filter( 'acf/load_value', [$this, 'load_field_from_custom_table'], 11, 3 );
add_filter( "acf/validate_field_group", [$this, 'validate_field_group'] );
load_plugin_textdomain( 'acfict', false, dirname( plugin_basename( ACF_FICT_PLUGIN_FILE ) ) . '/languages' );
}
public function register_meta_box() {
add_meta_box('acf-field-acfict', 'ACF: Fields in Custom Table', [$this, 'render_meta_box'], 'acf-field-group', 'normal');
}
public function render_meta_box()
{
global $field_group;
acf_render_field_wrap( [
'label' => __('Enabled', 'acfict'),
'instructions' => __( 'Enable Store fields in custom table for this field group?', 'acfict' ),
'type' => 'true_false',
'name' => self::SETTINGS_ENABLED,
'key' => self::SETTINGS_ENABLED,
'prefix' => 'acf_field_group',
'value' => esc_attr(acf_maybe_get( $field_group, self::SETTINGS_ENABLED, false )),
'ui' => 1,
] );
acf_render_field_wrap( [
'label' => __('Use Prefix', 'acfict'),
'instructions' => __( 'Append table name with prefix', 'acfict' ),
'type' => 'true_false',
'name' => self::SETTINGS_USE_PREFIX,
'key' => self::SETTINGS_USE_PREFIX,
'prefix' => 'acf_field_group',
'value' => esc_attr(acf_maybe_get( $field_group, self::SETTINGS_USE_PREFIX, true )),
'ui' => 1,
'conditional_logic' => [
'field' => self::SETTINGS_ENABLED,
'operator' => '==',
'value' => '1',
]
] );
acf_render_field_wrap( [
'label' => __( 'Custom table name', 'acfict' ),
'instructions' => __( 'Define the custom table name. Make sure it doesn\'t conflict with others tables names.', 'acfict' ),
'type' => 'text',
'name' => self::SETTINGS_TABLE_NAME,
'prefix' => 'acf_field_group',
'value' => esc_attr(acf_maybe_get( $field_group, self::SETTINGS_TABLE_NAME, false )),
'prepend' => $this->table_name(),
'required' => true,
'wrapper' => [
'class' => self::SETTINGS_TABLE_NAME.'_wrapper'
],
'conditional_logic' => [
'field' => self::SETTINGS_ENABLED,
'operator' => '==',
'value' => '1',
]
] );
?>
<script type="text/javascript">
if( typeof acf !== 'undefined' ) {
var field = acf.getField('acfict_use_prefix');
var prefixEl = jQuery('.<?php echo self::SETTINGS_TABLE_NAME ?>_wrapper .acf-input-prepend');
if(field.val() == false) {
prefixEl.addClass('acf-hidden')
}
field.on('change', function(e){
prefixEl.toggleClass('acf-hidden')
})
acf.newPostbox({
'id': 'acf-field-acfict',
'label': 'left'
});
}
</script>
<?php
}
public function add_settings( $field )
{
$field_group = acf_get_field_group( $field['parent'] );
foreach ([
self::SETTINGS_ENABLED => false,
self::SETTINGS_USE_PREFIX => true,
self::SETTINGS_TABLE_NAME => null
] as $key => $defaul_value)
{
$field[$key] = acf_maybe_get( $field_group, $key, $defaul_value );
}
return $field;
}
public function store_fields_in_custom_table( $post_id )
{
global $wpdb;
$values = [];
foreach ( $_POST['acf'] as $key => $value )
{
$field = get_field_object( sanitize_key( $key ) );
if (
$field[self::SETTINGS_ENABLED] && $field['name'] &&
$this->is_supported($field)
) {
$column_name = acfict_sanitize_keyword($field['name']);
$values[$this->table_name($field[self::SETTINGS_TABLE_NAME], $field[self::SETTINGS_USE_PREFIX])][$column_name] = apply_filters(
'acfict_sanitize_'.$field['type'],
$value,
$field
);
}
}
foreach ( $values as $table_name => $data)
{
$data['post_id'] = $post_id;
$wpdb->suppress_errors = true;
$wpdb->show_errors = false;
if ( false === $wpdb->replace($table_name, $data ) )
{
$message = __('ACF: Fields in Custom Table error:', 'acfict').$wpdb->last_error;
acfict_admin_notice_add($message, 'error');
}
}
}
public function delete_fields_in_custom_table($post_id)
{
global $wpdb;
foreach ( acf_get_field_groups( ['post_id' => $post_id] ) as $field_group )
{
if (
array_key_exists(self::SETTINGS_TABLE_NAME, $field_group) &&
$field_group[self::SETTINGS_TABLE_NAME]
) {
$wpdb->delete(
$this->table_name($field_group[self::SETTINGS_TABLE_NAME], $field_group[self::SETTINGS_USE_PREFIX]),
['post_id' => $post_id]
);
}
}
}
public function create_or_update_table( $field_group )
{
if ( !$field_group[self::SETTINGS_ENABLED]) {
return;
}
$columns = [];
$fields = acf_get_fields( $field_group );
foreach ( $fields as $field )
{
$column_type = apply_filters('acfict_column_type_'.$field['type'], $field);
if ( $column_type && is_a( $column_type, 'ACF_FICT_Type_Column') ) {
$columns[] = $column_type->get_definition();
}
}
$response = $this->do_create_or_alter_table(
$this->table_name( $field_group[self::SETTINGS_TABLE_NAME], $field_group[self::SETTINGS_USE_PREFIX] ),
$columns
);
if ( $response !== true ) {
$message = __('ACF: Fields in Custom Table error:', 'acfict').$response;
acfict_admin_notice_add($message, 'error');
}
}
private function do_create_or_alter_table( $table_name, $columns )
{
if (count($columns) === 0 ) {
return 'No supported fields';
}
global $wpdb;
$wpdb->suppress_errors = true;
$wpdb->show_errors = false;
$sql = "CREATE TABLE $table_name (
post_id bigint(20) unsigned NOT NULL,
".join(",\n\t", $columns).(count($columns) > 0 ? ',' : '')."
PRIMARY KEY (post_id)
) ENGINE=InnoDB {$wpdb->get_charset_collate()};";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$response = dbDelta( $sql );
if ( $wpdb->last_error ) {
return $wpdb->last_error;
}
if ( count($response) > 0 )
{
$notice = __('ACF: Fields in Custom Table: Database Changes', 'acfict');
$notice .= '<table class="wp-list-table widefat fixed striped table-view-list posts">';
$notice .= '<thead><tr><th>'.__('Column Name', 'acfict').'</th><th>'.__('Modification', 'acfict').'</th></tr></thead><tbody>';
foreach( $response as $column => $text) {
$notice .= sprintf('<tr><td>%s</td><td>%s</td></tr>', $column, $text);
}
$notice .= '</tbody></table>';
acfict_admin_notice_add($notice, 'info');
}
return true;
}
public function load_field_from_custom_table( $value, $post_id, $field )
{
if (
!$this->is_supported( $field ) ||
!array_key_exists( self::SETTINGS_ENABLED, $field ) ||
( array_key_exists( self::SETTINGS_ENABLED, $field ) && !$field[self::SETTINGS_ENABLED] )
) {
return $value;
}
$table_name = $this->table_name( $field[self::SETTINGS_TABLE_NAME], $field[self::SETTINGS_USE_PREFIX] );
if ( $this->table_exists( $table_name ) )
{
global $wpdb;
$column_name = sanitize_key($field['name']);
$value = $wpdb->get_var( $wpdb->prepare(
"SELECT $column_name FROM $table_name WHERE post_id = %d", $post_id
));
return apply_filters('acfict_escape_'.$field['type'], $value, $field);
}
return $value;
}
public function validate_field_group( $field_group )
{
if ( array_key_exists( self::SETTINGS_TABLE_NAME, $field_group ) ) {
$field_group[self::SETTINGS_TABLE_NAME] = acfict_sanitize_keyword( $field_group[self::SETTINGS_TABLE_NAME] );
}
return $field_group;
}
private function table_name( $name = '', $use_prefix = true) {
global $wpdb;
$prefix = $use_prefix ? apply_filters('acfict_table_prefix', $wpdb->prefix.'acf_', $name) : '';
return $prefix.$name;
}
private function is_supported( $field ) {
return apply_filters('acfict_supports_'.$field['type'], false);
}
private function table_exists( $table_name )
{
global $wpdb;
$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
return $wpdb->get_var( $query ) === $table_name;
}
public function display_admin_notices()
{
if ( false !== ($message = acfict_admin_notice_get()))
{
echo sprintf('<div class="notice notice-%s"><p>%s</p></div>',
$message['status'],
$message['message']
);
}
}
}
ACF_FICT::getInstance();
}