PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / src / FakerPress / Module / Meta.php

Meta.php in FakerPress 0.8.0, at src/FakerPress/Module/Meta.php

205 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FakerPress\Module;
4
5 use FakerPress\ThirdParty\Faker;
6 use FakerPress;
7
8 /**
9 * Meta Module which will generate one Meta Value at a time
10 *
11 * @since 0.3.0
12 *
13 */
14 class Meta extends Abstract_Module {
15
16 /**
17 * Which Faker Dependencies this Module will need
18 *
19 * @since 0.3.0
20 *
21 * @var string[]
22 */
23 protected $dependencies = [
24 FakerPress\ThirdParty\Faker\Provider\Base::class,
25 FakerPress\ThirdParty\Faker\Provider\Lorem::class,
26 FakerPress\ThirdParty\Faker\Provider\DateTime::class,
27 FakerPress\Provider\HTML::class,
28 FakerPress\ThirdParty\Faker\Provider\Internet::class,
29 FakerPress\ThirdParty\Faker\Provider\UserAgent::class,
30 FakerPress\ThirdParty\Faker\Provider\en_US\Company::class,
31 FakerPress\ThirdParty\Faker\Provider\en_US\Address::class,
32 FakerPress\ThirdParty\Faker\Provider\en_US\Person::class,
33 ];
34
35
36 /**
37 * Which Faker Provider class we are using here
38 *
39 * @since 0.3.0
40 *
41 * @var string
42 */
43 protected $provider_class = FakerPress\Provider\WP_Meta::class;
44
45 /**
46 * Which type of object we are saving to
47 *
48 * @since 0.3.0
49 *
50 * @var string
51 */
52 public $object_name = 'post';
53
54 /**
55 * Which object we are saving to
56 *
57 * @since 0.3.0
58 *
59 * @var integer
60 */
61 public $object_id = 0;
62
63 /**
64 * @inheritDoc
65 */
66 public static function get_slug(): string {
67 return 'meta';
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function hook(): void {
74 }
75
76 /**
77 * @inheritDoc
78 */
79 public function reset(): Interface_Module {
80 parent::reset();
81
82 $this->object_id = 0;
83
84 return $this;
85 }
86
87 /**
88 * Configure which Object we will save Meta to
89 *
90 * @since 0.3.0
91 *
92 * @return self
93 */
94 public function object( $id = 0, $name = 'post' ): Interface_Module {
95 $this->object_id = $id;
96 $this->object_name = $name;
97
98 return $this;
99 }
100
101 public function with( string $type, string $name, $args ): Interface_Module {
102 $this->data['meta_key'] = null;
103 $this->data['meta_value'] = null;
104
105 if ( empty( $type ) ) {
106 return $this;
107 }
108
109 if ( empty( $name ) ) {
110 return $this;
111 }
112
113 $this->data['meta_type'] = $type;
114 $this->data['meta_key'] = $name;
115 $this->data['meta_args'] = array_values( $args );
116
117 $faker = $this->get_faker();
118
119 // Pass which object we are dealing with.
120 $faker->set_meta_object( $this->object_name, $this->object_id );
121
122 return $this;
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function generate( bool $force = false ): Interface_Module {
129 // Only regenerate if there is no data, or we are forcing it.
130 if ( isset( $this->data['meta_value'] ) && ! $force ) {
131 return $this;
132 }
133
134 // Failed generation because we have no type.
135 if ( ! isset( $this->data['meta_type'] ) ) {
136 return $this;
137 }
138
139 $type = $this->data['meta_type'];
140 $args = $this->data['meta_args'] ?? [];
141
142 $faker = $this->get_faker();
143
144 if ( is_callable( [ $faker, 'meta_type_' . $type ] ) ) {
145 $this->data['meta_value'] = call_user_func_array( [ $faker, 'meta_type_' . $type ], $args );
146 } elseif ( count( $args ) === 1 ) {
147 $this->data['meta_value'] = reset( $args );
148 }
149
150 /**
151 * Allow filtering for the value for a Meta
152 *
153 * @since 0.4.8
154 *
155 * @param mixed $meta_value The Meta value that will be filtered
156 * @param string $meta_key Which meta key we are currently filtering for
157 * @param string $meta_type Which type of Meta we are dealing with
158 * @param self $module An instance of the Meta Module
159 */
160 $this->data['meta_value'] = apply_filters( "fakerpress.module.meta.value", $this->data['meta_value'], $this->data['meta_key'], $type, $this );
161
162 /**
163 * Allow filtering for the Value of a specific meta value based on it's key
164 *
165 * @since 0.4.8
166 *
167 * @param mixed $meta_value The Meta value that will be filtered
168 * @param string $meta_type Which type of Meta we are dealing with
169 * @param self $module An instance of the Meta Module
170 */
171 $this->data['meta_value'] = apply_filters( "fakerpress.module.meta.{$this->data['meta_key']}.value", $this->data['meta_value'], $type, $this );
172
173 return $this;
174 }
175
176 /**
177 * @inheritDoc
178 */
179 public function filter_save_response( $response, array $data, Abstract_Module $module ) {
180 if ( ! isset( $data['meta_value'] ) ) {
181 return false;
182 }
183
184 if ( empty( $data['meta_key'] ) ) {
185 return false;
186 }
187
188 return update_metadata( $this->object_name, $this->object_id, $data['meta_key'], $data['meta_value'] );
189 }
190
191 /**
192 * @inheritDoc
193 */
194 public static function fetch( array $args = [] ): array {
195 // TODO: Implement fetch() method.
196 }
197
198 /**
199 * @inheritDoc
200 */
201 public static function delete( $item ) {
202 // TODO: Implement delete() method.
203 }
204 }
205