C:\xampp\htdocs\landing\wp-content\updraft\plugins-old\penci-framework\inc\pre-query.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
if ( ! class_exists'Penci_Pre_Query' ) ) {
    class 
Penci_Pre_Query {

        private static 
$cache_thumbnail = array();

        public static function 
switch_query_args$atts ) {
            if ( isset( 
$atts['build_query'] ) ) {
                
$build_query $atts['build_query'];
                if ( 
is_array$build_query ) ) {
                    
$build_query['post_status'] = 'publish';
                } else {
                    
$build_query .= '|post_status:publish';
                }

                
$args penci_build_args_query$build_queryget_the_ID() );

                
$limit          = isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : get_option'posts_per_page' );
                
$paged          = isset( $atts['paged'] ) ? $atts['paged'] : 1;
                
$limit_loadmore = isset( $atts['limit_loadmore'] ) ? $atts['limit_loadmore'] : '';
                
$styleAction    = isset( $atts['styleAction'] ) ? $atts['styleAction'] : '';
                
$offset         = isset( $atts['offset'] ) ? $atts['offset'] : 0;
                
$args_offset    0;

                if ( isset( 
$args['offset'] ) && ! empty( $args['offset'] ) ) {
                    
$args_offset intval$args['offset'] );

                    if ( 
$paged ) {
                        
$args['offset'] = intval$args['offset'] ) + ( ( $paged ) * $limit );
                    }
                    
$offset $offset intval$args['offset'] );
                }

                if ( ! empty( 
$limit_loadmore ) && in_array$styleAction, array( 'load_more''infinite' ) ) ) {
                    
$offset $offset $offset 0;

                    if ( 
intval$paged ) > ) {
                        
$offset intval$limit + ( intval$paged ) * $limit_loadmore ) );

                        if ( isset( 
$args['offset'] ) && ! empty( $args['offset'] ) ) {
                            
$offset $offset $args_offset;

                        }
                    }

                    
$args['offset'] = $offset;
                    
$limit                      $limit_loadmore;
                }

                
$args['ignore_sticky_posts'] = isset( $atts['enable_stiky_post'] ) && ! $atts['enable_stiky_post'] ? 0;
                
$args['posts_per_page']         = $limit;
                
$args['paged']                  = ! empty( $paged ) ? $paged 1;

                
// SELECT post_id FROM wp_postmeta WHERE meta_key = ? AND meta_value = ?
                
if ( isset( $atts['shortcode_id'] ) && 'block_video' == $atts['shortcode_id'] ) {
                    
$args['tax_query'][] = array(
                        
'taxonomy' => 'post_format',
                        
'field'    => 'slug',
                        
'terms'    => array( 'post-format-video' ),
                    );
                }

                if ( isset( 
$atts['shortcode_id'] ) && 'block_36' == $atts['shortcode_id'] ) {
                    if ( isset( 
$atts['only_post_review'] ) && $atts['only_post_review'] ) {
                        
$args['meta_query'][] = array(
                            
'key'     => 'penci_total_review',
                            
'compare' => 'EXISTS',
                        );
                    }
                }

            } else {
                
$atts self::optimize_query_atts$atts );
                
$args self::get_query_args$atts );
            }
            
$args['use_cat_add'] = isset( $atts['use_cat_add'] ) ? $atts['use_cat_add'] : '';

            
$args apply_filters'penci_args_support_polylang'$args );

            return 
$args;
        }

        public static function 
do_query$atts ) {
            
$args self::switch_query_args$atts );
            
$result_query self::result_query$args );
            return 
$result_query;
        }

        public static function 
result_query$args ) {
            
$query_key    'query_key_' md5serialize$args ) );
            
$result_query wp_cache_get$query_key'pennews' );

            if ( ! 
$result_query ) {

                
$args apply_filters'penci_args_support_polylang'$args );
                
$result_query = new WP_Query$args );
                
wp_reset_postdata();
                
wp_cache_set$query_key$result_query'pennews' );
            }

            
