Passed
Push — master ( 27be6d...bb494a )
by Nirjhar
02:28
created

CGSS_SETTINGS::cpt_sub_menu_page()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 11
c 2
b 0
f 1
nc 4
nop 0
dl 0
loc 14
rs 9.2
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
19
20
		// Add basic actions for menu and settings
21
		public function __construct() {
22
23
			$this->capability = 'manage_options';
24
			$this->menuPage = array(
25
								'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

25
								'name' => /** @scrutinizer ignore-call */ __( 'SEO Scan', 'cgss' ),
Loading history...
26
								'heading' => __( 'SEO Scan', 'cgss' ),
27
								'slug' => 'seo-scan'
28
							);
29
			$this->subMenuPage = array(
30
									'name' => __( 'Seo Scan Overview', 'cgss' ),
31
									'heading' => __( 'Overview', 'cgss' ),
32
									'slug' => 'seo-scan',
33
									'parent_slug' => 'seo-scan',
34
									'help' => false,
35
									'screen' => true
36
								);
37
			$this->subMenuPageCpt = $this->get_post_type_menus();
38
39
			$this->screen = ''; // true/false
40
41
			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

41
			/** @scrutinizer ignore-call */ 
42
   add_action( 'admin_menu', array( $this, 'menu_page' ) );
Loading history...
42
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
43
			add_action( 'admin_menu', array( $this, 'cpt_sub_menu_page' ) );
44
			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

44
			/** @scrutinizer ignore-call */ 
45
   add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
Loading history...
45
		}
46
47
48
49
		// Get post type data to form menu variables
50
		public function get_post_type_menus() {
51
52
			$menu_vars = array();
53
54
			$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

54
			$post_types = /** @scrutinizer ignore-call */ get_post_types( array( 'public' => true, ), 'names' );
Loading history...
55
			unset( $post_types['attachment'] );
56
57
			foreach ( $post_types as $type ) {
58
59
				$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

59
				$count_posts = /** @scrutinizer ignore-call */ wp_count_posts( $type );
Loading history...
60
				if ( $count_posts->publish > 0 ) {
61
62
					$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

62
					$type_name = ucwords( /** @scrutinizer ignore-call */ get_post_type_object( $type )->labels->singular_name );
Loading history...
63
					$menu_vars[] = array(
64
									'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

64
									'name' => $type_name . ' ' . /** @scrutinizer ignore-call */ __( 'Seo Scan', 'cgss' ),
Loading history...
65
									'heading' => $type_name,
66
									'slug' => 'seo-scan-' . $type,
67
									'parent_slug' => 'seo-scan',
68
									'help' => false,
69
									'screen' => true
70
							);
71
				}
72
			}
73
74
			return $menu_vars;
75
		}
76
77
78
		// Add main menu page callback
79
		public function menu_page() {
80
81
			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...
82
				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

82
				/** @scrutinizer ignore-call */ 
83
    add_menu_page(
Loading history...
83
					$this->menuPage['name'],
84
					$this->menuPage['heading'],
85
					$this->capability,
86
					$this->menuPage['slug'],
87
					array( $this, 'overview_content_cb' ),
88
					'dashicons-search'
89
				);
90
			}
91
		}
92
93
94
95
		//Add a Submenu page callback
96
		public function sub_menu_page() {
97
98
			if ($this->subMenuPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->subMenuPage of type array<string,mixed|string|boolean> 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...
99
				$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

99
				$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
100
							$this->subMenuPage['parent_slug'],
101
							$this->subMenuPage['name'],
102
							$this->subMenuPage['heading'],
103
							$this->capability,
104
							$this->subMenuPage['slug'],
105
							array( $this, 'overview_content_cb' )
106
						);
107
					if ($this->subMenuPage['screen']) {
108
						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

108
						/** @scrutinizer ignore-call */ 
109
      add_action( 'load-' . $hook, array( $this, 'overview_screen_option' ) );
Loading history...
109
					}
110
				}
