forked from argoproject/navis-documentcloud
-
Notifications
You must be signed in to change notification settings - Fork 14
/
documentcloud.php
423 lines (368 loc) · 14.1 KB
/
documentcloud.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/***
* Plugin Name: DocumentCloud
* Plugin URI: https://www.documentcloud.org/
* Description: Embed DocumentCloud resources in WordPress content.
* Version: 0.4.4-dev
* Authors: Chris Amico, Justin Reese
* License: GPLv2
***/
/*
Copyright 2011 National Public Radio, Inc.
Copyright 2015 DocumentCloud, Investigative Reporters & Editors
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class WP_DocumentCloud {
// Plugin constants
const CACHING_ENABLED = true,
DEFAULT_EMBED_FULL_WIDTH = 940,
OEMBED_RESOURCE_DOMAIN = 'www.documentcloud.org',
OEMBED_PROVIDER = 'https://www.documentcloud.org/api/oembed.{format}',
DOCUMENT_PATTERN = '^(?P<protocol>https?):\/\/www\.documentcloud\.org\/documents\/(?P<document_slug>[0-9]+-[\p{L}\p{N}%-]+)';
/**
* Constructor.
*/
function __construct() {
// Check for conflicts with other DocumentCloud plugins.
// Not needed on WordPress VIP since no other DocumentCloud plugins exist.
if ( ! defined( 'WPCOM_IS_VIP_ENV' ) || ! WPCOM_IS_VIP_ENV ) {
add_action( 'admin_init', array( $this, 'check_dc_plugin_conflict' ) );
}
// Register the oEmbed provider
add_action( 'init', array( $this, 'register_dc_oembed_provider' ) );
// Set the textdomain for the plugin so it is translation compatible
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
// Only called when `[documentcloud]` shortcode is used
add_shortcode( 'documentcloud', array( $this, 'process_dc_shortcode' ) );
// Called just before oEmbed endpoint is hit
add_filter( 'oembed_fetch_url', array( $this, 'prepare_oembed_fetch' ), 10, 3 );
// Setup the settings page
add_action( 'admin_menu', array( $this, 'add_options_page' ) );
add_action( 'admin_init', array( $this, 'settings_init' ) );
}
/**
* Load plugin textdomain.
*/
function load_plugin_textdomain() {
load_plugin_textdomain( 'documentcloud' );
}
/**
* Check for conflicts with the Navis DocumentCloud plugin.
*/
function check_dc_plugin_conflict() {
if ( is_plugin_active( 'navis-documentcloud/navis-documentcloud.php' ) ) {
add_action( 'admin_notices', array( $this, 'dc_conflict_admin_notice' ) );
}
}
/**
* Create an admin notice when conflicts exist with Navis DocumentCloud.
*/
function dc_conflict_admin_notice() {
?>
<div class="error">
<p><?php echo wp_kses_post( __( '<b>Warning!</b> You have two conflicting DocumentCloud plugins activated. Please deactivate Navis DocumentCloud, which has been replaced by <a target="_blank" href="https://wordpress.org/plugins/documentcloud/">DocumentCloud</a>.', 'documentcloud' ) ); ?></p>
</div>
<?php
}
/**
* Register ourselves as an oEmbed provider. WordPress does NOT cURL the
* resource to inspect it for an oEmbed link tag; we have to tell it what
* our oEmbed endpoint looks like.
*/
function register_dc_oembed_provider() {
/*
Hello developer. If you wish to test this plugin against your
local installation of DocumentCloud (with its own testing
domain), set the OEMBED_PROVIDER and OEMBED_RESOURCE_DOMAIN
constants above to your local testing domain. You'll also want
to add the following line to your theme to let WordPress connect to local
domains:
add_filter( 'http_request_host_is_external', '__return_true');
*/
$oembed_resource_domain = apply_filters( 'documentcloud_oembed_resource_domain', WP_DocumentCloud::OEMBED_RESOURCE_DOMAIN );
$oembed_provider = apply_filters( 'documentcloud_oembed_provider', WP_DocumentCloud::OEMBED_PROVIDER );
wp_oembed_add_provider( 'http://' . $oembed_resource_domain . '/documents/*', $oembed_provider );
wp_oembed_add_provider( 'https://' . $oembed_resource_domain . '/documents/*', $oembed_provider );
}
/**
* Get the default sizes for DocumentCloud.
*
* @return array
*/
function get_default_sizes() {
$wp_embed_defaults = wp_embed_defaults();
$height = intval( get_option( 'documentcloud_default_height', $wp_embed_defaults['height'] ) );
$width = intval( get_option( 'documentcloud_default_width', $wp_embed_defaults['width'] ) );
$full_width = intval( get_option( 'documentcloud_full_width', WP_DocumentCloud::DEFAULT_EMBED_FULL_WIDTH ) );
return array(
'height' => $height,
'width' => $width,
'full_width' => $full_width,
);
}
/**
* Get the attribute defaults for the shortcode.
*
* @return array
*/
function get_default_atts() {
$default_sizes = $this->get_default_sizes();
return array(
'url' => null,
'container' => null,
'notes' => null,
'responsive_offset' => null,
'page' => null,
'note' => null,
'zoom' => null,
'search' => null,
'responsive' => null,
'sidebar' => null,
'text' => null,
'pdf' => null,
// The following defaults match the existing plugin, except
// `height/width` are prefixed `max*` per the oEmbed spec.
// You can still use `height/width` for backwards
// compatibility, but they'll be mapped to `max*`.
// Precedence (lower number == higher priority):
// 1. `width` on shortcode
// 2. `maxwidth` on shortcode
// 3. Settings > DocumentCloud > "Default embed width"
// 4. `wp_embed_defaults()['width']`
'maxheight' => $default_sizes['height'],
'maxwidth' => $default_sizes['width'],
'format' => 'normal',
);
}
/**
* Prepare the oEmbed fetch URL.
*
* @param string $provider
* @param string $url
* @param array $args
* @return string
*/
function prepare_oembed_fetch( $provider, $url, $args ) {
// Merge actual args with default attributes so that defaults are always
// sent to oEmbed endpoint
$default_atts = $this->get_default_atts();
$atts = array_merge( $default_atts, $args );
// Some resources (like notes) have multiple possible
// user-facing URLs. We recompose them into a single form.
$url = $this->clean_dc_url( $url );
// Send these to the oEmbed endpoint itself
$oembed_config_keys = array( 'maxheight', 'maxwidth' );
// Specifically *don't* include these on the embed config itself
$excluded_embed_config_keys = array( 'url', 'format', 'height', 'width', 'maxheight', 'maxwidth', 'discover' );
// Clean and prepare arguments
foreach ( $atts as $key => $value ) {
if ( in_array( $key, $oembed_config_keys ) ) {
$provider = add_query_arg( $key, $value, $provider );
}
if ( ! in_array( $key, $excluded_embed_config_keys ) ) {
// Without this check, `add_query_arg()` will treat values
// that are actually ID selectors, like `container=#foo`,
// as URL fragments and throw them at the end of the URL.
if ( 0 === strpos( $value, '#' ) ) {
$value = urlencode( $value );
}
$url = add_query_arg( $key, $value, $url );
}
}
$provider = add_query_arg( 'url', urlencode( $url ), $provider );
return $provider;
}
/**
* Create the DocumentCloud embed output from the shortcode.
*
* @param array $atts
* @return string
*/
function process_dc_shortcode( $atts ) {
$default_sizes = $this->get_default_sizes();
$default_atts = $this->get_default_atts();
// Smooshes together passed-in shortcode attrs with defaults
// and filters to only those we accept.
$filtered_atts = shortcode_atts( $default_atts, $atts );
// Either the `url` or `id` attributes are required, but `id`
// is only supported for backwards compatibility. If it's used,
// we force this to embed a document. I.e., `id` can't be used
// for embedding notes, pages, or other non-document resources.
if ( empty( $atts['url'] ) ) {
if ( empty( $atts['id'] ) ) {
return '';
} else {
$url = $filtered_atts['url'] = 'https://' . WP_DocumentCloud::OEMBED_RESOURCE_DOMAIN . "/documents/{$atts['id']}.html";
}
}
// `height/width` beat `maxheight/maxwidth`; see full precedence list in `get_default_atts()`.
if ( isset( $atts['height'] ) ) {
$filtered_atts['maxheight'] = $atts['height'];
}
if ( isset( $atts['width'] ) ) {
$filtered_atts['maxwidth'] = $atts['width'];
}
// `responsive` defaults true, but our responsive layout
// ignores width declarations. If a user indicates a width and
// hasn't otherwise specifically indicated `responsive='true'`,
// it's safe to assume they expect us to respect the width, so
// we disable the responsive flag.
if ( ( isset( $atts['width'] ) || isset( $atts['maxwidth'] ) ) && 'true' !== $atts['responsive'] ) {
$filtered_atts['responsive'] = 'false';
}
// If the format is set to wide, it blows away all other width
// settings.
if ( 'wide' === $filtered_atts['format'] ) {
$filtered_atts['maxwidth'] = $default_sizes['full_width'];
}
// For the benefit of some templates, notify template that
// we're requesting an asset wider than the default size.
global $post;
$is_wide = intval( $filtered_atts['maxwidth'] ) > $default_sizes['width'];
if ( apply_filters( 'documentcloud_caching_enabled', WP_DocumentCloud::CACHING_ENABLED ) ) {
// This lets WordPress cache the result of the oEmbed call.
// Thanks to http://bit.ly/1HykA0U for this pattern.
global $wp_embed;
$url = $filtered_atts['url'] = $this->clean_dc_url( $atts['url'] );
return $wp_embed->shortcode( $filtered_atts, $url );
} else {
return wp_oembed_get( $atts['url'], $filtered_atts );
}
}
/**
* Parse the DocumentCloud URL into its components.
*
* @param string $url
* @return array
*/
function parse_dc_url( $url ) {
$patterns = array(
// Document
'{' . WP_DocumentCloud::DOCUMENT_PATTERN . '\.html$}',
// Pages and page variants
'{' . WP_DocumentCloud::DOCUMENT_PATTERN . '.html#document\/p(?P<page_number>[0-9]+)$}',
'{' . WP_DocumentCloud::DOCUMENT_PATTERN . '\/pages\/(?P<page_number>[0-9]+)\.(html|js)$}',
// Notes and note variants
'{' . WP_DocumentCloud::DOCUMENT_PATTERN . '\/annotations\/(?P<note_id>[0-9]+)\.(html|js)$}',
'{' . WP_DocumentCloud::DOCUMENT_PATTERN . '.html#document\/p([0-9]+)/a(?P<note_id>[0-9]+)$}',
'{' . WP_DocumentCloud::DOCUMENT_PATTERN . '.html#annotation\/a(?P<note_id>[0-9]+)$}',
);
$elements = array();
foreach ( $patterns as $pattern ) {
$perfect_match = preg_match( $pattern, $url, $elements );
if ( $perfect_match ) {
break;
}
}
return $elements;
}
/**
* Clean the DocumentCloud URL.
*
* @param string $url
* @return string
*/
function clean_dc_url( $url ) {
$elements = $this->parse_dc_url( $url );
if ( isset( $elements['document_slug'] ) ) {
$url = "{$elements['protocol']}://" . WP_DocumentCloud::OEMBED_RESOURCE_DOMAIN . "/documents/{$elements['document_slug']}";
if ( isset( $elements['page_number'] ) ) {
$url .= "/pages/{$elements['page_number']}";
} else if ( isset( $elements['note_id'] ) ) {
$url .= "/annotations/{$elements['note_id']}";
}
$url .= '.html';
}
return $url;
}
/**
* Add the DocumentCloud options page.
*/
function add_options_page() {
if ( current_user_can( 'manage_options' ) ) {
add_options_page( 'DocumentCloud', 'DocumentCloud', 'manage_options', 'documentcloud', array( $this, 'render_options_page' ) );
}
}
/**
* Render the DocumentCloud options page.
*/
function render_options_page() {
?>
<h2><?php esc_html_e( 'DocumentCloud Options', 'documentcloud' ) ?></h2>
<form action="options.php" method="post">
<p><?php echo wp_kses_post( __( 'Any widths set here will only take effect if you set <code>responsive="false"</code> on an embed.', 'documentcloud' ) ) ?></p>
<?php settings_fields( 'documentcloud' ); ?>
<?php do_settings_sections( 'documentcloud' ); ?>
<p><input class="button-primary" name="<?php esc_attr_e( 'Submit', 'documentcloud' ); ?>" type="submit" value="<?php esc_attr_e( 'Save Changes', 'documentcloud' ); ?>" /></p>
</form>
<?php
}
/**
* Initialize settings for the DocumentCloud options page.
*/
function settings_init() {
if ( current_user_can( 'manage_options' ) ) {
add_settings_section(
'documentcloud',
'',
'__return_null',
'documentcloud'
);
add_settings_field(
'documentcloud_default_height',
__( 'Default embed height (px)', 'documentcloud' ),
array( $this, 'default_height_field' ),
'documentcloud',
'documentcloud'
);
register_setting( 'documentcloud', 'documentcloud_default_height' );
add_settings_field(
'documentcloud_default_width',
__( 'Default embed width (px)', 'documentcloud' ),
array( $this, 'default_width_field' ),
'documentcloud',
'documentcloud'
);
register_setting( 'documentcloud', 'documentcloud_default_width' );
add_settings_field(
'documentcloud_full_width',
__( 'Full-width embed width (px)', 'documentcloud' ),
array( $this, 'full_width_field' ),
'documentcloud',
'documentcloud'
);
register_setting( 'documentcloud', 'documentcloud_full_width' );
}
}
/**
* Render the default height field.
*/
function default_height_field() {
$default_sizes = $this->get_default_sizes();
echo '<input type="text" value="' . esc_attr( $default_sizes['height'] ) . '" name="documentcloud_default_height" />';
}
/**
* Render the default width field.
*/
function default_width_field() {
$default_sizes = $this->get_default_sizes();
echo '<input type="text" value="' . esc_attr( $default_sizes['width'] ) . '" name="documentcloud_default_width" />';
}
/**
* Render the full width field.
*/
function full_width_field() {
$default_sizes = $this->get_default_sizes();
echo '<input type="text" value="' . esc_attr( $default_sizes['full_width'] ) . '" name="documentcloud_full_width" />';
}
}
new WP_DocumentCloud;