| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JP\CC; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
|
| 11 |
class Helpers { |
| 12 |
|
| 13 |
public static function object_to_array( $object ) { |
| 14 |
$array = array(); |
| 15 |
foreach ( (array) $object as $key => $value ) { |
| 16 |
$array[ $key ] = is_object( $value ) || is_array( $value ) ? self::object_to_array( $value ) : $value; |
| 17 |
} |
| 18 |
|
| 19 |
return $array; |
| 20 |
} |
| 21 |
|
| 22 |
public static function post_type_selectlist( $post_type, $args = array(), $include_total = false ) { |
| 23 |
|
| 24 |
$args = wp_parse_args( $args, array( |
| 25 |
'posts_per_page' => 10, |
| 26 |
'post_type' => $post_type, |
| 27 |
'post__in' => null, |
| 28 |
'post__not_in' => null, |
| 29 |
'post_status' => null, |
| 30 |
'page' => 1, |
| 31 |
// Performance Optimization. |
| 32 |
'no_found_rows' => ! $include_total ? true : false, |
| 33 |
'update_post_term_cache' => false, |
| 34 |
'update_post_meta_cache' => false, |
| 35 |
) ); |
| 36 |
|
| 37 |
if ( $post_type == 'attachment' ) { |
| 38 |
$args['post_status'] = 'inherit'; |
| 39 |
} |
| 40 |
|
| 41 |
// Query Caching. |
| 42 |
static $queries = array(); |
| 43 |
|
| 44 |
$key = md5( serialize( $args ) ); |
| 45 |
|
| 46 |
if ( ! isset( $queries[ $key ] ) ) { |
| 47 |
$query = new \WP_Query( $args ); |
| 48 |
|
| 49 |
$posts = array(); |
| 50 |
foreach ( $query->posts as $post ) { |
| 51 |
$posts[ $post->post_title ] = $post->ID; |
| 52 |
} |
| 53 |
|
| 54 |
$results = array( |
| 55 |
'items' => $posts, |
| 56 |
'total_count' => $query->found_posts, |
| 57 |
); |
| 58 |
|
| 59 |
$queries[ $key ] = $results; |
| 60 |
} else { |
| 61 |
$results = $queries[ $key ]; |
| 62 |
} |
| 63 |
|
| 64 |
return ! $include_total ? $results['items'] : $results; |
| 65 |
} |
| 66 |
|
| 67 |
public static function taxonomy_selectlist( $taxonomies = array(), $args = array(), $include_total = false ) { |
| 68 |
if ( empty ( $taxonomies ) ) { |
| 69 |
$taxonomies = array( 'category' ); |
| 70 |
} |
| 71 |
|
| 72 |
$args = wp_parse_args( $args, array( |
| 73 |
'hide_empty' => false, |
| 74 |
'number' => 10, |
| 75 |
'search' => '', |
| 76 |
'include' => null, |
| 77 |
'offset' => 0, |
| 78 |
'page' => null, |
| 79 |
) ); |
| 80 |
|
| 81 |
if ( $args['page'] ) { |
| 82 |
$args['offset'] = ( $args['page'] - 1 ) * $args['number']; |
| 83 |
} |
| 84 |
|
| 85 |
// Query Caching. |
| 86 |
static $queries = array(); |
| 87 |
|
| 88 |
$key = md5( serialize( $args ) ); |
| 89 |
|
| 90 |
if ( ! isset( $queries[ $key ] ) ) { |
| 91 |
$terms = array(); |
| 92 |
|
| 93 |
foreach ( get_terms( $taxonomies, $args ) as $term ) { |
| 94 |
$terms[ $term->name ] = $term->term_id; |
| 95 |
} |
| 96 |
|
| 97 |
$total_args = $args; |
| 98 |
unset( $total_args['number'] ); |
| 99 |
unset( $total_args['offset'] ); |
| 100 |
|
| 101 |
$results = array( |
| 102 |
'items' => $terms, |
| 103 |
'total_count' => $include_total ? wp_count_terms( $taxonomies, $total_args ) : null, |
| 104 |
); |
| 105 |
|
| 106 |
$queries[ $key ] = $results; |
| 107 |
} else { |
| 108 |
$results = $queries[ $key ]; |
| 109 |
} |
| 110 |
|
| 111 |
return ! $include_total ? $results['items'] : $results; |
| 112 |
} |
| 113 |
|
| 114 |
public static function is_customize_preview() { |
| 115 |
global $wp_customize; |
| 116 |
|
| 117 |
return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview(); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|