Completed
Pull Request — master (#11647)
by
unknown
10:33
created

WC_Admin_Status::get_tools()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Debug/Status page
4
 *
5
 * @author      WooThemes
6
 * @category    Admin
7
 * @package     WooCommerce/Admin/System Status
8
 * @version     2.2.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * WC_Admin_Status Class.
17
 */
18
class WC_Admin_Status {
19
20
	/**
21
	 * Handles output of the reports page in admin.
22
	 */
23
	public static function output() {
24
		include_once( dirname( __FILE__ ) . '/views/html-admin-page-status.php' );
25
	}
26
27
	/**
28
	 * Handles output of report.
29
	 */
30
	public static function status_report() {
31
		include_once( dirname( __FILE__ ) . '/views/html-admin-page-status-report.php' );
32
	}
33
34
	/**
35
	 * Handles output of tools.
36
	 */
37
	public static function status_tools() {
38
		global $wpdb;
39
40
		$tools = self::get_tools();
41
42
		if ( ! empty( $_GET['action'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) ) {
43
			$tools_controller = new WC_REST_System_Status_Tools_Controller;
44
			$action           = wc_clean( $_GET['action'] );
45
46
			if ( array_key_exists( $action, $tools ) ) {
47
				$response = $tools_controller->execute_tool( $action );
48
			} else {
49
				$response = array( 'success' => false, 'message' => __( 'Tool does not exist.', 'woocommerce' ) );
50
			}
51
52
			if ( $response['success'] ) {
53
				echo '<div class="updated inline"><p>' . esc_html( $response['message'] ) . '</p></div>';
54
			} else {
55
				echo '<div class="error inline"><p>' . esc_html( $response['message'] ) . '</p></div>';
56
			}
57
		}
58
59
		// Display message if settings settings have been saved
60
		if ( isset( $_REQUEST['settings-updated'] ) ) {
61
			echo '<div class="updated inline"><p>' . __( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>';
62
		}
63
64
		include_once( dirname( __FILE__ ) . '/views/html-admin-page-status-tools.php' );
65
	}
66
67
	/**
68
	 * Get tools.
69
	 * @return array of tools
70
	 */
71
	public static function get_tools() {
72
		$tools_controller = new WC_REST_System_Status_Tools_Controller;
73
		return $tools_controller->get_tools();
74
	}
75
76
	/**
77
	 * Show the logs page.
78
	 */
79
	public static function status_logs() {
80
81
		$logs = self::scan_log_files();
82
83
		if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( $_REQUEST['log_file'] ) ] ) ) {
84
			$viewed_log = $logs[ sanitize_title( $_REQUEST['log_file'] ) ];
85
		} elseif ( ! empty( $logs ) ) {
86
			$viewed_log = current( $logs );
87
		}
88
89
		$handle = ! empty( $viewed_log ) ? self::get_log_file_handle( $viewed_log ) : '';
0 ignored issues
show
Unused Code introduced by
$handle is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
91
		if ( ! empty( $_REQUEST[ 'handle' ] ) ) {
92
			self::remove_log();
93
		}
94
95
		include_once( 'views/html-admin-page-status-logs.php' );
96
	}
97
98
	/**
99
	 * Retrieve metadata from a file. Based on WP Core's get_file_data function.
100
	 * @since  2.1.1
101
	 * @param  string $file Path to the file
102
	 * @return string
103
	 */
104
	public static function get_file_version( $file ) {
105
106
		// Avoid notices if file does not exist
107
		if ( ! file_exists( $file ) ) {
108
			return '';
109
		}
110
111
		// We don't need to write to the file, so just open for reading.
112
		$fp = fopen( $file, 'r' );
113
114
		// Pull only the first 8kiB of the file in.
115
		$file_data = fread( $fp, 8192 );
116
117
		// PHP will close file handle, but we are good citizens.
118
		fclose( $fp );
119
120
		// Make sure we catch CR-only line endings.
121
		$file_data = str_replace( "\r", "\n", $file_data );
122
		$version   = '';
123
124
		if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] )
125
			$version = _cleanup_header_comment( $match[1] );
126
127
		return $version ;
128
	}
129
130
	/**
131
	 * Return the log file handle.
132
	 *
133
	 * @param string $filename
134
	 * @return string
135
	 */
