Skip to content

Commit

Permalink
Merge pull request #54 from cyrillbolliger/dev
Browse files Browse the repository at this point in the history
3.3.0
  • Loading branch information
cyrillbolliger authored Feb 14, 2025
2 parents d52ec86 + f6e50d4 commit 73334c9
Show file tree
Hide file tree
Showing 10 changed files with 2,338 additions and 2,338 deletions.
28 changes: 24 additions & 4 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: cyrillbolliger
Tags: thumbnail, featured image, featured, image, featuredimage
Requires at least: 6.6
Requires PHP: 5.6
Tested up to: 6.7.0
Stable tag: 3.2.0
Tested up to: 6.7.2
Stable tag: 3.3.0
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -13,13 +13,16 @@ Choose if the featured image should be displayed in the single post/page view or
== Description ==
= Important notice =
If your theme does a customized call to load the featured image (like the Twenty Seventeen theme), this plugin might not work! Use `get_the_post_thumbnail()`, `wp_get_attachment_image()` or the [Post Featured Image](https://wordpress.org/support/article/post-featured-image-block/) block to be sure it will work.
By default, the plugin also only hides the featured image, if it is loaded within the loop. See the FAQ on how to use the plugin if you theme loads the featured image outside the loop.
By default, the plugin also only hides the featured image, if it is loaded within the loop. See the FAQ on how to use the plugin if your theme loads the featured image outside the loop.

= Description =
This plugin lets you choose for each post or page, if the featured image should be shown in the single view. This can get handy, if you use the featured image to show a thumbnail on the archives or front page but you don't want the featured image to be shown on every posts view itself.
This plugin lets you choose for each post or page, if the featured image should be shown in the single view. This can get handy, if you use the featured image to show a thumbnail on the archives or front page, but you don't want the featured image to be shown on every post's view itself.

The plugin adds a simple checkbox to the featured image panel (or meta box if you are using the classic editor), that lets you choose, if the featured image will be shown in the singular view or not.

= WooCommerce =
The plugin also supports WooCommerce. If the featured image is hidden for a product, it will be shown as thumbnail in the cart, checkout view, products lists etc. but not in the single product view. If a gallery is available, the gallery images will be shown as usual, but without the featured image.

== Frequently asked questions ==
= The plugin doesn't work with my theme. What can I do? =
Typically there are two possibilities why the plugin is not compatible with your theme:
Expand Down Expand Up @@ -88,6 +91,18 @@ function cybocfi_limit_to_posts_and_pages( $enabled, $post_type ) {
add_filter( 'cybocfi_enabled_for_post_type', 'cybocfi_limit_to_posts_and_pages', 10, 2 );
`

= WooCommerce: Can I remove the whitespace of the hidden image? =
Yes. The plugin already adds some CSS which does this for standard themes / block configurations. If it doesn't work for you, you can customize the CSS with the following filter:

`
function cybocfi_woocommerce_styles( $css ) {
return '.wp-block-woocommerce-product-image-gallery {display: none;}';
}
add_filter( 'cybocfi_woocommerce_style_overrides', 'cybocfi_woocommerce_styles' );
`

The styles are only loaded on single product view pages where the featured image is hidden.

= Is it possible to get the plugin in my language? =
Absolutely. You're invited to [contribute a translation](https://translate.wordpress.org/projects/wp-plugins/conditionally-display-featured-image-on-singular-pages/) in your language. Please keep in mind, that the translation needs to be reviewed by the community, so it will take a little while until it gets accepted.

Expand Down Expand Up @@ -143,6 +158,11 @@ In case you've only used one argument (`$post_type`), you must not only adapt th

== Changelog ==

= 3.3.0 =
* Added support for WooCommerce
* Fixed `bottom margin` deprecation notice
* Updated dependencies

= 3.2.0 =
* Requires at least WordPress 6.6
* Compatibility up to WordPress 6.7
Expand Down
3 changes: 2 additions & 1 deletion addon-php.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ xdebug.mode = develop,debug
xdebug.discover_client_host = 1
; enable if your IDE does not set the needed params
; https://xdebug.org/docs/step_debug#activate_debugger
xdebug.start_with_request = 1
xdebug.start_with_request = 1
xdebug.client_host = host.docker.internal
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-hooks'), 'version' => 'faf5f8aaff422f18f647');
<?php return array('dependencies' => array('react-jsx-runtime', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-hooks'), 'version' => 'd3f095f78ad392acd25e');
2 changes: 1 addition & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions conditional-featured-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Conditionally display featured image on singular pages and posts
Plugin URI: https://github.com/cyrillbolliger/conditional-featured-image
Description: Choose if the featured image should be displayed in the single post/page view or not. This plugin doesn't affect the archives view.
Version: 3.2.0
Version: 3.3.0
Author: Cyrill Bolliger
Text Domain: conditionally-display-featured-image-on-singular-pages
License: GPLv2
Expand All @@ -24,7 +24,7 @@
/**
* Version number (don't forget to change it also in the header)
*/
define( 'CYBOCFI_VERSION', '3.2.0' );
define( 'CYBOCFI_VERSION', '3.3.0' );

/**
* Plugin prefix
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
- "wp:/var/www/html:rw"
- ".:/var/www/html/wp-content/plugins/conditional-featured-image:ro"
- "./addon-php.ini:/usr/local/etc/php/conf.d/addon-php.ini:ro"
extra_hosts:
- "host.docker.internal:host-gateway"

mysql:
image: "mariadb:latest"
Expand Down
96 changes: 96 additions & 0 deletions include/class-conditional-featured-image-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public function run() {
* @since 2.6.0
*/
add_filter( 'twentynineteen_can_show_post_thumbnail', array( &$this, 'twentynineteen' ) );

/**
* Support for WooCommerce
*
* @since 3.3.0
*/
if ( class_exists( 'woocommerce' ) ) {
add_filter(
'woocommerce_single_product_image_thumbnail_html',
array( &$this, 'filter_woocommerce_single_product_image_thumbnail_html' ),
10,
2
);
add_action( 'wp_enqueue_scripts', array( &$this, 'add_woocommerce_css' ), 20, 2 );
}
}

/**
Expand Down Expand Up @@ -355,5 +370,86 @@ public function hide_featured_image_in_the_loop( $value, $object_id, $meta_key )

return $value;
}

/**
* Get the ids of the gallery images of the current woocommerce product
*
* @return array
*
* @since 3.3.0
*/
private function get_woocommerce_gallery_ids() {
$product = wc_get_product( get_the_ID() );

return $product && method_exists( $product, 'get_gallery_image_ids' )
? $product->get_gallery_image_ids()
: array();
}

/**
* Add css to hide the image block column if the featured image is hidden
*
* @since 3.3.0
*/
public function add_woocommerce_css() {
if ( ! $this->is_image_marked_hidden( get_the_ID() ) ) {
return;
}

// hide image space if there is no gallery
if ( empty( $this->get_woocommerce_gallery_ids() ) ) {
$styles = <<<EOL
/* hide image container */
.woocommerce-product-gallery {display: none;}
/* use the full width for the summary */
.woocommerce #content div.product div.summary,
.woocommerce div.product div.summary,
.woocommerce-page #content div.product div.summary,
.woocommerce-page div.product div.summary {float: unset; width: unset;}
EOL;
if ( wp_is_block_theme() ) {
// keep the classic theme styles because users can
// opt out of the block system (for woocommerce blocks)
$styles .= <<<EOL
/* hide image block */
.wp-block-woocommerce-product-image-gallery {display: none;}
/* hide column for image block */
.wp-block-column:has(.wp-block-woocommerce-product-image-gallery) {display: none;}
EOL;
}
/**
* Filter styles to hide space usually reserved for the image
*
* @param string $styles css rules
*
* @since 3.3.0
*/
$styles = apply_filters( 'cybocfi_woocommerce_style_overrides', $styles );

wp_add_inline_style( 'woocommerce-layout', $styles );
}
}

/**
* Filter WooCommerce's single product image markup.
*
* Removes the markup for the product image if it is the featured image,
* and it was marked hidden. Leaves the markup untouched otherwise.
*
* @param string $html
* @param mixed $post_thumbnail_id
*
* @return string Returns an empty string if the given thumbnail is the
* featured image, and it was hidden or the original HTML
* otherwise.
*
* @since 3.3.0
*/
public function filter_woocommerce_single_product_image_thumbnail_html( $html, $post_thumbnail_id ) {
return $this->is_image_marked_hidden( get_the_ID() ) // featured image hidden
&& ! in_array( $post_thumbnail_id, $this->get_woocommerce_gallery_ids(), true ) // not a gallery image
? ''
: $html;
}
}
}
Loading

0 comments on commit 73334c9

Please sign in to comment.