From b06eff5f2c41d727a3ded736b9e6383604cc27ef Mon Sep 17 00:00:00 2001 From: Karen Attfield Date: Thu, 22 Aug 2024 13:48:18 +0100 Subject: [PATCH 1/7] Copy content options code to CTH package, and add function_exists checks --- .../src/content-options.php | 170 ++++ .../src/content-options/author-bio.php | 107 +++ .../src/content-options/blog-display.php | 302 +++++++ .../src/content-options/customizer.js | 218 +++++ .../src/content-options/customizer.php | 518 +++++++++++ .../featured-images-fallback.php | 242 +++++ .../src/content-options/featured-images.php | 129 +++ .../src/content-options/post-details.php | 175 ++++ .../modules/theme-tools/content-options.php | 198 ++-- .../content-options/author-bio.php | 166 ++-- .../content-options/blog-display.php | 354 ++++---- .../content-options/customizer.php | 844 +++++++++--------- .../featured-images-fallback.php | 368 ++++---- .../content-options/featured-images.php | 196 ++-- .../content-options/post-details.php | 276 +++--- 15 files changed, 3118 insertions(+), 1145 deletions(-) create mode 100644 projects/packages/classic-theme-helper/src/content-options.php create mode 100644 projects/packages/classic-theme-helper/src/content-options/author-bio.php create mode 100644 projects/packages/classic-theme-helper/src/content-options/blog-display.php create mode 100644 projects/packages/classic-theme-helper/src/content-options/customizer.js create mode 100644 projects/packages/classic-theme-helper/src/content-options/customizer.php create mode 100644 projects/packages/classic-theme-helper/src/content-options/featured-images-fallback.php create mode 100644 projects/packages/classic-theme-helper/src/content-options/featured-images.php create mode 100644 projects/packages/classic-theme-helper/src/content-options/post-details.php diff --git a/projects/packages/classic-theme-helper/src/content-options.php b/projects/packages/classic-theme-helper/src/content-options.php new file mode 100644 index 0000000000000..8a14c3dc78912 --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options.php @@ -0,0 +1,170 @@ + 'content', // the default setting of the theme: 'content', 'excerpt' or array( 'content', 'excerpt' ) for themes mixing both display. + 'author-bio' => true, // display or not the author bio: true or false. + '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). + 'avatar-default' => true, // display or not the default avatar for the author bio: true or false. + 'masonry' => '.site-main', // a CSS selector matching the elements that triggers a masonry refresh if the theme is using a masonry layout. + 'post-details' => array( + 'stylesheet' => 'themeslug-style', // name of the theme's stylesheet. + 'date' => '.posted-on', // a CSS selector matching the elements that display the post date. + 'categories' => '.cat-links', // a CSS selector matching the elements that display the post categories. + 'tags' => '.tags-links', // a CSS selector matching the elements that display the post tags. + 'author' => '.byline', // a CSS selector matching the elements that display the post author. + 'comment' => '.comments-link', // a CSS selector matching the elements that display the comment link. + ), + 'featured-images' => array( + 'archive' => true, // enable or not the featured image check for archive pages: true or false. + '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). + 'post' => true, // enable or not the featured image check for single posts: true or false. + '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). + 'page' => true, // enable or not the featured image check for single pages: true or false. + '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). + 'portfolio' => true, // enable or not the featured image check for single projects: true or false. + '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). + 'fallback' => true, // enable or not the featured image fallback: true or false. + 'fallback-default' => true, // the default setting for featured image fallbacks: true or false (only required if false) + ), + ) ); + */ + +if ( ! function_exists( 'jetpack_content_options_init' ) ) { + + /** + * Activate the Content Options plugin. + * + * @uses current_theme_supports() + */ + function jetpack_content_options_init() { + // If the theme doesn't support 'jetpack-content-options', don't continue. + if ( ! current_theme_supports( 'jetpack-content-options' ) ) { + return; + } + + // Load the Customizer options. + require __DIR__ . '/content-options/customizer.php'; + + // Load Blog Display function. + require __DIR__ . '/content-options/blog-display.php'; + + // Load Author Bio function. + require __DIR__ . '/content-options/author-bio.php'; + + // Load Post Details function. + require __DIR__ . '/content-options/post-details.php'; + + // Load Featured Images function. + if ( jetpack_featured_images_should_load() ) { + require __DIR__ . '/content-options/featured-images.php'; + } + + // Load Featured Images Fallback function. + if ( jetpack_featured_images_fallback_should_load() ) { + require __DIR__ . '/content-options/featured-images-fallback.php'; + } + } + add_action( 'init', 'jetpack_content_options_init' ); + +} + +if ( ! function_exists( 'jetpack_featured_images_get_settings' ) ) { + + /** + * Get featured images settings using the jetpack-content-options theme support. + */ + function jetpack_featured_images_get_settings() { + $options = get_theme_support( 'jetpack-content-options' ); + + $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null; + + $settings = array( + 'archive' => ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null, + 'post' => ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null, + 'page' => ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null, + 'portfolio' => ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null, + 'archive-default' => ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1, + 'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1, + 'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1, + 'portfolio-default' => ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1, + 'fallback' => ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null, + 'fallback-default' => ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1, + ); + + $settings = array_merge( + $settings, + array( + 'archive-option' => get_option( 'jetpack_content_featured_images_archive', $settings['archive-default'] ), + 'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ), + 'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ), + 'portfolio-option' => get_option( 'jetpack_content_featured_images_portfolio', $settings['portfolio-default'] ), + 'fallback-option' => get_option( 'jetpack_content_featured_images_fallback', $settings['fallback-default'] ), + ) + ); + + return $settings; + } + +} + +if ( ! function_exists( 'jetpack_featured_images_should_load' ) ) { + + /** + * Determine if the Jetpack Featured Images should be load. + */ + function jetpack_featured_images_should_load() { + // If the theme doesn't support post thumbnails, don't continue. + if ( ! current_theme_supports( 'post-thumbnails' ) ) { + return false; + } + + $opts = jetpack_featured_images_get_settings(); + + // 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. + if ( + ( true !== $opts['archive'] && true !== $opts['post'] && true !== $opts['page'] ) + || ( 1 === $opts['archive-option'] && 1 === $opts['post-option'] && 1 === $opts['page-option'] && ! is_customize_preview() ) + ) { + return false; + } + + return true; + } + +} + +if ( ! function_exists( 'jetpack_featured_images_fallback_should_load' ) ) { + + /** + * Determine if the Jetpack Featured Images fallback should load. + */ + function jetpack_featured_images_fallback_should_load() { + // If the theme doesn't support post thumbnails, don't continue. + if ( ! current_theme_supports( 'post-thumbnails' ) ) { + return false; + } + + $opts = jetpack_featured_images_get_settings(); + + // If the theme doesn't support fallback, don't continue. + if ( true !== $opts['fallback'] ) { + return false; + } + + return true; + } + +} diff --git a/projects/packages/classic-theme-helper/src/content-options/author-bio.php b/projects/packages/classic-theme-helper/src/content-options/author-bio.php new file mode 100644 index 0000000000000..03b32d4532114 --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/author-bio.php @@ -0,0 +1,107 @@ + +
+ +
+ +
+ + +
+

+ ' . get_the_author() . '' ); + ?> +

+
+ +

+ + +

+
+ '404' ) ); + $headers = get_headers( $url ); + + // If 200 is found, the user has a Gravatar; otherwise, they don't. + return preg_match( '|200|', $headers[0] ) ? true : false; + } + +} diff --git a/projects/packages/classic-theme-helper/src/content-options/blog-display.php b/projects/packages/classic-theme-helper/src/content-options/blog-display.php new file mode 100644 index 0000000000000..ba86b931d9018 --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/blog-display.php @@ -0,0 +1,302 @@ +post_excerpt ) ) { + $text = strip_shortcodes( $post->post_content ); + $text = str_replace( ']]>', ']]>', $text ); + $text = wp_strip_all_tags( $text ); + /** This filter is documented in wp-includes/formatting.php */ + $excerpt_length = apply_filters( 'excerpt_length', 55 ); + /** This filter is documented in wp-includes/formatting.php */ + $excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); + + /* + * translators: If your word count is based on single characters (e.g. East Asian characters), + * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. + * Do not translate into your own language. + */ + if ( strpos( _x( 'words', 'Word count type. Do not translate!', 'jetpack-classic-theme-helper' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { + $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); + preg_match_all( '/./u', $text, $words ); + $words = array_slice( $words[0], 0, $excerpt_length + 1 ); + $sep = ''; + } else { + $words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY ); + $sep = ' '; + } + + if ( count( $words ) > $excerpt_length ) { + array_pop( $words ); + $text = implode( $sep, $words ); + $text = $text . $excerpt_more; + } else { + $text = implode( $sep, $words ); + } + } else { + $text = wp_kses_post( $post->post_excerpt ); + } + return sprintf( '

%s

', $text ); + } + +} + +if ( ! function_exists( 'jetpack_the_content_to_the_excerptt' ) ) { + + /** + * Display Excerpt instead of Content. + * + * @param string $content Post content. + */ + function jetpack_the_content_to_the_excerpt( $content ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + if ( post_password_required() ) { + $excerpt = sprintf( '

%s

', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack-classic-theme-helper' ) ); + } else { + $excerpt = jetpack_blog_display_custom_excerpt(); + } + } + if ( empty( $excerpt ) ) { + return $content; + } else { + return $excerpt; + } + } + +} + +if ( ! function_exists( 'jetpack_the_excerpt_to_the_content' ) ) { + + /** + * Display Content instead of Excerpt. + * + * @param string $content The post excerpt. + */ + function jetpack_the_excerpt_to_the_content( $content ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + ob_start(); + the_content( + sprintf( + wp_kses( + /* translators: %s: Name of current post. Only visible to screen readers */ + __( 'Continue reading "%s"', 'jetpack-classic-theme-helper' ), + array( + 'span' => array( + 'class' => array(), + ), + ) + ), + get_the_title() + ) + ); + $content = ob_get_clean(); + } + return $content; + } + +} + +if ( ! function_exists( 'jetpack_the_content_customizer' ) ) { + + /** + * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them. + * + * @param string $content The post content. + */ + function jetpack_the_content_customizer( $content ) { + $class = jetpack_the_content_customizer_class(); + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + if ( post_password_required() ) { + $excerpt = sprintf( '

%s

', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack-classic-theme-helper' ) ); + } else { + $excerpt = jetpack_blog_display_custom_excerpt(); + } + } + if ( empty( $excerpt ) ) { + return $content; + } else { + return sprintf( '
%s
%s
', $class, $content, $class, $excerpt ); + } + } + +} + +if ( ! function_exists( 'jetpack_the_excerpt_customizer' ) ) { + + /** + * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them. + * + * @param string $excerpt The post excerpt. + */ + function jetpack_the_excerpt_customizer( $excerpt ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + ob_start(); + the_content( + sprintf( + wp_kses( + /* translators: %s: Name of current post. Only visible to screen readers */ + __( 'Continue reading "%s"', 'jetpack-classic-theme-helper' ), + array( + 'span' => array( + 'class' => array(), + ), + ) + ), + get_the_title() + ) + ); + $content = ob_get_clean(); + } + if ( empty( $content ) ) { + return $excerpt; + } else { + return sprintf( '
%s
%s
', $content, $excerpt ); + } + } + +} + +if ( ! function_exists( 'jetpack_the_excerpt_mixed_customizer' ) ) { + + /** + * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display. + * + * @param string $content The post excerpt. + */ + function jetpack_the_excerpt_mixed_customizer( $content ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + jetpack_the_content_customizer_class( 'output-the-excerpt' ); + ob_start(); + the_content(); + $content = ob_get_clean(); + } + return $content; + } + +} + +if ( ! function_exists( 'jetpack_the_content_customizer_class' ) ) { + + /** + * Returns a class value, `output-the-content` by default. + * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default. + * + * @param string|null $new_class CSS class added to content container. + */ + function jetpack_the_content_customizer_class( $new_class = null ) { + static $class; + if ( isset( $new_class ) ) { + // Assign a new class and return. + $class = $new_class; + } elseif ( isset( $class ) ) { + // Reset the class after getting value. + $prev_class = $class; + $class = null; + return $prev_class; + } else { + // Return default class value. + return 'output-the-content'; + } + } + +} + +if ( is_customize_preview() ) { + /* + * Display Content and Excerpt if the default Blog Display is 'Content' + * and we are in the Customizer. + */ + if ( 'content' === $blog_display ) { + add_filter( 'the_content', 'jetpack_the_content_customizer' ); + } + + /* + * Display Content and Excerpt if the default Blog Display is 'Excerpt' + * and we are in the Customizer. + */ + if ( 'excerpt' === $blog_display ) { + add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' ); + } + + /* + * Display Content and Excerpt if the default Blog Display is 'Mixed' + * and we are in the Customizer. + */ + if ( 'mixed' === $blog_display ) { + add_filter( 'the_content', 'jetpack_the_content_customizer' ); + add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' ); + } +} else { + $display_option = get_option( 'jetpack_content_blog_display', $blog_display ); + + /* + * Display Excerpt if the default Blog Display is 'Content' + * or default Blog Display is 'Mixed' + * and the Option picked is 'Post Excerpt' + * and we aren't in the Customizer. + */ + if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) { + add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' ); + } + + /* + * Display Content if the default Blog Display is 'Excerpt' + * or default Blog Display is 'Mixed' + * and the Option picked is 'Full Post' + * and we aren't in the Customizer. + */ + if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) { + add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' ); + } +} diff --git a/projects/packages/classic-theme-helper/src/content-options/customizer.js b/projects/packages/classic-theme-helper/src/content-options/customizer.js new file mode 100644 index 0000000000000..e853604242cdd --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/customizer.js @@ -0,0 +1,218 @@ +/* global blogDisplay, postDetails */ + +/** + * customizer.js + * + * Theme Customizer enhancements for a better user experience. + * + * Contains handlers to make Theme Customizer preview reload changes asynchronously. + * @param $ + */ + +jQuery( function ( $, wp ) { + // Blog Display + wp.customize( 'jetpack_content_blog_display', function ( value ) { + if ( 'content' === blogDisplay.display ) { + $( '.jetpack-blog-display.jetpack-the-excerpt' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-content' ).css( { + clip: 'auto', + position: 'relative', + } ); + } else if ( 'excerpt' === blogDisplay.display ) { + $( '.jetpack-blog-display.jetpack-the-content' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-excerpt' ).css( { + clip: 'auto', + position: 'relative', + } ); + } else if ( 'mixed' === blogDisplay.display ) { + $( '.jetpack-blog-display.jetpack-the-content.output-the-content' ).css( { + clip: 'auto', + position: 'relative', + } ); + $( '.jetpack-blog-display.jetpack-the-excerpt.output-the-content' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-content.output-the-excerpt' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-excerpt.output-the-excerpt' ).css( { + clip: 'auto', + position: 'relative', + } ); + } + value.bind( function ( to ) { + if ( 'content' === to ) { + $( '.jetpack-blog-display.jetpack-the-excerpt' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-content' ).css( { + clip: 'auto', + position: 'relative', + } ); + } else if ( 'excerpt' === to ) { + $( '.jetpack-blog-display.jetpack-the-content' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-excerpt' ).css( { + clip: 'auto', + position: 'relative', + } ); + } else if ( 'mixed' === to ) { + $( '.jetpack-blog-display.jetpack-the-content.output-the-content' ).css( { + clip: 'auto', + position: 'relative', + } ); + $( '.jetpack-blog-display.jetpack-the-excerpt.output-the-content' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-content.output-the-excerpt' ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + position: 'absolute', + } ); + $( '.jetpack-blog-display.jetpack-the-excerpt.output-the-excerpt' ).css( { + clip: 'auto', + position: 'relative', + } ); + } + if ( blogDisplay.masonry ) { + $( blogDisplay.masonry ).masonry(); + } + } ); + } ); + + // Post Details: Date. + wp.customize( 'jetpack_content_post_details_date', function ( value ) { + value.bind( function ( to ) { + if ( false === to ) { + $( postDetails.date ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + height: '1px', + overflow: 'hidden', + position: 'absolute', + width: '1px', + } ); + $( 'body' ).addClass( 'date-hidden' ); + } else { + $( postDetails.date ).css( { + clip: 'auto', + height: 'auto', + overflow: 'auto', + position: 'relative', + width: 'auto', + } ); + $( 'body' ).removeClass( 'date-hidden' ); + } + } ); + } ); + + // Post Details: Categories. + wp.customize( 'jetpack_content_post_details_categories', function ( value ) { + value.bind( function ( to ) { + if ( false === to ) { + $( postDetails.categories ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + height: '1px', + overflow: 'hidden', + position: 'absolute', + width: '1px', + } ); + $( 'body' ).addClass( 'categories-hidden' ); + } else { + $( postDetails.categories ).css( { + clip: 'auto', + height: 'auto', + overflow: 'auto', + position: 'relative', + width: 'auto', + } ); + $( 'body' ).removeClass( 'categories-hidden' ); + } + } ); + } ); + + // Post Details: Tags. + wp.customize( 'jetpack_content_post_details_tags', function ( value ) { + value.bind( function ( to ) { + if ( false === to ) { + $( postDetails.tags ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + height: '1px', + overflow: 'hidden', + position: 'absolute', + width: '1px', + } ); + $( 'body' ).addClass( 'tags-hidden' ); + } else { + $( postDetails.tags ).css( { + clip: 'auto', + height: 'auto', + overflow: 'auto', + position: 'relative', + width: 'auto', + } ); + $( 'body' ).removeClass( 'tags-hidden' ); + } + } ); + } ); + + // Post Details: Author. + wp.customize( 'jetpack_content_post_details_author', function ( value ) { + value.bind( function ( to ) { + if ( false === to ) { + $( postDetails.author ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + height: '1px', + overflow: 'hidden', + position: 'absolute', + width: '1px', + } ); + $( 'body' ).addClass( 'author-hidden' ); + } else { + $( postDetails.author ).css( { + clip: 'auto', + height: 'auto', + overflow: 'auto', + position: 'relative', + width: 'auto', + } ); + $( 'body' ).removeClass( 'author-hidden' ); + } + } ); + } ); + + // Post Details: Comment link. + wp.customize( 'jetpack_content_post_details_comment', function ( value ) { + value.bind( function ( to ) { + if ( false === to ) { + $( postDetails.comment ).css( { + clip: 'rect(1px, 1px, 1px, 1px)', + height: '1px', + overflow: 'hidden', + position: 'absolute', + width: '1px', + } ); + $( 'body' ).addClass( 'comment-hidden' ); + } else { + $( postDetails.comment ).css( { + clip: 'auto', + height: 'auto', + overflow: 'auto', + position: 'relative', + width: 'auto', + } ); + $( 'body' ).removeClass( 'comment-hidden' ); + } + } ); + } ); +} )( jQuery ); diff --git a/projects/packages/classic-theme-helper/src/content-options/customizer.php b/projects/packages/classic-theme-helper/src/content-options/customizer.php new file mode 100644 index 0000000000000..ddc95e0d9ebc0 --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/customizer.php @@ -0,0 +1,518 @@ + + label ); ?> + add_section( + 'jetpack_content_options', + array( + 'title' => esc_html__( 'Content Options', 'jetpack-classic-theme-helper' ), + 'theme_supports' => 'jetpack-content-options', + 'priority' => 100, + ) + ); + + // Add Blog Display option. + if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) { + if ( 'mixed' === $blog_display ) { + $blog_display_choices = array( + 'content' => esc_html__( 'Full post', 'jetpack-classic-theme-helper' ), + 'excerpt' => esc_html__( 'Post excerpt', 'jetpack-classic-theme-helper' ), + 'mixed' => esc_html__( 'Default', 'jetpack-classic-theme-helper' ), + ); + + $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages, or opt for the theme\'s default combination of excerpt and full post.', 'jetpack-classic-theme-helper' ); + } else { + $blog_display_choices = array( + 'content' => esc_html__( 'Full post', 'jetpack-classic-theme-helper' ), + 'excerpt' => esc_html__( 'Post excerpt', 'jetpack-classic-theme-helper' ), + ); + + $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack-classic-theme-helper' ); + + if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) { + update_option( 'jetpack_content_blog_display', $blog_display ); + } + } + + $wp_customize->add_setting( + 'jetpack_content_blog_display', + array( + 'default' => $blog_display, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_blog_display', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Blog Display', 'jetpack-classic-theme-helper' ), + 'description' => $blog_display_description, + 'type' => 'radio', + 'choices' => $blog_display_choices, + ) + ); + } + + // Add Author Bio option. + if ( true === $author_bio ) { + $wp_customize->add_setting( 'jetpack_content_author_bio_title' ); + + $wp_customize->add_control( + new Jetpack_Customize_Control_Title( + $wp_customize, + 'jetpack_content_author_bio_title', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Author Bio', 'jetpack-classic-theme-helper' ), + 'type' => 'title', + ) + ) + ); + + $wp_customize->add_setting( + 'jetpack_content_author_bio', + array( + 'default' => $author_bio_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_author_bio', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on single posts', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + ) + ); + } + + // Add Post Details options. + if ( ( ! empty( $post_details ) ) + && ( ! empty( $post_details['stylesheet'] ) ) + && ( ! empty( $date ) + || ! empty( $categories ) + || ! empty( $tags ) + || ! empty( $author ) + || ! empty( $comment ) ) ) { + $wp_customize->add_setting( 'jetpack_content_post_details_title' ); + + $wp_customize->add_control( + new Jetpack_Customize_Control_Title( + $wp_customize, + 'jetpack_content_post_details_title', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Post Details', 'jetpack-classic-theme-helper' ), + 'type' => 'title', + ) + ) + ); + + // Post Details: Date. + if ( ! empty( $date ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_date', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_date', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display date', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + ) + ); + } + + // Post Details: Categories. + if ( ! empty( $categories ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_categories', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_categories', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display categories', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + ) + ); + } + + // Post Details: Tags. + if ( ! empty( $tags ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_tags', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_tags', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display tags', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + ) + ); + } + + // Post Details: Author. + if ( ! empty( $author ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_author', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_author', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display author', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + ) + ); + } + + // Post Details: Comment link. + if ( ! empty( $comment ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_comment', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_comment', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display comment link', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + ) + ); + } + } + + // Add Featured Images options. + if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) { + $wp_customize->add_setting( 'jetpack_content_featured_images_title' ); + + $wp_customize->add_control( + new Jetpack_Customize_Control_Title( + $wp_customize, + 'jetpack_content_featured_images_title', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Featured Images', 'jetpack-classic-theme-helper' ) . sprintf( '%1$s', esc_html__( 'Learn more about Featured Images', 'jetpack-classic-theme-helper' ) ), + 'type' => 'title', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ) + ); + + // Featured Images: Archive. + if ( true === $fi_archive ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_archive', + array( + 'default' => $fi_archive_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_archive', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on blog and archives', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } + + // Featured Images: Post. + if ( true === $fi_post ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_post', + array( + 'default' => $fi_post_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_post', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on single posts', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } + + // Featured Images: Page. + if ( true === $fi_page ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_page', + array( + 'default' => $fi_page_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_page', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on pages', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } + + // Featured Images: Portfolio. + if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_portfolio', + array( + 'default' => $fi_portfolio_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_portfolio', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on single projects', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } + + // Featured Images: Fallback. + if ( true === $fi_fallback ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_fallback', + array( + 'default' => $fi_fallback_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_fallback', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Automatically use first image in post', 'jetpack-classic-theme-helper' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } + } + } + add_action( 'customize_register', 'jetpack_content_options_customize_register' ); + +} + +if ( ! function_exists( 'jetpack_post_thumbnail_supports' ) ) { + + /** + * Return whether the theme supports Post Thumbnails. + */ + function jetpack_post_thumbnail_supports() { + return ( current_theme_supports( 'post-thumbnails' ) ); + } + +} + +if ( ! function_exists( 'jetpack_content_options_sanitize_checkbox' ) ) { + + /** + * Sanitize the checkbox. + * + * @param int $input The unsanitized value from the setting. + * @return boolean|string + */ + function jetpack_content_options_sanitize_checkbox( $input ) { + return ( 1 === (int) $input ) ? 1 : ''; + } + +} + +if ( ! function_exists( 'jetpack_content_options_sanitize_blog_display' ) ) { + + /** + * Sanitize the Display value. + * + * @param string $display The unsanitized value from the setting. + * @return string + */ + function jetpack_content_options_sanitize_blog_display( $display ) { + if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ), true ) ) { + $display = 'content'; + } + return $display; + } + +} + +if ( ! function_exists( 'jetpack_content_options_customize_preview_js' ) ) { + + /** + * Binds JS handlers to make Theme Customizer preview reload changes asynchronously. + */ + function jetpack_content_options_customize_preview_js() { + $options = get_theme_support( 'jetpack-content-options' ); + $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null; + $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display ); + sort( $blog_display ); + $blog_display = implode( ', ', $blog_display ); + $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display; + $masonry = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null; + $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; + $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; + $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; + $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; + $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; + $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; + + wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'jquery', 'customize-preview' ), '1.0', true ); + + wp_localize_script( + 'jetpack-content-options-customizer', + 'blogDisplay', + array( + 'display' => get_option( 'jetpack_content_blog_display', $blog_display ), + 'masonry' => $masonry, + ) + ); + + wp_localize_script( + 'jetpack-content-options-customizer', + 'postDetails', + array( + 'date' => $date, + 'categories' => $categories, + 'tags' => $tags, + 'author' => $author, + 'comment' => $comment, + ) + ); + } + add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' ); + +} diff --git a/projects/packages/classic-theme-helper/src/content-options/featured-images-fallback.php b/projects/packages/classic-theme-helper/src/content-options/featured-images-fallback.php new file mode 100644 index 0000000000000..77b5d1b5d798e --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/featured-images-fallback.php @@ -0,0 +1,242 @@ + false, + 'from_slideshow' => true, + 'from_gallery' => true, + 'from_attachment' => false, + ); + + $image = Jetpack_PostImages::get_image( $post_id, $args ); + + if ( ! empty( $image ) ) { + $image['width'] = ''; + $image['height'] = ''; + $image['crop'] = ''; + + if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { + $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; + $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; + $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; + } + + // Force `crop` to be a simple boolean, to avoid dealing with WP crop positions. + $image['crop'] = boolval( $image['crop'] ); + + $image_sizes = ''; + + $width = isset( $image['width'] ) ? intval( $image['width'] ) : null; + $height = isset( $image['height'] ) ? intval( $image['height'] ) : null; + + if ( ! empty( $image['src_width'] ) && ! empty( $image['src_height'] ) && ! empty( $image['width'] ) && ! empty( $image['height'] ) ) { + $src_width = intval( $image['src_width'] ); + $src_height = intval( $image['src_height'] ); + + // If we're aware of the source dimensions, calculate the final image height and width. + // This allows us to provide them as attributes in the `` tag, to avoid layout shifts. + // It also allows us to calculate a `srcset`. + if ( $image['crop'] ) { + // If we're cropping, the final dimensions are calculated independently of each other. + $width = min( $width, $src_width ); + $height = min( $height, $src_height ); + } else { + // If we're not cropping, we need to preserve aspect ratio. + $dims = wp_constrain_dimensions( $src_width, $src_height, $width, $height ); + $width = $dims[0]; + $height = $dims[1]; + } + + $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $width, $height ); + $image_srcset = Jetpack_PostImages::generate_cropped_srcset( $image, $width, $height, true ); + $image_sizes = 'min(' . $width . 'px, 100vw)'; + } else { + // If we're not aware of the source dimensions, leave the size calculations to the CDN, and + // fall back to a simpler `` tag without `width`/`height` or `srcset`. + $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); + + // Use the theme's crop setting rather than forcing to true. + $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); + + $image_srcset = null; + $image_sizes = null; + } + + $default_attr = array( + 'srcset' => $image_srcset, + 'sizes' => $image_sizes, + 'loading' => is_singular() ? 'eager' : 'lazy', + 'decoding' => 'async', + 'title' => wp_strip_all_tags( get_the_title() ), + 'alt' => '', + 'class' => "attachment-$size wp-post-image", + ); + + $image_attr = wp_parse_args( $attr, $default_attr ); + $hwstring = image_hwstring( $width, $height ); + + $html = rtrim( " $value ) { + if ( $value ) { + $html .= " $name=" . '"' . esc_attr( $value ) . '"'; + } + } + + $html .= ' />'; + + return trim( $html ); + } + } + + return trim( $html ); + } + add_filter( 'post_thumbnail_html', 'jetpack_featured_images_fallback_get_image', 10, 5 ); + +} + +if ( ! function_exists( 'jetpack_featured_images_fallback_get_image_src' ) ) { + + /** + * Get URL of one image from a specified post in the following order: + * Featured Image then first image from the_content HTML + * + * @param int $post_id The post ID to check. + * @param int $post_thumbnail_id The ID of the featured image. + * @param string $size The image size to return, defaults to 'post-thumbnail'. + * + * @return string|null $image_src The URL of the thumbnail image. + */ + function jetpack_featured_images_fallback_get_image_src( $post_id, $post_thumbnail_id, $size ) { + $image_src = wp_get_attachment_image_src( $post_thumbnail_id, $size ); + $image_src = ( ! empty( $image_src[0] ) ) ? $image_src[0] : null; + $opts = jetpack_featured_images_get_settings(); + + if ( ! empty( $image_src ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { + return esc_url( $image_src ); + } + + if ( jetpack_featured_images_should_load() ) { + if ( ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) + || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) ) { + return esc_url( $image_src ); + } + } + + if ( class_exists( 'Jetpack_PostImages' ) ) { + global $_wp_additional_image_sizes; + + $args = array( + 'from_thumbnail' => false, + 'from_slideshow' => true, + 'from_gallery' => true, + 'from_attachment' => false, + ); + + $image = Jetpack_PostImages::get_image( $post_id, $args ); + + if ( ! empty( $image ) ) { + $image['width'] = ''; + $image['height'] = ''; + $image['crop'] = ''; + + if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { + $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; + $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; + $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; + } + + $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); + + // Use the theme's crop setting rather than forcing to true. + $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); + + return esc_url( $image_src ); + } + } + + return esc_url( $image_src ); + } + +} + +if ( ! function_exists( 'jetpack_has_featured_image' ) ) { + + /** + * Check if post has an image attached, including a fallback. + * + * @param int $post The post ID to check. + * + * @return bool + */ + function jetpack_has_featured_image( $post = null ) { + return (bool) get_the_post_thumbnail( $post ); + } + +} + +if ( ! function_exists( 'jetpack_featured_images_post_class' ) ) { + + /** + * Adds custom class to the array of post classes. + * + * @param array $classes Classes for the post element. + * @param array $class Optional. Comma separated list of additional classes. + * @param array $post_id Unique The post ID to check. + * + * @return array $classes + */ + function jetpack_featured_images_post_class( $classes, $class, $post_id ) { + $post_password_required = post_password_required( $post_id ); + $opts = jetpack_featured_images_get_settings(); + + if ( jetpack_has_featured_image( $post_id ) && (bool) 1 === (bool) $opts['fallback-option'] && ! is_attachment() && ! $post_password_required && 'post' === get_post_type() ) { + $classes[] = 'has-post-thumbnail'; + $classes[] = 'fallback-thumbnail'; + } + + return $classes; + } + add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 ); + +} diff --git a/projects/packages/classic-theme-helper/src/content-options/featured-images.php b/projects/packages/classic-theme-helper/src/content-options/featured-images.php new file mode 100644 index 0000000000000..87b4d5df07894 --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/featured-images.php @@ -0,0 +1,129 @@ +get( 'page_id' ); + $is_static_front_page = 'page' === get_option( 'show_on_front' ); + + if ( $is_static_front_page && $front_page_id === $current_page_id ) { + $is_shop_page = ( wc_get_page_id( 'shop' ) === $current_page_id ) ? true : false; + } else { + $is_shop_page = is_shop(); + } + + return $is_shop_page; + } + +} diff --git a/projects/packages/classic-theme-helper/src/content-options/post-details.php b/projects/packages/classic-theme-helper/src/content-options/post-details.php new file mode 100644 index 0000000000000..78743a4bf9048 --- /dev/null +++ b/projects/packages/classic-theme-helper/src/content-options/post-details.php @@ -0,0 +1,175 @@ + ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null, + 'post' => ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null, + 'page' => ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null, + 'portfolio' => ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null, + 'archive-default' => ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1, + 'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1, + 'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1, + 'portfolio-default' => ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1, + 'fallback' => ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null, + 'fallback-default' => ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1, + ); + + $settings = array_merge( + $settings, + array( + 'archive-option' => get_option( 'jetpack_content_featured_images_archive', $settings['archive-default'] ), + 'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ), + 'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ), + 'portfolio-option' => get_option( 'jetpack_content_featured_images_portfolio', $settings['portfolio-default'] ), + 'fallback-option' => get_option( 'jetpack_content_featured_images_fallback', $settings['fallback-default'] ), + ) + ); + + return $settings; } - // Load Featured Images Fallback function. - if ( jetpack_featured_images_fallback_should_load() ) { - require __DIR__ . '/content-options/featured-images-fallback.php'; - } } -add_action( 'init', 'jetpack_content_options_init' ); -/** - * Get featured images settings using the jetpack-content-options theme support. - */ -function jetpack_featured_images_get_settings() { - $options = get_theme_support( 'jetpack-content-options' ); - - $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null; - - $settings = array( - 'archive' => ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null, - 'post' => ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null, - 'page' => ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null, - 'portfolio' => ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null, - 'archive-default' => ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1, - 'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1, - 'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1, - 'portfolio-default' => ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1, - 'fallback' => ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null, - 'fallback-default' => ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1, - ); - - $settings = array_merge( - $settings, - array( - 'archive-option' => get_option( 'jetpack_content_featured_images_archive', $settings['archive-default'] ), - 'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ), - 'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ), - 'portfolio-option' => get_option( 'jetpack_content_featured_images_portfolio', $settings['portfolio-default'] ), - 'fallback-option' => get_option( 'jetpack_content_featured_images_fallback', $settings['fallback-default'] ), - ) - ); - - return $settings; -} +if ( ! function_exists( 'jetpack_featured_images_should_load' ) ) { -/** - * Determine if the Jetpack Featured Images should be load. - */ -function jetpack_featured_images_should_load() { - // If the theme doesn't support post thumbnails, don't continue. - if ( ! current_theme_supports( 'post-thumbnails' ) ) { - return false; - } + /** + * Determine if the Jetpack Featured Images should be load. + */ + function jetpack_featured_images_should_load() { + // If the theme doesn't support post thumbnails, don't continue. + if ( ! current_theme_supports( 'post-thumbnails' ) ) { + return false; + } + + $opts = jetpack_featured_images_get_settings(); - $opts = jetpack_featured_images_get_settings(); + // 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. + if ( + ( true !== $opts['archive'] && true !== $opts['post'] && true !== $opts['page'] ) + || ( 1 === $opts['archive-option'] && 1 === $opts['post-option'] && 1 === $opts['page-option'] && ! is_customize_preview() ) + ) { + return false; + } - // 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. - if ( - ( true !== $opts['archive'] && true !== $opts['post'] && true !== $opts['page'] ) - || ( 1 === $opts['archive-option'] && 1 === $opts['post-option'] && 1 === $opts['page-option'] && ! is_customize_preview() ) - ) { - return false; + return true; } - return true; } -/** - * Determine if the Jetpack Featured Images fallback should load. - */ -function jetpack_featured_images_fallback_should_load() { - // If the theme doesn't support post thumbnails, don't continue. - if ( ! current_theme_supports( 'post-thumbnails' ) ) { - return false; - } +if ( ! function_exists( 'jetpack_featured_images_fallback_should_load' ) ) { + + /** + * Determine if the Jetpack Featured Images fallback should load. + */ + function jetpack_featured_images_fallback_should_load() { + // If the theme doesn't support post thumbnails, don't continue. + if ( ! current_theme_supports( 'post-thumbnails' ) ) { + return false; + } + + $opts = jetpack_featured_images_get_settings(); - $opts = jetpack_featured_images_get_settings(); + // If the theme doesn't support fallback, don't continue. + if ( true !== $opts['fallback'] ) { + return false; + } - // If the theme doesn't support fallback, don't continue. - if ( true !== $opts['fallback'] ) { - return false; + return true; } - return true; } diff --git a/projects/plugins/jetpack/modules/theme-tools/content-options/author-bio.php b/projects/plugins/jetpack/modules/theme-tools/content-options/author-bio.php index a15843780656e..2aab1a70181aa 100644 --- a/projects/plugins/jetpack/modules/theme-tools/content-options/author-bio.php +++ b/projects/plugins/jetpack/modules/theme-tools/content-options/author-bio.php @@ -5,95 +5,103 @@ * @package automattic/jetpack */ -/** - * The function to display Author Bio in a theme. - */ -function jetpack_author_bio() { - // If the theme doesn't support 'jetpack-content-options', don't continue. - if ( ! current_theme_supports( 'jetpack-content-options' ) ) { - return; - } +if ( ! function_exists( 'jetpack_author_bio' ) ) { - $options = get_theme_support( 'jetpack-content-options' ); - $author_bio = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null; - $author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1; - $avatar_default = ( isset( $options[0]['avatar-default'] ) && false === $options[0]['avatar-default'] ) ? '' : 1; + /** + * The function to display Author Bio in a theme. + */ + function jetpack_author_bio() { + // If the theme doesn't support 'jetpack-content-options', don't continue. + if ( ! current_theme_supports( 'jetpack-content-options' ) ) { + return; + } - // If the theme doesn't support 'jetpack-content-options[ 'author-bio' ]', don't continue. - if ( true !== $author_bio ) { - return; - } + $options = get_theme_support( 'jetpack-content-options' ); + $author_bio = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null; + $author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1; + $avatar_default = ( isset( $options[0]['avatar-default'] ) && false === $options[0]['avatar-default'] ) ? '' : 1; - // If 'jetpack_content_author_bio' is false, don't continue. - if ( ! get_option( 'jetpack_content_author_bio', $author_bio_default ) ) { - return; - } + // If the theme doesn't support 'jetpack-content-options[ 'author-bio' ]', don't continue. + if ( true !== $author_bio ) { + return; + } - // If we aren't on a single post, don't continue. - if ( ! is_single() ) { - return; - } + // If 'jetpack_content_author_bio' is false, don't continue. + if ( ! get_option( 'jetpack_content_author_bio', $author_bio_default ) ) { + return; + } - // Define class for entry-author. - if ( ! $avatar_default && ! jetpack_has_gravatar( get_the_author_meta( 'user_email' ) ) ) { - $class = 'author-avatar-hide'; - } else { - $class = 'author-avatar-show'; - } - ?> -
- -
- -
- - -
-

- ' . get_the_author() . '' ); - ?> -

-
+ // If we aren't on a single post, don't continue. + if ( ! is_single() ) { + return; + } -

- -

+ '404' ) ); - $headers = get_headers( $url ); + $url = get_avatar_url( $email, array( 'default' => '404' ) ); + $headers = get_headers( $url ); + + // If 200 is found, the user has a Gravatar; otherwise, they don't. + return preg_match( '|200|', $headers[0] ) ? true : false; + } - // If 200 is found, the user has a Gravatar; otherwise, they don't. - return preg_match( '|200|', $headers[0] ) ? true : false; } diff --git a/projects/plugins/jetpack/modules/theme-tools/content-options/blog-display.php b/projects/plugins/jetpack/modules/theme-tools/content-options/blog-display.php index 6b9ff3fed01ec..4a33aff67dfde 100644 --- a/projects/plugins/jetpack/modules/theme-tools/content-options/blog-display.php +++ b/projects/plugins/jetpack/modules/theme-tools/content-options/blog-display.php @@ -30,198 +30,226 @@ return; } -/** - * Apply Content filters. - * - * @since 9.7.0 Deprecated $content parameter. - * - * @param string $content Post content. Deprecated. - */ -function jetpack_blog_display_custom_excerpt( $content = '' ) { - if ( ! empty( $content ) ) { - _doing_it_wrong( - 'jetpack_blog_display_custom_excerpt', - esc_html__( 'You do not need to pass a $content parameter anymore.', 'jetpack' ), - 'jetpack-9.7.0' - ); - } +if ( ! function_exists( 'jetpack_blog_display_custom_excerpt' ) ) { - $post = get_post(); - if ( empty( $post ) ) { - return ''; - } + /** + * Apply Content filters. + * + * @since 9.7.0 Deprecated $content parameter. + * + * @param string $content Post content. Deprecated. + */ + function jetpack_blog_display_custom_excerpt( $content = '' ) { + if ( ! empty( $content ) ) { + _doing_it_wrong( + 'jetpack_blog_display_custom_excerpt', + esc_html__( 'You do not need to pass a $content parameter anymore.', 'jetpack' ), + 'jetpack-9.7.0' + ); + } - if ( empty( $post->post_excerpt ) ) { - $text = strip_shortcodes( $post->post_content ); - $text = str_replace( ']]>', ']]>', $text ); - $text = wp_strip_all_tags( $text ); - /** This filter is documented in wp-includes/formatting.php */ - $excerpt_length = apply_filters( 'excerpt_length', 55 ); - /** This filter is documented in wp-includes/formatting.php */ - $excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); - - /* - * translators: If your word count is based on single characters (e.g. East Asian characters), - * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. - * Do not translate into your own language. - */ - if ( strpos( _x( 'words', 'Word count type. Do not translate!', 'jetpack' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { - $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); - preg_match_all( '/./u', $text, $words ); - $words = array_slice( $words[0], 0, $excerpt_length + 1 ); - $sep = ''; - } else { - $words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY ); - $sep = ' '; + $post = get_post(); + if ( empty( $post ) ) { + return ''; } - if ( count( $words ) > $excerpt_length ) { - array_pop( $words ); - $text = implode( $sep, $words ); - $text = $text . $excerpt_more; + if ( empty( $post->post_excerpt ) ) { + $text = strip_shortcodes( $post->post_content ); + $text = str_replace( ']]>', ']]>', $text ); + $text = wp_strip_all_tags( $text ); + /** This filter is documented in wp-includes/formatting.php */ + $excerpt_length = apply_filters( 'excerpt_length', 55 ); + /** This filter is documented in wp-includes/formatting.php */ + $excerpt_more = apply_filters( 'excerpt_more', ' [...]' ); + + /* + * translators: If your word count is based on single characters (e.g. East Asian characters), + * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. + * Do not translate into your own language. + */ + if ( strpos( _x( 'words', 'Word count type. Do not translate!', 'jetpack' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { + $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); + preg_match_all( '/./u', $text, $words ); + $words = array_slice( $words[0], 0, $excerpt_length + 1 ); + $sep = ''; + } else { + $words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY ); + $sep = ' '; + } + + if ( count( $words ) > $excerpt_length ) { + array_pop( $words ); + $text = implode( $sep, $words ); + $text = $text . $excerpt_more; + } else { + $text = implode( $sep, $words ); + } } else { - $text = implode( $sep, $words ); + $text = wp_kses_post( $post->post_excerpt ); } - } else { - $text = wp_kses_post( $post->post_excerpt ); + return sprintf( '

