Skip to content

Commit 0bd76bc

Browse files
committed
Reinstate previous PHP filter for the Background image
I couldn't figure out how to enable a custom size for the background image with purely JS. This won't apply to Image blocks in a non-story editor, like for posts.
1 parent 8908fdb commit 0bd76bc

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

assets/src/stories-editor/blocks/amp-story-page/edit.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
withSelect,
3333
withDispatch,
3434
dispatch,
35-
select,
3635
} from '@wordpress/data';
3736
import { compose } from '@wordpress/compose';
3837

@@ -122,10 +121,8 @@ class PageEdit extends Component {
122121

123122
mediaType = media.type;
124123
}
125-
const { getMedia } = select( 'core' );
126-
const mediaObject = getMedia( media.id );
127-
const mediaUrl = has( mediaObject, [ 'media_details', 'sizes', MAX_IMAGE_SIZE_SLUG, 'source_url' ] ) ? mediaObject.media_details.sizes[ MAX_IMAGE_SIZE_SLUG ].source_url : media.url;
128124

125+
const mediaUrl = has( media, [ 'sizes', MAX_IMAGE_SIZE_SLUG, 'url' ] ) ? media.sizes[ MAX_IMAGE_SIZE_SLUG ].url : media.url;
129126
this.props.setAttributes( {
130127
mediaUrl,
131128
mediaId: media.id,
@@ -474,13 +471,13 @@ PageEdit.propTypes = {
474471
};
475472

476473
export default compose(
477-
withSelect( ( ownSelect, { clientId, attributes } ) => {
478-
const { getMedia } = ownSelect( 'core' );
479-
const { getBlockOrder, getBlockRootClientId } = ownSelect( 'core/block-editor' );
474+
withSelect( ( select, { clientId, attributes } ) => {
475+
const { getMedia } = select( 'core' );
476+
const { getBlockOrder, getBlockRootClientId } = select( 'core/block-editor' );
480477

481478
const isFirstPage = getBlockOrder().indexOf( clientId ) === 0;
482479
const isCallToActionAllowed = ! isFirstPage && ! getCallToActionBlock( clientId );
483-
const { getAnimatedBlocks } = ownSelect( 'amp/story' );
480+
const { getAnimatedBlocks } = select( 'amp/story' );
484481

485482
const { mediaId } = attributes;
486483

includes/class-amp-story-post-type.php

+25
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ public static function register() {
271271
add_filter( 'use_block_editor_for_post_type', array( __CLASS__, 'use_block_editor_for_story_post_type' ), PHP_INT_MAX, 2 );
272272
add_filter( 'classic_editor_enabled_editors_for_post_type', array( __CLASS__, 'filter_enabled_editors_for_story_post_type' ), PHP_INT_MAX, 2 );
273273

274+
add_filter( 'image_size_names_choose', array( __CLASS__, 'add_new_max_image_size' ) );
275+
274276
self::register_block_latest_stories();
275277

276278
register_block_type(
@@ -1523,4 +1525,27 @@ public static function change_embed_iframe_attributes( $output, $post ) {
15231525
$output
15241526
);
15251527
}
1528+
1529+
/**
1530+
* Adds a new max image size to the images sizes available.
1531+
*
1532+
* In the AMP story editor, when selecting Background Media,
1533+
* it will use this custom image size.
1534+
* But this filter won't make it available in the Image block's 'Image Size' <select> element.
1535+
*
1536+
* @param array $image_sizes {
1537+
* An associative array of image sizes.
1538+
*
1539+
* @type string $slug Image size slug, like 'medium'.
1540+
* @type string $name Image size name, like 'Medium'.
1541+
* }
1542+
* @return array $image_sizes The filtered image sizes.
1543+
*/
1544+
public static function add_new_max_image_size( $image_sizes ) {
1545+
if ( isset( $_POST['action'] ) && 'query-attachments' === $_POST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
1546+
$image_sizes[ self::MAX_IMAGE_SIZE_SLUG ] = __( 'AMP Story Max Size', 'amp' );
1547+
}
1548+
1549+
return $image_sizes;
1550+
}
15261551
}

0 commit comments

Comments
 (0)