No Description

content-options.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Content Options.
  4. *
  5. * This feature will only be activated for themes that declare their support.
  6. * This can be done by adding code similar to the following during the
  7. * 'after_setup_theme' action:
  8. *
  9. add_theme_support( 'jetpack-content-options', array(
  10. 'blog-display' => 'content', // the default setting of the theme: 'content', 'excerpt' or array( 'content', 'excerpt' ) for themes mixing both display.
  11. 'author-bio' => true, // display or not the author bio: true or false.
  12. 'author-bio-default' => false, // the default setting of the author bio, if it's being displayed or not: true or false (only required if false).
  13. 'masonry' => '.site-main', // a CSS selector matching the elements that triggers a masonry refresh if the theme is using a masonry layout.
  14. 'post-details' => array(
  15. 'stylesheet' => 'themeslug-style', // name of the theme's stylesheet.
  16. 'date' => '.posted-on', // a CSS selector matching the elements that display the post date.
  17. 'categories' => '.cat-links', // a CSS selector matching the elements that display the post categories.
  18. 'tags' => '.tags-links', // a CSS selector matching the elements that display the post tags.
  19. 'author' => '.byline', // a CSS selector matching the elements that display the post author.
  20. 'comment' => '.comments-link', // a CSS selector matching the elements that display the comment link.
  21. ),
  22. 'featured-images' => array(
  23. 'archive' => true, // enable or not the featured image check for archive pages: true or false.
  24. 'archive-default' => false, // the default setting of the featured image on archive pages, if it's being displayed or not: true or false (only required if false).
  25. 'post' => true, // enable or not the featured image check for single posts: true or false.
  26. 'post-default' => false, // the default setting of the featured image on single posts, if it's being displayed or not: true or false (only required if false).
  27. 'page' => true, // enable or not the featured image check for single pages: true or false.
  28. 'page-default' => false, // the default setting of the featured image on single pages, if it's being displayed or not: true or false (only required if false).
  29. 'portfolio' => true, // enable or not the featured image check for single projects: true or false.
  30. 'portfolio-default' => false, // the default setting of the featured image on single projects, if it's being displayed or not: true or false (only required if false).
  31. 'fallback' => true, // enable or not the featured image fallback: true or false.
  32. 'fallback-default' => true, // the default setting for featured image fallbacks: true or false (only required if false)
  33. ),
  34. ) );
  35. */
  36. /**
  37. * Activate the Content Options plugin.
  38. *
  39. * @uses current_theme_supports()
  40. */
  41. function jetpack_content_options_init() {
  42. // If the theme doesn't support 'jetpack-content-options', don't continue.
  43. if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
  44. return;
  45. }
  46. // Load the Customizer options.
  47. require dirname( __FILE__ ) . '/content-options/customizer.php';
  48. // Load Blog Display function.
  49. require dirname( __FILE__ ) . '/content-options/blog-display.php';
  50. // Load Author Bio function.
  51. require dirname( __FILE__ ) . '/content-options/author-bio.php';
  52. // Load Post Details function.
  53. require dirname( __FILE__ ) . '/content-options/post-details.php';
  54. // Load Featured Images function.
  55. if ( jetpack_featured_images_should_load() ) {
  56. require dirname( __FILE__ ) . '/content-options/featured-images.php';
  57. }
  58. // Load Featured Images Fallback function.
  59. if ( jetpack_featured_images_fallback_should_load() ) {
  60. require dirname( __FILE__ ) . '/content-options/featured-images-fallback.php';
  61. }
  62. }
  63. add_action( 'init', 'jetpack_content_options_init' );
  64. function jetpack_featured_images_get_settings() {
  65. $options = get_theme_support( 'jetpack-content-options' );
  66. $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
  67. $settings = array(
  68. 'archive' => ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null,
  69. 'post' => ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null,
  70. 'page' => ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null,
  71. 'portfolio' => ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null,
  72. 'archive-default' => ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1,
  73. 'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1,
  74. 'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1,
  75. 'portfolio-default' => ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1,
  76. 'fallback' => ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null,
  77. 'fallback-default' => ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1,
  78. );
  79. $settings = array_merge(
  80. $settings,
  81. array(
  82. 'archive-option' => get_option( 'jetpack_content_featured_images_archive', $settings['archive-default'] ),
  83. 'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ),
  84. 'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ),
  85. 'portfolio-option' => get_option( 'jetpack_content_featured_images_portfolio', $settings['portfolio-default'] ),
  86. 'fallback-option' => get_option( 'jetpack_content_featured_images_fallback', $settings['fallback-default'] ),
  87. )
  88. );
  89. return $settings;
  90. }
  91. function jetpack_featured_images_should_load() {
  92. // If the theme doesn't support post thumbnails, don't continue.
  93. if ( ! current_theme_supports( 'post-thumbnails' ) ) {
  94. return false;
  95. }
  96. $opts = jetpack_featured_images_get_settings();
  97. // If the theme doesn't support archive, post and page or if all the options are ticked and we aren't in the customizer, don't continue.
  98. if (
  99. ( true !== $opts['archive'] && true !== $opts['post'] && true !== $opts['page'] )
  100. || ( 1 === $opts['archive-option'] && 1 === $opts['post-option'] && 1 === $opts['page-option'] && ! is_customize_preview() )
  101. ) {
  102. return false;
  103. }
  104. return true;
  105. }
  106. function jetpack_featured_images_fallback_should_load() {
  107. // If the theme doesn't support post thumbnails, don't continue.
  108. if ( ! current_theme_supports( 'post-thumbnails' ) ) {
  109. return false;
  110. }
  111. $opts = jetpack_featured_images_get_settings();
  112. // If the theme doesn't support fallback, don't continue.
  113. if ( true !== $opts['fallback'] ) {
  114. return false;
  115. }
  116. return true;
  117. }