class-meta-boxes.php
16.2 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
<?php
namespace LearnDash\Course_Grid\Admin;
if ( ! defined( 'ABSPATH' ) ) {
exit();
}
use LearnDash\Course_Grid\Utilities;
class Meta_Boxes
{
public function __construct()
{
add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'save_post', [ $this, 'save_meta_boxes' ], 10, 3 );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
}
public function add_meta_boxes()
{
$post_types = Utilities::get_post_types_slugs();
add_meta_box(
'learndash-course-grid-meta-box',
__( 'LearnDash Course Grid Settings', 'learndash-course-grid' ),
[ $this, 'output_settings_meta_box' ],
$post_types,
'advanced',
'low',
[]
);
}
public function save_meta_boxes( $post_id, $post, $update )
{
$this->save_settings_meta_box( $post_id, $post, $update );
}
public function output_settings_meta_box( $args )
{
global $post;
$post_id = $post->ID;
$post_type = get_post_type_object( $post->post_type );
$post_type_label = $post_type ? strtolower( $post_type->labels->singular_name ) : '';
$description = get_post_meta( $post_id, '_learndash_course_grid_short_description', true );
$enable_video = get_post_meta( $post_id, '_learndash_course_grid_enable_video_preview', true );
$embed_code = get_post_meta( $post_id, '_learndash_course_grid_video_embed_code', true );
$duration = get_post_meta( $post_id, '_learndash_course_grid_duration', true );
$duration_h = is_numeric( $duration ) ? floor( $duration / HOUR_IN_SECONDS ) : null;
$duration_m = is_numeric( $duration ) ? floor( ( $duration % HOUR_IN_SECONDS ) / MINUTE_IN_SECONDS ) : null;
$button_text = get_post_meta( $post_id, '_learndash_course_grid_custom_button_text', true );
$ribbon_text = get_post_meta( $post_id, '_learndash_course_grid_custom_ribbon_text', true );
$video_html = '<video controls><source src="video-file.mp4" type="video/mp4"></video>';
?>
<?php wp_nonce_field( 'settings_meta_box', 'learndash_course_grid_nonce' ); ?>
<script>
var toggleVisibility = toggleVisibility || function( id ) {
var e = document.getElementById( id );
if ( e.style.display == 'block' ) {
e.style.display = 'none';
} else {
e.style.display = 'block';
}
};
jQuery( document ).ready( function( $ ) {
$( window ).on( 'load', function() {
if ( $( 'input[name="learndash_course_grid_enable_video_preview"]' ).is( ':checked' ) ) {
$( '#learndash_course_grid_video_embed_code_field' ).show();
}
} );
$( 'input[name="learndash_course_grid_enable_video_preview"]' ).change( function( e ) {
if ( $( this ).prop( 'checked' ) ) {
$( '#learndash_course_grid_video_embed_code_field' ).show();
} else {
$( '#learndash_course_grid_video_embed_code_field' ).hide();
}
});
});
</script>
<div class="sfwd sfwd_options learndash-course-grid-settings">
<div class="sfwd_input">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!">
<img src="<?php echo LEARNDASH_COURSE_GRID_PLUGIN_ASSET_URL . 'img/question.png' ?>" onclick="toggleVisibility( 'learndash_course_grid_short_description_help_text' );">
<label class="sfwd_label textinput" for="learndash_course_grid_short_description"><?php _e( 'Short Description', 'learndash-course-grid' ); ?></label>
</a>
<div class="sfwd_help_text_div" style="display:none" id="learndash_course_grid_short_description_help_text">
<label class="sfwd_help_text"><?php printf( __( 'Short description that will be displayed for this %s in the Course Grid.', 'learndash-course-grid' ), $post_type_label ); ; ?></label>
</div>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<textarea name="learndash_course_grid_short_description" id="learndash_course_grid_short_description" cols="20" rows="3"><?php echo esc_textarea( $description ) ?></textarea>
</div>
</span>
<p style="clear:left"></p>
</div>
<div class="sfwd_input">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!">
<img src="<?php echo LEARNDASH_COURSE_GRID_PLUGIN_ASSET_URL . 'img/question.png' ?>" onclick="toggleVisibility( 'learndash_course_grid_duration_help_text' );">
<label class="sfwd_label textinput" for="learndash_course_grid_duration"><?php _e( 'Duration', 'learndash-course-grid' ); ?></label>
</a>
<div class="sfwd_help_text_div" style="display:none" id="learndash_course_grid_duration_help_text">
<label class="sfwd_help_text"><?php printf( __( 'Duration for this %s.', 'learndash-course-grid' ), $post_type_label ); ; ?></label>
</div>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<label for="learndash_course_grid_duration_hour">
<input
type="number"
name="learndash_course_grid_duration_hour"
id="learndash_course_grid_duration_hour"
value="<?php echo esc_attr( $duration_h ); ?>"
class="small-text"
min="0"
> <?php _e( 'hour(s)', 'learndash-course-grid' ); ?>
</label>
<label for="learndash_course_grid_duration_minute" style="margin-left: 15px;">
<input
type="number"
name="learndash_course_grid_duration_minute"
id="learndash_course_grid_duration_minute"
value="<?php echo esc_attr( $duration_m ); ?>"
class="small-text"
min="0"
> <?php _e( 'minute(s)', 'learndash-course-grid' ); ?>
</label>
</div>
</span>
<p style="clear:left"></p>
</div>
<div class="sfwd_input">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!">
<img src="<?php echo LEARNDASH_COURSE_GRID_PLUGIN_ASSET_URL . 'img/question.png'; ?>" onclick="toggleVisibility( 'learndash_course_grid_enable_video_preview_help_text' );">
<label class="sfwd_label textinput" for="learndash_course_grid_enable_video_preview"><?php _e( 'Enable Video Preview', 'learndash-course-grid' ); ?></label>
</a>
<div class="sfwd_help_text_div" style="display:none" id="learndash_course_grid_enable_video_preview_help_text">
<label class="sfwd_help_text"><?php printf( __( 'Select this option to use a featured video for this %s in the Course Grid. If not selected, the featured image will be used.', 'learndash-course-grid' ), $post_type_label ) ; ?></label>
</div>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<input type="hidden" name="learndash_course_grid_enable_video_preview" value="0">
<input type="checkbox" name="learndash_course_grid_enable_video_preview" id="learndash_course_grid_enable_video_preview" value="1" <?php checked( $enable_video, 1, true ); ?>>
</div>
</span>
<p style="clear:left"></p>
</div>
<div class="sfwd_input" style="display: none;" id="learndash_course_grid_video_embed_code_field">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!">
<img src="<?php echo LEARNDASH_COURSE_GRID_PLUGIN_ASSET_URL . 'img/question.png'; ?>" onclick="toggleVisibility( 'learndash_course_grid_video_embed_code_help_text' );">
<label class="sfwd_label textinput" for="learndash_course_grid_video_embed_code"><?php _e( 'Video URL or Embed Code', 'learndash-course-grid' ); ?></label>
</a>
<div class="sfwd_help_text_div" style="display:none" id="learndash_course_grid_video_embed_code_help_text">
<label class="sfwd_help_text"><?php printf( __( 'Embed code of the video you want to use for this %s in the Course Grid. If you have a video file URL, you can use the following HTML tag to embed your video: %s', 'learndash-course-grid' ), $post_type_label, '<code>' . esc_html( $video_html ) . '</code>' ); ?>
</label>
</div>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<textarea name="learndash_course_grid_video_embed_code" id="learndash_course_grid_video_embed_code" rows="2" cols="57"><?php echo esc_textarea( $embed_code ); ?></textarea>
</div>
</span>
<p style="clear:left"></p>
</div>
<div class="sfwd_input" id="learndash_course_grid_custom_button_text_field">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!">
<img src="<?php echo LEARNDASH_COURSE_GRID_PLUGIN_ASSET_URL . 'img/question.png'; ?>" onclick="toggleVisibility( 'learndash_course_grid_custom_button_text_help_text' );">
<label class="sfwd_label textinput" for="learndash_course_grid_custom_button_text"><?php _e( 'Custom Button Text', 'learndash-course-grid' ); ?></label>
</a>
<div class="sfwd_help_text_div" style="display:none" id="learndash_course_grid_custom_button_text_help_text">
<label class="sfwd_help_text"><?php _e( 'Use this field to change the default "See More..." button text in the Course Grid.', 'learndash-course-grid' ); ?>
</label>
</div>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<input name="learndash_course_grid_custom_button_text" id="learndash_course_grid_custom_button_text" type="text" value="<?php echo esc_attr( $button_text ); ?>"></textarea>
</div>
</span>
<p style="clear:left"></p>
</div>
<div class="sfwd_input" id="learndash_course_grid_custom_ribbon_text_field">
<span class="sfwd_option_label" style="text-align:right;vertical-align:top;">
<a class="sfwd_help_text_link" style="cursor:pointer;" title="Click for Help!">
<img src="<?php echo LEARNDASH_COURSE_GRID_PLUGIN_ASSET_URL . 'img/question.png'; ?>" onclick="toggleVisibility( 'learndash_course_grid_custom_ribbon_text_help_text' );">
<label class="sfwd_label textinput" for="learndash_course_grid_custom_ribbon_text"><?php _e( 'Custom Ribbon Text', 'learndash-course-grid' ); ?></label>
</a>
<div class="sfwd_help_text_div" style="display:none" id="learndash_course_grid_custom_ribbon_text_help_text">
<label class="sfwd_help_text"><?php _e( 'Use this field to change the default ribbon text in the Course Grid.', 'learndash-course-grid' ); ?>
</label>
</div>
</span>
<span class="sfwd_option_input">
<div class="sfwd_option_div">
<input name="learndash_course_grid_custom_ribbon_text" id="learndash_course_grid_custom_ribbon_text" type="text" value="<?php echo esc_attr( $ribbon_text ); ?>"></textarea>
</div>
</span>
<p style="clear:left"></p>
</div>
</div>
<?php
}
/**
* Save course grid meta box fields
*
* @param int $post_id Post ID
* @param object $post WP post object
* @param bool $update True if post is an update
*/
public function save_settings_meta_box( $post_id, $post, $update )
{
if ( ! in_array( $post->post_type, Utilities::get_post_types_slugs() ) ) {
return;
}
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if ( ! isset( $_POST['learndash_course_grid_nonce'] ) || ! wp_verify_nonce( $_POST['learndash_course_grid_nonce'], 'settings_meta_box' ) ) {
return;
}
$allowed_html = wp_kses_allowed_html( 'learndash_course_grid_embed_code' );
if ( isset( $_POST['learndash_course_grid_short_description'] ) ) {
update_post_meta( $post_id, '_learndash_course_grid_short_description', wp_kses_post( $_POST['learndash_course_grid_short_description'] ) );
learndash_update_setting( $post_id, 'course_short_description', wp_kses_post( $_POST['learndash_course_grid_short_description'] ) );
}
$duration_second = '';
if ( ! empty( $_POST['learndash_course_grid_duration_hour'] ) || ! empty( $_POST['learndash_course_grid_duration_minute'] ) ) {
$duration_second = ( intval( $_POST['learndash_course_grid_duration_hour'] ) * HOUR_IN_SECONDS ) + ( intval( $_POST['learndash_course_grid_duration_minute'] ) * MINUTE_IN_SECONDS );
}
update_post_meta( $post_id, '_learndash_course_grid_duration', $duration_second );
if ( isset( $_POST['learndash_course_grid_enable_video_preview'] ) ) {
update_post_meta( $post_id, '_learndash_course_grid_enable_video_preview', wp_filter_kses( $_POST['learndash_course_grid_enable_video_preview'] ) );
}
if ( isset( $_POST['learndash_course_grid_video_embed_code'] ) ) {
update_post_meta( $post_id, '_learndash_course_grid_video_embed_code', wp_kses( $_POST['learndash_course_grid_video_embed_code'], $allowed_html ) );
}
if ( isset( $_POST['learndash_course_grid_custom_button_text'] ) ) {
update_post_meta( $post_id, '_learndash_course_grid_custom_button_text', sanitize_text_field( trim( $_POST['learndash_course_grid_custom_button_text'] ) ) );
}
if ( isset( $_POST['learndash_course_grid_custom_ribbon_text'] ) ) {
update_post_meta( $post_id, '_learndash_course_grid_custom_ribbon_text', sanitize_text_field( trim( $_POST['learndash_course_grid_custom_ribbon_text'] ) ) );
}
}
public function enqueue_scripts()
{
$screen = get_current_screen();
if ( 'post' === $screen->base && ! defined( 'LEARNDASH_VERSION' ) ) {
wp_enqueue_style(
'learndash-course-grid-meta-box',
LEARNDASH_COURSE_GRID_PLUGIN_URL . 'assets/css/meta-box.css',
[],
LEARNDASH_COURSE_GRID_VERSION,
'all'
);
}
}
}