-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
110 lines (90 loc) · 3.2 KB
/
plugin.php
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
<?php
/**
* Plugin Name: Wistia Embed Block
* Description: Embed a Wistia video.
* Plugin URI: https://github.com/s3rgiosan/wistia-embed-block
* Requires at least: 6.4
* Requires PHP: 7.4
* Version: 1.0.0
* Author: Sérgio Santos
* Author URI: https://s3rgiosan.dev/?utm_source=wp-plugins&utm_medium=wistia-embed-block&utm_campaign=author-uri
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wistia-embed-block
*
* @package WistiaEmbedBlock
*/
namespace s3rgiosan\WP\WistiaEmbedBlock;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'S3RGIOSAN_WISTIA_EMBED_BLOCK_PATH', plugin_dir_path( __FILE__ ) );
define( 'S3RGIOSAN_WISTIA_EMBED_BLOCK_URL', plugin_dir_url( __FILE__ ) );
// Define the Wistia embed pattern.
const WISTIA_EMBED_PATTERN = '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#';
if ( file_exists( S3RGIOSAN_WISTIA_EMBED_BLOCK_PATH . 'vendor/autoload.php' ) ) {
require_once S3RGIOSAN_WISTIA_EMBED_BLOCK_PATH . 'vendor/autoload.php';
}
/**
* Add Wistia as an oEmbed provider.
*
* @return void
*/
function add_oembed_provider() {
wp_oembed_add_provider(
WISTIA_EMBED_PATTERN,
'https://fast.wistia.com/oembed',
true
);
}
add_action( 'init', __NAMESPACE__ . '\add_oembed_provider' );
/**
* Enqueue the Wistia embed block variation.
*
* @return void
*/
function enqueue_block_editor_assets() {
$asset_file = sprintf(
'%s/build/index.asset.php',
untrailingslashit( S3RGIOSAN_WISTIA_EMBED_BLOCK_PATH )
);
$asset = file_exists( $asset_file ) ? require $asset_file : null;
$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : [];
$version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $asset_file );
wp_enqueue_script(
'wistia-embed-block',
sprintf(
'%s/build/index.js',
untrailingslashit( S3RGIOSAN_WISTIA_EMBED_BLOCK_URL )
),
$dependencies,
$version,
true
);
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_editor_assets' );
/**
* Filter the REST response to change the provider name.
*
* @param \WP_REST_Response|\WP_HTTP_Response|\WP_Error|mixed $response Result to send to the client.
* @param array $handler Route handler used for the request.
* @param \WP_REST_Request $request Request used to generate the response.
* @return \WP_REST_Response|\WP_HTTP_Response|\WP_Error|mixed
*/
function update_provider_name( $response, $handler, $request ) {
// Ignore requests that are not for the oEmbed proxy.
if ( false === strpos( $request->get_route(), '/oembed/1.0/proxy' ) ) {
return $response;
}
$params = $request->get_params();
// Only update the provider name for Wistia oEmbeds.
if ( ! isset( $params['url'] ) || ! preg_match( WISTIA_EMBED_PATTERN, $params['url'] ) ) {
return $response;
}
if ( is_object( $response ) && isset( $response->provider_name ) ) {
$response->provider_name = 'Wistia';
}
return $response;
}
add_filter( 'rest_request_after_callbacks', __NAMESPACE__ . '\update_provider_name', 10, 3 );