Completed
Push — dev/2.3.0 ( 7ef92d...92929a )
by Sudar
30:39 queued 27:56
created

SystemInfo::render()   F

Complexity

Conditions 32
Paths 96

Size

Total Lines 137
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1056

Importance

Changes 0
Metric Value
cc 32
eloc 114
nc 96
nop 0
dl 0
loc 137
ccs 0
cts 16
cp 0
crap 1056
rs 3.3333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace EmailLog\Core\UI\Component;
4
5
/**
6
 * Shows and generates the System Info file.
7
 *
8
 * This will be moved into a seperate repo as a library.
9
 *
10
 * Greatly inspired (and shares code) from the system info component in Easy Digital Downloads plugin.
11
 *
12
 * @since 2.3.0
13
 */
14
class SystemInfo {
15
	/**
16
	 * Plugin slug.
17
	 *
18
	 * @var string
19
	 */
20
	protected $plugin_slug = '';
21
22
	/**
23
	 * Config that controls which sections should be displayed.
24
	 *
25
	 * @var array
26
	 */
27
	protected $config = array();
28
29
	/**
30
	 * SystemInfo constructor.
31
	 *
32
	 * @param string $plugin_slug Slug of the plugin.
33
	 * @param array  $config      (Optional) Configuration options.
34
	 *
35
	 * @see SystemInfo::get_default_config for the list of default config information.
36
	 */
37
	public function __construct( $plugin_slug, $config = array() ) {
38
		$this->plugin_slug = $plugin_slug;
39
		$this->config      = wp_parse_args( $config, $this->get_default_config() );
40
	}
41
42
	/**
43
	 * Get Default configuration.
44
	 *
45
	 * @return array Default configuration.
46
	 */
47
	protected function get_default_config() {
48
		return array(
49
			'show_post_types'      => true,
50
			'show_taxonomies'      => true,
51
			'show_plugins'         => true,
52
			'show_network_plugins' => true,
53
		);
54
	}
55
56
	/**
57
	 * Render system info.
58
	 *
59
	 * PHPCS is disabled for this function since aligned will mess up the system info output.
60
	 * phpcs:disable
61
	 */
62
	public function render() {
63
		global $wpdb;
64
65
		?>
66
		<textarea wrap="off" readonly="readonly" name="<?php echo esc_attr( $this->plugin_slug ); ?>-system-info"
67
		          style="font-family:Menlo,Monaco,monospace; white-space:pre; width:100%; height:500px;" onclick="this.focus();this.select()"
68
		          title="<?php _e( 'To copy the system info, click below then press Ctrl + C (PC) or Cmd + C (Mac).', 'email-log' ); ?>">
69
### Begin System Info (Generated <?php echo date( 'Y-m-d H:i:s') ?>) ###
70
71
<?php
72
/**
73
 * Runs before displaying system info.
74
 *
75
 * This action is primarily for adding extra content in System Info.
76
 *
77
 * @param string $plugin_name Plugin slug.
78
 */
79
do_action( 'system_info_before', $this->plugin_slug );
80
$locale = get_locale();
81
?>
82
83
-- Site Info
84
85
Site URL:                 <?php echo site_url() . "\n"; ?>
86
Home URL:                 <?php echo home_url() . "\n"; ?>
87
Multisite:                <?php echo is_multisite() ? 'Yes' . "\n" : 'No' . "\n"; ?>
88
89
<?php
90
$host = $this->identify_host();
91
if ( ! empty( $host ) ) : ?>
92
-- Hosting Provider
93
94
Host:                     <?php echo $host . "\n"; ?>
95
<?php endif; ?>
96
-- User Browser
97
98
Platform:                 <?php echo php_uname('s') . "\n"; ?>
99
User Agent String:        <?php echo esc_html( $_SERVER['HTTP_USER_AGENT'] ), "\n"; ?>
100
101
-- WordPress Configuration:
102
103
Version:                  <?php echo get_bloginfo( 'version' ) . "\n"; ?>
104
Language:                 <?php ( ! empty( $locale ) ? $locale : 'en_US' ) . "\n"; ?>
105
Permalink Structure:      <?php echo get_option( 'permalink_structure' ) . "\n"; ?>
0 ignored issues
show
Bug introduced by
Are you sure get_option('permalink_structure') of type mixed|false can be used in concatenation? ( Ignorable by Annotation )

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

105
Permalink Structure:      <?php echo /** @scrutinizer ignore-type */ get_option( 'permalink_structure' ) . "\n"; ?>
Loading history...
106
Active Theme:             <?php echo $this->get_current_theme_name() . "\n"; ?>
107
ABSPATH:                  <?php echo ABSPATH . "\n"; ?>
108
WP Table Prefix:          <?php echo $wpdb->prefix, "\n"; ?>
109
WP_DEBUG:                 <?php echo defined( 'WP_DEBUG' ) ? WP_DEBUG ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n"; ?>
110
Memory Limit:             <?php echo WP_MEMORY_LIMIT; ?><?php echo "\n"; ?>
111
Memory Max Limit:         <?php echo WP_MAX_MEMORY_LIMIT; ?><?php echo "\n"; ?>
112
WP_SCRIPT_DEBUG:          <?php echo defined( 'WP_SCRIPT_DEBUG' ) ? WP_SCRIPT_DEBUG ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n"; ?>
0 ignored issues
show
Bug introduced by
The constant EmailLog\Core\UI\Component\WP_SCRIPT_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
113
GMT Offset:               <?php echo esc_html( get_option( 'gmt_offset' ) ), "\n"; ?>
0 ignored issues
show
Bug introduced by
It seems like get_option('gmt_offset') can also be of type false; however, parameter $text of esc_html() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

113
GMT Offset:               <?php echo esc_html( /** @scrutinizer ignore-type */ get_option( 'gmt_offset' ) ), "\n"; ?>
Loading history...
114
DISABLE_WP_CRON:          <?php echo defined( 'DISABLE_WP_CRON' ) ? DISABLE_WP_CRON ? 'Yes' . "\n" : 'No' . "\n" : 'Not set' . "\n"; ?>
115
WP_CRON_LOCK_TIMEOUT:     <?php echo defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 'Not set', "\n"; ?>
116
EMPTY_TRASH_DAYS:         <?php echo defined( 'EMPTY_TRASH_DAYS' ) ? EMPTY_TRASH_DAYS : 'Not set', "\n"; ?>
117
118
<?php if ( $this->config['show_post_types'] ) : ?>
119
<?php $post_types = get_post_types(); ?>
120
Registered Post types:    <?php echo implode( ', ', $post_types ) . "\n"; ?>
121
<?php
122
foreach ( $post_types as $post_type ) {
123
	echo $post_type;
124
	if ( strlen( $post_type ) < 26 ) {
125
		echo str_repeat( ' ', 26 - strlen( $post_type ) );
126
	}
127
	$post_count = wp_count_posts( $post_type );
128
	foreach ( $post_count as $key => $value ) {
129
		echo $key, '=', $value, ', ';
130
	}
131
	echo "\n";
132
}
133
?>
134
<?php endif; ?>
135
136
<?php if ( $this->config['show_taxonomies'] ) : ?>
137
<?php $taxonomies = get_taxonomies(); ?>
138
Registered Taxonomies:    <?php echo implode( ', ', $taxonomies ) . "\n"; ?>
139
<?php endif; ?>
140
<?php if ( $this->config['show_plugins'] ) : ?>
141
-- WordPress Active Plugins
142
143
<?php $this->print_current_plugins(); ?>
144
<?php endif;?>
145
146
<?php if ( $this->config['show_network_plugins'] ) : ?>
147
<?php if ( is_multisite() ) : ?>
148
-- Network Active Plugins
149
150
<?php $this->print_network_active_plugins(); ?>
151
<?php endif;?>
152
<?php endif;?>
153
-- Webserver Configuration
154
155
PHP Version:              <?php echo PHP_VERSION . "\n"; ?>
156
MySQL Version:            <?php echo $wpdb->db_version() . "\n"; ?>
157
Web Server Info:          <?php echo $_SERVER['SERVER_SOFTWARE'] . "\n"; ?>
158
159
-- PHP Configuration
160
161
PHP Memory Limit:         <?php echo ini_get( 'memory_limit' ) . "\n"; ?>
162
SAVEQUERIES:              <?php echo defined( 'SAVEQUERIES' ) ? SAVEQUERIES ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n"; ?>
0 ignored issues
show
Bug introduced by
The constant EmailLog\Core\UI\Component\SAVEQUERIES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
163
PHP Safe Mode:            <?php echo ini_get( 'safe_mode' ) ? 'Yes' : 'No', "\n"; // phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved?>
164
PHP Upload Max Size:      <?php echo ini_get( 'upload_max_filesize' ) . "\n"; ?>
165
PHP Post Max Size:        <?php echo ini_get( 'post_max_size' ) . "\n"; ?>
166
PHP Upload Max Filesize:  <?php echo ini_get( 'upload_max_filesize' ) . "\n"; ?>
167
PHP Time Limit:           <?php echo ini_get( 'max_execution_time' ) . "\n"; ?>
168
PHP Max Input Vars:       <?php echo ini_get( 'max_input_vars' ) . "\n"; // phpcs:ignore PHPCompatibility.PHP.NewIniDirectives.max_input_varsFound?>
169
Display Errors:           <?php echo ( ini_get( 'display_errors' ) ) ? 'On (' . ini_get( 'display_errors' ) . ')' : 'N/A'; ?><?php echo "\n"; ?>
170
PHP Arg Separator:        <?php echo ini_get( 'arg_separator.output' ) . "\n"; ?>
171
PHP Allow URL File Open:  <?php echo ini_get( 'allow_url_fopen' ) ? 'Yes' : 'No', "\n"; ?>
172
173
-- PHP Extensions
174
175
fsockopen:                <?php echo ( function_exists( 'fsockopen' ) ) ? 'Your server supports fsockopen.' : 'Your server does not support fsockopen.'; ?><?php echo "\n"; ?>
176
cURL:                     <?php echo ( function_exists( 'curl_init' ) ) ? 'Your server supports cURL.' : 'Your server does not support cURL.'; ?><?php echo "\n"; ?>
177
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"; ?>
178
SUHOSIN:                  <?php echo ( extension_loaded( 'suhosin' ) ) ? 'Your server has SUHOSIN installed.' : 'Your server does not have SUHOSIN installed.'; ?><?php echo "\n"; ?>
179
180
-- Session Configuration
181
182
Session:                  <?php echo isset( $_SESSION ) ? 'Enabled' : 'Disabled'; ?><?php echo "\n"; ?>
183
Session Name:             <?php echo esc_html( ini_get( 'session.name' ) ); ?><?php echo "\n"; ?>
184
Cookie Path:              <?php echo esc_html( ini_get( 'session.cookie_path' ) ); ?><?php echo "\n"; ?>
185
Save Path:                <?php echo esc_html( ini_get( 'session.save_path' ) ); ?><?php echo "\n"; ?>
186
Use Cookies:              <?php echo ini_get( 'session.use_cookies' ) ? 'On' : 'Off'; ?><?php echo "\n"; ?>
187
Use Only Cookies:         <?php echo ini_get( 'session.use_only_cookies' ) ? 'On' : 'Off'; ?><?php echo "\n"; ?>
188
189
<?php
190
/**
191
 * Runs after displaying system info.
192
 *
193
 * This action is primarily for adding extra content in System Info.
194
 *
195
 * @param string $plugin_name Plugin slug.
196
 */
197
do_action( 'system_info_after', $this->plugin_slug ); ?>
198
### End System Info ###</textarea>
199
		<?php
200
	}
201
	// phpcs:enable
202
203
	/**
204
	 * Download System info as a file.
205
	 *
206
	 * @param string $file_name (Optional)Name of the file. Default is {plugin slug}-system-info.txt.
207
	 */
208
	public function download_as_file( $file_name = '' ) {
209
		if ( empty( $file_name ) ) {
210
			$file_name = $this->plugin_slug . '-system-info.txt';
211
		}
212
213
		nocache_headers();
214
215
		header( 'Content-type: text/plain' );
216
		header( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
217
218
		echo wp_strip_all_tags( $_POST[ $this->plugin_slug . '-system-info'] );
219
		die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
220
	}
221
222
	/**
223
	 * Get current theme name.
224
	 *
225
	 * @return string Current theme name.
226
	 */
227
	protected function get_current_theme_name() {
228
		if ( get_bloginfo( 'version' ) < '3.4' ) {
229
			$theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' );
0 ignored issues
show
Deprecated Code introduced by
The function get_theme_data() has been deprecated: 3.4.0 Use wp_get_theme() ( Ignorable by Annotation )

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

229
			$theme_data = /** @scrutinizer ignore-deprecated */ get_theme_data( get_stylesheet_directory() . '/style.css' );

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
230
231
			return $theme_data['Name'] . ' ' . $theme_data['Version'];
232
		}
233
234
		$theme_data = wp_get_theme();
235
236
		return $theme_data->Name . ' ' . $theme_data->Version;
0 ignored issues
show
Bug introduced by
Are you sure $theme_data->Version of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

236
		return $theme_data->Name . ' ' . /** @scrutinizer ignore-type */ $theme_data->Version;
Loading history...
Bug introduced by
Are you sure $theme_data->Name of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

236
		return /** @scrutinizer ignore-type */ $theme_data->Name . ' ' . $theme_data->Version;
Loading history...
237
	}
238
239
	/**
240
	 * Try to identity the hosting provider.
241
	 *
242
	 * @return string Web host name if identified, empty string otherwise.
243
	 */
244
	protected function identify_host() {
245
		$host = '';
246
247
		if ( defined( 'WPE_APIKEY' ) ) {
248
			$host = 'WP Engine';
249
		} elseif ( defined( 'PAGELYBIN' ) ) {
250
			$host = 'Pagely';
251
		}
252
253
		/**
254
		 * Filter the identified webhost.
255
		 *
256
		 * @param string $host        Identified web host.
257
		 * @param string $plugin_name Plugin slug.
258
		 */
259
		return apply_filters( 'system_info_host', $host, $this->plugin_slug );
260
	}
261
262
	/**
263
	 * Print plugins that are currently active.
264
	 */
265
	protected function print_current_plugins() {
266
		$plugins        = get_plugins();
267
		$active_plugins = get_option( 'active_plugins', array() );
268
269
		foreach ( $plugins as $plugin_path => $plugin ) {
270
			// If the plugin isn't active, don't show it.
271
			if ( ! in_array( $plugin_path, $active_plugins, true ) ) {
0 ignored issues
show
Bug introduced by
It seems like $active_plugins can also be of type false; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

271
			if ( ! in_array( $plugin_path, /** @scrutinizer ignore-type */ $active_plugins, true ) ) {
Loading history...
272
				continue;
273
			}
274
275
			echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
276
		}
277
	}
278
279
	/**
280
	 * Print network active plugins.
281
	 */
282
	protected function print_network_active_plugins() {
283
		$plugins        = wp_get_active_network_plugins();
284
		$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
285
286
		foreach ( $plugins as $plugin_path ) {
287
			$plugin_base = plugin_basename( $plugin_path );
288
289
			// If the plugin isn't active, don't show it.
290
			if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
291
				continue;
292
			}
293
294
			$plugin = get_plugin_data( $plugin_path );
295
296
			echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
297
		}
298
	}
299
}
300