self::save_thub_cache$result_query );

            return 
$result_query;
        }


        public static function 
optimize_query_atts$atts ) {
            
$used_query = array(
                
'post_ids',
                
'category_ids',
                
'tag_slugs',
                
'sort',
                
'limit',
                
'autors_id',
                
'post_types',
                
'paged',
                
'shortcode_id',
                
'limit_loadmore',
                
'styleAction',
                
'enable_stiky_post',
                
'exclude_cat_id',
                
'offset',
                
'only_post_review'
            
);

            foreach ( 
$atts as $k => $v ) {
                if ( ! 
in_array$k$used_query ) ) {
                    unset( 
$atts$k ] );
                }
            }

            return 
$atts;
        }

        public static function 
save_thub_cache$query ) {

            if ( ! isset( 
$query->post ) ) {
                return;
            }

            
$posts $query->posts;

            
$thumbnails = array();

            foreach ( 
$posts as $result ) {
                if ( ! 
in_array$result->IDself::$cache_thumbnail ) ) {
                    
$thumbnails[]            = get_post_thumbnail_id$result->ID );
                    
self::$cache_thumbnail[] = $result->ID;
                }
            }

            if ( ! empty( 
$thumbnails ) ) {
                
$query = array(
                    
'post__in'  => $thumbnails,
                    
'post_type' => 'attachment',
                    
'showposts' => is_array$thumbnails ) ? count$thumbnails ) : 0
                
);

                
get_posts$query );
            }
        }

        public static function 
get_query_args$atts ) {

            
$default = array(
                
'post_ids'          => '',
                
'category_ids'      => '',
                
'tag_slugs'         => '',
                
'sort'              => '',
                
'limit'             => '',
                
'autors_id'         => '',
                
'post_types'        => 'post',
                
'paged'             => 1,
                
'shortcode_id'      => '',
                
'limit_loadmore'    => '',
                
'styleAction'       => '',
                
'enable_stiky_post' => 0,
                
'exclude_cat_id'    => '',
                
'offset'            => 0
            
);

            
$atts wp_parse_args$atts$default );

            
extract$atts );

            
$penci_query_args = array(
                
'post_type'              => $atts['post_types'],
                
'ignore_sticky_posts'    => ! $atts['enable_stiky_post'] ? 0,
                
'post_status'            => 'publish',
                
'update_post_meta_cache' => false,
                
'update_post_term_cache' => false,

            );

            if ( 
'block_video' == $atts['shortcode_id'] ) {
                
$penci_query_args['tax_query'][] = array(
                    
'taxonomy' => 'post_format',
                    
'field'    => 'slug',
                    
'terms'    => array( 'post-format-video' ),
                );
            }

            if ( 
'block_36' == $atts['shortcode_id'] ) {
                if( isset( 
$atts['only_post_review'] ) &&  $atts['only_post_review'] ) {
                    
$penci_query_args['meta_query'][] = array(
                        
'key'     => 'penci_total_review',
                        
'compare' => 'EXISTS',
                    );
                }
            }


            if ( ! empty( 
$atts['category_ids'] ) ) {
                
$penci_query_args['category_name'] = $atts['category_ids'];
            }

            if ( ! empty( 
$atts['exclude_cat_id'] ) ) {

                
$exclude_cat_ids explode','$atts['exclude_cat_id'] );
                
$cat_not_in      = array();
                foreach ( 
$exclude_cat_ids as $exclude_cat_id ) {
                    
$exclude_cat_id trim$exclude_cat_id );
                    if ( 
is_numeric$exclude_cat_id ) ) {
                        
$cat_not_in[] = $exclude_cat_id;
                    }
                }

                if ( ! empty( 
$cat_not_in ) ) {
                    
$penci_query_args['category__not_in'] = $cat_not_in;
                }
            }

            if ( ! empty( 
$atts['tag_slug'] ) ) {
                
$penci_query_args['tag'] = str_replace' ''-'$atts['tag_slug'] );
            }

            if ( ! empty( 
$atts['autors_id'] ) ) {
                
$penci_query_args['author'] = $atts['autors_id'];
            }
            
