1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The file handle post table. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/maab16 |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace WPB\App; |
10
|
|
|
|
11
|
|
|
use Illuminate\Database\Eloquent\Builder; |
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The post model class. |
16
|
|
|
* |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
* |
19
|
|
|
* @author Md Abu Ahsan basir <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Post extends Model |
22
|
|
|
{ |
23
|
|
|
protected $primaryKey = 'ID'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The post type scope. |
27
|
|
|
* |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
* |
30
|
|
|
* @var string The post type scope. |
31
|
|
|
*/ |
32
|
|
|
protected static $type_scope = 'post'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The post status scope. |
36
|
|
|
* |
37
|
|
|
* @since 1.0.0 |
38
|
|
|
* |
39
|
|
|
* @var string The post status scope. |
40
|
|
|
*/ |
41
|
|
|
protected static $status_scope = 'publish'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The unique identifier of this plugin. |
45
|
|
|
* |
46
|
|
|
* @since 1.0.0 |
47
|
|
|
* |
48
|
|
|
* @var string The author scope.. |
49
|
|
|
*/ |
50
|
|
|
protected static $author_scope = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The constant used to modify default created at fields. |
54
|
|
|
* |
55
|
|
|
* @since 1.0.0 |
56
|
|
|
* |
57
|
|
|
* @var string CREATED_AT The constant used to modify default created at fields. |
58
|
|
|
*/ |
59
|
|
|
const CREATED_AT = 'post_date'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The constant used to modify default updated at fields. |
63
|
|
|
* |
64
|
|
|
* @since 1.0.0 |
65
|
|
|
* |
66
|
|
|
* @var string UPDATED_AT The constant used to modify default updated at fields. |
67
|
|
|
*/ |
68
|
|
|
const UPDATED_AT = 'post_modified'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Boot the model. |
72
|
|
|
* |
73
|
|
|
* @since 1.0.0 |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected static function boot() |
78
|
|
|
{ |
79
|
|
|
parent::boot(); |
80
|
|
|
static::addGlobalScope( |
81
|
|
|
'type', |
82
|
|
|
function (Builder $builder) { |
83
|
|
|
$builder->where('post_type', '=', static::$type_scope); |
84
|
|
|
} |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Filter by post type. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder The eloquent builder. |
92
|
|
|
* @param string $type The post type scope. |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function scopeType(Builder $builder, $type) |
97
|
|
|
{ |
98
|
|
|
self::$type_scope = $type; |
99
|
|
|
|
100
|
|
|
return $builder->where('post_type', '=', $type); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Filter by post status. |
105
|
|
|
* |
106
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder The eloquent builder. |
107
|
|
|
* @param string $status The post status scope. |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function scopeStatus(Builder $builder, $status) |
112
|
|
|
{ |
113
|
|
|
return $builder->where('post_status', '=', $status); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Filter by post author. |
118
|
|
|
* |
119
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder The eloquent builder. |
120
|
|
|
* @param string|null $author The post status scope. |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function scopeAuthor(Builder $builder, $author) |
125
|
|
|
{ |
126
|
|
|
if ($author) { |
127
|
|
|
return $builder->where('post_author', '=', $author); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|