-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition-field.php
140 lines (125 loc) · 4.72 KB
/
position-field.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
<?php
/*
Plugin Name: Position Grid Field
Plugin URI: https://example.com/position-grid-field
Description: Adds a custom ACF field for flexible positioning using a 3x3 grid
Version: 1.0
Author: Taggetig
Author URI: https://taggetig.be
*/
// Prevent direct file access
if (!defined('ABSPATH')) {
exit;
}
class PositionGridField {
public function __construct() {
add_action('acf/init', [$this, 'create_position_field_type']);
add_action('acf/input/admin_enqueue_scripts', [$this, 'enqueue_styles']);
}
public function create_position_field_type() {
if (function_exists('acf_register_field_type')) {
acf_register_field_type(new acf_field_position_grid());
}
}
public function enqueue_styles() {
wp_enqueue_style('acf-position-grid', plugins_url('css/position-grid.css', __FILE__));
}
}
new PositionGridField();
class acf_field_position_grid extends \acf_field {
public function __construct() {
$this->name = 'position_grid';
$this->label = __('Position Grid', 'text_domain');
$this->category = 'layout';
$this->defaults = [
'return_format' => 'string',
'custom_values' => [],
'disabled_positions' => [],
];
parent::__construct();
}
public function render_field_settings($field) {
acf_render_field_setting($field, [
'label' => __('Return Format', 'text_domain'),
'instructions' => __('Choose how the position value is returned', 'text_domain'),
'type' => 'radio',
'name' => 'return_format',
'choices' => [
'string' => __('String (e.g., "center-center")', 'text_domain'),
'custom' => __('Custom Values', 'text_domain'),
],
]);
// Custom values and disabled options for each position
$positions = [
'top-left', 'top-center', 'top-right',
'center-left', 'center-center', 'center-right',
'bottom-left', 'bottom-center', 'bottom-right'
];
foreach ($positions as $pos) {
// Custom value setting
acf_render_field_setting($field, [
'label' => sprintf(__('Custom Value for %s', 'text_domain'), str_replace('-', ' ', ucwords($pos))),
'type' => 'text',
'name' => 'custom_' . $pos,
'conditional_logic' => [
[
[
'field' => 'return_format',
'operator' => '==',
'value' => 'custom',
]
]
],
'wrapper' => [
'class' => 'acf-field-setting-label'
]
]);
// Disabled setting
acf_render_field_setting($field, [
'label' => sprintf(__('Disable %s', 'text_domain'), str_replace('-', ' ', ucwords($pos))),
'type' => 'true_false',
'name' => 'disabled_' . $pos,
'ui' => 1,
'wrapper' => [
'class' => 'acf-field-setting-label'
]
]);
}
}
public function render_field($field) {
$value = empty($field['value']) ? '' : $field['value'];
$positions = [
'top-left', 'top-center', 'top-right',
'center-left', 'center-center', 'center-right',
'bottom-left', 'bottom-center', 'bottom-right'
];
?>
<div class="acf-position-grid">
<?php foreach ($positions as $pos):
$is_selected = ($value === $pos);
$input_id = $field['id'] . '_' . $pos;
$is_disabled = !empty($field['disabled_' . $pos]);
?>
<label for="<?php echo esc_attr($input_id); ?>">
<input type="radio"
id="<?php echo esc_attr($input_id); ?>"
name="<?php echo esc_attr($field['name']); ?>"
value="<?php echo esc_attr($pos); ?>"
<?php checked($is_selected); ?>
<?php disabled($is_disabled); ?> />
<span class="screen-reader-text"><?php echo esc_html(str_replace('-', ' ', ucwords($pos))); ?></span>
</label>
<?php endforeach; ?>
</div>
<?php
}
public function format_value($value, $post_id, $field) {
if (empty($value)) {
return '';
}
if ($field['return_format'] === 'custom' && !empty($field['custom_' . $value])) {
return $field['custom_' . $value];
}
return $value;
}
}