136
	public static function get_log_file_handle( $filename ) {
137
		return substr( $filename, 0, strlen( $filename ) > 37 ? strlen( $filename ) - 37 : strlen( $filename ) - 4 );
138
	}
139
140
	/**
141
	 * Scan the template files.
142
	 * @param  string $template_path
143
	 * @return array
144
	 */
145
	public static function scan_template_files( $template_path ) {
146
147
		$files  = @scandir( $template_path );
148
		$result = array();
149
150
		if ( ! empty( $files ) ) {
151
152
			foreach ( $files as $key => $value ) {
153
154
				if ( ! in_array( $value, array( ".",".." ) ) ) {
155
156
					if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) {
157
						$sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value );
158
						foreach ( $sub_files as $sub_file ) {
159
							$result[] = $value . DIRECTORY_SEPARATOR . $sub_file;
160
						}
161
					} else {
162
						$result[] = $value;
163
					}
164
				}
165
			}
166
		}
167
		return $result;
168
	}
169
170
	/**
171
	 * Scan the log files.
172
	 * @return array
173
	 */
174
	public static function scan_log_files() {
175
		$files  = @scandir( WC_LOG_DIR );
176
		$result = array();
177
178
		if ( ! empty( $files ) ) {
179
180
			foreach ( $files as $key => $value ) {
181
182
				if ( ! in_array( $value, array( '.', '..' ) ) ) {
183
					if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
184
						$result[ sanitize_title( $value ) ] = $value;
185
					}
186
				}
187
			}
188
189
		}
190
191
		return $result;
192
	}
193
194
	/**
195
	 * Get latest version of a theme by slug.
196
	 * @param  object $theme WP_Theme object.
197
	 * @return string Version number if found.
198
	 */
199
	public static function get_latest_theme_version( $theme ) {
200
		$api = themes_api( 'theme_information', array(
201
			'slug'     => $theme->get_stylesheet(),
202
			'fields'   => array(
203
				'sections' => false,
204
				'tags'     => false,
205
			)
206
		) );
207
208
		$update_theme_version = 0;
209
210
		// Check .org for updates.
211
		if ( is_object( $api ) && ! is_wp_error( $api ) ) {
212
			$update_theme_version = $api->version;
213
214
		// Check WooThemes Theme Version.
215
		} elseif ( strstr( $theme->{'Author URI'}, 'woothemes' ) ) {
216
			$theme_dir = substr( strtolower( str_replace( ' ','', $theme->Name ) ), 0, 45 );
217
218 View Code Duplication
			if ( false === ( $theme_version_data = get_transient( $theme_dir . '_version_data' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
				$theme_changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $theme_dir . '/changelog.txt' );
220
				$cl_lines  = explode( "\n", wp_remote_retrieve_body( $theme_changelog ) );
221
				if ( ! empty( $cl_lines ) ) {
222
					foreach ( $cl_lines as $line_num => $cl_line ) {
223
						if ( preg_match( '/^[0-9]/', $cl_line ) ) {
224
							$theme_date         = str_replace( '.' , '-' , trim( substr( $cl_line , 0 , strpos( $cl_line , '-' ) ) ) );
225
							$theme_version      = preg_replace( '~[^0-9,.]~' , '' ,stristr( $cl_line , "version" ) );
226
							$theme_update       = trim( str_replace( "*" , "" , $cl_lines[ $line_num + 1 ] ) );
227
							$theme_version_data = array( 'date' => $theme_date , 'version' => $theme_version , 'update' => $theme_update , 'changelog' => $theme_changelog );
228
							set_transient( $theme_dir . '_version_data', $theme_version_data , DAY_IN_SECONDS );
229
							break;
230
						}
231
					}
232
				}
233
			}
234
235
			if ( ! empty( $theme_version_data['version'] ) ) {
236
				$update_theme_version = $theme_version_data['version'];
237
			}
238
		}
239
240
		return $update_theme_version;
241
	}
242
243
	/**
244
	 * Remove/delete the chosen file.
245
	 */
246
	public static function remove_log() {
247 View Code Duplication
		if ( empty( $_REQUEST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_REQUEST[ '_wpnonce' ], 'remove_log' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
			wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
249
		}
250
251
		if ( ! empty( $_REQUEST[ 'handle' ] ) ) {
252
			$logger = wc_get_logger();
253
			$logger->remove( $_REQUEST[ 'handle' ] );
254
		}
255
256
		wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
257
		exit();
258
	}
259
}
260