Skip to content

Commit fdbb75a

Browse files
authored
Merge pull request #84 from justinmaurerdotdev/list-registered-providers
2 parents b93f5b0 + 7349a10 commit fdbb75a

2 files changed

Lines changed: 324 additions & 2 deletions

File tree

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
Feature: Register and use custom oEmbed providers
2+
3+
Background:
4+
Given a WP install
5+
And a filter-providers.php file:
6+
"""
7+
<?php
8+
WP_CLI::add_wp_hook( 'init', function () {
9+
wp_oembed_add_provider( 'http://example.com/*', 'http://example.com/api/oembed.{format}' );
10+
});
11+
"""
12+
13+
Scenario: List oEmbed providers after registration
14+
When I run `wp embed provider list --require=filter-providers.php`
15+
And save STDOUT as {DEFAULT_STDOUT}
16+
Then STDOUT should contain:
17+
"""
18+
format
19+
"""
20+
And STDOUT should contain:
21+
"""
22+
endpoint
23+
"""
24+
And STDOUT should not contain:
25+
"""
26+
regex
27+
"""
28+
And STDOUT should contain:
29+
"""
30+
http://example.com/*
31+
"""
32+
And STDOUT should contain:
33+
"""
34+
http://example.com/api/oembed.{format}
35+
"""
36+
And STDOUT should contain:
37+
"""
38+
youtube\.com/watch.*
39+
"""
40+
And STDOUT should contain:
41+
"""
42+
//www.youtube.com/oembed
43+
"""
44+
And STDOUT should contain:
45+
"""
46+
flickr\.com/
47+
"""
48+
And STDOUT should contain:
49+
"""
50+
flickr.com
51+
"""
52+
And STDOUT should contain:
53+
"""
54+
twitter\.com/
55+
"""
56+
And STDOUT should contain:
57+
"""
58+
twitter.com
59+
"""
60+
And STDOUT should contain:
61+
"""
62+
\.spotify\.com/
63+
"""
64+
And STDOUT should contain:
65+
"""
66+
spotify.com
67+
"""
68+
69+
When I run `wp embed provider list --fields=format,endpoint --require=filter-providers.php`
70+
Then STDOUT should be:
71+
"""
72+
{DEFAULT_STDOUT}
73+
"""
74+
75+
When I run `wp embed provider list --force-regex --require=filter-providers.php`
76+
Then STDOUT should contain:
77+
"""
78+
#https?\://example\.com/(.+)#i
79+
"""
80+
And STDOUT should contain:
81+
"""
82+
http://example.com/api/oembed.{format}
83+
"""
84+
And STDOUT should not contain:
85+
"""
86+
http://example.com/*
87+
"""
88+
And STDOUT should match /^#http/m
89+
90+
When I run `wp embed provider list --fields=format`
91+
Then STDOUT should contain:
92+
"""
93+
format
94+
"""
95+
And STDOUT should not contain:
96+
"""
97+
endpoint
98+
"""
99+
And STDOUT should contain:
100+
"""
101+
youtube\.com/watch.*
102+
"""
103+
And STDOUT should not contain:
104+
"""
105+
youtube.com
106+
"""
107+
108+
When I run `wp embed provider list --field=format`
109+
Then STDOUT should not contain:
110+
"""
111+
format
112+
"""
113+
And STDOUT should not contain:
114+
"""
115+
endpoint
116+
"""
117+
And STDOUT should contain:
118+
"""
119+
youtube\.com/watch.*
120+
"""
121+
And STDOUT should not contain:
122+
"""
123+
youtube.com
124+
"""
125+
126+
When I run `wp embed provider list --field=regex`
127+
Then STDOUT should match /^(?:(?:1|0)\n)+$/
128+
129+
@require-wp-4.0
130+
Scenario: Match an oEmbed provider
131+
# Provider not requiring discovery
132+
When I run `wp embed provider match https://www.youtube.com/watch?v=dQw4w9WgXcQ`
133+
Then STDOUT should contain:
134+
"""
135+
//www.youtube.com/oembed
136+
"""
137+
138+
# Provider requiring discovery
139+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
140+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus`
141+
And save STDOUT as {DEFAULT_STDOUT}
142+
Then the return code should be 0
143+
And STDERR should not contain:
144+
"""
145+
Error:
146+
"""
147+
And STDOUT should contain:
148+
"""
149+
audio.com/
150+
"""
151+
And STDOUT should contain:
152+
"""
153+
json
154+
"""
155+
And STDOUT should not contain:
156+
"""
157+
xml
158+
"""
159+
160+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
161+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus --link-type=json`
162+
Then the return code should be 0
163+
And STDERR should not contain:
164+
"""
165+
Error:
166+
"""
167+
And STDOUT should be:
168+
"""
169+
{DEFAULT_STDOUT}
170+
"""
171+
172+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
173+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus --link-type=xml`
174+
Then the return code should be 0
175+
And STDERR should not contain:
176+
"""
177+
Error:
178+
"""
179+
And STDOUT should not contain:
180+
"""
181+
{DEFAULT_STDOUT}
182+
"""
183+
And STDOUT should contain:
184+
"""
185+
audio.com/
186+
"""
187+
And STDOUT should contain:
188+
"""
189+
xml
190+
"""
191+
And STDOUT should not contain:
192+
"""
193+
json
194+
"""
195+
196+
# Depends on `oembed_remote_get_args` filter introduced in WP 4.0 https://core.trac.wordpress.org/ticket/23442
197+
@require-wp-4.0
198+
Scenario: Discover a provider with limited response size
199+
When I run `wp embed provider match https://audio.com/audio-com/collections/ambient-focus`
200+
Then save STDOUT as {DEFAULT_STDOUT}
201+
202+
# Response limit too small
203+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus --limit-response-size=10`
204+
Then the return code should be 1
205+
And STDERR should be:
206+
"""
207+
Error: No oEmbed provider found for given URL.
208+
"""
209+
210+
# Response limit big enough
211+
When I run `wp embed provider match https://audio.com/audio-com/collections/ambient-focus --limit-response-size=50000`
212+
Then STDOUT should be:
213+
"""
214+
{DEFAULT_STDOUT}
215+
"""
216+
217+
Scenario: Fail to match an oEmbed provider
218+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover`
219+
Then the return code should be 1
220+
And STDERR should be:
221+
"""
222+
Error: No oEmbed provider found for given URL. Maybe try discovery?
223+
"""
224+
And STDOUT should be empty
225+
226+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ`
227+
Then the return code should be 1
228+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "contain" to ignore these.
229+
And STDERR should contain:
230+
"""
231+
Error: No oEmbed provider found for given URL.
232+
"""
233+
And STDOUT should be empty
234+
235+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ --discover`
236+
Then the return code should be 1
237+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "contain" to ignore these.
238+
And STDERR should contain:
239+
"""
240+
Error: No oEmbed provider found for given URL.
241+
"""
242+
And STDOUT should be empty
243+
244+
@require-wp-4.0
245+
Scenario: Only match an oEmbed provider if discover
246+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus --no-discover`
247+
Then the return code should be 1
248+
And STDERR should be:
249+
"""
250+
Error: No oEmbed provider found for given URL. Maybe try discovery?
251+
"""
252+
And STDOUT should be empty
253+
254+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
255+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus`
256+
Then the return code should be 0
257+
And STDERR should not contain:
258+
"""
259+
Error:
260+
"""
261+
And STDOUT should contain:
262+
"""
263+
audio.com/
264+
"""
265+
And STDOUT should contain:
266+
"""
267+
collection
268+
"""
269+
270+
# Old versions of WP_oEmbed can trigger PHP "Only variables should be passed by reference" notices on discover so use "try" to cater for these.
271+
When I try `wp embed provider match https://audio.com/audio-com/collections/ambient-focus --discover`
272+
Then the return code should be 0
273+
And STDERR should not contain:
274+
"""
275+
Error:
276+
"""
277+
And STDOUT should contain:
278+
"""
279+
audio.com/
280+
"""
281+
282+
Scenario: Match custom oEmbed provider after registration
283+
When I run `wp embed provider match http://example.com/foo --require=filter-providers.php`
284+
Then STDOUT should be:
285+
"""
286+
http://example.com/api/oembed.json
287+
"""
288+
289+
Scenario: Incompatible or wrong options
290+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --limit-response-size=50000`
291+
Then the return code should be 1
292+
And STDERR should be:
293+
"""
294+
Error: The 'limit-response-size' option can only be used with discovery.
295+
"""
296+
And STDOUT should be empty
297+
298+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --link-type=json`
299+
Then the return code should be 1
300+
And STDERR should be:
301+
"""
302+
Error: The 'link-type' option can only be used with discovery.
303+
"""
304+
And STDOUT should be empty
305+
306+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --limit-response-size=50000 --link-type=json`
307+
Then the return code should be 1
308+
And STDERR should be:
309+
"""
310+
Error: The 'limit-response-size' and 'link-type' options can only be used with discovery.
311+
"""
312+
And STDOUT should be empty
313+
314+
When I try `wp embed provider match https://www.example.com/watch?v=dQw4w9WgXcQ --no-discover --link-type=blah`
315+
Then the return code should be 1
316+
And STDERR should contain:
317+
"""
318+
Error: Parameter errors:
319+
Invalid value specified for 'link-type'
320+
"""
321+
And STDOUT should be empty

‎src/Provider_Command.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class Provider_Command extends WP_CLI_Command {
8686
*/
8787
public function list_providers( $args, $assoc_args ) {
8888

89-
$oembed = new \WP_oEmbed();
89+
// We use _wp_oembed_get_object to get the singleton, which contains all the registered providers.
90+
$oembed = _wp_oembed_get_object();
9091

9192
$force_regex = Utils\get_flag_value( $assoc_args, 'force-regex' );
9293

@@ -146,7 +147,7 @@ public function list_providers( $args, $assoc_args ) {
146147
* @param array{discover?: bool, 'limit-response-size'?: string, 'link-type'?: 'json'|'xml', } $assoc_args Associative arguments.
147148
*/
148149
public function match_provider( $args, $assoc_args ) {
149-
$oembed = new \WP_oEmbed();
150+
$oembed = _wp_oembed_get_object();
150151

151152
$url = $args[0];
152153
$discover = Utils\get_flag_value( $assoc_args, 'discover', true );

0 commit comments

Comments
 (0)