-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use autoload on classes api, helpers & list-table (Related: #543)
- Loading branch information
Showing
11 changed files
with
151 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,68 @@ | ||
<?php | ||
/* | ||
* Description: File for accessing and modifying site-related data. | ||
*/ | ||
|
||
namespace Seravo; | ||
|
||
// Deny direct access to this file | ||
if ( ! defined('ABSPATH') ) { | ||
die('Access denied!'); | ||
} | ||
|
||
if ( ! class_exists('API') ) { | ||
|
||
class API { | ||
|
||
/** | ||
* Get various data from the site API for the current site. | ||
* @return \WP_Error|mixed | ||
*/ | ||
public static function get_site_data( $api_query = '', $handled_http_codes = array( 200 ) ) { | ||
$site = getenv('USER'); | ||
$ch = curl_init('http://localhost:8888/v1/site/' . $site . $api_query); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-Api-Key: ' . getenv('SERAVO_API_KEY') )); | ||
$response = curl_exec($ch); | ||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
/** | ||
* Class API | ||
* | ||
* Class for accessing and modifying site-related data. | ||
*/ | ||
class API { | ||
|
||
// Check for errors | ||
if ( curl_error($ch) || ! in_array($httpcode, $handled_http_codes) ) { | ||
error_log('SWD API (' . $api_query . ') error ' . $httpcode . ': ' . curl_error($ch)); | ||
curl_close($ch); | ||
return new \WP_Error('seravo-api-get-fail', __('API call failed. Aborting. The error has been logged.', 'seravo')); | ||
} | ||
/** | ||
* Get various data from the site API for the current site. | ||
* @return \WP_Error|mixed | ||
*/ | ||
public static function get_site_data( $api_query = '', $handled_http_codes = array( 200 ) ) { | ||
$site = getenv('USER'); | ||
$ch = curl_init('http://localhost:8888/v1/site/' . $site . $api_query); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-Api-Key: ' . getenv('SERAVO_API_KEY') )); | ||
$response = curl_exec($ch); | ||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
|
||
// Check for errors | ||
if ( curl_error($ch) || ! in_array($httpcode, $handled_http_codes) ) { | ||
error_log('SWD API (' . $api_query . ') error ' . $httpcode . ': ' . curl_error($ch)); | ||
curl_close($ch); | ||
return json_decode($response, true); | ||
return new \WP_Error('seravo-api-get-fail', __('API call failed. Aborting. The error has been logged.', 'seravo')); | ||
} | ||
|
||
/** | ||
* @return \WP_Error|bool|string | ||
*/ | ||
public static function update_site_data( $data, $api_query = '', $handled_http_codes = array( 200 ), $method = 'PUT' ) { | ||
$data_json = json_encode($data); | ||
$site = getenv('USER'); | ||
$ch = curl_init('http://localhost:8888/v1/site/' . $site . $api_query); | ||
curl_close($ch); | ||
return json_decode($response, true); | ||
} | ||
|
||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt( | ||
$ch, | ||
CURLOPT_HTTPHEADER, | ||
array( | ||
'X-Api-Key: ' . getenv('SERAVO_API_KEY'), | ||
'Content-Type: application/json', | ||
'Content-Length: ' . strlen($data_json), | ||
) | ||
); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); | ||
/** | ||
* @return \WP_Error|bool|string | ||
*/ | ||
public static function update_site_data( $data, $api_query = '', $handled_http_codes = array( 200 ), $method = 'PUT' ) { | ||
$data_json = json_encode($data); | ||
$site = getenv('USER'); | ||
$ch = curl_init('http://localhost:8888/v1/site/' . $site . $api_query); | ||
|
||
$response = curl_exec($ch); | ||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt( | ||
$ch, | ||
CURLOPT_HTTPHEADER, | ||
array( | ||
'X-Api-Key: ' . getenv('SERAVO_API_KEY'), | ||
'Content-Type: application/json', | ||
'Content-Length: ' . strlen($data_json), | ||
) | ||
); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); | ||
|
||
// Check for errors | ||
if ( curl_error($ch) || ! in_array($httpcode, $handled_http_codes) ) { | ||
error_log('SWD API (' . $api_query . ') error ' . $httpcode . ': ' . curl_error($ch)); | ||
curl_close($ch); | ||
return new \WP_Error('seravo-api-put-fail', __('API call failed. Aborting. The error has been logged.', 'seravo')); | ||
} | ||
$response = curl_exec($ch); | ||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
|
||
return $response; | ||
// Check for errors | ||
if ( curl_error($ch) || ! in_array($httpcode, $handled_http_codes) ) { | ||
error_log('SWD API (' . $api_query . ') error ' . $httpcode . ': ' . curl_error($ch)); | ||
curl_close($ch); | ||
return new \WP_Error('seravo-api-put-fail', __('API call failed. Aborting. The error has been logged.', 'seravo')); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,101 @@ | ||
<?php | ||
/* | ||
* Description: Helpers for this plugin and other modules | ||
*/ | ||
|
||
namespace Seravo; | ||
|
||
// Deny direct access to this file | ||
if ( ! defined('ABSPATH') ) { | ||
die('Access denied!'); | ||
} | ||
|
||
if ( ! class_exists('Helpers') ) { | ||
|
||
class Helpers { | ||
/** | ||
* Class Helpers | ||
* | ||
* Helpers for this plugin and other modules. | ||
*/ | ||
class Helpers { | ||
|
||
/** | ||
* Check if this if the site is running in Vagrant | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_development() { | ||
return (getenv('WP_ENV') && getenv('WP_ENV') === 'development'); | ||
} | ||
/** | ||
* Check if this if the site is running in Vagrant | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_development() { | ||
return (getenv('WP_ENV') && getenv('WP_ENV') === 'development'); | ||
} | ||
|
||
/** | ||
* Check if this is a live production site | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_production() { | ||
return (getenv('WP_ENV') && getenv('WP_ENV') === 'production'); | ||
} | ||
/** | ||
* Check if this is a live production site | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_production() { | ||
return (getenv('WP_ENV') && getenv('WP_ENV') === 'production'); | ||
} | ||
|
||
/** | ||
* Check if this is staging shadow | ||
* There shouldn't be difference between this and production, | ||
* but might be useful in the future. | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_staging() { | ||
return (getenv('WP_ENV') && getenv('WP_ENV') === 'staging'); | ||
} | ||
/** | ||
* Check if this is staging shadow | ||
* There shouldn't be difference between this and production, | ||
* but might be useful in the future. | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_staging() { | ||
return (getenv('WP_ENV') && getenv('WP_ENV') === 'staging'); | ||
} | ||
|
||
public static function seravo_plugin_version() { | ||
return get_file_data(SERAVO_PLUGIN_DIR . 'seravo-plugin.php', array( 'Version' ), 'plugin')[0]; | ||
} | ||
public static function seravo_plugin_version() { | ||
return get_file_data(SERAVO_PLUGIN_DIR . 'seravo-plugin.php', array( 'Version' ), 'plugin')[0]; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function human_file_size( $size, $precision = 2 ) { | ||
$size = (int) $size; // 'wp db size' returns value with non-numeric characters | ||
for ( $i = 0; ($size / 1024) > 0.9; ) { | ||
++$i; | ||
$size /= 1024; | ||
} | ||
return round($size, $precision) . array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' )[ $i ]; | ||
/** | ||
* @return string | ||
*/ | ||
public static function human_file_size( $size, $precision = 2 ) { | ||
$size = (int) $size; // 'wp db size' returns value with non-numeric characters | ||
for ( $i = 0; ($size / 1024) > 0.9; ) { | ||
++$i; | ||
$size /= 1024; | ||
} | ||
return round($size, $precision) . array( 'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' )[$i]; | ||
} | ||
|
||
/** | ||
* Get IP range limits from CIDR range format. Index 0 | ||
* is lower limit and index 1 upper limit. | ||
* | ||
* Example result: | ||
* [0] => 3221250250, | ||
* [1] => 3221254346, | ||
* | ||
* @param string IPv4 range in CIDR format (eg. xxx.xxx.xxx.xxx/20) | ||
* @return array Upper and lower limits in ip2long format. | ||
* @version 1.0 | ||
* @see https://gist.github.com/tott/7684443 | ||
**/ | ||
public static function cidr_to_range( $cidr ) { | ||
$cidr = explode('/', $cidr); | ||
$range = array(); | ||
$range[0] = (ip2long($cidr[0])) & ((-1 << (32 - (int) $cidr[1]))); | ||
$range[1] = $range[0] + pow(2, (32 - (int) $cidr[1])) - 1; | ||
return $range; | ||
} | ||
/** | ||
* Get IP range limits from CIDR range format. Index 0 | ||
* is lower limit and index 1 upper limit. | ||
* | ||
* Example result: | ||
* [0] => 3221250250, | ||
* [1] => 3221254346, | ||
* | ||
* @param string IPv4 range in CIDR format (eg. xxx.xxx.xxx.xxx/20) | ||
* @return array Upper and lower limits in ip2long format. | ||
* @version 1.0 | ||
* @see https://gist.github.com/tott/7684443 | ||
**/ | ||
public static function cidr_to_range( $cidr ) { | ||
$cidr = explode('/', $cidr); | ||
$range = array(); | ||
$range[0] = (ip2long($cidr[0])) & ((-1 << (32 - (int) $cidr[1]))); | ||
$range[1] = $range[0] + pow(2, (32 - (int) $cidr[1])) - 1; | ||
return $range; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public static function ip_in_range( $range, $ip ) { | ||
foreach ( $range as $limits ) { | ||
if ( $ip >= $limits[0] && $ip <= $limits[1] ) { | ||
return true; | ||
} | ||
/** | ||
* @return bool | ||
*/ | ||
public static function ip_in_range( $range, $ip ) { | ||
foreach ( $range as $limits ) { | ||
if ( $ip >= $limits[0] && $ip <= $limits[1] ) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public static function sanitize_full_path( $file ) { | ||
$path = explode('/', $file); | ||
foreach ( $path as $index => $part ) { | ||
$path[$index] = sanitize_file_name($part); | ||
} | ||
return implode('/', $path); | ||
/** | ||
* @return string | ||
*/ | ||
public static function sanitize_full_path( $file ) { | ||
$path = explode('/', $file); | ||
foreach ( $path as $index => $part ) { | ||
$path[$index] = sanitize_file_name($part); | ||
} | ||
|
||
return implode('/', $path); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.