Completed
Push — 125-fix/improve-preview ( be8b7d...ab5155 )
by Maria Daniel Deepak
07:24 queued 03:14
created

SystemInfo::get_current_theme_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 ###
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
?>
81
82
Multisite:                <?php echo is_multisite() ? 'Yes' . "\n" : 'No' . "\n"; ?>
83
84
SITE_URL:                 <?php echo site_url() . "\n"; ?>
85
HOME_URL:                 <?php echo home_url() . "\n"; ?>
86
Browser:                  <?php echo esc_html( $_SERVER['HTTP_USER_AGENT'] ), "\n"; ?>
87
88
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

88
Permalink Structure:      <?php echo /** @scrutinizer ignore-type */ get_option( 'permalink_structure' ) . "\n"; ?>
Loading history...
89
Active Theme:             <?php echo $this->get_current_theme_name() . "\n"; ?>
90
<?php
91
$host = $this->identify_host();
92
if ( ! empty( $host ) ) : ?>
93
Host:                     <?php echo $host . "\n\n"; ?>
94
<?php endif; ?>
95
96
<?php if ( $this->config['show_post_types'] ) : ?>
97
<?php $post_types = get_post_types(); ?>
98
Registered Post types:    <?php echo implode( ', ', $post_types ) . "\n"; ?>
99
<?php
100
foreach ( $post_types as $post_type ) {
101
	echo $post_type;
102
	if ( strlen( $post_type ) < 26 ) {
103
		echo str_repeat( ' ', 26 - strlen( $post_type ) );
104
	}
105
	$post_count = wp_count_posts( $post_type );
106
	foreach ( $post_count as $key => $value ) {
107
		echo $key, '=', $value, ', ';
108
	}
109
	echo "\n";
110
}
111
?>
112
<?php endif; ?>
113
114
<?php if ( $this->config['show_taxonomies'] ) : ?>
115
<?php $taxonomies = get_taxonomies(); ?>
116
Registered Taxonomies:    <?php echo implode( ', ', $taxonomies ) . "\n"; ?>
117
<?php endif; ?>
118
119
WordPress Version:        <?php echo get_bloginfo( 'version' ) . "\n"; ?>
120
PHP Version:              <?php echo PHP_VERSION . "\n"; ?>
121
MySQL Version:            <?php echo $wpdb->db_version() . "\n"; ?>
122
Web Server Info:          <?php echo $_SERVER['SERVER_SOFTWARE'] . "\n"; ?>
123
124
WordPress Memory Limit:   <?php echo WP_MEMORY_LIMIT; ?><?php echo "\n"; ?>
125
WordPress Max Limit:      <?php echo WP_MAX_MEMORY_LIMIT; ?><?php echo "\n"; ?>
126
PHP Memory Limit:         <?php echo ini_get( 'memory_limit' ) . "\n"; ?>
127
128
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...
129
WP_DEBUG:                 <?php echo defined( 'WP_DEBUG' ) ? WP_DEBUG ? 'Enabled' . "\n" : 'Disabled' . "\n" : 'Not set' . "\n"; ?>
130
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...
131
132
GMT Offset:               <?php echo esc_html( get_option( 'gmt_offset' ) ), "\n\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

132
GMT Offset:               <?php echo esc_html( /** @scrutinizer ignore-type */ get_option( 'gmt_offset' ) ), "\n\n"; ?>
Loading history...
133
DISABLE_WP_CRON:          <?php echo defined( 'DISABLE_WP_CRON' ) ? DISABLE_WP_CRON ? 'Yes' . "\n" : 'No' . "\n" : 'Not set' . "\n"; ?>
134
WP_CRON_LOCK_TIMEOUT:     <?php echo defined( 'WP_CRON_LOCK_TIMEOUT' ) ? WP_CRON_LOCK_TIMEOUT : 'Not set', "\n"; ?>
135
EMPTY_TRASH_DAYS:         <?php echo defined( 'EMPTY_TRASH_DAYS' ) ? EMPTY_TRASH_DAYS : 'Not set', "\n"; ?>
136
137
PHP Safe Mode:            <?php echo ini_get( 'safe_mode' ) ? 'Yes' : 'No', "\n"; // phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved?>
138
PHP Upload Max Size:      <?php echo ini_get( 'upload_max_filesize' ) . "\n"; ?>
139
PHP Post Max Size:        <?php echo ini_get( 'post_max_size' ) . "\n"; ?>
140
PHP Upload Max Filesize:  <?php echo ini_get( 'upload_max_filesize' ) . "\n"; ?>
141
PHP Time Limit:           <?php echo ini_get( 'max_execution_time' ) . "\n"; ?>
142
PHP Max Input Vars:       <?php echo ini_get( 'max_input_vars' ) . "\n"; // phpcs:ignore PHPCompatibility.PHP.NewIniDirectives.max_input_varsFound?>
143
PHP Arg Separator:        <?php echo ini_get( 'arg_separator.output' ) . "\n"; ?>
144
PHP Allow URL File Open:  <?php echo ini_get( 'allow_url_fopen' ) ? 'Yes' : 'No', "\n"; ?>
145
146
WP Table Prefix:          <?php echo $wpdb->prefix, "\n"; ?>
147
148
Session:                  <?php echo isset( $_SESSION ) ? 'Enabled' : 'Disabled'; ?><?php echo "\n"; ?>
149
Session Name:             <?php echo esc_html( ini_get( 'session.name' ) ); ?><?php echo "\n"; ?>
150
Cookie Path:              <?php echo esc_html( ini_get( 'session.cookie_path' ) ); ?><?php echo "\n"; ?>
151
Save Path:                <?php echo esc_html( ini_get( 'session.save_path' ) ); ?><?php echo "\n"; ?>
152
Use Cookies:              <?php echo ini_get( 'session.use_cookies' ) ? 'On' : 'Off'; ?><?php echo "\n"; ?>
153
Use Only Cookies:         <?php echo ini_get( 'session.use_only_cookies' ) ? 'On' : 'Off'; ?><?php echo "\n"; ?>
154
155
DISPLAY ERRORS:           <?php echo ( ini_get( 'display_errors' ) ) ? 'On (' . ini_get( 'display_errors' ) . ')' : 'N/A'; ?><?php echo "\n"; ?>
156
FSOCKOPEN:                <?php echo ( function_exists( 'fsockopen' ) ) ? 'Your server supports fsockopen.' : 'Your server does not support fsockopen.'; ?><?php echo "\n"; ?>
157
cURL:                     <?php echo ( function_exists( 'curl_init' ) ) ? 'Your server supports cURL.' : 'Your server does not support cURL.'; ?><?php echo "\n"; ?>
158
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"; ?>
159
SUHOSIN:                  <?php echo ( extension_loaded( 'suhosin' ) ) ? 'Your server has SUHOSIN installed.' : 'Your server does not have SUHOSIN installed.'; ?><?php echo "\n"; ?>
160
161
<?php if ( $this->config['show_plugins'] ) : ?>
162
ACTIVE PLUGINS:
163
164
<?php $this->print_current_plugins(); ?>
165
<?php endif;?>
166
167
<?php if ( $this->config['show_network_plugins'] ) : ?>
168
<?php if ( is_multisite() ) : ?>
169
NETWORK ACTIVE PLUGINS:
170
171
<?php $this->print_network_active_plugins(); ?>
172
<?php endif;?>
173
<?php endif;?>
174
175
<?php
176
/**
177
 * Runs after displaying system info.
178
 *
179
 * This action is primarily for adding extra content in System Info.
180
 *
181
 * @param string $plugin_name Plugin slug.
182
 */
