post-details.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Get the list of classes.
  35. $elements = implode( ', ', $elements );
  36. // Hide the classes with CSS.
  37. $css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }';
  38. // Add the CSS to the stylesheet.
  39. wp_add_inline_style( $post_details['stylesheet'], $css );
  40. }
  41. add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' );
  42. /**
  43. * Adds custom classes to the array of body classes.
  44. */
  45. function jetpack_post_details_body_classes( $classes ) {
  46. // Make sure we can proceed.
  47. list( $should_run, $options, $definied ) = jetpack_post_details_should_run();
  48. if ( ! $should_run ) {
  49. return $classes;
  50. }
  51. list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
  52. list( $date, $categories, $tags, $author, $comment ) = $definied;
  53. // If date option is unticked, add a class of 'date-hidden' to the body.
  54. if ( 1 != $date_option && ! empty( $date ) ) {
  55. $classes[] = 'date-hidden';
  56. }
  57. // If categories option is unticked, add a class of 'categories-hidden' to the body.
  58. if ( 1 != $categories_option && ! empty( $categories ) ) {
  59. $classes[] = 'categories-hidden';
  60. }
  61. // If tags option is unticked, add a class of 'tags-hidden' to the body.
  62. if ( 1 != $tags_option && ! empty( $tags ) ) {
  63. $classes[] = 'tags-hidden';
  64. }
  65. // If author option is unticked, add a class of 'author-hidden' to the body.
  66. if ( 1 != $author_option && ! empty( $author ) ) {
  67. $classes[] = 'author-hidden';
  68. }
  69. // If comment option is unticked, add a class of 'comment-hidden' to the body.
  70. if ( 1 != $comment_option && ! empty( $comment ) ) {
  71. $classes[] = 'comment-hidden';
  72. }
  73. return $classes;
  74. }
  75. add_filter( 'body_class', 'jetpack_post_details_body_classes' );
  76. /**
  77. * Determines if Post Details should run.
  78. */
  79. function jetpack_post_details_should_run() {
  80. // Empty value representing falsy return value.
  81. $void = array( false, null, null, null );
  82. // If the theme doesn't support 'jetpack-content-options', don't continue.
  83. if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
  84. return $void;
  85. }
  86. $options = get_theme_support( 'jetpack-content-options' );
  87. $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
  88. // If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue.
  89. if ( empty( $post_details ) ) {
  90. return $void;
  91. }
  92. $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
  93. $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
  94. $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
  95. $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
  96. $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
  97. // If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue.
  98. if (
  99. empty( $post_details['stylesheet'] )
  100. && ( empty( $date )
  101. || empty( $categories )
  102. || empty( $tags )
  103. || empty( $author )
  104. || empty( $comment ) )
  105. ) {
  106. return $void;
  107. }
  108. $date_option = get_option( 'jetpack_content_post_details_date', 1 );
  109. $categories_option = get_option( 'jetpack_content_post_details_categories', 1 );
  110. $tags_option = get_option( 'jetpack_content_post_details_tags', 1 );
  111. $author_option = get_option( 'jetpack_content_post_details_author', 1 );
  112. $comment_option = get_option( 'jetpack_content_post_details_comment', 1 );
  113. $options = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option );
  114. $definied = array( $date, $categories, $tags, $author, $comment );
  115. // If all the options are ticked, don't continue.
  116. if ( array( 1, 1, 1, 1, 1 ) === $options ) {
  117. return $void;
  118. }
  119. return array( true, $options, $definied, $post_details );
  120. }