PluginProbe
Scrollsequence – Cinematic Scroll Image Animation Plugin / 1.5.5
Scrollsequence – Cinematic Scroll Image Animation Plugin v1.5.5
1.4.3 1.4.4 1.4.5 1.4.6 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 trunk 0.9.92 0.9.93 0.9.94 0.9.96 1.0.072 1.0.073 All 61 releases
scrollsequence / includes / carbonfields / htmlburger / carbon-fields / core / Helper / Helper.php

Helper.php in Scrollsequence – Cinematic Scroll Image Animation Plugin 1.5.5, at includes/carbonfields/htmlburger/carbon-fields/core/Helper/Helper.php

724 lines 21.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Carbon_Fields\Helper;
4
5 use Carbon_Fields\Datastore\Datastore;
6 use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7 use WP_Query;
8
9 /**
10 * Helper functions and main initialization class.
11 */
12 class Helper {
13
14 /**
15 * Get a field from a specific container type or id
16 *
17 * @param string $container_type Container type to search in. Optional if $container_id is supplied
18 * @param string $container_id Container id to search in. Optional if $container_type is supplied
19 * @param string $field_name Field name to search for
20 * @return \Carbon_Fields\Field\Field
21 */
22 public static function get_field( $container_type, $container_id, $field_name ) {
23 \Carbon_Fields\Carbon_Fields::verify_fields_registered();
24
25 $repository = \Carbon_Fields\Carbon_Fields::resolve( 'container_repository' );
26 if ( $container_id ) {
27 return $repository->get_field_in_container( $field_name, $container_id );
28 }
29 return $repository->get_field_in_containers( $field_name, $container_type );
30 }
31
32 /**
33 * Get a clone of a field with a value loaded.
34 * WARNING: The datastore is cloned!
35 *
36 * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.)
37 * @param string $container_type Container type to search in. Optional if $container_id is supplied
38 * @param string $container_id Container id to search in. Optional if $container_type is supplied
39 * @param string $field_name Field name to search for
40 * @return \Carbon_Fields\Field\Field
41 */
42 public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) {
43 $field = static::get_field( $container_type, $container_id, $field_name );
44
45 if ( ! $field ) {
46 return null;
47 }
48
49 $clone = clone $field;
50 if ( $object_id !== null ) {
51 $clone->set_datastore( clone $clone->get_datastore(), $clone->has_default_datastore() );
52 $clone->get_datastore()->set_object_id( $object_id );
53 }
54 return $clone;
55 }
56
57 /**
58 * Execute an action with a clone of a field with a value loaded.
59 * WARNING: The datastore reference is kept!
60 *
61 * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.)
62 * @param string $container_type Container type to search in. Optional if $container_id is supplied
63 * @param string $container_id Container id to search in. Optional if $container_type is supplied
64 * @param string $field_name Field name to search for
65 * @param \Closure $action Action to execute
66 * @return void|mixed
67 */
68 public static function with_field_clone( $object_id, $container_type, $container_id, $field_name, $action ) {
69 $field = static::get_field( $container_type, $container_id, $field_name );
70
71 if ( ! $field ) {
72 return;
73 }
74
75 $clone = clone $field;
76 $datastore = $clone->get_datastore();
77 $datastore_object_id = $datastore->get_object_id();
78
79 if ( $object_id !== null ) {
80 $datastore->set_object_id( $object_id );
81 }
82
83 $result = $action($clone);
84
85 if ( $object_id !== null ) {
86 $datastore->set_object_id( $datastore_object_id );
87 }
88
89 return $result;
90 }
91
92 /**
93 * Get a value formatted for end-users
94 *
95 * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.)
96 * @param string $container_type Container type to search in
97 * @param string $container_id
98 * @param string $field_name Field name
99 * @return mixed
100 */
101 public static function get_value( $object_id, $container_type, $container_id, $field_name ) {
102 return static::with_field_clone(
103 $object_id,
104 $container_type,
105 $container_id,
106 $field_name,
107 function( $field ) {
108 if ( ! $field ) {
109 return '';
110 }
111 /** @var \Carbon_Fields\Field\Field $field */
112 $field->load();
113 return $field->get_formatted_value();
114 }
115 );
116 }
117
118 /**
119 * Set value for a field
120 *
121 * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.)
122 * @param string $container_type Container type to search in
123 * @param string $container_id
124 * @param string $field_name Field name
125 * @param array $value Field expects a `value_set`. Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md
126 * @return void
127 */
128 public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) {
129 static::with_field_clone(
130 $object_id,
131 $container_type,
132 $container_id,
133 $field_name,
134 function( $field ) use ( $container_type, $container_id, $field_name, $value ) {
135 if ( ! $field ) {
136 $container_message = $container_id ? 'in container with id "' . $container_id . '"' : 'in containers of type "' . $container_type . '"';
137 Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern ' . $container_message . ': ' . $field_name );
138 return;
139 }
140 /** @var \Carbon_Fields\Field\Field $field */
141 $field->set_value( $value );
142 $field->save();
143 }
144 );
145 }
146
147 /**
148 * Shorthand for get_post_meta().
149 * Uses the ID of the current post in the loop.
150 *
151 * @param string $name Field name
152 * @param string $container_id
153 * @return mixed
154 */
155 public static function get_the_post_meta( $name, $container_id = '' ) {
156 return static::get_post_meta( get_the_ID(), $name, $container_id );
157 }
158
159 /**
160 * Get post meta field for a post.
161 *
162 * @param int $id Post ID
163 * @param string $name Field name
164 * @param string $container_id
165 * @return mixed
166 */
167 public static function get_post_meta( $id, $name, $container_id = '' ) {
168 $id = apply_filters( 'carbon_get_post_meta_post_id', $id, $name, $container_id );
169 return static::get_value( $id, 'post_meta', $container_id, $name );
170 }
171
172 /**
173 * Set post meta field for a post.
174 *
175 * @param int $id Post ID
176 * @param string $name Field name
177 * @param array $value
178 * @param string $container_id
179 */
180 public static function set_post_meta( $id, $name, $value, $container_id = '' ) {
181 static::set_value( $id, 'post_meta', $container_id, $name, $value );
182 }
183
184 /**
185 * Get theme option field value.
186 *
187 * @param string $name Field name
188 * @param string $container_id
189 * @return mixed
190 */
191 public static function get_theme_option( $name, $container_id = '' ) {
192 return static::get_value( null, 'theme_options', $container_id, $name );
193 }
194
195 /**
196 * Set theme option field value.
197 *
198 * @param string $name Field name
199 * @param array $value
200 * @param string $container_id
201 */
202 public static function set_theme_option( $name, $value, $container_id = '' ) {
203 static::set_value( null, 'theme_options', $container_id, $name, $value );
204 }
205
206 /**
207 * Get network option field value for the main site.
208 *
209 * @param string $name Field name
210 * @param string $container_id
211 * @return mixed
212 */
213 public static function get_the_network_option( $name, $container_id = '' ) {
214 $id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
215 return static::get_network_option( $id, $name, $container_id );
216 }
217
218 /**
219 * Get network option field value for a site.
220 *
221 * @param string $id Site ID
222 * @param string $name Field name
223 * @param string $container_id
224 * @return mixed
225 */
226 public static function get_network_option( $id, $name, $container_id = '' ) {
227 return static::get_value( $id, 'network', $container_id, $name );
228 }
229
230 /**
231 * Set network option field value for a site.
232 *
233 * @param string $id Site ID
234 * @param string $name Field name
235 * @param array $value
236 * @param string $container_id
237 */
238 public static function set_network_option( $id, $name, $value, $container_id = '' ) {
239 static::set_value( $id, 'network', $container_id, $name, $value );
240 }
241
242 /**
243 * Get term meta field for a term.
244 *
245 * @param int $id Term ID
246 * @param string $name Field name
247 * @param string $container_id
248 * @return mixed
249 */
250 public static function get_term_meta( $id, $name, $container_id = '' ) {
251 return static::get_value( $id, 'term_meta', $container_id, $name );
252 }
253
254 /**
255 * Set term meta field for a term.
256 *
257 * @param int $id Term ID
258 * @param string $name Field name
259 * @param array $value
260 * @param string $container_id
261 */
262 public static function set_term_meta( $id, $name, $value, $container_id = '' ) {
263 static::set_value( $id, 'term_meta', $container_id, $name, $value );
264 }
265
266 /**
267 * Get user meta field for a user.
268 *
269 * @param int $id User ID
270 * @param string $name Field name
271 * @param string $container_id
272 * @return mixed
273 */
274 public static function get_user_meta( $id, $name, $container_id = '' ) {
275 return static::get_value( $id, 'user_meta', $container_id, $name );
276 }
277
278 /**
279 * Set user meta field for a user.
280 *
281 * @param int $id User ID
282 * @param string $name Field name
283 * @param array $value
284 * @param string $container_id
285 */
286 public static function set_user_meta( $id, $name, $value, $container_id = '' ) {
287 static::set_value( $id, 'user_meta', $container_id, $name, $value );
288 }
289
290 /**
291 * Get comment meta field for a comment.
292 *
293 * @param int $id Comment ID
294 * @param string $name Field name
295 * @param string $container_id
296 * @return mixed
297 */
298 public static function get_comment_meta( $id, $name, $container_id = '' ) {
299 return static::get_value( $id, 'comment_meta', $container_id, $name );
300 }
301
302 /**
303 * Set comment meta field for a comment.
304 *
305 * @param int $id Comment ID
306 * @param string $name Field name
307 * @param array $value
308 * @param string $container_id
309 */
310 public static function set_comment_meta( $id, $name, $value, $container_id = '' ) {
311 static::set_value( $id, 'comment_meta', $container_id, $name, $value );
312 }
313
314 /**
315 * Get nav menu item meta field for a nav menu item.
316 *
317 * @param int $id Nav menu item ID
318 * @param string $name Field name
319 * @param string $container_id
320 * @return mixed
321 */
322 public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) {
323 return static::get_value( $id, 'nav_menu_item', $container_id, $name );
324 }
325
326 /**
327 * Set nav menu item meta field for a nav menu item.
328 *
329 * @param int $id Nav menu item ID
330 * @param string $name Field name
331 * @param array $value
332 * @param string $container_id
333 */
334 public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) {
335 static::set_value( $id, 'nav_menu_item', $container_id, $name, $value );
336 }
337
338 /**
339 * Recursive sorting function by array key.
340 *
341 * @param array &$array The input array.
342 * @param int $sort_flags Flags for controlling sorting behavior.
343 * @return boolean
344 */
345 public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) {
346 if ( ! is_array( $array ) ) {
347 return false;
348 }
349 ksort( $array, $sort_flags );
350 foreach ( $array as $key => $value ) {
351 self::ksort_recursive( $array[ $key ], $sort_flags );
352 }
353 return true;
354 }
355
356 /**
357 * Get the relation type from an array similar to how meta_query works in WP_Query
358 *
359 * @param array $array
360 * @param array<string> $allowed_relations
361 * @param string $relation_key
362 * @return string
363 */
364 public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) {
365 $allowed_relations = array_values( $allowed_relations );
366 $allowed_relations = array_map( 'strtoupper', $allowed_relations );
367 $relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : '';
368
369 if ( isset( $array[ $relation_key ] ) ) {
370 $relation = strtoupper( $array[ $relation_key ] );
371 }
372
373 if ( ! in_array( $relation, $allowed_relations ) ) {
374 Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' .
375 'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' );
376 }
377
378 return $relation;
379 }
380
381 /**
382 * Normalize a label by updating case, stripping common prefixes etc.
383 *
384 * @param string $label
385 * @return string
386 */
387 public static function normalize_label( $label ) {
388 // remove the leading underscore(if it's there)
389 $label = preg_replace( '~^_~', '', $label );
390
391 // remove the leading "crb_"(if it's there)
392 $label = preg_replace( '~^crb_~', '', $label );
393
394 // split the name into words and make them capitalized
395 $label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE );
396
397 return $label;
398 }
399
400 /**
401 * Normalize a type string representing an object type
402 *
403 * @param string $type
404 * @return string
405 */
406 public static function normalize_type( $type ) {
407 $normalized_type = str_replace( ' ', '_', $type );
408 $normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type );
409 $normalized_type = preg_replace( '/^_|_$/', '', $normalized_type );
410 $normalized_type = strtolower( $normalized_type );
411 return $normalized_type;
412 }
413
414 /**
415 * Convert a string representing an object type to a fully qualified class name
416 *
417 * @param string $type
418 * @param string $namespace
419 * @param string $class_suffix
420 * @return string
421 */
422 public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) {
423 $classlike_type = static::normalize_type( $type );
424 $classlike_type = str_replace( '_', ' ', $classlike_type );
425 $classlike_type = ucwords( $classlike_type );
426 $classlike_type = str_replace( ' ', '_', $classlike_type );
427
428 $class = $classlike_type . $class_suffix;
429 if ( $namespace ) {
430 $class = $namespace . '\\' . $class;
431 }
432
433 return $class;
434 }
435
436 /**
437 * Convert a string representing an object type to a fully qualified class name
438 *
439 * @param string $class
440 * @param string $class_suffix
441 * @return string
442 */
443 public static function class_to_type( $class, $class_suffix = '' ) {
444 $reflection = new \ReflectionClass( $class );
445 $type = $reflection->getShortName();
446
447 if ( $class_suffix ) {
448 $type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type );
449 }
450
451 $type = static::normalize_type( $type );
452
453 return $type;
454 }
455
456 /**
457 * Get an array of sanitized html classes
458 *
459 * @param string|array<string> $classes
460 * @return array<string>
461 */
462 public static function sanitize_classes( $classes ) {
463 if ( ! is_array( $classes ) ) {
464 $classes = array_values( array_filter( explode( ' ', $classes ) ) );
465 }
466 $classes = array_map( 'sanitize_html_class', $classes );
467 return $classes;
468 }
469
470 /**
471 * Check if an id or name for containers and fields is valid
472 *
473 * @param string $id
474 * @return boolean
475 */
476 public static function is_valid_entity_id( $id ) {
477 return ! empty( $id ) && preg_match( '/\A[a-z0-9_\-]+\z/', $id );
478 }
479
480 /**
481 * Return a partial regex pettern matching allowed field name characters
482 *
483 * @return string
484 */
485 public static function get_field_name_characters_pattern() {
486 return 'a-z0-9_\-';
487 }
488
489 /**
490 * Get an attachment ID given a file URL
491 * Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/
492 *
493 * @static
494 * @access public
495 *
496 * @param string $url
497 * @return integer
498 */
499 public static function get_attachment_id( $url ) {
500 $attachment_id = 0;
501 $dir = wp_upload_dir();
502
503 /**
504 * Filters the attachment URL from which the attachment ID is being determined.
505 *
506 * @since 3.0.0
507 *
508 * @param string $url
509 */
510 $url = apply_filters( 'carbon_fields_attachment_id_base_url', $url );
511
512 $filename = wp_basename( $url );
513
514 if ( strpos( $url, $dir['baseurl'] . '/' ) !== false ) {
515 $query_args = array(
516 'post_type' => 'attachment',
517 'post_status' => 'inherit',
518 'fields' => 'ids',
519 'meta_query' => array(
520 array(
521 'value' => $filename,
522 'compare' => 'LIKE',
523 'key' => '_wp_attachment_metadata',
524 ),
525 )
526 );
527
528 $query = new WP_Query( $query_args );
529
530 if ( $query->have_posts() ) {
531 foreach ( $query->posts as $post_id ) {
532 $meta = wp_get_attachment_metadata( $post_id );
533 $original_file = wp_basename( $meta['file'] );
534 $sizes = isset( $meta['sizes'] ) && ! empty( $meta['sizes'] ) ? $meta['sizes'] : array();
535 $cropped_image_files = wp_list_pluck( $sizes, 'file' );
536
537 if ( $original_file === $filename || in_array( $filename, $cropped_image_files ) ) {
538 $attachment_id = intval( $post_id );
539
540 break;
541 }
542 }
543 }
544 }
545
546 /**
547 * Filters the attachment id found from the passed attachment URL.
548 *
549 * @since 3.0.0
550 *
551 * @param integer $attachment_id
552 * @param string $url
553 */
554 return apply_filters( 'carbon_fields_attachment_id_from_url', $attachment_id, $url );
555 }
556
557 /**
558 * Returns attachment metadata from an ID.
559 *
560 * @static
561 * @access public
562 *
563 * @param string $id
564 * @param string $type Value Type. Can be either id or url.
565 * @return array
566 */
567 public static function get_attachment_metadata( $id, $type ) {
568 $attachment_metadata = array(
569 'id' => 0,
570 'thumb_url' => '',
571 'file_type' => '',
572 'file_name' => '',
573 );
574
575 // when `$type` is set to 'url' the `$id` will hold the url, not the id
576 if ( $type === 'url' ) {
577 $attachment_id = static::get_attachment_id( $id );
578
579 if ( $attachment_id === 0 ) {
580 $attachment_metadata['thumb_url'] = $id;
581 }
582
583 $id = $attachment_id;
584 }
585
586 $attachment = get_post( $id );
587
588 if ( ! $attachment ) {
589 /**
590 * Filter the metadata for the attachment in case the attachment post is not found.
591 *
592 * @since 3.0.0
593 *
594 * @param array $attachment_metadata The attachment metadata.
595 * @param integer|string $id The attachment ID. Either attachment post ID or attachment url.
596 * @param string $type The type of `$id` passed. Either 'id' or 'url'.
597 */
598 return apply_filters( 'carbon_fields_attachment_not_found_metadata', $attachment_metadata, $id, $type );
599 }
600
601 $attachment_metadata['id'] = intval( $id );
602 $attachment_metadata['file_url'] = is_numeric( $id ) ? wp_get_attachment_url( $id ) : $id;
603 $attachment_metadata['file_name'] = wp_basename( $attachment_metadata['file_url'] );
604 $attachment_metadata['filetype'] = wp_check_filetype( $attachment_metadata['file_url'] );
605 $attachment_metadata['file_type'] = preg_replace( '~\/.+$~', '', $attachment_metadata['filetype']['type'] ); // image, video, etc..
606
607 if ( $attachment_metadata['file_type'] == 'image' ) {
608 $attachment_metadata['thumb_url'] = $attachment_metadata['file_url'];
609
610 if ( $type == 'id' ) {
611 $attachment_metadata['thumb_url'] = wp_get_attachment_thumb_url( $id );
612 }
613 } else {
614 $attachment_metadata['thumb_url'] = wp_mime_type_icon( $id );
615 }
616
617 /**
618 * Filter the metadata for the attachment.
619 *
620 * @since 3.0.0
621 *
622 * @param array $attachment_metadata The attachment metadata.
623 * @param integer|string $id The attachment ID. Either attachment post ID or attachment url.
624 * @param string $type The type of `$id` passed. Either 'id' or 'url'.
625 */
626 return apply_filters( 'carbon_fields_attachment_metadata', $attachment_metadata, $id, $type );
627 }
628
629 /**
630 * Get the current $_POST or $_GET input array with compacted input values merged in
631 *
632 * @return array
633 */
634 public static function input() {
635 $input = ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' ) ? $_POST : $_GET;
636 $input = stripslashes_deep( $input );
637
638 if ( \Carbon_Fields\COMPACT_INPUT ) {
639 $input = static::expand_compacted_input( $input );
640 }
641
642 return $input;
643 }
644
645 /**
646 * Get a copy of the passed array with compacted input values merged in
647 *
648 * @param array $input
649 * @return array
650 */
651 public static function expand_compacted_input( $input ) {
652 if ( isset( $input[ \Carbon_Fields\COMPACT_INPUT_KEY ] ) ) {
653 $inputs = $input[ \Carbon_Fields\COMPACT_INPUT_KEY ];
654 $input = array_merge( $input, $inputs );
655 }
656 return $input;
657 }
658
659 /**
660 * Get valid input from an input array compared to predefined options
661 *
662 * @param array $input
663 * @param array $options
664 * @return array
665 */
666 public static function get_valid_options( $input, $options ) {
667 // enforce comparison to be string so we do not get unexpected matches
668 // for cases such as "string without any numbers" == 0
669 // in array_search()
670 $search_options = array_map( 'strval', $options );
671
672 $valid_input = array();
673 foreach ( $input as $raw_value ) {
674 $index = array_search( strval( $raw_value ), $search_options, true );
675
676 if ( $index === false ) {
677 continue;
678 }
679
680 $valid_input[] = $options[ $index ];
681 }
682 return $valid_input;
683 }
684
685 /**
686 * Get an array of active sidebars
687 *
688 * @return array
689 */
690 public static function get_active_sidebars() {
691 global $wp_registered_sidebars;
692
693 $sidebars = array();
694
695 foreach ( $wp_registered_sidebars as $sidebar ) {
696 // Check if we have inactive sidebars
697 if ( isset( $sidebar['class'] ) && strpos( $sidebar['class'], 'inactive-sidebar' ) !== false ) {
698 continue;
699 }
700
701 $sidebars[] = array(
702 'id' => $sidebar['id'],
703 'name' => $sidebar['name'],
704 );
705 }
706
707 return $sidebars;
708 }
709
710 public static function get_attachments_urls( $media_files ) {
711 if ( empty( $media_files ) ) {
712 return is_array( $media_files ) ? [] : "";
713 }
714
715 if ( ! is_array( $media_files ) && (int) $media_files > 0 ) {
716 return wp_get_attachment_url( $media_files );
717 }
718
719 return array_map( function( $media_file ) {
720 return wp_get_attachment_url( $media_file );
721 }, $media_files );
722 }
723 }
724