183
do_action( 'system_info_after', $this->plugin_slug ); ?>
184
### End System Info ###</textarea>
185
		<?php
186
	}
187
	// phpcs:enable
188
189
	/**
190
	 * Download System info as a file.
191
	 *
192
	 * @param string $file_name (Optional)Name of the file. Default is {plugin slug}-system-info.txt.
193
	 */
194
	public function download_as_file( $file_name = '' ) {
195
		if ( empty( $file_name ) ) {
196
			$file_name = $this->plugin_slug . '-system-info.txt';
197
		}
198
199
		nocache_headers();
200
201
		header( 'Content-type: text/plain' );
202
		header( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
203
204
		echo wp_strip_all_tags( $_POST[ $this->plugin_slug . '-system-info'] );
205
		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...
206
	}
207
208
	/**
209
	 * Get current theme name.
210
	 *
211
	 * @return string Current theme name.
212
	 */
213
	protected function get_current_theme_name() {
214
		if ( get_bloginfo( 'version' ) < '3.4' ) {
215
			$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

215
			$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...
216
217
			return $theme_data['Name'] . ' ' . $theme_data['Version'];
218
		}
219
220
		$theme_data = wp_get_theme();
221
222
		return $theme_data->Name . ' ' . $theme_data->Version;
0 ignored issues
show
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

222
		return /** @scrutinizer ignore-type */ $theme_data->Name . ' ' . $theme_data->Version;
Loading history...
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

222
		return $theme_data->Name . ' ' . /** @scrutinizer ignore-type */ $theme_data->Version;
Loading history...
223
	}
224
225
	/**
226
	 * Try to identity the hosting provider.
227
	 *
228
	 * @return string Web host name if identified, empty string otherwise.
229
	 */
230
	protected function identify_host() {
231
		$host = '';
232
233
		if ( defined( 'WPE_APIKEY' ) ) {
234
			$host = 'WP Engine';
235
		} elseif ( defined( 'PAGELYBIN' ) ) {
236
			$host = 'Pagely';
237
		}
238
239
		/**
240
		 * Filter the identified webhost.
241
		 *
242
		 * @param string $host        Identified web host.
243
		 * @param string $plugin_name Plugin slug.
244
		 */
245
		return apply_filters( 'system_info_host', $host, $this->plugin_slug );
246
	}
247
248
	/**
249
	 * Print plugins that are currently active.
250
	 */
251
	protected function print_current_plugins() {
252
		$plugins        = get_plugins();
253
		$active_plugins = get_option( 'active_plugins', array() );
254
255
		foreach ( $plugins as $plugin_path => $plugin ) {
256
			// If the plugin isn't active, don't show it.
257
			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

257
			if ( ! in_array( $plugin_path, /** @scrutinizer ignore-type */ $active_plugins, true ) ) {
Loading history...
258
				continue;
259
			}
260
261
			echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
262
		}
263
	}
264
265
	/**
266
	 * Print network active plugins.
267
	 */
268
	protected function print_network_active_plugins() {
269
		$plugins        = wp_get_active_network_plugins();
270
		$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
271
272
		foreach ( $plugins as $plugin_path ) {
273
			$plugin_base = plugin_basename( $plugin_path );
274
275
			// If the plugin isn't active, don't show it.
276
			if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
277
				continue;
278
			}
279
280
			$plugin = get_plugin_data( $plugin_path );
281
282
			echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
283
		}
284
	}
285
}
286