Skip to content

Commit

Permalink
✨ Add new subscriber, service provider class and filter, remove proce…
Browse files Browse the repository at this point in the history
…dural code
  • Loading branch information
Khadreal committed Dec 20, 2024
1 parent 260a3cc commit 8fd3220
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 32 deletions.
63 changes: 63 additions & 0 deletions classes/Media/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);

namespace Imagify\Media;

use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use Imagify\Media\Upload\Upload;

/**
* Service provider for Media
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* Services provided by this provider
*
* @var array
*/
protected $provides = [
Upload::class,
Subscriber::class,
];

/**
* Subscribers provided by this provider
*
* @var array
*/
public $subscribers = [
Subscriber::class,
];

/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}

/**
* Returns the subscribers array
*
* @return array
*/
public function get_subscribers(): array {
return $this->subscribers;
}

/**
* Registers the provided classes
*
* @return void
*/
public function register(): void {
$this->getContainer()->add( Upload::class );

$this->getContainer()->addShared( Subscriber::class )
->addArgument( Upload::class );
}
}
46 changes: 46 additions & 0 deletions classes/Media/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace Imagify\Media;

use Imagify\EventManagement\SubscriberInterface;
use Imagify\Media\Upload\Upload;

/**
* Media Subscriber
*/
class Subscriber implements SubscriberInterface {

/**
* @var Upload
*/
private $upload;
/**
* Constructor
*
* @param Upload $upload Upload Instance.
*/
public function __construct( Upload $upload ) {
$this->upload = $upload;
}

/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events(): array {
return [
'restrict_manage_posts' => 'imagify_attachments_filter_dropdown',
];
}

/**
* Adds a dropdown that allows filtering on the attachments Imagify status.
*
* @return void
*/
public function imagify_attachments_filter_dropdown() {
$this->upload->add_imagify_filter_to_attachments_dropdown();
}
}
51 changes: 51 additions & 0 deletions classes/Media/Upload/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

namespace Imagify\Media\Upload;
/**
* Upload Media Class.
*
*/
class Upload {

/**
* Adds a dropdown that allows filtering on the attachments Imagify status.
*
* @return void
*/
public function add_imagify_filter_to_attachments_dropdown() {
if ( ! \Imagify_Views::get_instance()->is_wp_library_page() ) {
return;
}

/**
* Tell if imagify stats query should run.
*
* @param bool $boolean True if the query should be run. False otherwise.
*/
if ( apply_filters( 'imagify_display_library_stats', false ) ) {
$optimized = imagify_count_optimized_attachments();

Check warning on line 27 in classes/Media/Upload/Upload.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Media/Upload/Upload.php#L27

Avoid unused local variables such as '$optimized'.
$unoptimized = imagify_count_unoptimized_attachments();

Check warning on line 28 in classes/Media/Upload/Upload.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Media/Upload/Upload.php#L28

Avoid unused local variables such as '$unoptimized'.
$errors = imagify_count_error_attachments();

Check warning on line 29 in classes/Media/Upload/Upload.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Media/Upload/Upload.php#L29

Avoid unused local variables such as '$errors'.
}

$status = isset( $_GET['imagify-status'] ) ? wp_unslash( $_GET['imagify-status'] ) : 0; // WPCS: CSRF ok.
$options = array(
'optimized' => _x( 'Optimized', 'Media Files', 'imagify' ),
'unoptimized' => _x( 'Unoptimized', 'Media Files', 'imagify' ),
'errors' => _x( 'Errors', 'Media Files', 'imagify' ),
);

echo '<label class="screen-reader-text" for="filter-by-optimization-status">' . __( 'Filter by status', 'imagify' ) . '</label>';
echo '<select id="filter-by-optimization-status" name="imagify-status">';
echo '<option value="0" selected="selected">' . __( 'All Media Files', 'imagify' ) . '</option>';

foreach ( $options as $value => $label ) {
if ( isset( ${$value} ) ) {
$filter_value = ' (' . ${$value} . ')';
}
echo '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . $filter_value . '</option>';

Check failure on line 47 in classes/Media/Upload/Upload.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Variable $filter_value might not be defined.
}
echo '</select>&nbsp;';
}
}
1 change: 1 addition & 0 deletions config/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
'Imagify\Stats\ServiceProvider',
'Imagify\Webp\ServiceProvider',
'Imagify\ThirdParty\ServiceProvider',
'Imagify\Media\ServiceProvider',
];
32 changes: 0 additions & 32 deletions inc/admin/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,6 @@ function _imagify_manage_media_custom_column( $column_name, $attachment_id ) {
echo get_imagify_media_column_content( $process );
}

add_action( 'restrict_manage_posts', '_imagify_attachments_filter_dropdown' );
/**
* Adds a dropdown that allows filtering on the attachments Imagify status.
*
* @since 1.0
* @author Jonathan Buttigieg
*/
function _imagify_attachments_filter_dropdown() {
if ( ! Imagify_Views::get_instance()->is_wp_library_page() ) {
return;
}

$optimized = imagify_count_optimized_attachments();
$unoptimized = imagify_count_unoptimized_attachments();
$errors = imagify_count_error_attachments();
$status = isset( $_GET['imagify-status'] ) ? wp_unslash( $_GET['imagify-status'] ) : 0; // WPCS: CSRF ok.
$options = array(
'optimized' => _x( 'Optimized', 'Media Files', 'imagify' ),
'unoptimized' => _x( 'Unoptimized', 'Media Files', 'imagify' ),
'errors' => _x( 'Errors', 'Media Files', 'imagify' ),
);

echo '<label class="screen-reader-text" for="filter-by-optimization-status">' . __( 'Filter by status', 'imagify' ) . '</label>';
echo '<select id="filter-by-optimization-status" name="imagify-status">';
echo '<option value="0" selected="selected">' . __( 'All Media Files', 'imagify' ) . '</option>';

foreach ( $options as $value => $label ) {
echo '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . ' (' . ${$value} . ')</option>';
}
echo '</select>&nbsp;';
}

add_filter( 'request', '_imagify_sort_attachments_by_status' );
/**
* Modify the query based on the imagify-status variable in $_GET.
Expand Down

0 comments on commit 8fd3220

Please sign in to comment.