1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WPB\App; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
|
class Post extends Model |
9
|
|
|
{ |
10
|
|
|
protected $primaryKey = 'ID'; |
11
|
|
|
|
12
|
|
|
protected static $type_scope = 'post'; |
13
|
|
|
protected static $status_scope = 'publish'; |
14
|
|
|
protected static $author_scope = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name of the "created at" column. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const CREATED_AT = 'post_date'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The name of the "updated at" column. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const UPDATED_AT = 'post_modified'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The "booting" method of the model. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
protected static function boot() |
36
|
|
|
{ |
37
|
|
|
parent::boot(); |
38
|
|
|
|
39
|
|
|
// foreach (['PostTypeScope', 'PostStatusScope'] as $scope) { |
40
|
|
|
// $namespace = '\\CodexShaper\\WP\Database\\Eloquent\\Scopes'; |
41
|
|
|
// $defaultPostScopeClass = $namespace.'\\'.$scope; |
42
|
|
|
// static::addGlobalScope(new $defaultPostScopeClass); |
43
|
|
|
// } |
44
|
|
|
|
45
|
|
|
// static::addGlobalScope(new PostTypeScope); |
46
|
|
|
// static::addGlobalScope(new PostStatusScope); |
47
|
|
|
// static::addGlobalScope(new PostAuthorScope); |
48
|
|
|
static::addGlobalScope('type', function (Builder $builder) { |
49
|
|
|
$builder->where('post_type', '=', static::$type_scope); |
50
|
|
|
}); |
51
|
|
|
// static::addGlobalScope('scope', function (Builder $builder) { |
52
|
|
|
// $builder->where('post_status', '=', static::$status_scope); |
53
|
|
|
// }); |
54
|
|
|
// static::addGlobalScope('author', function (Builder $builder) { |
55
|
|
|
// $builder->where('post_author', '=', static::$author_scope); |
56
|
|
|
// }); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// public static function all($columns = ['*']) |
60
|
|
|
// { |
61
|
|
|
// return static::query()->where('post_type', '=', 'post')->get(); |
62
|
|
|
// } |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Filter by post type |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
68
|
|
|
* @param string $status |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
public function scopeType(Builder $builder, $type) |
73
|
|
|
{ |
74
|
|
|
self::$type_scope = $type; |
75
|
|
|
return $builder->where('post_type', '=', $type); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Filter by post status |
80
|
|
|
* |
81
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
82
|
|
|
* @param string $status |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function scopeStatus(Builder $builder, $status) |
87
|
|
|
{ |
88
|
|
|
return $builder->where('post_status', '=', $status); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Filter by post author |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
95
|
|
|
* @param string|null $author |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function scopeAuthor(Builder $builder, $author) |
100
|
|
|
{ |
101
|
|
|
if ($author) { |
102
|
|
|
return $builder->where('post_author', '=', $author); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |