| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Utilities { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Remote_Call { |
| 8 |
|
| 9 |
public static function post( $url, $body, $die = false, $headers = array() ) { |
| 10 |
$response = wp_remote_post( |
| 11 |
$url, |
| 12 |
array( |
| 13 |
'headers' => $headers, |
| 14 |
'body' => $body, |
| 15 |
'timeout' => 60, |
| 16 |
) |
| 17 |
); |
| 18 |
// var_dump($response); |
| 19 |
|
| 20 |
if ( is_wp_error( $response ) ) { |
| 21 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 22 |
if ( $die ) { |
| 23 |
wp_die( 'ERROR: Remote call failed [' . json_encode( $response ) . ']' ); |
| 24 |
} |
| 25 |
|
| 26 |
return $response->get_error_message(); |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! isset( $response['response'], $response['body'] ) ) { |
| 30 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 31 |
if ( $die ) { |
| 32 |
wp_die( 'ERROR: Remote call failed [missing response|body]' ); |
| 33 |
} |
| 34 |
|
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
return $response; |
| 39 |
} |
| 40 |
|
| 41 |
public static function get( $url, $args = array(), $die = false ) { |
| 42 |
$response = wp_remote_get( $url, $args ); |
| 43 |
// var_dump($response); |
| 44 |
|
| 45 |
if ( is_wp_error( $response ) ) { |
| 46 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 47 |
if ( $die ) { |
| 48 |
wp_die( 'ERROR: Remote call failed [' . json_encode( $response ) . ']' ); |
| 49 |
} |
| 50 |
|
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! isset( $response['response'], $response['body'] ) ) { |
| 55 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 56 |
if ( $die ) { |
| 57 |
wp_die( 'ERROR: Remote call failed [missing response|body]' ); |
| 58 |
} |
| 59 |
|
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
return $response; |
| 64 |
} |
| 65 |
|
| 66 |
public static function max_size() { |
| 67 |
$max = ini_get('post_max_size'); |
| 68 |
$unit = $max[ strlen( $max ) - 1 ]; |
| 69 |
$max = substr( $max, 0, strlen( $max ) - 1 ); |
| 70 |
|
| 71 |
switch($unit) { |
| 72 |
case 'G': |
| 73 |
$max *= 1024; |
| 74 |
case 'M': |
| 75 |
$max *= 1024; |
| 76 |
case 'K': |
| 77 |
$max *= 1024; |
| 78 |
} |
| 79 |
|
| 80 |
return $max; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
} |