| 1 |
<?php |
| 2 |
namespace Templately\Utils; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use WP_REST_Response; |
| 6 |
use function get_plugins; |
| 7 |
use function is_plugin_active; |
| 8 |
/** |
| 9 |
* Utility Helper for Templately |
| 10 |
* |
| 11 |
* This class contains some helper functions for easy access. |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
*/ |
| 15 |
class Helper extends Base { |
| 16 |
/** |
| 17 |
* Get installed WordPress Plugin List |
| 18 |
* @return array |
| 19 |
*/ |
| 20 |
public static function get_plugins(){ |
| 21 |
if( ! function_exists( 'get_plugins' ) ) { |
| 22 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 23 |
} |
| 24 |
return get_plugins(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get installed WordPress Plugin List |
| 29 |
* @return boolean |
| 30 |
*/ |
| 31 |
public static function is_plugin_active( $plugin ){ |
| 32 |
if( ! function_exists( 'is_plugin_active' ) ) { |
| 33 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 34 |
} |
| 35 |
return is_plugin_active( $plugin ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Collect IP from request. |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public static function get_ip() { |
| 44 |
$ip = '127.0.0.1'; // Local IP |
| 45 |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 46 |
$ip = $_SERVER['HTTP_CLIENT_IP']; |
| 47 |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 48 |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 49 |
} else { |
| 50 |
$ip = ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : $ip; |
| 51 |
} |
| 52 |
|
| 53 |
return sanitize_text_field( $ip ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get views for front-end display |
| 58 |
* |
| 59 |
* @param string $name it will be file name only from the view's folder. |
| 60 |
* @param array $data |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public static function views( $name, $data = [] ){ |
| 64 |
extract( $data ); |
| 65 |
$helper = self::class; |
| 66 |
$file = TEMPLATELY_PATH . 'views/' . $name . '.php'; |
| 67 |
|
| 68 |
if( is_readable( $file ) ) { |
| 69 |
include_once $file; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Sanitize Helper |
| 75 |
* |
| 76 |
* @param mixed $value |
| 77 |
* @param string $type |
| 78 |
* |
| 79 |
* @return bool|string |
| 80 |
*/ |
| 81 |
public static function sanitize( $value, $type = 'text' ){ |
| 82 |
switch ( $type ) { |
| 83 |
case 'boolean': |
| 84 |
$sanitized_value = rest_sanitize_boolean( $value ); |
| 85 |
break; |
| 86 |
default: |
| 87 |
$sanitized_value = sanitize_text_field( $value ); |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
return $sanitized_value; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* API Error Formatter |
| 96 |
* |
| 97 |
* @param int $error_code |
| 98 |
* @param mixed $error_message |
| 99 |
* @param string $endpoint |
| 100 |
* @param integer $status |
| 101 |
* @param array $additional_data |
| 102 |
* @return WP_Error |
| 103 |
*/ |
| 104 |
public static function error( $error_code, $error_message, $endpoint = '', $status = 500, $additional_data = [] ) { |
| 105 |
$additional_data['status'] = $status; |
| 106 |
if ( ! empty( $endpoint ) ) { |
| 107 |
$additional_data['endpoint'] = $endpoint; |
| 108 |
} |
| 109 |
|
| 110 |
return new WP_Error( $error_code, $error_message, $additional_data ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* API Response Formatter |
| 115 |
* |
| 116 |
* @param mixed $data |
| 117 |
* @return WP_REST_Response |
| 118 |
*/ |
| 119 |
public static function success( $data ) { |
| 120 |
return new WP_REST_Response( $data, 200 ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Normalize Favourites Data |
| 125 |
* |
| 126 |
* @param array $favourites |
| 127 |
* @param array $_favourites |
| 128 |
* @param boolean $undo |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public function normalizeFavourites( $favourites, $_favourites = [], $undo = false ){ |
| 133 |
if( $undo ) { |
| 134 |
$_favourites[ $favourites['type'] ] = array_values(array_filter( $_favourites[ $favourites['type'] ], function( $item ) use( $favourites ) { |
| 135 |
return $item != $favourites['id']; |
| 136 |
} )); |
| 137 |
return $_favourites; |
| 138 |
} |
| 139 |
|
| 140 |
array_map( function( $item ) use ( &$_favourites) { |
| 141 |
if( ! is_null( $item ) ) { |
| 142 |
$item = (array) $item; |
| 143 |
if ( isset( $_favourites[ $item['type'] ] ) ){ |
| 144 |
$_favourites[ $item['type'] ][] = $item['id']; |
| 145 |
} else { |
| 146 |
$_favourites[ $item['type'] ] = [ $item['id'] ]; |
| 147 |
} |
| 148 |
} |
| 149 |
return $_favourites; |
| 150 |
}, $favourites ); |
| 151 |
|
| 152 |
return $_favourites; |
| 153 |
} |
| 154 |
|
| 155 |
public function normalizeReviews( $favourites, $_favourites = [], $undo = false ){ |
| 156 |
array_map( function( $item ) use ( &$_favourites) { |
| 157 |
if( ! is_null( $item ) ) { |
| 158 |
$item = (array) $item; |
| 159 |
if ( !isset( $_favourites[ $item['type'] ] ) ){ |
| 160 |
$_favourites[ $item['type'] ] = []; |
| 161 |
} |
| 162 |
$_favourites[ $item['type'] ][$item['type_id']] = $item['rating']; |
| 163 |
} |
| 164 |
return $_favourites; |
| 165 |
}, $favourites ); |
| 166 |
|
| 167 |
return $_favourites; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Trigger Error |
| 172 |
* |
| 173 |
* @param object $triggered_by |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public static function trigger_error( $triggered_by, $method = 'get_instance' ){ |
| 177 |
$class = get_class( $triggered_by ); |
| 178 |
$trace = debug_backtrace(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection |
| 179 |
$file = $trace[0]['file']; |
| 180 |
$line = $trace[0]['line']; |
| 181 |
trigger_error("Call to undefined method $class::$method() in $file on line $line", E_USER_ERROR); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Printing Error Logs in debug.log file. |
| 186 |
* |
| 187 |
* @param mixed $log |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public static function log( $log ){ |
| 191 |
if ( defined('WP_DEBUG_LOG') && WP_DEBUG_LOG === true ) { |
| 192 |
if ( is_array( $log ) || is_object( $log ) ) { |
| 193 |
error_log( print_r( $log, true ) ); |
| 194 |
} else { |
| 195 |
error_log( $log ?: '' ); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |