diff --git a/inc/class-s3-media-sync.php b/inc/class-s3-media-sync.php index 74c3e1c..d23e107 100644 --- a/inc/class-s3-media-sync.php +++ b/inc/class-s3-media-sync.php @@ -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 ); @@ -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; } @@ -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; } @@ -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; + } }