|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Useful functions for FooGallery PRO Videos |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Returns the number of videos for a specific gallery |
|
8
|
|
|
* @param $post_id |
|
9
|
|
|
* |
|
10
|
|
|
* @return int |
|
11
|
|
|
*/ |
|
12
|
|
|
function foogallery_get_gallery_video_count( $post_id ) { |
|
13
|
|
|
$video_count = get_post_meta( $post_id , FOOGALLERY_VIDEO_POST_META_VIDEO_COUNT, true ); |
|
14
|
|
|
|
|
15
|
|
|
if ( !empty( $video_count ) ) { |
|
16
|
|
|
return absint( $video_count ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
return 0; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Sets the number of videos for a gallery |
|
24
|
|
|
* |
|
25
|
|
|
* @param $post_id |
|
26
|
|
|
*/ |
|
27
|
|
|
function foogallery_set_gallery_video_count( $post_id ) { |
|
28
|
|
|
$video_count = foogallery_calculate_gallery_video_count( $post_id ); |
|
29
|
|
|
if ( $video_count > 0 ) { |
|
30
|
|
|
update_post_meta( $post_id, FOOGALLERY_VIDEO_POST_META_VIDEO_COUNT, $video_count ); |
|
31
|
|
|
} else { |
|
32
|
|
|
delete_post_meta( $post_id, FOOGALLERY_VIDEO_POST_META_VIDEO_COUNT ); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Calculates the number of videos for a gallery |
|
38
|
|
|
* @param $post_id |
|
39
|
|
|
* |
|
40
|
|
|
* @return int |
|
41
|
|
|
*/ |
|
42
|
|
|
function foogallery_calculate_gallery_video_count( $post_id ) { |
|
43
|
|
|
$video_count = 0; |
|
44
|
|
|
$gallery = FooGallery::get_by_id( $post_id ); |
|
45
|
|
|
|
|
46
|
|
|
foreach ( $gallery->attachments() as $attachment ) { |
|
47
|
|
|
if ( foogallery_is_attachment_video( $attachment ) ) { |
|
48
|
|
|
//this attachment is a video |
|
49
|
|
|
$video_count++; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
return $video_count; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Determines if an attachment is a video |
|
57
|
|
|
* |
|
58
|
|
|
* @param $attachment_id |
|
59
|
|
|
* |
|
60
|
|
|
* @param FooGalleryAttachment $foogallery_attachment |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
function foogallery_is_attachment_video( $foogallery_attachment ) { |
|
65
|
|
|
if ( isset( $foogallery_attachment ) && isset( $foogallery_attachment->_post ) ) { |
|
66
|
|
|
if ( 'image/foogallery' === $foogallery_attachment->_post->post_mime_type ) { |
|
67
|
|
|
//we are definitely dealing with a video |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
//allow legacy to override |
|
73
|
|
|
return apply_filters( 'foogallery_is_attachment_video', false, $foogallery_attachment ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the video and image count for a gallery |
|
78
|
|
|
* |
|
79
|
|
|
* @param $total_count |
|
80
|
|
|
* @param $image_count |
|
81
|
|
|
* @param $video_count |
|
82
|
|
|
* |
|
83
|
|
|
* @return string|void |
|
84
|
|
|
*/ |
|
85
|
|
|
function foogallery_gallery_image_count_text( $total_count, $image_count, $video_count ) { |
|
86
|
|
|
//get image count text strings |
|
87
|
|
|
$images_single_text = foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) ); |
|
88
|
|
|
$images_plural_text = foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) ); |
|
89
|
|
|
|
|
90
|
|
|
//get video count text strings |
|
91
|
|
|
$videos_none_text = foogallery_get_setting( 'language_video_count_none_text', __( 'No images or videos', 'foogallery' ) ); |
|
92
|
|
|
$videos_single_text = foogallery_get_setting( 'language_video_count_single_text', __( '1 video', 'foogallery' ) ); |
|
93
|
|
|
$videos_plural_text = foogallery_get_setting( 'language_video_count_plural_text', __( '%s videos', 'foogallery' ) ); |
|
94
|
|
|
|
|
95
|
|
|
if ( 0 == $total_count ) { |
|
96
|
|
|
return $videos_none_text; |
|
97
|
|
|
} else if ( 0 == $video_count ) { |
|
98
|
|
|
//return the original text |
|
99
|
|
|
return sprintf( 1 == $image_count ? $images_single_text : $images_plural_text, $image_count ); |
|
100
|
|
|
} else if ( 0 == $image_count ) { |
|
101
|
|
|
//we only have videos |
|
102
|
|
|
return sprintf( 1 == $video_count ? $videos_single_text : $videos_plural_text, $video_count ); |
|
103
|
|
|
} else { |
|
104
|
|
|
//we have a mix of images and videos |
|
105
|
|
|
return sprintf( 1 == $image_count ? $images_single_text : $images_plural_text, $image_count ) |
|
106
|
|
|
. '; ' . |
|
107
|
|
|
sprintf( 1 == $video_count ? $videos_single_text : $videos_plural_text, $video_count ); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
///** |
|
112
|
|
|
// * Custom encoding for JSON so that special characters are handled correctly |
|
113
|
|
|
// * |
|
114
|
|
|
// * @param $data |
|
115
|
|
|
// * |
|
116
|
|
|
// * @return string |
|
117
|
|
|
// */ |
|
118
|
|
|
//function foogallery_esc_json_encode( $data ) { |
|
119
|
|
|
// if ( defined( 'JSON_HEX_AMP' ) ) { |
|
120
|
|
|
// // This is nice to have, but not strictly necessary since we use _wp_specialchars() below |
|
121
|
|
|
// $data = json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); |
|
122
|
|
|
// } else { |
|
123
|
|
|
// $data = json_encode( $data ); |
|
124
|
|
|
// } |
|
125
|
|
|
// return _wp_specialchars( $data, ENT_QUOTES, false, true ); |
|
126
|
|
|
//} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Cleans the video URL before sending it to the client |
|
130
|
|
|
* |
|
131
|
|
|
* @param String $url The URL of the video in question |
|
132
|
|
|
* |
|
133
|
|
|
* @return String the cleaned up video URL |
|
134
|
|
|
*/ |
|
135
|
|
|
function foogallery_clean_video_url( $url ) { |
|
136
|
|
|
//make the video URL Protocol-relative to cater for sites that are HTTPS |
|
137
|
|
|
$url = str_replace( 'http://', '//', $url ); |
|
138
|
|
|
|
|
139
|
|
|
return apply_filters( 'foogallery_clean_video_url', $url ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Build up the URL to the video URL from a FooGalleryAttachment object |
|
144
|
|
|
* |
|
145
|
|
|
* @param $attachment FooGalleryAttachment The attachment we want to extract the URL from |
|
146
|
|
|
* |
|
147
|
|
|
* @return String the video URL for the attachment |
|
148
|
|
|
*/ |
|
149
|
|
|
function foogallery_get_video_url_from_attachment( $attachment ) { |
|
150
|
|
|
if ( empty( $attachment ) ) return ''; |
|
151
|
|
|
|
|
152
|
|
|
$url = $attachment->custom_url; |
|
153
|
|
|
|
|
154
|
|
|
//append autoplay querystring |
|
155
|
|
|
$autoplay = foogallery_gallery_template_setting( 'video_autoplay', 'yes' ); |
|
156
|
|
|
if ( 'yes' === $autoplay ) { |
|
157
|
|
|
$url = add_query_arg( 'autoplay', '1', $url ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return foogallery_clean_video_url( $url ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
function foogallery_get_video_thumbnail_from_attachment( $attachment ) { |
|
164
|
|
|
$args = array( |
|
165
|
|
|
'width' => 90, |
|
166
|
|
|
'height' => 90, |
|
167
|
|
|
'crop' => true |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return apply_filters( 'foogallery_attachment_resize_thumbnail', $attachment->url, $args, $attachment ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Takes a URL and attempts to return the oEmbed data. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $url The URL to the content that should be attempted to be embedded. |
|
177
|
|
|
* @param array|string $args Optional. Arguments, usually passed from a shortcode. Default empty. |
|
178
|
|
|
* @return false|object False on failure, otherwise the result in the form of an object. |
|
179
|
|
|
* |
|
180
|
|
|
* @see WP_oEmbed::get_data() |
|
181
|
|
|
* |
|
182
|
|
|
* @description This method is a duplicate of the WP_oEmbed::get_data() method as it was only |
|
183
|
|
|
* included since WordPress 4.8.0. |
|
184
|
|
|
*/ |
|
185
|
|
|
function foogallery_oembed_get_data($url, $args = '') { |
|
186
|
|
|
$oembed = _wp_oembed_get_object(); |
|
187
|
|
|
$args = wp_parse_args($args); |
|
188
|
|
|
|
|
189
|
|
|
$provider = $oembed->get_provider($url, $args); |
|
190
|
|
|
|
|
191
|
|
|
if (!$provider) { |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$data = $oembed->fetch($provider, $url, $args); |
|
196
|
|
|
|
|
197
|
|
|
if (false === $data) { |
|
198
|
|
|
return false; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $data; |
|
202
|
|
|
} |