Aucune description

post-details.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * The function to include Post Details in a theme's stylesheet.
  4. */
  5. function jetpack_post_details_enqueue_scripts() {
  6. // Make sure we can proceed.
  7. list( $should_run, $options, $definied, $post_details ) = jetpack_post_details_should_run();
  8. if ( ! $should_run ) {
  9. return;
  10. }
  11. list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
  12. list( $date, $categories, $tags, $author, $comment ) = $definied;
  13. $elements = array();
  14. // If date option is unticked, add it to the list of classes.
  15. if ( 1 != $date_option && ! empty( $date ) ) {
  16. $elements[] = $date;
  17. }
  18. // If categories option is unticked, add it to the list of classes.
  19. if ( 1 != $categories_option && ! empty( $categories ) ) {
  20. $elements[] = $categories;
  21. }
  22. // If tags option is unticked, add it to the list of classes.
  23. if ( 1 != $tags_option && ! empty( $tags ) ) {
  24. $elements[] = $tags;
  25. }
  26. // If author option is unticked, add it to the list of classes.
  27. if ( 1 != $author_option && ! empty( $author ) ) {
  28. $elements[] = $author;
  29. }
  30. // If comment option is unticked, add it to the list of classes.
  31. if ( 1 != $comment_option && ! empty( $comment ) ) {
  32. $elements[] = $comment;
  33. }
  34. // If the Elements array is empty, return without setting custom CSS.
  35. if ( empty( $elements ) ) {
  36. return;
  37. }
  38. // Get the list of classes.
  39. $elements = implode( ', ', $elements );
  40. // Hide the classes with CSS.
  41. $css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }';
  42. // Add the CSS to the stylesheet.
  43. wp_add_inline_style( $post_details['stylesheet'], $css );
  44. }
  45. add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' );
  46. /**
  47. * Adds custom classes to the array of body classes.
  48. */
  49. function jetpack_post_details_body_classes( $classes ) {
  50. // Make sure we can proceed.
  51. list( $should_run, $options, $definied ) = jetpack_post_details_should_run();
  52. if ( ! $should_run ) {
  53. return $classes;
  54. }
  55. list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
  56. list( $date, $categories, $tags, $author, $comment ) = $definied;
  57. // If date option is unticked, add a class of 'date-hidden' to the body.
  58. if ( 1 != $date_option && ! empty( $date ) ) {
  59. $classes[] = 'date-hidden';
  60. }
  61. // If categories option is unticked, add a class of 'categories-hidden' to the body.
  62. if ( 1 != $categories_option && ! empty( $categories ) ) {
  63. $classes[] = 'categories-hidden';
  64. }
  65. // If tags option is unticked, add a class of 'tags-hidden' to the body.
  66. if ( 1 != $tags_option && ! empty( $tags ) ) {
  67. $classes[] = 'tags-hidden';
  68. }
  69. // If author option is unticked, add a class of 'author-hidden' to the body.
  70. if ( 1 != $author_option && ! empty( $author ) ) {
  71. $classes[] = 'author-hidden';
  72. }
  73. // If comment option is unticked, add a class of 'comment-hidden' to the body.
  74. if ( 1 != $comment_option && ! empty( $comment ) ) {
  75. $classes[] = 'comment-hidden';
  76. }
  77. return $classes;
  78. }
  79. add_filter( 'body_class', 'jetpack_post_details_body_classes' );
  80. /**
  81. * Determines if Post Details should run.
  82. */
  83. function jetpack_post_details_should_run() {
  84. // Empty value representing falsy return value.
  85. $void = array( false, null, null, null );
  86. // If the theme doesn't support 'jetpack-content-options', don't continue.
  87. if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
  88. return $void;
  89. }
  90. $options = get_theme_support( 'jetpack-content-options' );
  91. $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
  92. // If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue.
  93. if ( empty( $post_details ) ) {
  94. return $void;
  95. }
  96. $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
  97. $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
  98. $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
  99. $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
  100. $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
  101. // If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue.
  102. if (
  103. empty( $post_details['stylesheet'] )
  104. && ( empty( $date )
  105. || empty( $categories )
  106. || empty( $tags )
  107. || empty( $author )
  108. || empty( $comment ) )
  109. ) {
  110. return $void;
  111. }
  112. $date_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_date', 1 );
  113. $categories_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_categories', 1 );
  114. $tags_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_tags', 1 );
  115. $author_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_author', 1 );
  116. $comment_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_comment', 1 );
  117. $options = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option );
  118. $definied = array( $date, $categories, $tags, $author, $comment );
  119. // If all the options are ticked, don't continue.
  120. if ( array( 1, 1, 1, 1, 1 ) === $options ) {
  121. return $void;
  122. }
  123. return array( true, $options, $definied, $post_details );
  124. }