Completed
Pull Request — master (#11647)
by
unknown
08:14
created

WC_Admin_Status::remove_log()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 3
Ratio 23.08 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 0
dl 3
loc 13
rs 9.2
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();
0 ignored issues
show
Unused Code introduced by
$tools 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...
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
			$response = $tools_controller->execute_tool( $_GET['action'] );
45
46
			if ( $response['success'] ) {
47
				echo '<div class="updated inline"><p>' . $response['message'] . '</p></div>';
48
			} else {
49
				echo '<div class="error inline"><p>' . $response['message'] . '</p></div>';
50
			}
51
		}
52
53
		// Display message if settings settings have been saved
54
		if ( isset( $_REQUEST['settings-updated'] ) ) {
55
			echo '<div class="updated inline"><p>' . __( 'Your changes have been saved.', 'woocommerce' ) . '</p></div>';
56
		}
57
58
		include_once( dirname( __FILE__ ) . '/views/html-admin-page-status-tools.php' );
59
	}
60
61
	/**
62
	 * Get tools.
63
	 * @return array of tools
64
	 */
65
	public static function get_tools() {
66
		$tools_controller = new WC_REST_System_Status_Tools_Controller;
67
		return $tools_controller->get_tools();
68
	}
69
70
	/**
71
	 * Show the logs page.
72
	 */
73
	public static function status_logs() {
74
75
		$logs = self::scan_log_files();
76
77
		if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( $_REQUEST['log_file'] ) ] ) ) {
78
			$viewed_log = $logs[ sanitize_title( $_REQUEST['log_file'] ) ];
79
		} elseif ( ! empty( $logs ) ) {
80
			$viewed_log = current( $logs );
81
		}
82
83
		$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...
84
85
		if ( ! empty( $_REQUEST[ 'handle' ] ) ) {
86
			self::remove_log();
87
		}
88
89
		include_once( 'views/html-admin-page-status-logs.php' );
90
	}
91
92
	/**
93
	 * Retrieve metadata from a file. Based on WP Core's get_file_data function.
94
	 * @since  2.1.1
95
	 * @param  string $file Path to the file
96
	 * @return string
97
	 */
98
	public static function get_file_version( $file ) {
99
100
		// Avoid notices if file does not exist
101
		if ( ! file_exists( $file ) ) {
102
			return '';
103
		}
104
105
		// We don't need to write to the file, so just open for reading.
106
		$fp = fopen( $file, 'r' );
107
108
		// Pull only the first 8kiB of the file in.
109
		$file_data = fread( $fp, 8192 );
110
111
		// PHP will close file handle, but we are good citizens.
112
		fclose( $fp );
113
114
		// Make sure we catch CR-only line endings.
115
		$file_data = str_replace( "\r", "\n", $file_data );
116
		$version   = '';
117
118
		if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] )
119
			$version = _cleanup_header_comment( $match[1] );
120
121
		return $version ;
122
	}
123
124
	/**
125
	 * Return the log file handle.
126
	 *
127
	 * @param string $filename
128
	 * @return string
129
	 */
130
	public static function get_log_file_handle( $filename ) {
131
		return substr( $filename, 0, strlen( $filename ) > 37 ? strlen( $filename ) - 37 : strlen( $filename ) - 4 );
132
	}
133
134
	/**
135
	 * Scan the template files.
136
	 * @param  string $template_path
137
	 * @return array
138
	 */
139
	public static function scan_template_files( $template_path ) {
140
141
		$files  = @scandir( $template_path );
142
		$result = array();
143
144
		if ( ! empty( $files ) ) {
145
146
			foreach ( $files as $key => $value ) {
147
148
				if ( ! in_array( $value, array( ".",".." ) ) ) {
149
150
					if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) {
151
						$sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value );
152
						foreach ( $sub_files as $sub_file ) {
153
							$result[] = $value . DIRECTORY_SEPARATOR . $sub_file;
154
						}
155
					} else {
156
						$result[] = $value;
157
					}
158
				}
159
			}
160
		}
161
		return $result;
162
	}
163
164
	/**
165
	 * Scan the log files.
166
	 * @return array
167
	 */
168
	public static function scan_log_files() {
169
		$files  = @scandir( WC_LOG_DIR );
170
		$result = array();
171
172
		if ( ! empty( $files ) ) {
173
174
			foreach ( $files as $key => $value ) {
175
176
				if ( ! in_array( $value, array( '.', '..' ) ) ) {
177
					if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
178
						$result[ sanitize_title( $value ) ] = $value;
179
					}
180
				}
181
			}
182
183
		}
184
185
		return $result;
186
	}
187
188
	/**
189
	 * Get latest version of a theme by slug.
190
	 * @param  object $theme WP_Theme object.
191
	 * @return string Version number if found.
192
	 */
193
	public static function get_latest_theme_version( $theme ) {
194
		$api = themes_api( 'theme_information', array(
195
			'slug'     => $theme->get_stylesheet(),
196
			'fields'   => array(
197
				'sections' => false,
198
				'tags'     => false,
199
			)
200
		) );
201
202
		$update_theme_version = 0;
203
204
		// Check .org for updates.
205
		if ( is_object( $api ) && ! is_wp_error( $api ) ) {
206
			$update_theme_version = $api->version;
207
208
		// Check WooThemes Theme Version.
209
		} elseif ( strstr( $theme->{'Author URI'}, 'woothemes' ) ) {
210
			$theme_dir = substr( strtolower( str_replace( ' ','', $theme->Name ) ), 0, 45 );
211
212 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...
213
				$theme_changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $theme_dir . '/changelog.txt' );
214
				$cl_lines  = explode( "\n", wp_remote_retrieve_body( $theme_changelog ) );
215
				if ( ! empty( $cl_lines ) ) {
216
					foreach ( $cl_lines as $line_num => $cl_line ) {
217
						if ( preg_match( '/^[0-9]/', $cl_line ) ) {
218
							$theme_date         = str_replace( '.' , '-' , trim( substr( $cl_line , 0 , strpos( $cl_line , '-' ) ) ) );
219
							$theme_version      = preg_replace( '~[^0-9,.]~' , '' ,stristr( $cl_line , "version" ) );
220
							$theme_update       = trim( str_replace( "*" , "" , $cl_lines[ $line_num + 1 ] ) );
221
							$theme_version_data = array( 'date' => $theme_date , 'version' => $theme_version , 'update' => $theme_update , 'changelog' => $theme_changelog );
222
							set_transient( $theme_dir . '_version_data', $theme_version_data , DAY_IN_SECONDS );
223
							break;
224
						}
225
					}
226
				}
227
			}
228
229
			if ( ! empty( $theme_version_data['version'] ) ) {
230
				$update_theme_version = $theme_version_data['version'];
231
			}
232
		}
233
234
		return $update_theme_version;
235
	}
236
237
	/**
238
	 * Remove/delete the chosen file.
239
	 */
240
	public static function remove_log() {
241 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...
242
			wp_die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
243
		}
244
245
		if ( ! empty( $_REQUEST[ 'handle' ] ) ) {
246
			$logger = wc_get_logger();
247
			$logger->remove( $_REQUEST[ 'handle' ] );
248
		}
249
250
		wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) );
251
		exit();
252
	}
253
}
254