PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 1.1.6
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v1.1.6
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Helpers.php

Helpers.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 1.1.6, at classes/Helpers.php

219 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 post_type_selectlist_query( $post_type = array(), $args = array(), $include_total = false ) {
115 if ( empty ( $post_type ) ) {
116 $post_type = array( 'any' );
117 }
118
119 $args = wp_parse_args( $args, array(
120 'posts_per_page' => 10,
121 'post_type' => $post_type,
122 'post__in' => null,
123 'post__not_in' => null,
124 'post_status' => null,
125 'page' => 1,
126 // Performance Optimization.
127 'no_found_rows' => ! $include_total ? true : false,
128 'update_post_term_cache' => false,
129 'update_post_meta_cache' => false,
130 ) );
131
132 if ( $post_type == 'attachment' ) {
133 $args['post_status'] = 'inherit';
134 }
135
136 // Query Caching.
137 static $queries = array();
138
139 $key = md5( serialize( $args ) );
140
141 if ( ! isset( $queries[ $key ] ) ) {
142 $query = new \WP_Query( $args );
143
144 $posts = array();
145 foreach ( $query->posts as $post ) {
146 $posts[ $post->ID ] = $post->post_title;
147 }
148
149 $results = array(
150 'items' => $posts,
151 'total_count' => $query->found_posts,
152 );
153
154 $queries[ $key ] = $results;
155 } else {
156 $results = $queries[ $key ];
157 }
158
159 return ! $include_total ? $results['items'] : $results;
160 }
161
162 public static function taxonomy_selectlist_query( $taxonomies = array(), $args = array(), $include_total = false ) {
163 if ( empty ( $taxonomies ) ) {
164 $taxonomies = array( 'category' );
165 }
166
167 $args = wp_parse_args( $args, array(
168 'hide_empty' => false,
169 'number' => 10,
170 'search' => '',
171 'include' => null,
172 'exclude' => null,
173 'offset' => 0,
174 'page' => null,
175 'taxonomy' => $taxonomies,
176 ) );
177
178 if ( $args['page'] ) {
179 $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
180 }
181
182 // Query Caching.
183 static $queries = array();
184
185 $key = md5( serialize( $args ) );
186
187 if ( ! isset( $queries[ $key ] ) ) {
188 $terms = array();
189
190 foreach ( get_terms( $args ) as $term ) {
191 $terms[ $term->term_id ] = $term->name;
192 }
193
194 $total_args = $args;
195 $total_args['fields'] = 'count';
196 unset( $total_args['number'] );
197 unset( $total_args['offset'] );
198
199 $results = array(
200 'items' => $terms,
201 'total_count' => $include_total ? get_terms( $total_args ) : null,
202 );
203
204 $queries[ $key ] = $results;
205 } else {
206 $results = $queries[ $key ];
207 }
208
209 return ! $include_total ? $results['items'] : $results;
210 }
211
212 public static function is_customize_preview() {
213 global $wp_customize;
214
215 return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
216 }
217
218 }
219