Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplication - use same store form Carousel and Homepage Posts blocks; preview post in block previews #725

Merged
merged 7 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 7 additions & 162 deletions includes/class-newspack-blocks-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,130 +9,6 @@
* `Newspack_Blocks_API` is a wrapper for `register_rest_fields()`
*/
class Newspack_Blocks_API {

/**
* Register Newspack REST fields.
*/
public static function register_rest_fields() {
register_rest_field(
[ 'post', 'page' ],
'newspack_featured_image_src',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_get_image_src' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'array',
],
]
);

register_rest_field(
[ 'post', 'page' ],
'newspack_featured_image_caption',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_get_image_caption' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'string',
],
]
);

/* Add author info source */
register_rest_field(
'post',
'newspack_author_info',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_get_author_info' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'array',
],
]
);

/* Add first category source */
register_rest_field(
'post',
'newspack_category_info',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_get_primary_category' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'string',
],
]
);

/* Add list of categories for CSS classes */
register_rest_field(
'post',
'newspack_article_classes',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_get_cat_tag_classes' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'string',
],
]
);

/* Sponsors */
register_rest_field(
'post',
'newspack_post_sponsors',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_sponsor_info' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'array',
],
]
);

/* Post format */
register_rest_field(
'post',
'newspack_post_format',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_post_format' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'string',
],
]
);

/* Has custom excerpt */
register_rest_field(
'post',
'newspack_has_custom_excerpt',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_has_custom_excerpt' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'boolean',
],
]
);
}

/**
* Get thumbnail featured image source for the rest field.
*
Expand Down Expand Up @@ -438,11 +314,13 @@ public static function posts_endpoint( $request ) {
$args['post_type'] = $params['post_type'];
}

$block_attributes = [
'showExcerpt' => $params['show_excerpt'],
'excerptLength' => $params['excerpt_length'],
];
Newspack_Blocks::filter_excerpt_length( $block_attributes );
if ( isset( $params['show_excerpt'], $params['excerpt_length'] ) ) {
$block_attributes = [
'showExcerpt' => $params['show_excerpt'],
'excerptLength' => $params['excerpt_length'],
];
Newspack_Blocks::filter_excerpt_length( $block_attributes );
}

$query = new WP_Query();
$query_result = $query->query( $args );
Expand Down Expand Up @@ -550,39 +428,6 @@ public static function add_post_title_wildcard_search( $where, $query ) {
$where .= ' AND post_title LIKE "%' . $search . '%" ';
return $where;
}

/**
* Adds meta query support to API rest endpoint.
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
* @return array $args Filtered request parameters.
*/
public static function post_meta_request_params( $args, $request ) {
$params = $request->get_params();

if (
isset( $params['meta_key'], $params['meta_value_num'], $params['meta_compare'] ) &&
'_thumbnail_id' === $params['meta_key'] &&
'0' === $params['meta_value_num'] &&
'>' === $params['meta_compare']
) {
// phpcs:disable WordPress.DB.SlowDBQuery
$args['meta_key'] = $params['meta_key'];
$args['meta_value_num'] = $params['meta_value_num'];
$args['meta_compare'] = $params['meta_compare'];
// phpcs:enable WordPress.DB.SlowDBQuery
}

if ( $request->get_param( 'suppress_password_protected_posts' ) ) {
$args['has_password'] = false;
}

return $args;
}

}

add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_rest_fields' ) );
add_action( 'rest_api_init', array( 'Newspack_Blocks_API', 'register_video_playlist_endpoint' ) );
add_filter( 'rest_post_query', array( 'Newspack_Blocks_API', 'post_meta_request_params' ), 10, 2 );
12 changes: 7 additions & 5 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static function enqueue_block_editor_assets() {
'patterns' => self::get_patterns_for_post_type( get_post_type() ),
'posts_rest_url' => rest_url( 'newspack-blocks/v1/newspack-blocks-posts' ),
'specific_posts_rest_url' => rest_url( 'newspack-blocks/v1/newspack-blocks-specific-posts' ),
'assets_path' => plugins_url( '/src/assets', NEWSPACK_BLOCKS__PLUGIN_FILE ),
'post_subtitle' => get_theme_support( 'post-subtitle' ),
]
);

