| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Image_Sources\Post_Meta; |
| 4 |
|
| 5 |
use ISC_Log; |
| 6 |
use ISC_Model; |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles the post-images post meta value added to public post types. |
| 10 |
*/ |
| 11 |
class Post_Images_Meta { |
| 12 |
|
| 13 |
const META_KEY = 'isc_post_images'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Getter |
| 17 |
* |
| 18 |
* @param int $post_id Post ID. |
| 19 |
* |
| 20 |
* @return array|string The post images meta value, or empty string if not found. |
| 21 |
*/ |
| 22 |
public static function get( $post_id ) { |
| 23 |
$value = get_post_meta( $post_id, self::META_KEY, true ); |
| 24 |
|
| 25 |
if ( $value === false ) { |
| 26 |
ISC_Log::log( sprintf( 'Error getting %s for post %d', self::META_KEY, $post_id ) ); |
| 27 |
} |
| 28 |
|
| 29 |
return is_array( $value ) ? $value : ''; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Updater – also sets a new value |
| 34 |
* |
| 35 |
* @param int $post_id Post ID. |
| 36 |
* @param array $value The new value to set. |
| 37 |
* |
| 38 |
* @return bool|int |
| 39 |
*/ |
| 40 |
public static function update( $post_id, $value ) { |
| 41 |
$return = ISC_Model::update_post_meta( $post_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, $post_id ) ); |
| 46 |
} |
| 47 |
|
| 48 |
return $return; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Delete the post-images meta value |
| 53 |
* |
| 54 |
* @param int $post_id Post ID. |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public static function delete( $post_id ): bool { |
| 59 |
$return = delete_post_meta( $post_id, self::META_KEY ); |
| 60 |
|
| 61 |
if ( $return === false ) { |
| 62 |
ISC_Log::log( sprintf( 'Error deleting %s for post %d', self::META_KEY, $post_id ) ); |
| 63 |
} |
| 64 |
|
| 65 |
return $return; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Remove post_images index |
| 70 |
* namely the post meta field `isc_post_images` |
| 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 |
* Updates the post meta index mapping a specific post to the images it contains. |
| 80 |
* |
| 81 |
* This method processes an array of image IDs found within a post's content, |
| 82 |
* automatically includes the post's featured image (thumbnail), applies the |
| 83 |
* `isc_images_in_posts` filter for extensibility, and then saves the final |
| 84 |
* structured array to the post's meta field isc_post_images. |
| 85 |
* The saved array maps image IDs to their data, |
| 86 |
* including a flag indicating if an image is the featured image. |
| 87 |
* |
| 88 |
* @param int $post_id The ID of the post whose image index is being updated. |
| 89 |
* @param array $image_ids An array mapping image attachment IDs found in the post's |
| 90 |
* content to their data (e.g., ['src' => '...']). |
| 91 |
* Example: [ 123 => ['src' => 'url1'], 456 => ['src' => 'url2'] ] |
| 92 |
* The featured image ID will be added or updated automatically. |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public static function update_images_in_posts( int $post_id, array $image_ids ) { |
| 96 |
// add thumbnail information |
| 97 |
$thumb_id = get_post_thumbnail_id( $post_id ); |
| 98 |
|
| 99 |
/** |
| 100 |
* If an image is used both inside the post and as post thumbnail, the thumbnail entry overrides the regular image. |
| 101 |
*/ |
| 102 |
if ( ! empty( $thumb_id ) ) { |
| 103 |
$image_ids[ $thumb_id ] = [ |
| 104 |
'src' => wp_get_attachment_url( $thumb_id ), |
| 105 |
'thumbnail' => true, |
| 106 |
]; |
| 107 |
ISC_Log::log( 'thumbnail found with ID ' . $thumb_id ); |
| 108 |
} |
| 109 |
|
| 110 |
// apply filter to image array, so other developers can add their own logic |
| 111 |
$image_ids = apply_filters( 'isc_images_in_posts', $image_ids, $post_id ); |
| 112 |
|
| 113 |
if ( empty( $image_ids ) ) { |
| 114 |
$image_ids = []; |
| 115 |
} |
| 116 |
|
| 117 |
ISC_Log::log( sprintf( 'Updating %s for post %d.', self::META_KEY, $post_id ) ); |
| 118 |
|
| 119 |
self::update( $post_id, $image_ids ); |
| 120 |
} |
| 121 |
} |
| 122 |
|