functions.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_action( 'course_theme_variation_loaded', 'enqueue_style_for_variation' );
  68. /**
  69. * Filter the list of templates for the single lesson page.
  70. *
  71. * @param array $page_templates Array of page templates.
  72. * @param string $theme The current theme.
  73. * @param WP_Post $post The post being edited, provided for context, or null.
  74. *
  75. * @since Course 1.3.2
  76. *
  77. * @return array Array of page templates.
  78. */
  79. function course_theme_filter_single_lesson_template_for_sensei_learning_mode( $page_templates, $theme, $post ) {
  80. // In case some other plugin has a post type called lesson.
  81. if ( ! $post || ! class_exists( 'Sensei_Main' ) ) {
  82. return $page_templates;
  83. }
  84. $course_id = Sensei()->lesson->get_course_id( $post->ID );
  85. $is_learning_mode_enabled = Sensei_Course_Theme_Option::has_learning_mode_enabled( $course_id );
  86. if ( $is_learning_mode_enabled ) {
  87. unset( $page_templates['single-lesson'] );
  88. }
  89. return $page_templates;
  90. }
  91. /**
  92. * Add a body class with the variation.
  93. *
  94. * @param array $classes Body classes.
  95. *
  96. * @internal
  97. *
  98. * @return array Body classes.
  99. */
  100. function add_body_class_for_variation( $classes ) {
  101. $css_string = wp_get_global_stylesheet( array( 'variables' ) );
  102. $property_name = '--wp--custom--course-theme-variation';
  103. // 1. "/": Delimiters that mark the start and end of the regex pattern.
  104. // 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.
  105. // 3. "\s*": Matches zero or more whitespace characters.
  106. // 4. ":": Matches the colon you write to separate the CSS property name and property value.
  107. // 5. "\s*": Matches zero or more whitespace characters after the colon.
  108. // 6. "([^;]+)": This is a capturing group that matches one or more characters that are not a semicolon. It captures the value of the property.
  109. // 7. "/": The closing delimiter of the regex pattern.
  110. // Overall, this regex is designed to extract the value associated with a specific CSS property (defined in $property_name).
  111. $pattern = "/$property_name\s*:\s*([^;]+)/";
  112. $variation_name = 'default';
  113. if ( preg_match( $pattern, $css_string, $matches ) ) {
  114. // $matches[0] contains the full match.
  115. // $matches[1] contains the CSS value for the specified property.
  116. $css_value = trim( $matches[1] );
  117. $classes[] = 'is-' . $css_value;
  118. $variation_name = $css_value;
  119. }
  120. /**
  121. * Action to perform variation specific tasks.
  122. *
  123. * @hook course_theme_variation_loaded Fires after determining which theme variation is loaded.
  124. * @since 1.3.5
  125. *
  126. * @param string $variation_name Name of the variation.
  127. */
  128. do_action( 'course_theme_variation_loaded', $variation_name );
  129. return $classes;
  130. }
  131. /**
  132. * Enqueue the specific stylesheet for the variation.
  133. *
  134. * @param string $variation_name The current theme variation.
  135. *
  136. * @since Course 1.3.5
  137. */
  138. function enqueue_style_for_variation( $variation_name ) {
  139. if ( empty( $variation_name ) ) {
  140. return;
  141. }
  142. wp_enqueue_style( 'course-theme-variation-style', get_template_directory_uri() . '/assets/css/' . $variation_name . '.css', array(), wp_get_theme()->get( 'Version' ) );
  143. }