balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

http.php (981B)


      1 <?php
      2 namespace Elementor\Core\Utils;
      3 
      4 if ( ! defined( 'ABSPATH' ) ) {
      5 	exit; // Exit if accessed directly.
      6 }
      7 
      8 class Http extends \WP_Http {
      9 	/**
     10 	 * Pass multiple urls to implements a fallback machine when one of the urls
     11 	 * is sending an error or not exists anymore.
     12 	 *
     13 	 * @param array $urls
     14 	 * @param array $args
     15 	 *
     16 	 * @return array|\WP_Error|null
     17 	 */
     18 	public function request_with_fallback( array $urls, $args = [] ) {
     19 		$response = null;
     20 
     21 		foreach ( $urls as $url ) {
     22 			$response = $this->request( $url, $args );
     23 
     24 			if ( $this->is_successful_response( $response ) ) {
     25 				return $response;
     26 			}
     27 		}
     28 
     29 		return $response;
     30 	}
     31 
     32 	/**
     33 	 * @param $response
     34 	 *
     35 	 * @return bool
     36 	 */
     37 	private function is_successful_response( $response ) {
     38 		if ( is_wp_error( $response ) ) {
     39 			return false;
     40 		}
     41 
     42 		$response_code = (int) wp_remote_retrieve_response_code( $response );
     43 
     44 		if ( in_array( $response_code, [ 0, 404, 500 ], true ) ) {
     45 			return false;
     46 		}
     47 
     48 		return true;
     49 	}
     50 }