PluginProbe
Image Source Control Lite – Show Image Credits and Captions / trunk
Image Source Control Lite – Show Image Credits and Captions vtrunk
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / storage-model.php

storage-model.php in Image Source Control Lite – Show Image Credits and Captions trunk, at includes/storage-model.php

249 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The ISC Storage reduces the number of SQL queries for sources in the frontend.
4 * It can be extended to host image source information for images outside the WP media library, too.
5 *
6 * The Model class contans the logic to get and set entries in the ISC storage
7 *
8 * Structure of the storage array:
9 * key: the image URL
10 * value: array with data:
11 * - post_id – Post ID of the attachment or null if there is none
12 */
13 class ISC_Storage_Model {
14
15 /**
16 * Name of the option
17 *
18 * @var string
19 */
20 protected $option_slug = 'isc_storage';
21
22 /**
23 * Storage option with content
24 *
25 * @var array
26 */
27 public $storage;
28
29 /**
30 * Instance of ISC_Storage_Model
31 *
32 * @var ISC_Storage_Model
33 */
34 protected static $instance;
35
36 /**
37 * Load storage
38 */
39 public function __construct() {
40 $this->get_storage();
41 }
42
43 /**
44 * Get storage array
45 *
46 * @return array
47 */
48 public function get_storage() {
49 if ( $this->storage ) {
50 return $this->storage;
51 }
52
53 $this->storage = get_option( $this->option_slug, array() );
54
55 return $this->storage;
56 }
57
58 /**
59 * Sanitize the image URL to serve as a key
60 * - remove protocols
61 * - sanitize using esc_url
62 *
63 * We intentionally keep www. since this could prevent images from being found or the URLs to work later, when used in the frontend.
64 *
65 * @param string $url raw URL input.
66 * @return string sanitized URL string
67 */
68 public static function sanitize_url_key( $url ) {
69 $limit = 2;
70 return str_replace( array( 'http://', 'https://', '//' ), '', esc_url( $url ), $limit );
71 }
72
73 /**
74 * Check if the image URL is known to the storage
75 *
76 * @param string $url part of the image URL string.
77 * @return bool
78 */
79 public function is_image_url_in_storage( $url ) {
80 $storage = $this->get_storage();
81 $url = self::sanitize_url_key( $url );
82
83 return isset( $storage[ $url ] );
84 }
85
86 /**
87 * Return image ID based on a given URL
88 *
89 * @param string $url part of the image URL string.
90 * @return int|false|null attachment ID if found in the storage, false if not found, null if the image URL does not have an image ID.
91 */
92 public function get_image_id_from_storage( $url ) {
93 $url = self::sanitize_url_key( $url );
94
95 if ( ! $this->is_image_url_in_storage( $url ) ) {
96 return false;
97 }
98
99 $storage = $this->get_storage();
100
101 // return post ID or null if element exists
102 return ( isset( $storage[ $url ]['post_id'] ) ) ? absint( $storage[ $url ]['post_id'] ) : null;
103 }
104
105 /**
106 * Return any data for a given image URL
107 *
108 * @param string $url part of the image URL string.
109 * @param string $key key in the image data.
110 *
111 * @return int|false|null attachment ID if found in the storage, false if not found, null if the image URL does not have an image ID.
112 */
113 public function get_data_by_image_url( $url, $key ) {
114 $url = self::sanitize_url_key( $url );
115
116 if ( ! $this->is_image_url_in_storage( $url ) ) {
117 return false;
118 }
119
120 $storage = $this->get_storage();
121
122 // return post ID or null if element exists
123 return ( isset( $storage[ $url ][ $key ] ) ) ? $storage[ $url ][ $key ] : null;
124 }
125
126 /**
127 * Updates or adds image URL with the post ID to the storage
128 *
129 * @param string $url image URL.
130 * @param integer $post_id WP_Post ID.
131 */
132 public function update_post_id( $url, $post_id ) {
133 $url = self::sanitize_url_key( $url );
134
135 if ( absint( $post_id ) ) {
136 $this->update( $url, array( 'post_id' => absint( $post_id ) ) );
137 }
138 }
139
140 /**
141 * Updates or adds image URL with the post ID to the storage
142 *
143 * @param string $url image URL.
144 * @param string $key key in the image data.
145 * @param string $value new value.
146 */
147 public function update_data_by_image_url( $url, $key, $value ) {
148 $key = esc_attr( $key );
149 $url = self::sanitize_url_key( $url );
150
151 $storage = $this->get_storage();
152 if ( isset( $storage[ $url ] ) ) {
153 $data = $storage[ $url ];
154 } else {
155 $data = array();
156 }
157
158 $data[ $key ] = $value;
159
160 $this->update( $url, $data );
161 }
162
163 /**
164 * Updates or adds element to storage
165 *
166 * @param string $url image URL.
167 * @param array $data storage data.
168 */
169 public function update( $url, array $data ) {
170 $url = self::sanitize_url_key( $url );
171 $storage = $this->get_storage();
172
173 // merge existing data with new data
174 if ( isset( $storage[ $url ] ) ) {
175 $storage[ $url ] = array_merge( $storage[ $url ], $data );
176 } else {
177 $storage[ $url ] = $data;
178 }
179
180 $this->storage = $storage;
181 // autoload is false since this can get quite large
182 update_option( $this->option_slug, $storage, false );
183 }
184
185 /**
186 * Remove an element from the storage
187 *
188 * @param string $url image URL.
189 */
190 public function remove_image( $url ) {
191 $url = self::sanitize_url_key( $url );
192
193 $storage = $this->get_storage();
194 if ( ! isset( $storage[ $url ] ) ) {
195 return;
196 }
197
198 unset( $storage[ $url ] );
199 $this->storage = $storage;
200 update_option( $this->option_slug, $storage, true );
201 }
202
203 /**
204 * Remove an element from the storage based on the post ID
205 *
206 * @param int $post_id WP_Post ID.
207 */
208 public function remove_image_by_id( $post_id ) {
209 $storage = $this->get_storage();
210
211 // search for the post ID
212 $image_key = array_search( $post_id, array_combine( array_keys( $storage ), array_column( $storage, 'post_id' ) ) );
213
214 if ( $image_key ) {
215 $this->remove_image( $image_key );
216 }
217 }
218
219 /**
220 * Clear storage by removing the isc_storage option
221 *
222 * MAKE SURE THE OPTION DOES NOT CONTAIN DATA, LIKE IMAGE SOURCE STRINGS
223 *
224 * @return bool true if the option was removed
225 */
226 public static function clear_storage() {
227 return delete_option( 'isc_storage' );
228 }
229
230 /**
231 * Return storage without images that have an attachment ID
232 *
233 * @return array
234 */
235 public function get_storage_without_wp_images() {
236 $storage = $this->get_storage();
237
238 $storage_filtered = array();
239 // remove any entry with a post_ID, since they are hosted in WP Media
240 foreach ( $storage as $url => $data ) {
241 if ( ! isset( $data['post_id'] ) ) {
242 $storage_filtered[ $url ] = $data;
243 }
244 }
245
246 return $storage_filtered;
247 }
248 }
249