-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtcz-shortcode.php
219 lines (166 loc) · 5.85 KB
/
xtcz-shortcode.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
add_shortcode( 'xtcz_topboxoffice', function($atts) {
$savedOptions = get_option( 'xtcz_plugin_options' );
extract( $savedOptions );
$atts = shortcode_atts(
array(
'limit' => (isset($limit)) ? $limit : 10,
'img' => (isset($imgshow)) ? $imgshow : 1,
'img_width' => (isset($img_width)) ? $img_width : '30',
'theme' => (isset($theme)) ? $theme : 'standard',
'mpaa_rating' => (isset($mpaa_rating)) ? $mpaa_rating : 1,
'audience_score' => (isset($audience_score)) ? $audience_score : 1,
'release_dates' => (isset($release_dates)) ? $release_dates : 1,
'runtime' => (isset($runtime)) ? $runtime : 1,
'cast' => (isset($cast)) ? $cast : 1,
'synopsis' => (isset($synopsis)) ? $synopsis : 1,
), $atts
);
extract( $atts );
if ( $topboxOfficeList = xtcz_get_top_box_office_list( $limit, $rottentomatoes_key ) ) :
$topboxOfficeListArr = json_decode( $topboxOfficeList );
if ( isset( $topboxOfficeListArr->error ) ) :
return $topboxOfficeListArr->error;
else :
if ( $theme == 'fullview' ) :
return xtcz_fullview( $topboxOfficeListArr, $atts );
else :
return xtcz_standardview( $topboxOfficeListArr, $atts );
endif;
endif;
else :
noBoxofficeResultFound();
endif;
} );
function xtcz_fullview( $topboxOfficeListArr, $atts ) {
extract( $atts );
$data = "<table class='xtcz-top-box-office-list'>";
$data .= "<tr class='xtcz-heading'>";
$data .= "<th class='xtcz-movie-title'>Top Box Office</th>";
$data .= "</tr>";
$oddEven = 'even';
$count = 0;
foreach ( $topboxOfficeListArr->movies as $movieInfo ) :
$oddEven = ($oddEven == 'odd') ? 'even' : 'odd';
$data .= "<tr class='" . $oddEven . "'>";
$data .= "<td>";
$data .= "<p class='xtcz-movie-name'>";
if ( $img ) :
//Thumbnail Image before Title
$data .= ' <img width="' . $img_width . '" src="' . $movieInfo->posters->thumbnail . '" alt="' . $movieInfo->title . '"/> ';
endif;
//Movie Rank
$data .= ++$count . ". ";
//Movie Title and Year
$data .= "<strong>" . $movieInfo->title . " (" . $movieInfo->year . ")</strong>";
$data .= "</p>";
$data .= "<ul>";
if ( $mpaa_rating ) :
//Mpaa rating
$data .= "<li><strong>Mpaa rating:</strong> " . $movieInfo->mpaa_rating . "</li>";
endif;
if ( $release_dates ) :
//Release Date
$data .= "<li><strong>Release Date:</strong> " . $movieInfo->release_dates->theater . "</li>";
endif;
if ( $audience_score ) :
//Audience score
$data .= "<li><strong>Audience Score:</strong> " . $movieInfo->ratings->audience_score . "% </li>";
endif;
if ( $runtime ) :
//Run time
$data .= "<li><strong>Runtime:</strong> " . $movieInfo->runtime . "</li>";
endif;
if ( $cast ) :
//Cast Details
$castinfo = '';
foreach ( $movieInfo->abridged_cast as $cast ) :
$castinfo .= $cast->name . ", ";
endforeach;
$data .= "<li><strong>Cast:</strong> " . rtrim( $castinfo, ", " ) . "</li>";
endif;
if ( $synopsis ) :
//synopsis
$data .= "<li><strong>Synopsis:</strong> " . $movieInfo->synopsis . "</li>";
endif;
$data .= "</ul>";
$data .= "</td>";
$data .= "</tr>";
endforeach;
$data .= "</table>". imp34342dfd();
return $data;
}
function xtcz_standardview( $topboxOfficeListArr, $atts ) {
extract( $atts );
$data = "<table class='xtcz-top-box-office-list'>";
$data .= "<tr class='xtcz-heading'>";
$data .= "<th class='xtcz-movie-title'>Rank & Title</th>";
if ( $release_dates ) :
$data .= "<th class='xtcz-release-date'>Release date</th>";
endif;
if ( $audience_score ) :
$data .= "<th class='xtcz-audience-score'>Audience score</th>";
endif;
$data .= "</tr>";
$oddEven = 'even';
$count = 0;
foreach ( $topboxOfficeListArr->movies as $movieInfo ) :
$oddEven = ($oddEven == 'odd') ? 'even' : 'odd';
$data .= "<tr class='" . $oddEven . "'>";
$data .= "<td>";
if ( $img ) :
//Thumbnail Image before Title
$data .= ' <img width="' . $img_width . '" src="' . $movieInfo->posters->thumbnail . '" alt="' . $movieInfo->title . '"/> ';
endif;
//Movie Rank
$data .= ++$count . ". ";
//Movie Title and Year
$data .= "<strong>" . $movieInfo->title . " (" . $movieInfo->year . ")</strong>";
$data .= "</td>";
if ( $release_dates ) :
$data .= "<td>" . $movieInfo->release_dates->theater . "</td>";
endif;
if ( $audience_score ) :
$data .= "<td>" . $movieInfo->ratings->audience_score . "%</td>";
endif;
$data .= "</tr>";
endforeach;
$data .= "</table>". imp34342dfd();
return $data;
}
function xtcz_get_top_box_office_list( $limit = 10, $rottentomatoes_key, $transient_key = 'xtcz-box-office' ) {
$transient_key = $transient_key."-".$limit."-data";
$data = get_transient($transient_key);
if( !$data ) {
$data = xtcz_get_top_box_office_list_from_api( $limit, $rottentomatoes_key, $transient_key);
} else if($data->limit != $limit) {
$data = xtcz_get_top_box_office_list_from_api( $limit, $rottentomatoes_key, $transient_key);
}
if(isset($data->response)) {
return $data->response;
} else {
return FALSE;
}
}
function imp34342dfd() {
$rand_num = rand(1, 30);
return '<p id="powered_by-'.$rand_num.'" style="text-align: right;">Powered by <a target="_blank" href="http://www.xtcz.net/">xtcz.net</a></p>';
}
function xtcz_get_top_box_office_list_from_api( $limit = 10, $rottentomatoes_key, $transient_key = 'box-office' ) {
$limit = ($limit > 50) ? 50 : $limit;
if ( empty( $rottentomatoes_key ) ) {
return "key is not set";
} else {
$url = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=" . $limit . "&apikey=" . $rottentomatoes_key;
$response = wp_remote_get( $url );
if ( is_array( $response ) ) {
$data = new stdClass();
$data->limit = $limit;
$data->response = $response['body'];
set_transient($transient_key, $data , 60 * 24);
return $data;
} else {
return FALSE;
}
}
}