Skip to content

Commit

Permalink
v1.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
tamw-wnet committed May 30, 2024
1 parent 62dcbea commit fa24591
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ A collection of tools for publishing from and to NPR's Story API. [Find this plu
- Original developers: NPRDS, INN Labs
- Requires at least: 3.8.14
- Tested up to: 6.4.1
- Stable tag: 1.9.6
- Version: 1.9.7
- Stable tag: 1.9.7
- License: GPLv2
- License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -70,6 +71,15 @@ NPR Stories having got gotten
## Changelog

<!-- copy from readme.txt to here -->
### V1.9.7
* Updated ID pattern matching on get_stories.php to accommodate NPR's new alphanumeric IDs

### V1.9.6.2
* Fixed a potentially fatal error when trying to pull a Story API ID from a blank URL

### V1.9.6.1
* Added messaging about the eventual retirement of the Story API with links to the newly launched CDS plugin

### V1.9.6
* Articles imported from the API will now show the original article URL as the canonical URL in the header

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license" : "GPL-2.0+",
"require": {},
"dist": {
"url": "https://github.com/OpenPublicMedia/nprapi-wordpress/archive/refs/tags/v1.9.6.zip",
"url": "https://github.com/OpenPublicMedia/nprapi-wordpress/archive/refs/tags/v1.9.7.zip",
"type": "zip"
},
"authors": [
Expand Down
10 changes: 5 additions & 5 deletions ds-npr-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: NPR Story API
* Description: A collection of tools for reusing content from NPR.org, now maintained and updated by NPR member station developers
* Version: 1.9.6
* Version: 1.9.7
* Author: Open Public Media
* License: GPLv2
*/
Expand Down Expand Up @@ -264,7 +264,7 @@ function nprstory_add_header_meta() {
}
add_action( 'wp_head', 'nprstory_add_header_meta', 9 );

// add_action('admin_notices', 'nprstory_cds_plugin_admin_notice');
add_action('admin_notices', 'nprstory_cds_plugin_admin_notice');

function nprstory_cds_plugin_admin_notice() {
global $pagenow;
Expand All @@ -282,10 +282,10 @@ function nprstory_cds_plugin_admin_notice() {
}
}
if ( $display ) { ?>
<div class="notice notice-warning">
<div class="notice notice-warning is-dismissible">
<h2><?php _e( 'The NPR Story API plugin is going away!', 'ds_npr_api' ); ?></h2>
<p><?php _e( 'In the coming months, the NPR Story API will be sunset in favor of the NPR Content Distribution System (CDS). The same great content, but with greater flexibility, a more modern architecture, and richer media support.', 'ds_npr_api' ); ?></p>
<p><?php _e( 'While the Story API will continue to function for some time, we encourage you to try out the new NPR CDS Plugin. Be sure to file a ticket in NPR Studio to get your authorization token and station information.', 'ds_npr_api' ); ?></p>
<p><?php _e( 'In the coming months, the NPR Story API will be sunset in favor of the <a href="https://npr.github.io/content-distribution-service/">NPR Content Distribution Service (CDS)</a>. The same great content, but with greater flexibility, a more modern architecture, and richer media support.', 'ds_npr_api' ); ?></p>
<p><?php _e( 'While the Story API will continue to function for some time, we encourage you to try out the <a href="https://wordpress.org/plugins/npr-content-distribution-service/">new NPR CDS Plugin</a>. Be sure to file a ticket in <a href="https://studio.npr.org/">NPR Studio</a> to get your authorization token and station information.', 'ds_npr_api' ); ?></p>
</div>
<?php
}
Expand Down
63 changes: 36 additions & 27 deletions get_stories.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,52 +109,62 @@ public function load_page_hook() {
403
);
}
$story_id = sanitize_text_field( $_POST[ 'story_id' ] );
$story_id = trim( sanitize_text_field( $_POST[ 'story_id' ] ) );
if ( isset( $_POST['publishNow'] ) ) {
$publish = true;
}
if ( isset( $_POST['createDaft'] ) ) {
if ( isset( $_POST['createDraft'] ) ) {
$publish = false;
}
} elseif ( isset( $_GET['story_id'] ) && isset( $_GET['create_draft'] ) ) {
$story_id = sanitize_text_field( $_GET['story_id'] );
$story_id = trim( sanitize_text_field( $_GET['story_id'] ) );
}

