class-ld-shortcodes-tinymce.php
15.9 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**
* LearnDash Admin Shortcodes TinyMCE Class.
*
* @since 2.4.0
* @package LearnDash\Settings\Shortcodes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'LearnDash_Shortcodes_TinyMCE' ) ) {
/**
* Class for LearnDash Admin Shortcodes TinyMCE.
*
* @since 2.4.0
*/
class LearnDash_Shortcodes_TinyMCE {
/**
* Shortcode assets
*
* @var array learndash_admin_shortcodes_assets
*/
protected $learndash_admin_shortcodes_assets = array();
/**
* Public constructor for class.
*
* @since 2.4.0
*/
public function __construct() {
add_action( 'wp_enqueue_editor', array( $this, 'wp_enqueue_editor' ) );
add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugin' ), 1 );
add_filter( 'mce_buttons', array( $this, 'register_button' ), 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'qt_button_script' ) );
add_action( 'wp_ajax_learndash_generate_shortcodes_content', array( $this, 'learndash_generate_shortcodes_content' ) );
}
/**
* Shortcodes assets init
*
* @since 3.0.7
*/
protected function shortcodes_assets_init() {
global $typenow, $pagenow, $post;
if ( empty( $this->learndash_admin_shortcodes_assets ) ) {
$this->learndash_admin_shortcodes_assets['popup_title'] = esc_html__( 'LearnDash Shortcodes', 'learndash' );
/**
* Filters TinyMCE shortcode popup type.
*
* @param string $popup_type Type of the popup for TinyMCE.
*/
$this->learndash_admin_shortcodes_assets['popup_type'] = apply_filters( 'learndash_shortcodes_popup_type', LEARNDASH_ADMIN_POPUP_STYLE );
$this->learndash_admin_shortcodes_assets['typenow'] = $typenow;
$this->learndash_admin_shortcodes_assets['pagenow'] = $pagenow;
$this->learndash_admin_shortcodes_assets['nonce'] = wp_create_nonce( 'learndash_admin_shortcodes_assets_nonce_' . get_current_user_id() . '_' . $pagenow );
}
}
/**
* Enqueue Editor
*
* Fires on `wp_enqueue_editor` hook.
*
* @since 3.0.7
*
* @param array $editor_args Editor args array.
*/
public function wp_enqueue_editor( $editor_args = array() ) {
$this->shortcodes_assets_init();
if ( 'thickbox' === $this->learndash_admin_shortcodes_assets['popup_type'] ) {
wp_enqueue_style( 'thickbox' );
wp_enqueue_script( 'thickbox' );
} elseif ( 'jQuery-dialog' === $this->learndash_admin_shortcodes_assets['popup_type'] ) {
wp_enqueue_script( 'jquery-ui-dialog' ); // jquery and jquery-ui should be dependencies, didn't check though...
wp_enqueue_style( 'wp-jquery-ui-dialog' );
}
if ( ( isset( $editor_args['tinymce'] ) ) && ( true === $editor_args['tinymce'] ) ) {
$this->add_button();
}
if ( ( isset( $editor_args['quicktags'] ) ) && ( true === $editor_args['quicktags'] ) ) {
add_action( 'admin_print_footer_scripts', array( $this, 'qt_button_script' ) );
}
}
/**
* Quicktags hooks
*
* Fires on `admin_print_footer_scripts` hook.
*
* @since 2.4.0
*/
public function qt_button_script() {
?>
<script type="text/javascript">
if (typeof QTags !== 'undefined') {
QTags.addButton( 'learndash_shortcodes', '[ld]', learndash_shortcodes_qt_callback, '', '', '', 'LearnDash Shortcodes' );
// In the QTags.addButton we need to call this intermediate function because learndash_shortcodes is now loaded yet.
function learndash_shortcodes_qt_callback() {
learndash_shortcodes.qt_callback();
}
}
</script>
<?php
}
/**
* Add TinyMCE buttons
*
* @since 2.4.0
*/
public function add_button() {
add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugin' ), 1 );
add_filter( 'mce_buttons', array( $this, 'register_button' ), 1 );
}
/**
* Add TinyMCE support
*
* @since 2.4.0
*
* @param array $plugin_array Array of TinyMCE plugins.
*
* @return array
*/
public function add_tinymce_plugin( $plugin_array ) {
$plugin_array['learndash_shortcodes_tinymce'] = LEARNDASH_LMS_PLUGIN_URL . 'assets/js/learndash-admin-shortcodes-tinymce' . learndash_min_asset() . '.js';
return $plugin_array;
}
/**
* Register TinyMCE button callback
*
* @since 2.4.0
*
* @param array $buttons Array of buttons.
*/
public function register_button( $buttons ) {
array_push( $buttons, 'learndash_shortcodes_tinymce' );
return $buttons;
}
/**
* Load admin scripts
*
* @since 2.4.0
*/
public function load_admin_scripts() {
global $typenow, $pagenow;
global $learndash_assets_loaded;
wp_enqueue_style(
'sfwd-module-style',
LEARNDASH_LMS_PLUGIN_URL . 'assets/css/sfwd_module' . learndash_min_asset() . '.css',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN
);
$learndash_assets_loaded['styles']['sfwd-module-style'] = __FUNCTION__;
wp_enqueue_script(
'sfwd-module-script',
LEARNDASH_LMS_PLUGIN_URL . 'assets/js/sfwd_module' . learndash_min_asset() . '.js',
array( 'jquery' ),
LEARNDASH_SCRIPT_VERSION_TOKEN,
true
);
$learndash_assets_loaded['scripts']['sfwd-module-script'] = __FUNCTION__;
$data = array();
$data['ajaxurl'] = admin_url( 'admin-ajax.php' );
$data = array( 'json' => wp_json_encode( $data ) );
wp_localize_script( 'sfwd-module-script', 'sfwd_data', $data );
wp_enqueue_style(
'learndash_admin_shortcodes_style',
LEARNDASH_LMS_PLUGIN_URL . 'assets/css/learndash-admin-shortcodes' . learndash_min_asset() . '.css',
array(),
LEARNDASH_SCRIPT_VERSION_TOKEN
);
wp_style_add_data( 'learndash_admin_shortcodes_style', 'rtl', 'replace' );
$learndash_assets_loaded['styles']['learndash_shortcodes_admin_style'] = __FUNCTION__;
$this->shortcodes_assets_init();
wp_enqueue_script(
'learndash_admin_shortcodes_script',
LEARNDASH_LMS_PLUGIN_URL . 'assets/js/learndash-admin-shortcodes' . learndash_min_asset() . '.js',
array( 'jquery' ),
LEARNDASH_SCRIPT_VERSION_TOKEN,
true
);
$learndash_assets_loaded['styles']['learndash_admin_shortcodes_script'] = __FUNCTION__;
wp_localize_script( 'learndash_admin_shortcodes_script', 'learndash_admin_shortcodes_assets', $this->learndash_admin_shortcodes_assets );
if ( 'jQuery-dialog' === $this->learndash_admin_shortcodes_assets['popup_type'] ) {
// Hold until after LD 3.0 release.
learndash_admin_settings_page_assets();
}
}
/**
* Button callback content
*
* @since 2.4.0
*/
public function learndash_generate_shortcodes_content() {
if ( ( ! isset( $_POST['atts'] ) ) || ( empty( $_POST['atts'] ) ) ) {
die();
}
$fields_args = array(
'typenow' => '',
'pagenow' => '',
'nonce' => '',
);
// The wp_verify_nonce() call is just a few lines below.
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$fields_args = shortcode_atts( $fields_args, $_POST['atts'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing
if ( ( empty( $fields_args['nonce'] ) ) || ( empty( $fields_args['pagenow'] ) ) ) {
die();
}
if ( ( empty( $fields_args['post_type'] ) ) && ( ! empty( $fields_args['typenow'] ) ) ) { // @phpstan-ignore-line -- false positive of does not exist.
$fields_args['post_type'] = $fields_args['typenow'];
}
if ( ! wp_verify_nonce( $fields_args['nonce'], 'learndash_admin_shortcodes_assets_nonce_' . get_current_user_id() . '_' . $fields_args['pagenow'] ) ) {
die();
}
$shortcode_sections = array();
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/class-ld-shortcodes-sections.php';
if ( 'sfwd-certificates' !== $fields_args['typenow'] ) {
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/learndash_login.php';
$shortcode_sections['learndash_login'] = new LearnDash_Shortcodes_Section_learndash_login( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_profile.php';
$shortcode_sections['ld_profile'] = new LearnDash_Shortcodes_Section_ld_profile( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_course_list.php';
$shortcode_sections['ld_course_list'] = new LearnDash_Shortcodes_Section_ld_course_list( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_lesson_list.php';
$shortcode_sections['ld_lesson_list'] = new LearnDash_Shortcodes_Section_ld_lesson_list( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_topic_list.php';
$shortcode_sections['ld_topic_list'] = new LearnDash_Shortcodes_Section_ld_topic_list( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_quiz_list.php';
$shortcode_sections['ld_quiz_list'] = new LearnDash_Shortcodes_Section_ld_quiz_list( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/learndash_course_progress.php';
$shortcode_sections['learndash_course_progress'] = new LearnDash_Shortcodes_Section_learndash_course_progress( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/visitor.php';
$shortcode_sections['visitor'] = new LearnDash_Shortcodes_Section_visitor( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/student.php';
$shortcode_sections['student'] = new LearnDash_Shortcodes_Section_student( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/course_complete.php';
$shortcode_sections['course_complete'] = new LearnDash_Shortcodes_Section_course_complete( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/course_inprogress.php';
$shortcode_sections['course_inprogress'] = new LearnDash_Shortcodes_Section_course_inprogress( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/course_notstarted.php';
$shortcode_sections['course_notstarted'] = new LearnDash_Shortcodes_Section_course_notstarted( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_course_resume.php';
$shortcode_sections['ld_course_resume'] = new LearnDash_Shortcodes_Section_ld_course_resume( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_course_info.php';
$shortcode_sections['ld_course_info'] = new LearnDash_Shortcodes_Section_ld_course_info( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_user_course_points.php';
$shortcode_sections['ld_user_course_points'] = new LearnDash_Shortcodes_Section_ld_user_course_points( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/user_groups.php';
$shortcode_sections['user_groups'] = new LearnDash_Shortcodes_Section_user_groups( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_group.php';
$shortcode_sections['ld_group'] = new LearnDash_Shortcodes_Section_ld_group( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_group_list.php';
$shortcode_sections['ld_group_list'] = new LearnDash_Shortcodes_Section_ld_group_list( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/learndash_payment_buttons.php';
$shortcode_sections['learndash_payment_buttons'] = new LearnDash_Shortcodes_Section_learndash_payment_buttons( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/course_content.php';
$shortcode_sections['course_content'] = new LearnDash_Shortcodes_Section_course_content( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_navigation.php';
$shortcode_sections['ld_navigation'] = new LearnDash_Shortcodes_Section_ld_navigation( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_course_expire_status.php';
$shortcode_sections['ld_course_expire_status'] = new LearnDash_Shortcodes_Section_ld_course_expire_status( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_certificate.php';
$shortcode_sections['ld_certificate'] = new LearnDash_Shortcodes_Section_ld_certificate( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_quiz_complete.php';
$shortcode_sections['ld_quiz_complete'] = new LearnDash_Shortcodes_Section_ld_quiz_complete( $fields_args );
if ( ( 'sfwd-lessons' === $fields_args['typenow'] ) || ( 'sfwd-topic' === $fields_args['typenow'] ) ) {
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_video.php';
$shortcode_sections['ld_video'] = new LearnDash_Shortcodes_Section_ld_video( $fields_args );
}
}
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/courseinfo.php';
$shortcode_sections['courseinfo'] = new LearnDash_Shortcodes_Section_courseinfo( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/groupinfo.php';
$shortcode_sections['groupinfo'] = new LearnDash_Shortcodes_Section_groupinfo( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/quizinfo.php';
$shortcode_sections['quizinfo'] = new LearnDash_Shortcodes_Section_quizinfo( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/usermeta.php';
$shortcode_sections['usermeta'] = new LearnDash_Shortcodes_Section_usermeta( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_registration.php';
$shortcode_sections['ld_registration'] = new LearnDash_Shortcodes_Section_ld_registration( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_infobar.php';
$shortcode_sections['ld_infobar'] = new LearnDash_Shortcodes_Section_ld_infobar( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/ld_materials.php';
$shortcode_sections['ld_materials'] = new LearnDash_Shortcodes_Section_ld_materials( $fields_args );
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/settings/shortcodes-sections/learndash_user_status.php';
$shortcode_sections['learndash_user_status'] = new LearnDash_Shortcodes_Section_learndash_user_status( $fields_args );
/**
* Filters TinyMCE shortcode content arguments.
*
* @param array $shortcode_content The shortcode content arguments.
*/
$shortcode_sections = apply_filters( 'learndash_shortcodes_content_args', $shortcode_sections );
?>
<div id="learndash_shortcodes_wrap" class="wrap sfwd_options">
<div id="learndash_shortcodes_tabs">
<ul>
<?php foreach ( $shortcode_sections as $section ) { ?>
<li><a data-nav="<?php echo esc_attr( $section->get_shortcodes_section_key() ); ?>" href="#"><?php echo wp_kses_post( $section->get_shortcodes_section_title() ); ?></a></li>
<?php } ?>
</ul>
</div>
<div id="learndash_shortcodes_sections">
<?php foreach ( $shortcode_sections as $section ) { ?>
<div id="tabs-<?php echo esc_attr( $section->get_shortcodes_section_key() ); ?>" class="hidable wrap" style="display: none;">
<?php echo $section->show_section_fields(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML. ?>
</div>
<?php } ?>
</div>
</div>
<?php
// }
die();
}
// End of functions.
}
}
add_action(
'plugins_loaded',
function() {
new LearnDash_Shortcodes_TinyMCE();
}
);