PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.1
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / app / Traits / HasWordPressPostBehavior.php
kirki / app / Traits Last commit date
HasWordPressPostBehavior.php 3 weeks ago SearchablePost.php 3 weeks ago SearchableUser.php 3 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 }