PluginProbe
Sight – Professional Image Gallery and Portfolio / trunk
Sight – Professional Image Gallery and Portfolio vtrunk
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 trunk, at core/core-plugin-functions.php

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