get_option('acf_s3_region'), 'acf_s3_bucket' => get_option('acf_s3_bucket'), 'acf_s3_key' => get_option('acf_s3_key'), 'acf_s3_secret' => get_option('acf_s3_secret'), ); $client = acf_s3_get_client($config_check); try { $client->GetBucketLocation([ 'Bucket' => get_option('acf_s3_bucket') ]); $msg_key = 'Update successful...'; } catch (S3Exception $e) { $msg_key = 'Plz check again config s3, configuration incorrect...'; } } catch (Aws\S3\Exception\S3Exception $e) { $msg_key = 'Plz check again field config s3, configuration incorrect...'; } } ?>

ACF S3 Config

'.$msg_key.'

'; } ?>
'US East (Ohio)', 'us-east-1' => 'US East (N. Virginia)', 'us-west-1' => 'US West (N. California)', 'us-west-2' => 'US West (Oregon)', 'af-south-1' => 'Africa (Cape Town)', 'ap-east-1' => 'Asia Pacific (Hong Kong)', 'ap-south-1' => 'Asia Pacific (Mumbai)', 'ap-northeast-3' => 'Asia Pacific (Osaka)', 'ap-northeast-2' => 'Asia Pacific (Seoul)', 'ap-southeast-1' => 'Asia Pacific (Singapore)', 'ap-southeast-2' => 'Asia Pacific (Sydney)', 'ap-northeast-1' => 'Asia Pacific (Tokyo)', 'ca-central-1' => 'Canada (Central)', 'cn-north-1' => 'China (Beijing)', 'cn-northwest-1' => 'China (Ningxia)', 'eu-central-1' => 'Europe (Frankfurt)', 'eu-west-1' => 'Europe (Ireland)', 'eu-west-2' => 'Europe (London)', 'eu-south-1' => 'Europe (Milan)', 'eu-west-3' => 'Europe (Paris)', 'eu-north-1' => 'Europe (Stockholm)', 'me-south-1' => 'Middle East (Bahrain)', 'sa-east-1' => 'South America (São Paulo)', ]; ?> Setting the AWS Region

Are you already a customer?

Let's connect! We woud love to help you

Rate Our Plugin

Did ACF S3 Media Files help you out? Please leave a 5-star review. Thank you!

Write a review

Customization Service

We are a European Company. To hire our agency to help you with this plugin installation or any other customization or requirements please contact us through our site contact form or email gdpr@codemenschen.at directly.

Hire Us Now
'', 'acf_s3_bucket' => '', 'acf_s3_key' => '', 'acf_s3_secret' => '', ]; /** * @param string[] $config * @return S3Client */ function acf_s3_get_client(array $config) { return new S3Client([ 'credentials' => [ 'key' => $config['acf_s3_key'], 'secret' => $config['acf_s3_secret'], ], 'region' => $config['acf_s3_region'], 'version' => 'latest', ]); } /** * @return string[] */ function acf_s3_get_config(): array { /* @var array|null $config */ static $config = null; if ($config === null) { $config = []; foreach (ACF_S3_OPTIONS as $key => $name) { // if(empty(get_option($key))) { // update_option( $key, $name); // } $config[$key] = get_option($key); } } return $config; } /** * @param string $fieldKey * @param int|false $postId defaulted to false to preserve backwards compatibility with ACF get_field() * @return S3Item[] */ function acf_s3_get_field(string $fieldKey, $postId = false) { $names = get_field($fieldKey, $postId, false); $conf = acf_s3_get_config(); if (!is_array($names)) { $names = []; } return array_map(function ($n) use ($conf) { return new S3Item($conf['acf_s3_bucket'], $n); }, $names); } /** * Scans a location in S3 and updates the linked files in a post * * @param string $fieldKey acf field key * @param int $postId post id to link to * @param string $baseKey base key to scan in s3 * @return string[] keys to the linked files */ function acf_s3_relink(string $fieldKey, int $postId, string $baseKey): array { $config = acf_s3_get_config(); $s3 = acf_s3_get_client($config); // make sure the key only ends with a slash if we're not at the root $baseKey = ltrim(trim($baseKey, '/') . '/', '/'); $data = $s3->listObjects([ 'Bucket' => $config['acf_s3_bucket'], 'Prefix' => $baseKey, ])->toArray(); $contents = isset($data['Contents']) ? $data['Contents'] : []; // if directories have been created manually on S3, empty "ghost files" will // appear with the same key as the base key. Remove them. $contents = array_filter($contents, function ($it) use ($baseKey) { return $it['Key'] !== $baseKey; }); // if elements have been removed by the filter there might be holes in the array. // this can cause json_encode to return an object instead of an array. $contents = array_values($contents); $items = array_map(function ($it) { return $it['Key']; }, $contents); $check = update_field($fieldKey, $items, $postId); return $items; } /** * @return mixed */ function acf_s3_getJsonBody() { $data = file_get_contents('php://input'); return json_decode($data, true); } // v5 add_action('acf/include_fields', function () { $config = acf_s3_get_config(); new S3Field($config['acf_s3_bucket']); }); add_action('wp_ajax_acf-s3_media_files_action', function () { $config = acf_s3_get_config(); $client = acf_s3_get_client($config); $action = isset($_GET['command']) ? sanitize_text_field($_GET['command']) : ''; $proxy = new S3Proxy($client, $config['acf_s3_bucket']); // var_dump($proxy); // var_dump($client);exit; $body = acf_s3_getJsonBody(); $out = []; switch ($action) { case 'createMultipartUpload': $out = $proxy->createMultipartUpload($body['Key'], $body['ContentType']); break; case 'abortMultipartUpload': $out = $proxy->abortMultipartUpload($body['Key'], $body['UploadId']); break; case 'completeMultipartUpload': $out = $proxy->completeMultipartUpload($body['Key'], $body['Parts'], $body['UploadId']); break; case 'listMultipartUploads': $out = $proxy->listMultipartUploads(); break; case 'signUploadPart': $out = $proxy->signUploadPart($body['Key'], $body['PartNumber'], $body['UploadId']); break; case 'deleteObject': $out = $proxy->deleteObject($body['Key']); break; default: throw new Exception('No matching action found'); } echo json_encode($out); die(); }); add_action('wp_ajax_acf-s3_update_field', function () { $body = acf_s3_getJsonBody(); $key = sanitize_text_field($body['key']); $value = $body['value']; $postId = sanitize_text_field($body['post_id']); update_field($key, $value, $postId); die(); }); add_action('wp_ajax_acf-s3_relink', function () { $body = acf_s3_getJsonBody(); $key = sanitize_text_field($body['key']); $postId = sanitize_text_field($body['post_id']); $path = sanitize_text_field($body['base_key']); $items = acf_s3_relink($key, $postId, $path); echo json_encode($items); die(); }); /** * @param array $args * @return void */ function acf_s3_create_input(array $args): void { echo ''; } add_action('admin_init', function () { $group = 'acf_s3_media_files'; $fields = ACF_S3_OPTIONS; // remove api token when deactivating plugin register_deactivation_hook(__FILE__, function () use ($fields) { foreach ($fields as $key => $name) { delete_option($key); } }); add_settings_section($group, 'ACF S3 Content', '', 'general'); foreach ($fields as $key => $name) { add_settings_field($key, $name, 'acf_s3_create_input', 'general', $group, [ 'name' => $key, 'value' => get_option($key) ]); register_setting('general', $key); } });