Skip to content

Commit

Permalink
Gallery: Add support for ID-less shortcode
Browse files Browse the repository at this point in the history
Per the WP Codex, the [gallery] shortcode should be supported when no
IDs are provided:

> The default behavior, if no ID is specified, is to display images
> attached to the current post.
-- https://codex.wordpress.org/Gallery_Shortcode
  • Loading branch information
mcsf committed Apr 21, 2018
1 parent c27d600 commit 145eccc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
57 changes: 52 additions & 5 deletions blocks/library/gallery/block.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External Dependencies
*/
import { filter, pick } from 'lodash';
import { filter, noop, pick } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -14,11 +14,14 @@ import {
DropZone,
FormFileUpload,
PanelBody,
Placeholder,
RangeControl,
SelectControl,
Spinner,
ToggleControl,
Toolbar,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -144,19 +147,39 @@ class GalleryBlock extends Component {
);
}

componentWillReceiveProps( nextProps ) {
componentWillReceiveProps( { isSelected, attachedImages } ) {
// Deselect images when deselecting the block
if ( ! nextProps.isSelected && this.props.isSelected ) {
if ( ! isSelected && this.props.isSelected ) {
this.setState( {
selectedImage: null,
captionSelected: false,
} );
}

// Process REST API data and commit to attributes.
if ( attachedImages !== this.props.attachedImages &&
attachedImages && ! this.props.url
) {
this.props.setAttributes( {
images: attachedImages.map( ( image ) => ( {
url: image.source_url,
alt: image.alt_text,
} ) ),
useAttachedImages: false,
} );
}
}

render() {
const { attributes, isSelected, className } = this.props;
const { images, columns = defaultColumnsNumber( attributes ), align, imageCrop, linkTo } = attributes;
const {
align,
columns = defaultColumnsNumber( attributes ),
imageCrop,
images,
linkTo,
useAttachedImages,
} = attributes;

const dropZone = (
<DropZone
Expand Down Expand Up @@ -192,6 +215,19 @@ class GalleryBlock extends Component {
</BlockControls>
);

if ( useAttachedImages && images.length === 0 ) {
return [
<Placeholder
key="placeholder"
instructions={ __( 'Loading gallery images…' ) }
icon="format-gallery"
label={ __( 'Gallery' ) }
className={ className }>
<Spinner />
</Placeholder>,
];
}

if ( images.length === 0 ) {
return (
<Fragment>
Expand Down Expand Up @@ -269,4 +305,15 @@ class GalleryBlock extends Component {
}
}

export default GalleryBlock;
export default withSelect( ( select, { attributes } ) => {
if ( ! attributes.useAttachedImages ) {
return;
}

const { getPostMedia = noop } = select( 'core' ) || noop;
const { getCurrentPostId = noop } = select( 'core/editor' ) || noop;
const postId = getCurrentPostId();
return postId && {
attachedImages: getPostMedia( postId ),
};
} )( GalleryBlock );
11 changes: 11 additions & 0 deletions blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ const blockAttributes = {
},
},
},
// To be set only by the shortcode transform to signal the intent to
// converting `[shortcode]` to a Gallery block with images filled in as
// attributes.
useAttachedImages: {
type: 'boolean',
default: false,
},
columns: {
type: 'number',
},
Expand Down Expand Up @@ -111,6 +118,10 @@ export const settings = {
} ) );
},
},
useAttachedImages: {
type: 'boolean',
shortcode: ( { named: { ids } } ) => ! ids,
},
columns: {
type: 'number',
shortcode: ( { named: { columns = '3' } } ) => {
Expand Down
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__gallery.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"caption": []
}
],
"useAttachedImages": false,
"imageCrop": true,
"linkTo": "none"
},
Expand Down
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__gallery__columns.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"caption": []
}
],
"useAttachedImages": false,
"columns": 1,
"imageCrop": true,
"linkTo": "none"
Expand Down

0 comments on commit 145eccc

Please sign in to comment.