balmet.com

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

Downloader.php (3797B)


      1 <?php
      2 /**
      3  * Class for downloading a file from a given URL.
      4  *
      5  * @package ocdi
      6  */
      7 
      8 namespace OCDI;
      9 
     10 class Downloader {
     11 	/**
     12 	 * Holds full path to where the files will be saved.
     13 	 *
     14 	 * @var string
     15 	 */
     16 	private $download_directory_path = '';
     17 
     18 	/**
     19 	 * Constructor method.
     20 	 *
     21 	 * @param string $download_directory_path Full path to where the files will be saved.
     22 	 */
     23 	public function __construct( $download_directory_path = '' ) {
     24 		$this->set_download_directory_path( $download_directory_path );
     25 	}
     26 
     27 
     28 	/**
     29 	 * Download file from a given URL.
     30 	 *
     31 	 * @param string $url URL of file to download.
     32 	 * @param string $filename Filename of the file to save.
     33 	 * @return string|WP_Error Full path to the downloaded file or WP_Error object with error message.
     34 	 */
     35 	public function download_file( $url, $filename ) {
     36 		$content = $this->get_content_from_url( $url );
     37 
     38 		// Check if there was an error and break out.
     39 		if ( is_wp_error( $content ) ) {
     40 			return $content;
     41 		}
     42 
     43 		return Helpers::write_to_file( $content, $this->download_directory_path . $filename );
     44 	}
     45 
     46 
     47 	/**
     48 	 * Helper function: get content from an URL.
     49 	 *
     50 	 * @param string $url URL to the content file.
     51 	 * @return string|WP_Error, content from the URL or WP_Error object with error message.
     52 	 */
     53 	private function get_content_from_url( $url ) {
     54 		// Test if the URL to the file is defined.
     55 		if ( empty( $url ) ) {
     56 			return new \WP_Error(
     57 				'missing_url',
     58 				__( 'Missing URL for downloading a file!', 'pt-ocdi' )
     59 			);
     60 		}
     61 
     62 		// Get file content from the server.
     63 		$response = wp_remote_get(
     64 			$url,
     65 			array( 'timeout' => apply_filters( 'pt-ocdi/timeout_for_downloading_import_file', 20 ) )
     66 		);
     67 
     68 		// Test if the get request was not successful.
     69 		if ( is_wp_error( $response ) || 200 !== $response['response']['code'] ) {
     70 			// Collect the right format of error data (array or WP_Error).
     71 			$response_error = $this->get_error_from_response( $response );
     72 
     73 			return new \WP_Error(
     74 				'download_error',
     75 				sprintf(
     76 					__( 'An error occurred while fetching file from: %1$s%2$s%3$s!%4$sReason: %5$s - %6$s.', 'pt-ocdi' ),
     77 					'<strong>',
     78 					$url,
     79 					'</strong>',
     80 					'<br>',
     81 					$response_error['error_code'],
     82 					$response_error['error_message']
     83 				) . '<br>' .
     84 				apply_filters( 'pt-ocdi/message_after_file_fetching_error', '' )
     85 			);
     86 		}
     87 
     88 		// Return content retrieved from the URL.
     89 		return wp_remote_retrieve_body( $response );
     90 	}
     91 
     92 
     93 	/**
     94 	 * Helper function: get the right format of response errors.
     95 	 *
     96 	 * @param array|WP_Error $response Array or WP_Error or the response.
     97 	 * @return array Error code and error message.
     98 	 */
     99 	private function get_error_from_response( $response ) {
    100 		$response_error = array();
    101 
    102 		if ( is_array( $response ) ) {
    103 			$response_error['error_code']    = $response['response']['code'];
    104 			$response_error['error_message'] = $response['response']['message'];
    105 		}
    106 		else {
    107 			$response_error['error_code']    = $response->get_error_code();
    108 			$response_error['error_message'] = $response->get_error_message();
    109 		}
    110 
    111 		return $response_error;
    112 	}
    113 
    114 
    115 	/**
    116 	 * Get download_directory_path attribute.
    117 	 */
    118 	public function get_download_directory_path() {
    119 		return $this->download_directory_path;
    120 	}
    121 
    122 
    123 	/**
    124 	 * Set download_directory_path attribute.
    125 	 * If no valid path is specified, the default WP upload directory will be used.
    126 	 *
    127 	 * @param string $download_directory_path Path, where the files will be saved.
    128 	 */
    129 	public function set_download_directory_path( $download_directory_path ) {
    130 		if ( file_exists( $download_directory_path ) ) {
    131 			$this->download_directory_path = $download_directory_path;
    132 		}
    133 		else {
    134 			$upload_dir = wp_upload_dir();
    135 			$this->download_directory_path = apply_filters( 'pt-ocdi/upload_file_path', trailingslashit( $upload_dir['path'] ) );
    136 		}
    137 	}
    138 }