Passed
Push — master ( ab9b1d...ea9273 )
by Nirjhar
04:28
created

CGSS_SETTINGS::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 19
c 3
b 0
f 1
nc 1
nop 0
dl 0
loc 25
rs 9.6333
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit;
3
4
/**
5
 * Backend settings page class, can have settings fields or data table
6
 */
7
if ( ! class_exists( 'CGSS_SETTINGS' ) ) {
8
9
	final class CGSS_SETTINGS {
10
11
		public $capability;
12
		public $menuPage;
13
		public $subMenuPage;
14
		public $subMenuPageCpt;
15
		public $help;
16
		public $screen;
17
18
		// Add basic actions for menu and settings
19
		public function __construct() {
20
21
			$this->capability = 'manage_options';
22
			$this->menuPage = array(
23
								'name' => __( 'SEO Scan', 'cgss' ),
0 ignored issues
show
Bug introduced by
The function __ 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

23
								'name' => /** @scrutinizer ignore-call */ __( 'SEO Scan', 'cgss' ),
Loading history...
24
								'heading' => __( 'SEO Scan', 'cgss' ),
25
								'slug' => 'seo-scan'
26
							);
27
			$this->subMenuPage = array(
28
									'name' => __( 'Seo Scan Overview', 'cgss' ),
29
									'heading' => __( 'Overview', 'cgss' ),
30
									'slug' => 'seo-scan',
31
									'parent_slug' => 'seo-scan',
32
									'help' => false,
33
									'screen' => true
34
								);
35
			$this->subMenuPageCpt = $this->get_post_type_menus();
36
37
			$this->screen = ''; // true/false
38
39
			add_action( 'admin_menu', array( $this, 'menu_page' ) );
0 ignored issues
show
Bug introduced by
The function add_action 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

39
			/** @scrutinizer ignore-call */ 
40
   add_action( 'admin_menu', array( $this, 'menu_page' ) );
Loading history...
40
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
41
			add_action( 'admin_menu', array( $this, 'cpt_sub_menu_page' ) );
42
			add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
0 ignored issues
show
Bug introduced by
The function add_filter 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

42
			/** @scrutinizer ignore-call */ 
43
   add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
Loading history...
43
			add_filter('screen_options_show_screen', array( $this, 'remove_screen_options'));
44
		}
45
46
47
		public function remove_screen_options() {
48
49
			if (isset($_GET['page']) && $_GET['page'] == 'seo-scan') {
50
				return false;
51
			} else {
52
				return true;
53
			}
54
		}
55
56
		// Get post type data to form menu variables
57
		public function get_post_type_menus() {
58
59
			$menu_vars = array();
60
61
			$post_types = get_post_types( array( 'public' => true, ), 'names' );
0 ignored issues
show
Bug introduced by
The function get_post_types 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

61
			$post_types = /** @scrutinizer ignore-call */ get_post_types( array( 'public' => true, ), 'names' );
Loading history...
62
			unset( $post_types['attachment'] );
63
64
			foreach ( $post_types as $type ) {
65
66
				$count_posts = wp_count_posts( $type );
0 ignored issues
show
Bug introduced by
The function wp_count_posts 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

66
				$count_posts = /** @scrutinizer ignore-call */ wp_count_posts( $type );
Loading history...
67
				if ( $count_posts->publish > 0 ) {
68
69
					$type_name = ucwords( get_post_type_object( $type )->labels->singular_name );
0 ignored issues
show
Bug introduced by
The function get_post_type_object 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

69
					$type_name = ucwords( /** @scrutinizer ignore-call */ get_post_type_object( $type )->labels->singular_name );
Loading history...
70
					$menu_vars[] = array(
71
									'name' => $type_name . ' ' . __( 'Seo Scan', 'cgss' ),
0 ignored issues
show
Bug introduced by
The function __ 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

71
									'name' => $type_name . ' ' . /** @scrutinizer ignore-call */ __( 'Seo Scan', 'cgss' ),
Loading history...
72
									'heading' => $type_name,
73
									'slug' => 'seo-scan-' . $type,
74
									'parent_slug' => 'seo-scan',
75
									'help' => false,
76
									'screen' => true
77
							);
78
				}
79
			}
80
81
			return $menu_vars;
82
		}
83
84
85
		// Add main menu page callback
86
		public function menu_page() {
87
88
			if ($this->menuPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->menuPage of type array<string,mixed|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89
				add_menu_page(
0 ignored issues
show
Bug introduced by
The function add_menu_page 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

89
				/** @scrutinizer ignore-call */ 
90
    add_menu_page(
Loading history...
90
					$this->menuPage['name'],
91
					$this->menuPage['heading'],
92
					$this->capability,
93
					$this->menuPage['slug'],
94
					array( $this, 'overview_content_cb' ),
95
					'dashicons-search'
96
				);
97
			}
98
		}
99
100
101
102
		//Add a Submenu page callback
103
		public function sub_menu_page() {
104
105
			if ($this->subMenuPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->subMenuPage of type array<string,boolean|mixed|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
106
				$hook = add_submenu_page(
0 ignored issues
show
Bug introduced by
The function add_submenu_page 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

106
				$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
107
							$this->subMenuPage['parent_slug'],
108
							$this->subMenuPage['name'],
109
							$this->subMenuPage['heading'],
110
							$this->capability,
111
							$this->subMenuPage['slug'],
112
							array( $this, 'overview_content_cb' )
113
				);
114
				if ($this->subMenuPage['screen']) {
115
					add_action( 'load-' . $hook, array( $this, 'overview_screen_option' ) );
0 ignored issues
show
Bug introduced by
The function add_action 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

115
					/** @scrutinizer ignore-call */ 
116
     add_action( 'load-' . $hook, array( $this, 'overview_screen_option' ) );
Loading history...
116
				}
117
			}
118
		}
119
120
121
122
		// Add Submenu page callback for cpts
123
		public function cpt_sub_menu_page() {
124
125
			if ($this->subMenuPageCpt) {
126
				foreach ($this->subMenuPageCpt as $value) {
127
					$hook = add_submenu_page(
0 ignored issues
show
Bug introduced by
The function add_submenu_page 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

127
					$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
128
							$value['parent_slug'],
129
							$value['name'],
130
							$value['heading'],
131
							$this->capability,
132
							$value['slug'],
133
							array( $this, 'cpt_content_cb' )
134
						);
135
					if ($value['screen']) {
136
						add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
0 ignored issues
show
Bug introduced by
The function add_action 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

136
						/** @scrutinizer ignore-call */ 
137
      add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
Loading history...
137
					}
138
				}
139
			}
140
		}
141
142
143
144
		//Set screen option
145
		public function set_screen($status, $option, $value) {
146
147
    		if ( 'post_per_page' == $option ) return $value; // Related to PLUGIN_TABLE()
148
    			//return $status;
149
		}
150
151
152
153
		//Set screen option for Items table
154
		public function overview_screen_option() {
155
156
			$this->overview = new CGSS_OVERVIEW_TABLE();
0 ignored issues
show
Bug Best Practice introduced by
The property overview does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
157
		}
158
159
160
161
		//Set screen option for Items table
162
		public function screen_option() {
163
164
			$option = 'per_page';
165
			$args   = array(
166
						'label'   => __( 'Show per page', 'cgss' ),
0 ignored issues
show
Bug introduced by
The function __ 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

166
						'label'   => /** @scrutinizer ignore-call */ __( 'Show per page', 'cgss' ),
Loading history...
167
						'default' => 10,
168
						'option'  => 'post_per_page' // Related to CGSS_TABLE()
169
						);
170
			add_screen_option( $option, $args );
0 ignored issues
show
Bug introduced by
The function add_screen_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

170
			/** @scrutinizer ignore-call */ 
171
   add_screen_option( $option, $args );
Loading history...
171
			$this->table = new CGSS_TABLE();
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
172
		}
173
174
175
176
		// Menu page callback
177
		public function overview_content_cb() { ?>
178
179
			<div class="wrap">
180
				<h1><?php echo get_admin_page_title(); ?>
0 ignored issues
show
Bug introduced by
The function get_admin_page_title 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

180
				<h1><?php echo /** @scrutinizer ignore-call */ get_admin_page_title(); ?>
Loading history...
181
					&nbsp;
182
					<a href="?page=seo-scan&fetch=true" class="button button-secondary">
183
						<?php _e( 'Fetch Insight', 'cgss' ); ?>
0 ignored issues
show
Bug introduced by
The function _e 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

183
						<?php /** @scrutinizer ignore-call */ _e( 'Fetch Insight', 'cgss' ); ?>
Loading history...
184
					</a>
185
				</h1>
186
				<br class="clear">
187
				<?php if (isset($_GET['fetch'])) : ?>
188
					<?php do_action( 'cgss_fetch_insight' ); ?>
0 ignored issues
show
Bug introduced by
The function do_action 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

188
					<?php /** @scrutinizer ignore-call */ do_action( 'cgss_fetch_insight' ); ?>
Loading history...
189
				<?php endif; ?>
190
				<?php
191
					// Source: /lib/overview-table.php
192
					$this->overview = new CGSS_OVERVIEW_TABLE();
0 ignored issues
show
Bug Best Practice introduced by
The property overview does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
193
					$this->overview->prepare_items();
194
					$this->overview->display();
195
				?>
196
				<br class="clear">
197
			</div>
198
		<?php
199
		}
200
201
202
203
		// Add submenu page callback
204
		public function cpt_content_cb() { ?>
205
206
			<div class="wrap">
207
				<h1><?php echo get_admin_page_title(); ?></h1>
0 ignored issues
show
Bug introduced by
The function get_admin_page_title 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

207
				<h1><?php echo /** @scrutinizer ignore-call */ get_admin_page_title(); ?></h1>
Loading history...
208
				<br class="clear">
209
					<?php if (isset($_GET['scan'])) : ?>
210
						<?php do_action( 'cgss_scan' ); ?>
0 ignored issues
show
Bug introduced by
The function do_action 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

210
						<?php /** @scrutinizer ignore-call */ do_action( 'cgss_scan' ); ?>
Loading history...
211
					<?php elseif (isset($_GET['compete'])) : ?>
212
						<?php do_action( 'cgss_compete' ); ?>
213
					<?php else : ?>
214
						<form method="post" action="">
215
						<?php
216
							// Source: /lib/table.php
217
							$this->table = new CGSS_TABLE();
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
218
							$this->table->prepare_items();
219
							$this->table->display();
220
						?>
221
						</form>
222
					<?php endif; ?>
223
				<br class="clear">
224
			</div>
225
		<?php
226
		}
227
	}
228
} ?>
229