-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cover: Handling site plan and its errors (#16107)
Co-authored-by: Jeremy Herve <[email protected]>
- Loading branch information
Showing
7 changed files
with
172 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockEditContext } from '@wordpress/block-editor'; | ||
import { useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import UpgradeNudge from "../../components/upgrade-nudge"; | ||
import { videoFileExtensions } from './utils'; | ||
import { isSimpleSite } from "../../site-type-utils"; | ||
import getJetpackExtensionAvailability from "../../get-jetpack-extension-availability"; | ||
|
||
/** | ||
* Module Constants | ||
*/ | ||
const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ]; | ||
|
||
/** | ||
* Nudge shows when the user tries to upload a video file. | ||
* Unlike the core/video block, handled/extended by the videopress block, | ||
* the nudge is not shown permanently. | ||
* It's handled by the MediaPlaceholder component | ||
* when the user tries to upload a video file. | ||
* For this reason, we can't wrap the edit setting | ||
* with the wrapPaidBlock() HOC, as the videopress does. | ||
* | ||
* @param {object} props - Information about the user. | ||
* @param {string} props.name - Show the Nudge component. | ||
* @param {boolean} props.show - Show the Nudge component. | ||
* @returns {*} Nudge component or Null. | ||
*/ | ||
const JetpackCoverUpgradeNudge = ( { name, show } ) => | ||
show | ||
? <UpgradeNudge | ||
plan="value_bundle" | ||
blockName={ name } | ||
title={ { | ||
knownPlan: __( 'To use a video in this block, upgrade to %(planName)s.', 'jetpack' ), | ||
unknownPlan: __( 'To use a video in this block, upgrade to a paid plan.', 'jetpack' ), | ||
} } | ||
subtitle={ false } | ||
/> | ||
: null; | ||
|
||
export default CoreMediaPlaceholder => props => { | ||
const [ error, setError ] = useState( false ); | ||
const { name } = useBlockEditContext(); | ||
const { unavailableReason } = getJetpackExtensionAvailability( 'videopress' ); | ||
|
||
if ( | ||
( ! name || name !== 'core/cover' ) || // extend only for cover block | ||
! isSimpleSite() || // only for Simple sites | ||
! [ 'missing_plan', 'unknown' ].includes( unavailableReason ) | ||
) { | ||
return <CoreMediaPlaceholder { ...props } />; | ||
} | ||
|
||
const { onError } = props; | ||
return ( | ||
<div className="jetpack-cover-media-placeholder"> | ||
<JetpackCoverUpgradeNudge name={ name } show={ !! error } /> | ||
<CoreMediaPlaceholder | ||
{ ...props } | ||
multiple={ false } | ||
onError = { ( message ) => { | ||
// Try to pick up filename from the error message. | ||
// We should find a better way to do it. Unstable. | ||
const filename = message?.[0]?.props?.children; | ||
if ( ! filename ) { | ||
return onError( message ); | ||
} | ||
|
||
const fileExtension = ( filename.split( '.' ) )?.[ 1 ]; | ||
if ( ! videoFileExtensions.includes( fileExtension ) ) { | ||
return onError( message ); | ||
} | ||
|
||
return setError( message ); | ||
} } | ||
allowedTypes={ ALLOWED_MEDIA_TYPES } | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.jetpack-cover-media-placeholder { | ||
width: 100%; | ||
|
||
// tweak Upgrade nudge. | ||
.block-editor-warning__contents { | ||
flex-wrap: initial; | ||
|
||
.jetpack-block-nudge__text-container, | ||
.block-editor-warning__actions { | ||
align-self: center; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import coverEditMediaPlaceholder from './cover-media-placeholder'; | ||
import isCurrentUserConnected from "../../is-current-user-connected"; | ||
import './editor.scss'; | ||
|
||
if ( isCurrentUserConnected() ) { | ||
// Take the control of MediaPlaceholder. | ||
addFilter( | ||
'editor.MediaPlaceholder', | ||
'jetpack/cover-edit-media-placeholder', | ||
coverEditMediaPlaceholder | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
export const videoFileExtensions = [ | ||
'ogv', | ||
'mp4', | ||
'm4v', | ||
'mov', | ||
'qt', | ||
'wmv', | ||
'avi', | ||
'mpeg', | ||
'mpg', | ||
'mpe', | ||
'3gp', | ||
'3gpp', | ||
'3g2', | ||
'3gp2', | ||
'3gp', | ||
'3g2', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters