-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-flexi-metabox.php
350 lines (330 loc) · 12.4 KB
/
class-flexi-metabox.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
<?php
/**
* Register meta boxes
*
* @link https://odude.com/
* @since 1.0.0
* @author ODude <[email protected]>
* @package Flexi
* @subpackage Flexi/includes
*/
class Flexi_Meta_boxes
{
public function __construct()
{
add_action('bulk_edit_custom_box', array($this, 'quick_edit_add'), 10, 2);
add_action('quick_edit_custom_box', array($this, 'quick_edit_add'), 10, 2);
add_action('save_post', array($this, 'save_quick_edit_data'));
}
/**
* Register CMB2 Meta-boxes
*
* @link https://github.com/CMB2/CMB2
*/
public function register_meta_box()
{
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box(array(
'id' => 'flexi_metabox',
'title' => __('Flexi Meta Controls', 'flexi'),
'object_types' => array('flexi'), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
));
// Regular Image Field
$cmb->add_field(array(
'name' => 'Primary image file',
'desc' => 'Upload an image',
'id' => 'flexi_image',
'type' => 'file',
// Optional:
'options' => array(
'url' => false, // Hide the text input for the url
),
'text' => array(
'add_upload_file_text' => 'Add Image File', // Change upload button text. Default: "Add or Upload File"
),
// query_args are passed to wp.media's library query.
'query_args' => array(
//'type' => 'application/pdf', // Make library only display PDFs.
// Or only allow gif, jpg, or png images
'type' => array(
'image/gif',
'image/jpeg',
'image/png',
),
),
'preview_size' => 'medium', // Image size to use when previewing in the admin.
));
$cmb->add_field(array(
'name' => 'Physical file',
'desc' => 'Select file other then images',
'id' => 'flexi_file',
'type' => 'file',
'text' => array(
'add_upload_file_text' => 'Add file',
),
));
//Add Image gallery
$cmb->add_field(array(
'name' => 'Standalone Image Gallery',
'desc' => '',
'id' => 'flexi_standalone_gallery',
'type' => 'file_list',
// 'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
'query_args' => array('type' => 'image'), // Only images attachment
// Optional, override default text strings
'text' => array(
'add_upload_files_text' => 'Upload Multiple Image Files', // default: "Add or Upload Files"
//'remove_image_text' => 'Replacement', // default: "Remove Image"
//'file_text' => 'Replacement', // default: "File:"
//'file_download_text' => 'Replacement', // default: "Download"
//'remove_text' => 'Replacement', // default: "Remove"
),
));
$cmb->add_field(array(
'name' => 'oEmbed URL',
'desc' => 'Enter a youtube, twitter, or instagram URL. Supports services listed at <a href="http://codex.wordpress.org/Embeds">http://codex.wordpress.org/Embeds</a>.',
'id' => 'flexi_url',
'type' => 'oembed',
'width' => '200',
'attributes' => array(
'width' => '200',
),
));
// Add meta box to flexi_category
/**
* Initiate the metabox
*/
$cmb_category = new_cmb2_box(array(
'id' => 'flexi_metabox_category',
'title' => __('Category Thumbnail', 'flexi'),
'object_types' => array('term'), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array('flexi_category'), // Tells CMB2 which taxonomies should have these fields
'context' => 'normal',
'priority' => 'high',
'show_names' => false, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
));
// Regular Image Field
$cmb_category->add_field(array(
'name' => 'Thumbnail file',
'desc' => 'Upload an image',
'id' => 'flexi_image_category',
'show_names' => true, // Show field names on the left
'type' => 'file',
// Optional:
'options' => array(
'url' => false, // Hide the text input for the url
),
'text' => array(
'add_upload_file_text' => 'Add Image File', // Change upload button text. Default: "Add or Upload File"
),
// query_args are passed to wp.media's library query.
'query_args' => array(
//'type' => 'application/pdf', // Make library only display PDFs.
// Or only allow gif, jpg, or png images
'type' => array(
'image/gif',
'image/jpeg',
'image/png',
),
),
'preview_size' => 'medium', // Image size to use when previewing in the admin.
));
$cmb_category->add_field(array(
'name' => 'Hide this category',
'desc' => 'Category is hidden in forms & list. But contents are visible.',
'id' => 'flexi_hide_cate',
'show_names' => true, // Show field names on the left
'type' => 'checkbox',
));
$cmb_category->add_field(array(
'name' => 'Link to sub-category',
'desc' => '<a href="' . admin_url('admin.php?page=flexi_settings&tab=gallery§ion=flexi_categories_settings') . '">' . __("Category", "flexi") . '</a> ' . __("page is linked which displays sub-category", "flexi"),
'id' => 'flexi_link_sub_cate',
'show_names' => true, // Show field names on the left
'type' => 'checkbox',
));
$cmb_side = new_cmb2_box(array(
'id' => 'flexi_metabox_side',
'title' => __('Flexi Shortcode', 'flexi'),
'object_types' => array('flexi'), // Post type
'context' => 'side', // 'normal', 'advanced', or 'side'
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
));
$shortcode = __('Save & reopen to get shortcode', 'flexi');
try {
if (isset($_GET['post']) && !is_array($_GET['post'])) {
$shortcode = '[flexi-standalone id="' . sanitize_text_field(wp_unslash($_GET['post'])) . '"]';
}
} catch (Exception $e) {
//Do nothing
}
// Regular text field
$cmb_side->add_field(array(
'name' => 'Shortcode for standalone gallery',
'description' => 'Display gallery of images available only on this post only.<br>No layouts<br>No Settings',
'id' => 'flexi_standalone_shortcode',
'type' => 'text',
'default' => $shortcode,
'save_field' => false, // Otherwise CMB2 will end up removing the value.
'attributes' => array(
'readonly' => 'readonly',
//'disabled' => 'disabled',
),
));
$cmb_side->add_field(array(
'name' => 'Detail Layout',
'desc' => 'Select detail layout',
'show_option_none' => '-- ' . __('Default', 'flexi') . ' --',
'option_none_value' => 'default',
'id' => 'flexi_layout',
'type' => 'select',
'show_option_none' => true,
'default' => 'default',
'options' => array(
'basic' => __('Basic', 'flexi'),
'complex' => __('Complex', 'flexi'),
),
));
// Add edit page select option
$cmb_side->add_field(array(
'name' => 'Edit Page ID',
'description' => 'Enter page ID of edit form if you want to change default edit form page. Set value to 0 to use default setting.',
'id' => 'flexi_new_edit_page',
'type' => 'text',
'default' => '',
'attributes' => array(
'type' => 'number',
'pattern' => '\d*',
),
'sanitization_cb' => 'absint',
'escape_cb' => 'absint',
));
}
public function allowed_html($html)
{
$allowed_html = array(
'input' => array(
'type' => array(),
'name' => array(),
'value' => array(),
'checked' => array(),
'class' => array(),
'id' => array(),
'min' => array(),
'max' => array(),
'step' => array(),
'checked' => array(),
),
'p' => array(
'class' => array(),
),
'pre' => array(
'class' => array(),
),
'select' => array(
'class' => array(),
'name' => array(),
'id' => array(),
),
'option' => array(
'value' => array(),
'selected' => array(),
),
'a' => array(
'href' => array(),
'title' => array(),
'style' => array(),
'target' => array(),
),
'textarea' => array(
'rows' => array(),
'id' => array(),
'class' => array(),
'cols' => array(),
'name' => array(),
),
'span' => array(
'class' => array(),
),
'label' => array(
'for' => array(),
),
'img' => array(
'title' => array(),
'src' => array(),
'alt' => array(),
'class' => array(),
'id' => array(),
'size' => array(),
),
'br' => array(),
'em' => array(),
'strong' => array(),
'fieldset' => array(),
);
return wp_kses($html, $allowed_html);
}
/**
* Add detail layout selection quick edit screen
*
* @param string $column_name Custom column name, used to check
* @param string $post_type
*
* @return void
*/
public function quick_edit_add($column_name, $post_type)
{
if ('flexi_layout' != $column_name) {
return;
}
wp_nonce_field('flexi-nonce', 'flexi-nonce', false);
$dropdown_args = array(
'show_option_none' => '-- ' . __('Default', 'flexi') . ' --',
'option_none_value' => '',
'selected' => '',
'name' => 'flexi_layout',
'id' => 'flexi_layout',
'folder' => 'detail',
);
// echo $args['name'] . "--------";
//var_dump($args);
echo '<label class="inline-edit-layout alignleft">
<span class="title">' . __('Detail Layout', 'flexi') . '</span> ';
echo $this->allowed_html(flexi_layout_list($dropdown_args));
echo '</label>';
}
/**
* Save quick edit data
*
* @param int $post_id
*
* @return void|int
*/
public function save_quick_edit_data($post_id)
{
// check user capabilities
if (!current_user_can('edit_post', $post_id)) {
return;
}
// check nonce
if (!isset($_REQUEST['flexi-nonce']) || !wp_verify_nonce(sanitize_text_field($_REQUEST['flexi-nonce']), 'flexi-nonce')) {
return;
}
// update the price
if (isset($_REQUEST['flexi_layout'])) {
update_post_meta($post_id, 'flexi_layout', sanitize_text_field($_REQUEST['flexi_layout']));
}
}
}