Skip to content

Commit daec8a1

Browse files
emeaguiarravichdev
authored andcommitted
Merge branch 'develop' into feature/12-gallery-options
2 parents d22493d + 58b6f51 commit daec8a1

File tree

54 files changed

+2362
-144
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2362
-144
lines changed

plugin/assets/css/src/block-editor.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
width: 100%;
5858
}
5959

60+
.block-editor-block-list__block .material-design-block-handpicked-posts__types__list .components-radio-control__option {
61+
display: inline-block;
62+
63+
& label {
64+
margin-right: 8px;
65+
}
66+
}
67+
6068
.mdc-image-list--with-text-protection .mdc-image-list__supporting {
6169
align-items: start;
6270
height: auto;

plugin/assets/src/block-editor/blocks/common-posts-list/components/single-post.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* External dependencies
19+
*/
20+
import { get } from 'lodash';
21+
1722
/**
1823
* WordPress dependencies
1924
*/
@@ -36,8 +41,12 @@ import HorizontalCardLayout from './horizontal-card-layout';
3641
* @return {Function} A functional component.
3742
*/
3843
const SinglePost = ( { post, style, attributes } ) => {
39-
const titleTrimmed = post.title.rendered.trim();
40-
let excerpt = post.excerpt.rendered;
44+
const titleTrimmed = get( post, [ 'title', 'rendered' ], '' ).trim();
45+
let excerpt = get( post, [ 'excerpt', 'rendered' ], '' );
46+
47+
if ( ! excerpt ) {
48+
excerpt = get( post, [ 'content', 'rendered' ], '' );
49+
}
4150

4251
const excerptElement = document.createElement( 'div' );
4352
excerptElement.innerHTML = excerpt;
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* External dependencies
19+
*/
20+
import { get, isUndefined, pickBy } from 'lodash';
21+
22+
/**
23+
* WordPress dependencies
24+
*/
25+
import { createHigherOrderComponent } from '@wordpress/compose';
26+
import { withSelect } from '@wordpress/data';
27+
import { useEffect, useState } from '@wordpress/element';
28+
29+
/**
30+
* Internal dependencies
31+
*/
32+
import './style.css';
33+
import NoPosts from './components/no-posts';
34+
import PostsList from './components/posts-list';
35+
import { getPosts } from '../../utils/api';
36+
37+
/**
38+
* Edit component.
39+
*
40+
* @param {Object} props - Component props.
41+
*
42+
* @return {Function} A functional component.
43+
*/
44+
const Edit = props => {
45+
const { postsToDisplay } = props;
46+
const hasPosts = Array.isArray( postsToDisplay ) && postsToDisplay.length;
47+
48+
if ( ! hasPosts ) {
49+
return <NoPosts { ...props } />;
50+
}
51+
52+
return <PostsList { ...props } />;
53+
};
54+
55+
/**
56+
* @type {Function} A functional component.
57+
*/
58+
const EditWithSelect = withSelect( ( select, props ) => {
59+
const {
60+
category,
61+
postsToShow,
62+
posts,
63+
orderby,
64+
displayFeaturedImage,
65+
style,
66+
} = props.attributes;
67+
const { name } = props;
68+
let { fetchedPosts } = props;
69+
70+
const featuredImageSizeSlug = style === 'list' ? 'medium' : 'large';
71+
72+
const { getEntityRecords, getMedia } = select( 'core' );
73+
74+
let queryArgs = {
75+
categories: category,
76+
per_page: postsToShow,
77+
};
78+
79+
if ( name === 'material/hand-picked-posts' ) {
80+
queryArgs = {
81+
include: posts.map( Number ),
82+
per_page: posts.length,
83+
orderby: 'date',
84+
order: 'desc',
85+
};
86+
87+
if ( orderby ) {
88+
if ( orderby === 'title' ) {
89+
queryArgs.orderby = 'title';
90+
queryArgs.order = 'asc';
91+
} else if ( orderby === 'popularity' ) {
92+
queryArgs.orderby = 'comment_count';
93+
queryArgs.order = 'desc';
94+
}
95+
}
96+
}
97+
98+
const fetchedPostsQuery = pickBy(
99+
queryArgs,
100+
value => ! isUndefined( value )
101+
);
102+
103+
fetchedPosts = Array.isArray( fetchedPosts )
104+
? fetchedPosts
105+
: getEntityRecords( 'postType', 'post', fetchedPostsQuery );
106+
107+
return {
108+
postsToDisplay: ! Array.isArray( fetchedPosts )
109+
? fetchedPosts
110+
: fetchedPosts.map( post => {
111+
if ( displayFeaturedImage && post.featured_media ) {
112+
const image = getMedia( post.featured_media );
113+
let url = get(
114+
image,
115+
[ 'media_details', 'sizes', featuredImageSizeSlug, 'source_url' ],
116+
null
117+
);
118+
if ( ! url ) {
119+
url = get( image, 'source_url', null );
120+
}
121+
return { ...post, featuredImageSourceUrl: url };
122+
}
123+
return post;
124+
} ),
125+
};
126+
} )( Edit );
127+
128+
export default EditWithSelect;
129+
130+
const withGetPosts = createHigherOrderComponent( WrappedComponent => {
131+
return props => {
132+
const { category, posts, orderby, postType } = props.attributes;
133+
134+
const queryArgs = {
135+
categories: category,
136+
include: posts.map( Number ),
137+
per_page: posts.length,
138+
orderby: 'date',
139+
order: 'desc',
140+
};
141+
142+
if ( orderby ) {
143+
if ( orderby === 'title' ) {
144+
queryArgs.orderby = 'title';
145+
queryArgs.order = 'asc';
146+
} else if ( orderby === 'popularity' ) {
147+
queryArgs.orderby = 'comment_count';
148+
queryArgs.order = 'desc';
149+
}
150+
}
151+
152+
const [ postsToDisplay, setPostsToDisplay ] = useState( [] );
153+
154+
useEffect( () => {
155+
getPosts( { selected: posts, postType, queryArgs } )
156+
.then( data => {
157+
setPostsToDisplay( data );
158+
} )
159+
.catch( e => {
160+
console.error( e );
161+
} );
162+
}, [] ); // eslint-disable-line react-hooks/exhaustive-deps
163+
164+
return <WrappedComponent { ...props } fetchedPosts={ postsToDisplay } />;
165+
};
166+
}, 'withGetPosts' );
167+
168+
export const EditWithGetPosts = withGetPosts( EditWithSelect );

plugin/assets/src/block-editor/blocks/common-posts-list/style.css

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
padding: 16px;
2020
}
2121

22-
.single-post-card .mdc-button__label {
23-
white-space: nowrap;
22+
.single-post-card {
23+
24+
& .mdc-card__action--button {
25+
text-transform: none;
26+
}
27+
28+
& .mdc-button__label {
29+
white-space: nowrap;
30+
}
2431
}
2532

2633
/* @todo: Will need refactoring. */
@@ -36,12 +43,15 @@
3643

3744
.single-post-card__secondary {
3845
padding: 0 16px 8px 16px;
46+
47+
& > p {
48+
margin: 0;
49+
}
3950
}
4051

41-
/* @todo: Will need refactoring. */
4252
.single-post-card__secondary-text-over-media,
4353
.single-post-card__secondary-text-above-media {
44-
padding-top: 1em !important;
54+
padding-top: 16px;
4555
}
4656

4757
.single-post-card__media.mdc-card__media--square {

plugin/assets/src/block-editor/blocks/hand-picked-posts/block.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
"preview": {
8181
"type": "boolean",
8282
"default": false
83+
},
84+
"postType": {
85+
"type": "string",
86+
"default": "posts-pages"
8387
}
8488
},
8589
"supports": {
@@ -88,4 +92,4 @@
8892
"full"
8993
]
9094
}
91-
}
95+
}

