Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Added an 'npr_image_crop_url' hook. #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions classes/NPRAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,20 @@ function parse() {
return;
}

$object = simplexml_load_string($xml);
try {
$object = simplexml_load_string( $xml );
} catch ( Exception $e ) {
echo "\nXML ERROR: {$e->getMessage()}\n";
$this->stories = array();
return;
}
$this->add_simplexml_attributes($object, $this);

if (!empty($object->message)) {
$this->message->id = $this->get_attribute($object->message, 'id');
$this->message->level = $this->get_attribute($object->message, 'level');
$this->message = (object) array(
'id' => $this->get_attribute( $object->message, 'id' ),
'level' => $this->get_attribute( $object->message, 'level' )
);
}

if (!empty($object->list->story)) {
Expand Down
64 changes: 49 additions & 15 deletions classes/NPRAPIWordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,24 @@ function query_by_url( $url ) {
* This function will go through the list of stories in the object and check to see if there are updates
* available from the NPR API if the pubDate on the API is after the pubDate originally stored locally.
*
* @see https://github.com/nprds/nprapi-wordpress/commit/ac0a7f7e3b7428ba783853eb76696b499bc85ecc for is_numeric($args)
*
* @since 1.7
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could, resubmit your PR with the most recent master branch, which has the $qnum parameter added to this function instead of an $opts array or WP arg string

Copy link
Contributor Author

@mikeschinkel mikeschinkel Feb 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be happy to remove my last changeset if that's what you would prefer.

But before I do may I ask to make sure you were aware they were based on the suggestions I made to PR #39? I added comments inline to the changeset but I don't see the comments I added: https://github.com/nprds/nprapi-wordpress/pull/39/files

I basically suggested that you future proof and not add a numeric 2nd parameter to update_posts_from_stories() but instead add an $opts array that would contain the query_number so that in future if you need to add other parameters you would have end up with a list of many ordinal parameters.

But of course, feel free to decline this modification if you feel it is unimportant in which case I will revert that last commit from my PR.

* @param bool $publish
* @param array|int $opts { If numeric it will be `'query_number' otherwise an array of named options.
*
* @type int $query_number The number of the query for $args['tags_input']
*
* }
* @return int|null $post_id or null
*/
function update_posts_from_stories( $publish = TRUE ) {
function update_posts_from_stories( $publish = TRUE, $opts = array() ) {

$opts = wp_parse_args( $opts, array(
'query_number' => is_numeric( $opts ) ? intval( $opts ) : false,
));

$pull_post_type = get_option( 'ds_npr_pull_post_type' );
if ( empty( $pull_post_type ) ) {
$pull_post_type = 'post';
Expand Down Expand Up @@ -154,6 +168,9 @@ function update_posts_from_stories( $publish = TRUE ) {
'post_type' => $pull_post_type,
'post_date' => $post_date,
);
if ( false !== $opts[ 'query_number' ] ) {
$args['tags_input'] = get_option( "ds_npr_query_tags_{$opts[ 'query_number' ]}" );
}
//check the last modified date and pub date (sometimes the API just updates the pub date), if the story hasn't changed, just go on
if ( $post_mod_date != strtotime( $story->lastModifiedDate->value ) || $post_pub_date != strtotime( $story->pubDate->value ) ) {

Expand Down Expand Up @@ -272,16 +289,16 @@ function update_posts_from_stories( $publish = TRUE ) {
//are there any images saved for this post, probably on update, but no sense looking of the post didn't already exist
if ( $existing ) {
$image_args = array(
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post_id,
'post_status' => null,
'post_type' => 'attachment',
'post_date' => $post_date,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post_id,
'post_status' => null,
'post_type' => 'attachment',
'post_date' => $post_date,
);
$attached_images = get_children( $image_args );
}
foreach ( (array) $story->image as $image ) {
foreach ( (array) $story->image as $image_index => $image ) {
$image_url = '';
//check the <enlargement> and then the crops, in this order "enlargement", "standard" if they don't exist, just get the image->src
if ( ! empty( $image->enlargement ) ) {
Expand Down Expand Up @@ -312,7 +329,24 @@ function update_posts_from_stories( $publish = TRUE ) {
if ( empty( $image_url ) && ! empty( $image->src ) ) {
$image_url = $image->src;
}
nprstory_error_log( 'Got image from: ' . $image_url );

/**
* Filters the image crop url
*
* Allows a site to decide which crop it prefers to use for thumbnail/featured image.
* Especially useful if/when the crop is way too big.
*
* @since 1.7
*
* @param string $image_url URL of image crop to download
* @param NPRMLElement $image Image object containing crops
* @param int $post_id Post ID or NULL if no post ID.
* @param NPRMLEntity $story Story object created during import
* @param int $image_index Index into $story->image[] that results in $image
*/
$image_url = apply_filters( 'npr_image_crop_url', $image_url, $story->image[ $image_index ], $post_id, $story, $image_index );

nprstory_error_log( 'Got image from: ' . $image_url );
// Download file to temp location
$tmp = download_url( $image_url );

Expand Down Expand Up @@ -389,12 +423,12 @@ function update_posts_from_stories( $publish = TRUE ) {
}

$args = array(
'post_title' => $story->title,
'post_content' => $story->body,
'post_excerpt' => $story->teaser,
'post_type' => $pull_post_type,
'ID' => $post_id,
'post_date' => $post_date,
'post_title' => $story->title,
'post_content' => $story->body,
'post_excerpt' => $story->teaser,
'post_type' => $pull_post_type,
'ID' => $post_id,
'post_date' => $post_date,
);

//set author
Expand Down
2 changes: 1 addition & 1 deletion get_stories.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function nprstory_cron_pull() {
if ( $pub_option == 'Publish' ) {
$pub_flag = TRUE;
}
$story = $api->update_posts_from_stories($pub_flag);
$story = $api->update_posts_from_stories($pub_flag, "query_number={$i}" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument for update_posts_from_stories just takes an integer

} else {
if ( empty($story) ) {
error_log('NPR Story API: not going to save story. Query '. $query_string .' returned an error '.$api->message->id. ' error'); // debug use
Expand Down
4 changes: 2 additions & 2 deletions push_story.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ function nprstory_api_delete ( $post_ID ) {
$push_post_type = 'post';
}

$api_id_meta = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY );
$api_id = $api_id_meta[0];
$api_id = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY, true );

$post = get_post( $post_ID );
//if the push url isn't set, don't even try to delete.
$push_url = get_option( 'ds_npr_api_push_url' );
Expand Down
15 changes: 15 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function nprstory_settings_init() {

add_settings_section( 'ds_npr_api_get_multi_settings', 'NPR API multiple get settings', 'nprstory_api_get_multi_settings_callback', 'ds_npr_api_get_multi_settings' );

add_settings_field( 'ds_npr_num', 'Number of things to get', 'nprstory_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );

add_settings_field( 'ds_npr_num', 'Number of things to get', 'nprstory_api_num_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this from your patch to prevent the conflict from #39?

register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_num', 'intval' );

Expand All @@ -72,6 +74,10 @@ function nprstory_settings_init() {
//ds_npr_query_publish_
add_settings_field( 'ds_npr_query_publish_' . $i, 'Publish Stories ' . $i, 'nprstory_api_query_publish_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_publish_' . $i , 'nprstory_validation_callback_select');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this from your patch to prevent the conflict from #39?

// Add tags
add_settings_field( 'ds_npr_query_tags_' . $i, 'Add Tags ' . $i, 'ds_npr_api_query_tags_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings', $i );
register_setting( 'ds_npr_api_get_multi_settings', 'ds_npr_query_tags_' . $i );
}

add_settings_field( 'dp_npr_query_run_multi', 'Run the queries on saving changes', 'nprstory_query_run_multi_callback', 'ds_npr_api_get_multi_settings', 'ds_npr_api_get_multi_settings' );
Expand Down Expand Up @@ -170,6 +176,15 @@ function nprstory_api_query_callback( $i ) {

}

function ds_npr_api_query_tags_callback( $i ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this from your patch to prevent the conflict from #39?

$name = 'ds_npr_query_tags_' . $i;
$option = get_option( $name );

echo "<input type='text' value='$option' name='$name' style='width: 300px;' /> <p> Add tag(s) to each story pulled from NPR (comma separated).</p>";
wp_nonce_field( 'nprstory_nonce_ds_npr_tags_' . $i, 'nprstory_nonce_ds_npr_tags_' . $i . '_name', true, true );
echo "<p><hr></p>";
}

function nprstory_api_num_multi_callback() {
$option = get_option('ds_npr_num');
echo "<input type='number' value='$option' name='ds_npr_num' /> <p> Increase the number of queries by changing the number in the field above.";
Expand Down