Skip to content

Commit 7077112

Browse files
committed
Fix image uploads failing due to bad container injection
1 parent ed4b7c6 commit 7077112

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

includes/Images/ImageManager.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,22 @@ private function initialize_settings( Container $container ) {
3939
* @param Container $container Dependency injection container.
4040
*/
4141
private function initialize_services( Container $container ) {
42-
$this->initialize_upload_listener();
42+
$this->initialize_upload_listener( $container );
4343
$this->maybe_initialize_lazy_loader();
4444
$this->maybe_initialize_bulk_optimizer();
45-
$this->maybe_initialize_rest_api();
45+
$this->maybe_initialize_rest_api( $container );
4646
$this->maybe_initialize_marker();
4747
$this->maybe_initialize_image_rewrite_handler( $container );
4848
$this->maybe_initialize_image_limit_banner( $container );
4949
}
5050

5151
/**
5252
* Initializes the ImageUploadListener if auto-optimization is enabled.
53+
*
54+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
5355
*/
54-
private function initialize_upload_listener() {
55-
new ImageUploadListener( ImageSettings::is_auto_delete_enabled() );
56+
private function initialize_upload_listener( $container ) {
57+
new ImageUploadListener( ImageSettings::is_auto_delete_enabled(), $container );
5658
}
5759

5860
/**
@@ -75,10 +77,12 @@ private function maybe_initialize_bulk_optimizer() {
7577

7678
/**
7779
* Initializes the REST API routes if accessed via REST and user is an admin.
80+
*
81+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
7882
*/
79-
private function maybe_initialize_rest_api() {
83+
private function maybe_initialize_rest_api( $container ) {
8084
if ( Permissions::rest_is_authorized_admin() ) {
81-
new RestApi();
85+
new RestApi( $container );
8286
}
8387
}
8488

includes/Images/ImageService.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010
*/
1111
class ImageService {
1212

13+
/**
14+
* Dependency injection container.
15+
*
16+
* @var \NewfoldLabs\WP\Container\Container
17+
*/
18+
protected $container;
19+
20+
21+
/**
22+
* Constructor.
23+
*
24+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
25+
*/
26+
public function __construct( $container ) {
27+
$this->container = $container;
28+
}
29+
30+
1331
/**
1432
* Cloudflare Worker URL for image optimization.
1533
*/
@@ -100,7 +118,7 @@ public function optimize_image( $image_url, $original_file_path ) {
100118
'monthlyRequestCount' => $monthly_request_count,
101119
'maxRequestsPerMonth' => $monthly_limit,
102120
);
103-
ImageSettings::update( $settings );
121+
ImageSettings::update( $settings, $this->container );
104122
}
105123

106124
// Handle errors from the HTTP request
@@ -219,7 +237,7 @@ private function ban_site() {
219237
$settings['bulk_optimization'] = false;
220238
$settings['auto_optimized_uploaded_images']['enabled'] = false;
221239
$settings['auto_optimized_uploaded_images']['auto_delete_original_image'] = false;
222-
ImageSettings::update( $settings );
240+
ImageSettings::update( $settings, $this->container );
223241
}
224242

225243
/**
@@ -488,7 +506,7 @@ public function get_monthly_usage_limit() {
488506

489507
$settings = ImageSettings::get( false );
490508
$settings['monthly_usage'] = $body;
491-
ImageSettings::update( $settings );
509+
ImageSettings::update( $settings, $this->container );
492510

493511
return $body;
494512
}

includes/Images/ImageSettings.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,13 @@ public static function get( $call_worker = true ) {
441441
/**
442442
* Updates the image optimization settings.
443443
*
444-
* @param array $settings The new settings array.
444+
* @param array $settings The new settings array.
445+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
445446
*
446447
* @return bool true if the settings were updated successfully, false otherwise.
447448
*/
448-
public static function update( $settings ) {
449-
$instance = new self();
449+
public static function update( $settings, $container ) {
450+
$instance = new self( $container );
450451
$sanitized_settings = $instance->sanitize_settings( $settings );
451452
return update_option( self::SETTING_KEY, $sanitized_settings );
452453
}

includes/Images/ImageUploadListener.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ class ImageUploadListener {
2626
/**
2727
* Constructor to initialize the listener.
2828
*
29-
* @param bool $delete_original Whether to delete the original file after optimization.
29+
* @param bool $delete_original Whether to delete the original file after optimization.
30+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
3031
*/
31-
public function __construct( $delete_original = false ) {
32-
$this->image_service = new ImageService();
32+
public function __construct( $delete_original = false, $container ) {
33+
$this->image_service = new ImageService( $container );
3334
$this->delete_original = $delete_original;
3435
$this->register_hooks();
3536
}

includes/Images/RestApi/ImagesController.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
* Provides REST API for single media item optimization.
1111
*/
1212
class ImagesController {
13+
/**
14+
* Dependency injection container.
15+
*
16+
* @var \NewfoldLabs\WP\Container\Container
17+
*/
18+
protected $container;
1319

1420
/**
1521
* The REST route namespace.
@@ -25,6 +31,15 @@ class ImagesController {
2531
*/
2632
protected $rest_base = '/images';
2733

34+
/**
35+
* Constructor.
36+
*
37+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
38+
*/
39+
public function __construct( $container ) {
40+
$this->container = $container;
41+
}
42+
2843
/**
2944
* Registers API routes.
3045
*/
@@ -74,7 +89,7 @@ public function optimize_image( \WP_REST_Request $request ) {
7489
);
7590
}
7691

77-
$image_service = new ImageService();
92+
$image_service = new ImageService( $this->container );
7893
$delete_original = ImageSettings::is_auto_delete_enabled();
7994
$optimized_result = $image_service->optimize_image( $image_url, $file_path );
8095

includes/Images/RestApi/RestApi.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
* Instantiate controllers and register routes.
77
*/
88
final class RestApi {
9+
/**
10+
* Dependency injection container.
11+
*
12+
* @var \NewfoldLabs\WP\Container\Container
13+
*/
14+
protected $container;
915

1016
/**
1117
* List of custom REST API controllers
@@ -18,9 +24,12 @@ final class RestApi {
1824

1925

2026
/**
21-
* Setup the custom REST API
27+
* Setup the custom REST API.
28+
*
29+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
2230
*/
23-
public function __construct() {
31+
public function __construct( $container ) {
32+
$this->container = $container;
2433
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
2534
}
2635

@@ -34,7 +43,7 @@ public function register_routes() {
3443
*
3544
* @var $instance WP_REST_Controller
3645
*/
37-
$instance = new $controller();
46+
$instance = new $controller( $this->container );
3847
$instance->register_routes();
3948
}
4049
}

0 commit comments

Comments
 (0)