forked from OpenPublicMedia/nprapi-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_stories_ui.php
145 lines (125 loc) · 4.93 KB
/
get_stories_ui.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Functions related to the user interface for fetching stories from the API
*/
/*
* This will turn on the update story column for all posts
* yes, this is intentionally commented out
*
add_filter( 'manage_posts_columns', 'nprstory_update_column' );
function nprstory_update_column( $defaults ) {
$pull_post_type = DS_NPR_API::nprstory_get_pull_post_type();
global $post_type;
if ( $post_type == $pull_post_type ) {
$defaults['update_story'] = 'Update Story';
}
return $defaults;
}
*/
// Add the update story column to the page listing the posts for the pull-type
add_filter( 'manage_edit-' . DS_NPR_API::nprstory_get_pull_post_type() . '_columns', 'nprstory_add_new_story_columns' );
function nprstory_add_new_story_columns( $cols ) {
$cols['update_story'] = 'Update Story';
return $cols;
}
add_action( 'manage_posts_custom_column', 'nprstory_update_column_content', 10, 2 );
function nprstory_update_column_content( $column_name, $post_ID ) {
if ( $column_name == 'update_story' ) {
$retrieved = get_post_meta( $post_ID, NPR_RETRIEVED_STORY_META_KEY, true );
if ( $retrieved ) {
$api_id = get_post_meta( $post_ID, NPR_STORY_ID_META_KEY, TRUE );
echo nprstory_esc_html( '<a href="' . admin_url( 'edit.php?page=get-npr-stories&story_id=' .$api_id ) . '"> Update </a>' );
}
}
}
//add the bulk action to the dropdown on the post admin page
add_action( 'admin_footer-edit.php', 'nprstory_bulk_action_update_dropdown' );
function nprstory_bulk_action_update_dropdown() {
$pull_post_type = DS_NPR_API::nprstory_get_pull_post_type();
global $post_type;
if ( $post_type == $pull_post_type ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('<option>').val('updateNprStory').text('<?php _e( 'Update NPR Story' ); ?>').appendTo("select[name='action']");
$('<option>').val('updateNprStory').text('<?php _e( 'Update NPR Story' ); ?>').appendTo("select[name='action2']");
});
</script>
<?php
}
}
//do the new bulk action
add_action( 'load-edit.php', 'nprstory_bulk_action_update_action' );
function nprstory_bulk_action_update_action() {
// 1. get the action
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
$action = $wp_list_table->current_action();
switch ( $action ) {
// 3. Perform the action
case 'updateNprStory':
// make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids'
if ( isset( $_REQUEST['post'] ) ) {
$post_ids = array_map( 'intval', $_REQUEST['post'] );
}
$exported = 0;
foreach( $post_ids as $post_id ) {
$api_id = get_post_meta( $post_id, NPR_STORY_ID_META_KEY, TRUE );
// don't run API queries for posts that have no ID
// @todo: why do some posts have no ID
// @todo: oh, it's only imported drafts that don't have an ID
if ( !empty( $api_id ) ) {
$api = new NPRAPIWordpress();
$params = [ 'id' => $api_id, 'apiKey' => get_option( 'ds_npr_api_key' ) ];
$api->request( $params, 'query', get_option( 'ds_npr_api_pull_url' ) );
$api->parse();
if ( empty( $api->message ) || $api->message->level != 'warning' ) {
nprstory_error_log( 'updating story for API ID=' . $api_id );
$story = $api->update_posts_from_stories();
}
}
}
// build the redirect url
//$sendback = add_query_arg( array('exported' => $exported, 'ids' => join(',', $post_ids) ), $sendback );
break;
default: return;
}
// ...
// 4. Redirect client
//wp_redirect($sendback);
//exit();
}
function nprstory_get_stories() {
global $is_IE;
$api_key = get_option( 'ds_npr_api_key' );
$pull_url = get_option( 'ds_npr_api_pull_url' );
?>
<div class="wrap">
<h2>Get NPR Stories</h2>
<?php
if ( !$api_key ) {
nprstory_show_message( 'You do not currently have an API Key set. <a href="' . admin_url( 'options-general.php?page=ds_npr_api' ) . '">Set your API Key here.</a>', TRUE );
}
if ( !$pull_url ) {
nprstory_show_message( 'You do not currently have an API Pull URL set. <a href="' . admin_url( 'options-general.php?page=ds_npr_api' ) . '">Set your API Pull URL here.</a>', TRUE );
}
// Get the story ID from the URL, then paste it into the input's value field with esc_attr
$story_id = '';
if ( ( isset( $_POST ) && isset( $_POST[ 'story_id' ] ) ) || ( isset( $_GET ) && isset( $_GET['story_id'] ) ) ) {
if ( !empty( $_POST['story_id'] ) ) {
$story_id = sanitize_text_field( $_POST['story_id'] );
}
if ( !empty( $_GET['story_id'] ) ) {
$story_id = sanitize_text_field( $_GET['story_id'] );
}
}
?>
<div style="float: left;">
<form action="" method="POST">
Enter an NPR Story ID or URL: <input type="text" name="story_id" value="<?php echo esc_attr( $story_id ); ?>" />
<?php wp_nonce_field( 'nprstory_nonce_story_id', 'nprstory_nonce_story_id_field' ); ?>
<input type="submit" name='createDaft' value="Create Draft" />
<input type="submit" name='publishNow' value="Publish Now" />
</form>
</div>
</div><?php
}