From 300ed667d6df4fa3d5dbb4a4bf2743a10d1c5fa2 Mon Sep 17 00:00:00 2001 From: freedmand Date: Fri, 28 Aug 2020 11:28:13 -0400 Subject: [PATCH] Beta support (0.5.0) --- README.md | 3 +++ documentcloud.php | 49 ++++++++++++++++++++++++++++++++++------------- readme.txt | 7 +++++-- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2160d55..553594d 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,9 @@ If you find yourself absolutely needing to expire the cache, though, you have tw ## Changelog +### 0.5.0 +* Add support for the DocumentCloud beta + ### 0.4.3 * Separate the oEmbed config options (provided as params to the endpoint) from the embed config options (encoded as params on the `url`) (#48) * Rename `default_page` and `default_note` options back to `page` and `note` (#47) diff --git a/documentcloud.php b/documentcloud.php index 538a539..d864879 100644 --- a/documentcloud.php +++ b/documentcloud.php @@ -3,13 +3,14 @@ * 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 + * Version: 0.5.0 + * Authors: Chris Amico, Justin Reese, Dylan Freedman * License: GPLv2 ***/ /* Copyright 2011 National Public Radio, Inc. Copyright 2015 DocumentCloud, Investigative Reporters & Editors + Copyright 2020 MuckRock Foundation, Inc. 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 @@ -28,11 +29,17 @@ 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 = '^(?Phttps?):\/\/www\.documentcloud\.org\/documents\/(?P[0-9]+-[\p{L}\p{N}%-]+)'; + 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 = '^(?Phttps?):\/\/(?P.*documentcloud\.org)\/documents\/(?P[0-9]+-[\p{L}\p{N}%-]+)', + CONTAINER_TEMPLATE_START = "
", + CONTAINER_TEMPLATE_END = "
", + BETA_ID_CUTOFF = 20000000, + BETA_OEMBED_RESOURCE_DOMAIN = 'beta.documentcloud.org', + BETA_OEMBED_DOMAIN_MATCH = '#https?://(www\.)?(beta|embed).documentcloud.org/.*#i', + BETA_OEMBED_PROVIDER = 'https://api.beta.documentcloud.org/api/oembed'; /** * Constructor. */ @@ -108,7 +115,14 @@ function register_dc_oembed_provider() { $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 ); + wp_oembed_add_provider( 'https://' . $oembed_resource_domain . '/documents/*', $oembed_provider ); + + // Add oembed provider for the DocumentCloud beta + wp_oembed_add_provider( + WP_DocumentCloud::BETA_OEMBED_DOMAIN_MATCH, + WP_DocumentCloud::BETA_OEMBED_PROVIDER, + true + ); } /** @@ -233,7 +247,15 @@ function process_dc_shortcode( $atts ) { if ( empty( $atts['id'] ) ) { return ''; } else { - $url = $filtered_atts['url'] = 'https://' . WP_DocumentCloud::OEMBED_RESOURCE_DOMAIN . "/documents/{$atts['id']}.html"; + // Determine which URL on the basis of the DocumentCloud ID + if (intval($atts['id']) >= WP_DocumentCloud::BETA_ID_CUTOFF) { + // Populate beta URL + // TODO: use only one URL after the switch + $url = $filtered_atts['url'] = 'https://' . WP_DocumentCloud::BETA_OEMBED_RESOURCE_DOMAIN . "/documents/{$atts['id']}.html"; + } else { + // Populate legacy URL + $url = $filtered_atts['url'] = 'https://' . WP_DocumentCloud::OEMBED_RESOURCE_DOMAIN . "/documents/{$atts['id']}.html"; + } } } @@ -270,9 +292,9 @@ function process_dc_shortcode( $atts ) { // 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 ); + return WP_DocumentCloud::CONTAINER_TEMPLATE_START . $wp_embed->shortcode( $filtered_atts, $url ) . WP_DocumentCloud::CONTAINER_TEMPLATE_END; } else { - return wp_oembed_get( $atts['url'], $filtered_atts ); + return WP_DocumentCloud::CONTAINER_TEMPLATE_START . wp_oembed_get( $atts['url'], $filtered_atts ) . WP_DocumentCloud::CONTAINER_TEMPLATE_END; } } @@ -316,7 +338,7 @@ function parse_dc_url( $url ) { 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']}"; + $url = "{$elements['protocol']}://{$elements['dc_host']}/documents/{$elements['document_slug']}"; if ( isset( $elements['page_number'] ) ) { $url .= "/pages/{$elements['page_number']}"; } else if ( isset( $elements['note_id'] ) ) { @@ -340,11 +362,12 @@ function add_options_page() { * Render the DocumentCloud options page. */ function render_options_page() { + // TODO: remove the responsive warning after the switch ?>

-

responsive="false" on an embed.', 'documentcloud' ) ) ?>

+

responsive="false" on an embed.', 'documentcloud' ) ) ?>

diff --git a/readme.txt b/readme.txt index 312c46d..fe53968 100644 --- a/readme.txt +++ b/readme.txt @@ -1,8 +1,8 @@ === DocumentCloud === -Contributors: chrisamico, reefdog +Contributors: chrisamico, reefdog, freedmand Tags: documentcloud, documents, journalism, reporting, research Requires at least: 3.5 -Tested up to: 4.7 +Tested up to: 5.5 Stable tag: trunk License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -79,6 +79,9 @@ You can read more about publishing and embedding DocumentCloud resources on http == Changelog == += 0.5.0 = +* Add support for the DocumentCloud beta + = 0.4.3 = * Separate the oEmbed config options (provided as params to the endpoint) from the embed config options (encoded as params on the `url`) (#48) * Rename `default_page` and `default_note` options back to `page` and `note` (#47)