plugin/assets/src/block-editor/blocks/hand-picked-posts/components/posts-picker.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
/**
1818
* WordPress dependencies
1919
*/
20-
import { __ } from '@wordpress/i18n';
21-
import { Placeholder, Button } from '@wordpress/components';
20+
import { Placeholder, Button, RadioControl } from '@wordpress/components';
2221
import { useCallback } from '@wordpress/element';
22+
import { __ } from '@wordpress/i18n';
2323

2424
/**
2525
* Internal dependencies
2626
*/
2727
import PostsControl from '../../../components/posts-control';
2828
import genericAttributesSetter from '../../../utils/generic-attributes-setter';
29+
import getConfig from '../../../utils/get-config';
2930

3031
/**
3132
* Posts Picker component.
@@ -37,6 +38,7 @@ import genericAttributesSetter from '../../../utils/generic-attributes-setter';
3738
*
3839
* @return {Function} A functional component.
3940
*/
41+
4042
const PostsPicker = ( { attributes, debouncedSpeak, setAttributes } ) => {
4143
const setter = useCallback( genericAttributesSetter( setAttributes ) );
4244

@@ -47,16 +49,37 @@ const PostsPicker = ( { attributes, debouncedSpeak, setAttributes } ) => {
4749
);
4850
};
4951

