Skip to content

Commit

Permalink
Validate settings before adding media sync hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlanglands committed Feb 11, 2025
1 parent 97616c3 commit 2c756dc
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions inc/class-s3-media-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function setup() {
add_action( 'admin_menu', [ $this, 'register_menu_settings' ] );
add_action( 'admin_init', [ $this, 'settings_screeen_init' ] );

if ( ! empty( $this->settings ) ) {
if ( $this->has_required_settings() ) {
// Perform on-the-fly media syncs by hooking into these actions
add_filter( 'wp_handle_upload', [ $this, 'add_attachment_to_s3' ], 10, 2 );
add_action( 'delete_attachment', [ $this, 'delete_attachment_from_s3' ], 10, 1 );
Expand Down Expand Up @@ -113,12 +113,7 @@ public function add_updated_attachment_to_s3( $override, $filename, $image, $mim
*/
public function register_stream_wrapper() {
// Only proceed to register the stream wrapper if all required fields are set
if (
empty( $this->settings['bucket'] ) ||
empty( $this->settings['key'] ) ||
empty( $this->settings['secret'] ) ||
empty( $this->settings['region'] )
) {
if ( ! $this->has_required_settings() ) {
return;
}

Expand Down Expand Up @@ -148,12 +143,7 @@ public function register_menu_settings() {
*/
function s3_media_sync_settings_validation( $input ) {
// Only proceed to validate the bucket if all necessary settings are set
if (
empty( $this->settings['bucket'] ) ||
empty( $this->settings['key'] ) ||
empty( $this->settings['secret'] ) ||
empty( $this->settings['region'] )
) {
if ( ! $this->has_required_settings() ) {
return $input;
}

Expand Down Expand Up @@ -328,4 +318,21 @@ public function s3() {

return $this->s3;
}

/**
* Check if all required s3 bucket settings are set.
*
* @return bool
*/
private function has_required_settings() {
$required_keys = [ 'bucket', 'key', 'secret', 'region' ];

foreach ( $required_keys as $key ) {
if ( empty( $this->settings[ $key ] ) ) {
return false;
}
}

return true;
}
}

0 comments on commit 2c756dc

Please sign in to comment.