| 1 |
<?php |
| 2 |
|
| 3 |
namespace FakerPress\Module; |
| 4 |
|
| 5 |
use FakerPress\ThirdParty\Faker\Generator; |
| 6 |
use FakerPress\ThirdParty\Faker\Provider\Base; |
| 7 |
use function FakerPress\is_truthy; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Abstract_Module. |
| 11 |
* |
| 12 |
* @since 0.6.0 |
| 13 |
* |
| 14 |
* @package FakerPress\Module |
| 15 |
*/ |
| 16 |
abstract class Abstract_Module implements Interface_Module { |
| 17 |
|
| 18 |
/** |
| 19 |
* Stores the Faker generator. |
| 20 |
* |
| 21 |
* @since 0.6.0 |
| 22 |
* |
| 23 |
* @var Generator |
| 24 |
*/ |
| 25 |
protected $faker; |
| 26 |
|
| 27 |
/** |
| 28 |
* Stores the default set of dependencies. |
| 29 |
* |
| 30 |
* @since 0.6.0 |
| 31 |
* |
| 32 |
* @var string[] |
| 33 |
*/ |
| 34 |
protected $dependencies = []; |
| 35 |
|
| 36 |
/** |
| 37 |
* Stores the default provider. |
| 38 |
* |
| 39 |
* @since 0.6.0 |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $provider_class; |
| 44 |
|
| 45 |
/** |
| 46 |
* Stores the generated data. |
| 47 |
* |
| 48 |
* @since 0.6.0 |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $data = []; |
| 53 |
|
| 54 |
/** |
| 55 |
* @inheritDoc |
| 56 |
*/ |
| 57 |
abstract public static function get_slug(): string; |
| 58 |
|
| 59 |
/** |
| 60 |
* @inheritDoc |
| 61 |
*/ |
| 62 |
public static function get_permission_required(): string { |
| 63 |
return 'publish_posts'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @inheritDoc |
| 68 |
*/ |
| 69 |
public static function get_flag(): string { |
| 70 |
$slug = static::get_slug(); |
| 71 |
|
| 72 |
// Default flag value. |
| 73 |
$flag = 'fakerpress_flag'; |
| 74 |
|
| 75 |
/** |
| 76 |
* String holding the flag used to mark items from this module as FakerPress created. |
| 77 |
* |
| 78 |
* @since 0.6.0 |
| 79 |
* |
| 80 |
* @param string $flag Value for the flag. |
| 81 |
* @param string $module_class Which module class we are using. |
| 82 |
*/ |
| 83 |
$flag = apply_filters( "fakerpress.module.flag", $flag, self::class ); |
| 84 |
|
| 85 |
/** |
| 86 |
* String holding the flag used to mark items from this module as FakerPress created. |
| 87 |
* |
| 88 |
* @since 0.6.0 |
| 89 |
* |
| 90 |
* @param string $flag Value for the flag. |
| 91 |
* @param string $module_class Which module class we are using. |
| 92 |
*/ |
| 93 |
$flag = apply_filters( "fakerpress.module.{$slug}.flag", $flag, self::class ); |
| 94 |
|
| 95 |
return $flag; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @inheritDoc |
| 100 |
*/ |
| 101 |
public function get_amount_allowed(): int { |
| 102 |
$slug = static::get_slug(); |
| 103 |
|
| 104 |
return apply_filters( "fakerpress.module.{$slug}.amount_allowed", 15, $this ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @inheritDoc |
| 109 |
*/ |
| 110 |
public function get_faker(): Generator { |
| 111 |
if ( ! $this->faker ) { |
| 112 |
$this->faker = \FakerPress\ThirdParty\Faker\Factory::create(); |
| 113 |
|
| 114 |
// We need to merge the Provider to the Dependencies, so everything is loaded. |
| 115 |
$providers = array_merge( $this->get_dependencies(), (array) $this->get_provider_class() ); |
| 116 |
$providers = array_unique( $providers ); |
| 117 |
$providers = array_filter( $providers ); |
| 118 |
|
| 119 |
foreach ( $providers as $provider_class ) { |
| 120 |
$this->faker->addProvider( new $provider_class( $this->faker ) ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return $this->faker; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Filters the dependencies classes for this module. |
| 129 |
* |
| 130 |
* @since 0.6.0 |
| 131 |
* |
| 132 |
* @param array $dependencies Which default dependencies classes will be used. |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
protected function filter_dependencies( array $dependencies ): array { |
| 137 |
$slug = static::get_slug(); |
| 138 |
|
| 139 |
/** |
| 140 |
* Allows the modification of the default provider class used. |
| 141 |
* |
| 142 |
* @since 0.6.0 |
| 143 |
* |
| 144 |
* @param array $dependencies Which default dependencies classes will be used. |
| 145 |
* @param self $this Module we are currently getting the provider for. |
| 146 |
*/ |
| 147 |
$dependencies = apply_filters( 'fakerpress.module.dependencies', $dependencies, $this ); |
| 148 |
|
| 149 |
/** |
| 150 |
* Allows the modification of the default reset data set for a specific module. |
| 151 |
* |
| 152 |
* @since 0.6.0 |
| 153 |
* |
| 154 |
* @param array $dependencies Which default dependencies classes will be used. |
| 155 |
* @param self $this Module we are currently getting the provider for. |
| 156 |
*/ |
| 157 |
return apply_filters( "fakerpress.module.{$slug}.dependencies", $dependencies, $this ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @inheritDoc |
| 162 |
*/ |
| 163 |
public function get_dependencies(): array { |
| 164 |
return $this->filter_dependencies( $this->dependencies ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Filters the provider class for this module. |
| 169 |
* |
| 170 |
* @since 0.6.0 |
| 171 |
* |
| 172 |
* @param string $provider Which default provider class used. |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
protected function filter_provider_class( string $provider ): string { |
| 177 |
$slug = static::get_slug(); |
| 178 |
|
| 179 |
/** |
| 180 |
* Allows the modification of the default provider class used. |
| 181 |
* |
| 182 |
* @since 0.6.0 |
| 183 |
* |
| 184 |
* @param string $provider Which default provider class used. |
| 185 |
* @param self $this Module we are currently getting the provider for. |
| 186 |
*/ |
| 187 |
$provider = apply_filters( 'fakerpress.module.provider_class', $provider, $this ); |
| 188 |
|
| 189 |
/** |
| 190 |
* Allows the modification of the default reset data set for a specific module. |
| 191 |
* |
| 192 |
* @since 0.6.0 |
| 193 |
* |
| 194 |
* @param string $provider Which default provider class used. |
| 195 |
* @param self $this Module we are currently getting the provider for. |
| 196 |
*/ |
| 197 |
return apply_filters( "fakerpress.module.{$slug}.provider_class", $provider, $this ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @inheritDoc |
| 202 |
*/ |
| 203 |
public function get_provider_class(): string { |
| 204 |
return $this->filter_provider_class( $this->provider_class ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @inheritDoc |
| 209 |
*/ |
| 210 |
abstract public static function delete( $item ); |
| 211 |
|
| 212 |
/** |
| 213 |
* @inheritDoc |
| 214 |
*/ |
| 215 |
abstract public static function fetch( array $args = [] ): array; |
| 216 |
|
| 217 |
/** |
| 218 |
* @inheritDoc |
| 219 |
*/ |
| 220 |
final public function set( $key ): Interface_Module { |
| 221 |
if ( ! is_string( $key ) && ! is_array( $key ) ) { |
| 222 |
return $this; |
| 223 |
} |
| 224 |
|
| 225 |
// Allow a bunch of params |
| 226 |
$arguments = func_get_args(); |
| 227 |
|
| 228 |
/** |
| 229 |
* This allows the following behavior, both will have the same arguments. |
| 230 |
* |
| 231 |
* $module->set( [ 'post_title', 'post_content' ], false, true ); |
| 232 |
*/ |
| 233 |
if ( is_array( $key ) ) { |
| 234 |
// Remove any non string keys. |
| 235 |
$keys = array_filter( $key, 'is_string' ); |
| 236 |
|
| 237 |
foreach ( $keys as $key ) { |
| 238 |
// Set the key. |
| 239 |
$arguments[0] = $key; |
| 240 |
|
| 241 |
// Re-call Set with the string key instead of array. |
| 242 |
$this->set( ...$arguments ); |
| 243 |
} |
| 244 |
|
| 245 |
return $this; |
| 246 |
} |
| 247 |
|
| 248 |
// Remove $key. |
| 249 |
array_shift( $arguments ); |
| 250 |
|
| 251 |
$this->data[ $key ] = (object) [ |
| 252 |
'key' => $key, |
| 253 |
'generator' => $key, |
| 254 |
'arguments' => (array) $arguments, |
| 255 |
]; |
| 256 |
|
| 257 |
return $this; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Modules extending this Abstract should use this module to make sure their data is properly saved as the actual |
| 262 |
* save method is final. |
| 263 |
* |
| 264 |
* @since 0.6.4 |
| 265 |
* |
| 266 |
* @param mixed $response |
| 267 |
* @param array $data |
| 268 |
* @param Abstract_Module $module |
| 269 |
* |
| 270 |
* @return mixed |
| 271 |
*/ |
| 272 |
abstract protected function filter_save_response( $response, array $data, Abstract_Module $module ); |
| 273 |
|
| 274 |
/** |
| 275 |
* @inheritDoc |
| 276 |
*/ |
| 277 |
final public function save( bool $reset = true ) { |
| 278 |
$slug = static::get_slug(); |
| 279 |
|
| 280 |
/** |
| 281 |
* Allows the hooking into before the saving of a module. |
| 282 |
* |
| 283 |
* @since 0.6.0 |
| 284 |
* |
| 285 |
* @param bool $reset Will reset the data in this module after save. |
| 286 |
* @param self $this Module we are currently saving. |
| 287 |
*/ |
| 288 |
do_action( 'fakerpress.module.before_save', $reset, $this ); |
| 289 |
|
| 290 |
/** |
| 291 |
* Allows the hooking into before the saving of a module specifically. |
| 292 |
* |
| 293 |
* @since 0.6.0 |
| 294 |
* |
| 295 |
* @param bool $reset Will reset the data in this module after save. |
| 296 |
* @param self $this Module we are currently saving. |
| 297 |
*/ |
| 298 |
do_action( "fakerpress.module.{$slug}.before_save", $reset, $this ); |
| 299 |
|
| 300 |
$data = []; |
| 301 |
foreach ( $this->data as $key => $item ) { |
| 302 |
if ( is_object( $item ) ) { |
| 303 |
$data[ $item->key ] = $item->value; |
| 304 |
} else { |
| 305 |
$data[ $key ] = $item; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Allows us to prevent `_encloseme` and `_pingme` meta when generating Posts |
| 311 |
* |
| 312 |
* @since 0.4.9 |
| 313 |
* |
| 314 |
* @param bool $prevent_enclose_ping_meta |
| 315 |
*/ |
| 316 |
$prevent_enclose_ping_meta = is_truthy( apply_filters( 'fakerpress.module.generate.prevent_enclose_ping_meta', true ) ); |
| 317 |
|
| 318 |
// This will prevent us having `_encloseme` and `_pingme`. |
| 319 |
if ( $prevent_enclose_ping_meta && ! defined( 'WP_IMPORTING' ) ) { |
| 320 |
define( 'WP_IMPORTING', true ); |
| 321 |
} |
| 322 |
|
| 323 |
$response = $this->filter_save_response( false, $data, $this ); |
| 324 |
|
| 325 |
/** |
| 326 |
* Allows the modification of the response for modules. |
| 327 |
* |
| 328 |
* @since 0.6.0 |
| 329 |
* |
| 330 |
* @param mixed $response The actual response when saving. |
| 331 |
* @param array $data Data generated by this module. |
| 332 |
* @param bool $reset Will reset the data in this module after save. |
| 333 |
* @param self $this Module we are currently saving. |
| 334 |
*/ |
| 335 |
$response = apply_filters( 'fakerpress.module.save', $response, $data, $reset, $this ); |
| 336 |
|
| 337 |
/** |
| 338 |
* Allows the modification of the response for a specific module. |
| 339 |
* |
| 340 |
* @since 0.6.0 |
| 341 |
* |
| 342 |
* @param mixed $response The actual response when saving. |
| 343 |
* @param array $data Data generated by this module. |
| 344 |
* @param bool $reset Will reset the data in this module after save. |
| 345 |
* @param self $this Module we are currently saving. |
| 346 |
*/ |
| 347 |
$response = apply_filters( "fakerpress.module.{$slug}.save", $response, $data, $reset, $this ); |
| 348 |
|
| 349 |
if ( $reset ) { |
| 350 |
$this->reset(); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Allows the hooking into before the saving of a module. |
| 355 |
* |
| 356 |
* @since 0.6.0 |
| 357 |
* |
| 358 |
* @param mixed $response The actual response when saving. |
| 359 |
* @param bool $reset Will reset the data in this module after save. |
| 360 |
* @param self $this Module we are currently saving. |
| 361 |
*/ |
| 362 |
do_action( 'fakerpress.module.after_save', $response, $reset, $this ); |
| 363 |
|
| 364 |
/** |
| 365 |
* Allows the hooking into before the saving of a module specifically. |
| 366 |
* |
| 367 |
* @since 0.6.0 |
| 368 |
* |
| 369 |
* @param mixed $response The actual response when saving. |
| 370 |
* @param bool $reset Will reset the data in this module after save. |
| 371 |
* @param self $this Module we are currently saving. |
| 372 |
*/ |
| 373 |
do_action( "fakerpress.module.{$slug}.after_save", $response, $reset, $this ); |
| 374 |
|
| 375 |
return $response; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* @inheritDoc |
| 380 |
*/ |
| 381 |
public function reset(): Interface_Module { |
| 382 |
$slug = static::get_slug(); |
| 383 |
|
| 384 |
/** |
| 385 |
* Allows the modification of the default reset data set. |
| 386 |
* |
| 387 |
* @since 0.6.0 |
| 388 |
* |
| 389 |
* @param array $data Which data will be applied when reset. |
| 390 |
* @param self $this Module we are currently resetting for. |
| 391 |
*/ |
| 392 |
$data = apply_filters( 'fakerpress.module.reset_data', [], $this ); |
| 393 |
|
| 394 |
/** |
| 395 |
* Allows the modification of the default reset data set for a specific module. |
| 396 |
* |
| 397 |
* @since 0.6.0 |
| 398 |
* |
| 399 |
* @param array $data Which data will be applied when reset. |
| 400 |
* @param self $this Module we are currently resetting for. |
| 401 |
*/ |
| 402 |
$data = apply_filters( "fakerpress.module.{$slug}.reset_data", $data, $this ); |
| 403 |
|
| 404 |
$this->data = $data; |
| 405 |
|
| 406 |
return $this; |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* @inheritDoc |
| 411 |
*/ |
| 412 |
public function generate( bool $force = false ): Interface_Module { |
| 413 |
foreach ( $this->data as $name => $item ) { |
| 414 |
if ( ! $force && $this->has_value( $name ) ) { |
| 415 |
continue; |
| 416 |
} |
| 417 |
|
| 418 |
$this->data[ $name ]->value = $this->apply( $item ); |
| 419 |
} |
| 420 |
|
| 421 |
return $this; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Applies a set of arguments to the generator method of the provider. |
| 426 |
* |
| 427 |
* @since 0.6.0 |
| 428 |
* |
| 429 |
* @param $item |
| 430 |
* |
| 431 |
* @return false|mixed |
| 432 |
*/ |
| 433 |
protected function apply( $item ) { |
| 434 |
// Where there is no generator just return the first argument. |
| 435 |
if ( ! isset( $item->generator ) ) { |
| 436 |
return reset( $item->arguments ); |
| 437 |
} |
| 438 |
|
| 439 |
return call_user_func_array( [ $this->get_faker(), $item->generator ], ( isset( $item->arguments ) ? (array) $item->arguments : [] ) ); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* @inheritDoc |
| 444 |
*/ |
| 445 |
public function get_values(): array { |
| 446 |
$values = []; |
| 447 |
|
| 448 |
foreach ( $this->data as $name => $item ) { |
| 449 |
$values[ $name ] = $this->data[ $name ]->value; |
| 450 |
} |
| 451 |
|
| 452 |
return $values; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* @inheritdoc |
| 457 |
*/ |
| 458 |
public function get( string $key ): ?object { |
| 459 |
return $this->data[ $key ] ?? null; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* @inheritdoc |
| 464 |
*/ |
| 465 |
public function get_value( string $key ) { |
| 466 |
$data = $this->get( $key ); |
| 467 |
return $data->value ?? null; |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* @inheritdoc |
| 472 |
*/ |
| 473 |
public function has_value( string $key ): bool { |
| 474 |
$data = $this->get( $key ); |
| 475 |
return isset( $data->value ); |
| 476 |
} |
| 477 |
} |
| 478 |
|