/
class.llms.meta.boxes.php
369 lines (300 loc) · 9.32 KB
/
class.llms.meta.boxes.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
/**
* Admin base Metabox.
*
* @package LifterLMS/Admin/PostTypes/Classes
*
* @since 1.0.0
* @version 6.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_Admin_Meta_Boxes class
*
* Sets up base metabox functionality and global save.
*
* @since 1.0.0
* @since 3.35.0 Verify nonces and sanitize `$_POST` data.
*/
class LLMS_Admin_Meta_Boxes {
/**
* Array of collected errors.
*
* @access public
* @var string
*/
private static $errors = array();
/**
* Constructor.
*
* @since 1.0.0
* @since 3.16.0 Unknown.
* @since 6.0.0 Instantiate award engagement submit meta box.
* Instantiate meta boxes to sync awarded certificates and achievements with their templates.
*
* @return void
*/
public function __construct() {
// Achievements.
new LLMS_Meta_Box_Achievement();
new LLMS_Meta_Box_Achievement_Sync();
// Certs.
new LLMS_Meta_Box_Certificate();
new LLMS_Meta_Box_Certificate_Sync();
// Emails.
new LLMS_Meta_Box_Email_Settings();
// Engagements.
new LLMS_Meta_Box_Engagement();
// Award Engagements.
new LLMS_Meta_Box_Award_Engagement_Submit();
// Membership restriction metabox.
new LLMS_Meta_Box_Access();
// Courses.
new LLMS_Meta_Box_Course_Options();
// Memberships.
new LLMS_Meta_Box_Membership();
// Courses & memberships.
require_once 'meta-boxes/class.llms.meta.box.course.builder.php';
require_once 'meta-boxes/class.llms.meta.box.visibility.php';
require_once 'meta-boxes/class.llms.meta.box.instructors.php';
new LLMS_Meta_Box_Product();
new LLMS_Meta_Box_Students();
// Lessons.
require_once 'meta-boxes/class.llms.meta.box.lesson.php';
// Coupons.
new LLMS_Meta_Box_Coupon();
// Orders.
new LLMS_Meta_Box_Order_Submit();
new LLMS_Meta_Box_Order_Details();
new LLMS_Meta_Box_Order_Transactions();
new LLMS_Meta_Box_Order_Enrollment();
new LLMS_Meta_Box_Order_Notes();
// Vouchers.
new LLMS_Meta_Box_Voucher();
add_action( 'add_meta_boxes', array( $this, 'hide_meta_boxes' ), 10 );
add_action( 'add_meta_boxes', array( $this, 'refresh_meta_boxes' ), 10 );
add_action( 'add_meta_boxes', array( $this, 'get_meta_boxes' ), 10 );
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 10, 2 );
add_action( 'lifterlms_process_llms_voucher_meta', 'LLMS_Meta_Box_Voucher_Export::export', 10, 2 );
// Error handling.
add_action( 'admin_notices', array( $this, 'display_errors' ) );
add_action( 'shutdown', array( $this, 'set_errors' ) );
// Modify the title placeholder text for achievement and certificate templates.
add_filter( 'enter_title_here', array( $this, 'maybe_modify_title_placeholder' ), 10, 2 );
// Add default image information for achievement and certificate templates.
add_filter( 'admin_post_thumbnail_html', array( $this, 'maybe_modify_post_thumbnail_html' ), 10, 3 );
}
/**
* Add error messages from metaboxes
*
* @param string $text
*/
public static function add_error( $text ) {
self::$errors[] = $text;
}
/**
* Save messages to the database
*/
public function set_errors() {
update_option( 'lifterlms_errors', self::$errors );
}
/**
* Display the messages in the error dialog box
*/
public function display_errors() {
$errors = get_option( 'lifterlms_errors' );
if ( empty( $errors ) ) {
return;
}
$errors = maybe_unserialize( $errors );
echo '<div id="lifterlms_errors" class="error"><p>';
foreach ( $errors as $error ) {
echo esc_html( $error );
}
echo '</p></div>';
delete_option( 'lifterlms_errors' );
}
/**
* Add Metaboxes
*
* @return void
* @since 1.0.0
* @version 3.16.0
*/
public function get_meta_boxes() {
add_action( 'media_buttons', 'llms_merge_code_button' );
/**
* @todo Transition to new style metaboxes.
*/
add_meta_box( 'lifterlms-voucher-export', __( 'Export CSV', 'lifterlms' ), 'LLMS_Meta_Box_Voucher_Export::output', 'llms_voucher', 'side', 'default' );
}
/**
* Remove Metaboxes
*
* @return void
* @since 3.4.0
* @version 3.13.0
*/
public function hide_meta_boxes() {
// Remove some defaults from orders.
remove_meta_box( 'commentstatusdiv', 'llms_order', 'normal' );
remove_meta_box( 'commentsdiv', 'llms_order', 'normal' );
remove_meta_box( 'slugdiv', 'llms_order', 'normal' );
// Remove the default submit box in favor of our custom box.
remove_meta_box( 'submitdiv', 'llms_order', 'side' );
// Remove some defaults from the course.
remove_meta_box( 'postexcerpt', 'course', 'normal' );
remove_meta_box( 'tagsdiv-course_difficulty', 'course', 'side' );
}
/**
* Modifies the featured image metabox for achievement and certificate templates.
*
* Displays the default image, text denoting that the default image is being used,
* and a link to the settings page where the default image can be changed.
*
* This additional content is only displayed when there's no featured image set
* for the template.
*
* @since 6.0.0
*
* @param string $content Default metabox HTML.
* @param int $post_id WP_Post ID of the post being edited.
* @param int $image_id Attachment ID for the saved featured image.
* @return string
*/
public function maybe_modify_post_thumbnail_html( $content, $post_id, $image_id ) {
$post_types = array(
'llms_achievement',
'llms_my_achievement',
'llms_certificate',
'llms_my_certificate',
);
$post_type = get_post_type( $post_id );
if ( ! $image_id && in_array( $post_type, $post_types, true ) ) {
$add_content = '';
$class = str_replace( array( 'llms_', 'my_' ), '', $post_type ) . 's';
$image_id = llms()->$class()->get_default_image_id();
$alt = $image_id ? get_post_meta( $image_id, '_wp_attachment_image_alt', true ) : __( 'Default image', 'lifterlms' );
$add_content = '<p><img alt="' . trim( wp_strip_all_tags( $alt ) ) . '" src="' . esc_url( llms()->$class()->get_default_image( $post_id ) ) . '" /></p>';
$settings_url = admin_url( 'admin.php?page=llms-settings&tab=engagements' );
$add_content .= '<p class="howto">' . __( 'Using the global default.', 'lifterlms' ) . ' <a href="' . esc_url( $settings_url ) . '">' . __( 'Edit', 'lifterlms' ) . '</a></p>';
$content = $add_content . $content;
}
return $content;
}
/**
* Modifies the placeholder text for the post title field.
*
* This is used to denote that the achievement and certificate template title fields
* are for internal use only to help avoid confusion as to why there are two separate
* titles.
*
* @since 6.0.0
*
* @param string $placeholder Default placeholder text.
* @param WP_Post $post Post object.
* @return string
*/
public function maybe_modify_title_placeholder( $placeholder, $post ) {
$post_types = array(
'llms_achievement',
'llms_certificate',
);
if ( in_array( $post->post_type, $post_types, true ) ) {
$placeholder = sprintf(
'%1$s (%2$s)',
$placeholder,
_x( 'for internal use only', 'added achievement and certificate template post title placeholder', 'lifterlms' )
);
}
return $placeholder;
}
/**
* Updates global $post variable
*
* @return void
*/
public function refresh_meta_boxes() {
global $post;
}
/**
* Validates post and metabox data before saving.
*
* @since Unknown
* @since 3.35.0 Verify nonces and sanitize `$_POST` data.
*
* @param int $post_id WP Post ID.
* @param WP_Post $post Post object.
* @return bool
*/
public function validate_post( $post_id, $post ) {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return false;
} elseif ( empty( $post_id ) || empty( $post ) ) {
return false;
} elseif ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
return false;
} elseif ( ! llms_verify_nonce( 'lifterlms_meta_nonce', 'lifterlms_save_data' ) ) {
return false;
} elseif ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) {
return false;
}
return true;
}
/**
* Check whether the post is a LifterLMS post type.
*
* @since unknown
* @since 6.0.0 Added 'llms_my_achievement' and 'llms_my_certificate'.
*
* @param WP_Post $post WP_Post instance.
* @return boolean
*/
public function is_llms_post_type( $post ) {
$post_types = array(
'course',
'section',
'lesson',
'llms_order',
'llms_email',
'llms_certificate',
'llms_my_certificate',
'llms_achievement',
'llms_my_achievement',
'llms_engagement',
'llms_membership',
'llms_quiz',
'llms_question',
'llms_coupon',
'llms_voucher',
);
/**
* Filters the post type names that are secific of LifterLMS.
*
* Used to determine whether or not fire actions of the type "lifterlms_process_{$post->post_type}_meta" on save.
*
* @since 6.0.0
*
* @param string[] $post_types Array of post type names.
* @param WP_Post $post WP_Post instance.
*/
$post_types = apply_filters( 'llms_metaboxes_llms_post_types', $post_types, $post );
if ( in_array( $post->post_type, $post_types, true ) ) {
return true;
}
}
/**
* Global Metabox Save
*
* @return void
* @param $post, $post_id
*/
public function save_meta_boxes( $post_id, $post ) {
if ( self::validate_post( $post_id, $post ) ) {
if ( self::is_llms_post_type( $post ) ) {
do_action( 'lifterlms_process_' . $post->post_type . '_meta', $post_id, $post );
}
}
}
}
new LLMS_Admin_Meta_Boxes();