Completed
Branch master (162e0f)
by Evan
02:29
created

Model::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Silk\Post;
4
5
use stdClass;
6
use WP_Post;
7
use WP_Query;
8
use Illuminate\Support\Collection;
9
use Silk\Query\Builder;
10
use Silk\Query\QueryBuilder;
11
use Silk\Database\ActiveRecord;
12
use Silk\Exception\WP_ErrorException;
13
use Silk\Post\Exception\PostNotFoundException;
14
use Silk\Post\Exception\ModelPostTypeMismatchException;
15
16
/**
17
 * @property-read WP_Post $post
18
 * @property-read int     $id
19
 *
20
 * @property int    $ID
21
 * @property int    $comment_count
22
 * @property string $comment_status
23
 * @property string $filter
24
 * @property string $guid
25
 * @property int    $menu_order
26
 * @property string $ping_status
27
 * @property string $pinged
28
 * @property int    $post_author
29
 * @property string $post_content
30
 * @property string $post_content_filtered
31
 * @property string $post_date
32
 * @property string $post_date_gmt
33
 * @property string $post_excerpt
34
 * @property string $post_mime_type
35
 * @property string $post_modified
36
 * @property string $post_modified_gmt
37
 * @property string $post_name
38
 * @property int    $post_parent
39
 * @property string $post_password
40
 * @property string $post_status
41
 * @property string $post_title
42
 * @property string $post_type
43
 * @property string $to_ping
44
 */
45
abstract class Model extends ActiveRecord
46
{
47
    /**
48
     * The post type of the post this model wraps
49
     * @var string
50
     */
51
    const POST_TYPE = '';
52
53
    /**
54
     * The object type in WordPress
55
     * @var string
56
     */
57
    const OBJECT_TYPE = 'post';
58
59
    /**
60
     * The primary ID property on the object
61
     */
62
    const ID_PROPERTY = 'ID';
63
64
    use QueryBuilder;
65
66
    /**
67
     * Create a new instance
68
     *
69
     * @param WP_Post $post  Post object to model
70
     */
71
    public function __construct(WP_Post $post = null)
72
    {
73
        if (! $post) {
74
            $post = new WP_Post(new stdClass);
75
            $post->post_type = static::postTypeId();
76
        }
77
78
        $this->object = $post;
79
    }
80
81
    /**
82
     * Create a new instance from the given WP_Post object
83
     *
84
     * @param  WP_Post $post
85
     *
86
     * @return static
87
     */
88
    public static function fromWpPost(WP_Post $post)
89
    {
90
        if ($post->post_type !== static::postTypeId()) {
91
            throw new ModelPostTypeMismatchException(static::class, $post);
92
        }
93
94
        return new static($post);
95
    }
96
97
    /**
98
     * Create a new instance from a Post with the given ID
99
     *
100
     * @param  int|string $id  Post ID of post to create the instance from
101
     *
102
     * @return static
103
     */
104
    public static function fromID($id)
105
    {
106
        $post = WP_Post::get_instance($id);
107
108
        if (false === $post) {
109
            throw new PostNotFoundException("No post found with ID {$id}");
110
        }
111
112
        return static::fromWpPost($post);
113
    }
114
115
    /**
116
     * Create a new instance from a Post with the given slug
117
     *
118
     * @param  string $slug  the post slug
119
     *
120
     * @return static
121
     */
122
    public static function fromSlug($slug)
123
    {
124
        $found = static::whereSlug($slug)->limit(1)->results();
125
126
        if ($found->isEmpty()) {
127
            throw new PostNotFoundException("No post found with slug {$slug}");
128
        }
129
130
        return $found->first();
131
    }
132
133
    /**
134
     * Create a new instance from the global $post
135
     *
136
     * @return static
137
     */
138
    public static function fromGlobal()
1 ignored issue
show
Coding Style introduced by
fromGlobal uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
139
    {
140
        if (! $GLOBALS['post'] instanceof WP_Post) {
141
            throw new PostNotFoundException('Global $post not an instance of WP_Post');
142
        }
143
144
        return static::fromWpPost($GLOBALS['post']);
145
    }
146
147
    /**
148
     * Create a new post of the model's type
149
     *
150
     * @param  array $attributes
151
     *
152
     * @return static
153
     */
154
    public static function create($attributes = [])
155
    {
156
        $post = new WP_Post((object)
157
            Collection::make($attributes)
158
                ->except(static::ID_PROPERTY)
159
                ->put('post_type', static::postTypeId())
160
                ->all()
161
        );
162
163
        return static::fromWpPost($post)->save();
164
    }
165
166
    /**
167
     * Get the post type identifier for this model
168
     *
169
     * @return string post type identifier (slug)
170
     */
171
    public static function postTypeId()
172
    {
173
        return static::POST_TYPE;
174
    }
175
176
    /**
177
     * Get the post type API
178
     *
179
     * @return mixed        Loads an existing type as a new PostType,
180
     *                      or returns a new PostTypeBuilder for registering a new type.
181
     */
182
    public static function postType()
183
    {
184
        return PostType::make(static::postTypeId());
185
    }
186
187
    /**
188
     * Send the post to the trash
189
     *
190
     * If trash is disabled, the post or page is permanently deleted.
191
     *
192
     * @return $this
193
     */
194
    public function trash()
195
    {
196
        if (wp_trash_post($this->id)) {
197
            $this->refresh();
198
        }
199
200
        return $this;
201
    }
202
203
    /**
204
     * Restore a post or page from the Trash
205
     *
206
     * @return $this
207
     */
208
    public function untrash()
209
    {
210
        if (wp_untrash_post($this->id)) {
211
            $this->refresh();
212
        }
213
214
        return $this;
215
    }
216
217
    /**
218
     * Get a new query builder for the model.
219
     *
220
     * @return Builder
221
     */
222
    public function newQuery()
223
    {
224
        return (new Builder(new WP_Query))->setModel($this);
225
    }
226
227
    /**
228
     * Get the array of actions and their respective handler classes.
229
     *
230
     * @return array
231
     */
232
    protected function actionClasses()
233
    {
234
        return [
235
            'save'   => Action\PostSaver::class,
236
            'load'   => Action\PostLoader::class,
237
            'delete' => Action\PostDeleter::class,
238
        ];
239
    }
240
}
241