'attachment', ), $file_data ); // Standard Offloaded Media Library Item. $as3cf_item = Media_Library_Item::get_by_source_id( $post_id ); if ( $as3cf_item && ! is_wp_error( $as3cf_item ) ) { return $as3cf_item->get_provider_url( null, $expires, $headers ); } // Official EDD S3 addon upload - path should not start with '/', 'http', 'https' or 'ftp' or contain AWSAccessKeyId $url = parse_url( $file_name ); if ( ( '/' !== $file_name[0] && false === isset( $url['scheme'] ) ) || false !== ( strpos( $file_name, 'AWSAccessKeyId' ) ) ) { $bucket = ( isset( $edd_options['edd_amazon_s3_bucket'] ) ) ? trim( $edd_options['edd_amazon_s3_bucket'] ) : $this->as3cf->get_setting( 'bucket' ); $expires = time() + $expires; return $this->as3cf->get_provider_client()->get_object_url( $bucket, $file_name, $expires, $headers ); } // None S3 upload return $file; } /** * Apply ACL to files uploaded outside of EDD on save of EDD download files * * @param array $files * * @return mixed */ public function make_edd_files_private_on_provider( $files ) { global $post; // get existing files attached to download $old_files = edd_get_download_files( $post->ID ); $old_attachment_ids = wp_list_pluck( $old_files, 'attachment_id' ); $new_attachment_ids = array(); /** @var Update_Acl_Handler $acl_handler */ $acl_handler = $this->as3cf->get_item_handler( Update_Acl_Handler::get_item_handler_key_name() ); if ( is_array( $files ) ) { foreach ( $files as $file ) { $new_attachment_ids[] = $file['attachment_id']; $as3cf_item = Media_Library_Item::get_by_source_id( $file['attachment_id'] ); if ( ! $as3cf_item ) { // not offloaded, ignore. continue; } if ( $as3cf_item->is_private() ) { // already private continue; } if ( $this->as3cf->is_pro_plugin_setup( true ) ) { $options = array( 'object_keys' => array( null ), 'set_private' => true, ); $result = $acl_handler->handle( $as3cf_item, $options ); if ( true === $result ) { $this->as3cf->make_acl_admin_notice( $as3cf_item ); } } } } // determine which attachments have been removed and maybe set to public $removed_attachment_ids = array_diff( $old_attachment_ids, $new_attachment_ids ); $this->maybe_make_removed_edd_files_public( $removed_attachment_ids, $post->ID ); return $files; } /** * Remove public ACL from attachments removed from a download * as long as they are not attached to any other downloads * * @param array $attachment_ids * @param integer $download_id */ function maybe_make_removed_edd_files_public( $attachment_ids, $download_id ) { global $wpdb; /** @var Update_Acl_Handler $acl_handler */ $acl_handler = $this->as3cf->get_item_handler( Update_Acl_Handler::get_item_handler_key_name() ); foreach ( $attachment_ids as $id ) { $as3cf_item = Media_Library_Item::get_by_source_id( $id ); if ( ! $as3cf_item ) { // Not offloaded, ignore. continue; } if ( ! $as3cf_item->is_private() ) { // already public continue; } $length = strlen( $id ); // check the attachment isn't used by other downloads $sql = " SELECT COUNT(*) FROM `{$wpdb->prefix}postmeta` WHERE `{$wpdb->prefix}postmeta`.`meta_key` = 'edd_download_files' AND `{$wpdb->prefix}postmeta`.`post_id` != {$download_id} AND `{$wpdb->prefix}postmeta`.`meta_value` LIKE '%s:13:\"attachment_id\";s:{$length}:\"{$id}\"%' "; if ( $wpdb->get_var( $sql ) > 0 ) { // used for another download, ignore continue; } // set acl to public $options = array( 'object_keys' => array( null ), 'set_private' => false, ); $result = $acl_handler->handle( $as3cf_item, $options ); if ( true === $result ) { $this->as3cf->make_acl_admin_notice( $as3cf_item ); } } } }