$sort $atts['sort'];

            switch ( 
$sort ) {
                case 
'popular':
                    
$penci_query_args['meta_key'] = '_count-views_all';
                    
$penci_query_args['orderby']  = 'meta_value_num';
                    
$penci_query_args['order']    = 'DESC';
                    break;
                case 
'popular7':
                    
$penci_query_args['meta_key'] = '_count-views_week';
                    
$penci_query_args['orderby']  = 'meta_value_num';
                    
$penci_query_args['order']    = 'DESC';
                    break;
                case 
'popular_month':
                    
$penci_query_args['meta_key'] = '_count-views_month';
                    
$penci_query_args['orderby']  = 'meta_value_num';
                    
$penci_query_args['order']    = 'DESC';
                    break;
                case 
'review':
                    
$penci_query_args['meta_key'] = 'penci_total_review';
                    
$penci_query_args['orderby']  = 'meta_value_num';
                    
$penci_query_args['order']    = 'DESC';
                    break;
                case 
'random_posts':
                    
$penci_query_args['orderby'] = 'rand';
                    break;
                case 
'alphabetical_order':
                    
$penci_query_args['orderby'] = 'title';
                    
$penci_query_args['order']   = 'ASC';
                    break;
                case 
'comment_count':
                    
$penci_query_args['orderby'] = 'comment_count';
                    
$penci_query_args['order']   = 'DESC';
                    break;
                case 
'random_today':
                    
$penci_query_args['orderby']  = 'rand';
                    
$penci_query_args['year']     = date'Y' );
                    
$penci_query_args['monthnum'] = date'n' );
                    
$penci_query_args['day']      = date'j' );
                    break;
                case 
'random_7_day':
                    
$penci_query_args['orderby']    = 'rand';
                    
$penci_query_args['date_query'] = array(
                        
'column' => 'post_date_gmt',
                        
'after'  => '1 week ago'
                    
);
                    break;
            }

            if ( ! empty( 
$atts['post_ids'] ) ) {

                
$post_id_arr explode','$atts['post_ids'] );

                
$post_in $post_not_in = array();

                foreach ( 
$post_id_arr as $p_id ) {
                    
$pre_post_id trim$p_id );
                    if ( ! 
is_numeric$pre_post_id ) ) {
                        continue;
                    }

                    
$pre_post_id intval$pre_post_id );

                    if ( 
$pre_post_id ) {
                        
$post_not_in [] = str_replace'-'''$pre_post_id );
                    } else {
                        
$post_in [] = $pre_post_id;
                    }
                }

                if ( ! empty( 
$post_not_in ) ) {
                    if ( ! empty( 
$penci_query_args['post__not_in'] ) ) {
                        
$penci_query_args['post__not_in'] = array_merge$penci_query_args['post__not_in'], $post_not_in );
                    } else {
                        
$penci_query_args['post__not_in'] = $post_not_in;
                    }
                }

                if ( ! empty( 
$post_in ) ) {
                    
$penci_query_args['post__in'] = $post_in;
                    
$penci_query_args['orderby']  = 'post__in';
                }

            }

            if ( empty( 
$limit ) ) {
                
$limit get_option'posts_per_page' );
            }

            if ( ! empty( 
$limit_loadmore ) && in_array$styleAction, array( 'load_more''infinite' ) ) ) {
                
$offset $offset $offset 0;

                if ( 
intval$paged ) > ) {
                    
$offset intval$limit + ( intval$paged ) * $limit_loadmore ) );
                }

                
$penci_query_args['offset'] = $offset;

                
$limit $limit_loadmore;
            }


            
$penci_query_args['posts_per_page'] = $limit;
            
$penci_query_args['paged']          = ! empty( $paged ) ? $paged 1;

            return 
$penci_query_args;
        }
    }
}

new 
Penci_Pre_Query;
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586