Meta.php
4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace FakerPress\Module;
use FakerPress\Admin;
use FakerPress\Variable;
use FakerPress\Plugin;
use Faker;
use FakerPress;
/**
* Meta Module which will generate one Meta Value at a time
*
* @since 0.3.0
*
*/
class Meta extends Base {
/**
* Which Faker Dependencies this Module will need
*
* @since 0.3.0
*
* @var array
*/
public $dependencies = [
Faker\Provider\Base::class,
Faker\Provider\Lorem::class,
Faker\Provider\DateTime::class,
FakerPress\Provider\HTML::class,
Faker\Provider\Internet::class,
Faker\Provider\UserAgent::class,
Faker\Provider\en_US\Company::class,
Faker\Provider\en_US\Address::class,
Faker\Provider\en_US\Person::class,
];
/**
* Which Faker Provider class we are using here
*
* @since 0.3.0
*
* @var string
*/
public $provider = FakerPress\Provider\WP_Meta::class;
/**
* Wether or not FakerPress will generate a page for this
*
* @since 0.3.0
*
* @var boolean
*/
public $page = false;
/**
* Which type of object we are saving to
*
* @since 0.3.0
*
* @var string
*/
public $object_name = 'post';
/**
* Which object we are saving to
*
* @since 0.3.0
*
* @var integer
*/
public $object_id = 0;
/**
* Initalize and Add the correct hooks into the Meta Module
*
* @since 0.3.0
*
* @return void
*/
public function init() {
add_filter( "fakerpress.module.{$this->slug}.save", [ $this, 'do_save' ], 10, 3 );
}
/**
* Resets which original object that we will save the meta to
*
* @since 0.3.0
*
* @return self
*/
public function reset() {
parent::reset();
$this->object_id = 0;
return $this;
}
/**
* Configure which Object we will save Meta to
*
* @since 0.3.0
*
* @return self
*/
public function object( $id = 0, $name = 'post' ) {
$this->object_id = $id;
$this->object_name = $name;
return $this;
}
/**
* Generate the meta based on the Params given
*
* @since 0.3.0
*
* @param string $type Type of Meta we are dealing with
* @param string $name Name of the Meta, used to save
* @param array $args Arguments used to setup the Meta
*
* @return self
*/
public function generate() {
// Allow a bunch of params
$arguments = func_get_args();
// Remove $key and $name
$type = array_shift( $arguments );
$name = array_shift( $arguments );
$args = array_shift( $arguments );
$this->data['meta_key'] = null;
$this->data['meta_value'] = null;
if ( empty( $type ) ) {
return $this;
}
if ( empty( $name ) ) {
return $this;
}
$this->data['meta_key'] = $name;
unset( $args['name'], $args['type'] );
// Pass which object we are dealing with
$this->faker->set_meta_object( $this->object_name, $this->object_id );
if ( is_callable( [ $this->faker, 'meta_type_' . $type ] ) ) {
$this->data['meta_value'] = call_user_func_array( [ $this->faker, 'meta_type_' . $type ], array_values( $args ) );
} else {
$this->data['meta_value'] = reset( $args );
}
/**
* Allow filtering for the value for a Meta
*
* @since 0.4.8
*
* @param mixed $meta_value The Meta value that will be filtered
* @param string $meta_key Which meta key we are currently filtering for
* @param string $meta_type Which type of Meta we are dealing with
* @param self $module An instance of the Meta Module
*/
$this->data['meta_value'] = apply_filters( "fakerpress.module.meta.value", $this->data['meta_value'], $this->data['meta_key'], $type, $this );
/**
* Allow filtering for the Value of a specific meta value based on it's key
*
* @since 0.4.8
*
* @param mixed $meta_value The Meta value that will be filtered
* @param string $meta_type Which type of Meta we are dealing with
* @param self $module An instance of the Meta Module
*/
$this->data['meta_value'] = apply_filters( "fakerpress.module.meta.{$this->data['meta_key']}.value", $this->data['meta_value'], $type, $this );
return $this;
}
/**
* Actually save the meta value into the Database
*
* @since 0.3.0
*
* @param string $return_val Unsed variable that comes from the hook
* @param string $data Data generated, meta_key and meta_value
* @param array $module Arguments used to setup the Meta
*
* @return self
*/
public function do_save( $return_val, $data, $module ) {
$status = false;
if ( ! isset( $data['meta_value'] ) ) {
return false;
}
if ( empty( $data['meta_key'] ) ) {
return false;
}
if ( ! is_null( $data['meta_value'] ) ) {
$status = update_metadata( $this->object_name, $this->object_id, $data['meta_key'], $data['meta_value'] );
}
return $status;
}
}