Еще скажите будет работать такой вариант ?.
// Run this function on every image upload
add_action('wp_handle_upload', 'process_images');
function process_images($results) {
if( $results['type'] === 'image/jpeg' || $results['type'] === 'image/png' || $results['type'] === 'image/gif' ) {
$imgfilename = $results[ 'file' ];
$imgurl = $results[ 'url' ];
if ( ! is_wp_error( $theme_image ) ) {
$imageName = wp_basename($imgurl);
// Save image to cloud storage
saveToStorage($imgurl, $imageName, $results['type']);
}
return $results;
}
}
function saveToStorage($url, $imageName, $type){
$objects = callGoogleClient();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$imgData = curl_exec($ch);
$postbody = array('mimeType' => $type, "data" => $imgData);
$gso = new Google_StorageObject();
$gso->setName(A_CLOUD_FOLDER.'/'.$imageName);
$resp = $objects->insert(MY_BUCKET, $gso, $postbody);
}
// Delete an image from cloud storage
add_filter('wp_delete_file', 'on_remove_file');
function on_remove_file($file) {
$objects = callGoogleClient();
$name = wp_basename($file);
$resp = $objects->delete(MY_BUCKET, A_CLOUD_FOLDER.'/'. $name);
return $file;
}
function callGoogleClient(){
$client = new Google_Client();
$client->setClientId('your_id.apps.googleusercontent.com');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, KEY_FILE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$key = curl_exec($ch);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/devstorage.full_control'),
$key)
);
$StorageService = new Google_StorageService($client);
$objects = $StorageService->objects;
return $objects;
}