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

Cover: Handling site plan and its errors #16107

Merged
merged 33 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
98474fa
cover: start customize media placeholder
retrofox Jun 8, 2020
8433eb8
cover: allos only images by default
retrofox Jun 8, 2020
f794b6c
cover: show upgrade nudge
retrofox Jun 8, 2020
c19538b
cover: extend media placeholder in filter
retrofox Jun 9, 2020
aa724bd
cover: show nudge only when error contains a file
retrofox Jun 8, 2020
32f1077
cover: populate with wpAllowedVideoMimeTypes prop
retrofox Jun 9, 2020
15d9222
cover: populate with allowed videos props
retrofox Jun 9, 2020
397d950
cover: show upgrade nudge only for videos
retrofox Jun 9, 2020
3a79ce0
cover: do not allow multiple uploading
retrofox Jun 9, 2020
d624244
cover: TRY picking up file from prop
retrofox Jun 9, 2020
bdd36d2
cover: extend only for simple sites
retrofox Jun 9, 2020
8157640
cover: allow uploading video
retrofox Jun 9, 2020
83231fc
cover: halding simple site + free plan
retrofox Jun 9, 2020
e5d902f
cover: use constans to detect uploading video
retrofox Jun 9, 2020
56a6403
cover: improve doc
retrofox Jun 9, 2020
8f8c972
cover: refact -> use hook to get block name
retrofox Jun 9, 2020
f76982f
cover: remove unused props
retrofox Jun 9, 2020
be1460a
cover: move site & plan logic to media placeholder
retrofox Jun 9, 2020
a18b2d7
cover: adjust dims of nudge
retrofox Jun 9, 2020
da2622e
cover: fix i18n bug
retrofox Jun 9, 2020
b2b3147
cover: tweak upgrade nudge.
retrofox Jun 10, 2020
9590bae
cover: remove ussage of the experimental prop
retrofox Jun 10, 2020
a5b3b85
Update extensions/shared/blocks/cover/cover-media-placeholder.js
retrofox Jun 10, 2020
2209fed
upgrade-nudge: allow don't render subtitle
retrofox Jun 10, 2020
5bd4db9
cover: remove nudge subtitle
retrofox Jun 10, 2020
19c02fe
Merge branch 'update/cover-block-upgrade' of github.com:Automattic/je…
retrofox Jun 10, 2020
4eff238
cover: improve jsdoc
retrofox Jun 11, 2020
25294bd
cover: move hook at top of the component
retrofox Jun 11, 2020
ec16a96
cover: improve checking need upgrade
retrofox Jun 11, 2020
ac7bde5
cover: do not create an HOC unecessarely
retrofox Jun 11, 2020
84f695a
cover: remove unneeded dependency. Improve jsdoc.
retrofox Jun 15, 2020
e7a61ea
cover: apply filter only when user is connected
retrofox Jun 15, 2020
c272350
upgrade-nudge: improve jsdoc
retrofox Jun 15, 2020
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
1 change: 1 addition & 0 deletions extensions/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './shared/block-category';
import './shared/plan-upgrade-notification';
import './shared/stripe-connection-notification';
import './shared/external-media';
import './shared/blocks/cover';
import analytics from '../_inc/client/lib/analytics';

// @TODO Please make a shared analytics solution and remove this!
Expand Down
88 changes: 88 additions & 0 deletions extensions/shared/blocks/cover/cover-media-placeholder.js
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
retrofox marked this conversation as resolved.
Show resolved Hide resolved
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>
);
};
13 changes: 13 additions & 0 deletions extensions/shared/blocks/cover/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.jetpack-cover-media-placeholder {
width: 100%;

// tweak Upgrade nudge.
retrofox marked this conversation as resolved.
Show resolved Hide resolved
.block-editor-warning__contents {
flex-wrap: initial;

.jetpack-block-nudge__text-container,
.block-editor-warning__actions {
align-self: center;
}
}
}
24 changes: 24 additions & 0 deletions extensions/shared/blocks/cover/index.js
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
);
}
19 changes: 19 additions & 0 deletions extensions/shared/blocks/cover/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

export const videoFileExtensions = [
retrofox marked this conversation as resolved.
Show resolved Hide resolved
'ogv',
'mp4',
'm4v',
'mov',
'qt',
'wmv',
'avi',
'mpeg',
'mpg',
'mpe',
'3gp',
'3gpp',
'3g2',
'3gp2',
'3gp',
'3g2',
];
2 changes: 1 addition & 1 deletion extensions/shared/components/block-nudge/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const BlockNudge = ( { autosaveAndRedirect, buttonLabel, href, icon, subt
{ icon }
<span className="jetpack-block-nudge__text-container">
<span className="jetpack-block-nudge__title">{ title }</span>
<span className="jetpack-block-nudge__message">{ subtitle }</span>
{ subtitle && <span className="jetpack-block-nudge__message">{ subtitle }</span> }
</span>
</span>
</Warning>
Expand Down
34 changes: 26 additions & 8 deletions extensions/shared/components/upgrade-nudge/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ const getTitle = ( customTitle, planName ) => {
: __( 'Upgrade to a paid plan to use this block on your site.', 'jetpack' );
};

/**
* Return the nudge description translated to the user language, or Null.
* `subtitle` param accepts three types:
* - A string, in which case it will translate and returned.
* - False (boolean), in which case it will return false
* - Undefined: it will return the default nudge description.
*
* @param {string|boolean} subtitle - Subtitle to translate, or False.
* @returns {string|null} Nudge description, or Null.
*/
const getSubtitle = ( subtitle ) => {
if ( subtitle === false ) {
return null;
}
retrofox marked this conversation as resolved.
Show resolved Hide resolved

if ( ! subtitle ) {
return __(
'You can try it out before upgrading, but only you will see it. It will be hidden from your visitors until you upgrade.',
'jetpack'
);
}

return subtitle;
};

export const UpgradeNudge = ( {
planName,
trackViewEvent,
Expand Down Expand Up @@ -61,14 +86,7 @@ export const UpgradeNudge = ( {
href={ upgradeUrl }
onClick={ trackClickEvent }
title={ getTitle( title, planName ) }
subtitle={
subtitle
? subtitle
: __(
'You can try it out before upgrading, but only you will see it. It will be hidden from your visitors until you upgrade.',
'jetpack'
)
}
subtitle={ getSubtitle( subtitle ) }
/>
);
};
Expand Down