Skip to content

Commit 7b10de0

Browse files
committed
Store settings as constants and reduce redundancy
* Create class constants for regularly-modifiable settings (mostly for testing) * Consolidate cached and non-cached shortcode call and just toggle where it mattesr Affects #16
1 parent aa8af32 commit 7b10de0

File tree

1 file changed

+19
-59
lines changed

1 file changed

+19
-59
lines changed

documentcloud.php

+19-59
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
*/
2727

2828
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';
2933

3034
function __construct() {
3135

3236
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'));
3438
add_filter('oembed_fetch_url', array(&$this, 'add_dc_arguments'), 10, 3);
3539

3640
// Setup TinyMCE shortcode-generation plugin
@@ -51,17 +55,15 @@ function register_dc_oembed_provider() {
5155
/*
5256
Hello developer. If you wish to test this plugin against your
5357
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.
5862
*/
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');
6264

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);
6567
}
6668

6769
function default_dc_atts() {
@@ -136,63 +138,21 @@ function handle_dc_shortcode($atts) {
136138
return '';
137139
}
138140
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";
140142
}
141143
} else {
142144
$url = $atts['url'];
143145
}
144146

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);
169152
} 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);
193154
}
194155

195-
return $wp_embed->shortcode($filtered_atts, $url);
196156
}
197157

198158
// TinyMCE and settings page

0 commit comments

Comments
 (0)