Skip to content

Commit

Permalink
Use autoload on classes api, helpers & list-table (Related: #543)
Browse files Browse the repository at this point in the history
  • Loading branch information
Santeri Kangas authored and Moppa5 committed Jun 18, 2021
1 parent 667db89 commit 0aeeb02
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 214 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"autoload": {
"classmap": [
"src/lib/postbox",
"src/lib/ajax"
"src/lib/ajax",
"src/lib/api.php",
"src/lib/helpers.php",
"src/lib/list-table.php"
]
},
"scripts": {
Expand Down
10 changes: 0 additions & 10 deletions seravo-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@
*
*/

/*
* Load helpers so that these functions can be used in modules
*/
require_once SERAVO_PLUGIN_SRC . 'lib/helpers.php';

/*
* Load Seravo API module
*/
require_once SERAVO_PLUGIN_SRC . 'lib/api.php';

/**
* Load Seravo postbox functionalities
*/
Expand Down
110 changes: 52 additions & 58 deletions src/lib/api.php
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;
}
}
4 changes: 0 additions & 4 deletions src/lib/domain-tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
die('Access denied!');
}

if ( ! class_exists('Seravo\WP_List_Table') ) {
require_once SERAVO_PLUGIN_SRC . 'lib/list-table.php';
}

class Seravo_Domains_List_Table extends WP_List_Table {

public function __construct() {
Expand Down
173 changes: 83 additions & 90 deletions src/lib/helpers.php
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);
}

}
}
5 changes: 0 additions & 5 deletions src/lib/list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

namespace Seravo;

// Deny direct access to this file
if ( ! defined('ABSPATH') ) {
die('Access denied!');
}

/**
* Base class for displaying a list of items in an ajaxified HTML table.
*
Expand Down
Loading

0 comments on commit 0aeeb02

Please sign in to comment.