-
Notifications
You must be signed in to change notification settings - Fork 199
/
includes_course-theme_class-sensei-course-theme-template-selection.php.html
324 lines (270 loc) · 20.3 KB
/
includes_course-theme_class-sensei-course-theme-template-selection.php.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Source: includes/course-theme/class-sensei-course-theme-template-selection.php - Sensei LMS Hook Docs</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono|IBM+Plex+Sans:300,400|Playfair+Display:900&display=swap" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="styles.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: includes/course-theme/class-sensei-course-theme-template-selection.php</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code><?php
/**
* The class file for Sensei_Course_Theme_Template_Selection.
*
* @author Automattic
* @package Sensei
* @version 4.7.0
*/
/**
* Class Sensei_Course_Theme_Template_Selection
*/
class Sensei_Course_Theme_Template_Selection {
/**
* The default Learning Mode block template name.
*
* @var string
*/
const DEFAULT_TEMPLATE_NAME = 'default';
/**
* The seperator used when appending the template name to
* post_name.
*
* @var string
*/
const TEMPLATE_NAME_SEPERATOR = '___';
/**
* Sensei_Course_Theme constructor. Prevents other instances from being created outside of `self::instance()`.
*/
private function __construct() {
}
/**
* Instance of class.
*
* @var self
*/
private static $instance;
/**
* Fetches an instance of the class.
*
* @return self
*/
public static function instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize the class.
*/
public function init() {
add_action( 'save_post', [ $this, 'maybe_set_block_template_name' ], 10, 3 );
add_action( 'update_option_sensei-settings', [ $this, 'update_legacy_template_naming' ], 10, 3 );
}
/**
* Returns the templates info.
*
* @return Sensei_Course_Theme_Template[]
*/
public static function get_templates(): array {
$base_path = Sensei_Course_Theme::instance()->get_course_theme_root() . '/templates';
$base_url = Sensei_Course_Theme::instance()->get_course_theme_root_url() . '/templates';
$quiz_path = "$base_path/quiz.html";
$upsell = [
'title' => __( 'Upgrade to Pro', 'sensei-lms' ),
'tag' => __( 'Premium', 'sensei-lms' ),
'url' => 'https://senseilms.com/sensei-pro/?utm_source=plugin_sensei&utm_medium=upsell&utm_campaign=learning-mode-themes',
];
$templates = [
'default' => new Sensei_Course_Theme_Template(
[
'name' => 'default',
'title' => __( 'Default', 'sensei-lms' ),
'content' => [
'lesson' => "$base_path/default/lesson.html",
'quiz' => $quiz_path,
],
'version' => '1.0.0',
'styles' => [ Sensei()->assets->asset_url( 'course-theme/themes/default-theme.css' ) ],
'scripts' => [],
'screenshots' => [
'thumbnail' => "$base_url/default/img-thumb.jpg",
'full' => "$base_url/default/img-full.jpg",
],
]
),
'modern' => new Sensei_Course_Theme_Template(
[
'name' => 'modern',
'title' => __( 'Modern', 'sensei-lms' ),
'content' => [
'lesson' => '',
'quiz' => $quiz_path,
],
'version' => '1.0.0',
'styles' => [],
'scripts' => [],
'screenshots' => [
'thumbnail' => "$base_url/modern/img-thumb.jpg",
'full' => "$base_url/modern/img-full.jpg",
],
'upsell' => $upsell,
]
),
'video' => new Sensei_Course_Theme_Template(
[
'name' => 'video',
'title' => __( 'Video', 'sensei-lms' ),
'content' => [
'lesson' => '',
'quiz' => $quiz_path,
],
'version' => '1.0.0',
'styles' => [],
'scripts' => [],
'screenshots' => [
'thumbnail' => "$base_url/video/img-thumb.jpg",
'full' => "$base_url/video/img-full.jpg",
],
'upsell' => $upsell,
]
),
'video-full' => new Sensei_Course_Theme_Template(
[
'name' => 'video-full',
'title' => __( 'Large Video', 'sensei-lms' ),
'content' => [
'lesson' => '',
'quiz' => $quiz_path,
],
'version' => '1.0.0',
'styles' => [],
'scripts' => [],
'screenshots' => [
'thumbnail' => "$base_url/video-full/img-thumb.jpg",
'full' => "$base_url/video-full/img-full.jpg",
],
'upsell' => $upsell,
]
),
];
/**
* Filters the Learning Mode block templates list. Allows to add additional ones too.
*
* @since 4.7.0
* @hook sensei_learning_mode_block_templates
*
* @param Sensei_Course_Theme_Template[] $templates {
* The list of Learning Mode block templates. If adding a new template then it's key
* should be the template name.
*
* @return Sensei_Course_Theme_Template[] The list of extra learning mode block templates.
*/
return apply_filters( 'sensei_learning_mode_block_templates', $templates );
}
/**
* Retrieves the block template data that is currently activated in the settings.
*/
public static function get_active_template(): Sensei_Course_Theme_Template {
$active_template = \Sensei()->settings->get( 'sensei_learning_mode_template' );
$templates = self::get_templates();
$default_template = $templates[ self::DEFAULT_TEMPLATE_NAME ];
if ( isset( $templates[ $active_template ] ) ) {
$template = $templates[ $active_template ];
// In case the selected template does not have the template contents somehow
// supply the default template contents.
if ( ! isset( $template->content ) ) {
$template->content = [];
}
// Make sure the lesson content is not empty.
if ( empty( $template->content['lesson'] ) ) {
$template->content['lesson'] = $default_template->content['lesson'];
}
// Make sure the quiz content is not empty.
if ( empty( $template->content['quiz'] ) ) {
$template->content['quiz'] = $default_template->content['quiz'];
}
} else {
$template = $default_template;
}
return $template;
}
/**
* Get active LM template name.
*/
public static function get_active_template_name(): string {
return \Sensei()->settings->get( 'sensei_learning_mode_template' ) ?? self::DEFAULT_TEMPLATE_NAME;
}
/**
* Sets the LM template name as a post_name suffix for the custom LM template in the db.
*
* @param int $post_id The id of the post saved.
* @param WP_Post $post The instance of the post that was saved.
* @param boolean $update Whether this was an update of the existing post or not.
*/
public function maybe_set_block_template_name( $post_id, $post, $update ) {
if (
// We set the template name meta only for wp_template post types.
'wp_template' !== $post->post_type ||
// We set the template name meta only for templates for lesson and quiz posts.
! in_array( $post->post_name, [ 'lesson', 'quiz' ], true )
) {
return;
}
$active_template_name = self::get_active_template_name();
$post->post_name = $post->post_name . self::TEMPLATE_NAME_SEPERATOR . $active_template_name;
wp_update_post( $post );
}
/**
* Updates the legacy lm template naming in the db.
*
* @param mixed $old_settings The old value of the option.
* @param mixed $new_settings The new value of the option.
* @param string $option The name of the option being updated.
*/
public function update_legacy_template_naming( $old_settings, $new_settings, $option ) {
$key = 'sensei_learning_mode_template';
$old_template = empty( $old_settings[ $key ] ) ? self::DEFAULT_TEMPLATE_NAME : $old_settings[ $key ];
$new_template = empty( $new_settings[ $key ] ) ? self::DEFAULT_TEMPLATE_NAME : $new_settings[ $key ];
// Do nothing if active lm template hasn't changed.
if ( $old_template === $new_template ) {
return;
}
// Get lm templates that are stored in the db.
$db_templates = Sensei_Course_Theme_Templates::get_db_templates();
// Check each lm template and append a template name postfix if they don't have it.
foreach ( $db_templates as $db_template ) {
if ( in_array( $db_template->post_name, [ 'lesson', 'quiz' ], true ) ) {
$db_template->post_name = $db_template->post_name . self::TEMPLATE_NAME_SEPERATOR . self::DEFAULT_TEMPLATE_NAME;
wp_update_post( $db_template );
}
}
}
}
</code></pre>
</article>
</section>
<footer>
<a href="https://senseilms.com/">SenseiLMS.com</a> •
<a href="https://github.com/automattic/sensei">Sensei LMS on GitHub</a>
</footer>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Actions</h3><ul><li><a href="sensei_course_learning_mode_load_override_styles.html">sensei_course_learning_mode_load_override_styles</a></li><li><a href="sensei_course_learning_mode_load_theme.html">sensei_course_learning_mode_load_theme</a></li><li><a href="sensei_course_learning_mode_override_theme.html">sensei_course_learning_mode_override_theme</a></li><li><a href="sensei_course_new_teacher_assigned.html">sensei_course_new_teacher_assigned</a></li><li><a href="sensei_data_port_complete.html">sensei_data_port_complete</a></li><li><a href="sensei_disable_legacy_emails.html">sensei_disable_legacy_emails</a></li><li><a href="sensei_email_send.html">sensei_email_send</a></li><li><a href="sensei_grading_allowed_user_ids.html">sensei_grading_allowed_user_ids</a></li><li><a href="sensei_loaded.html">sensei_loaded</a></li><li><a href="sensei_login_success_redirect_url.html">sensei_login_success_redirect_url</a></li><li><a href="sensei_new_private_message.html">sensei_new_private_message</a></li><li><a href="sensei_reports_overview_after_top_filters.html">sensei_reports_overview_after_top_filters</a></li><li><a href="sensei_reports_overview_before_top_filters.html">sensei_reports_overview_before_top_filters</a></li><li><a href="sensei_reports_overview_students_data_provider_pre_user_query.html">sensei_reports_overview_students_data_provider_pre_user_query</a></li><li><a href="sensei_rest_api_category_question_saved.html">sensei_rest_api_category_question_saved</a></li><li><a href="sensei_rest_api_lesson_quiz_update.html">sensei_rest_api_lesson_quiz_update</a></li><li><a href="sensei_rest_api_question_saved.html">sensei_rest_api_question_saved</a></li><li><a href="sensei_settings_after_links.html">sensei_settings_after_links</a></li><li><a href="sensei_tools_listing_after_%257B$tool_id%257D.html">sensei_tools_listing_after_{$tool_id}</a></li></ul><h3>Filters</h3><ul><li><a href="sensei_add_comment_indexes.html">sensei_add_comment_indexes</a></li><li><a href="sensei_admin_notices.html">sensei_admin_notices</a></li><li><a href="sensei_analysis_nav_title.html">sensei_analysis_nav_title</a></li><li><a href="sensei_analysis_sub_menu.html">sensei_analysis_sub_menu</a></li><li><a href="sensei_block_type_args.html">sensei_block_type_args</a></li><li><a href="sensei_can_access_course_content.html">sensei_can_access_course_content</a></li><li><a href="sensei_can_use_users_relationship.html">sensei_can_use_users_relationship</a></li><li><a href="sensei_context_notices.html">sensei_context_notices</a></li><li><a href="sensei_course_block_template.html">sensei_course_block_template</a></li><li><a href="sensei_course_completed_page_template.html">sensei_course_completed_page_template</a></li><li><a href="sensei_course_completed_page_url.html">sensei_course_completed_page_url</a></li><li><a href="sensei_course_learning_mode_enabled.html">sensei_course_learning_mode_enabled</a></li><li><a href="sensei_course_learning_mode_theme_override_enabled.html">sensei_course_learning_mode_theme_override_enabled</a></li><li><a href="sensei_course_loop_content_class.html">sensei_course_loop_content_class</a></li><li><a href="sensei_course_open_access.html">sensei_course_open_access</a></li><li><a href="sensei_courses_page_template.html">sensei_courses_page_template</a></li><li><a href="sensei_custom_navigation_allowed_screens.html">sensei_custom_navigation_allowed_screens</a></li><li><a href="sensei_default_feature_flag_settings.html">sensei_default_feature_flag_settings</a></li><li><a href="sensei_duplicate_post_args.html">sensei_duplicate_post_args</a></li><li><a href="sensei_duplicate_post_ignore_meta.html">sensei_duplicate_post_ignore_meta</a></li><li><a href="sensei_email_allowed_stylesheets.html">sensei_email_allowed_stylesheets</a></li><li><a href="sensei_email_generators.html">sensei_email_generators</a></li><li><a href="sensei_email_is_available.html">sensei_email_is_available</a></li><li><a href="sensei_email_list_columns.html">sensei_email_list_columns</a></li><li><a href="sensei_email_list_row_actions.html">sensei_email_list_row_actions</a></li><li><a href="sensei_email_list_row_data.html">sensei_email_list_row_data</a></li><li><a href="sensei_email_replacements.html">sensei_email_replacements</a></li><li><a href="sensei_email_seeder_data.html">sensei_email_seeder_data</a></li><li><a href="sensei_email_styles.html">sensei_email_styles</a></li><li><a href="sensei_email_stylesheets.html">sensei_email_stylesheets</a></li><li><a href="sensei_existing_questions_query_results.html">sensei_existing_questions_query_results</a></li><li><a href="sensei_feature_open_access_courses.html">sensei_feature_open_access_courses</a></li><li><a href="sensei_feature_preview_students.html">sensei_feature_preview_students</a></li><li><a href="sensei_file_upload_file_prefix.html">sensei_file_upload_file_prefix</a></li><li><a href="sensei_filter_category_questions_by_author.html">sensei_filter_category_questions_by_author</a></li><li><a href="sensei_filter_module_terms_by_owner.html">sensei_filter_module_terms_by_owner</a></li><li><a href="sensei_get_question_grade.html">sensei_get_question_grade</a></li><li><a href="sensei_get_question_template_data.html">sensei_get_question_template_data</a></li><li><a href="sensei_grading_display_quiz_question.html">sensei_grading_display_quiz_question</a></li><li><a href="sensei_guest_user_supported_actions.html">sensei_guest_user_supported_actions</a></li><li><a href="sensei_home_is_plugin_licensed_%257B$plugin_slug%257D.html">sensei_home_is_plugin_licensed_{$plugin_slug}</a></li><li><a href="sensei_home_promo_banner_show.html">sensei_home_promo_banner_show</a></li><li><a href="sensei_home_remote_data_other_plugins.html">sensei_home_remote_data_other_plugins</a></li><li><a href="sensei_home_remote_data_primary_plugin_slug.html">sensei_home_remote_data_primary_plugin_slug</a></li><li><a href="sensei_home_remote_data_retry_error.html">sensei_home_remote_data_retry_error</a></li><li><a href="sensei_home_support_ticket_creation_upsell_show.html">sensei_home_support_ticket_creation_upsell_show</a></li><li><a href="sensei_icon_href.html">sensei_icon_href</a></li><li><a href="sensei_import_csv_delimiter.html">sensei_import_csv_delimiter</a></li><li><a href="sensei_import_csv_delimiter_options.html">sensei_import_csv_delimiter_options</a></li><li><a href="sensei_is_login_required.html">sensei_is_login_required</a></li><li><a href="sensei_learner_courses_page_template.html">sensei_learner_courses_page_template</a></li><li><a href="sensei_learner_enrolled_courses_args.html">sensei_learner_enrolled_courses_args</a></li><li><a href="sensei_learners_query.html">sensei_learners_query</a></li><li><a href="sensei_learning_mode_block_templates.html">sensei_learning_mode_block_templates</a></li><li><a href="sensei_lesson_archive_title.html">sensei_lesson_archive_title</a></li><li><a href="sensei_lesson_block_template.html">sensei_lesson_block_template</a></li><li><a href="sensei_lesson_content_drip_hide.html">sensei_lesson_content_drip_hide</a></li><li><a href="sensei_lesson_count.html">sensei_lesson_count</a></li><li><a href="sensei_lesson_course_signup_notice_level.html">sensei_lesson_course_signup_notice_level</a></li><li><a href="sensei_lesson_course_signup_notice_message.html">sensei_lesson_course_signup_notice_message</a></li><li><a href="sensei_lesson_excerpt.html">sensei_lesson_excerpt</a></li><li><a href="sensei_lesson_placeholder_image_url.html">sensei_lesson_placeholder_image_url</a></li><li><a href="sensei_lesson_prerequisite.html">sensei_lesson_prerequisite</a></li><li><a href="sensei_lesson_quiz_questions.html">sensei_lesson_quiz_questions</a></li><li><a href="sensei_lesson_show_course_signup_notice.html">sensei_lesson_show_course_signup_notice</a></li><li><a href="sensei_lesson_take_course_url.html">sensei_lesson_take_course_url</a></li><li><a href="sensei_list_bulk_actions.html">sensei_list_bulk_actions</a></li><li><a href="sensei_login_url.html">sensei_login_url</a></li><li><a href="sensei_module_course_column_max_links_count.html">sensei_module_course_column_max_links_count</a></li><li><a href="sensei_notice.html">sensei_notice</a></li><li><a href="sensei_question_answer_message_correct_answer.html">sensei_question_answer_message_correct_answer</a></li><li><a href="sensei_question_answer_message_css_class.html">sensei_question_answer_message_css_class</a></li><li><a href="sensei_question_answer_message_grade.html">sensei_question_answer_message_grade</a></li><li><a href="sensei_question_answer_message_text.html">sensei_question_answer_message_text</a></li><li><a href="sensei_question_answer_notes.html">sensei_question_answer_notes</a></li><li><a href="sensei_question_image_size.html">sensei_question_image_size</a></li><li><a href="sensei_question_show_answers.html">sensei_question_show_answers</a></li><li><a href="sensei_question_title.html">sensei_question_title</a></li><li><a href="sensei_question_type_specific_properties.html">sensei_question_type_specific_properties</a></li><li><a href="sensei_question_types.html">sensei_question_types</a></li><li><a href="sensei_questions_get_correct_answer.html">sensei_questions_get_correct_answer</a></li><li><a href="sensei_quiz_answer_file_upload_types.html">sensei_quiz_answer_file_upload_types</a></li><li><a href="sensei_quiz_enable_block_based_editor.html">sensei_quiz_enable_block_based_editor</a></li><li><a href="sensei_quiz_ordering_question_type_hide.html">sensei_quiz_ordering_question_type_hide</a></li><li><a href="sensei_quiz_pagination_args.html">sensei_quiz_pagination_args</a></li><li><a href="sensei_quiz_panel_add.html">sensei_quiz_panel_add</a></li><li><a href="sensei_quiz_settings.html">sensei_quiz_settings</a></li><li><a href="sensei_registration_url.html">sensei_registration_url</a></li><li><a href="sensei_reports_overview_export_button_url.html">sensei_reports_overview_export_button_url</a></li><li><a href="sensei_rest_api_lesson_quiz_response.html">sensei_rest_api_lesson_quiz_response</a></li><li><a href="sensei_rest_api_schema_question_type.html">sensei_rest_api_schema_question_type</a></li><li><a href="sensei_rest_api_schema_single_question.html">sensei_rest_api_schema_single_question</a></li><li><a href="sensei_scripts_allowed_pages.html">sensei_scripts_allowed_pages</a></li><li><a href="sensei_scripts_allowed_post_type_pages.html">sensei_scripts_allowed_post_type_pages</a></li><li><a href="sensei_scripts_allowed_post_types.html">sensei_scripts_allowed_post_types</a></li><li><a href="sensei_settings_content.html">sensei_settings_content</a></li><li><a href="sensei_show_admin_notices_%257B$screen_id%257D.html">sensei_show_admin_notices_{$screen_id}</a></li><li><a href="sensei_show_enrolment_debug_button.html">sensei_show_enrolment_debug_button</a></li><li><a href="sensei_show_lesson_numbers.html">sensei_show_lesson_numbers</a></li><li><a href="sensei_student_groups_hide.html">sensei_student_groups_hide</a></li><li><a href="sensei_students_report_last_activity_filter_enabled.html">sensei_students_report_last_activity_filter_enabled</a></li><li><a href="sensei_the_question_number.html">sensei_the_question_number</a></li><li><a href="sensei_tools.html">sensei_tools</a></li><li><a href="sensei_usage_tracking_data.html">sensei_usage_tracking_data</a></li><li><a href="sensei_user_courses_filter_options.html">sensei_user_courses_filter_options</a></li><li><a href="sensei_user_courses_query.html">sensei_user_courses_query</a></li><li><a href="sensei_wpcom_product_slugs.html">sensei_wpcom_product_slugs</a></li></ul>
</nav>
<br class="clear">
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>