Completed
Push — 97-fix/system-info-section ( 39a59e...7fb1eb )
by Rajan
01:51
created

SystemInfoPage   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 332
ccs 0
cts 97
cp 0
rs 6.96
c 0
b 0
f 0
wmc 53
lcom 2
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 13 1
A verify_nonce() 0 19 3
A register_page() 0 13 1
A el_get_current_theme_name() 0 11 2
A el_identify_host() 0 10 3
A el_print_current_plugins() 0 13 3
A el_print_network_active_plugins() 0 17 3
A el_plugin_version() 0 5 1
F render_page() 0 145 33
A generate_sysinfo_download() 0 9 1
A request_handler() 0 13 2

How to fix   Complexity   

Complex Class

Complex classes like SystemInfoPage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SystemInfoPage, and based on these observations, apply Extract Interface, too.

1
<?php namespace EmailLog\Core\UI\Page;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * System Info Page.
7
 * This page is displayed about ststem info.
8
 *
9
 * @since 2.0.0
10
 */
11
class SystemInfoPage extends BasePage {
12
	const PAGE_SLUG = 'system_infos';
13
14
	/**
15
	 * Capability to manage system info.
16
	 *
17
	 * @since 2.3.0
18
	 */
19
	const CAPABILITY = 'manage_system_infos';
20
21
	/**
22
	 * Specify additional hooks.
23
	 *
24
	 * @inheritdoc
25
	 */
26
	public function load() {
27
		parent::load();
28
29
		$this->messages = array(
0 ignored issues
show
Bug introduced by
The property messages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
			'info_message' => __( 'Please include this information when posting support requests.', 'email-log' ),
31
		);
32
33
		$this->actions     = array( 'download_sysinfo' );
0 ignored issues
show
Bug introduced by
The property actions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
		add_action( 'el_download_sysinfo', array( $this, 'generate_sysinfo_download' ) );
35
36
		add_action( 'admin_init', array( $this, 'request_handler' ) );
37
		add_filter( 'el_action_nonce_check', array( $this, 'verify_nonce' ), 10, 2 );
38
	}
39
40
	/**
41
	 * Check for nonce before executing the action.
42
	 *
43
	 * @param bool   $result The current result.
44
	 * @param string $action Action name.
45
	 *
46
	 * @return bool True if nonce is verified, False otherwise.
47
	 */
48
	public function verify_nonce( $result, $action ) {
49
		/**
50
		 * List of actions for page.
51
		 *
52
		 * @param array    $actions Actions.
53
		 * @param BasePage $page    Page objects.
54
		 *
55
		 * @since 2.3.0
56
		 */
57
		$page_actions = apply_filters( 'el_page_actions', $this->actions, $this );
58
59
		if ( in_array( $action, $page_actions, true ) ) {
60
			if ( check_admin_referer( 'el-{self::PAGE_SLUG}', 'el-{self::PAGE_SLUG}-nonce' ) ) {
61
				return true;
62
			}
63
		}
64
65
		return $result;
66
	}
67
68
	/**
69
	 * Register page.
70
	 */
71
	public function register_page() {
72
73
		$this->page = add_submenu_page(
74
			LogListPage::PAGE_SLUG,
75
			__( 'System Info', 'email-log' ),
76
			__( 'System Info', 'email-log' ),
77
			'manage_options',
78
			self::PAGE_SLUG,
79
			array( $this, 'render_page' )
80
		);
81
82
		add_action( "load-{$this->page}", array( $this, 'render_help_tab' ) );
83
	}
84
85
	/**
86
	 * Get current theme name.
87
	 *
88
	 * @return string Current theme name.
89
	 */
90
	protected function el_get_current_theme_name() {
91
		if ( get_bloginfo( 'version' ) < '3.4' ) {
92
			$theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' );
93
94
			return $theme_data['Name'] . ' ' . $theme_data['Version'];
95
		} else {
96
			$theme_data = wp_get_theme();
97
98
			return $theme_data->Name . ' ' . $theme_data->Version;
99
		}
100
	}
101
102
	/**
103
	 * Try to identity the hosting provider.
104
	 *
105
	 * @return string Web host name if identified, empty string otherwise.
106
	 */
107
	protected function el_identify_host() {
108
		$host = '';
109
		if ( defined( 'WPE_APIKEY' ) ) {
110
			$host = 'WP Engine';
111
		} elseif ( defined( 'PAGELYBIN' ) ) {
112
			$host = 'Pagely';
113
		}
114
115
		return $host;
116
	}
117
118
	/**
119
	 * Print plugins that are currently active.
120
	 */
121
	protected function el_print_current_plugins() {
122
		$plugins        = get_plugins();
123
		$active_plugins = get_option( 'active_plugins', array() );
124
125
		foreach ( $plugins as $plugin_path => $plugin ) {
126
			// If the plugin isn't active, don't show it.
127
			if ( ! in_array( $plugin_path, $active_plugins ) ) {
128
				continue;
129
			}
130
131
			echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
132
		}
133
	}
134
135
	/**
136
	 * Print network active plugins.
137
	 */
138
	protected function el_print_network_active_plugins() {
139
		$plugins        = wp_get_active_network_plugins();
140
		$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
141
142
		foreach ( $plugins as $plugin_path ) {
143
			$plugin_base = plugin_basename( $plugin_path );
144
145
			// If the plugin isn't active, don't show it.
146
			if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
147
				continue;
148
			}
149
150
			$plugin = get_plugin_data( $plugin_path );
151
152
			echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
153
		}
154
	}