111
			}
112
113
114
115
		// Add Submenu page callback for cpts
116
		public function cpt_sub_menu_page() {
117
118
			if ($this->subMenuPageCpt) {
119
				foreach ($this->subMenuPageCpt as $value) {
120
					$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

120
					$hook = /** @scrutinizer ignore-call */ add_submenu_page(
Loading history...
121
							$value['parent_slug'],
122
							$value['name'],
123
							$value['heading'],
124
							$this->capability,
125
							$value['slug'],
126
							array( $this, 'cpt_content_cb' )
127
						);
128
					if ($value['screen']) {
129
						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

129
						/** @scrutinizer ignore-call */ 
130
      add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
Loading history...
130
					}
131
				}
132
			}
133
		}
134
135
136
137
		//Set screen option
138
		public function set_screen($status, $option, $value) {
139
 
140
    		if ( 'post_per_page' == $option ) return $value; // Related to PLUGIN_TABLE()
141
    			//return $status; 
142
		}
143
144
145
146
		//Set screen option for Items table
147
		public function overview_screen_option() {
148
149
			$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...
150
		}
151
152
153
154
		//Set screen option for Items table
155
		public function screen_option() {
156
157
			$option = 'per_page';
158
			$args   = array(
159
						'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

159
						'label'   => /** @scrutinizer ignore-call */ __( 'Show per page', 'cgss' ),
Loading history...
160
						'default' => 10,
161
						'option'  => 'post_per_page' // Related to CGSS_TABLE()
162
						);
163
			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

163
			/** @scrutinizer ignore-call */ 
164
   add_screen_option( $option, $args );
Loading history...
164
			$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...
165
		}
166
167
168
169
		// Menu page callback
170
		public function overview_content_cb() { ?>
171
172
			<div class="wrap">
173
				<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

173
				<h1><?php echo /** @scrutinizer ignore-call */ get_admin_page_title(); ?>
Loading history...
174
					&nbsp;<a href="?page=seo-scan&fetch=true" class="button button-secondary"><?php _e( 'Fetch Insight', 'cgss' ); ?></a>
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

174
					&nbsp;<a href="?page=seo-scan&fetch=true" class="button button-secondary"><?php /** @scrutinizer ignore-call */ _e( 'Fetch Insight', 'cgss' ); ?></a>
Loading history...
175
				</h1>
176
				<br class="clear">
177
				<?php if (isset($_GET['fetch'])) : ?>
178
					<?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

178
					<?php /** @scrutinizer ignore-call */ do_action( 'cgss_fetch_insight' ); ?>
Loading history...
179
				<?php endif; ?>
180
				<?php
181
					// Source: /lib/overview-table.php
182
					$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...
183
					$this->overview->prepare_items();
184
					$this->overview->display();
185
				?>
186
				<br class="clear">
187
			</div>
188
		<?php
189
		}
190
191
192
193
		// Add submenu page callback
194
		public function cpt_content_cb() { ?>
195
196
			<div class="wrap">
197
				<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

197
				<h1><?php echo /** @scrutinizer ignore-call */ get_admin_page_title(); ?></h1>
Loading history...
198
				<br class="clear">
199
					<?php if (isset($_GET['scan'])) : ?>
200
						<?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

200
						<?php /** @scrutinizer ignore-call */ do_action( 'cgss_scan' ); ?>
Loading history...
201
					<?php elseif (isset($_GET['compete'])) : ?>
202
						<?php do_action( 'cgss_compete' ); ?>
203
					<?php else : ?>
204
						<form method="post" action="">
205
						<?php
206
							// Source: /lib/table.php
207
							$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...
208
							$this->table->prepare_items();
209
							$this->table->display();
210
						?>
211
						</form>
212
					<?php endif; ?>
213
				<br class="clear">
214
			</div>
215
		<?php
216
		}
217
	}
218
} ?>