Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Captions
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2020 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2020 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Media\Video;
|
||||
|
||||
use Google\Web_Stories\Service_Base;
|
||||
|
||||
/**
|
||||
* Class Captions
|
||||
*/
|
||||
class Captions extends Service_Base {
|
||||
/**
|
||||
* Initializes the File_Type logic.
|
||||
*
|
||||
* @since 1.7.0
|
||||
*/
|
||||
public function register(): void {
|
||||
add_filter( 'site_option_upload_filetypes', [ $this, 'filter_list_of_allowed_filetypes' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add VTT file type to allow file in multisite.
|
||||
*
|
||||
* @param string|mixed $value List of allowed file types.
|
||||
* @return string|mixed List of allowed file types.
|
||||
*/
|
||||
public function filter_list_of_allowed_filetypes( $value ) {
|
||||
if ( ! \is_string( $value ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$filetypes = explode( ' ', $value );
|
||||
if ( ! \in_array( 'vtt', $filetypes, true ) ) {
|
||||
$filetypes[] = 'vtt';
|
||||
$value = implode( ' ', $filetypes );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
111
wp-content/plugins/web-stories/includes/Media/Video/Is_Gif.php
Normal file
111
wp-content/plugins/web-stories/includes/Media/Video/Is_Gif.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Is_Gif
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2022 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2022 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Media\Video;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\HasMeta;
|
||||
use Google\Web_Stories\Infrastructure\PluginUninstallAware;
|
||||
use Google\Web_Stories\Service_Base;
|
||||
|
||||
/**
|
||||
* Class Is_Gif
|
||||
*/
|
||||
class Is_Gif extends Service_Base implements HasMeta, PluginUninstallAware {
|
||||
/**
|
||||
* The post meta key.
|
||||
*/
|
||||
public const IS_GIF_POST_META_KEY = 'web_stories_is_gif';
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @since 1.23.0
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->register_meta();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register post meta
|
||||
*
|
||||
* @since 1.23.0
|
||||
*/
|
||||
public function register_meta(): void {
|
||||
register_post_meta(
|
||||
'attachment',
|
||||
self::IS_GIF_POST_META_KEY,
|
||||
[
|
||||
'sanitize_callback' => 'rest_sanitize_boolean',
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether the video is to be considered a GIF', 'web-stories' ),
|
||||
'show_in_rest' => true,
|
||||
'default' => false,
|
||||
'single' => true,
|
||||
'object_subtype' => 'attachment',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the attachment data prepared for JavaScript.
|
||||
*
|
||||
* @since 1.23.0
|
||||
*
|
||||
* @param array|mixed $response Array of prepared attachment data.
|
||||
* @return array|mixed Response data.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @phpstan-return ($response is array<T> ? array<T> : mixed)
|
||||
*/
|
||||
public function wp_prepare_attachment_for_js( $response ) {
|
||||
if ( ! \is_array( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post ID.
|
||||
*
|
||||
* @var int $post_id
|
||||
*/
|
||||
$post_id = $response['id'];
|
||||
|
||||
$response[ self::IS_GIF_POST_META_KEY ] = get_post_meta( $post_id, self::IS_GIF_POST_META_KEY, true );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin uninstall.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*/
|
||||
public function on_plugin_uninstall(): void {
|
||||
delete_post_meta_by_key( self::IS_GIF_POST_META_KEY );
|
||||
}
|
||||
}
|
231
wp-content/plugins/web-stories/includes/Media/Video/Muting.php
Normal file
231
wp-content/plugins/web-stories/includes/Media/Video/Muting.php
Normal file
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Muting
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Media\Video;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\HasMeta;
|
||||
use Google\Web_Stories\Infrastructure\PluginUninstallAware;
|
||||
use Google\Web_Stories\Service_Base;
|
||||
use WP_Error;
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* Class Muting
|
||||
*/
|
||||
class Muting extends Service_Base implements HasMeta, PluginUninstallAware {
|
||||
|
||||
/**
|
||||
* Is muted.
|
||||
*/
|
||||
public const IS_MUTED_POST_META_KEY = 'web_stories_is_muted';
|
||||
|
||||
/**
|
||||
* The muted video id post meta key.
|
||||
*/
|
||||
public const MUTED_ID_POST_META_KEY = 'web_stories_muted_id';
|
||||
|
||||
/**
|
||||
* Is muted.
|
||||
*/
|
||||
public const IS_MUTED_REST_API_KEY = 'web_stories_is_muted';
|
||||
|
||||
/**
|
||||
* Register.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->register_meta();
|
||||
|
||||
add_action( 'delete_attachment', [ $this, 'delete_video' ] );
|
||||
add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
|
||||
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register meta for attachment post type.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
public function register_meta(): void {
|
||||
register_meta(
|
||||
'post',
|
||||
self::IS_MUTED_POST_META_KEY,
|
||||
[
|
||||
'type' => 'boolean',
|
||||
'description' => __( 'Whether the video is muted', 'web-stories' ),
|
||||
'default' => false,
|
||||
'single' => true,
|
||||
'object_subtype' => 'attachment',
|
||||
]
|
||||
);
|
||||
|
||||
register_meta(
|
||||
'post',
|
||||
self::MUTED_ID_POST_META_KEY,
|
||||
[
|
||||
'sanitize_callback' => 'absint',
|
||||
'type' => 'integer',
|
||||
'description' => __( 'ID of muted video.', 'web-stories' ),
|
||||
'show_in_rest' => true,
|
||||
'default' => 0,
|
||||
'single' => true,
|
||||
'object_subtype' => 'attachment',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers additional REST API fields upon API initialization.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
public function rest_api_init(): void {
|
||||
register_rest_field(
|
||||
'attachment',
|
||||
self::IS_MUTED_REST_API_KEY,
|
||||
[
|
||||
'get_callback' => [ $this, 'get_callback_is_muted' ],
|
||||
'schema' => [
|
||||
'type' => [ 'boolean', 'null' ],
|
||||
'description' => __( 'Whether the video is muted', 'web-stories' ),
|
||||
'default' => null,
|
||||
'context' => [ 'view', 'edit', 'embed' ],
|
||||
'arg_options' => [
|
||||
'sanitize_callback' => 'rest_sanitize_boolean',
|
||||
],
|
||||
],
|
||||
'update_callback' => [ $this, 'update_callback_is_muted' ],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the attachment data prepared for JavaScript.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*
|
||||
* @param array|mixed $response Array of prepared attachment data.
|
||||
* @return array|mixed Response data.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @phpstan-return ($response is array<T> ? array<T> : mixed)
|
||||
*/
|
||||
public function wp_prepare_attachment_for_js( $response ) {
|
||||
if ( ! \is_array( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
if ( 'video' === $response['type'] ) {
|
||||
$response[ self::IS_MUTED_REST_API_KEY ] = $this->get_callback_is_muted( $response );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachment's post meta.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*
|
||||
* @param array<string, mixed> $prepared Array of data to add to.
|
||||
*/
|
||||
public function get_callback_is_muted( array $prepared ): ?bool {
|
||||
/**
|
||||
* Attachment ID.
|
||||
*
|
||||
* @var int $id
|
||||
*/
|
||||
$id = $prepared['id'];
|
||||
|
||||
/**
|
||||
* Muted value.
|
||||
*
|
||||
* @var bool|null $value
|
||||
*/
|
||||
$value = get_metadata_raw( 'post', $id, self::IS_MUTED_POST_META_KEY, true );
|
||||
|
||||
if ( null === $value ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return rest_sanitize_boolean( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the attachment's post meta.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*
|
||||
* @param mixed $value Value to updated.
|
||||
* @param WP_Post $post Post object to be updated.
|
||||
* @return true|WP_Error
|
||||
*/
|
||||
public function update_callback_is_muted( $value, WP_Post $post ) {
|
||||
$object_id = $post->ID;
|
||||
$name = self::IS_MUTED_REST_API_KEY;
|
||||
$meta_key = self::IS_MUTED_POST_META_KEY;
|
||||
|
||||
if ( ! current_user_can( 'edit_post_meta', $object_id, $meta_key ) ) {
|
||||
return new \WP_Error(
|
||||
'rest_cannot_update',
|
||||
/* translators: %s: Custom field key.**/
|
||||
sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.', 'web-stories' ), $name ),
|
||||
[
|
||||
'key' => $name,
|
||||
'status' => rest_authorization_required_code(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
update_post_meta( $object_id, $meta_key, $value );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes associated meta data when a video is deleted.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*
|
||||
* @param int $attachment_id ID of the attachment to be deleted.
|
||||
*/
|
||||
public function delete_video( int $attachment_id ): void {
|
||||
delete_metadata( 'post', 0, self::MUTED_ID_POST_META_KEY, $attachment_id, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin uninstall.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*/
|
||||
public function on_plugin_uninstall(): void {
|
||||
delete_post_meta_by_key( self::MUTED_ID_POST_META_KEY );
|
||||
delete_post_meta_by_key( self::IS_MUTED_POST_META_KEY );
|
||||
}
|
||||
}
|
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Optimization
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Media\Video;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\HasMeta;
|
||||
use Google\Web_Stories\Infrastructure\PluginUninstallAware;
|
||||
use Google\Web_Stories\Service_Base;
|
||||
|
||||
/**
|
||||
* Class Optimization
|
||||
*/
|
||||
class Optimization extends Service_Base implements HasMeta, PluginUninstallAware {
|
||||
|
||||
/**
|
||||
* The optimized video id post meta key.
|
||||
*/
|
||||
public const OPTIMIZED_ID_POST_META_KEY = 'web_stories_optimized_id';
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->register_meta();
|
||||
add_action( 'delete_attachment', [ $this, 'delete_video' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register meta
|
||||
*
|
||||
* @since 1.15.0
|
||||
*/
|
||||
public function register_meta(): void {
|
||||
register_meta(
|
||||
'post',
|
||||
self::OPTIMIZED_ID_POST_META_KEY,
|
||||
[
|
||||
'sanitize_callback' => 'absint',
|
||||
'type' => 'integer',
|
||||
'description' => __( 'ID of optimized video.', 'web-stories' ),
|
||||
'show_in_rest' => true,
|
||||
'default' => 0,
|
||||
'single' => true,
|
||||
'object_subtype' => 'attachment',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes associated meta data when a video is deleted.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*
|
||||
* @param int $attachment_id ID of the attachment to be deleted.
|
||||
*/
|
||||
public function delete_video( int $attachment_id ): void {
|
||||
delete_metadata( 'post', 0, self::OPTIMIZED_ID_POST_META_KEY, $attachment_id, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin uninstall.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*/
|
||||
public function on_plugin_uninstall(): void {
|
||||
delete_post_meta_by_key( self::OPTIMIZED_ID_POST_META_KEY );
|
||||
}
|
||||
}
|
279
wp-content/plugins/web-stories/includes/Media/Video/Poster.php
Normal file
279
wp-content/plugins/web-stories/includes/Media/Video/Poster.php
Normal file
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Poster
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Media\Video;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\HasMeta;
|
||||
use Google\Web_Stories\Infrastructure\PluginUninstallAware;
|
||||
use Google\Web_Stories\Media\Media_Source_Taxonomy;
|
||||
use Google\Web_Stories\Service_Base;
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* Class Poster
|
||||
*/
|
||||
class Poster extends Service_Base implements HasMeta, PluginUninstallAware {
|
||||
/**
|
||||
* The poster post meta key.
|
||||
*/
|
||||
public const POSTER_POST_META_KEY = 'web_stories_is_poster';
|
||||
|
||||
/**
|
||||
* The poster id post meta key.
|
||||
*/
|
||||
public const POSTER_ID_POST_META_KEY = 'web_stories_poster_id';
|
||||
|
||||
/**
|
||||
* Media_Source_Taxonomy instance.
|
||||
*
|
||||
* @var Media_Source_Taxonomy Experiments instance.
|
||||
*/
|
||||
protected Media_Source_Taxonomy $media_source_taxonomy;
|
||||
|
||||
/**
|
||||
* Poster constructor.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @param Media_Source_Taxonomy $media_source_taxonomy Media_Source_Taxonomy instance.
|
||||
*/
|
||||
public function __construct( Media_Source_Taxonomy $media_source_taxonomy ) {
|
||||
$this->media_source_taxonomy = $media_source_taxonomy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->register_meta();
|
||||
add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
|
||||
add_action( 'delete_attachment', [ $this, 'delete_video_poster' ] );
|
||||
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ], 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register meta for attachment post type.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*/
|
||||
public function register_meta(): void {
|
||||
register_meta(
|
||||
'post',
|
||||
self::POSTER_ID_POST_META_KEY,
|
||||
[
|
||||
'sanitize_callback' => 'absint',
|
||||
'type' => 'integer',
|
||||
'description' => __( 'Attachment id of generated poster image.', 'web-stories' ),
|
||||
'show_in_rest' => true,
|
||||
'default' => 0,
|
||||
'single' => true,
|
||||
'object_subtype' => 'attachment',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers additional REST API fields upon API initialization.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function rest_api_init(): void {
|
||||
register_rest_field(
|
||||
'attachment',
|
||||
'featured_media',
|
||||
[
|
||||
'schema' => [
|
||||
'description' => __( 'The ID of the featured media for the object.', 'web-stories' ),
|
||||
'type' => 'integer',
|
||||
'context' => [ 'view', 'edit', 'embed' ],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
register_rest_field(
|
||||
'attachment',
|
||||
'featured_media_src',
|
||||
[
|
||||
'get_callback' => [ $this, 'get_callback_featured_media_src' ],
|
||||
'schema' => [
|
||||
'description' => __( 'URL, width and height.', 'web-stories' ),
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'src' => [
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
],
|
||||
'width' => [
|
||||
'type' => 'integer',
|
||||
],
|
||||
'height' => [
|
||||
'type' => 'integer',
|
||||
],
|
||||
'generated' => [
|
||||
'type' => 'boolean',
|
||||
],
|
||||
],
|
||||
'context' => [ 'view', 'edit', 'embed' ],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attachment source for featured media.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array<string, mixed> $prepared Prepared data before response.
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function get_callback_featured_media_src( array $prepared ): array {
|
||||
/**
|
||||
* Featured media ID.
|
||||
*
|
||||
* @var int|null $id
|
||||
*/
|
||||
$id = $prepared['featured_media'] ?? null;
|
||||
$image = [];
|
||||
if ( $id ) {
|
||||
$image = $this->get_thumbnail_data( $id );
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the attachment data prepared for JavaScript.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array<string, mixed>|mixed $response Array of prepared attachment data.
|
||||
* @param WP_Post $attachment Attachment object.
|
||||
* @return array<string, mixed>|mixed $response
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @phpstan-return ($response is array<T> ? array<T> : mixed)
|
||||
*/
|
||||
public function wp_prepare_attachment_for_js( $response, WP_Post $attachment ) {
|
||||
if ( ! \is_array( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
if ( 'video' === $response['type'] ) {
|
||||
$thumbnail_id = (int) get_post_thumbnail_id( $attachment );
|
||||
$image = '';
|
||||
if ( 0 !== $thumbnail_id ) {
|
||||
$image = $this->get_thumbnail_data( $thumbnail_id );
|
||||
}
|
||||
|
||||
$response['featured_media'] = $thumbnail_id;
|
||||
$response['featured_media_src'] = $image;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get poster image data.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int $thumbnail_id Attachment ID.
|
||||
* @return array{src?: string, width?: int, height?: int, generated?: bool}
|
||||
*/
|
||||
public function get_thumbnail_data( int $thumbnail_id ): array {
|
||||
$img_src = wp_get_attachment_image_src( $thumbnail_id, 'full' );
|
||||
|
||||
if ( ! $img_src ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
[ $src, $width, $height ] = $img_src;
|
||||
$generated = $this->is_poster( $thumbnail_id );
|
||||
return compact( 'src', 'width', 'height', 'generated' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes associated poster image when a video is deleted.
|
||||
*
|
||||
* This prevents the poster image from becoming an orphan because it is not
|
||||
* displayed anywhere in WordPress or the story editor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param int $attachment_id ID of the attachment to be deleted.
|
||||
*/
|
||||
public function delete_video_poster( int $attachment_id ): void {
|
||||
/**
|
||||
* Post ID.
|
||||
*
|
||||
* @var int|string $post_id
|
||||
*/
|
||||
$post_id = get_post_meta( $attachment_id, self::POSTER_ID_POST_META_KEY, true );
|
||||
|
||||
if ( empty( $post_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Used in favor of slow meta queries.
|
||||
$is_poster = $this->is_poster( (int) $post_id );
|
||||
if ( $is_poster ) {
|
||||
wp_delete_attachment( (int) $post_id, true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin uninstall.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*/
|
||||
public function on_plugin_uninstall(): void {
|
||||
delete_post_meta_by_key( self::POSTER_ID_POST_META_KEY );
|
||||
delete_post_meta_by_key( self::POSTER_POST_META_KEY );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper util to check if attachment is a poster.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param int $post_id Attachment ID.
|
||||
*/
|
||||
protected function is_poster( int $post_id ): bool {
|
||||
$terms = get_the_terms( $post_id, $this->media_source_taxonomy->get_taxonomy_slug() );
|
||||
if ( \is_array( $terms ) && ! empty( $terms ) ) {
|
||||
$slugs = wp_list_pluck( $terms, 'slug' );
|
||||
|
||||
return \in_array( $this->media_source_taxonomy::TERM_POSTER_GENERATION, $slugs, true );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
138
wp-content/plugins/web-stories/includes/Media/Video/Trimming.php
Normal file
138
wp-content/plugins/web-stories/includes/Media/Video/Trimming.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Trimming
|
||||
*
|
||||
* @link https://github.com/googleforcreators/web-stories-wp
|
||||
*
|
||||
* @copyright 2021 Google LLC
|
||||
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copyright 2021 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Google\Web_Stories\Media\Video;
|
||||
|
||||
use Google\Web_Stories\Infrastructure\HasMeta;
|
||||
use Google\Web_Stories\Infrastructure\PluginUninstallAware;
|
||||
use Google\Web_Stories\Service_Base;
|
||||
|
||||
/**
|
||||
* Class Trimming
|
||||
*/
|
||||
class Trimming extends Service_Base implements HasMeta, PluginUninstallAware {
|
||||
|
||||
/**
|
||||
* The trim video post meta key.
|
||||
*/
|
||||
public const TRIM_POST_META_KEY = 'web_stories_trim_data';
|
||||
|
||||
/**
|
||||
* Is trim.
|
||||
*/
|
||||
public const TRIM_DATA_KEY = 'trim_data';
|
||||
|
||||
/**
|
||||
* Register.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->register_meta();
|
||||
|
||||
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register meta for attachment post type.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*/
|
||||
public function register_meta(): void {
|
||||
register_meta(
|
||||
'post',
|
||||
self::TRIM_POST_META_KEY,
|
||||
[
|
||||
'type' => 'object',
|
||||
'description' => __( 'Video trim data.', 'web-stories' ),
|
||||
'show_in_rest' => [
|
||||
'schema' => [
|
||||
'properties' => [
|
||||
'original' => [
|
||||
'description' => __( 'Original attachment id', 'web-stories' ),
|
||||
'type' => 'integer',
|
||||
],
|
||||
'start' => [
|
||||
'description' => __( 'Start time.', 'web-stories' ),
|
||||
'type' => 'string',
|
||||
],
|
||||
'end' => [
|
||||
'description' => __( 'End time.', 'web-stories' ),
|
||||
'type' => 'string',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'default' => [
|
||||
'original' => 0,
|
||||
],
|
||||
'single' => true,
|
||||
'object_subtype' => 'attachment',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the attachment data prepared for JavaScript.
|
||||
*
|
||||
* @since 1.12.0
|
||||
*
|
||||
* @param array|mixed $response Array of prepared attachment data.
|
||||
* @return array|mixed Response data.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @phpstan-return ($response is array<T> ? array<T> : mixed)
|
||||
*/
|
||||
public function wp_prepare_attachment_for_js( $response ) {
|
||||
if ( ! \is_array( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
if ( 'video' === $response['type'] ) {
|
||||
/**
|
||||
* Post ID.
|
||||
*
|
||||
* @var int $post_id
|
||||
*/
|
||||
$post_id = $response['id'];
|
||||
|
||||
$response[ self::TRIM_DATA_KEY ] = get_post_meta( $post_id, self::TRIM_POST_META_KEY, true );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Act on plugin uninstall.
|
||||
*
|
||||
* @since 1.26.0
|
||||
*/
|
||||
public function on_plugin_uninstall(): void {
|
||||
delete_post_meta_by_key( self::TRIM_POST_META_KEY );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user