-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps-rest-api.php
124 lines (90 loc) · 3.92 KB
/
ps-rest-api.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
<?php
/*
Plugin Name: Horror Movie API Example Plugin
Plugin URI: https://example.com/
Description: Example using the HTTP API to parse JSON from a remote horror movie API
Author: WROX
Author URI: http://wrox.com
*/
// Register custom post type
add_action( 'init', 'ps_register_custom_post_type' );
// Register the movies custom post type
function ps_register_custom_post_type() {
register_post_type( 'movie',
array(
'labels' => array(
'name' => 'Movies',
'singular_name' => 'Movie'
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields' ),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'movies' ),
'show_in_rest' => true,
)
);
}
// Register a custom page for your plugin
add_action( 'admin_menu', 'ps_create_menu' );
function ps_create_menu() {
// Create custom top-level menu
//add_menu_page( 'Movies Page', 'Movies', 'manage_options', 'ps-movies', 'ps_movie_api_results', 'dashicons-smiley', 99 );
add_submenu_page( 'edit.php?post_type=movie', 'Movie Importer Page', 'Import Movies', 'manage_options', 'ps-movies', 'ps_movie_api_results' );
}
// Request and display Movie API data
function ps_movie_api_results() {
// Set your API URL
$request = wp_remote_get( 'https://sampleapis.com/movies/api/horror' );
// If an error is returned, return false to end the request
if( is_wp_error( $request ) ) {
return false;
}
// Retrieve only the body from the raw response
$body = wp_remote_retrieve_body( $request );
// Decode the JSON string
$data = json_decode( $body );
// Verify the $data variable is not empty
if( ! empty( $data ) ) {
echo '<ul>';
// Loop through the returned dataset
foreach( $data as $movies ) {
//check if we already have a total stored for today's date
echo 'CHECKING MOVIE: ' . esc_html( $movies->title );
$existing_movie = get_page_by_title( esc_html( $movies->title ), OBJECT, 'movie' );
if ( !$existing_movie ) {
// No entry exists, so let's create one
echo '<li>';
echo 'Movie imported: ';
echo '<a href="https://www.imdb.com/title/' . esc_attr( $movies->imdbId ) . '" target="_blank">';
echo esc_html( $movies->title );
echo '</a>';
echo '</li>';
// Set values for our new movies post
$new_post = array(
'post_title' => esc_html( $movies->title ),
'post_content' => '<a href="https://www.imdb.com/title/' . esc_attr( $movies->imdbId ) . '">' .esc_html( $movies->title ). '</a>',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'movie',
);
// Insert the post into the database
$new_post_id = wp_insert_post( $new_post );
// Sideload the image to the media library
$image_id = media_sideload_image( esc_url( $movies->posterURL ), $new_post_id, esc_html( $movies->title), 'id' );
// Set image as featured on the post
set_post_thumbnail( $new_post_id, $image_id );
//exit();
}else{
// Movie entry exists, so we are skipping
echo '<li>';
echo 'Movie DUPLICATE: ';
echo '<a href="https://www.imdb.com/title/' . esc_attr( $movies->imdbId ) . '" target="_blank">';
echo esc_html( $movies->title );
echo '</a>';
echo '</li>';
//exit();
}
}
echo '</ul>';
}
}