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

Publish manager #3

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
13 changes: 12 additions & 1 deletion classes/NPRAPIWordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ function update_posts_from_stories($publish = TRUE ) {
$single_story = FALSE;
}
foreach ($this->stories as $story) {


//check if story is blacklisted
global $wpdb;
$table_name = $wpdb->prefix . "ds_npr_dont_pull_list";
$sql = $wpdb->prepare( "SELECT id FROM $table_name WHERE story_id = '%d'", $story->id );
$blacklist = count($wpdb->get_var($sql));

if ($blacklist >= 1){ //skip story
error_log ("Story number " . $story->id ." Blacklisted");
} else { //process story instance- ends on 380

$exists = new WP_Query( array( 'meta_key' => NPR_STORY_ID_META_KEY,
'meta_value' => $story->id,
'post_type' => $pull_post_type,
Expand Down Expand Up @@ -367,6 +377,7 @@ function update_posts_from_stories($publish = TRUE ) {
}
$ret = wp_insert_post( $args );
}
} //end blacklist story skip
}
if ($single_story){
return $post_id;
Expand Down
31 changes: 31 additions & 0 deletions ds-npr-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,34 @@ function ds_npr_create_post_type() {
)
);
}
/**
*
* enables publish management tools to blacklist/whitelist a specific story from pulling using story id number
* (applies only to NPR pull post type)
*/

//create a db table on install to store the npr_story_ids to not pull again
global $ds_npr_dont_pull_list;
$ds_npr_dont_pull_list = "1.0";

function dontpull_install() {
global $wpdb;
global $ds_npr_dont_pull_list;

$table_name = $wpdb->prefix . "ds_npr_dont_pull_list";

$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
story_id text NOT NULL,
UNIQUE KEY id (id)
);";

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

add_option( "ds_npr_dont_pull_list", $ds_npr_dont_pull_list );
}
register_activation_hook( __FILE__, 'dontpull_install' );

//load project manager php to power db updates, enqueue scripts and create ajax object
require_once('publish_manager.php');
34 changes: 34 additions & 0 deletions publish_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

jQuery(document).ready(function($) {

$( ".blacklist_story" ).click(function() {
var idtoblock = $(this).attr('data-storyid');
var postObject = new Object();
postObject.action = "blacklist";
postObject.storyId = idtoblock;

jQuery.post(ajax_object.ajax_url, postObject, function(response) {
/* console.log(response); */
});

});
$( ".whitelist_Btn" ).on( "click", function() {
var idtoRemove = $("#nprStoryId").val();
var postObject = new Object();
postObject.action = "whitelist";
postObject.storyId = idtoRemove;
jQuery.post(ajax_object.ajax_url, postObject, function(response) {
if (response>0){
$( "<div class='updated ajax-notifcation' style='display:inline-block;'>" + idtoRemove + " has been removed from the blacklist. <button class='closeAjax'>X</button></div>" ).appendTo( "#whitelistUI" );
}
else {
$( "<div class='error ajax-notifcation' style='display:inline-block;'>" + idtoRemove + " was not found. <button class='closeAjax'>X</button></div>" ).appendTo( "#whitelistUI" );
}
/* add listener for notification close button */
$( ".closeAjax" ).on( "click", function() {
$(this).parent().remove();
});
});
});

});
47 changes: 47 additions & 0 deletions publish_manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
// add don't pull button to links under title
add_filter( 'post_row_actions', 'dontpull_row_actions', 10, 2 );
function dontpull_row_actions( $actions, WP_Post $post ) {
$npr_story_id = get_post_meta( $post->ID, 'npr_story_id', true );
$pull_post_type = get_option('ds_npr_pull_post_type');;
if ( $post->post_type != $pull_post_type || $npr_story_id == '' ) { //return default actions if
return $actions;
}

$actions['dontpull'] = "<a class='blacklist_story' data-storyid='" . $npr_story_id . "' title='" . esc_attr( __( 'This story will NOT be pulled again)') ) . "' href='" . get_delete_post_link( $post->ID,"", true ) . "'>" . __( 'Delete Permanently/Don&#8217;t Pull' ) . "</a>";
return $actions;
}

//ajax_object & event handler script load
add_action( 'admin_enqueue_scripts', 'enqueue_manager' );
function enqueue_manager($hook) {
wp_enqueue_script( 'ajax-script', plugins_url( '/publish_manager.js', __FILE__ ), array('jquery'));

// in javascript, object properties are accessed as ajax_object.ajax_url
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

//handler function to process blacklist/whitelist requests
add_action('wp_ajax_blacklist', 'blacklist_callback');
add_action('wp_ajax_whitelist', 'blacklist_callback');
function blacklist_callback() {
global $wpdb;
$number_to_block = $_POST['storyId'];
$action = $_POST['action'];
$table_name = $wpdb->prefix . "ds_npr_dont_pull_list";
global $wpdb;
if ($action == 'blacklist'){
$rows_affected = $wpdb->insert( $table_name, array( 'story_id' => $number_to_block) );
}
else if ($action == 'whitelist'){
$rows_affected = is_numeric($number_to_block) ? $wpdb->delete( $table_name, array( 'story_id' => $number_to_block), array( '%s' )) : 0;

}
echo $rows_affected;
die();
}




12 changes: 12 additions & 0 deletions settings_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ function ds_npr_api_get_multi_options_page() {
<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
</form>
</div>
<p>&nbsp;</p>
<hr align="LEFT" width="90%">

<!-- New Div for Whitelist UI -->
<div>
<h3>Remove Story from Blacklist</h3>
<p>If you have pushed the "Trash/Don't Pull Again" button on the post listing page, that story was added to the database to be ignored. Enter one NPR Story ID and push remove to lift the blacklist from a specific story. </p>
<div id="whitelistUI">NPR Story ID: <input type="text" id="nprStoryId"> (only enter one) <button type="button" class="whitelist_Btn">Remove from List</button> </div>

</div>
<!-- END New Div for Whitelist UI -->

<?php
}

Expand Down