| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Framework; |
| 4 |
|
| 5 |
// @since 1.3.4 |
| 6 |
class System_Status { |
| 7 |
|
| 8 |
/** |
| 9 |
* Get array of environment information. Includes thing like software |
| 10 |
* versions, and various server settings. |
| 11 |
* |
| 12 |
* @return array |
| 13 |
*/ |
| 14 |
public function get_environment_info() { |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
// Figure out cURL version, if installed. |
| 18 |
$curl_version = ''; |
| 19 |
|
| 20 |
if ( function_exists( 'curl_version' ) ) { |
| 21 |
$curl_version = curl_version(); |
| 22 |
$curl_version = $curl_version['version'] . ', ' . $curl_version['ssl_version']; |
| 23 |
} |
| 24 |
|
| 25 |
// WP memory limit |
| 26 |
$wp_memory_limit = erp_let_to_num( WP_MEMORY_LIMIT ); |
| 27 |
|
| 28 |
if ( function_exists( 'memory_get_usage' ) ) { |
| 29 |
$wp_memory_limit = max( $wp_memory_limit, erp_let_to_num( @ini_get( 'memory_limit' ) ) ); |
| 30 |
} |
| 31 |
|
| 32 |
// Test POST requests |
| 33 |
$post_response = wp_safe_remote_post( 'https://www.paypal.com/cgi-bin/webscr', [ |
| 34 |
'timeout' => 10, |
| 35 |
'user-agent' => 'ERP/' . wperp()->version, |
| 36 |
'httpversion' => '1.1', |
| 37 |
'body' => [ |
| 38 |
'cmd' => '_notify-validate', |
| 39 |
], |
| 40 |
] ); |
| 41 |
$post_response_successful = false; |
| 42 |
|
| 43 |
if ( ! is_wp_error( $post_response ) && $post_response['response']['code'] >= 200 && $post_response['response']['code'] < 300 ) { |
| 44 |
$post_response_successful = true; |
| 45 |
} |
| 46 |
|
| 47 |
// Test GET requests |
| 48 |
$get_response = wp_safe_remote_get( 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=' . ( is_multisite() ? '1' : '0' ) ); |
| 49 |
$get_response_successful = false; |
| 50 |
|
| 51 |
if ( ! is_wp_error( $post_response ) && $post_response['response']['code'] >= 200 && $post_response['response']['code'] < 300 ) { |
| 52 |
$get_response_successful = true; |
| 53 |
} |
| 54 |
|
| 55 |
$upload_dir = wp_upload_dir(); |
| 56 |
$log_directory = $upload_dir['basedir'] . '/erp-logs/'; |
| 57 |
|
| 58 |
$server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; |
| 59 |
|
| 60 |
// Return all environment info. Described by JSON Schema. |
| 61 |
return [ |
| 62 |
'home_url' => get_option( 'home' ), |
| 63 |
'site_url' => get_option( 'siteurl' ), |
| 64 |
'version' => wperp()->version, |
| 65 |
'log_directory' => $log_directory, |
| 66 |
'log_directory_writable' => ( @fopen( $log_directory . 'test-log.log', 'a' ) ? true : false ), |
| 67 |
'wp_version' => get_bloginfo( 'version' ), |
| 68 |
'wp_multisite' => is_multisite(), |
| 69 |
'wp_memory_limit' => $wp_memory_limit, |
| 70 |
'wp_debug_mode' => ( defined( 'WP_DEBUG' ) && WP_DEBUG ), |
| 71 |
'wp_cron' => ! ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ), |
| 72 |
'language' => get_locale(), |
| 73 |
'server_info' => $server_software, |
| 74 |
'php_version' => phpversion(), |
| 75 |
'php_post_max_size' => erp_let_to_num( ini_get( 'post_max_size' ) ), |
| 76 |
'php_max_execution_time' => ini_get( 'max_execution_time' ), |
| 77 |
'php_max_input_vars' => ini_get( 'max_input_vars' ), |
| 78 |
'curl_version' => $curl_version, |
| 79 |
'suhosin_installed' => extension_loaded( 'suhosin' ), |
| 80 |
'max_upload_size' => wp_max_upload_size(), |
| 81 |
'mysql_version' => ( ! empty( $wpdb->is_mysql ) ? $wpdb->db_version() : '' ), |
| 82 |
'default_timezone' => date_default_timezone_get(), |
| 83 |
'fsockopen_or_curl_enabled' => ( function_exists( 'fsockopen' ) || function_exists( 'curl_init' ) ), |
| 84 |
'soapclient_enabled' => class_exists( 'SoapClient' ), |
| 85 |
'domdocument_enabled' => class_exists( 'DOMDocument' ), |
| 86 |
'gzip_enabled' => is_callable( 'gzopen' ), |
| 87 |
'mbstring_enabled' => extension_loaded( 'mbstring' ), |
| 88 |
'remote_post_successful' => $post_response_successful, |
| 89 |
'remote_post_response' => ( is_wp_error( $post_response ) ? $post_response->get_error_message() : $post_response['response']['code'] ), |
| 90 |
'remote_get_successful' => $get_response_successful, |
| 91 |
'remote_get_response' => ( is_wp_error( $get_response ) ? $get_response->get_error_message() : $get_response['response']['code'] ), |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Add prefix to table. |
| 97 |
* |
| 98 |
* @param string $table table name |
| 99 |
* |
| 100 |
* @return stromg |
| 101 |
*/ |
| 102 |
protected function add_db_table_prefix( $table ) { |
| 103 |
global $wpdb; |
| 104 |
|
| 105 |
return $wpdb->prefix . $table; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get array of database information. Version, prefix, and table existence. |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_database_info() { |
| 114 |
global $wpdb; |
| 115 |
|
| 116 |
$database_table_sizes = $wpdb->get_results( $wpdb->prepare( " |
| 117 |
SELECT |
| 118 |
table_name AS 'name', |
| 119 |
round( ( data_length / 1024 / 1024 ), 2 ) 'data', |
| 120 |
round( ( index_length / 1024 / 1024 ), 2 ) 'index' |
| 121 |
FROM information_schema.TABLES |
| 122 |
WHERE table_schema = %s |
| 123 |
ORDER BY name ASC; |
| 124 |
", DB_NAME ) ); |
| 125 |
|
| 126 |
// ERP Core tables to check existence of |
| 127 |
$core_tables = apply_filters( 'erp_database_tables', [ |
| 128 |
'erp_hr_depts', |
| 129 |
'erp_hr_dependents', |
| 130 |
] ); |
| 131 |
|
| 132 |
/** |
| 133 |
* Adding the prefix to the tables array, for backwards compatibility. |
| 134 |
* |
| 135 |
* If we changed the tables above to include the prefix, then any filters against that table could break. |
| 136 |
*/ |
| 137 |
$core_tables = array_map( [ $this, 'add_db_table_prefix' ], $core_tables ); |
| 138 |
|
| 139 |
/** |
| 140 |
* Organize WooCommerce and non-WooCommerce tables separately for display purposes later. |
| 141 |
* |
| 142 |
* To ensure we include all ERP tables, even if they do not exist, pre-populate the ERP array with all the tables. |
| 143 |
*/ |
| 144 |
$tables = [ |
| 145 |
'erp' => array_fill_keys( $core_tables, false ), |
| 146 |
'other' => [], |
| 147 |
]; |
| 148 |
|
| 149 |
$database_size = [ |
| 150 |
'data' => 0, |
| 151 |
'index' => 0, |
| 152 |
]; |
| 153 |
|
| 154 |
foreach ( $database_table_sizes as $table ) { |
| 155 |
$table_type = in_array( $table->name, $core_tables ) ? 'erp' : 'other'; |
| 156 |
|
| 157 |
$tables[ $table_type ][ $table->name ] = [ |
| 158 |
'data' => $table->data, |
| 159 |
'index' => $table->index, |
| 160 |
]; |
| 161 |
|
| 162 |
$database_size[ 'data' ] += $table->data; |
| 163 |
$database_size[ 'index' ] += $table->index; |
| 164 |
} |
| 165 |
|
| 166 |
// Return all database info. Described by JSON Schema. |
| 167 |
return [ |
| 168 |
'wp_erp_db_version' => get_option( 'wp_erp_db_version' ), |
| 169 |
'database_prefix' => $wpdb->prefix, |
| 170 |
'database_tables' => $tables, |
| 171 |
'database_size' => $database_size, |
| 172 |
]; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Get array of counts of objects. Orders, products, etc. |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public function get_post_type_counts() { |
| 181 |
global $wpdb; |
| 182 |
|
| 183 |
$post_type_counts = $wpdb->get_results( "SELECT post_type AS 'type', count(1) AS 'count' FROM {$wpdb->posts} GROUP BY post_type;" ); |
| 184 |
|
| 185 |
return is_array( $post_type_counts ) ? $post_type_counts : []; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get a list of plugins active on the site. |
| 190 |
* |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public function get_active_plugins() { |
| 194 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 195 |
require_once ABSPATH . 'wp-admin/includes/update.php'; |
| 196 |
|
| 197 |
if ( ! function_exists( 'get_plugin_updates' ) ) { |
| 198 |
return []; |
| 199 |
} |
| 200 |
|
| 201 |
// Get both site plugins and network plugins |
| 202 |
$active_plugins = (array) get_option( 'active_plugins', [] ); |
| 203 |
|
| 204 |
if ( is_multisite() ) { |
| 205 |
$network_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', [] ) ); |
| 206 |
$active_plugins = array_merge( $active_plugins, $network_activated_plugins ); |
| 207 |
} |
| 208 |
|
| 209 |
$active_plugins_data = []; |
| 210 |
$available_updates = get_plugin_updates(); |
| 211 |
|
| 212 |
foreach ( $active_plugins as $plugin ) { |
| 213 |
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); |
| 214 |
$dirname = dirname( $plugin ); |
| 215 |
$version_latest = ''; |
| 216 |
$slug = explode( '/', $plugin ); |
| 217 |
$slug = explode( '.', end( $slug ) ); |
| 218 |
$slug = $slug[0]; |
| 219 |
|
| 220 |
if ( isset( $available_updates[ $plugin ]->update->new_version ) ) { |
| 221 |
$version_latest = $available_updates[ $plugin ]->update->new_version; |
| 222 |
} |
| 223 |
|
| 224 |
// convert plugin data to json response format. |
| 225 |
$active_plugins_data[] = [ |
| 226 |
'plugin' => $plugin, |
| 227 |
'name' => $data['Name'], |
| 228 |
'version' => $data['Version'], |
| 229 |
'version_latest' => $version_latest, |
| 230 |
'url' => $data['PluginURI'], |
| 231 |
'author_name' => $data['AuthorName'], |
| 232 |
'author_url' => esc_url_raw( $data['AuthorURI'] ), |
| 233 |
'network_activated' => $data['Network'], |
| 234 |
]; |
| 235 |
} |
| 236 |
|
| 237 |
return $active_plugins_data; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Get info on the current active theme, info on parent theme (if presnet) |
| 242 |
* and a list of template overrides. |
| 243 |
* |
| 244 |
* @return array |
| 245 |
*/ |
| 246 |
public function get_theme_info() { |
| 247 |
$active_theme = wp_get_theme(); |
| 248 |
|
| 249 |
// Get parent theme info if this theme is a child theme, otherwise |
| 250 |
// pass empty info in the response. |
| 251 |
if ( is_child_theme() ) { |
| 252 |
$parent_theme = wp_get_theme( $active_theme->Template ); |
| 253 |
$parent_theme_info = [ |
| 254 |
'parent_name' => $parent_theme->Name, |
| 255 |
'parent_version' => $parent_theme->Version, |
| 256 |
'parent_version_latest' => \WeDevs\ERP\Status::get_latest_theme_version( $parent_theme ), |
| 257 |
'parent_author_url' => $parent_theme->{'Author URI'}, |
| 258 |
]; |
| 259 |
} else { |
| 260 |
$parent_theme_info = [ 'parent_name' => '', 'parent_version' => '', 'parent_version_latest' => '', 'parent_author_url' => '' ]; |
| 261 |
} |
| 262 |
|
| 263 |
$active_theme_info = [ |
| 264 |
'name' => $active_theme->Name, |
| 265 |
'version' => $active_theme->Version, |
| 266 |
'version_latest' => \WeDevs\ERP\Status::get_latest_theme_version( $active_theme ), |
| 267 |
'author_url' => esc_url_raw( $active_theme->{'Author URI'} ), |
| 268 |
'is_child_theme' => is_child_theme(), |
| 269 |
]; |
| 270 |
|
| 271 |
return array_merge( $active_theme_info, $parent_theme_info ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns security tips. |
| 276 |
* |
| 277 |
* @return array |
| 278 |
*/ |
| 279 |
public function get_security_info() { |
| 280 |
return [ |
| 281 |
'secure_connection' => 'https' === substr( get_home_url(), 0, 5 ), |
| 282 |
'hide_errors' => ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), |
| 283 |
]; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Get any query params needed. |
| 288 |
* |
| 289 |
* @return array |
| 290 |
*/ |
| 291 |
public function get_collection_params() { |
| 292 |
return [ |
| 293 |
'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 294 |
]; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* This method will return ERP User Role Counts |
| 299 |
* |
| 300 |
* @param string $type accepted types are: active_users, hrm_manager, crm_manager, crm_agent, accounting_manager |
| 301 |
* |
| 302 |
* @since 1.6.4 |
| 303 |
* |
| 304 |
* @return bool|int|void |
| 305 |
*/ |
| 306 |
public function get_erp_user_count( $type = 'active_users' ) { |
| 307 |
$roles = []; |
| 308 |
|
| 309 |
switch ( $type ) { |
| 310 |
case 'hrm_manager': |
| 311 |
if ( wperp()->modules->is_module_active( 'hrm' ) ) { |
| 312 |
$roles[] = erp_hr_get_manager_role(); |
| 313 |
} |
| 314 |
break; |
| 315 |
|
| 316 |
case 'crm_manager': |
| 317 |
if ( wperp()->modules->is_module_active( 'crm' ) ) { |
| 318 |
$roles[] = erp_crm_get_manager_role(); |
| 319 |
} |
| 320 |
break; |
| 321 |
|
| 322 |
case 'crm_agent': |
| 323 |
if ( wperp()->modules->is_module_active( 'crm' ) ) { |
| 324 |
$roles[] = erp_crm_get_agent_role(); |
| 325 |
} |
| 326 |
break; |
| 327 |
|
| 328 |
case 'accounting_manager': |
| 329 |
if ( wperp()->modules->is_module_active( 'accounting' ) ) { |
| 330 |
$roles[] = erp_ac_get_manager_role(); |
| 331 |
} |
| 332 |
break; |
| 333 |
|
| 334 |
case 'active_users': |
| 335 |
if ( wperp()->modules->is_module_active( 'crm' ) ) { |
| 336 |
$roles[] = erp_crm_get_manager_role(); |
| 337 |
$roles[] = erp_crm_get_agent_role(); |
| 338 |
} |
| 339 |
|
| 340 |
if ( wperp()->modules->is_module_active( 'accounting' ) ) { |
| 341 |
$roles[] = erp_ac_get_manager_role(); |
| 342 |
} |
| 343 |
|
| 344 |
if ( wperp()->modules->is_module_active( 'hrm' ) ) { |
| 345 |
$roles[] = erp_hr_get_manager_role(); |
| 346 |
$roles[] = erp_hr_get_employee_role(); |
| 347 |
} |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
if ( empty( $roles ) ) { |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
$user_count = get_users( [ |
| 356 |
'role__in' => $roles, |
| 357 |
'role__not_in' => 'administrator', |
| 358 |
'fields' => 'ID', |
| 359 |
] ); |
| 360 |
|
| 361 |
// count inactive employees |
| 362 |
if ( $type === 'active_users' && wperp()->modules->is_module_active( 'hrm' ) ) { |
| 363 |
$employees = $this->erp_hr_get_employees(); |
| 364 |
$user_count = array_diff( $user_count, $employees ); |
| 365 |
} |
| 366 |
|
| 367 |
return count( $user_count ); |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* This method will count active users of hrm |
| 372 |
* |
| 373 |
* @since 1.6.4 |
| 374 |
* |
| 375 |
* @return int |
| 376 |
*/ |
| 377 |
public function erp_hr_get_employees() { |
| 378 |
global $wpdb; |
| 379 |
|
| 380 |
$employee_tbl = $wpdb->prefix . 'erp_hr_employees'; |
| 381 |
|
| 382 |
$query = $wpdb->prepare( |
| 383 |
"select $employee_tbl.user_id from $employee_tbl |
| 384 |
left join {$wpdb->users} on $employee_tbl.user_id = {$wpdb->users}.ID |
| 385 |
where $employee_tbl.status != %s or $employee_tbl.deleted_at is not null", |
| 386 |
[ 'active' ] ); |
| 387 |
|
| 388 |
return $wpdb->get_col( $query ); |
| 389 |
} |
| 390 |
} |
| 391 |
|