PluginProbe ʕ •ᴥ•ʔ
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN / 3.23.3
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN v3.23.3
4.1.1 4.1.0 4.0.3 4.0.2 2.8.1 2.9.1 3.0.0 3.0.1 3.0.2 3.1.1 3.10.1 3.10.2 3.10.3 3.11.1 3.12.3 3.12.4 3.12.5 3.12.6 3.13.0 3.13.1 3.13.2 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.2 3.16.4 3.16.5 3.16.6 3.17.0 3.17.1 3.18.0 3.18.1 3.2.0.1 3.2.1 3.2.2.1 3.2.4 3.20.0 3.21.1 3.22.1 3.22.3 3.23.0 3.23.1 3.23.2 3.23.3 3.23.4 3.24.0 3.24.0-beta.2 3.3.0 3.3.1 3.3.2 3.4.1 3.4.2 3.6.1 3.6.3 3.7.0 3.7.1 3.7.2 3.7.3 3.8.2 3.8.3 3.8.4 3.8.5 3.8.7 3.8.8 3.9.0 3.9.1 3.9.11 3.9.2 3.9.4 3.9.5 3.9.8 3.9.9 trunk 1.0.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.5.3 1.6.5.4 1.7 1.7.1 1.7.1.1 2.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.6.2 2.0.6.3 2.0.6.5 2.0.7 2.0.7.1 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.2 2.3 2.3.1 2.4 2.4.2 2.4.3 2.4.4 2.4.5 2.5.2 2.5.3 2.6.1 2.6.2 2.6.3 2.7 2.7.1 2.7.4 2.7.4.1 2.7.5 2.7.6 2.7.8 2.7.8.1 2.7.9.1 2.8.0 2.8.0.1
wp-smushit / core / class-attachment-id-list.php
wp-smushit / core Last commit date
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