| 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 |
|
| 19 |
if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) { |
| 20 |
// WPDA::wpda_log_wp_error( $response ); |
| 21 |
} |
| 22 |
|
| 23 |
if ( is_wp_error( $response ) ) { |
| 24 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 25 |
if ( $die ) { |
| 26 |
wp_die( 'ERROR: Remote call failed [' . json_encode( $response ) . ']' ); |
| 27 |
} |
| 28 |
|
| 29 |
return $response->get_error_message(); |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! isset( $response['response'], $response['body'] ) ) { |
| 33 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 34 |
if ( $die ) { |
| 35 |
wp_die( 'ERROR: Remote call failed [missing response|body]' ); |
| 36 |
} |
| 37 |
|
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
return $response; |
| 42 |
} |
| 43 |
|
| 44 |
public static function get( $url, $args = array(), $die = false ) { |
| 45 |
$response = wp_remote_get( $url, $args ); |
| 46 |
// var_dump($response); |
| 47 |
|
| 48 |
if ( is_wp_error( $response ) ) { |
| 49 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 50 |
if ( $die ) { |
| 51 |
wp_die( 'ERROR: Remote call failed [' . json_encode( $response ) . ']' ); |
| 52 |
} |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! isset( $response['response'], $response['body'] ) ) { |
| 58 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 59 |
if ( $die ) { |
| 60 |
wp_die( 'ERROR: Remote call failed [missing response|body]' ); |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
return $response; |
| 67 |
} |
| 68 |
|
| 69 |
public static function max_size() { |
| 70 |
$max = ini_get('post_max_size'); |
| 71 |
$unit = $max[ strlen( $max ) - 1 ]; |
| 72 |
$max = substr( $max, 0, strlen( $max ) - 1 ); |
| 73 |
|
| 74 |
switch($unit) { |
| 75 |
case 'G': |
| 76 |
$max *= 1024; |
| 77 |
case 'M': |
| 78 |
$max *= 1024; |
| 79 |
case 'K': |
| 80 |
$max *= 1024; |
| 81 |
} |
| 82 |
|
| 83 |
return $max; |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
} |