class-entity.php
1 year ago
class-extras.php
1 year ago
class-repository-helpers.php
2 days ago
class-wrapper.php
1 week ago
index.php
2 years ago
class-repository-helpers.php
98 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shared repository helper methods. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0.24 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Traits; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Repository helpers for repeated internal repository logic. |
| 16 | */ |
| 17 | trait Repository_Helpers { |
| 18 | |
| 19 | /** |
| 20 | * Normalize a list of entity IDs to positive integers. |
| 21 | * |
| 22 | * @param int[] $ids Entity IDs. |
| 23 | * |
| 24 | * @return int[] |
| 25 | */ |
| 26 | protected function normalize_entity_ids( array $ids ): array { |
| 27 | return array_values( array_filter( array_map( 'absint', $ids ) ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Convert cached summaries into an ID => title dropdown map. |
| 32 | * |
| 33 | * @param array<int, array{id: int, title: string}> $summaries Entity summaries. |
| 34 | * |
| 35 | * @return array<int, string> |
| 36 | */ |
| 37 | protected function summaries_to_dropdown( array $summaries ): array { |
| 38 | if ( empty( $summaries ) ) { |
| 39 | return []; |
| 40 | } |
| 41 | |
| 42 | return wp_list_pluck( $summaries, 'title', 'id' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Hydrate post-backed entities for the given post IDs. |
| 47 | * |
| 48 | * @param int[] $post_ids Post IDs. |
| 49 | * @param callable $loader Loader callback receiving the post ID. |
| 50 | * @param bool $skip_invalid Whether to skip falsey loader results. |
| 51 | * |
| 52 | * @return array<int, mixed> |
| 53 | */ |
| 54 | protected function hydrate_post_entities( array $post_ids, callable $loader, bool $skip_invalid = false ): array { |
| 55 | $post_ids = $this->normalize_entity_ids( $post_ids ); |
| 56 | |
| 57 | if ( ! empty( $post_ids ) ) { |
| 58 | _prime_post_caches( $post_ids, false, true ); |
| 59 | } |
| 60 | |
| 61 | $entities = []; |
| 62 | foreach ( $post_ids as $post_id ) { |
| 63 | $entity = $loader( $post_id ); |
| 64 | |
| 65 | if ( $skip_invalid && ( false === $entity || null === $entity ) ) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | $entities[ $post_id ] = $entity; |
| 70 | } |
| 71 | |
| 72 | return $entities; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Update only the modified timestamps for a post-backed entity. |
| 77 | * |
| 78 | * @param int $post_id Post ID. |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | protected function touch_post_modified_time( int $post_id ): void { |
| 83 | global $wpdb; |
| 84 | |
| 85 | $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 86 | $wpdb->posts, |
| 87 | [ |
| 88 | 'post_modified' => current_time( 'mysql' ), |
| 89 | 'post_modified_gmt' => current_time( 'mysql', 1 ), |
| 90 | ], |
| 91 | [ |
| 92 | 'ID' => $post_id, |
| 93 | ] |
| 94 | ); |
| 95 | clean_post_cache( $post_id ); |
| 96 | } |
| 97 | } |
| 98 |