-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add new subscriber, service provider class and filter, remove proce…
…dural code
- Loading branch information
Showing
5 changed files
with
161 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
$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 ) { | ||
if ( isset( ${$value} ) ) { | ||
$filter_value = ' (' . ${$value} . ')'; | ||
} | ||
echo '<option value="' . $value . '" ' . selected( $status, $value, false ) . '>' . $label . $filter_value . '</option>'; | ||
} | ||
echo '</select> '; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters