PluginProbe
Sight – Professional Image Gallery and Portfolio / 1.0.4
Sight – Professional Image Gallery and Portfolio v1.0.4
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
sight / core / core-plugin-functions.php

core-plugin-functions.php in Sight – Professional Image Gallery and Portfolio 1.0.4, at core/core-plugin-functions.php

571 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Functions
4 *
5 * Utility functions.
6 *
7 * @package Sight
8 */
9
10 if ( ! function_exists( 'sight_doing_request' ) ) {
11 /**
12 * Determines whether the current request is a WordPress REST or Ajax request.
13 */
14 function sight_doing_request() {
15 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
16 return true;
17 }
18 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
19 return true;
20 }
21 }
22 }
23
24 if ( ! function_exists( 'sight_is_context_editor' ) ) {
25 /**
26 * Determines whether the current request is from WordPress Editor.
27 */
28 function sight_is_context_editor() {
29 if ( isset( $_REQUEST['context'] ) && 'edit' === $_REQUEST['context'] ) { // Input var ok; sanitization ok.
30 return true;
31 }
32 }
33 }
34
35 if ( ! function_exists( 'sight_is_archive' ) ) {
36 /**
37 * Determines whether the current request is a WordPress REST or Ajax request.
38 */
39 function sight_is_archive() {
40 return apply_filters( 'sight_portfolio_is_archive', false );
41 }
42 }
43
44 if ( ! function_exists( 'sight_encode_data' ) ) {
45 /**
46 * Encode data
47 *
48 * @param mixed $content The content.
49 * @param string $secret_key The key.
50 * @return string
51 */
52 function sight_encode_data( $content, $secret_key = 'sight' ) {
53
54 $content = wp_json_encode( $content );
55
56 return base64_encode( $content );
57 }
58 }
59
60 if ( ! function_exists( 'sight_decode_data' ) ) {
61 /**
62 * Decode data
63 *
64 * @param string $content The content.
65 * @param string $secret_key The key.
66 * @return string
67 */
68 function sight_decode_data( $content, $secret_key = 'sight' ) {
69
70 $content = base64_decode( $content );
71
72 return json_decode( $content );
73 }
74 }
75
76 if ( ! function_exists( 'sight_get_available_image_sizes' ) ) {
77 /**
78 * Get the available image sizes
79 */
80 function sight_get_available_image_sizes() {
81 $wais = & $GLOBALS['_wp_additional_image_sizes'];
82
83 $sizes = array();
84 $image_sizes = get_intermediate_image_sizes();
85
86 if ( is_array( $image_sizes ) && $image_sizes ) {
87 foreach ( $image_sizes as $size ) {
88 if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ), true ) ) {
89 $sizes[ $size ] = array(
90 'width' => get_option( "{$size}_size_w" ),
91 'height' => get_option( "{$size}_size_h" ),
92 'crop' => (bool) get_option( "{$size}_crop" ),
93 );
94 } elseif ( isset( $wais[ $size ] ) ) {
95 $sizes[ $size ] = array(
96 'width' => $wais[ $size ]['width'],
97 'height' => $wais[ $size ]['height'],
98 'crop' => $wais[ $size ]['crop'],
99 );
100 }
101
102 // Size registered, but has 0 width and height.
103 if ( 0 === (int) $sizes[ $size ]['width'] && 0 === (int) $sizes[ $size ]['height'] ) {
104 unset( $sizes[ $size ] );
105 }
106 }
107 }
108
109 return $sizes;
110 }
111 }
112
113 if ( ! function_exists( 'sight_get_image_size' ) ) {
114 /**
115 * Gets the data of a specific image size.
116 *
117 * @param string $size Name of the size.
118 */
119 function sight_get_image_size( $size ) {
120 if ( ! is_string( $size ) ) {
121 return;
122 }
123
124 $sizes = sight_get_available_image_sizes();
125
126 return isset( $sizes[ $size ] ) ? $sizes[ $size ] : false;
127 }
128 }
129
130 if ( ! function_exists( 'sight_get_list_available_image_sizes' ) ) {
131 /**
132 * Get the list available image sizes
133 */
134 function sight_get_list_available_image_sizes() {
135
136 $image_sizes = wp_cache_get( 'sight_available_image_sizes' );
137
138 if ( empty( $image_sizes ) ) {
139 $image_sizes = array();
140
141 $intermediate_image_sizes = get_intermediate_image_sizes();
142
143 foreach ( $intermediate_image_sizes as $size ) {
144 $image_sizes[ $size ] = $size;
145
146 $data = sight_get_image_size( $size );
147
148 if ( isset( $data['width'] ) || isset( $data['height'] ) ) {
149
150 $width = '~';
151 $height = '~';
152
153 if ( isset( $data['width'] ) && $data['width'] ) {
154 $width = $data['width'] . 'px';
155 }
156 if ( isset( $data['height'] ) && $data['height'] ) {
157 $height = $data['height'] . 'px';
158 }
159
160 $image_sizes[ $size ] .= sprintf( ' [%s, %s]', $width, $height );
161 }
162 }
163
164 wp_cache_set( 'sight_available_image_sizes', $image_sizes );
165 }
166
167 return array_merge( array( 'full' => esc_html__( 'Full', 'sight' ) ), $image_sizes );
168 }
169 }
170
171 if ( ! function_exists( 'sight_str_truncate' ) ) {
172 /**
173 * Truncates string with specified length
174 *
175 * @param string $string Text string.
176 * @param int $length Letters length.
177 * @param string $etc End truncate.
178 * @param bool $break_words Break words or not.
179 * @return string
180 */
181 function sight_str_truncate( $string, $length = 80, $etc = '&hellip;', $break_words = false ) {
182 if ( 0 === $length ) {
183 return '';
184 }
185
186 if ( function_exists( 'mb_strlen' ) ) {
187
188 // MultiBite string functions.
189 if ( mb_strlen( $string ) > $length ) {
190 $length -= min( $length, mb_strlen( $etc ) );
191 if ( ! $break_words ) {
192 $string = preg_replace( '/\s+?(\S+)?$/', '', mb_substr( $string, 0, $length + 1 ) );
193 }
194
195 return mb_substr( $string, 0, $length ) . $etc;
196 }
197 } else {
198
199 // Default string functions.
200 if ( strlen( $string ) > $length ) {
201 $length -= min( $length, strlen( $etc ) );
202 if ( ! $break_words ) {
203 $string = preg_replace( '/\s+?(\S+)?$/', '', substr( $string, 0, $length + 1 ) );
204 }
205
206 return substr( $string, 0, $length ) . $etc;
207 }
208 }
209
210 return $string;
211 }
212 }
213
214 if ( ! function_exists( 'sight_get_the_excerpt' ) ) {
215 /**
216 * Filters the number of words in an excerpt.
217 */
218 function sight_get_the_excerpt_length() {
219 return 5000;
220 }
221
222 /**
223 * Get excerpt of post.
224 *
225 * @param int $length Letters length.
226 * @param string $etc End truncate.
227 * @param bool $break_words Break words or not.
228 */
229 function sight_get_the_excerpt( $length = 80, $etc = '&hellip;', $break_words = false ) {
230 add_filter( 'excerpt_length', 'sight_get_the_excerpt_length' );
231
232 $excerpt = get_the_excerpt();
233
234 $func_remove = sprintf( 'remove_%s', 'filter' );
235
236 $func_remove( 'excerpt_length', 'sight_get_the_excerpt_length' );
237
238 return sight_str_truncate( $excerpt, $length, $etc, $break_words );
239 }
240 }
241
242 if ( ! function_exists( 'sight_powerkit_module_enabled' ) ) {
243 /**
244 * Helper function to check the status of powerkit modules
245 *
246 * @param array $name Name of module.
247 */
248 function sight_powerkit_module_enabled( $name ) {
249 if ( function_exists( 'powerkit_module_enabled' ) && powerkit_module_enabled( $name ) ) {
250 return true;
251 }
252 }
253 }
254
255 if ( ! function_exists( 'sight_post_views_enabled' ) ) {
256 /**
257 * Check post views module.
258 *
259 * @return string Type.
260 */
261 function sight_post_views_enabled() {
262
263 // Post Views Counter.
264 if ( class_exists( 'Post_Views_Counter' ) ) {
265 return 'post_views';
266 }
267
268 // Powerkit Post Views.
269 if ( sight_powerkit_module_enabled( 'post_views' ) ) {
270 return 'pk_post_views';
271 }
272 }
273 }
274
275 if ( ! function_exists( 'sight_get_post_types_stack' ) ) {
276 /**
277 * Get portfolio post types.
278 */
279 function sight_get_post_types_stack() {
280
281 $stack = wp_cache_get( 'sight_get_post_types_stack' );
282
283 if ( ! $stack ) {
284
285 $stack = array();
286
287 $post_types = get_post_types( array( 'publicly_queryable' => 1 ), 'objects' );
288
289 foreach ( $post_types as $post_type ) {
290 $stack[ $post_type->name ] = $post_type->label;
291 }
292
293 wp_cache_set( 'sight_get_post_types_stack', $stack );
294 }
295
296 return $stack ? $stack : array();
297 }
298 }
299
300 if ( ! function_exists( 'sight_get_categories_stack' ) ) {
301 /**
302 * Get portfolio categories.
303 */
304 function sight_get_categories_stack() {
305
306 $stack = wp_cache_get( 'sight_get_categories_stack' );
307
308 if ( ! $stack ) {
309
310 $stack = array();
311
312 $categories = get_terms(
313 array(
314 'taxonomy' => 'sight-categories',
315 'hide_empty' => false,
316 )
317 );
318
319 foreach ( $categories as $category ) {
320 $stack[ $category->term_id ] = $category->name;
321 }
322
323 wp_cache_set( 'sight_get_categories_stack', $stack );
324 }
325
326 return $stack ? $stack : array();
327 }
328 }
329
330 if ( ! function_exists( 'sight_portfolio_area_classes' ) ) {
331 /**
332 * Get portfolio area classes.
333 *
334 * @param array $attributes The attributes.
335 * @param array $options The options.
336 */
337 function sight_portfolio_area_classes( $attributes, $options ) {
338 $classes = array( 'sight-portfolio-area' );
339
340 // Enable Lightbox.
341 if ( isset( $attributes['attachment_lightbox'] ) && $attributes['attachment_lightbox'] ) {
342 $classes[] = 'sight-portfolio-area-lightbox';
343 }
344
345 // Apply filters.
346 $classes = apply_filters( 'sight_portfolio_area_classes', $classes, $attributes, $options );
347
348 // Build class.
349 $class = implode( ' ', $classes );
350
351 // Return.
352 return $class;
353 }
354 }
355
356 if ( ! function_exists( 'sight_portfolio_area_main_attrs' ) ) {
357 /**
358 * Output portfolio area main attrs.
359 *
360 * @param array $attributes The attributes.
361 * @param array $options The options.
362 */
363 function sight_portfolio_area_main_attrs( $attributes, $options ) {
364 // Apply filters.
365 $attrs = apply_filters( 'sight_portfolio_area_main_attrs', '', $attributes, $options );
366
367 // Return.
368 return call_user_func( 'printf', '%s', $attrs );
369 }
370 }
371
372 if ( ! function_exists( 'sight_get_youtube_video_id' ) ) {
373 /**
374 * Get Youtube video ID from URL
375 *
376 * @param string $url YouTube URL.
377 */
378 function sight_get_youtube_video_id( $url ) {
379 preg_match( '/(http(s|):|)\/\/(www\.|)yout(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})/i', $url, $results );
380
381 if ( isset( $results[6] ) && $results[6] ) {
382 return $results[6];
383 }
384 }
385 }
386
387 if ( ! function_exists( 'sight_get_local_video_url' ) ) {
388 /**
389 * Get local video URL
390 *
391 * @param string $url Local URL.
392 */
393 function sight_get_local_video_url( $url ) {
394 if ( attachment_url_to_postid( $url ) ) {
395 return $url;
396 }
397 }
398 }
399
400 if ( ! function_exists( 'sight_get_video_background' ) ) {
401 /**
402 * Get element video background
403 *
404 * @param string $type The type.
405 * @param string $location The current location.
406 * @param int $post_id The id of post.
407 * @param string $template Template.
408 * @param bool $controls Display tools.
409 */
410 function sight_get_video_background( $type = 'always', $location = null, $post_id = null, $template = 'default', $controls = true ) {
411
412 if ( sight_is_context_editor() ) {
413 return;
414 }
415
416 if ( is_customize_preview() ) {
417 return;
418 }
419
420 if ( ! $post_id ) {
421 $post_id = get_the_ID();
422 }
423
424 // Params.
425 $url = get_post_meta( $post_id, 'sight_post_video_url', true );
426 $start = get_post_meta( $post_id, 'sight_post_video_bg_start_time', true );
427 $end = get_post_meta( $post_id, 'sight_post_video_bg_end_time', true );
428
429 // Location.
430 if ( $location ) {
431 $support = (array) get_post_meta( $post_id, 'sight_post_video_location', true );
432
433 if ( ! in_array( $location, $support, true ) ) {
434 return;
435 }
436 }
437
438 // Video info.
439 $local_url = sight_get_local_video_url( $url );
440 $youtube_id = sight_get_youtube_video_id( $url );
441
442 // Output.
443 if ( $youtube_id || $local_url ) {
444 // Get video mode.
445 $mode = $youtube_id ? 'youtube' : 'local';
446
447 // Controls.
448 if ( true === $controls ) {
449 $controls = array( 'youtube', 'volume', 'state' );
450
451 if ( 'hover' === $type ) {
452 $controls = array_diff( $controls, array( 'state' ) );
453 }
454 if ( 'local' === $mode ) {
455 $controls = array_diff( $controls, array( 'youtube' ) );
456 }
457 }
458 ?>
459 <div class="sight-portfolio-video-container" data-video-type="<?php echo esc_attr( $type ); ?>" data-video-mode="<?php echo esc_attr( $mode ); ?>" data-youtube-id="<?php echo esc_attr( $youtube_id ); ?>" data-video-start="<?php echo esc_attr( (int) $start ); ?>" data-video-end="<?php echo esc_attr( (int) $end ); ?>">
460 <?php if ( $youtube_id ) { ?>
461 <div class="sight-portfolio-video-inner"></div>
462 <?php } else { ?>
463 <video class="sight-portfolio-video-inner" loop muted>
464 <source src="<?php echo esc_attr( $local_url ); ?>" type="video/webm" />
465 </video>
466 <?php } ?>
467
468 <div class="sight-portfolio-video-loader"></div>
469 </div>
470
471 <?php if ( is_array( $controls ) && $controls ) { ?>
472 <div class="sight-portfolio-video-controls sight-portfolio-video-controls-<?php echo esc_attr( $template ); ?>">
473 <?php if ( in_array( 'youtube', $controls, true ) ) { ?>
474 <a class="sight-portfolio-player-control sight-portfolio-player-link sight-portfolio-player-stop" target="_blank" href="<?php echo esc_url( $url ); ?>">
475 <span class="sight-portfolio-tooltip"><span><?php esc_html_e( 'View on YouTube', 'sight' ); ?></span></span>
476 </a>
477 <?php } ?>
478
479 <?php if ( in_array( 'volume', $controls, true ) ) { ?>
480 <span class="sight-portfolio-player-control sight-portfolio-player-volume sight-portfolio-player-mute"></span>
481 <?php } ?>
482
483 <?php if ( in_array( 'state', $controls, true ) ) { ?>
484 <span class="sight-portfolio-player-control sight-portfolio-player-state sight-portfolio-player-pause"></span>
485 <?php } ?>
486 </div>
487 <?php } ?>
488 <?php
489 }
490 }
491 }
492
493 if ( ! function_exists( 'sight_portfolio_render_style' ) ) {
494 /**
495 * Callback used to render style for portfolio.
496 *
497 * @param array $attributes The attributes.
498 * @param array $options The options.
499 * @param string $id The id.
500 */
501 function sight_portfolio_render_style( $attributes, $options, $id ) {
502 ob_start();
503
504 // Heading Font Size.
505 if ( isset( $attributes['typography_heading'] ) && $attributes['typography_heading'] ) {
506 ?>
507 .sight-block-portfolio-id-{id} .sight-portfolio-entry__heading {
508 font-size: <?php echo esc_attr( $attributes['typography_heading'] ); ?> !important;
509 }
510 <?php
511 }
512
513 // Caption Font Size.
514 if ( isset( $attributes['typography_caption'] ) && $attributes['typography_caption'] ) {
515 ?>
516 .sight-block-portfolio-id-{id} .sight-portfolio-entry__caption {
517 font-size: <?php echo esc_attr( $attributes['typography_caption'] ); ?> !important;
518 }
519 <?php
520 }
521
522 // Heading Color.
523 if ( isset( $attributes['color_heading'] ) && $attributes['color_heading'] ) {
524 ?>
525 .sight-block-portfolio-id-{id} .sight-portfolio-entry__heading,
526 .sight-block-portfolio-id-{id} .sight-portfolio-entry__heading a {
527 color: <?php echo esc_attr( $attributes['color_heading'] ); ?> !important;
528 }
529 <?php
530 }
531
532 // Heading Hover Color.
533 if ( isset( $attributes['color_heading_hover'] ) && $attributes['color_heading_hover'] ) {
534 ?>
535 .sight-block-portfolio-id-{id} .sight-portfolio-entry__heading a:hover {
536 color: <?php echo esc_attr( $attributes['color_heading_hover'] ); ?> !important;
537 }
538 <?php
539 }
540
541 // Caption Color.
542 if ( isset( $attributes['color_caption'] ) && $attributes['color_caption'] ) {
543 ?>
544 .sight-block-portfolio-id-{id} .sight-portfolio-entry__caption {
545 color: <?php echo esc_attr( $attributes['color_caption'] ); ?> !important;
546 }
547 <?php
548 }
549
550 $style = ob_get_clean();
551
552 /*
553 * -------------------------------------
554 */
555
556 // Apply filters.
557 $style = apply_filters( 'sight_portfolio_render_css', $style, $attributes, $options, $id );
558
559 // Replace ids.
560 $style = str_replace( '{id}', $id, $style );
561
562 // Wrap tags.
563 if ( $style ) {
564 $style = sprintf( '<style>%s</style>', $style );
565 }
566
567 // Print.
568 call_user_func( 'printf', '%s', $style );
569 }
570 }
571