%s

', $text ); } - return sprintf( '

%s

', $text ); + } -/** - * Display Excerpt instead of Content. - * - * @param string $content Post content. - */ -function jetpack_the_content_to_the_excerpt( $content ) { - if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { - if ( post_password_required() ) { - $excerpt = sprintf( '

%s

', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) ); +if ( ! function_exists( 'jetpack_the_content_to_the_excerptt' ) ) { + + /** + * Display Excerpt instead of Content. + * + * @param string $content Post content. + */ + function jetpack_the_content_to_the_excerpt( $content ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + if ( post_password_required() ) { + $excerpt = sprintf( '

%s

', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) ); + } else { + $excerpt = jetpack_blog_display_custom_excerpt(); + } + } + if ( empty( $excerpt ) ) { + return $content; } else { - $excerpt = jetpack_blog_display_custom_excerpt(); + return $excerpt; } } - if ( empty( $excerpt ) ) { - return $content; - } else { - return $excerpt; - } + } -/** - * Display Content instead of Excerpt. - * - * @param string $content The post excerpt. - */ -function jetpack_the_excerpt_to_the_content( $content ) { - if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { - ob_start(); - the_content( - sprintf( - wp_kses( - /* translators: %s: Name of current post. Only visible to screen readers */ - __( 'Continue reading "%s"', 'jetpack' ), - array( - 'span' => array( - 'class' => array(), - ), - ) - ), - get_the_title() - ) - ); - $content = ob_get_clean(); +if ( ! function_exists( 'jetpack_the_excerpt_to_the_content' ) ) { + + /** + * Display Content instead of Excerpt. + * + * @param string $content The post excerpt. + */ + function jetpack_the_excerpt_to_the_content( $content ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + ob_start(); + the_content( + sprintf( + wp_kses( + /* translators: %s: Name of current post. Only visible to screen readers */ + __( 'Continue reading "%s"', 'jetpack' ), + array( + 'span' => array( + 'class' => array(), + ), + ) + ), + get_the_title() + ) + ); + $content = ob_get_clean(); + } + return $content; } - return $content; + } -/** - * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them. - * - * @param string $content The post content. - */ -function jetpack_the_content_customizer( $content ) { - $class = jetpack_the_content_customizer_class(); - if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { - if ( post_password_required() ) { - $excerpt = sprintf( '

%s

', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) ); +if ( ! function_exists( 'jetpack_the_content_customizer' ) ) { + + /** + * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them. + * + * @param string $content The post content. + */ + function jetpack_the_content_customizer( $content ) { + $class = jetpack_the_content_customizer_class(); + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + if ( post_password_required() ) { + $excerpt = sprintf( '

%s

', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) ); + } else { + $excerpt = jetpack_blog_display_custom_excerpt(); + } + } + if ( empty( $excerpt ) ) { + return $content; } else { - $excerpt = jetpack_blog_display_custom_excerpt(); + return sprintf( '
%s
%s
', $class, $content, $class, $excerpt ); } } - if ( empty( $excerpt ) ) { - return $content; - } else { - return sprintf( '
%s
%s
', $class, $content, $class, $excerpt ); - } + } -/** - * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them. - * - * @param string $excerpt The post excerpt. - */ -function jetpack_the_excerpt_customizer( $excerpt ) { - if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { - ob_start(); - the_content( - sprintf( - wp_kses( - /* translators: %s: Name of current post. Only visible to screen readers */ - __( 'Continue reading "%s"', 'jetpack' ), - array( - 'span' => array( - 'class' => array(), - ), - ) - ), - get_the_title() - ) - ); - $content = ob_get_clean(); - } - if ( empty( $content ) ) { - return $excerpt; - } else { - return sprintf( '
%s
%s
', $content, $excerpt ); +if ( ! function_exists( 'jetpack_the_excerpt_customizer' ) ) { + + /** + * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them. + * + * @param string $excerpt The post excerpt. + */ + function jetpack_the_excerpt_customizer( $excerpt ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + ob_start(); + the_content( + sprintf( + wp_kses( + /* translators: %s: Name of current post. Only visible to screen readers */ + __( 'Continue reading "%s"', 'jetpack' ), + array( + 'span' => array( + 'class' => array(), + ), + ) + ), + get_the_title() + ) + ); + $content = ob_get_clean(); + } + if ( empty( $content ) ) { + return $excerpt; + } else { + return sprintf( '
%s
%s
', $content, $excerpt ); + } } + } -/** - * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display. - * - * @param string $content The post excerpt. - */ -function jetpack_the_excerpt_mixed_customizer( $content ) { - if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { - jetpack_the_content_customizer_class( 'output-the-excerpt' ); - ob_start(); - the_content(); - $content = ob_get_clean(); +if ( ! function_exists( 'jetpack_the_excerpt_mixed_customizer' ) ) { + + /** + * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display. + * + * @param string $content The post excerpt. + */ + function jetpack_the_excerpt_mixed_customizer( $content ) { + if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) { + jetpack_the_content_customizer_class( 'output-the-excerpt' ); + ob_start(); + the_content(); + $content = ob_get_clean(); + } + return $content; } - return $content; + } -/** - * Returns a class value, `output-the-content` by default. - * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default. - * - * @param string|null $new_class CSS class added to content container. - */ -function jetpack_the_content_customizer_class( $new_class = null ) { - static $class; - if ( isset( $new_class ) ) { - // Assign a new class and return. - $class = $new_class; - } elseif ( isset( $class ) ) { - // Reset the class after getting value. - $prev_class = $class; - $class = null; - return $prev_class; - } else { - // Return default class value. - return 'output-the-content'; +if ( ! function_exists( 'jetpack_the_content_customizer_class' ) ) { + + /** + * Returns a class value, `output-the-content` by default. + * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default. + * + * @param string|null $new_class CSS class added to content container. + */ + function jetpack_the_content_customizer_class( $new_class = null ) { + static $class; + if ( isset( $new_class ) ) { + // Assign a new class and return. + $class = $new_class; + } elseif ( isset( $class ) ) { + // Reset the class after getting value. + $prev_class = $class; + $class = null; + return $prev_class; + } else { + // Return default class value. + return 'output-the-content'; + } } + } if ( is_customize_preview() ) { diff --git a/projects/plugins/jetpack/modules/theme-tools/content-options/customizer.php b/projects/plugins/jetpack/modules/theme-tools/content-options/customizer.php index c06836f6ff90d..fb2e482454cb5 100644 --- a/projects/plugins/jetpack/modules/theme-tools/content-options/customizer.php +++ b/projects/plugins/jetpack/modules/theme-tools/content-options/customizer.php @@ -5,494 +5,514 @@ * @package automattic/jetpack */ -/** - * Add Content section to the Theme Customizer. - * - * @param WP_Customize_Manager $wp_customize Theme Customizer object. - */ -function jetpack_content_options_customize_register( $wp_customize ) { - $options = get_theme_support( 'jetpack-content-options' ); - $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null; - $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display ); - sort( $blog_display ); - $blog_display = implode( ', ', $blog_display ); - $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display; - $author_bio = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null; - $author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1; - $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; - $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; - $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; - $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; - $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; - $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; - $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null; - $fi_archive = ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null; - $fi_post = ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null; - $fi_page = ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null; - $fi_portfolio = ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null; - $fi_fallback = ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null; - $fi_archive_default = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1; - $fi_post_default = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1; - $fi_page_default = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1; - $fi_portfolio_default = ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1; - $fi_fallback_default = ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1; - - // If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', 'jetpack-content-options[ 'author-bio' ]', 'jetpack-content-options[ 'post-details' ]' and 'jetpack-content-options[ 'featured-images' ]', don't continue. - if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) - && ( true !== $author_bio ) - && ( ( empty( $post_details['stylesheet'] ) ) - && ( empty( $date ) - || empty( $categories ) - || empty( $tags ) - || empty( $author ) - || empty( $comment ) ) ) - && ( true !== $fi_archive && true !== $fi_post && true !== $fi_page && true !== $fi_portfolio && true !== $fi_fallback ) ) { - return; - } +if ( ! function_exists( 'jetpack_content_options_customize_register' ) ) { /** - * New Customizer control type: Title. + * Add Content section to the Theme Customizer. + * + * @param WP_Customize_Manager $wp_customize Theme Customizer object. */ - class Jetpack_Customize_Control_Title extends WP_Customize_Control { - /** - * Customizer control type. - * - * @var string - */ - public $type = 'title'; + function jetpack_content_options_customize_register( $wp_customize ) { + $options = get_theme_support( 'jetpack-content-options' ); + $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null; + $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display ); + sort( $blog_display ); + $blog_display = implode( ', ', $blog_display ); + $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display; + $author_bio = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null; + $author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1; + $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; + $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; + $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; + $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; + $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; + $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; + $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null; + $fi_archive = ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null; + $fi_post = ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null; + $fi_page = ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null; + $fi_portfolio = ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null; + $fi_fallback = ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null; + $fi_archive_default = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1; + $fi_post_default = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1; + $fi_page_default = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1; + $fi_portfolio_default = ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1; + $fi_fallback_default = ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1; + + // If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', 'jetpack-content-options[ 'author-bio' ]', 'jetpack-content-options[ 'post-details' ]' and 'jetpack-content-options[ 'featured-images' ]', don't continue. + if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) + && ( true !== $author_bio ) + && ( ( empty( $post_details['stylesheet'] ) ) + && ( empty( $date ) + || empty( $categories ) + || empty( $tags ) + || empty( $author ) + || empty( $comment ) ) ) + && ( true !== $fi_archive && true !== $fi_post && true !== $fi_page && true !== $fi_portfolio && true !== $fi_fallback ) ) { + return; + } /** - * Render the control's content. + * New Customizer control type: Title. */ - public function render_content() { // phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction - ?> - label ); ?> - add_section( - 'jetpack_content_options', - array( - 'title' => esc_html__( 'Content Options', 'jetpack' ), - 'theme_supports' => 'jetpack-content-options', - 'priority' => 100, - ) - ); - - // Add Blog Display option. - if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) { - if ( 'mixed' === $blog_display ) { - $blog_display_choices = array( - 'content' => esc_html__( 'Full post', 'jetpack' ), - 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ), - 'mixed' => esc_html__( 'Default', 'jetpack' ), - ); - - $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages, or opt for the theme\'s default combination of excerpt and full post.', 'jetpack' ); - } else { - $blog_display_choices = array( - 'content' => esc_html__( 'Full post', 'jetpack' ), - 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ), - ); - - $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' ); - - if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) { - update_option( 'jetpack_content_blog_display', $blog_display ); + class Jetpack_Customize_Control_Title extends WP_Customize_Control { + /** + * Customizer control type. + * + * @var string + */ + public $type = 'title'; + + /** + * Render the control's content. + */ + public function render_content() { // phpcs:ignore MediaWiki.Usage.NestedFunctions.NestedFunction + ?> + label ); ?> + add_setting( - 'jetpack_content_blog_display', - array( - 'default' => $blog_display, - 'type' => 'option', - 'transport' => 'postMessage', - 'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display', - ) - ); - - $wp_customize->add_control( - 'jetpack_content_blog_display', + // Add Content section. + $wp_customize->add_section( + 'jetpack_content_options', array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Blog Display', 'jetpack' ), - 'description' => $blog_display_description, - 'type' => 'radio', - 'choices' => $blog_display_choices, + 'title' => esc_html__( 'Content Options', 'jetpack' ), + 'theme_supports' => 'jetpack-content-options', + 'priority' => 100, ) ); - } - - // Add Author Bio option. - if ( true === $author_bio ) { - $wp_customize->add_setting( 'jetpack_content_author_bio_title' ); - $wp_customize->add_control( - new Jetpack_Customize_Control_Title( - $wp_customize, - 'jetpack_content_author_bio_title', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Author Bio', 'jetpack' ), - 'type' => 'title', - ) - ) - ); - - $wp_customize->add_setting( - 'jetpack_content_author_bio', - array( - 'default' => $author_bio_default, - 'type' => 'option', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); - - $wp_customize->add_control( - 'jetpack_content_author_bio', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display on single posts', 'jetpack' ), - 'type' => 'checkbox', - ) - ); - } - - // Add Post Details options. - if ( ( ! empty( $post_details ) ) - && ( ! empty( $post_details['stylesheet'] ) ) - && ( ! empty( $date ) - || ! empty( $categories ) - || ! empty( $tags ) - || ! empty( $author ) - || ! empty( $comment ) ) ) { - $wp_customize->add_setting( 'jetpack_content_post_details_title' ); - - $wp_customize->add_control( - new Jetpack_Customize_Control_Title( - $wp_customize, - 'jetpack_content_post_details_title', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Post Details', 'jetpack' ), - 'type' => 'title', - ) - ) - ); + // Add Blog Display option. + if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) { + if ( 'mixed' === $blog_display ) { + $blog_display_choices = array( + 'content' => esc_html__( 'Full post', 'jetpack' ), + 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ), + 'mixed' => esc_html__( 'Default', 'jetpack' ), + ); + + $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages, or opt for the theme\'s default combination of excerpt and full post.', 'jetpack' ); + } else { + $blog_display_choices = array( + 'content' => esc_html__( 'Full post', 'jetpack' ), + 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ), + ); + + $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' ); + + if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) { + update_option( 'jetpack_content_blog_display', $blog_display ); + } + } - // Post Details: Date. - if ( ! empty( $date ) ) { $wp_customize->add_setting( - 'jetpack_content_post_details_date', + 'jetpack_content_blog_display', array( - 'default' => 1, + 'default' => $blog_display, 'type' => 'option', 'transport' => 'postMessage', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + 'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display', ) ); $wp_customize->add_control( - 'jetpack_content_post_details_date', + 'jetpack_content_blog_display', array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display date', 'jetpack' ), - 'type' => 'checkbox', + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Blog Display', 'jetpack' ), + 'description' => $blog_display_description, + 'type' => 'radio', + 'choices' => $blog_display_choices, ) ); } - // Post Details: Categories. - if ( ! empty( $categories ) ) { - $wp_customize->add_setting( - 'jetpack_content_post_details_categories', - array( - 'default' => 1, - 'type' => 'option', - 'transport' => 'postMessage', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); + // Add Author Bio option. + if ( true === $author_bio ) { + $wp_customize->add_setting( 'jetpack_content_author_bio_title' ); $wp_customize->add_control( - 'jetpack_content_post_details_categories', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display categories', 'jetpack' ), - 'type' => 'checkbox', + new Jetpack_Customize_Control_Title( + $wp_customize, + 'jetpack_content_author_bio_title', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Author Bio', 'jetpack' ), + 'type' => 'title', + ) ) ); - } - // Post Details: Tags. - if ( ! empty( $tags ) ) { $wp_customize->add_setting( - 'jetpack_content_post_details_tags', + 'jetpack_content_author_bio', array( - 'default' => 1, + 'default' => $author_bio_default, 'type' => 'option', - 'transport' => 'postMessage', 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', ) ); $wp_customize->add_control( - 'jetpack_content_post_details_tags', + 'jetpack_content_author_bio', array( 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display tags', 'jetpack' ), + 'label' => esc_html__( 'Display on single posts', 'jetpack' ), 'type' => 'checkbox', ) ); } - // Post Details: Author. - if ( ! empty( $author ) ) { - $wp_customize->add_setting( - 'jetpack_content_post_details_author', - array( - 'default' => 1, - 'type' => 'option', - 'transport' => 'postMessage', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); + // Add Post Details options. + if ( ( ! empty( $post_details ) ) + && ( ! empty( $post_details['stylesheet'] ) ) + && ( ! empty( $date ) + || ! empty( $categories ) + || ! empty( $tags ) + || ! empty( $author ) + || ! empty( $comment ) ) ) { + $wp_customize->add_setting( 'jetpack_content_post_details_title' ); $wp_customize->add_control( - 'jetpack_content_post_details_author', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display author', 'jetpack' ), - 'type' => 'checkbox', + new Jetpack_Customize_Control_Title( + $wp_customize, + 'jetpack_content_post_details_title', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Post Details', 'jetpack' ), + 'type' => 'title', + ) ) ); - } - // Post Details: Comment link. - if ( ! empty( $comment ) ) { - $wp_customize->add_setting( - 'jetpack_content_post_details_comment', - array( - 'default' => 1, - 'type' => 'option', - 'transport' => 'postMessage', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); - - $wp_customize->add_control( - 'jetpack_content_post_details_comment', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display comment link', 'jetpack' ), - 'type' => 'checkbox', - ) - ); - } - } + // Post Details: Date. + if ( ! empty( $date ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_date', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_date', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display date', 'jetpack' ), + 'type' => 'checkbox', + ) + ); + } - // Add Featured Images options. - if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) { - $wp_customize->add_setting( 'jetpack_content_featured_images_title' ); + // Post Details: Categories. + if ( ! empty( $categories ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_categories', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_categories', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display categories', 'jetpack' ), + 'type' => 'checkbox', + ) + ); + } - $wp_customize->add_control( - new Jetpack_Customize_Control_Title( - $wp_customize, - 'jetpack_content_featured_images_title', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Featured Images', 'jetpack' ) . sprintf( '%1$s', esc_html__( 'Learn more about Featured Images', 'jetpack' ) ), - 'type' => 'title', - 'active_callback' => 'jetpack_post_thumbnail_supports', - ) - ) - ); + // Post Details: Tags. + if ( ! empty( $tags ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_tags', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_tags', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display tags', 'jetpack' ), + 'type' => 'checkbox', + ) + ); + } - // Featured Images: Archive. - if ( true === $fi_archive ) { - $wp_customize->add_setting( - 'jetpack_content_featured_images_archive', - array( - 'default' => $fi_archive_default, - 'type' => 'option', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); + // Post Details: Author. + if ( ! empty( $author ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_author', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_author', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display author', 'jetpack' ), + 'type' => 'checkbox', + ) + ); + } - $wp_customize->add_control( - 'jetpack_content_featured_images_archive', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display on blog and archives', 'jetpack' ), - 'type' => 'checkbox', - 'active_callback' => 'jetpack_post_thumbnail_supports', - ) - ); + // Post Details: Comment link. + if ( ! empty( $comment ) ) { + $wp_customize->add_setting( + 'jetpack_content_post_details_comment', + array( + 'default' => 1, + 'type' => 'option', + 'transport' => 'postMessage', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_post_details_comment', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display comment link', 'jetpack' ), + 'type' => 'checkbox', + ) + ); + } } - // Featured Images: Post. - if ( true === $fi_post ) { - $wp_customize->add_setting( - 'jetpack_content_featured_images_post', - array( - 'default' => $fi_post_default, - 'type' => 'option', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); + // Add Featured Images options. + if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) { + $wp_customize->add_setting( 'jetpack_content_featured_images_title' ); $wp_customize->add_control( - 'jetpack_content_featured_images_post', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display on single posts', 'jetpack' ), - 'type' => 'checkbox', - 'active_callback' => 'jetpack_post_thumbnail_supports', - ) - ); - } - - // Featured Images: Page. - if ( true === $fi_page ) { - $wp_customize->add_setting( - 'jetpack_content_featured_images_page', - array( - 'default' => $fi_page_default, - 'type' => 'option', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + new Jetpack_Customize_Control_Title( + $wp_customize, + 'jetpack_content_featured_images_title', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Featured Images', 'jetpack' ) . sprintf( '%1$s', esc_html__( 'Learn more about Featured Images', 'jetpack' ) ), + 'type' => 'title', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) ) ); - $wp_customize->add_control( - 'jetpack_content_featured_images_page', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display on pages', 'jetpack' ), - 'type' => 'checkbox', - 'active_callback' => 'jetpack_post_thumbnail_supports', - ) - ); - } + // Featured Images: Archive. + if ( true === $fi_archive ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_archive', + array( + 'default' => $fi_archive_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_archive', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on blog and archives', 'jetpack' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } - // Featured Images: Portfolio. - if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) { - $wp_customize->add_setting( - 'jetpack_content_featured_images_portfolio', - array( - 'default' => $fi_portfolio_default, - 'type' => 'option', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); + // Featured Images: Post. + if ( true === $fi_post ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_post', + array( + 'default' => $fi_post_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_post', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on single posts', 'jetpack' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } - $wp_customize->add_control( - 'jetpack_content_featured_images_portfolio', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Display on single projects', 'jetpack' ), - 'type' => 'checkbox', - 'active_callback' => 'jetpack_post_thumbnail_supports', - ) - ); - } + // Featured Images: Page. + if ( true === $fi_page ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_page', + array( + 'default' => $fi_page_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_page', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on pages', 'jetpack' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } - // Featured Images: Fallback. - if ( true === $fi_fallback ) { - $wp_customize->add_setting( - 'jetpack_content_featured_images_fallback', - array( - 'default' => $fi_fallback_default, - 'type' => 'option', - 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', - ) - ); + // Featured Images: Portfolio. + if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_portfolio', + array( + 'default' => $fi_portfolio_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_portfolio', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Display on single projects', 'jetpack' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } - $wp_customize->add_control( - 'jetpack_content_featured_images_fallback', - array( - 'section' => 'jetpack_content_options', - 'label' => esc_html__( 'Automatically use first image in post', 'jetpack' ), - 'type' => 'checkbox', - 'active_callback' => 'jetpack_post_thumbnail_supports', - ) - ); + // Featured Images: Fallback. + if ( true === $fi_fallback ) { + $wp_customize->add_setting( + 'jetpack_content_featured_images_fallback', + array( + 'default' => $fi_fallback_default, + 'type' => 'option', + 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox', + ) + ); + + $wp_customize->add_control( + 'jetpack_content_featured_images_fallback', + array( + 'section' => 'jetpack_content_options', + 'label' => esc_html__( 'Automatically use first image in post', 'jetpack' ), + 'type' => 'checkbox', + 'active_callback' => 'jetpack_post_thumbnail_supports', + ) + ); + } } } + add_action( 'customize_register', 'jetpack_content_options_customize_register' ); + } -add_action( 'customize_register', 'jetpack_content_options_customize_register' ); -/** - * Return whether the theme supports Post Thumbnails. - */ -function jetpack_post_thumbnail_supports() { - return ( current_theme_supports( 'post-thumbnails' ) ); +if ( ! function_exists( 'jetpack_post_thumbnail_supports' ) ) { + + /** + * Return whether the theme supports Post Thumbnails. + */ + function jetpack_post_thumbnail_supports() { + return ( current_theme_supports( 'post-thumbnails' ) ); + } + } -/** - * Sanitize the checkbox. - * - * @param int $input The unsanitized value from the setting. - * @return boolean|string - */ -function jetpack_content_options_sanitize_checkbox( $input ) { - return ( 1 === (int) $input ) ? 1 : ''; +if ( ! function_exists( 'jetpack_content_options_sanitize_checkbox' ) ) { + + /** + * Sanitize the checkbox. + * + * @param int $input The unsanitized value from the setting. + * @return boolean|string + */ + function jetpack_content_options_sanitize_checkbox( $input ) { + return ( 1 === (int) $input ) ? 1 : ''; + } + } -/** - * Sanitize the Display value. - * - * @param string $display The unsanitized value from the setting. - * @return string - */ -function jetpack_content_options_sanitize_blog_display( $display ) { - if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ), true ) ) { - $display = 'content'; +if ( ! function_exists( 'jetpack_content_options_sanitize_blog_display' ) ) { + + /** + * Sanitize the Display value. + * + * @param string $display The unsanitized value from the setting. + * @return string + */ + function jetpack_content_options_sanitize_blog_display( $display ) { + if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ), true ) ) { + $display = 'content'; + } + return $display; } - return $display; + } -/** - * Binds JS handlers to make Theme Customizer preview reload changes asynchronously. - */ -function jetpack_content_options_customize_preview_js() { - $options = get_theme_support( 'jetpack-content-options' ); - $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null; - $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display ); - sort( $blog_display ); - $blog_display = implode( ', ', $blog_display ); - $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display; - $masonry = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null; - $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; - $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; - $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; - $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; - $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; - $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; - - wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'jquery', 'customize-preview' ), '1.0', true ); - - wp_localize_script( - 'jetpack-content-options-customizer', - 'blogDisplay', - array( - 'display' => get_option( 'jetpack_content_blog_display', $blog_display ), - 'masonry' => $masonry, - ) - ); - - wp_localize_script( - 'jetpack-content-options-customizer', - 'postDetails', - array( - 'date' => $date, - 'categories' => $categories, - 'tags' => $tags, - 'author' => $author, - 'comment' => $comment, - ) - ); +if ( ! function_exists( 'jetpack_content_options_customize_preview_js' ) ) { + + /** + * Binds JS handlers to make Theme Customizer preview reload changes asynchronously. + */ + function jetpack_content_options_customize_preview_js() { + $options = get_theme_support( 'jetpack-content-options' ); + $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null; + $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display ); + sort( $blog_display ); + $blog_display = implode( ', ', $blog_display ); + $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display; + $masonry = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null; + $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; + $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; + $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; + $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; + $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; + $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; + + wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'jquery', 'customize-preview' ), '1.0', true ); + + wp_localize_script( + 'jetpack-content-options-customizer', + 'blogDisplay', + array( + 'display' => get_option( 'jetpack_content_blog_display', $blog_display ), + 'masonry' => $masonry, + ) + ); + + wp_localize_script( + 'jetpack-content-options-customizer', + 'postDetails', + array( + 'date' => $date, + 'categories' => $categories, + 'tags' => $tags, + 'author' => $author, + 'comment' => $comment, + ) + ); + } + add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' ); + } -add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' ); diff --git a/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images-fallback.php b/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images-fallback.php index 79045af6351f4..350862899186d 100644 --- a/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images-fallback.php +++ b/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images-fallback.php @@ -5,222 +5,238 @@ * @package automattic/jetpack */ -/** - * Get one image from a specified post in the following order: - * Featured Image then first image from the_content HTML - * and filter the post_thumbnail_html - * - * @param string $html The HTML for the image markup. - * @param int $post_id The post ID to check. - * @param int $post_thumbnail_id The ID of the featured image. - * @param string $size The image size to return, defaults to 'post-thumbnail'. - * @param string|array $attr Optional. Query string or array of attributes. - * - * @return string $html Thumbnail image with markup. - */ -function jetpack_featured_images_fallback_get_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - $opts = jetpack_featured_images_get_settings(); - - if ( ! empty( $html ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { - return trim( $html ); - } - - if ( jetpack_featured_images_should_load() ) { - if ( - ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) - || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) - || ! $opts['fallback-option'] - ) { +if ( ! function_exists( 'jetpack_featured_images_fallback_get_image' ) ) { + + /** + * Get one image from a specified post in the following order: + * Featured Image then first image from the_content HTML + * and filter the post_thumbnail_html + * + * @param string $html The HTML for the image markup. + * @param int $post_id The post ID to check. + * @param int $post_thumbnail_id The ID of the featured image. + * @param string $size The image size to return, defaults to 'post-thumbnail'. + * @param string|array $attr Optional. Query string or array of attributes. + * + * @return string $html Thumbnail image with markup. + */ + function jetpack_featured_images_fallback_get_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $opts = jetpack_featured_images_get_settings(); + + if ( ! empty( $html ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { return trim( $html ); } - } - if ( class_exists( 'Jetpack_PostImages' ) ) { - global $_wp_additional_image_sizes; - - $args = array( - 'from_thumbnail' => false, - 'from_slideshow' => true, - 'from_gallery' => true, - 'from_attachment' => false, - ); - - $image = Jetpack_PostImages::get_image( $post_id, $args ); - - if ( ! empty( $image ) ) { - $image['width'] = ''; - $image['height'] = ''; - $image['crop'] = ''; - - if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { - $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; - $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; - $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; + if ( jetpack_featured_images_should_load() ) { + if ( + ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) + || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) + || ! $opts['fallback-option'] + ) { + return trim( $html ); } + } - // Force `crop` to be a simple boolean, to avoid dealing with WP crop positions. - $image['crop'] = boolval( $image['crop'] ); + if ( class_exists( 'Jetpack_PostImages' ) ) { + global $_wp_additional_image_sizes; - $image_sizes = ''; + $args = array( + 'from_thumbnail' => false, + 'from_slideshow' => true, + 'from_gallery' => true, + 'from_attachment' => false, + ); - $width = isset( $image['width'] ) ? intval( $image['width'] ) : null; - $height = isset( $image['height'] ) ? intval( $image['height'] ) : null; + $image = Jetpack_PostImages::get_image( $post_id, $args ); - if ( ! empty( $image['src_width'] ) && ! empty( $image['src_height'] ) && ! empty( $image['width'] ) && ! empty( $image['height'] ) ) { - $src_width = intval( $image['src_width'] ); - $src_height = intval( $image['src_height'] ); + if ( ! empty( $image ) ) { + $image['width'] = ''; + $image['height'] = ''; + $image['crop'] = ''; - // If we're aware of the source dimensions, calculate the final image height and width. - // This allows us to provide them as attributes in the `` tag, to avoid layout shifts. - // It also allows us to calculate a `srcset`. - if ( $image['crop'] ) { - // If we're cropping, the final dimensions are calculated independently of each other. - $width = min( $width, $src_width ); - $height = min( $height, $src_height ); - } else { - // If we're not cropping, we need to preserve aspect ratio. - $dims = wp_constrain_dimensions( $src_width, $src_height, $width, $height ); - $width = $dims[0]; - $height = $dims[1]; + if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { + $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; + $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; + $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; } - $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $width, $height ); - $image_srcset = Jetpack_PostImages::generate_cropped_srcset( $image, $width, $height, true ); - $image_sizes = 'min(' . $width . 'px, 100vw)'; - } else { - // If we're not aware of the source dimensions, leave the size calculations to the CDN, and - // fall back to a simpler `` tag without `width`/`height` or `srcset`. - $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); - - // Use the theme's crop setting rather than forcing to true. - $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); - - $image_srcset = null; - $image_sizes = null; - } - - $default_attr = array( - 'srcset' => $image_srcset, - 'sizes' => $image_sizes, - 'loading' => is_singular() ? 'eager' : 'lazy', - 'decoding' => 'async', - 'title' => wp_strip_all_tags( get_the_title() ), - 'alt' => '', - 'class' => "attachment-$size wp-post-image", - ); + // Force `crop` to be a simple boolean, to avoid dealing with WP crop positions. + $image['crop'] = boolval( $image['crop'] ); + + $image_sizes = ''; + + $width = isset( $image['width'] ) ? intval( $image['width'] ) : null; + $height = isset( $image['height'] ) ? intval( $image['height'] ) : null; + + if ( ! empty( $image['src_width'] ) && ! empty( $image['src_height'] ) && ! empty( $image['width'] ) && ! empty( $image['height'] ) ) { + $src_width = intval( $image['src_width'] ); + $src_height = intval( $image['src_height'] ); + + // If we're aware of the source dimensions, calculate the final image height and width. + // This allows us to provide them as attributes in the `` tag, to avoid layout shifts. + // It also allows us to calculate a `srcset`. + if ( $image['crop'] ) { + // If we're cropping, the final dimensions are calculated independently of each other. + $width = min( $width, $src_width ); + $height = min( $height, $src_height ); + } else { + // If we're not cropping, we need to preserve aspect ratio. + $dims = wp_constrain_dimensions( $src_width, $src_height, $width, $height ); + $width = $dims[0]; + $height = $dims[1]; + } + + $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $width, $height ); + $image_srcset = Jetpack_PostImages::generate_cropped_srcset( $image, $width, $height, true ); + $image_sizes = 'min(' . $width . 'px, 100vw)'; + } else { + // If we're not aware of the source dimensions, leave the size calculations to the CDN, and + // fall back to a simpler `` tag without `width`/`height` or `srcset`. + $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); - $image_attr = wp_parse_args( $attr, $default_attr ); - $hwstring = image_hwstring( $width, $height ); + // Use the theme's crop setting rather than forcing to true. + $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); - $html = rtrim( " $value ) { - if ( $value ) { - $html .= " $name=" . '"' . esc_attr( $value ) . '"'; + $default_attr = array( + 'srcset' => $image_srcset, + 'sizes' => $image_sizes, + 'loading' => is_singular() ? 'eager' : 'lazy', + 'decoding' => 'async', + 'title' => wp_strip_all_tags( get_the_title() ), + 'alt' => '', + 'class' => "attachment-$size wp-post-image", + ); + + $image_attr = wp_parse_args( $attr, $default_attr ); + $hwstring = image_hwstring( $width, $height ); + + $html = rtrim( " $value ) { + if ( $value ) { + $html .= " $name=" . '"' . esc_attr( $value ) . '"'; + } } - } - $html .= ' />'; + $html .= ' />'; - return trim( $html ); + return trim( $html ); + } } + + return trim( $html ); } + add_filter( 'post_thumbnail_html', 'jetpack_featured_images_fallback_get_image', 10, 5 ); - return trim( $html ); } -add_filter( 'post_thumbnail_html', 'jetpack_featured_images_fallback_get_image', 10, 5 ); -/** - * Get URL of one image from a specified post in the following order: - * Featured Image then first image from the_content HTML - * - * @param int $post_id The post ID to check. - * @param int $post_thumbnail_id The ID of the featured image. - * @param string $size The image size to return, defaults to 'post-thumbnail'. - * - * @return string|null $image_src The URL of the thumbnail image. - */ -function jetpack_featured_images_fallback_get_image_src( $post_id, $post_thumbnail_id, $size ) { - $image_src = wp_get_attachment_image_src( $post_thumbnail_id, $size ); - $image_src = ( ! empty( $image_src[0] ) ) ? $image_src[0] : null; - $opts = jetpack_featured_images_get_settings(); - - if ( ! empty( $image_src ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { - return esc_url( $image_src ); - } +if ( ! function_exists( 'jetpack_featured_images_fallback_get_image_src' ) ) { + + /** + * Get URL of one image from a specified post in the following order: + * Featured Image then first image from the_content HTML + * + * @param int $post_id The post ID to check. + * @param int $post_thumbnail_id The ID of the featured image. + * @param string $size The image size to return, defaults to 'post-thumbnail'. + * + * @return string|null $image_src The URL of the thumbnail image. + */ + function jetpack_featured_images_fallback_get_image_src( $post_id, $post_thumbnail_id, $size ) { + $image_src = wp_get_attachment_image_src( $post_thumbnail_id, $size ); + $image_src = ( ! empty( $image_src[0] ) ) ? $image_src[0] : null; + $opts = jetpack_featured_images_get_settings(); + + if ( ! empty( $image_src ) || (bool) 1 !== (bool) $opts['fallback-option'] ) { + return esc_url( $image_src ); + } - if ( jetpack_featured_images_should_load() ) { - if ( ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) - || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) ) { - return esc_url( $image_src ); + if ( jetpack_featured_images_should_load() ) { + if ( ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] ) + || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) ) { + return esc_url( $image_src ); + } } - } - if ( class_exists( 'Jetpack_PostImages' ) ) { - global $_wp_additional_image_sizes; + if ( class_exists( 'Jetpack_PostImages' ) ) { + global $_wp_additional_image_sizes; - $args = array( - 'from_thumbnail' => false, - 'from_slideshow' => true, - 'from_gallery' => true, - 'from_attachment' => false, - ); + $args = array( + 'from_thumbnail' => false, + 'from_slideshow' => true, + 'from_gallery' => true, + 'from_attachment' => false, + ); - $image = Jetpack_PostImages::get_image( $post_id, $args ); + $image = Jetpack_PostImages::get_image( $post_id, $args ); - if ( ! empty( $image ) ) { - $image['width'] = ''; - $image['height'] = ''; - $image['crop'] = ''; + if ( ! empty( $image ) ) { + $image['width'] = ''; + $image['height'] = ''; + $image['crop'] = ''; - if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { - $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; - $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; - $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; - } + if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) { + $image['width'] = $_wp_additional_image_sizes[ $size ]['width']; + $image['height'] = $_wp_additional_image_sizes[ $size ]['height']; + $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop']; + } - $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); + $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] ); - // Use the theme's crop setting rather than forcing to true. - $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); + // Use the theme's crop setting rather than forcing to true. + $image_src = add_query_arg( 'crop', $image['crop'], $image_src ); - return esc_url( $image_src ); + return esc_url( $image_src ); + } } + + return esc_url( $image_src ); } - return esc_url( $image_src ); } -/** - * Check if post has an image attached, including a fallback. - * - * @param int $post The post ID to check. - * - * @return bool - */ -function jetpack_has_featured_image( $post = null ) { - return (bool) get_the_post_thumbnail( $post ); +if ( ! function_exists( 'jetpack_has_featured_image' ) ) { + + /** + * Check if post has an image attached, including a fallback. + * + * @param int $post The post ID to check. + * + * @return bool + */ + function jetpack_has_featured_image( $post = null ) { + return (bool) get_the_post_thumbnail( $post ); + } + } -/** - * Adds custom class to the array of post classes. - * - * @param array $classes Classes for the post element. - * @param array $class Optional. Comma separated list of additional classes. - * @param array $post_id Unique The post ID to check. - * - * @return array $classes - */ -function jetpack_featured_images_post_class( $classes, $class, $post_id ) { - $post_password_required = post_password_required( $post_id ); - $opts = jetpack_featured_images_get_settings(); +if ( ! function_exists( 'jetpack_featured_images_post_class' ) ) { + + /** + * Adds custom class to the array of post classes. + * + * @param array $classes Classes for the post element. + * @param array $class Optional. Comma separated list of additional classes. + * @param array $post_id Unique The post ID to check. + * + * @return array $classes + */ + function jetpack_featured_images_post_class( $classes, $class, $post_id ) { + $post_password_required = post_password_required( $post_id ); + $opts = jetpack_featured_images_get_settings(); + + if ( jetpack_has_featured_image( $post_id ) && (bool) 1 === (bool) $opts['fallback-option'] && ! is_attachment() && ! $post_password_required && 'post' === get_post_type() ) { + $classes[] = 'has-post-thumbnail'; + $classes[] = 'fallback-thumbnail'; + } - if ( jetpack_has_featured_image( $post_id ) && (bool) 1 === (bool) $opts['fallback-option'] && ! is_attachment() && ! $post_password_required && 'post' === get_post_type() ) { - $classes[] = 'has-post-thumbnail'; - $classes[] = 'fallback-thumbnail'; + return $classes; } + add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 ); - return $classes; } -add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 ); diff --git a/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images.php b/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images.php index 44b6db2ea4c8f..95af5926aedef 100644 --- a/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images.php +++ b/projects/plugins/jetpack/modules/theme-tools/content-options/featured-images.php @@ -5,113 +5,125 @@ * @package automattic/jetpack */ -/** - * The function to prevent for Featured Images to be displayed in a theme. - * - * @param array $metadata Post metadata. - * @param int $object_id Post ID. - * @param string $meta_key Metadata key. - */ -function jetpack_featured_images_remove_post_thumbnail( $metadata, $object_id, $meta_key ) { - $opts = jetpack_featured_images_get_settings(); +if ( ! function_exists( 'jetpack_featured_images_remove_post_thumbnail' ) ) { /** - * Allow featured images to be displayed at all times for specific CPTs. - * - * @module theme-tools + * The function to prevent for Featured Images to be displayed in a theme. * - * @since 9.1.0 - * - * @param array $excluded_post_types Array of excluded post types. + * @param array $metadata Post metadata. + * @param int $object_id Post ID. + * @param string $meta_key Metadata key. */ - $excluded_post_types = apply_filters( - 'jetpack_content_options_featured_image_exclude_cpt', - array( 'jp_pay_product' ) - ); - - // Automatically return metadata for specific post types, when we don't want to hide the Featured Image. - if ( in_array( get_post_type( $object_id ), $excluded_post_types, true ) ) { - return $metadata; - } + function jetpack_featured_images_remove_post_thumbnail( $metadata, $object_id, $meta_key ) { + $opts = jetpack_featured_images_get_settings(); - // Return false if the archive option or singular option is unticked. - if ( - ( true === $opts['archive'] - && ( is_home() || is_archive() || is_search() ) - && ! jetpack_is_shop_page() - && ! $opts['archive-option'] - && ( isset( $meta_key ) - && '_thumbnail_id' === $meta_key ) - && in_the_loop() - ) - || ( true === $opts['post'] - && is_single() - && ! jetpack_is_product() - && ! $opts['post-option'] - && ( isset( $meta_key ) - && '_thumbnail_id' === $meta_key ) - && in_the_loop() - ) - || ( true === $opts['page'] - && is_singular() - && is_page() - && ! $opts['page-option'] - && ( isset( $meta_key ) - && '_thumbnail_id' === $meta_key ) - && in_the_loop() - ) - || ( true === $opts['portfolio'] - && post_type_exists( 'jetpack-portfolio' ) - && is_singular( 'jetpack-portfolio' ) - && ! $opts['portfolio-option'] - && ( isset( $meta_key ) - && '_thumbnail_id' === $meta_key ) - && in_the_loop() - ) - ) { - - // Do not override thumbnail settings within blocks (eg. Latest Posts block). - $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace - foreach ( $trace as $frame ) { - if ( ! empty( $frame['class'] ) && class_exists( $frame['class'], false ) && is_a( $frame['class'], WP_Block::class, true ) ) { - return $metadata; - } + /** + * Allow featured images to be displayed at all times for specific CPTs. + * + * @module theme-tools + * + * @since 9.1.0 + * + * @param array $excluded_post_types Array of excluded post types. + */ + $excluded_post_types = apply_filters( + 'jetpack_content_options_featured_image_exclude_cpt', + array( 'jp_pay_product' ) + ); + + // Automatically return metadata for specific post types, when we don't want to hide the Featured Image. + if ( in_array( get_post_type( $object_id ), $excluded_post_types, true ) ) { + return $metadata; } - return false; - } else { - return $metadata; + // Return false if the archive option or singular option is unticked. + if ( + ( true === $opts['archive'] + && ( is_home() || is_archive() || is_search() ) + && ! jetpack_is_shop_page() + && ! $opts['archive-option'] + && ( isset( $meta_key ) + && '_thumbnail_id' === $meta_key ) + && in_the_loop() + ) + || ( true === $opts['post'] + && is_single() + && ! jetpack_is_product() + && ! $opts['post-option'] + && ( isset( $meta_key ) + && '_thumbnail_id' === $meta_key ) + && in_the_loop() + ) + || ( true === $opts['page'] + && is_singular() + && is_page() + && ! $opts['page-option'] + && ( isset( $meta_key ) + && '_thumbnail_id' === $meta_key ) + && in_the_loop() + ) + || ( true === $opts['portfolio'] + && post_type_exists( 'jetpack-portfolio' ) + && is_singular( 'jetpack-portfolio' ) + && ! $opts['portfolio-option'] + && ( isset( $meta_key ) + && '_thumbnail_id' === $meta_key ) + && in_the_loop() + ) + ) { + + // Do not override thumbnail settings within blocks (eg. Latest Posts block). + $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace + foreach ( $trace as $frame ) { + if ( ! empty( $frame['class'] ) && class_exists( $frame['class'], false ) && is_a( $frame['class'], WP_Block::class, true ) ) { + return $metadata; + } + } + + return false; + } else { + return $metadata; + } } -} -add_filter( 'get_post_metadata', 'jetpack_featured_images_remove_post_thumbnail', true, 3 ); + add_filter( 'get_post_metadata', 'jetpack_featured_images_remove_post_thumbnail', true, 3 ); -/** - * Check if we are in a WooCommerce Product in order to exclude it from the is_single check. - */ -function jetpack_is_product() { - return ( function_exists( 'is_product' ) ) ? is_product() : false; } -/** - * Check if we are in a WooCommerce Shop in order to exclude it from the is_archive check. - */ -function jetpack_is_shop_page() { - // Check if WooCommerce is active first. - if ( ! class_exists( 'WooCommerce' ) ) { - return false; +if ( ! function_exists( 'jetpack_is_product' ) ) { + + /** + * Check if we are in a WooCommerce Product in order to exclude it from the is_single check. + */ + function jetpack_is_product() { + return ( function_exists( 'is_product' ) ) ? is_product() : false; } - global $wp_query; +} + +if ( ! function_exists( 'jetpack_is_shop_page' ) ) { - $front_page_id = get_option( 'page_on_front' ); - $current_page_id = $wp_query->get( 'page_id' ); - $is_static_front_page = 'page' === get_option( 'show_on_front' ); + /** + * Check if we are in a WooCommerce Shop in order to exclude it from the is_archive check. + */ + function jetpack_is_shop_page() { + // Check if WooCommerce is active first. + if ( ! class_exists( 'WooCommerce' ) ) { + return false; + } + + global $wp_query; + + $front_page_id = get_option( 'page_on_front' ); + $current_page_id = $wp_query->get( 'page_id' ); + $is_static_front_page = 'page' === get_option( 'show_on_front' ); + + if ( $is_static_front_page && $front_page_id === $current_page_id ) { + $is_shop_page = ( wc_get_page_id( 'shop' ) === $current_page_id ) ? true : false; + } else { + $is_shop_page = is_shop(); + } - if ( $is_static_front_page && $front_page_id === $current_page_id ) { - $is_shop_page = ( wc_get_page_id( 'shop' ) === $current_page_id ) ? true : false; - } else { - $is_shop_page = is_shop(); + return $is_shop_page; } - return $is_shop_page; } diff --git a/projects/plugins/jetpack/modules/theme-tools/content-options/post-details.php b/projects/plugins/jetpack/modules/theme-tools/content-options/post-details.php index 25220fbdab3d3..73919ab6f7d7f 100644 --- a/projects/plugins/jetpack/modules/theme-tools/content-options/post-details.php +++ b/projects/plugins/jetpack/modules/theme-tools/content-options/post-details.php @@ -5,159 +5,171 @@ * @package automattic/jetpack */ -/** - * The function to include Post Details in a theme's stylesheet. - */ -function jetpack_post_details_enqueue_scripts() { - // Make sure we can proceed. - list( $should_run, $options, $definied, $post_details ) = jetpack_post_details_should_run(); - - if ( ! $should_run ) { - return; - } - - list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options; - list( $date, $categories, $tags, $author, $comment ) = $definied; - - $elements = array(); - - // If date option is unticked, add it to the list of classes. - if ( 1 !== (int) $date_option && ! empty( $date ) ) { - $elements[] = $date; - } - - // If categories option is unticked, add it to the list of classes. - if ( 1 !== (int) $categories_option && ! empty( $categories ) ) { - $elements[] = $categories; - } +if ( ! function_exists( 'jetpack_post_details_enqueue_scripts' ) ) { - // If tags option is unticked, add it to the list of classes. - if ( 1 !== (int) $tags_option && ! empty( $tags ) ) { - $elements[] = $tags; - } + /** + * The function to include Post Details in a theme's stylesheet. + */ + function jetpack_post_details_enqueue_scripts() { + // Make sure we can proceed. + list( $should_run, $options, $definied, $post_details ) = jetpack_post_details_should_run(); - // If author option is unticked, add it to the list of classes. - if ( 1 !== (int) $author_option && ! empty( $author ) ) { - $elements[] = $author; - } + if ( ! $should_run ) { + return; + } - // If comment option is unticked, add it to the list of classes. - if ( 1 !== (int) $comment_option && ! empty( $comment ) ) { - $elements[] = $comment; - } + list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options; + list( $date, $categories, $tags, $author, $comment ) = $definied; - // If the Elements array is empty, return without setting custom CSS. - if ( empty( $elements ) ) { - return; - } + $elements = array(); - // Get the list of classes. - $elements = implode( ', ', $elements ); + // If date option is unticked, add it to the list of classes. + if ( 1 !== (int) $date_option && ! empty( $date ) ) { + $elements[] = $date; + } - // Hide the classes with CSS. - $css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }'; - - // Add the CSS to the stylesheet. - wp_add_inline_style( $post_details['stylesheet'], $css ); -} -add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' ); - -/** - * Adds custom classes to the array of body classes. - * - * @param array $classes Classes for the body element. - */ -function jetpack_post_details_body_classes( $classes ) { - // Make sure we can proceed. - list( $should_run, $options, $definied ) = jetpack_post_details_should_run(); + // If categories option is unticked, add it to the list of classes. + if ( 1 !== (int) $categories_option && ! empty( $categories ) ) { + $elements[] = $categories; + } - if ( ! $should_run ) { - return $classes; - } + // If tags option is unticked, add it to the list of classes. + if ( 1 !== (int) $tags_option && ! empty( $tags ) ) { + $elements[] = $tags; + } - list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options; - list( $date, $categories, $tags, $author, $comment ) = $definied; + // If author option is unticked, add it to the list of classes. + if ( 1 !== (int) $author_option && ! empty( $author ) ) { + $elements[] = $author; + } - // If date option is unticked, add a class of 'date-hidden' to the body. - if ( 1 !== (int) $date_option && ! empty( $date ) ) { - $classes[] = 'date-hidden'; - } + // If comment option is unticked, add it to the list of classes. + if ( 1 !== (int) $comment_option && ! empty( $comment ) ) { + $elements[] = $comment; + } - // If categories option is unticked, add a class of 'categories-hidden' to the body. - if ( 1 !== (int) $categories_option && ! empty( $categories ) ) { - $classes[] = 'categories-hidden'; - } + // If the Elements array is empty, return without setting custom CSS. + if ( empty( $elements ) ) { + return; + } - // If tags option is unticked, add a class of 'tags-hidden' to the body. - if ( 1 !== (int) $tags_option && ! empty( $tags ) ) { - $classes[] = 'tags-hidden'; - } + // Get the list of classes. + $elements = implode( ', ', $elements ); - // If author option is unticked, add a class of 'author-hidden' to the body. - if ( 1 !== (int) $author_option && ! empty( $author ) ) { - $classes[] = 'author-hidden'; - } + // Hide the classes with CSS. + $css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }'; - // If comment option is unticked, add a class of 'comment-hidden' to the body. - if ( 1 !== (int) $comment_option && ! empty( $comment ) ) { - $classes[] = 'comment-hidden'; + // Add the CSS to the stylesheet. + wp_add_inline_style( $post_details['stylesheet'], $css ); } + add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' ); - return $classes; } -add_filter( 'body_class', 'jetpack_post_details_body_classes' ); - -/** - * Determines if Post Details should run. - */ -function jetpack_post_details_should_run() { - // Empty value representing falsy return value. - $void = array( false, null, null, null ); - // If the theme doesn't support 'jetpack-content-options', don't continue. - if ( ! current_theme_supports( 'jetpack-content-options' ) ) { - return $void; - } +if ( ! function_exists( 'jetpack_post_details_body_classes' ) ) { + + /** + * Adds custom classes to the array of body classes. + * + * @param array $classes Classes for the body element. + */ + function jetpack_post_details_body_classes( $classes ) { + // Make sure we can proceed. + list( $should_run, $options, $definied ) = jetpack_post_details_should_run(); + + if ( ! $should_run ) { + return $classes; + } + + list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options; + list( $date, $categories, $tags, $author, $comment ) = $definied; + + // If date option is unticked, add a class of 'date-hidden' to the body. + if ( 1 !== (int) $date_option && ! empty( $date ) ) { + $classes[] = 'date-hidden'; + } + + // If categories option is unticked, add a class of 'categories-hidden' to the body. + if ( 1 !== (int) $categories_option && ! empty( $categories ) ) { + $classes[] = 'categories-hidden'; + } + + // If tags option is unticked, add a class of 'tags-hidden' to the body. + if ( 1 !== (int) $tags_option && ! empty( $tags ) ) { + $classes[] = 'tags-hidden'; + } + + // If author option is unticked, add a class of 'author-hidden' to the body. + if ( 1 !== (int) $author_option && ! empty( $author ) ) { + $classes[] = 'author-hidden'; + } + + // If comment option is unticked, add a class of 'comment-hidden' to the body. + if ( 1 !== (int) $comment_option && ! empty( $comment ) ) { + $classes[] = 'comment-hidden'; + } - $options = get_theme_support( 'jetpack-content-options' ); - $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; - - // If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue. - if ( empty( $post_details ) ) { - return $void; - } - - $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; - $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; - $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; - $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; - $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; - - // If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue. - if ( - empty( $post_details['stylesheet'] ) - && ( empty( $date ) - || empty( $categories ) - || empty( $tags ) - || empty( $author ) - || empty( $comment ) ) - ) { - return $void; + return $classes; } + add_filter( 'body_class', 'jetpack_post_details_body_classes' ); - $date_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_date', 1 ); - $categories_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_categories', 1 ); - $tags_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_tags', 1 ); - $author_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_author', 1 ); - $comment_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_comment', 1 ); - - $options = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option ); - $definied = array( $date, $categories, $tags, $author, $comment ); +} - // If all the options are ticked, don't continue. - if ( array( 1, 1, 1, 1, 1 ) === $options ) { - return $void; +if ( ! function_exists( 'jetpack_post_details_should_run' ) ) { + + /** + * Determines if Post Details should run. + */ + function jetpack_post_details_should_run() { + // Empty value representing falsy return value. + $void = array( false, null, null, null ); + + // If the theme doesn't support 'jetpack-content-options', don't continue. + if ( ! current_theme_supports( 'jetpack-content-options' ) ) { + return $void; + } + + $options = get_theme_support( 'jetpack-content-options' ); + $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null; + + // If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue. + if ( empty( $post_details ) ) { + return $void; + } + + $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null; + $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null; + $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null; + $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null; + $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null; + + // If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue. + if ( + empty( $post_details['stylesheet'] ) + && ( empty( $date ) + || empty( $categories ) + || empty( $tags ) + || empty( $author ) + || empty( $comment ) ) + ) { + return $void; + } + + $date_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_date', 1 ); + $categories_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_categories', 1 ); + $tags_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_tags', 1 ); + $author_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_author', 1 ); + $comment_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_comment', 1 ); + + $options = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option ); + $definied = array( $date, $categories, $tags, $author, $comment ); + + // If all the options are ticked, don't continue. + if ( array( 1, 1, 1, 1, 1 ) === $options ) { + return $void; + } + + return array( true, $options, $definied, $post_details ); } - return array( true, $options, $definied, $post_details ); } From 02e8432774c47211a072e40f6f106feeb2418988 Mon Sep 17 00:00:00 2001 From: Karen Attfield Date: Thu, 22 Aug 2024 15:44:04 +0100 Subject: [PATCH 2/7] Fixing and commenting phan issues where possible --- .../classic-theme-helper/.phan/baseline.php | 1 + .../src/content-options/author-bio.php | 2 +- .../src/content-options/blog-display.php | 4 +-- .../src/content-options/customizer.php | 35 ++++++++++--------- .../featured-images-fallback.php | 15 ++++---- .../src/content-options/featured-images.php | 11 +++--- .../src/content-options/post-details.php | 29 ++++++++------- projects/plugins/jetpack/.phan/baseline.php | 13 +++---- .../content-options/customizer.php | 34 ++++++++++-------- 9 files changed, 80 insertions(+), 64 deletions(-) diff --git a/projects/packages/classic-theme-helper/.phan/baseline.php b/projects/packages/classic-theme-helper/.phan/baseline.php index 8b19e8c51bca0..5a86f2ce604d0 100644 --- a/projects/packages/classic-theme-helper/.phan/baseline.php +++ b/projects/packages/classic-theme-helper/.phan/baseline.php @@ -25,6 +25,7 @@ '_inc/lib/tonesque.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentInternal', 'PhanTypeMismatchArgumentProbablyReal'], 'src/class-featured-content.php' => ['PhanTypeComparisonToArray', 'PhanTypeInvalidDimOffset', 'PhanTypeMismatchArgument', 'PhanTypeMismatchProperty', 'PhanTypePossiblyInvalidDimOffset'], 'src/class-social-links.php' => ['PhanUndeclaredClassMethod', 'PhanUndeclaredClassReference', 'PhanUndeclaredTypeProperty'], + 'src/content-options/featured-images-fallback.php' => ['PhanTypePossiblyInvalidDimOffset'], ], // 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed. // (directory_suppressions will currently be ignored by subsequent calls to --save-baseline, but may be preserved in future Phan releases) diff --git a/projects/packages/classic-theme-helper/src/content-options/author-bio.php b/projects/packages/classic-theme-helper/src/content-options/author-bio.php index 03b32d4532114..93ab623ce0668 100644 --- a/projects/packages/classic-theme-helper/src/content-options/author-bio.php +++ b/projects/packages/classic-theme-helper/src/content-options/author-bio.php @@ -74,7 +74,7 @@ function jetpack_author_bio() {

-