HasWordPressPostBehavior.php
2 weeks ago
SearchablePost.php
2 weeks ago
SearchableUser.php
2 weeks ago
HasWordPressPostBehavior.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Traits; |
| 4 | |
| 5 | use Exception; |
| 6 | use Kirki\Framework\Constants\DateTimeFormats; |
| 7 | |
| 8 | |
| 9 | trait HasWordPressPostBehavior |
| 10 | { |
| 11 | |
| 12 | /** |
| 13 | * Create post |
| 14 | * |
| 15 | * @param array $data |
| 16 | * |
| 17 | * @return static |
| 18 | */ |
| 19 | public static function create_post(array $data) |
| 20 | { |
| 21 | if (!empty($data['post_date']) && $data['post_date'] instanceof \DateTimeInterface) { |
| 22 | $date = $data['post_date']; |
| 23 | $data['post_date'] = $date->format(DateTimeFormats::DB_DATETIME); |
| 24 | |
| 25 | $date_gmt = new \DateTime($date->format(DateTimeFormats::DB_DATETIME), $date->getTimezone()); |
| 26 | $date_gmt->setTimezone(new \DateTimeZone('UTC')); |
| 27 | $data['post_date_gmt'] = $date_gmt->format(DateTimeFormats::DB_DATETIME); |
| 28 | } |
| 29 | |
| 30 | $post_id = wp_insert_post($data, true); |
| 31 | |
| 32 | if (is_wp_error($post_id)) { |
| 33 | throw new Exception($post_id->get_error_message()); |
| 34 | } |
| 35 | |
| 36 | if (!$post_id) { |
| 37 | throw new Exception(__('Failed to create post.', 'kirki')); |
| 38 | } |
| 39 | |
| 40 | return static::find($post_id); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Update post |
| 45 | * |
| 46 | * @param int $post_id |
| 47 | * @param array $data |
| 48 | * |
| 49 | * @return static |
| 50 | */ |
| 51 | public static function update_post(int $post_id, array $data) |
| 52 | { |
| 53 | $data['ID'] = $post_id; |
| 54 | |
| 55 | if (!empty($data['post_date']) && $data['post_date'] instanceof \DateTimeInterface) { |
| 56 | $date = $data['post_date']; |
| 57 | $data['post_date'] = $date->format(DateTimeFormats::DB_DATETIME); |
| 58 | |
| 59 | $date_gmt = new \DateTime($date->format(DateTimeFormats::DB_DATETIME), $date->getTimezone()); |
| 60 | $date_gmt->setTimezone(new \DateTimeZone('UTC')); |
| 61 | $data['post_date_gmt'] = $date_gmt->format(DateTimeFormats::DB_DATETIME); |
| 62 | } |
| 63 | |
| 64 | $post_id = wp_update_post($data, true); |
| 65 | |
| 66 | if (is_wp_error($post_id)) { |
| 67 | throw new Exception($post_id->get_error_message()); |
| 68 | } |
| 69 | |
| 70 | if (!$post_id) { |
| 71 | throw new Exception(__('Failed to update post.', 'kirki')); |
| 72 | } |
| 73 | |
| 74 | return static::find($post_id); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Update or Create post |
| 79 | * |
| 80 | * @param array $data |
| 81 | * |
| 82 | * @return static |
| 83 | */ |
| 84 | public static function update_or_create_post(array $data) |
| 85 | { |
| 86 | if (!empty($data['ID'])) { |
| 87 | return static::update_post((int) $data['ID'], $data); |
| 88 | } |
| 89 | |
| 90 | return static::create_post($data); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Delete post |
| 95 | * |
| 96 | * @param int $post_id |
| 97 | * @param bool $force_delete |
| 98 | * |
| 99 | * @return bool |
| 100 | */ |
| 101 | public static function delete_post(int $post_id, bool $force_delete = false) |
| 102 | { |
| 103 | $is_deleted = wp_delete_post($post_id, $force_delete); |
| 104 | |
| 105 | return !empty($is_deleted); |
| 106 | } |
| 107 | } |