functions.php 3.6 KB

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