PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 3.11.0
Image Source Control Lite – Show Image Credits and Captions v3.11.0
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 / image-sources / Post_Meta / Image_Posts_Meta.php

Image_Posts_Meta.php in Image Source Control Lite – Show Image Credits and Captions 3.11.0, at includes/image-sources/Post_Meta/Image_Posts_Meta.php

137 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ISC\Image_Sources\Post_Meta;
4
5 use ISC_Log;
6 use ISC_Model;
7
8 /**
9 * Handles the image-posts post meta value added to attachments.
10 */
11 class Image_Posts_Meta {
12
13 const META_KEY = 'isc_image_posts';
14
15 /**
16 * Getter
17 *
18 * @param int $image_id Attachment post ID.
19 *
20 * @return array|false The post images meta value, or empty string if not found.
21 */
22 public static function get( $image_id ) {
23 $value = get_post_meta( $image_id, self::META_KEY, true );
24
25 if ( $value === false ) {
26 ISC_Log::log( sprintf( 'Error getting %s for post %d', self::META_KEY, $image_id ) );
27 }
28
29 return is_array( $value ) ? $value : '';
30 }
31
32 /**
33 * Updater – also sets a new value
34 *
35 * @param int $image_id Attachment post ID.
36 * @param array $value The new value to set.
37 *
38 * @return bool|int
39 */
40 public static function update( $image_id, $value ) {
41 $return = ISC_Model::update_post_meta( $image_id, self::META_KEY, $value );
42
43 // Log on error
44 if ( $return === false ) {
45 ISC_Log::log( sprintf( 'Error updating %s for post %d', self::META_KEY, $image_id ) );
46 }
47
48 return $return;
49 }
50
51 /**
52 * Delete the post-images meta value
53 *
54 * @param int $image_id Attachment post ID.
55 *
56 * @return bool
57 */
58 public static function delete( $image_id ): bool {
59 $return = delete_post_meta( $image_id, self::META_KEY );
60
61 if ( $return === false ) {
62 ISC_Log::log( sprintf( 'Error deleting %s for post %d', self::META_KEY, $image_id ) );
63 }
64
65 return $return;
66 }
67
68 /**
69 * Remove post_images index
70 * namely the post meta field `isc_image_posts`
71 *
72 * @return bool True on success, false on failure.
73 */
74 public static function delete_all(): bool {
75 return delete_post_meta_by_key( self::META_KEY );
76 }
77
78 /**
79 * Update the isc_image_posts meta field with a filtered limit
80 *
81 * @param integer $image_id ID of the image.
82 * @param array $post_ids IDs of the posts in which the image appears in.
83 */
84 public static function update_image_posts_meta_with_limit( int $image_id, array $post_ids ) {
85 // limit the number of post IDs to 10
86 $post_ids = array_slice( $post_ids, 0, apply_filters( 'isc_image_posts_meta_limit', 10 ) );
87
88 self::update( $image_id, $post_ids );
89 }
90
91 /**
92 * Adds an association between an image and a post in the image's meta.
93 *
94 * @param int $image_id Image ID.
95 * @param int $post_id Post ID.
96 *
97 * @return void
98 */
99 public static function add_image_post_association( int $image_id, int $post_id ): void {
100 if ( empty( $image_id ) ) {
101 return;
102 }
103
104 $meta = get_post_meta( $image_id, self::META_KEY, true );
105 if ( ! is_array( $meta ) ) {
106 $meta = [];
107 }
108 if ( ! in_array( $post_id, $meta, true ) ) {
109 $meta[] = $post_id;
110 ISC_Log::log( sprintf( 'Adding post %d to %s for image %d.', $post_id, self::META_KEY, $image_id ) );
111 self::update_image_posts_meta_with_limit( $image_id, array_unique( $meta ) );
112 }
113 }
114
115 /**
116 * Removes an association between an image and a post in the image's meta.
117 *
118 * @param int $image_id Image ID.
119 * @param int $post_id Post ID.
120 */
121 public static function remove_image_post_association( int $image_id, int $post_id ) {
122 if ( empty( $image_id ) ) {
123 return;
124 }
125
126 $meta = self::get( $image_id );
127 if ( is_array( $meta ) ) {
128 $offset = array_search( $post_id, $meta, true );
129 if ( $offset !== false ) {
130 array_splice( $meta, $offset, 1 );
131 ISC_Log::log( sprintf( 'Removing post %d from %s for image %d.', $post_id, self::META_KEY, $image_id ) );
132 self::update_image_posts_meta_with_limit( $image_id, array_unique( $meta ) );
133 }
134 }
135 }
136 }
137