155
156
	protected function el_plugin_version() {
157
		$plugin_path = WP_PLUGIN_DIR . '/email-log/email-log.php';
158
		$plugin_data = get_plugin_data( $plugin_path );
159
		echo $plugin_data['Version'];
160
	}
161
162
	/**
163
	 * Render the page.
164
	 */
165
	public function render_page() {
166
		global $wpdb;
167
		?>
168
		<form method = "post">
169
		<div class="updated">
170
			<p><strong><?php echo $this->messages['info_message']; ?></strong></p>
171
		</div>
172
173
		<?php if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { ?>
174
			<div class="notice notice-warning">
175
				<p><strong>
176
					<?php printf( __( 'SAVEQUERIES is <a href="%s" target="_blank">enabled</a>. This puts additional load on the memory and will restrict the number of items that can be deleted.', 'bulk-delete' ), 'https://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis' ); ?>
177
				</strong></p>
178
			</div>
179
		<?php } ?>
180
181
		<?php if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { ?>
182
			<div class="notice notice-warning">
183
				<p><strong>
184
					<?php printf( __( 'DISABLE_WP_CRON is <a href="%s" target="_blank">enabled</a>. This prevents scheduler from running.', 'bulk-delete' ), 'https://codex.wordpress.org/Editing_wp-config.php#Disable_Cron_and_Cron_Timeout' ); ?>
185
				</strong></p>
186
			</div>
187
		<?php } ?>
188
		<div class="wrap">
189
			<h1><?php _e( 'Email Log  - System Info', 'email-log' ); ?></h1>
190
191
		<textarea wrap="off" style="width:100%;height:500px;font-family:Menlo,Monaco,monospace;white-space:pre;" readonly="readonly" onclick="this.focus();this.select()" id="system-info-textarea" name="email-log-sysinfo" title="<?php _e( 'To copy the system info, click below then press Ctrl + C (PC) or Cmd + C (Mac).', 'bulk-delete' ); ?>">
192
### Begin System Info ###
193
<?php
194
	/**
195
	 * Runs before displaying system info.
196
	 *
197
	 * This action is primarily for adding extra content in System Info.
198
	 */
199
	do_action( 'el_system_info_before' );
200
?>
201
202
Multisite:                <?php echo is_multisite() ? 'Yes' . "\n" : 'No' . "\n" ?>
203
204
SITE_URL:                 <?php echo site_url() . "\n"; ?>
205
HOME_URL:                 <?php echo home_url() . "\n"; ?>
206
Browser:                  <?php echo esc_html( $_SERVER['HTTP_USER_AGENT'] ), "\n"; ?>
207
208
Permalink Structure:      <?php echo get_option( 'permalink_structure' ) . "\n"; ?>
209
Active Theme:             <?php echo $this->el_get_current_theme_name() . "\n"; ?>
210
<?php
211
		$host = $this->el_identify_host();
212
		if ( '' !== $host ) : ?>
213
Host:                     <?php echo $host . "\n\n"; ?>
214
<?php endif; ?>
215
216
<?php $post_types = get_post_types(); ?>
217
Registered Post types:    <?php echo implode( ', ', $post_types ) . "\n"; ?>
218
<?php
219
		foreach ( $post_types as $post_type ) {
220
			echo $post_type;
221
			if ( strlen( $post_type ) < 26 ) {
222
				echo str_repeat( ' ', 26 - strlen( $post_type ) );
223
			}
224
			$post_count = wp_count_posts( $post_type );
225
			foreach ( $post_count as $key => $value ) {
226
				echo $key, '=', $value, ', ';
227
			}
228
			echo "\n";
229
		}
230
?>
231
232
<?php $taxonomies = get_taxonomies(); ?>
233
Registered Taxonomies:    <?php echo implode( ', ', $taxonomies ) . "\n"; ?>
234
235
Email log Version:        <?php $this->el_plugin_version() . "\n"; ?>
236
WordPress Version:        <?php echo get_bloginfo( 'version' ) . "\n"; ?>
237
PHP Version:              <?php echo PHP_VERSION . "\n"; ?>
238
MySQL Version:            <?php echo $wpdb->db_version() . "\n"; ?>
239
Web Server Info:          <?php echo $_SERVER['SERVER_SOFTWARE'] . "\n"; ?>
240
241
WordPress Memory Limit:   <?php echo WP_MEMORY_LIMIT; ?><?php echo "\n"; ?>
242
WordPress Max Limit:      <?php echo WP_MAX_MEMORY_LIMIT; ?><?php echo "\n"; ?>
243
PHP Memory Limit:         <?php echo ini_get( 'memory_limit' ) . "\n"; ?>
244
245
SAVEQUERIES:              <?php echo defined( 'SAVEQUERIES' ) ? SAVEQUERIES ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n" ?>
246
WP_DEBUG:                 <?php echo defined( 'WP_DEBUG' ) ? WP_DEBUG ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n" ?>
247
WP_SCRIPT_DEBUG:          <?php echo defined( 'WP_SCRIPT_DEBUG' ) ? WP_SCRIPT_DEBUG ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n" ?>
248
249
GMT Offset:               <?php echo esc_html( get_option( 'gmt_offset' ) ), "\n\n"; ?>
250
DISABLE_WP_CRON:          <?php echo defined( 'DISABLE_WP_CRON' ) ? DISABLE_WP_CRON ? 'Yes' . "\n" : 'No' . "\n" : 'Not set' . "\n" ?>
251
WP_CRON_LOCK_TIMEOUT:     <?php echo defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 'Not set', "\n" ?>
252
EMPTY_TRASH_DAYS:         <?php echo defined( 'EMPTY_TRASH_DAYS' ) ? EMPTY_TRASH_DAYS : 'Not set', "\n" ?>
253
254
<?php if ( version_compare( PHP_VERSION, '5.3.0', '<' ) ){ ?> 
255
PHP Safe Mode:            <?php echo ini_get( 'safe_mode' ) ? 'Yes' : 'No', "\n"; // phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved?>
256
<?php }?>
257
PHP Upload Max Size:      <?php echo ini_get( 'upload_max_filesize' ) . "\n"; ?>
258
PHP Post Max Size:        <?php echo ini_get( 'post_max_size' ) . "\n"; ?>
259
PHP Upload Max Filesize:  <?php echo ini_get( 'upload_max_filesize' ) . "\n"; ?>
260
PHP Time Limit:           <?php echo ini_get( 'max_execution_time' ) . "\n"; ?>
261
<?php if ( version_compare( PHP_VERSION, '5.3.8', '<' ) ){ ?> 
262
PHP Max Input Vars:       <?php echo ini_get( 'max_input_vars' ) . "\n"; // phpcs:ignore PHPCompatibility.PHP.NewIniDirectives.max_input_varsFound?>
263
<?php }?>
264
PHP Arg Separator:        <?php echo ini_get( 'arg_separator.output' ) . "\n"; ?>
265
PHP Allow URL File Open:  <?php echo ini_get( 'allow_url_fopen' ) ? 'Yes' : 'No', "\n"; ?>
266
267
WP Table Prefix:          <?php echo $wpdb->prefix, "\n";?>
268
269
Session:                  <?php echo isset( $_SESSION ) ? 'Enabled' : 'Disabled'; ?><?php echo "\n"; ?>
270
Session Name:             <?php echo esc_html( ini_get( 'session.name' ) ); ?><?php echo "\n"; ?>
271
Cookie Path:              <?php echo esc_html( ini_get( 'session.cookie_path' ) ); ?><?php echo "\n"; ?>
272
Save Path:                <?php echo esc_html( ini_get( 'session.save_path' ) ); ?><?php echo "\n"; ?>
273
Use Cookies:              <?php echo ini_get( 'session.use_cookies' ) ? 'On' : 'Off'; ?><?php echo "\n"; ?>
274
Use Only Cookies:         <?php echo ini_get( 'session.use_only_cookies' ) ? 'On' : 'Off'; ?><?php echo "\n"; ?>
275
276
DISPLAY ERRORS:           <?php echo ( ini_get( 'display_errors' ) ) ? 'On (' . ini_get( 'display_errors' ) . ')' : 'N/A'; ?><?php echo "\n"; ?>
277
FSOCKOPEN:                <?php echo ( function_exists( 'fsockopen' ) ) ? 'Your server supports fsockopen.' : 'Your server does not support fsockopen.'; ?><?php echo "\n"; ?>
278
cURL:                     <?php echo ( function_exists( 'curl_init' ) ) ? 'Your server supports cURL.' : 'Your server does not support cURL.'; ?><?php echo "\n"; ?>
279
SOAP Client:              <?php echo ( class_exists( 'SoapClient' ) ) ? 'Your server has the SOAP Client enabled.' : 'Your server does not have the SOAP Client enabled.'; ?><?php echo "\n"; ?>
280
SUHOSIN:                  <?php echo ( extension_loaded( 'suhosin' ) ) ? 'Your server has SUHOSIN installed.' : 'Your server does not have SUHOSIN installed.'; ?><?php echo "\n"; ?>
281
282
ACTIVE PLUGINS:
283
284
<?php $this->el_print_current_plugins(); ?>
285
286
<?php
287
		if ( is_multisite() ) : ?>
288
NETWORK ACTIVE PLUGINS:
289
290
<?php
291
			$this->el_print_network_active_plugins();
292
		endif;
293
?>
294
295
<?php do_action( 'el_system_info_after' );?>
296
### End System Info ###</textarea>
297
298
		<p class="submit">
299
			<input type="hidden" name="el_action" value="download_sysinfo">
300
			<?php submit_button( 'Download System Info File', 'primary', 'email-log-sysinfo', false ); ?>
301
		</p>
302
303
304
		</div>
305
	</form>
306
		<?php
307
308
		$this->render_page_footer();
309
	}
310
311
	/**
312
	 * Generates the System Info Download File.
313
	 */
314
	public function generate_sysinfo_download() {
315
		nocache_headers();
316
317
		header( 'Content-type: text/plain' );
318
		header( 'Content-Disposition: attachment; filename="email-log-system-info.txt"' );
319
320
		echo wp_strip_all_tags( $_POST['email-log-sysinfo'] );
321
		die();
322
	}
323
324
	/**
325
	 * Handle both POST and GET requests.
326
	 * This method automatically triggers all the actions after checking the nonce.
327
	 */
328
	public function request_handler() {
329
		if ( isset( $_POST['el_action'] ) ) {
330
			$el_action   = sanitize_text_field( $_POST['el_action'] );
331
332
			/**
333
			 * Perform the operation.
334
			 * This hook is for doing the operation. Nonce check has already happened by this point.
335
			 *
336
			 * @since 2.3.0
337
			 */
338
			do_action( 'el_' . $el_action, $_POST );
339
		}
340
	}
341
342
}
343