Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.x' into 2.x-timber-2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Mar 4, 2024
2 parents d9504b0 + b3232c5 commit fdc34b3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/CHANGELOG.md export-ignore
/README.md export-ignore
/bin export-ignore
/composer.json export-ignore
/docs export-ignore
/phpunit.xml.dist export-ignore
/release-please-config.json export-ignore
/tests export-ignore
21 changes: 21 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Release

on:
push:
branches:
- 1.x
- 1.x-release-workflow

permissions:
contents: write
pull-requests: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v4
with:
target-branch: ${{ github.ref_name }}
config-file: release-please-config.json
manifest-file: .release-please-manifest.json
3 changes: 3 additions & 0 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
".": "1.0.0"
}
64 changes: 43 additions & 21 deletions lib/Timmy.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public function after_setup_theme() {
// Filters the metadata for an image.
add_filter( 'wp_get_attachment_metadata', array( $this, 'filter_attachment_metadata' ), 10, 2 );

// Filters the generated attachment meta data.
add_filter( 'wp_generate_attachment_metadata', array( $this, 'filter_wp_generate_attachment_metadata' ), 10, 2 );
// Hook into generating attachment meta data filter.
add_filter( 'wp_generate_attachment_metadata', array( $this, 'delete_generated_image_sizes' ), 5, 2 );
add_filter( 'wp_generate_attachment_metadata', array( $this, 'filter_wp_generate_attachment_metadata' ), 30, 2 );

// Filters the attachment data prepared for JavaScript.
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'filter_wp_prepare_attachment_for_js' ), 10, 3 );
Expand Down Expand Up @@ -324,22 +325,22 @@ public function filter_attachment_metadata( $meta_data, $attachment_id ) {
}

/**
* Hooks into the filter that generates additional image sizes to generate all additional image
* sizes with TimberImageHelper.
* Deletes all existing image sizes for that file.
*
* This function will run when you upload an image. It will also run if you run Regenerate
* Thumbnails, so all additional images sizes registered with Timber will be first deleted and
* then regenerated through Timmy.
* This function will run when you upload an image. It will also run if you
* run plugins like Regenerate Thumbnails. All additional images sizes
* registered with Timber will be first deleted.
*
* @param array $meta_data Meta data for an attachment.
* @param int $attachment_id Attachment ID.
* Because Timber also creates image sizes when they’re needed, we can
* safely do this.
*
* @return array $meta_data
* By running with a priority of 5, we make sure that generated image files
* are not present before other plugins run their filters.
*/
public function filter_wp_generate_attachment_metadata( $meta_data, $attachment_id ) {
public function delete_generated_image_sizes( $meta_data, $attachment_id ) {
/**
* Don’t automatically generate image sizes on upload for SVG and GIF images.
* GIF images will still be resized when requested on the fly.
* Don’t automatically generate image sizes on upload for SVG and GIF
* images. GIF images will still be resized when requested on the fly.
*/
if ( self::ignore_attachment( $attachment_id ) ) {
return $meta_data;
Expand All @@ -348,16 +349,37 @@ public function filter_wp_generate_attachment_metadata( $meta_data, $attachment_
// Timber needs the file src as a URL.
$file_src = Helper::get_original_attachment_url( $attachment_id );

$attachment = get_post( $attachment_id );
Timber\ImageHelper::delete_generated_files( $file_src );

return $meta_data;
}

/**
* Hooks into the filter that generates additional image sizes to generate
* all additional image sizes with TimberImageHelper.
*
* This function will run when you upload an image. It will also run if you
* run a plugin like Regenerate Thumbnails. All additional images sizes
* registered with Timber will be regenerated through Timmy.
*
* By running this with a priority of 30, we make sure that image sizes are
* generated after other plugins have run their filters.
*
* @param array $meta_data Meta data for an attachment.
* @param int $attachment_id Attachment ID.
*
* @return array $meta_data
*/
public function filter_wp_generate_attachment_metadata( $meta_data, $attachment_id ) {
/**
* Delete all existing image sizes for that file.
*
* This way, when Regenerate Thumbnails will be used, all non-registered image sizes will be
* deleted as well. Because Timber creates image sizes when they’re needed, we can safely do
* this.
* Don’t automatically generate image sizes on upload for SVG and GIF
* images. GIF images will still be resized when requested on the fly.
*/
Timber\ImageHelper::delete_generated_files( $file_src );
if ( self::ignore_attachment( $attachment_id ) ) {
return $meta_data;
}

$attachment = get_post( $attachment_id );

$meta_data['sizes'] = $this->generate_image_sizes( $attachment );

Expand Down Expand Up @@ -448,7 +470,7 @@ public function filter_image_downsize( $return, $attachment_id, $size ) {

// When media files are requested through an AJAX call, an action will be present in $_POST.
$action = is_admin() && isset( $_POST['action'] )
? filter_var( $_POST['action'], FILTER_SANITIZE_STRING )
? htmlspecialchars( $_POST['action'] )
: false;

$attachment = get_post( $attachment_id );
Expand Down
13 changes: 13 additions & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"packages": {
".": {
"changelog-path": "CHANGELOG.md",
"release-type": "php",
"bump-minor-pre-major": false,
"bump-patch-for-minor-pre-major": false,
"draft": false,
"prerelease": false
}
},
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json"
}

0 comments on commit fdc34b3

Please sign in to comment.