Passed
Push — master ( c65969...fb6e64 )
by Nirjhar
08:27 queued 05:37
created

Plugin/Src/settings.php (2 issues)

1
<?php
2
3
namespace NirjharLo\Cgss\Src;
4
5
if ( ! defined( 'ABSPATH' ) ) exit;
6
7
use \NirjharLo\Cgss\Lib\Table;
8
use \NirjharLo\Cgss\Lib\OverviewTable;
9
10
11
/**
12
 * Backend settings page class, can have settings fields or data table
13
 */
14
15
	final class Settings {
16
17
		public $capability;
18
		public $menuPage;
19
		public $subMenuPage;
20
		public $subMenuPageCpt;
21
		public $help;
22
		public $screen;
23
24
		// Add basic actions for menu and settings
25
		public function __construct() {
26
27
			$this->capability = 'manage_options';
28
			$this->menuPage = array(
29
								'name' => __( 'SEO Scan', 'cgss' ),
30
								'heading' => __( 'SEO Scan', 'cgss' ),
31
								'slug' => 'seo-scan'
32
							);
33
			$this->subMenuPage = array(
34
									'name' => __( 'Seo Scan Overview', 'cgss' ),
35
									'heading' => __( 'Overview', 'cgss' ),
36
									'slug' => 'seo-scan',
37
									'parent_slug' => 'seo-scan',
38
									'help' => false,
39
									'screen' => true
40
								);
41
			$this->subMenuPageCpt = $this->get_post_type_menus();
42
43
			$this->screen = ''; // true/false
44
45
			add_action( 'admin_menu', array( $this, 'menu_page' ) );
46
			add_action( 'admin_menu', array( $this, 'sub_menu_page' ) );
47
			add_action( 'admin_menu', array( $this, 'cpt_sub_menu_page' ) );
48
			add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
49
			add_filter('screen_options_show_screen', array( $this, 'remove_screen_options'));
50
		}
51
52
53
		public function remove_screen_options() {
54
55
			if (isset($_GET['page']) && $_GET['page'] == 'seo-scan') {
56
				return false;
57
			} else {
58
				return true;
59
			}
60
		}
61
62
		// Get post type data to form menu variables
63
		public function get_post_type_menus() {
64
65
			$menu_vars = array();
66
67
			$post_types = get_post_types( array( 'public' => true, ), 'names' );
68
			unset( $post_types['attachment'] );
69
70
			foreach ( $post_types as $type ) {
71
72
				$count_posts = wp_count_posts( $type );
73
				if ( $count_posts->publish > 0 ) {
74
75
					$type_name = ucwords( get_post_type_object( $type )->labels->singular_name );
76
					$menu_vars[] = array(
77
									'name' => $type_name . ' ' . __( 'Seo Scan', 'cgss' ),
78
									'heading' => $type_name,
79
									'slug' => 'seo-scan-' . $type,
80
									'parent_slug' => 'seo-scan',
81
									'help' => false,
82
									'screen' => true
83
							);
84
				}
85
			}
86
87
			return $menu_vars;
88
		}
89
90
91
		// Add main menu page callback
92
		public function menu_page() {
93
94
			if ($this->menuPage) {
95
				add_menu_page(
96
					$this->menuPage['name'],
97
					$this->menuPage['heading'],
98
					$this->capability,
99
					$this->menuPage['slug'],
100
					array( $this, 'overview_content_cb' ),
101
					'dashicons-search'
102
				);
103
			}
104
		}
105
106
107
108
		//Add a Submenu page callback
109
		public function sub_menu_page() {
110
111
			if ($this->subMenuPage) {
112
				$hook = add_submenu_page(
113
							$this->subMenuPage['parent_slug'],
114
							$this->subMenuPage['name'],
115
							$this->subMenuPage['heading'],
116
							$this->capability,
117
							$this->subMenuPage['slug'],
118
							array( $this, 'overview_content_cb' )
119
				);
120
				if ($this->subMenuPage['screen']) {
121
					add_action( 'load-' . $hook, array( $this, 'overview_screen_option' ) );
122
				}
123
			}
124
		}
125
126
127
128
		// Add Submenu page callback for cpts
129
		public function cpt_sub_menu_page() {
130
131
			if ($this->subMenuPageCpt) {
132
				foreach ($this->subMenuPageCpt as $value) {
133
					$hook = add_submenu_page(
134
							$value['parent_slug'],
135
							$value['name'],
136
							$value['heading'],
137
							$this->capability,
138
							$value['slug'],
139
							array( $this, 'cpt_content_cb' )
140
						);
141
					if ($value['screen']) {
142
						add_action( 'load-' . $hook, array( $this, 'screen_option' ) );
143
					}
144
				}
145
			}
146
		}
147
148
149
150
		//Set screen option
151
		public function set_screen($status, $option, $value) {
152
153
    		if ( 'post_per_page' == $option ) return $value; // Related to PLUGIN_TABLE()
154
    			//return $status;
155
		}
156
157
158
159
		//Set screen option for Items table
160
		public function overview_screen_option() {
161
162
			$this->overview = new OverviewTable();
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...
163
		}
164
165
166
167
		//Set screen option for Items table
168
		public function screen_option() {
169
170
			$option = 'per_page';
171
			$args   = array(
172
						'label'   => __( 'Show per page', 'cgss' ),
173
						'default' => 10,
174
						'option'  => 'post_per_page' // Related to TABLE()
175
						);
176
			add_screen_option( $option, $args );
177
			$this->table = new Table();
178
		}
179
180
181
182
		// Menu page callback
183
		public function overview_content_cb() { ?>
184
185
			<div class="wrap">
186
				<h1><?php echo get_admin_page_title(); ?>
187
					&nbsp;
188
					<a href="?page=seo-scan&fetch=true" class="button button-secondary">
189
						<?php _e( 'Fetch Insight', 'cgss' ); ?>
190
					</a>
191
				</h1>
192
				<br class="clear">
193
				<?php if (isset($_GET['fetch'])) : ?>
194
					<?php do_action( 'cgss_fetch_insight' ); ?>
195
				<?php endif; ?>
196
				<?php
197
					// Source: /lib/overview-table.php
198
					$this->overview = new OverviewTable();
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...
199
					$this->overview->prepare_items();
200
					$this->overview->display();
201
				?>
202
				<br class="clear">
203
			</div>
204
		<?php
205
		}
206
207
208
209
		// Add submenu page callback
210
		public function cpt_content_cb() { ?>
211
212
			<div class="wrap">
213
				<h1><?php echo get_admin_page_title(); ?></h1>
214
				<br class="clear">
215
					<?php if (isset($_GET['scan'])) : ?>
216
						<?php do_action( 'cgss_scan' ); ?>
217
					<?php elseif (isset($_GET['compete'])) : ?>
218
						<?php do_action( 'cgss_compete' ); ?>
219
					<?php else : ?>
220
						<form method="post" action="">
221
						<?php
222
							// Source: /lib/table.php
223
							$this->table = new Table();
224
							$this->table->prepare_items();
225
							$this->table->display();
226
						?>
227
						</form>
228
					<?php endif; ?>
229
				<br class="clear">
230
			</div>
231
		<?php
232
		}
233
	} ?>
234