functions.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. add_editor_style( 'learning-mode.css' );
  23. }
  24. endif;
  25. add_action( 'after_setup_theme', 'course_support' );
  26. if ( ! function_exists( 'course_scripts' ) ) :
  27. /**
  28. * Enqueue scripts and styles.
  29. *
  30. * @since Course 1.0
  31. *
  32. * @return void
  33. */
  34. function course_scripts() {
  35. wp_register_style( 'course-style', get_stylesheet_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
  36. wp_enqueue_script( 'course-header', get_template_directory_uri() . '/assets/js/header.js', [], wp_get_theme()->get( 'Version' ), true );
  37. wp_enqueue_style( 'course-style' );
  38. /**
  39. * Temporary Hook to skip the learning mode style when the Sensei LMS is able to provide it.
  40. * It is only used to continue loading the deprecated styles if a old sensei version is installed.
  41. */
  42. $use_deprecated_style = apply_filters( 'course_learning_mode_load_styles', true );
  43. if ( class_exists( 'Sensei_Main' ) && $use_deprecated_style ) {
  44. wp_register_style( 'course-sensei-learning-mode', get_stylesheet_directory_uri() . '/learning-mode.css', array(), wp_get_theme()->get( 'Version' ) );
  45. wp_enqueue_style( 'course-sensei-learning-mode' );
  46. }
  47. }
  48. endif;
  49. add_action( 'wp_enqueue_scripts', 'course_scripts' );
  50. function course_theme_init() {
  51. add_option( 'course_theme_variation', 'default' );
  52. register_block_style(
  53. 'core/navigation-link',
  54. array(
  55. 'name' => 'navigation-link-button',
  56. 'label' => __( 'Button', 'course' ),
  57. )
  58. );
  59. }
  60. add_action( 'init', 'course_theme_init' );
  61. /**
  62. * Register Sensei LMS block patterns category.
  63. */
  64. function course_register_block_patterns_category() {
  65. register_block_pattern_category(
  66. 'sensei-lms',
  67. [ 'label' => 'Sensei LMS' ]
  68. );
  69. }
  70. add_action( 'init', 'course_register_block_patterns_category' );
  71. /**
  72. * Determine the theme variation and save in option.
  73. *
  74. * @param int $post_id Post ID.
  75. * @param WP_Post $post Post object.
  76. * @param bool $update Whether this is an existing post being updated or not.
  77. */
  78. function course_save_global_styles( $post_id, $post, $update ) {
  79. if ( 'wp_global_styles' !== $post->post_type ) {
  80. return;
  81. }
  82. $global_styles = json_decode( $post->post_content, true );
  83. $global['settings'] = isset( $global_styles['settings'] ) ? $global_styles['settings'] : array();
  84. $global['styles'] = isset( $global_styles['styles'] ) ? $global_styles['styles'] : array();
  85. $variations = WP_Theme_JSON_Resolver::get_style_variations();
  86. $current_variation = 'default';
  87. foreach ( $variations as $variation ) {
  88. if ( $variation['settings'] === $global['settings'] && $variation['styles'] === $global['styles'] ) {
  89. $current_variation = sanitize_title( $variation['title'] );
  90. }
  91. }
  92. update_option( 'course_theme_variation', $current_variation );
  93. }
  94. add_action( 'save_post', 'course_save_global_styles', 10, 3 );
  95. /**
  96. * Add the theme variation to the body class.
  97. *
  98. * @param array $classes Array of body classes.
  99. */
  100. function course_add_variation_body_class( $classes ) {
  101. $current_variation = get_option( 'course_theme_variation' );
  102. if ( ! $current_variation ) {
  103. return $classes;
  104. }
  105. $classes[] = 'is-' . $current_variation;
  106. return $classes;
  107. }
  108. add_action( 'body_class', 'course_add_variation_body_class' );