api
4 months ago
backups
4 months ago
cache
4 months ago
cli
4 months ago
directory
4 months ago
external
4 months ago
integrations
4 months ago
lazy-load
4 months ago
media
4 months ago
media-library
4 months ago
membership
4 months ago
modules
4 months ago
parser
4 months ago
photon
4 months ago
resize
4 months ago
security
4 months ago
smush
4 months ago
srcset
4 months ago
stats
4 months ago
threads
4 months ago
transform
4 months ago
class-animated-status-controller.php
4 months ago
class-array-utils.php
4 months ago
class-attachment-id-list.php
4 months ago
class-backup-size.php
4 months ago
class-configs.php
4 months ago
class-controller.php
4 months ago
class-core.php
4 months ago
class-cron-controller.php
4 months ago
class-deprecated-hooks.php
4 months ago
class-error-handler.php
4 months ago
class-file-system.php
4 months ago
class-file-utils.php
4 months ago
class-format-utils.php
4 months ago
class-helper.php
4 months ago
class-hub-connector.php
4 months ago
class-installer.php
4 months ago
class-keyword-exclusions.php
4 months ago
class-modules.php
4 months ago
class-optimization-controller.php
4 months ago
class-plugin-settings-watcher.php
4 months ago
class-product-analytics.php
4 months ago
class-rest.php
4 months ago
class-server-utils.php
4 months ago
class-settings.php
4 months ago
class-shim.php
4 months ago
class-smush-file.php
4 months ago
class-stats.php
4 months ago
class-time-utils.php
4 months ago
class-timer.php
4 months ago
class-upload-dir.php
4 months ago
class-url-utils.php
4 months ago
class-wp-query-utils.php
4 months ago
wp-compat.php
4 months ago
class-attachment-id-list.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Smush\Core; |
| 4 | |
| 5 | use Smush\Core\Modules\Background\Mutex; |
| 6 | |
| 7 | class Attachment_Id_List { |
| 8 | private $option_id; |
| 9 | |
| 10 | private $ids; |
| 11 | /** |
| 12 | * @var Array_Utils |
| 13 | */ |
| 14 | private $array_utils; |
| 15 | |
| 16 | public function __construct( $option_id ) { |
| 17 | $this->option_id = $option_id; |
| 18 | $this->array_utils = new Array_Utils(); |
| 19 | } |
| 20 | |
| 21 | private function set_ids( $ids ) { |
| 22 | $this->ids = $ids; |
| 23 | } |
| 24 | |
| 25 | public function get_ids() { |
| 26 | if ( is_null( $this->ids ) ) { |
| 27 | $this->ids = $this->fetch_ids(); |
| 28 | } |
| 29 | |
| 30 | return $this->ids; |
| 31 | } |
| 32 | |
| 33 | private function fetch_ids() { |
| 34 | wp_cache_delete( $this->option_id, 'options' ); |
| 35 | |
| 36 | return $this->string_to_array( (string) get_option( $this->option_id ) ); |
| 37 | } |
| 38 | |
| 39 | public function has_id( $id ) { |
| 40 | return in_array( $id, $this->get_ids() ); |
| 41 | } |
| 42 | |
| 43 | public function add_id( $attachment_id ) { |
| 44 | $this->mutex( function () use ( $attachment_id ) { |
| 45 | $ids = $this->fetch_ids(); |
| 46 | if ( ! in_array( $attachment_id, $ids ) ) { |
| 47 | $ids[] = $attachment_id; |
| 48 | } |
| 49 | $this->_update_ids( $ids ); |
| 50 | } ); |
| 51 | } |
| 52 | |
| 53 | public function add_ids( $attachment_ids ) { |
| 54 | $this->mutex( function () use ( $attachment_ids ) { |
| 55 | $new_ids = array_merge( $this->fetch_ids(), $attachment_ids ); |
| 56 | $new_ids = $this->array_utils->fast_array_unique( $new_ids ); |
| 57 | $this->_update_ids( $new_ids ); |
| 58 | } ); |
| 59 | } |
| 60 | |
| 61 | public function remove_id( $attachment_id ) { |
| 62 | $this->mutex( function () use ( $attachment_id ) { |
| 63 | $ids = $this->fetch_ids(); |
| 64 | $index = array_search( $attachment_id, $ids ); |
| 65 | if ( $index !== false ) { |
| 66 | unset( $ids[ $index ] ); |
| 67 | } |
| 68 | $this->_update_ids( $ids ); |
| 69 | } ); |
| 70 | } |
| 71 | |
| 72 | public function update_ids( $ids ) { |
| 73 | $this->mutex( function () use ( $ids ) { |
| 74 | $this->_update_ids( $ids ); |
| 75 | } ); |
| 76 | } |
| 77 | |
| 78 | public function delete_ids() { |
| 79 | delete_option( $this->option_id ); |
| 80 | $this->set_ids( array() ); |
| 81 | } |
| 82 | |
| 83 | private function string_to_array( $string ) { |
| 84 | return empty( $string ) |
| 85 | ? array() |
| 86 | : explode( ',', $string ); |
| 87 | } |
| 88 | |
| 89 | private function array_to_string( $array ) { |
| 90 | $array = empty( $array ) || ! is_array( $array ) |
| 91 | ? array() |
| 92 | : $array; |
| 93 | |
| 94 | return join( ',', $array ); |
| 95 | } |
| 96 | |
| 97 | private function mutex( $operation ) { |
| 98 | $option_id = $this->option_id; |
| 99 | ( new Mutex( "{$option_id}_mutex" ) )->execute( $operation ); |
| 100 | } |
| 101 | |
| 102 | private function _update_ids( $ids ) { |
| 103 | update_option( |
| 104 | $this->option_id, |
| 105 | $this->array_to_string( $ids ), |
| 106 | false |
| 107 | ); |
| 108 | $this->set_ids( $ids ); |
| 109 | } |
| 110 | |
| 111 | public function get_count() { |
| 112 | return count( $this->get_ids() ); |
| 113 | } |
| 114 | } |
| 115 |