52+
const boldText = {
53+
fontWeight: 'bold',
54+
textAlign: 'left',
55+
};
56+
5057
return (
5158
<Placeholder
5259
icon={ <i className="material-icons-outlined">library_books</i> }
5360
label={ __( 'Curated Post Collection', 'material-design' ) }
5461
className="material-design-block-products-grid material-design-block-handpicked-posts"
5562
>
56-
{ __( 'Display a selection of hand-picked posts.', 'material-design' ) }
63+
<div className="material-design-block-handpicked-posts__types">
64+
<p>
65+
{ __(
66+
'Display a selection of hand-picked posts.',
67+
'material-design'
68+
) }
69+
</p>
70+
71+
<p style={ boldText }>{ __( 'Content', 'material-design' ) }</p>
72+
<RadioControl
73+
selected={ attributes.postType }
74+
options={ getConfig( 'postTypes' ) }
75+
onChange={ setter( 'postType' ) }
76+
className="material-design-block-handpicked-posts__types__list"
77+
/>
78+
</div>
5779
<div className="material-design-block-handpicked-posts__selection">
5880
<PostsControl
5981
selected={ attributes.posts }
82+
postType={ attributes.postType }
6083
onChange={ setter( 'posts', ( value = [] ) => {
6184
/* istanbul ignore next */
6285
return value.map( ( { id } ) => id );

plugin/assets/src/block-editor/blocks/hand-picked-posts/edit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import PostsPicker from './components/posts-picker';
2727
import HandpickedPostBlockControls from './components/block-controls';
2828
import InspectorControls from '../common-posts-list/components/inspector-controls';
2929
import './editor.css';
30-
import EditWithSelect from '../common-posts-list/edit-with-select';
30+
import { EditWithGetPosts } from '../common-posts-list/edit';
3131
import getConfig from '../../utils/get-config';
3232

3333
/**
@@ -60,7 +60,7 @@ const Edit = props => {
6060
{ editMode ? (
6161
<PostsPicker { ...props } />
6262
) : (
63-
<EditWithSelect { ...props } />
63+
<EditWithGetPosts { ...props } />
6464
) }
6565
</>
6666
);

plugin/assets/src/block-editor/blocks/recent-posts/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Internal dependencies
1919
*/
2020
import InspectorControls from './components/inspector-controls';
21-
import EditWithSelect from '../common-posts-list/edit-with-select';
21+
import EditWithSelect from '../common-posts-list/edit';
2222

2323
/**
2424
* Recent Posts Edit component.

plugin/assets/src/block-editor/components/with-global-default/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ export const withGlobalColorDefault = createHigherOrderComponent(
148148
* @return {string} Color value.
149149
*/
150150
export const getColor = ( globalPropName, value ) =>
151-
// eslint-disable-next-line
152-
useMemo( () => {
151+
useMemo( () => {// eslint-disable-line
153152
const materialDesignDefaults = getConfig( 'defaults' );
154153

155154
if (

0 commit comments

Comments
 (0)