diff --git a/docs/updating-pym.md b/docs/maintainer-notes.md similarity index 66% rename from docs/updating-pym.md rename to docs/maintainer-notes.md index e5ece1c..acf736f 100644 --- a/docs/updating-pym.md +++ b/docs/maintainer-notes.md @@ -1,4 +1,6 @@ -## Notes for Plugin maintainers +# Notes for Plugin maintainers + +### Updating Pym.js `js/pym.v1.min.js` in this plugin should be kept up-to-date with the current version of https://pym.nprapps.org/pym.v1.min.js. @@ -10,3 +12,18 @@ To update: - update this plugin's version number to the `Pym.js` version number NPR Visuals Team's [stated intention](https://github.com/nprapps/pym.js/tree/master#versioning) is that versions of `Pym.js` will be backwards-compatible for `0.x` and `0.0.x` releases, so we can copy those in directly. When a `x.0.0` release comes around, we'll need to figure out a plan for that. See discussion in https://github.com/INN/pym-shortcode/issues/12 + +### Updating the plugin + +The plugin's `A.B.C` version number should match the version number of the bundled copy of `Pym.js`. We started doing this in plugin release 1.1.2. + +The plugin's [version history](https://github.com/INN/pym-shortcode/releases) looks like this: + +- 0.1: initial release +- 1.1.2 +- 1.2.0 +- 1.2.0.1: a fix in this plugin +- 1.2.0.2: a fix in this plugin +- 1.3.1 +- 1.3.2 +- 1.3.2.1: Gutenberg and settings page diff --git a/docs/readme.md b/docs/readme.md index 4723e24..f457439 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -7,13 +7,14 @@ Contents: 1. [Plugin Installation](#plugin-installation) 2. [The Pym Shortcode](#the-pym-shortcode) 3. [The Pym Block](#the-pym-block) -4. [Options](#options) - 1. [src: the only required argument](#src-the-child-url) - 2. pymsrc - 3. pymoptions - 4. class - 5. align - 6. id +4. [Embed Options](#embed-options) + 1. [src: the only required argument](#src-the-child-url-required-argument) + 2. [pymsrc](#pymsrc-the-url-for-pymjs) + 3. [pymoptions](#pymoptions-settings-for-pym) + 4. [class](#class-to-add-html-classes-to-the-pym-parent-element) + 5. [align](#align-for-wordpress-alignment-support) + 6. [id](#id-to-set-the-pym-parent-elements-id) +5. [Plugin Options](#plugin-options) 5. [Frequently Asked Questions](#frequently-asked-questions) 1. [Why would I want to use Pym in the first place?](#why-would-i-want-to-use-pym-in-the-first-place) 2. [Why is a WordPress plugin needed to use Pym.js?](#why-is-a-wordpress-plugin-needed-to-use-pymjs) @@ -53,7 +54,7 @@ Example in a post: For the block, all options available via shortcode arguments are available through the block's advanced options panel. -## Options +## Embed Options ``` [pym src="" pymsrc="" pymoptions="" class="" align="" id="" ] @@ -102,7 +103,9 @@ To do the same thing with this Pym shortcode, you would write: For example, the shortcode `[pym src="https://blog.apps.npr.org/pym.js/examples/table/child.html" class="one two three four float-left mw_50"]` results in the following output: ```html -
+ +... + ``` If you do not want the class `'pym'` output on container elements, [add a filter](https://codex.wordpress.org/Plugin_API/Filter_Reference) to the hook `pym_shortcode_default_class` that returns an empty string. @@ -122,9 +125,35 @@ In the Gutenberg editor, the alignment options are provided by the alignment con For example, the shortcode `[pym src="https://blog.apps.npr.org/pym.js/examples/table/child.html" id="extremely_specific_id"]` results in the following output: ```html - +%1$s
', + wp_kses_post( __( 'For information on how to use the block and shortcode provided by the Pym.js Embeds plugin, read the plugin\'s documentation on GitHub.', 'pym-shortcode' ) ) + ); + + printf( + '', + esc_html__( 'The URL for the copy of Pym.js hosted on this site is:', 'pym-shortcode' ) + ); + + // copying how qz.com does their share links + printf( + '', + esc_attr( pym_pymsrc_local_url() ) + ); +} diff --git a/inc/settings-page.php b/inc/settings-page.php new file mode 100644 index 0000000..ace28e6 --- /dev/null +++ b/inc/settings-page.php @@ -0,0 +1,241 @@ +%1$s', + esc_html( get_admin_page_title() ) + ); + + ?> + + __NAMESPACE__ . '\sanitize_callback', + ) + ); + + add_settings_section( + settings_section(), + __( 'Pym.js Source Settings', 'pym-shortcode' ), + __NAMESPACE__ . '\pym_settings_section_callback', + settings_page() + ); + + add_settings_field( + 'default_pymsrc', + __( 'Default pymsrc', 'pym-shortcode' ), + __NAMESPACE__ . '\field_default_pymsrc', + settings_page(), // menu slug of this page. + settings_section(), // settings section slug. + array( + 'label_for' => 'default_pymsrc', + ) + ); + + add_settings_field( + 'override_pymsrc', + __( 'Override pymsrc', 'pym-shortcode' ), + __NAMESPACE__ . '\field_override_pymsrc', + settings_page(), // menu slug of this page. + settings_section(), // settings section slug. + array( + 'label_for' => 'override_pymsrc', + ) + ); +} +add_action( 'admin_init', __NAMESPACE__ . '\admin_init' ); + +/** + * Settings section callback + * + * Does nothing. + * + * @param Array $args Arguments passed to the section callback. + */ +function pym_settings_section_callback( $args ) { + printf( + '%1$s
', + __( 'The Pym.js JavaScript library can be provided from many sources. By default, shortcodes and blocks will use a copy of Pym.js hosted on your website to power embeds. For more information about changing the Pym.js source URL, referred to as \'pymsrc\', please read this plugin\'s documentation.', 'pym-shortcode' ) + ); +} + +/** + * Sanitization callback for the option_value. + * + * If the default_pymsrc isn't a valid URL or is the pym_pymsrc_local_url, blank it. + * If the override_pymsrc isn't 'on' indicating it's checked, blank it. + * + * @since 1.3.2.1 + * @param Mixed $value The setting. + * @return Array The sanitized setting + * @uses pym_pymsrc_local_url + * @uses pym_plugin_version + */ +function sanitize_callback( $value ) { + error_log(var_export( $value, true)); + $new_value = array(); + + $proposed_pymsrc = wp_http_validate_url( $value['default_pymsrc'] ); + if ( pym_pymsrc_local_url() === $proposed_pymsrc ) { + $proposed_pymsrc = null; + } + $new_value['default_pymsrc'] = ! empty( $proposed_pymsrc ) ? $proposed_pymsrc : null; + + $new_value['override_pymsrc'] = ( isset( $value['override_pymsrc'] ) && 'on' === $value['override_pymsrc'] ) ? 'on': null; + + $new_value['version'] = pym_plugin_version(); + + return $new_value; +} + +/** + * The field for the default Pymsrc + * + * @param Array $args The arguments passed to the settings field callback. + */ +function field_default_pymsrc( $args ) { + $settings = get_option( option_key() ); + $id = isset( $args['label_for'] ) ? $args['label_for'] : 'default_pymsrc'; + $id = option_key() . '[' . $id . ']'; + + $url = isset( $settings['default_pymsrc'] ) ? $settings['default_pymsrc'] : ''; + printf( + '', + esc_attr( $id ), + esc_attr( $url ) + ); + + printf( + '', + esc_attr( $id ), + wp_kses_post( __( 'This URL is where Pym.js will be loaded from for all embeds that do not set a pymsrc in the shortcode attributes or block settings. NPR and the Pym.js Embed plugin maintainers recommend that you use the NPR-provided CDN for this purpose:https://pym.nprapps.org/pym.v1.min.js', 'pym-shortcode' ) )
+ );
+ printf(
+ '',
+ esc_attr( $id ),
+ sprintf(
+ // translators: %1$s is a bare URL.
+ wp_kses_post( __( 'If no pymsrc URL is set here, then the plugin-provided copy of Pym.js will be used as the default: %1$s', 'pym-shortcode' ) ),
+ esc_html( pym_pymsrc_local_url() )
+ )
+ );
+}
+
+/**
+ * The checkbox to force all embeds to use the default
+ *
+ * @param Array $args The arguments passed to the settings field callback.
+ */
+function field_override_pymsrc( $args ) {
+ $settings = get_option( option_key() );
+ $id = isset( $args['label_for'] ) ? $args['label_for'] : 'override_pymsrc';
+ $id = option_key() . '[' . $id . ']';
+
+ $value = isset( $settings['override_pymsrc'] ) ? $settings['override_pymsrc'] : '';
+ printf(
+ '',
+ esc_attr( $id ),
+ checked( $value, 'on', false )
+ );
+
+ printf(
+ '',
+ esc_attr( $id ),
+ esc_html__( 'Checking this box means that every Pym.js embed will use the default pymsrc URL, ignoring the pymsrc URL set in the embed\'s shortcode attributes or block settings. We recommend that you check this box after setting the default pymsrc URL to the CDN-provided copy of the library.', 'pym-shortcode' )
+ );
+}
diff --git a/inc/shortcode.php b/inc/shortcode.php
index 247502b..cf73074 100644
--- a/inc/shortcode.php
+++ b/inc/shortcode.php
@@ -5,6 +5,8 @@
* @package pym-shortcode
*/
+ use INN\PymEmbeds\Settings\option_key;
+
/**
* A shortcode to simplify the process of embedding articles using pym.js
*
@@ -14,6 +16,8 @@
* @param String $content the enclosed content; should be empty for this shortcode.
* @param String $tag the shortcode tag.
* @uses pym_shortcode_script_footer_enqueue
+ * @uses pym_pymsrc_default_url
+ * @uses \INN\PymEmbeds\Settings\option_key()
* @return String the embed HTML
*/
function pym_shortcode( $atts = array(), $content = '', $tag = '' ) {
@@ -31,7 +35,7 @@ function pym_shortcode( $atts = array(), $content = '', $tag = '' ) {
$actual_id = empty( $id ) ? 'pym_' . $pym_id : $id;
/**
- * Filter pym_shortcode_default_class allows setting the default class on embeds
+ * Filter pym_shortcode_default_class allows setting the default class on embeds.
*
* @param String $default
* @return String the default class name
@@ -58,7 +62,15 @@ function pym_shortcode( $atts = array(), $content = '', $tag = '' ) {
);
// What's the pymsrc for this shortcode?
- $pymsrc = empty( $atts['pymsrc'] ) ? plugins_url( '/js/pym.v1.min.js', dirname( __FILE__ ) ) : $atts['pymsrc'];
+ if (
+ pym_maybe_override_pymsrc() // if the box to override has been checked...
+ || empty( $atts['pymsrc'] ) // if there is no specified pymsrc...
+ || ! wp_http_validate_url( $atts['pymsrc'] ) // if the specified pymsrc is not a safe URL...
+ ) {
+ $pymsrc = pym_pymsrc_default_url(); // use the default URL.
+ } else {
+ $pymsrc = $atts['pymsrc'];
+ }
// If this is the first Pym element on the page, output the pymsrc script tag
// or if the pymsrc is set, output that.
@@ -137,3 +149,44 @@ function() use ( $args ) {
);
}
}
+
+/**
+ * Whether to force use of the default pymsrc URL.
+ *
+ * @since 1.3.2.1
+ * @uses \INN\PymEmbeds\Settings\option_key()
+ * @return bool Whether to force use of the default URL.
+ */
+function pym_maybe_override_pymsrc() {
+ $settings = get_option( \INN\PymEmbeds\Settings\option_key() );
+ if ( isset( $settings['override_pymsrc'] ) && 'on' === $settings['override_pymsrc'] ) {
+ return true;
+ }
+ return false;
+}
+
+/**
+ * The default URL for pymsrc, as defined in plugin settings
+ *
+ * @since 1.3.2.1
+ * @uses pym_pymsrc_local_url
+ */
+function pym_pymsrc_default_url() {
+ $settings = get_option( \INN\PymEmbeds\Settings\option_key() );
+ if ( isset ( $settings['default_pymsrc'] ) ) {
+ if ( wp_http_validate_url( $settings['default_pymsrc'] ) ) {
+ return $settings['default_pymsrc'];
+ }
+ }
+ return pym_pymsrc_local_url();
+}
+
+/**
+ * The plugin-provided pymsrc url
+ *
+ * @since 1.3.2.1
+ * @return string The URL for /wp-content/plugins/pym-shortcode/js/pym.v1.min.js
+ */
+function pym_pymsrc_local_url() {
+ return plugins_url( '/js/pym.v1.min.js', dirname( __FILE__ ) );
+}
diff --git a/pym-shortcode.php b/pym-shortcode.php
index d89ae7f..574d5c4 100644
--- a/pym-shortcode.php
+++ b/pym-shortcode.php
@@ -3,7 +3,7 @@
Plugin Name: Pym Shortcode
Plugin URI: https://github.com/INN/pym-shortcode
Description: Adds a [pym src=""] shortcode to simplify use of NPR's Pym.js
-Version: 1.3.2
+Version: 1.3.2.1
Author: The INN Nerds
Author URI: http://nerds.inn.org/
License: GPL Version 2 or later
@@ -16,9 +16,20 @@
die;
}
+/**
+ * What version is this plugin?
+ *
+ * @return string The plugin version number
+ */
+function pym_plugin_version() {
+ return '1.3.2.1';
+}
+
$includes = array(
'/inc/block.php',
'/inc/shortcode.php',
+ '/inc/info-page.php',
+ '/inc/settings-page.php',
'/inc/class-pymsrc-output.php',
);
foreach ( $includes as $include ) {
diff --git a/readme.txt b/readme.txt
index d766415..2efd317 100644
--- a/readme.txt
+++ b/readme.txt
@@ -3,6 +3,7 @@ Contributors: inn_nerds
Donate link: https://inn.org/donate
Tags: shortcode, iframe, javascript, embeds, responsive, pym, NPR
Requires at least: 3.0.1
+Requires PHP: 5.3
Tested up to: 4.9.8
Stable tag: 1.3.2
License: GPLv2 or later
@@ -18,7 +19,8 @@ Pym Shortcode will resize an iframe responsively depending on the height of its
1. In the WordPress Dashboard go to **Plugins**, then click the **Add Plugins** button and search the WordPress Plugins Directory for Pym Shortcode. Alternatively, you can download the zip file from this Github repo and upload it manually to your WordPress site.
2. Activate the plugin through the 'Plugins' screen in WordPress
-3. Nothing to configure, just begin using Pym Shortcode!
+3. In **Settings > Pym.js Embed Settings**, decide whether you'd like to change the plugin's behavior to use a non-default source URL for `Pym.js`, and whether you'd like to prevent post authors from setting embed-specific URLs for `Pym.js`
+4. Begin embedding content!
== Frequently Asked Questions ==
@@ -44,13 +46,34 @@ Mobile view of the WordPress post with the NPR embed using Pym Shortcode:
== Changelog ==
-= [unreleased] =
+= [1.3.2.1] =
+
+This is a major update!
+
+Following the practice begun at plugin version 1.1.2 of [having the plugin version number match the version number of the bundled copy of `Pym.js`](https://github.com/INN/pym-shortcode/blob/master/docs/maintainer-notes.md), the first three numbers in this plugin's version do not change with this release because the `Pym.js` version has not changed. We've tacked a `.1` on to the end to denote this release. Please read the release notes and test your site as appropriate before upgrading in production.
+
+New features:
* Adds a "Pym Embed" block for use in Gutenberg. [PR #34](https://github.com/INN/pym-shortcode/pull/34) for issue [#28](https://github.com/INN/pym-shortcode/issues/28).
+ * If a block is created using this plugin and Gutenberg, and Gutenberg is then disabled, the block will show a link to the embedded graphic.
+* Through the settings page, you can now serve pym.js using your newsroom's CDN or NPR's CDN! [PR #45]() for [issue #31](https://github.com/INN/pym-shortcode/issues/31).
+* Adds a settings page, available to those with the `manage_options` capability, with the following options:
+ * Change the default pymsrc URL. [PR #45]() for [issue #8](https://github.com/INN/pym-shortcode/issues/8).
+ * Override block and shortcode pymsrc URLs with the default pymsrc URL. [PR #45]() for [issue #8](https://github.com/INN/pym-shortcode/issues/8).
+* Adds an informational page, available to all who can make posts, that lists the plugin's default source URL for `Pym.js`. This is to make the process of building new interactives easier.
* Shortcode now gains an `align=""` parameter, so that WordPress's generated [alignment CSS classes](https://codex.wordpress.org/CSS#WordPress_Generated_Classes) can be used on embeds. By enabling this in the shortcode, the Gutenberg Block also gains support for alignment. [PR #34](https://github.com/INN/pym-shortcode/pull/34)
* Script tags for embeds are no longer output by `the_content()`, instead being output during `wp_footer()` by [closures](https://secure.php.net/manual/en/functions.anonymous.php) hooked on the `'wp_footer'` action. [PR #34](https://github.com/INN/pym-shortcode/pull/34) for issues [#33](https://github.com/INN/pym-shortcode/issues/33) and [#35](https://github.com/INN/pym-shortcode/issues/35).
-* The source URL for `pym.js` is no longer output by `the_content()`, instead being output during `wp_footer` by an action dedicated to the task. If different shortcodes and/or blocks on the page specify different source URLs for Pym.js, all are output (after removing duplicates), but a message is logged in the browser console. If `WP_DEBUG` is set, this message is also logged to the server log, with the post ID specified. [PR #34](https://github.com/INN/pym-shortcode/pull/34) for issues [#33](https://github.com/INN/pym-shortcode/issues/33) and [#35](https://github.com/INN/pym-shortcode/issues/35). See https://github.com/INN/pym-shortcode/tree/master/docs#ive-set-a-different-pymsrc-option-but-now-im-seeing-a-message-in-the-console
* The script tag used to run `new pym.Parent` is now configurable. By replacing the [pluggable function](https://codex.wordpress.org/Pluggable_Functions) `pym_shortcode_script_footer_enqueue()` with your own function, you can now use alternate forms of embed code that may be required for PJAX sites or custom versions of Pym.js. This resolves issue [#19](https://github.com/INN/pym-shortcode/issues/19).
+* Adds "Requires PHP: 5.3" metadata to the plugin's `readme.txt`, since we're now using PHP namespaces for some code.
+* Adds documentation for how to test the plugin:
+ * tests to run before enabling the "override pymsrc" option in production
+ * tests to run for site compatibility with Gutenberg
+
+Changes:
+
+* The source URL for `pymjs`, known as the pymsrc URL, is now passed through [wp_http_validate_url](https://developer.wordpress.org/reference/functions/wp_http_validate_url/). [PR #45]() for [issue #8](https://github.com/INN/pym-shortcode/issues/8).
+* The source URL for `pym.js` is no longer output by `the_content()`, instead being output during `wp_footer` by an action dedicated to the task. If different shortcodes and/or blocks on the page specify different source URLs for Pym.js, all are output (after removing duplicates), but a message is logged in the browser console. If `WP_DEBUG` is set, this message is also logged to the server log, with the post ID specified. [PR #34](https://github.com/INN/pym-shortcode/pull/34) for issues [#33](https://github.com/INN/pym-shortcode/issues/33) and [#35](https://github.com/INN/pym-shortcode/issues/35). See https://github.com/INN/pym-shortcode/tree/master/docs#ive-set-a-different-pymsrc-option-but-now-im-seeing-a-message-in-the-console
+* `docs/updating-pym.md` becomes `docs/maintainer-notes.md`
= 1.3.2 =