Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add strict check to media sync hooks #34

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}