functions.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Course functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Course
  8. * @since Course 1.0
  9. */
  10. if ( ! function_exists( 'course_support' ) ) :
  11. /**
  12. * Sets up theme defaults and registers support for various WordPress features.
  13. *
  14. * @since Course 1.0
  15. *
  16. * @return void
  17. */
  18. function course_support() {
  19. add_theme_support( 'sensei-learning-mode' );
  20. // Enqueue editor styles.
  21. add_editor_style( 'style.css' );
  22. }
  23. endif;
  24. add_action( 'after_setup_theme', 'course_support' );
  25. if ( ! function_exists( 'course_scripts' ) ) :
  26. /**
  27. * Enqueue scripts and styles.
  28. *
  29. * @since Course 1.0
  30. *
  31. * @return void
  32. */
  33. function course_scripts() {
  34. wp_register_style( 'course-style', get_stylesheet_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
  35. wp_enqueue_script( 'course-header', get_template_directory_uri() . '/assets/js/header.js', array(), wp_get_theme()->get( 'Version' ), true );
  36. wp_enqueue_style( 'course-style' );
  37. }
  38. endif;
  39. add_action( 'wp_enqueue_scripts', 'course_scripts' );
  40. function course_theme_init() {
  41. register_block_style(
  42. 'core/navigation-link',
  43. array(
  44. 'name' => 'navigation-link-button',
  45. 'label' => __( 'Button', 'course' ),
  46. )
  47. );
  48. }
  49. add_action( 'init', 'course_theme_init' );
  50. /**
  51. * Register Sensei LMS block patterns category.
  52. */
  53. function course_register_block_patterns_category() {
  54. register_block_pattern_category(
  55. 'sensei-lms',
  56. array( 'label' => 'Sensei LMS' )
  57. );
  58. }
  59. add_action( 'init', 'course_register_block_patterns_category' );
  60. add_filter(
  61. 'theme_lesson_templates',
  62. 'course_theme_filter_single_lesson_template_for_sensei_learning_mode',
  63. 11,
  64. 3
  65. );
  66. add_filter( 'body_class', 'add_body_class_for_variation' );
  67. add_filter( 'admin_body_class', 'add_body_class_for_variation' );
  68. add_action( 'course_theme_variation_loaded', 'enqueue_style_for_variation' );
  69. /**
  70. * Filter the list of templates for the single lesson page.
  71. *
  72. * @param array $page_templates Array of page templates.
  73. * @param string $theme The current theme.
  74. * @param WP_Post $post The post being edited, provided for context, or null.
  75. *
  76. * @since Course 1.3.2
  77. *
  78. * @return array Array of page templates.
  79. */
  80. function course_theme_filter_single_lesson_template_for_sensei_learning_mode( $page_templates, $theme, $post ) {
  81. // In case some other plugin has a post type called lesson.
  82. if ( ! $post || ! class_exists( 'Sensei_Main' ) ) {
  83. return $page_templates;
  84. }
  85. $course_id = Sensei()->lesson->get_course_id( $post->ID );
  86. $is_learning_mode_enabled = Sensei_Course_Theme_Option::has_learning_mode_enabled( $course_id );
  87. if ( $is_learning_mode_enabled ) {
  88. unset( $page_templates['single-lesson'] );
  89. }
  90. return $page_templates;
  91. }
  92. /**
  93. * Add a body class with the variation.
  94. * body_class hook passes classes as array whereas
  95. * admin_body_class passes them as string. So we handle both.
  96. *
  97. * @param array|string $classes Body classes.
  98. *
  99. * @internal
  100. *
  101. * @return array|string Body classes.
  102. */
  103. function add_body_class_for_variation( $classes ) {
  104. $css_string = wp_get_global_stylesheet( array( 'variables' ) );
  105. $property_name = '--wp--custom--course-theme-variation';
  106. $is_array = is_array( $classes );
  107. // 1. "/": Delimiters that mark the start and end of the regex pattern.
  108. // 2. "$property_name": This part of the pattern matches the specific property name, in our case, '--wp--custom--course-theme-variation', defined in Course theme's JSON files.
  109. // 3. "\s*": Matches zero or more whitespace characters.
  110. // 4. ":": Matches the colon you write to separate the CSS property name and property value.
  111. // 5. "\s*": Matches zero or more whitespace characters after the colon.
  112. // 6. "([^;]+)": This is a capturing group that matches one or more characters that are not a semicolon. It captures the value of the property.
  113. // 7. "/": The closing delimiter of the regex pattern.
  114. // Overall, this regex is designed to extract the value associated with a specific CSS property (defined in $property_name).
  115. $pattern = "/$property_name\s*:\s*([^;]+)/";
  116. $variation_name = 'default';
  117. if ( preg_match( $pattern, $css_string, $matches ) ) {
  118. // $matches[0] contains the full match.
  119. // $matches[1] contains the CSS value for the specified property.
  120. $css_value = trim( $matches[1] );
  121. $variation_name = $css_value;
  122. $class_value = 'is-' . $css_value;
  123. if ( $is_array ) {
  124. $classes[] = $class_value;
  125. } else {
  126. $classes .= ' ' . $class_value;
  127. }
  128. }
  129. /**
  130. * Action to perform variation specific tasks.
  131. *
  132. * @hook course_theme_variation_loaded Fires after determining which theme variation is loaded.
  133. * @since 1.3.5
  134. *
  135. * @param string $variation_name Name of the variation.
  136. */
  137. do_action( 'course_theme_variation_loaded', $variation_name );
  138. return $classes;
  139. }
  140. /**
  141. * Enqueue the specific stylesheet for the variation.
  142. *
  143. * @param string $variation_name The current theme variation.
  144. *
  145. * @since Course 1.3.5
  146. */
  147. function enqueue_style_for_variation( $variation_name ) {
  148. if ( empty( $variation_name ) ) {
  149. return;
  150. }
  151. wp_enqueue_style( 'course-theme-variation-style', get_template_directory_uri() . '/assets/css/' . $variation_name . '.css', array(), wp_get_theme()->get( 'Version' ) );
  152. }