forked from wp-plugins/acf-google-font-selector-field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acf-google_font_selector-v4.php
executable file
·273 lines (222 loc) · 7.01 KB
/
acf-google_font_selector-v4.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
<?php
/**
* ACF 4 Field Class
*
* This file holds the class required for our field to work with ACF 4
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
/**
* ACF 4 Google Font Selector Class
*
* The Google Font selector class enables users to select webfonts from
* The Google Fonts service. This is the class that is used for ACF 4.
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
class acf_field_google_font_selector extends acf_field {
var $settings;
var $defaults;
/**
* Field Constructor
*
* Sets basic properties and runs the parent constructor
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function __construct() {
$this->settings = array(
'path' => apply_filters('acf/helpers/get_path', __FILE__),
'dir' => plugin_dir_url( __FILE__ ),
'version' => '1.0.0'
);
$this->name = 'google_font_selector';
$this->label = __( 'Google Font Selector', 'acf-google-font-selector-field');
$this->category = __( 'Choice' , 'acf' );
$this->defaults = array(
'include_web_safe_fonts' => true,
'enqueue_font' => true,
'default_font' => 'Droid Sans',
);
parent::__construct();
add_action( 'wp_ajax_acfgfs_get_font_details', 'acfgfs_action_get_font_details' );
if( !defined( 'ACFGFS_NOENQUEUE' ) ) {
add_action( 'wp_enqueue_scripts', 'acfgfs_google_font_enqueue' );
}
}
/**
* Field Options
*
* Creates the options for the field, they are shown when the user
* creates a field in the back-end. Currently there are three fields.
*
* The Web Safe Fonts setting allows you to add regular fonts available
* in any browser to the list
*
* The Enqueue Font setting will load the fonts on the appropriate page
* when checked.
*
* The default font settings allows you to specify the font set as the
* default for the field.
*
* @param array $field The details of this field
* @author Daniel Pataki
* @since 3.0.0
*
*/
function create_options( $field ) {
$field = array_merge($this->defaults, $field);
$key = $field['name'];
?>
<!-- Web Safe Fonts Field -->
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Web Safe Fonts?",'acf-google-font-selector-field'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'true_false',
'name' => 'fields['.$key.'][include_web_safe_fonts]',
'value' => $field['include_web_safe_fonts'],
'layout' => 'horizontal',
'message' => __('Include web safe fonts?','acf-google-font-selector-field'),
));
?>
</td>
</tr>
<!-- Enqueue Fonts Field -->
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Enqueue Font?",'acf-google-font-selector-field'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'true_false',
'name' => 'fields['.$key.'][enqueue_font]',
'value' => $field['enqueue_font'],
'layout' => 'horizontal',
'message' => __('Automatically load font?','acf-google-font-selector-field'),
));
?>
</td>
</tr>
<!-- Default Font Field -->
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Default Font",'acf-google-font-selector-field'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'select',
'name' => 'fields['.$key.'][default_font]',
'value' => $field['default_font'],
'choices' => acfgfs_get_font_dropdown_array()
));
?>
</td>
</tr>
<?php
}
/**
* Field Display
*
* This function takes care of displaying our field to the users, taking
* the field options into account.
*
* @param array $field The details of this field
* @author Daniel Pataki
* @since 3.0.0
*
*/
function create_field( $field ){
$current_font_family = ( empty( $field['value'] ) ) ? $field['default_font'] : $field['value']['font'];
?>
<div class="acfgfs-font-selector">
<div class="acfgfs-loader"></div>
<div class="acfgfs-form-control acfgfs-font-family">
<div class="acfgfs-form-control-title"><?php _e('Font Family', 'acf-google-font-selector-field') ?></div>
<select name="<?php echo esc_attr($field['name']) ?>">
<?php
$options = acfgfs_get_font_dropdown_array( $field );
foreach( $options as $option ) {
echo '<option ' . selected( $option, $current_font_family ) . ' value="' . $option . '">' . $option . '</option>';
}
?>
</select>
<?php $font = str_replace( ' ', '+', $current_font_family ); ?>
<div class='acfgfs-preview'>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=<?php echo $font ?>">
<div style='font-family:<?php echo $current_font_family ?>'>
<?php _e( 'This is a preview of the selected font', 'acf-google-font-selector-field' ) ?>
</div>
</div>
</div>
<div class="acfgfs-form-control acfgfs-font-variants">
<div class="acfgfs-form-control-title"><?php _e('Variants', 'acf-google-font-selector-field') ?></div>
<div class="acfgfs-list">
<?php acfgfs_display_variant_list( $field ) ?>
</div>
</div>
<div class="acfgfs-form-control acfgfs-font-subsets">
<div class="acfgfs-form-control-title"><?php _e('Subsets', 'acf-google-font-selector-field') ?></div>
<div class="acfgfs-list">
<?php acfgfs_display_subset_list( $field ) ?>
</div>
</div>
<textarea name="acfgfs-font-data" class="acfgfs-font-data"><?php echo json_encode( $field ) ?></textarea>
</div>
<?php
}
/**
* Enqueue Assets
*
* This function enqueues the scripts and styles needed to display the
* field
*
* @author Daniel Pataki
* @since 3.0.0
*
*/
function input_admin_enqueue_scripts() {
wp_enqueue_script( 'acf-input-google_font_selector', $this->settings['dir'] . 'js/input.js', array('acf-input'), $this->settings['version'] );
wp_enqueue_style( 'acf-input-google_font_selector', $this->settings['dir'] . 'css/input.css', array('acf-input'), $this->settings['version'] );
}
/**
* Pre-Save Value Modification
*
* This filter is applied to the $value before it is updated in the db
*
* @param mixed $value The value which will be saved in the database
* @param int $post_id The $post_id of which the value will be saved
* @param array $field The field array holding all the field options
* @return mixed The new value
* @author Daniel Pataki
* @since 3.0.0
*
*/
function update_value( $value, $post_id, $field ) {
$new_value = array();
$new_value['font'] = $value;
if( empty( $_POST[$field['key'] . '_variants'] ) ) {
$_POST[$field['key'] . '_variants'] = acfgfs_get_font_variant_array( $new_value['font'] );
}
if( empty( $_POST[$field['key'] . '_subsets'] ) ) {
$_POST[$field['key'] . '_subsets'] = acfgfs_get_font_subset_array( $new_value['font'] );
}
$new_value['variants'] = $_POST[$field['key'] . '_variants'];
$new_value['subsets'] = $_POST[$field['key'] . '_subsets'];
return $new_value;
}
}
// create field
new acf_field_google_font_selector();
?>