Skip to content

Commit

Permalink
Closes #944 Add filter for imagify stat (#946)
Browse files Browse the repository at this point in the history
Co-authored-by: WordPressFan <[email protected]>
  • Loading branch information
Khadreal and wordpressfan authored Dec 23, 2024
1 parent e0f1a9e commit 3587e38
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 32 deletions.
67 changes: 67 additions & 0 deletions Tests/Integration/inc/classes/Media/upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

namespace Imagify\Tests\Integration\inc\classes\Media;

use Imagify\Tests\Integration\TestCase;
use Brain\Monkey\Functions;
use ReflectionClass;

/**
* @covers \Imagify\Media\Upload\Upload::add_imagify_filter_to_attachments_dropdown
* @group Upload
*/
class Upload extends TestCase {
protected $view_instance;

public function tear_down() {
remove_all_filters( 'imagify_display_library_stats' );
remove_all_filters( 'imagify_count_optimized_attachments' );
remove_all_filters( 'imagify_count_unoptimized_attachments' );
remove_all_filters( 'imagify_count_error_attachments' );

if ($this->view_instance) {

}
parent::tear_down();
}

public function set_up() {

parent::set_up();
}

public function testShouldReturnExpected() {
$upload = new \Imagify\Media\Upload\Upload();

add_filter( 'imagify_display_library_stats', '__return_true' );

$this->mock_imagify_count_functions();
ob_start();
$upload->add_imagify_filter_to_attachments_dropdown();
$output = ob_get_clean();

$this->assertStringContainsString( 'Filter by status', $output );
$this->assertStringContainsString( '<select id="filter-by-optimization-status"', $output );
$this->assertStringContainsString( 'selected="selected"', $output );
$this->assertStringContainsString( 'Errors (2)', $output );
$this->assertSame( 1, 1 );
}

/**
* Mock the return values of imagify count functions.
*
* @return void
*/
public function mock_imagify_count_functions() {
add_filter( 'imagify_count_optimized_attachments', function() {
return 10;
} );
add_filter( 'imagify_count_unoptimized_attachments', function() {
return 5;
} );
add_filter( 'imagify_count_error_attachments', function() {
return 2;
} );
}
}
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 );
}
}
51 changes: 51 additions & 0 deletions classes/Media/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

namespace Imagify\Media;

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

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

/**
* Upload instance.
*
* @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() {
if ( ! \Imagify_Views::get_instance()->is_wp_library_page() ) {
return;
}
$this->upload->add_imagify_filter_to_attachments_dropdown();
}
}
47 changes: 47 additions & 0 deletions classes/Media/Upload/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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() {
$data = [];

/**
* 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 ) ) {
$data['optimized'] = imagify_count_optimized_attachments();
$data['unoptimized'] = imagify_count_unoptimized_attachments();
$data['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 ) {
$filter_value = isset( $data[ $value ] ) ? ' (' . $data[ $value ] . ')' : '';
echo '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . $filter_value . '</option>';
}
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 3587e38

Please sign in to comment.