Skip to content

Commit 7c10411

Browse files
authored
Merge pull request #267 from newfold-labs/fix/bad-image-upload
Fix image uploads failing due to bad container injection
2 parents 50ac2a8 + 8e9f364 commit 7c10411

File tree

11 files changed

+87
-30
lines changed

11 files changed

+87
-30
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '01a17e472960d5851af6');
1+
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '4b455cd2f16b1fc7ec6f');

build/performance/performance.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
*/
1111
class ImageService {
1212

13+
/**
14+
* Dependency injection container.
15+
*
16+
* @var \NewfoldLabs\WP\Container\Container
17+
*/
18+
protected $container;
19+
20+
/**
21+
* Constructor.
22+
*
23+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
24+
*/
25+
public function __construct( $container ) {
26+
$this->container = $container;
27+
}
28+
1329
/**
1430
* Cloudflare Worker URL for image optimization.
1531
*/
@@ -95,12 +111,12 @@ public function optimize_image( $image_url, $original_file_path ) {
95111
$monthly_request_count = ( '' !== $monthly_request_count ) ? intval( $monthly_request_count ) : null;
96112
$monthly_limit = ( '' !== $monthly_limit ) ? intval( $monthly_limit ) : null;
97113
if ( null !== $monthly_request_count && null !== $monthly_limit ) {
98-
$settings = ImageSettings::get();
114+
$settings = ImageSettings::get( $this->container, true );
99115
$settings['monthly_usage'] = array(
100116
'monthlyRequestCount' => $monthly_request_count,
101117
'maxRequestsPerMonth' => $monthly_limit,
102118
);
103-
ImageSettings::update( $settings );
119+
ImageSettings::update( $settings, $this->container );
104120
}
105121

106122
// Handle errors from the HTTP request
@@ -214,12 +230,12 @@ public function optimize_image( $image_url, $original_file_path ) {
214230
* Permanently ban the site from accessing image optimization.
215231
*/
216232
private function ban_site() {
217-
$settings = ImageSettings::get();
233+
$settings = ImageSettings::get( $this->container, true );
218234
$settings['banned_status'] = true;
219235
$settings['bulk_optimization'] = false;
220236
$settings['auto_optimized_uploaded_images']['enabled'] = false;
221237
$settings['auto_optimized_uploaded_images']['auto_delete_original_image'] = false;
222-
ImageSettings::update( $settings );
238+
ImageSettings::update( $settings, $this->container );
223239
}
224240

225241
/**
@@ -486,9 +502,9 @@ public function get_monthly_usage_limit() {
486502
);
487503
}
488504

489-
$settings = ImageSettings::get( false );
505+
$settings = ImageSettings::get( $this->container, false );
490506
$settings['monthly_usage'] = $body;
491-
ImageSettings::update( $settings );
507+
ImageSettings::update( $settings, $this->container );
492508

493509
return $body;
494510
}

includes/Images/ImageSettings.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,12 @@ public static function get_monthly_usage() {
407407
/**
408408
* Retrieves the image optimization settings.
409409
*
410-
* @param bool $call_worker Whether to fetch the latest monthly usage from the worker. Default is true.
410+
* @param \NewfoldLabs\WP\Container\Container|null $container Dependency injection container (optional).
411+
* @param bool $call_worker Whether to fetch the latest monthly usage from the worker. Default is true.
411412
*
412413
* @return array The current image optimization settings, including monthly usage and banned status.
413414
*/
414-
public static function get( $call_worker = true ) {
415+
public static function get( $container, $call_worker = true ) {
415416
$settings = get_option( self::SETTING_KEY, array() );
416417

417418
if ( ! is_array( $settings ) ) {
@@ -423,7 +424,7 @@ public static function get( $call_worker = true ) {
423424
}
424425

425426
if ( $call_worker && ( empty( $settings['monthly_usage'] ) || ! is_array( $settings['monthly_usage'] ) ) ) {
426-
$usage_data = ( new ImageService() )->get_monthly_usage_limit( true );
427+
$usage_data = ( new ImageService( $container ) )->get_monthly_usage_limit( true );
427428
if ( ! is_wp_error( $usage_data ) ) {
428429
$settings['monthly_usage'] = $usage_data;
429430
update_option( self::SETTING_KEY, $settings );
@@ -441,12 +442,13 @@ public static function get( $call_worker = true ) {
441442
/**
442443
* Updates the image optimization settings.
443444
*
444-
* @param array $settings The new settings array.
445+
* @param array $settings The new settings array.
446+
* @param \NewfoldLabs\WP\Container\Container $container Dependency injection container.
445447
*
446448
* @return bool true if the settings were updated successfully, false otherwise.
447449
*/
448-
public static function update( $settings ) {
449-
$instance = new self();
450+
public static function update( $settings, $container ) {
451+
$instance = new self( $container );
450452
$sanitized_settings = $instance->sanitize_settings( $settings );
451453
return update_option( self::SETTING_KEY, $sanitized_settings );
452454
}

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
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Module to manage performance settings in newfold plugins",
44
"license": "GPL-2.0-or-later",
55
"private": true,
6-
"version": "3.2.5",
6+
"version": "3.2.6",
77
"contributors": [
88
"Abdulrahman Al Ani (https://alani.dev/)",
99
"Evan Mullins (https://evanmullins.com)",

0 commit comments

Comments
 (0)