Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 | 1x 1x 1x 3009x 1317x 1692x 1692x 1x 1x 108x 108x 90x 18x 18x 18x 18x 18x 6x 6x 12x 6x 6x 6x 18x 18x 18x 18x 18x 18x 9x 1x 1x 1x 108x 108x 1x 108x 108x 1x 1x 1x 1x 1x | /**
* External dependencies
*/
import analytics from '@automattic/jetpack-analytics';
import { isWpcomPlatformSite, isSimpleSite } from '@automattic/jetpack-script-data';
import {
getJetpackExtensionAvailability,
withHasWarningIsInteractiveClassNames,
} from '@automattic/jetpack-shared-extension-utils';
import { createBlobURL } from '@wordpress/blob';
import { useBlockEditContext, store as blockEditorStore } from '@wordpress/block-editor';
import { parse } from '@wordpress/block-serialization-default-parser';
import { createBlock, getBlockType } from '@wordpress/blocks';
import { Button } from '@wordpress/components';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useDispatch, select } from '@wordpress/data';
import { mediaUpload, store as editorStore } from '@wordpress/editor';
import { useContext, useEffect } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { VideoPressBlockContext } from './components';
import deprecatedV1 from './deprecated/v1';
import deprecatedV2 from './deprecated/v2';
import deprecatedV3 from './deprecated/v3';
import deprecatedV4 from './deprecated/v4';
import withVideoPressEdit from './edit';
import { registerNativeVideoHtmlSources } from './native-video-attributes';
import withVideoPressSave from './save';
import { pickGUIDFromUrl, isVideoPressBlockBasedOnAttributes } from './utils';
import addV6TransformSupport from './v6-transform';
import addVideoPressVideoChaptersSupport from './video-chapters';
import videoPressBlockExampleImage from './videopress-block-example-image.jpg';
import './editor.scss';
const videoPressNoPlanMediaPlaceholder = createHigherOrderComponent(
OriginalPlaceholder => props => {
const { name } = useBlockEditContext();
// Apply to both core/video and videopress/video blocks
if ( name !== 'core/video' && name !== 'videopress/video' ) {
return <OriginalPlaceholder { ...props } />;
}
return (
<OriginalPlaceholder
{ ...props }
disableDropZone={ true }
className="no-videopress-media-placeholder"
>
<Button
disabled={ true }
className="components-button no-videopress-disabled-button"
variant="secondary"
>
{ __( 'Media Library', 'jetpack' ) }
</Button>
<Button
disabled={ true }
className="components-button no-videopress-disabled-button"
variant="secondary"
>
{ __( 'Upload', 'jetpack' ) }
</Button>
</OriginalPlaceholder>
);
},
'videoPressNoPlanMediaPlaceholder'
);
const videoPressMediaPlaceholder = createHigherOrderComponent(
OriginalPlaceholder => props => {
const { name } = useBlockEditContext();
if ( name !== 'core/video' ) {
return <OriginalPlaceholder { ...props } />;
}
const { onFilesSelected, onMediaItemSelected } = useContext( VideoPressBlockContext );
// We will handle video uploads
const newProps = {
...props,
handleUpload: false,
disableDropZone: true,
onSelect: selected => {
if ( undefined !== selected.length ) {
// Browser file upload
onFilesSelected( selected );
} else {
// WP Media Library item selected
onMediaItemSelected( selected );
}
},
};
return (
<OriginalPlaceholder
{ ...newProps }
className="videopress-media-placeholder"
></OriginalPlaceholder>
);
},
'videoPressMediaPlaceholder'
);
/**
* Gutenberg introduced a change that causes a `wp-block-video` class to be
* applied to the block via the `blocks.getSaveContent.extraProps` hook. This
* results in all prior deprecations being unable to generate what was
* previously valid content.
*
* This filter removes that introduced class so the deprecations can produce
* content that matches the originally saved post content and successfully
* migrate deprecated blocks to the current version.
*
* @param {object} props - Additional props applied to the save element.
* @param {object} blockType - Block type definition.
* @param {object} attributes - Block's attributes.
* @return {object} - Filtered props applied to the save element.
*/
const preventBlockClassOnDeprecations = ( props, blockType, attributes ) => {
// Skip manipulating the block's className prop if:
// - Not a video block
// - Is a placeholder video block ( no guid )
// - Already has wp-block-video CSS class ( block was added after Gutenberg change )
// - Block has been migrated ( previous bug meant videoPressClassNames was undefined )
if (
blockType.name !== 'core/video' ||
! attributes.guid ||
attributes.className?.indexOf( 'wp-block-video' ) >= 0 ||
attributes.videoPressClassNames
) {
return props;
}
// Prevent `wp-block-video` class being applied.
props.className = props.className.replace( 'wp-block-video', '' ).trim();
return props;
};
// Override the core/embed block to add support for v.wordpress.com URLs and hide the "videopress" embed
// block from the selectable block if VideoPress is enabled.
const addCoreEmbedOverride = settings => {
if ( ! ( 'variations' in settings ) || 'object' !== typeof settings.variations ) {
return;
}
const { available } = getJetpackExtensionAvailability( 'videopress' );
settings.variations.some( variation => {
if ( 'videopress' === variation.name ) {
// If VideoPress is available, hide the core VideoPress embed blocks.
if ( available ) {
variation.scope = [];
}
// Add support for old v.wordpress.com URLs
variation.patterns.push( /^https?:\/\/v\.wordpress\.com\/([a-zA-Z\d]{8})(.+)?$/i );
return true;
}
return false;
} );
};
const addVideoPressSupport = ( settings, name ) => {
Iif ( 'core/embed' === name ) {
addCoreEmbedOverride( settings );
return settings;
}
// Bail if this is not the video block or if the hook has been triggered by a deprecation.
if ( 'core/video' !== name || settings.isDeprecation ) {
return settings;
}
const { deprecated, edit, save, supports, transforms } = settings;
const { available, unavailableReason } = getJetpackExtensionAvailability( 'videopress' );
const isNotAvailable =
isWpcomPlatformSite() && [ 'missing_plan', 'unknown' ].includes( unavailableReason );
const resumableUploadEnabled = !! window.videoPressResumableEnabled;
// Check if VideoPress is unavailable and filter the mediaplaceholder to limit options
if ( isNotAvailable ) {
addFilter( 'editor.MediaPlaceholder', 'jetpack/videopress', videoPressNoPlanMediaPlaceholder );
addFilter(
'editor.BlockListBlock',
`jetpack/videopress-with-has-warning-is-interactive-class-names`,
withHasWarningIsInteractiveClassNames( `core/video` )
);
} else if ( available ) {
Iif ( resumableUploadEnabled ) {
addFilter( 'editor.MediaPlaceholder', 'jetpack/videopress', videoPressMediaPlaceholder );
}
// If VideoPress is available, we update the block description and example with VideoPress-specific content.
settings.description = __(
'Embed a video from your media library or upload a new one with VideoPress.',
'jetpack'
);
settings.example.attributes = {
caption: '',
isVideoPressExample: true,
src: videoPressBlockExampleImage,
};
}
addFilter(
'blocks.getSaveContent.extraProps',
'jetpack/videopress',
preventBlockClassOnDeprecations,
20
);
// We customize the video block even if VideoPress it not available so we can support videos that were uploaded to
// VideoPress if it was available in the past (i.e. before a plan downgrade).
Eif (
available ||
[ 'missing_plan', 'missing_module', 'unknown' ].includes( unavailableReason )
) {
const attributesDefinition = {
autoplay: {
type: 'boolean',
},
caption: {
type: 'string',
source: 'html',
selector: 'figcaption',
},
controls: {
type: 'boolean',
default: true,
},
maxWidth: {
type: 'string',
default: '100%',
},
guid: {
type: 'string',
},
id: {
type: 'number',
},
loop: {
type: 'boolean',
},
isVideoPressExample: {
type: 'boolean',
default: false,
},
muted: {
type: 'boolean',
},
playsinline: {
type: 'boolean',
},
poster: {
type: 'string',
},
preload: {
type: 'string',
default: 'metadata',
},
seekbarPlayedColor: {
type: 'string',
default: '',
},
seekbarLoadingColor: {
type: 'string',
default: '',
},
seekbarColor: {
type: 'string',
default: '',
},
src: {
type: 'string',
source: 'attribute',
selector: 'video',
attribute: 'src',
},
useAverageColor: {
type: 'boolean',
default: true,
},
videoPressTracks: {
type: 'array',
items: {
type: 'object',
},
default: [],
},
videoPressClassNames: {
type: 'string',
},
fileForImmediateUpload: {
type: 'object',
default: null,
},
};
const oldVideoEmbedRegex = /https?:\/\/v\.wordpress\.com\/([a-zA-Z\d]{8})(.+)?/i;
registerNativeVideoHtmlSources( settings.attributes );
return {
...settings,
attributes: {
// Keep the original attributes to avoid breaking the block when its video is not a VideoPress video.
...settings.attributes,
...attributesDefinition,
},
transforms: {
...transforms,
from: [
{
type: 'files',
isMatch: files => files.every( file => file.type.indexOf( 'video/' ) === 0 ),
// We define a higher priority (lower number) than the default of 10. This ensures that this
// transformation prevails over the core video block default transformations.
priority: 9,
transform: ( files, onChange ) => {
const blocks = [];
files.forEach( file => {
if ( available && resumableUploadEnabled ) {
// VideoPress block handles the upload
const block = createBlock( 'core/video', {
fileForImmediateUpload: file,
} );
blocks.push( block );
} else {
// Use core block w/ standard mediaUpload
const block = createBlock( 'core/video', {
src: createBlobURL( file ),
} );
mediaUpload( {
filesList: [ file ],
onFileChange: ( [ { id, url } ] ) => {
onChange( block.clientId, { id, src: url } );
},
allowedTypes: [ 'video' ],
} );
blocks.push( block );
}
} );
return blocks;
},
},
// Transform old v.wordpress.com classic block embeds to videopress.com/v/ embed
{
type: 'raw',
isMatch: node => {
return node.nodeName === 'P' && oldVideoEmbedRegex.test( node.innerHTML );
},
transform: node => {
const matches = oldVideoEmbedRegex.exec( node.innerHTML );
return createBlock( 'core/embed', {
url: 'https://videopress.com/v/' + matches[ 1 ].trim(),
} );
},
},
],
},
supports: {
...supports,
reusable: false,
},
edit: withVideoPressEdit( edit ),
save: withVideoPressSave( save ),
deprecated: [
...( deprecated || [] ),
deprecatedV4,
deprecatedV3,
{
attributes: attributesDefinition,
isEligible: attrs => ! attrs.guid,
save,
supports,
isDeprecation: true,
},
deprecatedV2,
deprecatedV1,
],
};
}
return settings;
};
/**
* Assign higher-than-default priority to make our modifications before the more generic
* Gutenberg filters are run (that e.g. inject an extra `align` attribute based on the
* corresponding `supports` field).
*
* @see packages/block-editor/src/hooks/align.js
*/
addFilter( 'blocks.registerBlockType', 'jetpack/videopress', addVideoPressSupport, 5 );
addFilter(
'blocks.registerBlockType',
'videopress/add-wp-chapters-support',
addVideoPressVideoChaptersSupport
);
addFilter(
'blocks.registerBlockType',
'videopress/add-v6-transform-support',
addV6TransformSupport
);
/**
* Handle videopress/video block unavailability.
* When the block is unavailable due to missing plan, add a filter to disable
* the upload buttons in the MediaPlaceholder.
*
* @param {object} settings - Block settings.
* @param {string} name - Block name.
* @return {object} Modified block settings.
*/
function handleVideoPressVideoUnavailability( settings, name ) {
// Only apply to videopress/video block.
Eif ( name !== 'videopress/video' ) {
return settings;
}
const { available, unavailableReason } = getJetpackExtensionAvailability( 'videopress/video' );
// If available, don't modify.
if ( available ) {
return settings;
}
// Check if unavailable due to missing plan on WPCOM.
const isUnavailableDueToMissingPlan =
isWpcomPlatformSite() && [ 'missing_plan', 'unknown' ].includes( unavailableReason );
if ( ! isUnavailableDueToMissingPlan ) {
return settings;
}
// Add the filter to disable upload buttons in the MediaPlaceholder for videopress/video blocks.
addFilter(
'editor.MediaPlaceholder',
'jetpack/videopress-video-no-plan',
videoPressNoPlanMediaPlaceholder
);
// Add the warning/interactive class names filter for visual consistency with core/video.
addFilter(
'editor.BlockListBlock',
'jetpack/videopress-video-with-has-warning-is-interactive-class-names',
withHasWarningIsInteractiveClassNames( 'videopress/video' )
);
return settings;
}
addFilter(
'blocks.registerBlockType',
'videopress/handle-unavailability',
handleVideoPressVideoUnavailability,
// Use priority 20 to run after the block is registered but before other modifications.
20
);
/**
* Extend videopress/video transform to/from core/video block.
*
* @param {object} settings - Block settings.
* @param {string} name - Block name.
* @return {object} Modified block settings.
*/
function addVideoPressCoreVideoTransform( settings, name ) {
// Apply only to videopress/video block.
Eif ( name !== 'videopress/video' ) {
return settings;
}
const isVideoPressVideoBlockRegistered = getBlockType( 'videopress/video' );
const { available: isVideoPressVideoBlockAvailable } =
getJetpackExtensionAvailability( 'videopress/video' );
// If videopress/video block is not registered or not available, do not extend transforms.
if ( ! isVideoPressVideoBlockRegistered || ! isVideoPressVideoBlockAvailable ) {
return settings;
}
return {
...settings,
transforms: {
from: [
...( settings.transforms?.from || [] ),
{
type: 'block',
blocks: [ 'core/video' ],
isMatch: attrs => {
const { src, guid } = attrs;
const guidFromSrc = pickGUIDFromUrl( src );
return guid || guidFromSrc;
},
transform: attrs => {
const postId = select( editorStore ).getCurrentPostId();
analytics?.tracks?.recordEvent(
'jetpack_editor_videopress_block_manual_transform_click',
{
post_id: postId,
}
);
return createBlock( 'videopress/video', attrs );
},
},
],
to: [
...( settings.transforms?.to || [] ),
{
type: 'block',
blocks: [ 'core/video' ],
transform: attrs => createBlock( 'core/video', attrs ),
},
],
},
};
}
addFilter(
'blocks.registerBlockType',
'videopress/add-core-video-transform',
addVideoPressCoreVideoTransform
);
/**
* Organize the block attributes for the new videopress/video block
*
* @param {object} attributes - core/video block attributes
* @param {object} defaultAttributes - default core/video block attributes
* @return {object} The new attributes
*/
function getVideoPressVideoBlockAttributes( attributes, defaultAttributes ) {
const attrs = attributes || defaultAttributes;
// Update attributes names to match the new VideoPress Video block.
if ( attrs?.videoPressTracks ) {
attrs.tracks = attrs.videoPressTracks || [];
delete attrs.videoPressTracks;
}
if ( attrs?.isVideoPressExample ) {
attrs.isExample = attrs.isVideoPressExample || [];
delete attrs.isVideoPressExample;
}
return attrs;
}
function mapV6AttributesToV5( attributes ) {
const newAttributes = { ...attributes };
if ( attributes?.tracks ) {
newAttributes.videoPressTracks = attributes.tracks;
delete newAttributes.tracks;
}
if ( attributes?.isExample ) {
newAttributes.isVideoPressExample = attributes.isExample;
delete newAttributes.isExample;
}
if ( attributes?.classNames ) {
newAttributes.videoPressClassNames = attributes.classNames;
delete newAttributes.classNames;
}
// Clean the rest of the attributes.
delete newAttributes.title;
delete newAttributes.description;
delete newAttributes.cacheHtml;
delete newAttributes.videoRatio;
delete newAttributes.privacySetting;
delete newAttributes.allowDownload;
delete newAttributes.displayEmbed;
delete newAttributes.rating;
delete newAttributes.isPrivate;
return newAttributes;
}
/**
* Convert video blocks to VideoPress video blocks,
* when the app detects that the block is a VideoPress block instance.
*
* Blocks list:
* - core/video
* - core/embed is not auto-converted for the moment.
* @todo consider to do it in the future.
*/
const convertVideoBlockToVideoPressVideoBlock = createHigherOrderComponent( BlockListBlock => {
return props => {
const { block } = props;
const { name, attributes, clientId, __unstableBlockSource } = block;
const { replaceBlock } = useDispatch( blockEditorStore );
const { url, guid: guidAttr, providerNameSlug } = attributes;
/*
* We try to recognize core/video Jetpack VideoPress block,
* based on some of its attributes.
*/
const isCoreVideoVideoPressBlock = isVideoPressBlockBasedOnAttributes( attributes );
const isVideoPressVideoBlockRegistered = getBlockType( 'videopress/video' );
const { available: isVideoPressVideoBlockAvailable } =
getJetpackExtensionAvailability( 'videopress/video' );
const isCoreVideoBlock = name === 'core/video';
const isCoreEmbedBlock = name === 'core/embed';
const guidFromUrl = pickGUIDFromUrl( url );
const isCoreEmbedVideoPressVariation = providerNameSlug === 'videopress' && !! guidFromUrl;
/*
* GUID can come `guid` attribute (for core/video)
* or from the `url` attribute (for core/embed)
*/
const guid = isCoreEmbedBlock && guidFromUrl ? guidFromUrl : guidAttr;
const isSimple = isSimpleSite();
// Note: conversion disabled for now.
const shouldConvertCoreVideoToVideoPressVideoBlock = !! (
isCoreVideoBlock && // Only auto-convert if the block is a core/video block
isVideoPressVideoBlockRegistered && // Only auto-convert if the VideoPress block is registered
isCoreVideoVideoPressBlock && // Only auto-convert if the block is a VideoPress block
isVideoPressVideoBlockAvailable && // Only auto-convert if the feature is available
isSimple && // Only auto-convert if the site is Simple
// Disable auto-conversion for now.
false
);
// Note: conversion disabled for now.
const shouldConvertCoreEmbedToVideoPressVideoBlock = !! (
isCoreEmbedBlock && // Only auto-convert if the block is a core/embed block
isVideoPressVideoBlockRegistered && // Only auto-convert if the VideoPress block is registered
isCoreEmbedVideoPressVariation && // Only auto-convert if the block is a embed VideoPress variation
isVideoPressVideoBlockAvailable && // Only auto-convert if the feature is available
isSimple && // Only auto-convert if the site is Simple
// Disable auto-conversion for now.
false
);
const shouldConvertToVideoPressVideoBlock =
shouldConvertCoreVideoToVideoPressVideoBlock || shouldConvertCoreEmbedToVideoPressVideoBlock;
// clean oEmbed class attribute since it's not needed for v6
if ( shouldConvertCoreEmbedToVideoPressVideoBlock && attributes.className ) {
delete attributes.className;
}
useEffect( () => {
if ( ! shouldConvertToVideoPressVideoBlock ) {
return;
}
replaceBlock(
clientId,
createBlock(
'videopress/video',
getVideoPressVideoBlockAttributes( __unstableBlockSource?.attrs, { ...attributes, guid } )
)
);
}, [
clientId,
shouldConvertToVideoPressVideoBlock,
attributes,
__unstableBlockSource,
replaceBlock,
guid,
] );
return <BlockListBlock { ...props } />;
};
}, 'convertVideoBlockToVideoPressVideoBlock' );
addFilter(
'editor.BlockListBlock',
'videopress/jetpack-convert-to-videopress-video-block',
convertVideoBlockToVideoPressVideoBlock
);
function ConvertV6toV5Effect( { BlockListBlock, ...props } ) {
const { block } = props;
const { name, attributes, clientId } = block;
const { replaceBlock } = useDispatch( blockEditorStore );
useEffect( () => {
try {
const parsedData = parse( attributes.originalContent );
const originalBlock = parsedData?.[ 0 ];
if ( ! originalBlock ) {
return;
}
const { attrs } = originalBlock;
replaceBlock( clientId, createBlock( 'core/video', mapV6AttributesToV5( attrs ) ) );
} catch ( e ) {
// eslint-disable-next-line no-console
console.error( 'Error converting VideoPress block to core/video', e );
}
}, [ name, clientId, attributes, replaceBlock ] );
return <BlockListBlock { ...props } />;
}
const convertV6toV5 = createHigherOrderComponent( BlockListBlock => {
return props => {
const { block } = props;
const { name, attributes } = block;
// CAUTION: code added before this line will be executed for all blocks
// (also on typing), not just missing blocks.
if ( name !== 'core/missing' || attributes?.originalName !== 'videopress/video' ) {
return <BlockListBlock { ...props } />;
}
return <ConvertV6toV5Effect { ...props } BlockListBlock={ BlockListBlock } />;
};
}, 'convertV6toV5' );
addFilter( 'editor.BlockListBlock', 'videopress/jetpack-convert-from-v6-to-v5', convertV6toV5 );
|