$valid = false;
// try to get the ID of the story from the URL
if ( isset( $story_id ) ) {
if ( !empty( $story_id ) ) {
//check to see if we got an ID or a URL
if ( is_numeric( $story_id ) ) {
if ( strlen( $story_id ) >= 8 ) {
$story_id = $story_id;
}
} elseif ( strpos( $story_id, 'npr.org' ) !== false ) {
if ( is_numeric( $story_id ) && strlen( $story_id ) >= 8 ) {
$valid = true;
} elseif ( preg_match( '/^[a-z0-9\-]+$/', $story_id ) ) {
$valid = true;
} elseif ( wp_http_validate_url( $story_id ) ) {
$story_id = sanitize_url( $story_id );
// url format: /yyyy/mm/dd/id
// url format: /blogs/name/yyyy/mm/dd/id
$story_id = preg_replace( '/https?\:\/\/[^\s\/]*npr\.org\/((([^\/]*\/){3,5})([0-9]{8,12}))\/.*/', '$4', $story_id );
if ( !is_numeric( $story_id ) ) {
// url format: /templates/story/story.php?storyId=id
$story_id = preg_replace( '/https?\:\/\/[^\s\/]*npr\.org\/([^&\s\<]*storyId\=([0-9]+)).*/', '$2', $story_id );
}
} else {
$story_id = sanitize_url( $story_id );
$meta = get_meta_tags( $story_id );
if ( !empty( $meta['brightspot-datalayer'] ) ) {
$json = json_decode( html_entity_decode( $meta['brightspot-datalayer'] ), TRUE );
if ( !empty( $json['nprStoryId'] ) ) {
$story_id = $json['nprStoryId'];
if ( str_contains( $story_id, 'npr.org' ) ) {
preg_match( '/https?:\/\/[^\s\/]*npr\.org\/((([^\/]*\/){3,5})([a-z\-0-9]+))\/.*/', $story_id, $matches );
if ( !empty( $matches[4] ) ) {
$story_id = $matches[4];
$valid = true;
} else {
preg_match( '/https?\:\/\/[^\s\/]*npr\.org\/([^&\s\<]*storyId\=([0-9]+)).*/', $story_id, $matches );
if ( !empty( $matches[2] ) ) {
$story_id = $matches[2];
$valid = true;
}
}
} elseif ( !empty( $meta['story_id'] ) ) {
$story_id = $meta['story_id'];
} else {
nprstory_show_message( "The referenced URL (" . $story_id . ") does not contain a valid NPR Story API ID. Please try again.", TRUE );
error_log( "The referenced URL (" . $story_id . ") does not contain a valid NPR Story API ID. Please try again." ); // debug use
$meta = get_meta_tags( $story_id );
if ( ! empty( $meta['brightspot-datalayer'] ) ) {
$json = json_decode( html_entity_decode( $meta['brightspot-datalayer'] ), true );
if ( ! empty( $json['nprStoryId'] ) ) {
$story_id = $json['nprStoryId'];
$valid = true;
}
} elseif ( ! empty( $meta['story_id'] ) ) {
$story_id = $meta['story_id'];
$valid = true;
} else {
nprstory_show_message( "The referenced URL (" . $story_id . ") does not contain a valid NPR CDS ID. Please try again.", true );
error_log( "The referenced URL (" . $story_id . ") does not contain a valid NPR CDS ID. Please try again." ); // debug use
}
}
}
}

// Don't do anything if $story_id isn't an ID
if ( isset( $story_id ) && is_numeric( $story_id ) ) {
if ( !empty( $story_id ) && $valid ) {
// start the API class
// todo: check that the API key is actually set
$api = new NPRAPIWordpress();
Expand All @@ -175,7 +185,6 @@ public function load_page_hook() {
$xml = simplexml_load_string( $api->xml );
nprstory_show_message( 'Error retrieving story for id = ' . $story_id . '<br> API error =' . $api->message->id . '<br> API Message =' . $xml->message->text, TRUE );
error_log( 'Not going to save the return from query for story_id=' . $story_id .', we got an error=' . $api->message->id . ' from the NPR Story API' ); // debug use
return;
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Donate link: https://www.npr.org/series/750002/support-public-radio
Tags: npr, news, public radio, api
Requires at least: 3.8.14
Tested up to: 6.0
Stable tag: 1.9.6
Version: 1.9.7
Stable tag: 1.9.7
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: nprapi
Expand Down Expand Up @@ -70,6 +71,15 @@ NPR Stories having got gotten


== Changelog ==
= V1.9.7 =
* Updated ID pattern matching on get_stories.php to accommodate NPR's new alphanumeric IDs

= V1.9.6.2 =
* Fixed a potentially fatal error when trying to pull a Story API ID from a blank URL

= V1.9.6.1 =
* Added messaging about the eventual retirement of the Story API with links to the newly launched CDS plugin

= V1.9.6 =
* Articles imported from the API will now show the original article URL as the canonical URL in the header

Expand Down

0 comments on commit fa24591

Please sign in to comment.