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

Closes #944 Add filter for imagify stat #946

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
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();
}
}
46 changes: 46 additions & 0 deletions classes/Media/Upload/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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() {
/**
* 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 22 in classes/Media/Upload/Upload.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Media/Upload/Upload.php#L22

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

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Media/Upload/Upload.php#L23

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

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

classes/Media/Upload/Upload.php#L24

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>';
$filter_value = '';
foreach ( $options as $value => $label ) {
if ( isset( ${$value} ) ) {
$filter_value = ' (' . ${$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
Loading