Expand Down Expand Up @@ -317,20 +319,20 @@ public static function add_image_sizes() {
* Builds and returns query args based on block attributes.
*
* @param array $attributes An array of block attributes.
* @param array $block_name Name of the block requesting the query.
*
* @return array
*/
public static function build_articles_query( $attributes ) {
public static function build_articles_query( $attributes, $block_name ) {
global $newspack_blocks_post_id;
if ( ! $newspack_blocks_post_id ) {
$newspack_blocks_post_id = array();
}

// Get all blocks and gather specificPosts ids of all Homepage Articles blocks.
// Get all blocks and gather specificPosts ids of all eligible blocks.
global $newspack_blocks_all_specific_posts_ids;
if ( ! is_array( $newspack_blocks_all_specific_posts_ids ) ) {
$blocks = parse_blocks( get_the_content() );
$block_name = apply_filters( 'newspack_blocks_block_name', 'newspack-blocks/homepage-articles' );
$newspack_blocks_all_specific_posts_ids = array_reduce(
$blocks,
function ( $acc, $block ) use ( $block_name ) {
Expand All @@ -357,7 +359,7 @@ function ( $acc, $block ) use ( $block_name ) {
$tag_exclusions = isset( $attributes['tagExclusions'] ) ? $attributes['tagExclusions'] : array();
$specific_posts = isset( $attributes['specificPosts'] ) ? $attributes['specificPosts'] : array();
$posts_to_show = intval( $attributes['postsToShow'] );
$specific_mode = intval( $attributes['specificMode'] );
$specific_mode = isset( $attributes['specificMode'] ) ? intval( $attributes['specificMode'] ) : false;
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
Expand Down Expand Up @@ -674,7 +676,7 @@ public static function remove_wc_memberships_excerpt_limit() {
*/
public static function filter_excerpt_length( $attributes ) {
// If showing excerpt, filter the length using the block attribute.
if ( $attributes['showExcerpt'] ) {
if ( isset( $attributes['excerptLength'] ) && $attributes['showExcerpt'] ) {
self::$newspack_blocks_excerpt_length_closure = add_filter(
'excerpt_length',
function() use ( $attributes ) {
Expand Down
17 changes: 0 additions & 17 deletions newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,3 @@ function newspack_blocks_plugin_textdomain() {
load_plugin_textdomain( 'newspack-blocks', false, dirname( plugin_basename( NEWSPACK_BLOCKS__PLUGIN_FILE ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'newspack_blocks_plugin_textdomain' );


/**
* Add global variable for theme supports detection.
*
* @action enqueue_block_editor_assets
*/
function newspack_blocks_post_subtitle_detection() {
wp_localize_script(
'newspack-blocks-editor',
'newspackIsPostSubtitleSupported',
array(
'post_subtitle' => get_theme_support( 'post-subtitle' ),
)
);
}
add_action( 'enqueue_block_editor_assets', 'newspack_blocks_post_subtitle_detection' );
Binary file added src/assets/newspack-1024x536.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/newspack-600x800.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/newspack-800x600.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/newspack-800x600.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/newspack-800x800.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 21 additions & 25 deletions src/blocks/carousel/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { isEqual, isUndefined, pickBy } from 'lodash';
import { isEqual } from 'lodash';
import classnames from 'classnames';

/**
Expand All @@ -21,7 +21,7 @@ import {
Spinner,
ToggleControl,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { decodeEntities } from '@wordpress/html-entities';

Expand All @@ -36,6 +36,8 @@ import {
formatSponsorLogos,
formatSponsorByline,
} from '../../shared/js/utils';
// Use same posts store as Homepage Posts block.
import { postsBlockSelector, postsBlockDispatch, shouldReflow } from '../homepage-articles/utils';

class Edit extends Component {
constructor( props ) {
Expand All @@ -51,9 +53,14 @@ class Edit extends Component {

componentDidMount() {
this.initializeSwiper( 0 );
this.props.triggerReflow();
}

componentDidUpdate( prevProps ) {
if ( shouldReflow( prevProps, this.props ) ) {
this.props.triggerReflow();
}

const { attributes, latestPosts } = this.props;

if (
Expand All @@ -76,6 +83,10 @@ class Edit extends Component {
}
}

componentWillUnmount() {
this.props.triggerReflow();
}

initializeSwiper( initialSlide ) {
const { latestPosts } = this.props;

Expand All @@ -102,7 +113,7 @@ class Edit extends Component {
}

render() {
const { attributes, className, setAttributes, latestPosts } = this.props;
const { attributes, className, setAttributes, latestPosts, isUIDisabled } = this.props;
const {
authors,
autoplay,
Expand All @@ -119,7 +130,10 @@ class Edit extends Component {
className,
'wp-block-newspack-blocks-carousel', // Default to make styles work for third-party consumers.
'swiper-container',
autoplay && 'wp-block-newspack-blocks-carousel__autoplay-playing'
{
'wp-block-newspack-blocks-carousel__autoplay-playing': autoplay,
'newspack-block--disabled': isUIDisabled,
}
);
const dateFormat = __experimentalGetSettings().formats.date;
return (
Expand Down Expand Up @@ -290,24 +304,6 @@ class Edit extends Component {
}
}

export default compose( [
withSelect( ( select, props ) => {
const { postsToShow, authors, categories, tags } = props.attributes;
const { getEntityRecords } = select( 'core' );
const latestPostsQuery = pickBy(
{
per_page: postsToShow,
categories,
author: authors,
tags,
meta_key: '_thumbnail_id',
meta_value_num: 0,
meta_compare: '>',
},
value => ! isUndefined( value )
);
return {
latestPosts: getEntityRecords( 'postType', 'post', latestPostsQuery ),
};
} ),
] )( Edit );
export default compose( [ withSelect( postsBlockSelector ), withDispatch( postsBlockDispatch ) ] )(
Edit
);
Loading