class-meta-boxes.php
9.06 KB
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WPF_Meta_Boxes {
public function __construct() {
add_action( 'add_meta_boxes', array($this, 'add_meta_box' ), 20);
add_action( 'admin_enqueue_scripts', array($this, 'metabox_scripts'));
add_action( 'save_post', array($this, 'save_meta_box_data' ));
// Meta box content
add_action( 'wpf_meta_box_content', array( $this, 'restrict_content_checkbox' ), 10, 2 );
add_action( 'wpf_meta_box_content', array( $this, 'required_tags_select' ), 15, 2 );
add_action( 'wpf_meta_box_content', array( $this, 'page_redirect_select' ), 20, 2 );
add_action( 'wpf_meta_box_content', array( $this, 'external_redirect_input' ), 25, 2 );
add_action( 'wpf_meta_box_content', array( $this, 'apply_tags_select' ), 30, 2 );
add_action( 'wpf_meta_box_content', array( $this, 'apply_to_children' ), 40, 2 );
add_action( 'wpf_meta_box_save', array( $this, 'save_changes_to_children' ), 10, 2 );
}
/**
* Enqueue meta box scripts
*
* @access public
* @return void
*/
public function metabox_scripts() {
wp_enqueue_style( 'select4', WPF_DIR_URL . 'includes/admin/options/lib/select2/select4.min.css', array(), '4.0.1');
wp_enqueue_script( 'select4', WPF_DIR_URL . 'includes/admin/options/lib/select2/select4.min.js', array('jquery'), '4.0.1');
wp_enqueue_style( 'wpf-admin', WPF_DIR_URL . 'assets/css/wpf-admin.css', array(), WP_FUSION_VERSION );
wp_enqueue_script( 'wpf-admin', WPF_DIR_URL . 'assets/js/wpf-admin.js', array('jquery', 'select4'), WP_FUSION_VERSION, true);
wp_localize_script( 'wpf-admin', 'wpf', array( 'crm_supports' => wp_fusion()->crm->supports ) );
}
/**
* Adds meta boxes to the configured post types
*
* @access public
* @return void
*/
public function add_meta_box() {
$post_types = get_post_types();
unset($post_types['attachment']);
unset($post_types['revision']);
$post_types = apply_filters( 'wpf_meta_box_post_types', $post_types );
foreach ( $post_types as $post_type ) {
add_meta_box('wpf-meta','WP Fusion', array($this, 'meta_box_callback'), $post_type, 'side', 'core');
}
}
/**
* Shows restrict content checkbox
*
* @access public
* @return void
*/
public function restrict_content_checkbox( $post, $settings ) {
$post_type_object = get_post_type_object( $post->post_type );
echo '<p><input class="checkbox" type="checkbox" data-unlock="allow_tags wpf-redirect wpf-redirect-url" id="wpf-lock-content" name="wpf-settings[lock_content]" value="1" '.checked($settings['lock_content'], 1, FALSE).' /> <strong>Restrict access to this ' . strtolower($post_type_object->labels->singular_name) . '</strong></p>';
}
/**
* Shows required tags input
*
* @access public
* @return void
*/
public function required_tags_select( $post, $settings ) {
echo '<p><label' .($settings['lock_content'] != true ? "" : ' class="disabled"') .' for="wpf-allow-tags"><small>Required tags (any):</small></label>';
if($settings['lock_content'] != true) {
$disabled = true;
} else {
$disabled = false;
}
wpf_render_tag_multiselect( $settings['allow_tags'], 'wpf-settings', 'allow_tags', null, $disabled );
echo '</p>';
}
/**
* Shows page redirect select
*
* @access public
* @return void
*/
public function page_redirect_select( $post, $settings ) {
$post_types = get_post_types(array('public' => true));
$available_posts = array();
unset($post_types['attachment']);
$post_types = apply_filters( 'wpf_redirect_post_types', $post_types );
foreach ($post_types as $post_type) {
$posts = get_posts(array('post_type' => $post_type, 'posts_per_page' => -1, 'orderby' => 'post_title', 'order' => 'ASC', 'post__not_in' => array( $post->ID )));
foreach($posts as $post) {
$available_posts[$post_type][$post->post_title] = $post->ID;
}
}
echo '<p><label' .($settings['lock_content'] == 1 ? "" : ' class="disabled"') .' for="wpf-redirect"><small>Redirect if access is denied:</small></label><br />';
// Compatibility fix for earlier versions where we stored the URL
if(!empty($settings['redirect']) && !is_numeric($settings['redirect']))
$settings['redirect'] = url_to_postid( $settings['redirect'] );
echo '<select' . ($settings['lock_content'] == 1 ? "" : ' disabled') . ' id="wpf-redirect" class="select4-search" style="width: 100%;" data-placeholder="None" name="wpf-settings[redirect]">';
echo '<option></option>';
foreach($available_posts as $post_type => $data) {
echo '<optgroup label="' . $post_type . '">';
foreach($available_posts[$post_type] as $post_name => $id) {
echo '<option value="' . $id . '"' . selected($id, $settings['redirect'], FALSE) . '>' . $post_name . '</option>';
}
echo '</optgroup>';
}
echo '</select></p>';
}
/**
* Shows external redirect text input
*
* @access public
* @return void
*/
public function external_redirect_input( $post, $settings ) {
echo '<p><label' .($settings['lock_content'] == 1 ? "" : ' class="disabled"') .' for="wpf-redirect-url"><small>Or enter a URL below:</small></label>';
echo '<input' .($settings['lock_content'] == 1 ? "" : ' disabled') .' type="text" id="wpf-redirect-url" name="wpf-settings[redirect_url]" value="' . esc_attr( $settings['redirect_url'] ) . '" />';
echo '</p>';
}
/**
* Shows select field with tags to apply on page load, with delay
*
* @access public
* @return void
*/
public function apply_tags_select( $post, $settings ) {
$post_type_object = get_post_type_object( $post->post_type );
echo '<p><label for="wpf-apply-tags"><small>Apply tags when a user views this ' . strtolower($post_type_object->labels->singular_name) . ':</small></label>';
wpf_render_tag_multiselect( $settings['apply_tags'], 'wpf-settings', 'apply_tags' );
echo '</p>';
/*
// Delay before applying tags
*/
echo '<p><label for="wpf-apply-delay"><small>Delay (in ms) before applying tags:</small></label>';
echo '<input type="text" id="wpf-apply-delay" name="wpf-settings[apply_delay]" value="' . esc_attr( $settings['apply_delay'] ) . '" size="15" />';
echo '</p>';
}
/**
* Shows apply settings to children textbox
*
* @access public
* @return void
*/
public function apply_to_children( $post, $settings ) {
$children = get_children( array('post_parent' => $post->ID, 'post_type' => $post->post_type));
if(count($children) > 0)
echo '<p><input class="checkbox" type="checkbox" id="wpf-apply-children" name="wpf-settings[apply_children]" value="1" '.checked($settings['apply_children'], 1, FALSE).' /> Apply these settings to ' . count($children) . ' children</p>';
}
/**
* Shows apply settings to children textbox
*
* @access public
* @return void
*/
public function save_changes_to_children( $post_id, $data ) {
// Apply settings to children if required
if(isset($data['apply_children'])) {
$children = get_children( array('post_parent' => $post_id, 'post_type' => $_POST['post_type']));
foreach($children as $child) {
update_post_meta( $child->ID, 'wpf-settings', $data );
}
}
}
/**
* Renders WPF meta box
*
* @access public
* @return void
*/
public function meta_box_callback( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'wpf_meta_box', 'wpf_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$settings = array(
'lock_content' => 0,
'allow_tags' => array(),
'apply_tags' => array(),
'apply_delay' => 0,
'redirect' => '',
'redirect_url' => ''
);
if(get_post_meta( $post->ID, 'wpf-settings', true ))
$settings = array_merge($settings, get_post_meta( $post->ID, 'wpf-settings', true ));
// Outputs the different input fields for the WPF meta box
do_action( 'wpf_meta_box_content', $post, $settings );
}
function save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['wpf_meta_box_nonce'] ) )
return;
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['wpf_meta_box_nonce'], 'wpf_meta_box' ) )
return;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Don't update on revisions
if($_POST['post_type'] == 'revision')
return;
if(isset($_POST['wpf-settings'])) {
$data = $_POST['wpf-settings'];
// Sanitize
if(isset($data['apply_delay']))
$data['apply_delay'] = sanitize_text_field( $data['apply_delay'] );
if(isset($data['redirect_url']))
$data['redirect_url'] = sanitize_text_field( $data['redirect_url'] );
} else {
$data = array();
}
if(!isset($data['lock_content']))
$data['lock_content'] = 0;
// Allow other plugins to save their own data
do_action( 'wpf_meta_box_save', $post_id, $data );
// Update the meta field in the database.
update_post_meta( $post_id, 'wpf-settings', $data );
}
}
new WPF_Meta_Boxes;