-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmdb-movie-fetcher.php
201 lines (157 loc) · 6.29 KB
/
tmdb-movie-fetcher.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
Plugin Name: TMDb Movie Fetcher
Description: Fetches movie data from TMDb API.
Version: 1.0
Author: Chat GPT
*/
// Replace 'your-tmdb-api-key' with your actual TMDb API key
define('TMDB_API_KEY', 'paste_your_api_from_tmdb');
// Register shortcodes
add_shortcode('tmdb_movie_title', 'tmdb_movie_title_shortcode');
add_shortcode('tmdb_movie_release_date', 'tmdb_movie_release_date_shortcode');
add_shortcode('tmdb_movie_overview', 'tmdb_movie_overview_shortcode');
add_shortcode('tmdb_movie_cast_and_crew', 'tmdb_movie_cast_and_crew_shortcode');
add_shortcode('tmdb_movie_trailer', 'tmdb_movie_trailer_shortcode');
add_shortcode('tmdb_movie_where_to_watch', 'tmdb_movie_where_to_watch_shortcode');
add_shortcode('tmdb_movie_poster', 'tmdb_movie_poster_shortcode');
add_shortcode('tmdb_movie_genres', 'tmdb_movie_genres_shortcode');
// Movie poster shortcode
function tmdb_movie_poster_shortcode($atts) {
$atts = shortcode_atts(array('id' => ''), $atts, 'tmdb_movie_poster');
$movie_id = $atts['id'];
if (empty($movie_id)) {
return 'Error: No movie ID provided.';
}
$movie_data = fetch_movie_data($movie_id);
if ($movie_data) {
$poster_path = $movie_data['poster_path'];
$poster_url = "https://image.tmdb.org/t/p/w500{$poster_path}";
return '<img src="' . $poster_url . '" alt="' . $movie_data['title'] . '">';
} else {
return 'Error: Could not fetch movie data.';
}
}
// Movie genres shortcode
function tmdb_movie_genres_shortcode($atts) {
$atts = shortcode_atts(array('id' => ''), $atts, 'tmdb_movie_genres');
$movie_id = $atts['id'];
if (empty($movie_id)) {
return 'Error: No movie ID provided.';
}
$movie_data = fetch_movie_data($movie_id);
if ($movie_data) {
$genres = $movie_data['genres'];
$genre_names = array_map(function($genre) {
return $genre['name'];
}, $genres);
return implode(', ', $genre_names);
} else {
return 'Error: Could not fetch movie data.';
}
}
// Movie title shortcode
function tmdb_movie_title_shortcode($atts) {
$atts = shortcode_atts(array('id' => ''), $atts, 'tmdb_movie_title');
$movie_id = $atts['id'];
if (empty($movie_id)) {
return 'Error: No movie ID provided.';
}
$movie_data = fetch_movie_data($movie_id);
if ($movie_data) {
return $movie_data['title'];
} else {
return 'Error: Could not fetch movie data.';
}
}
// Movie release date shortcode
function tmdb_movie_release_date_shortcode($atts) {
$atts = shortcode_atts(array('id' => ''), $atts, 'tmdb_movie_release_date');
$movie_id = $atts['id'];
if (empty($movie_id)) {
return 'Error: No movie ID provided.';
}
$movie_data = fetch_movie_data($movie_id);
if ($movie_data) {
return $movie_data['release_date'];
} else {
return 'Error: Could not fetch movie data.';
}
}
// Movie overview shortcode
function tmdb_movie_overview_shortcode($atts) {
$atts = shortcode_atts(array('id' => ''), $atts, 'tmdb_movie_overview');
$movie_id = $atts['id'];
if (empty($movie_id)) {
return 'Error: No movie ID provided.';
}
$movie_data = fetch_movie_data($movie_id);
if ($movie_data) {
return $movie_data['overview'];
} else {
return 'Error: Could not fetch movie data.';
}
}
// Movie cast and crew shortcode
function tmdb_movie_cast_and_crew_shortcode($atts) {
// This function is a placeholder, as the trailer data is not available in the basic movie data.
// You will need to fetch the videos data separately using the TMDb API.
return 'Movie cast and crew data is not available in this version of the plugin.';
}
// Movie trailer shortcode
function tmdb_movie_trailer_shortcode($atts) {
// This function is a placeholder, as the trailer data is not available in the basic movie data.
// You will need to fetch the videos data separately using the TMDb API.
return 'Trailer data is not available in this version of the plugin.';
}
// Movie where to watch shortcode
function tmdb_movie_where_to_watch_shortcode($atts) {
$atts = shortcode_atts(array('id' => ''), $atts, 'tmdb_movie_where_to_watch');
$movie_id = $atts['id'];
if (empty($movie_id)) {
return 'Error: No movie ID provided.';
}
$watch_providers_data = fetch_watch_providers_data($movie_id);
if ($watch_providers_data) {
$providers = $watch_providers_data['results']['US']['flatrate'] ?? []; // Change 'US' to the desired country code
$provider_names = array_map(function($provider) {
return $provider['provider_name'];
}, $providers);
$providers_list = implode(', ', $provider_names);
return !empty($providers_list) ? 'Where to watch: ' . $providers_list : 'No streaming providers found.';
} else {
return 'Error: Could not fetch where to watch data.';
}
}
// Fetch movie data
function fetch_movie_data($movie_id) {
$api_url = "https://api.themoviedb.org/3/movie/{$movie_id}?api_key=" . TMDB_API_KEY;
$response = wp_remote_get($api_url);
if (is_wp_error($response)) {
error_log('TMDb Movie Fetcher: ' . $response->get_error_message());
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['status_code']) && isset($data['status_message'])) {
error_log('TMDb Movie Fetcher: ' . $data['status_message']);
return false;
}
return $data;
}
// where to watch funtion
function fetch_watch_providers_data($movie_id) {
$api_url = "https://api.themoviedb.org/3/movie/{$movie_id}/watch/providers?api_key=" . TMDB_API_KEY;
$response = wp_remote_get($api_url);
if (is_wp_error($response)) {
error_log('TMDb Movie Fetcher: ' . $response->get_error_message());
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['status_code']) && isset($data['status_message'])) {
error_log('TMDb Movie Fetcher: ' . $data['status_message']);
return false;
}
return $data;
}