Created
March 8, 2019 12:52
-
-
Save uprise10/7303ab58cab08d14fc78dd8d494169e4 to your computer and use it in GitHub Desktop.
When using Gravity Forms and the Microsoft Azure Storage plugin, uploaded files via a form are not automatically uploaded to Azure. This piece of code fixes that.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Radish_GF_Azure_HandleUploads { | |
private static $instance; | |
public function __construct() { | |
$this->run(); | |
} | |
public function run() { | |
add_action( 'gform_after_submission', [ $this, 'process_uploads_to_azure' ], 10 ); | |
} | |
public function process_uploads_to_azure( $entry ) { | |
if( ! class_exists( 'Windows_Azure_Helper' ) ) { | |
return false; | |
} | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
$upload_dir = wp_upload_dir(); | |
// Determine file. | |
$file_url = $entry[8]; | |
$file_url = json_decode( $file_url )[0]; | |
// Generate correct paths. | |
$url_parts = parse_url( $file_url ); | |
$file_path = str_replace( '/media/', '', $url_parts['path'] ); | |
$full_path = $upload_dir['basedir'] . '/' . $file_path; | |
// Upload the file to Azure. | |
\Windows_Azure_Helper::put_uploaded_file_to_blob_storage( 'media', $file_path, $full_path ); | |
return true; | |
} | |
/** | |
* Returns an instance of this class. An implementation of the singleton design pattern. | |
* | |
* @return object A reference to an instance of this class. | |
* @since 1.0.0 | |
*/ | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new HandleUploads(); | |
} | |
return self::$instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment