-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProvider_Command.php
More file actions
219 lines (198 loc) · 6.51 KB
/
Copy pathProvider_Command.php
File metadata and controls
219 lines (198 loc) · 6.51 KB
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
<?php
namespace WP_CLI\Embeds;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI\Utils;
use WP_CLI_Command;
/**
* Retrieves oEmbed providers.
*
* ## EXAMPLES
*
* # List format,endpoint fields of available providers.
* $ wp embed provider list
* +------------------------------+-----------------------------------------+
* | format | endpoint |
* +------------------------------+-----------------------------------------+
* | #https?://youtu\.be/.*#i | https://www.youtube.com/oembed |
* | #https?://flic\.kr/.*#i | https://www.flickr.com/services/oembed/ |
* | #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/ |
*
* # Get the matching provider for the URL.
* $ wp embed provider match https://www.youtube.com/watch?v=dQw4w9WgXcQ
* https://www.youtube.com/oembed
*
* @package wp-cli
*/
class Provider_Command extends WP_CLI_Command {
protected $default_fields = array(
'format',
'endpoint',
);
/**
* Lists all available oEmbed providers.
*
* ## OPTIONS
*
* [--field=<field>]
* : Display the value of a single field
*
* [--fields=<fields>]
* : Limit the output to specific fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* ---
*
* [--force-regex]
* : Turn the asterisk-type provider URLs into regexes.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each provider:
*
* * format
* * endpoint
*
* This field is optionally available:
*
* * regex
*
* ## EXAMPLES
*
* # List format,endpoint fields of available providers.
* $ wp embed provider list --fields=format,endpoint
* +------------------------------+-----------------------------------------+
* | format | endpoint |
* +------------------------------+-----------------------------------------+
* | #https?://youtu\.be/.*#i | https://www.youtube.com/oembed |
* | #https?://flic\.kr/.*#i | https://www.flickr.com/services/oembed/ |
* | #https?://wordpress\.tv/.*#i | https://wordpress.tv/oembed/ |
*
* @subcommand list
*
* @param string[] $args Positional arguments. Unused.
* @param array{field?: string, fields?: string, format: 'table'|'csv'|'json', 'force-regex'?: bool} $assoc_args Associative arguments.
*/
public function list_providers( $args, $assoc_args ) {
// We use _wp_oembed_get_object to get the singleton, which contains all the registered providers.
$oembed = _wp_oembed_get_object();
$force_regex = Utils\get_flag_value( $assoc_args, 'force-regex' );
$providers = array();
foreach ( (array) $oembed->providers as $matchmask => $data ) {
list( $providerurl, $regex ) = $data;
// Turn the asterisk-type provider URLs into regex
if ( $force_regex && ! $regex ) {
$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
}
$providers[] = array(
'format' => $matchmask,
'endpoint' => $providerurl,
'regex' => $regex ? '1' : '0',
);
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $providers );
}
/**
* Gets the matching provider for a given URL.
*
* ## OPTIONS
*
* <url>
* : URL to retrieve provider for.
*
* [--discover]
* : Whether to use oEmbed discovery or not. Defaults to true.
*
* [--limit-response-size=<size>]
* : Limit the size of the resulting HTML when using discovery. Default 150 KB (the standard WordPress limit). Not compatible with 'no-discover'.
*
* [--link-type=<json|xml>]
* : Whether to accept only a certain link type when using discovery. Defaults to any (json or xml), preferring json. Not compatible with 'no-discover'.
* ---
* options:
* - json
* - xml
* ---
*
* ## EXAMPLES
*
* # Get the matching provider for the URL.
* $ wp embed provider match https://www.youtube.com/watch?v=dQw4w9WgXcQ
* https://www.youtube.com/oembed
*
* @subcommand match
*
* @param array{0: string} $args Positional arguments.
* @param array{discover?: bool, 'limit-response-size'?: string, 'link-type'?: 'json'|'xml', } $assoc_args Associative arguments.
*/
public function match_provider( $args, $assoc_args ) {
$oembed = _wp_oembed_get_object();
$url = $args[0];
$discover = Utils\get_flag_value( $assoc_args, 'discover', true );
$response_size_limit = Utils\get_flag_value( $assoc_args, 'limit-response-size' );
$link_type = Utils\get_flag_value( $assoc_args, 'link-type' );
if ( ! $discover && ( null !== $response_size_limit || null !== $link_type ) ) {
if ( null !== $response_size_limit && null !== $link_type ) {
$msg = "The 'limit-response-size' and 'link-type' options can only be used with discovery.";
} elseif ( null !== $response_size_limit ) {
$msg = "The 'limit-response-size' option can only be used with discovery.";
} else {
$msg = "The 'link-type' option can only be used with discovery.";
}
WP_CLI::error( $msg );
}
if ( $response_size_limit ) {
add_filter(
'oembed_remote_get_args',
function ( $args ) use ( $response_size_limit ) {
$args['limit_response_size'] = (int) $response_size_limit;
return $args;
}
);
}
if ( $link_type ) {
// Filter discovery response.
add_filter(
'oembed_linktypes',
function ( $linktypes ) use ( $link_type ) {
foreach ( $linktypes as $mime_type => $linktype_format ) {
if ( $link_type !== $linktype_format ) {
unset( $linktypes[ $mime_type ] );
}
}
return $linktypes;
}
);
}
$oembed_args = array(
'discover' => $discover,
);
$provider = $oembed->get_provider( $url, $oembed_args );
if ( ! $provider ) {
if ( ! $discover ) {
WP_CLI::error( 'No oEmbed provider found for given URL. Maybe try discovery?' );
} else {
WP_CLI::error( 'No oEmbed provider found for given URL.' );
}
}
WP_CLI::line( $provider );
}
/**
* Get Formatter object based on supplied parameters.
*
* @param array $assoc_args Parameters passed to command. Determines formatting.
* @return \WP_CLI\Formatter
*/
protected function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->default_fields );
}
}