Passed
Push — master ( 836585...2607e6 )
by Nirjhar
03:36
created

PLUGIN_QUERY::__construct()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 6
eloc 14
c 2
b 1
f 1
nc 16
nop 0
dl 0
loc 29
rs 9.2222
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit;
3
4
/**
5
 * WP Query class for querying WP database
6
 * For more reference of arguments visit: https://gist.github.com/nirjharlo/5c6f8ac4cc5271f88376788e599c287b
7
 * This class depends on WP pagenavi plugin: https://wordpress.org/plugins/wp-pagenavi/
8
 */
9
if ( ! class_exists( 'PLUGIN_QUERY' ) ) {
10
11
	class PLUGIN_QUERY {
12
13
		public $display_count = NULL;
14
15
		public function __construct() {
16
17
			global $post, $ga_customers, $user;
18
19
			$this->display_count = get_option('posts_per_page', 10);
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
			$this->display_count = /** @scrutinizer ignore-call */ get_option('posts_per_page', 10);
Loading history...
20
21
			$paged = get_query_var('paged') ?
0 ignored issues
show
Bug introduced by
The function get_query_var was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
			$paged = /** @scrutinizer ignore-call */ get_query_var('paged') ?
Loading history...
22
			get_query_var('paged')
23
			 : (get_query_var('page') ?
24
			  	get_query_var('page') : 1);
25
26
			$args = $this->user_args($paged); // OR $this->post_args($paged)
27
28
			$the_query = new WP_Query( $args );
0 ignored issues
show
Bug introduced by
The type WP_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
30
			// The Loop
31
			if ( $the_query->have_posts() ) {
32
				while ( $the_query->have_posts() ) {
33
      		$the_query->the_post();
34
	  			// Do Stuff
35
				} // end while
36
			} // endif
37
38
			if (function_exists('wp_pagenavi'))  {
39
				wp_pagenavi( array( 'query' => $the_query, 'echo' => true ) );//For user query add param 'type' => 'users'
40
			}
41
42
			// Reset Post Data
43
			wp_reset_postdata();
0 ignored issues
show
Bug introduced by
The function wp_reset_postdata was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
			/** @scrutinizer ignore-call */ 
44
   wp_reset_postdata();
Loading history...
44
		}
45
46
47
		/**
48
		 *
49
		 */
50
		public function user_args($paged) {
51
52
			$offset = ( $paged - 1 ) * $this->display_count;
53
54
			$args = array (
55
				'role' => '', // user role
56
				'order' => '', //ASC or DESC
57
				'fields' => '', //
58
				'orderby' => '',
59
				'role__in' => '',
60
				'role__not_in'=> '',
61
				'include' => '',
62
				'exclude' => '',
63
				'blog_id' => '',
64
				'taxonomy' => '',
65
				'terms' => '',
66
				'number' => $this->display_count,
67
				'count_total' => true,
68
				'paged' => $paged,
69
				'offset' =>  $offset,
70
				'search' => '*'.esc_attr( $_GET['search'] ).'*',
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
				'search' => '*'./** @scrutinizer ignore-call */ esc_attr( $_GET['search'] ).'*',
Loading history...
71
				'meta_query' => array( // It supports nested meta query
72
					'relation' => 'AND', //or 'OR'
73
					array(
74
						'key'     => 'meta_key',
75
						'value'   => 'some_value',
76
						'compare' => 'LIKE' //like others
77
					),
78
					array(
79
						'key'     => 'meta_key',
80
						'value'   => 'some_value',
81
						'compare' => 'LIKE' //like others
82
					)
83
				)
84
			);
85
86
			return $args;
87
		}
88
89
90
		/**
91
		 *
92
		 */
93
		public function post_args($paged) {
94
95
			$offset = ( $paged - 1 ) * $this->display_count;
96
97
			$args = array (
98
				'post_type' => '', // array of type slugs
99
				'order' => 'ASC',
100
				'fields' => '', //
101
				'orderby' => '',
102
				'include' => '',
103
				'exclude' => '',
104
				'blog_id' => '',
105
				'taxonomy' => '',
106
				'terms' => '',
107
				'number' => $this->display_count,
108
				'count_total' => true,
109
				'paged' => $paged,
110
				'offset' =>  $offset,
111
				'search' => '*'.esc_attr( $_GET['search'] ).'*',
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
				'search' => '*'./** @scrutinizer ignore-call */ esc_attr( $_GET['search'] ).'*',
Loading history...
112
				'meta_query' => array( // It supports nested meta query
113
					'relation' => 'AND', //or 'OR'
114
					array(
115
						'key'     => 'meta_key',
116
						'value'   => 'some_value',
117
						'compare' => 'LIKE' //like others
118
					),
119
					array(
120
						'key'     => 'meta_key',
121
						'value'   => 'some_value',
122
						'compare' => 'LIKE' //like others
123
					)
124
				)
125
			);
126
127
			return $args;
128
		}
129
	}
130
} ?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
131