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

Backport fixes to WordPress 5.5 beta2 #23903

Merged
merged 15 commits into from
Jul 13, 2020
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
69 changes: 66 additions & 3 deletions lib/class-wp-rest-image-editor-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public function register_routes() {
'type' => 'integer',
),

// Src is required to check for correct $image_meta.
'src' => array(
'type' => 'string',
'required' => true,
),

// Crop values are in percents.
'x' => array(
'type' => 'number',
Expand Down Expand Up @@ -114,9 +120,66 @@ public function apply_edits( $request ) {
$image_file = wp_get_original_image_path( $attachment_id );
$image_meta = wp_get_attachment_metadata( $attachment_id );

if ( ! $image_meta || ! $image_file ) {
$error = __( 'Unable to get meta information for file.', 'gutenberg' );
return new WP_Error( 'rest_unknown_attachment', $error, array( 'status' => 404 ) );
if ( function_exists( 'wp_image_file_matches_image_meta' ) ) {
if (
! $image_meta ||
! $image_file ||
! wp_image_file_matches_image_meta( $request['src'], $image_meta )
) {
return new WP_Error(
'rest_unknown_attachment',
__( 'Unable to get meta information for file.', 'gutenberg' ),
array( 'status' => 404 )
);
}
} else {
// Back-compat for WP versions < 5.5.
if ( ! $image_meta || ! $image_file ) {
return new WP_Error(
'rest_unknown_attachment',
__( 'Unable to get meta information for file.', 'gutenberg' ),
array( 'status' => 404 )
);
} else {
$match = false;
$image_src = $request['src'];

if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
// Remove quiery args.
list( $image_src ) = explode( '?', $image_src );

// Check if the relative image path from the image meta is at the end of $image_src.
if ( strrpos( $image_src, $image_meta['file'] ) === strlen( $image_src ) - strlen( $image_meta['file'] ) ) {
$match = true;
}

if ( ! empty( $image_meta['sizes'] ) ) {
// Retrieve the uploads sub-directory from the full size image.
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );

if ( $dirname ) {
$dirname = trailingslashit( $dirname );
}

foreach ( $image_meta['sizes'] as $image_size_data ) {
$relative_path = $dirname . $image_size_data['file'];

if ( strrpos( $image_src, $relative_path ) === strlen( $image_src ) - strlen( $relative_path ) ) {
$match = true;
break;
}
}
}
}

if ( ! $match ) {
return new WP_Error(
'rest_unknown_attachment',
__( 'Unable to get meta information for file.', 'gutenberg' ),
array( 'status' => 404 )
);
}
}
}

$supported_types = array( 'image/jpeg', 'image/png', 'image/gif' );
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

12 changes: 4 additions & 8 deletions packages/block-editor/src/components/block-draggable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ const BlockDraggable = ( {
onDragStart,
onDragEnd,
} ) => {
const { srcRootClientId, index, isDraggable } = useSelect(
const { srcRootClientId, isDraggable } = useSelect(
( select ) => {
const {
getBlockIndex,
getBlockRootClientId,
getTemplateLock,
} = select( 'core/block-editor' );
const { getBlockRootClientId, getTemplateLock } = select(
'core/block-editor'
);
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
const templateLock = rootClientId
? getTemplateLock( rootClientId )
: null;

return {
index: getBlockIndex( clientIds[ 0 ], rootClientId ),
srcRootClientId: rootClientId,
isDraggable: 'all' !== templateLock,
};
Expand Down Expand Up @@ -64,7 +61,6 @@ const BlockDraggable = ( {

const transferData = {
type: 'block',
srcIndex: index,
srcClientIds: clientIds,
srcRootClientId,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export default function useScrollWhenDragging() {
}, [] );

const scrollOnDragOver = useCallback( ( event ) => {
if ( ! scrollParentY.current ) {
return;
}
const scrollParentHeight = scrollParentY.current.offsetHeight;
const offsetDragStartPosition =
dragStartY.current - scrollParentY.current.offsetTop;
Expand Down
14 changes: 11 additions & 3 deletions packages/block-editor/src/components/block-list/block-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
forwardRef,
} from '@wordpress/element';
import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom';
import { ENTER } from '@wordpress/keycodes';
import { ENTER, BACKSPACE, DELETE } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';

Expand Down Expand Up @@ -75,7 +75,9 @@ const BlockComponent = forwardRef(
},
[ isSelected ]
);
const { insertDefaultBlock } = useDispatch( 'core/block-editor' );
const { insertDefaultBlock, removeBlock } = useDispatch(
'core/block-editor'
);
const fallbackRef = useRef();
const isAligned = wrapperProps && !! wrapperProps[ 'data-align' ];
wrapper = wrapper || fallbackRef;
Expand Down Expand Up @@ -169,7 +171,11 @@ const BlockComponent = forwardRef(
props.onKeyDown( event );
}

if ( keyCode !== ENTER ) {
if (
keyCode !== ENTER &&
keyCode !== BACKSPACE &&
keyCode !== DELETE
) {
return;
}

Expand All @@ -181,6 +187,8 @@ const BlockComponent = forwardRef(

if ( keyCode === ENTER ) {
insertDefaultBlock( {}, rootClientId, index + 1 );
} else {
removeBlock( clientId );
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Overrides the default padding applied in editor styles otherwise preview centering break.
&.editor-styles-wrapper {
padding: 0;
margin: 0;
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,11 @@ export default compose( [
getBlockRootClientId,
hasInserterItems,
__experimentalGetAllowedBlocks,
getBlockSelectionEnd,
} = select( 'core/block-editor' );
const { getBlockVariations } = select( 'core/blocks' );

rootClientId =
rootClientId ||
getBlockRootClientId( clientId || getBlockSelectionEnd() ) ||
undefined;
rootClientId || getBlockRootClientId( clientId ) || undefined;

const allowedBlocks = __experimentalGetAllowedBlocks( rootClientId );

Expand Down
11 changes: 10 additions & 1 deletion packages/block-editor/src/components/inserter/quick-inserter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { orderBy } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -46,7 +51,11 @@ function QuickInserterList( {
onHover,
} ) {
const shownBlockTypes = useMemo(
() => blockTypes.slice( 0, SHOWN_BLOCK_TYPES ),
() =>
orderBy( blockTypes, [ 'frecency' ], [ 'desc' ] ).slice(
0,
SHOWN_BLOCK_TYPES
),
[ blockTypes ]
);
const shownBlockPatterns = useMemo(
Expand Down
10 changes: 6 additions & 4 deletions packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ $block-inserter-tabs-height: 44px;
}

.block-editor-inserter__quick-inserter {
width: $block-inserter-width;
width: 100%;

@include break-medium {
width: $block-inserter-width;
}
}

.block-editor-inserter__quick-inserter-results {
Expand All @@ -291,9 +295,7 @@ $block-inserter-tabs-height: 44px;
}

.block-editor-inserter__popover.is-quick > .components-popover__content > div {
@include break-medium {
padding: 0;
}
padding: 0;
}

.block-editor-inserter__quick-inserter-expand.components-button {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function KeyboardShortcuts() {
},
[ clientIds, removeBlocks ]
),
{ isDisabled: clientIds.length < 1 }
{ isDisabled: clientIds.length < 2 }
);

useShortcut(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function SpacingPanelControl( { children, ...props } ) {
const isSpacingEnabled = useSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );
return get( getSettings(), '__experimentalEnableCustomSpacing' );
} );
}, [] );

if ( ! isSpacingEnabled ) return null;

Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/tool-selector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { forwardRef } from '@wordpress/element';
import { edit as editIcon } from '@wordpress/icons';
import { Icon, edit as editIcon } from '@wordpress/icons';

const selectIcon = (
<SVG
Expand Down Expand Up @@ -60,7 +60,7 @@ function ToolSelector( props, ref ) {
value: 'edit',
label: (
<>
{ editIcon }
<Icon icon={ editIcon } />
{ __( 'Edit' ) }
</>
),
Expand Down
43 changes: 24 additions & 19 deletions packages/block-editor/src/components/use-block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ function parseDropEvent( event ) {
let result = {
srcRootClientId: null,
srcClientIds: null,
srcIndex: null,
type: null,
};

Expand Down Expand Up @@ -176,28 +175,32 @@ export default function useBlockDropZone( {
} ) {
const [ targetBlockIndex, setTargetBlockIndex ] = useState( null );

function selector( select ) {
const {
getBlockListSettings,
getClientIdsOfDescendants,
getSettings,
getTemplateLock,
} = select( 'core/block-editor' );
return {
orientation: getBlockListSettings( targetRootClientId )
?.orientation,
getClientIdsOfDescendants,
hasUploadPermissions: !! getSettings().mediaUpload,
isLockedAll: getTemplateLock( targetRootClientId ) === 'all',
};
}

const {
getClientIdsOfDescendants,
getBlockIndex,
hasUploadPermissions,
isLockedAll,
orientation,
} = useSelect( selector, [ targetRootClientId ] );
} = useSelect(
( select ) => {
const {
getBlockListSettings,
getClientIdsOfDescendants: _getClientIdsOfDescendants,
getBlockIndex: _getBlockIndex,
getSettings,
getTemplateLock,
} = select( 'core/block-editor' );
return {
orientation: getBlockListSettings( targetRootClientId )
?.orientation,
getClientIdsOfDescendants: _getClientIdsOfDescendants,
getBlockIndex: _getBlockIndex,
hasUploadPermissions: !! getSettings().mediaUpload,
isLockedAll: getTemplateLock( targetRootClientId ) === 'all',
};
},
[ targetRootClientId ]
);
const {
insertBlocks,
updateBlockAttributes,
Expand Down Expand Up @@ -249,7 +252,6 @@ export default function useBlockDropZone( {
const {
srcRootClientId: sourceRootClientId,
srcClientIds: sourceClientIds,
srcIndex: sourceBlockIndex,
type: dropType,
} = parseDropEvent( event );

Expand All @@ -258,6 +260,8 @@ export default function useBlockDropZone( {
return;
}

const sourceBlockIndex = getBlockIndex( sourceClientIds[ 0 ] );

// If the user is dropping to the same position, return early.
if (
sourceRootClientId === targetRootClientId &&
Expand Down Expand Up @@ -299,6 +303,7 @@ export default function useBlockDropZone( {
},
[
getClientIdsOfDescendants,
getBlockIndex,
targetBlockIndex,
moveBlocksToPosition,
targetRootClientId,
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/image/image-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ export default function ImageEditor( {
attrs.rotation = rotation;
}

attrs.src = url;

apiFetch( {
path: `wp/v2/media/${ id }/edit`,
method: 'POST',
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
function render_block_core_search( $attributes ) {
static $instance_id = 0;

// Older versions of the Search block defaulted the label and buttonText
// attributes to `__( 'Search' )` meaning that many posts contain `<!--
// wp:search /-->`. Support these by defaulting an undefined label and
// buttonText to `__( 'Search' )`.
$attributes = wp_parse_args(
$attributes,
array(
'label' => __( 'Search' ),
'buttonText' => __( 'Search' ),
)
);

$input_id = 'wp-block-search__input-' . ++$instance_id;
$label_markup = '';
$button_markup = '';
Expand Down
Loading