-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmyarcade_api.php
337 lines (282 loc) · 9.64 KB
/
myarcade_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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* MyArcadePlugin Theme API helps theme developers to create MyArcadePlugin Pro compatible themes.
*
* Use this function only within the loop.
*
* @package MyArcadePlugin Theme API
* @author Daniel Bakovic - http://myarcadeplugin.com
* @version 3.0.0
*/
if ( ! function_exists( 'myarcade_title' ) ) {
/**
* Display or retrieve the title of the current post/game. The title can be cutted after $chars.
* Words will not be cutted off (wordwrap).
*
* @param int $chars Optional. Max. length of the title
* @param bool $echo Optional. default to true. Whether to display or return.
* @return null|string Null on no title. String if $echo parameter is false.
*/
function myarcade_title( $chars = 0, $echo = true ) {
global $post;
$chars = intval($chars);
$title = strip_tags( the_title('', '', false) ); // before, after, echo
if ( $chars > 0 ) {
if ( (strlen($title) > $chars) ) {
$title = mb_substr($title, 0, $chars);
$title = mb_substr($title, 0, -strlen(strrchr($title, ' '))); // Wordwrap
if ( strlen($title) < 4 ) {
$title = mb_substr( the_title('', '', false), 0, $chars );
}
$title .= ' ..';
}
}
if ( $echo == true ) {
echo $title;
}
else {
return $title;
}
}
}
if ( ! function_exists( 'myarcade_description' ) ) {
/**
* Display or retrieve the description of the current game. The description can be cutted after $chars.
* Words will not be cutted off (wordwrap).
*
* @param int $chars Optional. Max. length of the description
* @param bool $echo Optional. default to true. Whether to display or return.
* @return null|string Null on no description. String if $echo parameter is false.
*/
function myarcade_description ($chars = 0, $echo = true) {
global $post;
$chars = intval($chars);
$description = get_post_meta($post->ID, 'mabp_description', true);
if ( $chars > 0 ) {
if ( (strlen($description) > $chars) ) {
$description = mb_substr($description, 0, $chars);
$description = mb_substr($description, 0, -strlen(strrchr($description, ' '))); // Wordwrap
if ( strlen($description) < 4 ) {
$description = mb_substr( get_post_meta($post->ID, 'mabp_description', true), 0, $chars );
}
$description .= ' ..';
}
}
if ( $echo == true ) {
echo $description;
}
else {
return $description;
}
}
}
if ( !function_exists( 'myarcade_excerpt' ) ) {
/**
* Display or retrieve the excerpt of a game post. All tags will be removed.
*
* @param int $length Character length of the excerpt
* @param bool $echo Optional. Return or echo the result
*/
function myarcade_excerpt( $length = false, $echo = true ) {
global $post;
// Get post excerpt
$text = strip_shortcodes( $post->post_content );
//$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = wp_trim_words( $text, 100, '' );
if ( $length ) {
if ( strlen($text) > $length ) {
$text = mb_substr($text, 0, $length).' [...]';
}
}
if ( $echo ) {
echo $text;
}
else {
return $text;
}
}
}
if ( ! function_exists( 'myarcade_thumbnail' ) ) {
/**
* Display the game thumbnail of the current game.
* If no thumbnail is available the function will display a default thumbnail located in the template directory.
*
* @param int $width Optional. Width of the thumbnail in px. Default: 100
* @param int $height Optional. Height of the thumbnail in px. Default: 100
* @param string $class Optional. CSS class fot the image tag
*/
function myarcade_thumbnail( $width = 100, $height = 100, $class = '' ) {
global $post;
if ( ! empty($class) ) { $class = 'class="'.$class.'"'; }
$thumbnail_id = get_post_thumbnail_id();
$thumbnail = '';
if ( ! empty( $thumbnail_id ) ) {
$thumbnail_array = wp_get_attachment_image_src( $thumbnail_id );
if ( ! empty( $thumbnail_array ) ) {
$thumbnail = $thumbnail_array[0];
}
}
if ( ! $thumbnail ) {
$thumbnail = get_post_meta($post->ID, "mabp_thumbnail_url", true);
}
if ( preg_match('|^(http).*|i', $thumbnail) == 0 ) {
// No Thumbail available.. get the default thumb
$thumbnail = get_template_directory_uri().'/images/noimg.png';
}
echo '<img src="'.$thumbnail.'" width="'.$width.'" height="'.$height.'" '.$class.' alt="'.the_title_attribute( array( 'echo' => false ) ).'" />';
}
}
if ( ! function_exists( 'myarcade_thumbnail_url' ) ) {
/**
* Display the url of the current game thumbnail
*
* @return string
*/
function myarcade_thumbnail_url() {
global $post;
$thumbnail_id = get_post_thumbnail_id();
$thumbnail = '';
if ( ! empty( $thumbnail_id ) ) {
$thumbnail_array = wp_get_attachment_image_src( $thumbnail_id );
if ( ! empty( $thumbnail_array ) ) {
$thumbnail = $thumbnail_array[0];
}
}
if ( ! $thumbnail ) {
$thumbnail = get_post_meta($post->ID, "mabp_thumbnail_url", true);
}
if ( preg_match('|^(http).*|i', $thumbnail) == 0 ) {
// No Thumbail available.. get the default thumb
$thumbnail = get_template_directory_uri().'/images/noimg.png';
}
return $thumbnail;
}
}
if ( !function_exists('myarcade_instructions') ) {
/**
* Display or retrieve the game instructions
* @since 1.0
*/
function myarcade_instructions($echo = true) {
global $post;
$instructions = get_post_meta($post->ID, "mabp_instructions", true);
if ($echo == true) { echo $instructions; } else { return $instructions; }
}
}
if ( ! function_exists( 'myarcade_count_screenshots' ) ) {
/**
* Retrieve the number of available screenshots for the current game.
*
* @return int
*/
function myarcade_count_screenshots () {
global $post;
$screen_count = 0;
for ($screen_nr = 1; $screen_nr <= 4; $screen_nr++) {
if ( preg_match('|^(http).*|i', get_post_meta($post->ID, "mabp_screen".$screen_nr."_url", true)) ) {
$screen_count++;
}
}
return intval($screen_count);
}
}
if ( ! function_exists( 'myarcade_screenshot' ) ) {
/**
* Display the given screen shot of the current game.
*
* @param int $width Optional. Width of the screen shot in px. Default: 450
* @param int $height Optional. Height of the screen shot in px. Default: 350
* @param int $screen_nr Optional. The number of the screen (1..4). Default 1
* @param string $class Optional. CSS class fot the image tag
* @param bool $echo Optional. Return or echo the result
*/
function myarcade_screenshot ($width = 450, $height = 300, $screen_nr = 1, $class = '', $echo = true) {
global $post;
$output = '';
if ( !empty($class) ) { $class = 'class="'.$class.'"'; }
$screenshot = get_post_meta($post->ID, "mabp_screen".$screen_nr."_url", true);
if ( preg_match('|^(http).*|i', $screenshot) ) {
$output = '<img src="'.$screenshot.'" width="'.$width.'" height="'.$height.'" '.$class.' alt="" />';
}
if ( $echo == true ) {
echo $output;
}
else {
return $output;
}
}
}
if ( !function_exists('myarcade_get_screenshot_url') ) {
/**
* Retrieves the url of a screenshot
*
* @param int $screen_nr Optional. The number of the screen (1..4). Default 1
* @param bool $echo Optional. Return or echo the result
*/
function myarcade_get_screenshot_url ( $screen_nr = 1, $echo = true ) {
global $post;
$screenshot = get_post_meta($post->ID, "mabp_screen".$screen_nr."_url", true);
if ( $echo == true ) {
echo $screenshot;
}
else {
return $screenshot;
}
}
}
if ( ! function_exists( 'myarcade_all_screenshots' ) ) {
/**
* Display all available screen shot of the current game.
*
* @param int $width Optional. Width of the screen shot in px. Default: 450
* @param int $height Optional. Height of the screen shot in px. Default: 350
* @param int $screen_nr Optional. The number of the screen (1..4). Default 1
* @param string $class Optional. CSS class fot the image tag
*/
function myarcade_all_screenshots ($width = 450, $height = 300, $class = '') {
global $post;
if ( !empty($class) ) { $class = 'class="'.$class.'"'; }
for ($screen_nr = 1; $screen_nr <= 4; $screen_nr++) {
$screenshot = get_post_meta($post->ID, "mabp_screen".$screen_nr."_url", true);
if ( preg_match('|^(http).*|i', $screenshot) ) {
echo '<a href="'.$screenshot.'" title="" rel="lightbox"><img src="'.$screenshot.'" width="'.$width.'" height="'.$height.'" '.$class.' alt="" /></a>';
}
}
}
}
if ( ! function_exists( 'myarcade_video' ) ) {
/**
* Display (Embed) the gameplay video of the current game.
*
* @param int $width Optional. Width of the video in px. Default: 450
* @param int $height Optional. Height of the video in px. Default: 350
* @return string Video embed code
*/
function myarcade_video( $width = 400, $height = 336 ) {
global $post;
$video_url = get_post_meta($post->ID, "mabp_video_url", true);
if ( $video_url ) {
// Get the embed code
return wp_oembed_get( $video_url, array( 'width' => $width, 'height' => $height ) );
}
return false;
}
}
if ( ! function_exists( 'myarcade_format_number' ) ) {
/**
* Facebook like number formatting
*
* @param int $n number
* @return string number
*/
function myarcade_format_number( $n ) {
$s = array("K", "M", "G", "T");
$out = "";
while ($n >= 1000 && count($s) > 0) {
$n = $n / 1000.0;
$out = array_shift($s);
}
return round($n, max(0, 3 - strlen((int)$n))) ."$out";
}
}