26
26
*/
27
27
28
28
class WP_DocumentCloud {
29
+
30
+ const CACHING_ENABLED = false ,
31
+ OEMBED_PROVIDER = 'https://www.documentcloud.org/api/oembed.{format} ' ,
32
+ OEMBED_RESOURCE_DOMAIN = 'www.documentcloud.org ' ;
29
33
30
34
function __construct () {
31
35
32
36
add_action ('init ' , array (&$ this , 'register_dc_oembed_provider ' ));
33
- add_shortcode ('documentcloud ' , array (&$ this , 'handle_dc_shortcode_with_caching ' ));
37
+ add_shortcode ('documentcloud ' , array (&$ this , 'handle_dc_shortcode ' ));
34
38
add_filter ('oembed_fetch_url ' , array (&$ this , 'add_dc_arguments ' ), 10 , 3 );
35
39
36
40
// Setup TinyMCE shortcode-generation plugin
@@ -51,17 +55,15 @@ function register_dc_oembed_provider() {
51
55
/*
52
56
Hello developer. If you wish to test this plugin against your
53
57
local installation of DocumentCloud (with its own testing
54
- domain), uncomment-out the next three lines and replace
55
- `[your_domain]` with, uh, your domain. You'll also want to keep
56
- the provider HTTP unless cURL can happily access your domain via
57
- HTTPS. Please remember not to commit these back to the repo .
58
+ domain), set the OEMBED_PROVIDER and OEMBED_RESOURCE_DOMAIN
59
+ constants above to your local testing domain. You'll also want
60
+ to uncomment the next line to let WordPress connect to local
61
+ domains .
58
62
*/
59
- // add_filter( 'http_request_host_is_external', '__return_true' );
60
- // wp_oembed_add_provider('http://[your_domain]/documents/*','http://[your_domain]/api/oembed.{format}');
61
- // wp_oembed_add_provider('https://[your_domain]/documents/*','http://[your_domain]/api/oembed.{format}');
63
+ // add_filter( 'http_request_host_is_external', '__return_true');
62
64
63
- wp_oembed_add_provider (' http://www.documentcloud.org /documents/* ' , ' https://www.documentcloud.org/api/oembed.{format} ' );
64
- wp_oembed_add_provider (' https://www.documentcloud.org /documents/* ' , ' https://www.documentcloud.org/api/oembed.{format} ' );
65
+ wp_oembed_add_provider (" http:// " . WP_DocumentCloud:: OEMBED_RESOURCE_DOMAIN . " /documents/* " , WP_DocumentCloud:: OEMBED_PROVIDER );
66
+ wp_oembed_add_provider (" https:// " . WP_DocumentCloud:: OEMBED_RESOURCE_DOMAIN . " /documents/* " , WP_DocumentCloud:: OEMBED_PROVIDER );
65
67
}
66
68
67
69
function default_dc_atts () {
@@ -136,63 +138,21 @@ function handle_dc_shortcode($atts) {
136
138
return '' ;
137
139
}
138
140
else {
139
- $ url = $ filtered_atts ['url ' ] = "https://www.documentcloud.org /documents/ {$ atts ['id ' ]}.html " ;
141
+ $ url = $ filtered_atts ['url ' ] = "https:// " . WP_DocumentCloud:: OEMBED_RESOURCE_DOMAIN . " /documents/ {$ atts ['id ' ]}.html " ;
140
142
}
141
143
} else {
142
144
$ url = $ atts ['url ' ];
143
145
}
144
146
145
- return wp_oembed_get ($ url , $ filtered_atts );
146
- }
147
-
148
- // This is an exact clone of `handle_dc_shortcode`, except that it
149
- // lets WordPress cache the result of the oEmbed call. Thanks to
150
- // http://bit.ly/1HykA0U for this pattern.
151
- function handle_dc_shortcode_with_caching ($ atts ) {
152
- global $ wp_embed ;
153
-
154
- $ filtered_atts = shortcode_atts ($ this ->default_dc_atts (), $ atts );
155
-
156
- // This is a tricky bit of logic that ends up:
157
- // 1. Allowing both `width/height` and `maxwidth/maxheight` as
158
- // acceptable shortcode parameters;
159
- // 2. Only sending `maxwidth/maxheight` to the oEmbed service;
160
- // 3. Respecting the user's settings
161
- // To understand it, you must deeply understand the flow of
162
- // data through the WordPress bowels, or at least misunderstand
163
- // it in the same way we do. It could likely be cleaned up,
164
- // but should be WELL TESTED if so.
165
- if (isset ($ atts ['maxheight ' ])) {
166
- $ filtered_atts ['maxheight ' ] = $ atts ['maxheight ' ];
167
- } else if (isset ($ atts ['height ' ])) {
168
- $ filtered_atts ['maxheight ' ] = $ atts ['height ' ];
147
+ if (WP_DocumentCloud::CACHING_ENABLED ) {
148
+ // This lets WordPress cache the result of the oEmbed call.
149
+ // Thanks to http://bit.ly/1HykA0U for this pattern.
150
+ global $ wp_embed ;
151
+ return $ wp_embed ->shortcode ($ filtered_atts , $ url );
169
152
} else {
170
- $ filtered_atts ['maxheight ' ] = get_option ('documentcloud_default_height ' , 600 );
171
- }
172
- if (isset ($ atts ['maxwidth ' ])) {
173
- $ filtered_atts ['maxwidth ' ] = $ atts ['maxwidth ' ];
174
- } else if (isset ($ atts ['width ' ])) {
175
- $ filtered_atts ['maxwidth ' ] = $ atts ['width ' ];
176
- } else {
177
- $ filtered_atts ['maxwidth ' ] = get_option ('documentcloud_default_width ' , 620 );
178
- }
179
-
180
- // Either the `url` or `id` attributes are required, but `id`
181
- // is only supported for backwards compatibility. If it's used,
182
- // we force this to embed a document. I.e., it can't be used
183
- // for embedding notes, pages, or other non-document resources.
184
- if (!$ atts ['url ' ]) {
185
- if (!$ atts ['id ' ]) {
186
- return '' ;
187
- }
188
- else {
189
- $ url = $ filtered_atts ['url ' ] = "https://www.documentcloud.org/documents/ {$ atts ['id ' ]}.html " ;
190
- }
191
- } else {
192
- $ url = $ atts ['url ' ];
153
+ return wp_oembed_get ($ url , $ filtered_atts );
193
154
}
194
155
195
- return $ wp_embed ->shortcode ($ filtered_atts , $ url );
196
156
}
197
157
198
158
// TinyMCE and settings page
0 commit comments