forked from OpenPublicMedia/npr-cds-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_stories_ui.php
120 lines (107 loc) · 4.17 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
<?php
/**
* Functions related to the user interface for fetching stories from the API
*/
// Add the update story column to the page listing the posts for the pull-type
add_filter( 'manage_edit-' . NPR_CDS::get_pull_post_type() . '_columns', 'npr_cds_add_new_story_columns' );
function npr_cds_add_new_story_columns( $cols ) {
$cols['update_story'] = 'Update Story';
return $cols;
}
add_action( 'manage_posts_custom_column', 'npr_cds_update_column_content', 10, 2 );
function npr_cds_update_column_content( string $column_name, int $post_ID ): void {
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 npr_cds_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', 'npr_cds_bulk_action_update_dropdown' );
function npr_cds_bulk_action_update_dropdown(): void {
$pull_post_type = NPR_CDS::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', 'npr_cds_bulk_action_update_action' );
/**
* @throws Exception
*/
function npr_cds_bulk_action_update_action(): void {
// 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':
$post_ids = [];
// 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'] );
}
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 NPR_CDS_WP();
$params = [ 'id' => $api_id ];
$api->request( $params );
$api->parse();
if ( empty( $api->message ) ) {
npr_cds_error_log( 'updating story for CDS ID = ' . $api_id );
$api->update_posts_from_stories();
}
}
}
break;
default:
}
}
function npr_cds_get_stories(): void {
$api_key = get_option( 'npr_cds_token' );
$pull_url = NPR_CDS_PULL_URL;
?>
<div class="wrap">
<h2>Get NPR Stories</h2>
<?php
if ( !$api_key ) {
npr_cds_show_message( 'You do not currently have a CDS token set. <a href="' . admin_url( 'options-general.php?page=npr_cds#npr-general' ) . '">Set your CDS token here.</a>', TRUE );
}
if ( !$pull_url ) {
npr_cds_show_message( 'You do not currently have a CDS Pull URL set. <a href="' . admin_url( 'options-general.php?page=npr_cds#npr-general' ) . '">Set your CDS 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['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( 'npr_cds_nonce_story_id', 'npr_cds_nonce_story_id_field' ); ?>
<input type="submit" name='createDraft' value="Create Draft" />
<input type="submit" name='publishNow' value="Publish Now" />
</form>
</div>
</div><?php
}