Skip to content

Commit

Permalink
Merge pull request #1904 from Automattic/trunk
Browse files Browse the repository at this point in the history
Alpha release Oct 14
  • Loading branch information
dkoo authored Oct 14, 2024
2 parents 5f0a9cd + 4876de6 commit c404843
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 71 deletions.
2 changes: 1 addition & 1 deletion includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static function render_variation_selection() {
$products = array_keys( self::$products );
foreach ( $products as $product_id ) {
$product = wc_get_product( $product_id );
if ( ! $product->is_type( 'variable' ) ) {
if ( ! $product || ! $product->is_type( 'variable' ) ) {
continue;
}
?>
Expand Down
25 changes: 25 additions & 0 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public static function enqueue_block_editor_assets() {
'custom_taxonomies' => self::get_custom_taxonomies(),
'can_use_name_your_price' => self::can_use_name_your_price(),
'tier_amounts_template' => self::get_formatted_amount(),
'can_use_video_playlist' => self::can_use_video_playlist_block(),
];

if ( class_exists( 'WP_REST_Newspack_Author_List_Controller' ) ) {
Expand Down Expand Up @@ -1643,5 +1644,29 @@ public static function get_image_caption( $attachment_id = null, $include_captio

return $combined_caption;
}

/**
* Check if the current site can use the Video Playlist block.
* If the block doesn't already exist in site content, it won't be registered.
*/
public static function can_use_video_playlist_block() {
// Check if the block exists in any content on the site.
$existing_blocks = new WP_Query(
[
'fields' => 'ids',
'post_type' => 'any',
'post_status' => 'publish',
's' => 'newspack-blocks/youtube-video-playlist',
'posts_per_page' => 1,
]
);

// Don't register the block if it's not already on the site.
if ( 0 < $existing_blocks->found_posts ) {
return true;
}

return false;
}
}
Newspack_Blocks::init();
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"@rushstack/eslint-patch": "^1.10.4",
"@testing-library/dom": "^10.4.0",
"@testing-library/user-event": "^14.5.2",
"@types/lodash": "^4.17.7",
"@wordpress/browserslist-config": "^6.7.0",
"@types/lodash": "^4.17.10",
"@wordpress/browserslist-config": "^6.9.0",
"eslint": "^8.57.0",
"fetch-mock-jest": "^1.5.1",
"html-entities": "^2.5.2",
Expand Down
12 changes: 2 additions & 10 deletions src/blocks/homepage-articles/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { addQueryArgs } from '@wordpress/url';
* Internal dependencies
*/
import metadata from './block.json';
import { getBlockQueries, sanitizePostList } from './utils';
import { getBlockQueries, sanitizePostList, recursivelyGetBlocks } from './utils';

const { name } = metadata;
export const STORE_NAMESPACE = `newspack-blocks/${ name }`;
Expand Down Expand Up @@ -138,15 +138,7 @@ const createFetchPostsSaga = blockNames => {

yield put( { type: 'DISABLE_UI' } );

// Ensure innerBlocks are populated for widget area blocks.
// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216.
const blocks = getBlocks().map( block => {
const innerBlocks = select( 'core/block-editor' ).getBlocks( block.clientId );
return {
...block,
innerBlocks,
};
} );
const blocks = recursivelyGetBlocks( getBlocks );

const blockQueries = getBlockQueries( blocks, blockNames );

Expand Down
20 changes: 20 additions & 0 deletions src/blocks/homepage-articles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,23 @@ export const postsBlockDispatch = (
triggerReflow: isEditorBlock ? dispatch( STORE_NAMESPACE ).reflow : () => undefined,
};
};

// Ensure innerBlocks are populated for some blocks (e.g. `widget-area` and `post-content`).
// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216.
// See https://github.com/Automattic/wp-calypso/issues/91839.
export const recursivelyGetBlocks = (
getBlocks: ( clientId?: string ) => Block[],
blocks: Block[] = getBlocks()
) => {
return blocks.map( ( block ) => {
let innerBlocks =
block.innerBlocks.length === 0
? getBlocks( block.clientId )
: block.innerBlocks;
innerBlocks = recursivelyGetBlocks( getBlocks, innerBlocks );
return {
...block,
innerBlocks,
};
});
};
80 changes: 33 additions & 47 deletions src/blocks/video-playlist/edit.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { Component, Fragment } from '@wordpress/element';
import { Placeholder, Spinner, PanelBody, RangeControl } from '@wordpress/components';
import { Notice, Placeholder, Spinner, PanelBody } from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor';
import { addQueryArgs } from '@wordpress/url';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies.
*/
import AutocompleteTokenField from '../../components/autocomplete-tokenfield';

class Edit extends Component {
constructor( props ) {
super( props );
Expand Down Expand Up @@ -175,55 +170,46 @@ class Edit extends Component {
} );
};

/**
* Hide the overlay so users can play the videos.
*/
hideOverlay() {
this.setState( { interactive: true } );
}

/**
* Render.
*/
render() {
const { attributes, setAttributes } = this.props;
const { categories, videosToShow } = attributes;
const { embed, isLoading, interactive } = this.state;
const { embed, isLoading } = this.state;

const Warning = () => (
<>
<h2>{ __( 'The YouTube Video Playlist block is deprecated', 'newspack-plugin' ) }</h2>
<p dangerouslySetInnerHTML={ {
__html: sprintf(
// translators: %1$s is the link to Google's help doc on creating YouTube playlists. %2$s is the link to the help doc on embedding playlists.
__( 'Consider using <a href="%1$s">YouTube Playlists</a> instead, which can be <a href="%2$s">embedded</a> into post or page content.', 'newspack-blocks' ),
'https://support.google.com/youtube/answer/57792',
'https://support.google.com/youtube/answer/171780'
),
} } />
</>
)

return (
<Fragment>
{ ( isLoading || '' === embed ) && this.renderPlaceholder() }
{ ! ( isLoading || '' === embed ) && (
<div className="wpbnbvp-preview">
<div dangerouslySetInnerHTML={ { __html: embed } } />
{ ! interactive && (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div className="wpbnbvp__overlay" onMouseUp={ () => this.hideOverlay() } />
) }
</div>
) }
<div>
{ ( isLoading || '' === embed ) && this.renderPlaceholder() }
{ ! ( isLoading || '' === embed ) && (
<div className="wpbnbvp-preview">
<div dangerouslySetInnerHTML={ { __html: embed } } />
</div>
) }
{ ! isLoading && (
<div className="wpbnbvp__overlay">
<Warning />
</div>
) }
</div>
<InspectorControls>
<PanelBody title={ __( 'Settings', 'newspack-blocks' ) } initialOpen={ true }>
<Fragment>
<RangeControl
className="videosToShow"
label={ __( 'Videos', 'newspack-blocks' ) }
help={ __( 'The maximum number of videos to pull from posts.', 'newspack-blocks' ) }
value={ videosToShow }
onChange={ _videosToShow => setAttributes( { videosToShow: _videosToShow } ) }
min={ 1 }
max={ 30 }
required
/>
<AutocompleteTokenField
key="categories"
tokens={ categories || [] }
onChange={ _categories => setAttributes( { categories: _categories } ) }
fetchSuggestions={ this.fetchCategorySuggestions }
fetchSavedInfo={ this.fetchSavedCategories }
label={ __( 'Categories', 'newspack-blocks' ) }
/>
</Fragment>
<Notice status="warning" isDismissible={ false }>
<Warning />
</Notice>
</PanelBody>
</InspectorControls>
</Fragment>
Expand Down
4 changes: 3 additions & 1 deletion src/blocks/video-playlist/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
import { registerBlockType } from '@wordpress/blocks';
import { name, settings } from '.';

registerBlockType( `newspack-blocks/${ name }`, settings );
if ( window.newspack_blocks_data?.can_use_video_playlist ) {
registerBlockType( `newspack-blocks/${ name }`, settings );
}
11 changes: 10 additions & 1 deletion src/blocks/video-playlist/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
* Prevents interaction with the player until the block is focused.
*/
.wpbnbvp__overlay {
align-content: center;
background-color: rgba(255, 255, 255, 0.9);
display: flex;
flex-direction: column;
justify-content: center;
left: 0;
height: 100%;
position: absolute;
inset: 0;
text-align: center;
top: 0;
width: 100%;
}

.wpbnbvp-preview {
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/video-playlist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import edit from './edit';
import './editor.scss';

export const name = 'youtube-video-playlist';
export const title = __( 'YouTube Video Playlist', 'newspack-blocks' );
export const title = __( 'YouTube Video Playlist (DEPRECATED)', 'newspack-blocks' );

export const icon = (
<SVG xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/video-playlist/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function newspack_blocks_render_block_video_playlist( $attributes ) {
* Registers the `newspack-blocks/donate` block on server.
*/
function newspack_blocks_register_video_playlist() {
if ( ! Newspack_Blocks::can_use_video_playlist_block() ) {
return;
}

register_block_type(
'newspack-blocks/youtube-video-playlist',
array(
Expand Down

0 comments on commit c404843

Please sign in to comment.