| 1 |
<?php |
| 2 |
namespace FakerPress\Module; |
| 3 |
use function FakerPress\make; |
| 4 |
use function FakerPress\get; |
| 5 |
use function FakerPress\get_request_var; |
| 6 |
use FakerPress\Plugin; |
| 7 |
use FakerPress\Utils; |
| 8 |
use FakerPress\ThirdParty\Faker; |
| 9 |
use FakerPress; |
| 10 |
|
| 11 |
class Term extends Abstract_Module { |
| 12 |
/** |
| 13 |
* @inheritDoc |
| 14 |
*/ |
| 15 |
protected $dependencies = [ |
| 16 |
FakerPress\ThirdParty\Faker\Provider\Lorem::class, |
| 17 |
]; |
| 18 |
|
| 19 |
public $meta = false; |
| 20 |
|
| 21 |
/** |
| 22 |
* @inheritDoc |
| 23 |
*/ |
| 24 |
protected $provider_class = FakerPress\Provider\WP_Term::class; |
| 25 |
|
| 26 |
/** |
| 27 |
* @inheritDoc |
| 28 |
*/ |
| 29 |
public static function get_slug(): string { |
| 30 |
return 'terms'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @inheritDoc |
| 35 |
*/ |
| 36 |
public function hook(): void { |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @inheritDoc |
| 42 |
*/ |
| 43 |
public static function fetch( array $args = [] ): array { |
| 44 |
$terms = get_option( 'fakerpress.module_flag.term', [] ); |
| 45 |
|
| 46 |
return $terms; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @inheritDoc |
| 51 |
*/ |
| 52 |
public static function delete( $items ) { |
| 53 |
$deleted = []; |
| 54 |
foreach ( $items as $taxonomy => $terms ){ |
| 55 |
$deleted[ $taxonomy ] = []; |
| 56 |
|
| 57 |
foreach ( $terms as $term ){ |
| 58 |
$deleted[ $taxonomy ][ $term ] = wp_delete_term( $term, $taxonomy ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
delete_option( 'fakerpress.module_flag.term' ); |
| 63 |
|
| 64 |
return $deleted; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @inheritDoc |
| 69 |
*/ |
| 70 |
public function filter_save_response( $response, array $data, Abstract_Module $module ) { |
| 71 |
$args = [ |
| 72 |
'description' => $data['description'], |
| 73 |
'parent' => $data['parent_term'], |
| 74 |
]; |
| 75 |
|
| 76 |
$term_object = wp_insert_term( $data['name'], $data['taxonomy'], $args ); |
| 77 |
if ( is_wp_error( $term_object ) ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
$flagged = get_option( 'fakerpress.module_flag.' . $this::get_slug(), [] ); |
| 82 |
|
| 83 |
// Ensure that this option is an Array by reseting the variable. |
| 84 |
if ( ! is_array( $flagged ) ){ |
| 85 |
$flagged = []; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! isset( $flagged[ $data['taxonomy'] ] ) || ! is_array( $flagged[ $data['taxonomy'] ] ) ){ |
| 89 |
$flagged[ $data['taxonomy'] ] = []; |
| 90 |
} |
| 91 |
$flagged[ $data['taxonomy'] ] = array_merge( $flagged[ $data['taxonomy'] ], (array) $term_object['term_id'] ); |
| 92 |
|
| 93 |
// When in posts relating is harder so we store in the Options Table |
| 94 |
update_option( 'fakerpress.module_flag.' . $this::get_slug(), $flagged ); |
| 95 |
|
| 96 |
return $term_object['term_id']; |
| 97 |
} |
| 98 |
|
| 99 |
public function parse_request( $qty, $request = [] ) { |
| 100 |
if ( is_null( $qty ) ) { |
| 101 |
$qty = make( Utils::class )->get_qty_from_range( get_request_var( [ Plugin::$slug, 'qty' ] ) ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( 0 === $qty ){ |
| 105 |
return esc_attr__( 'Zero is not a good number of terms to fake...', 'fakerpress' ); |
| 106 |
} |
| 107 |
|
| 108 |
$name_size = get_request_var( [ Plugin::$slug, 'size' ] ); |
| 109 |
|
| 110 |
// Fetch taxonomies |
| 111 |
$taxonomies = get( $request, 'taxonomies' ); |
| 112 |
$taxonomies = array_map( 'trim', explode( ',', $taxonomies ) ); |
| 113 |
$taxonomies = array_intersect( get_taxonomies( [ 'public' => true ] ), $taxonomies ); |
| 114 |
|
| 115 |
// Only has meta after 4.4-beta |
| 116 |
$has_metas = version_compare( $GLOBALS['wp_version'], '4.4-beta', '>=' ); |
| 117 |
|
| 118 |
if ( $has_metas ) { |
| 119 |
$metas = get( $request, 'meta' ); |
| 120 |
} |
| 121 |
|
| 122 |
for ( $i = 0; $i < $qty; $i++ ) { |
| 123 |
$this->set( 'taxonomy', $taxonomies ); |
| 124 |
$this->set( 'name', $name_size ); |
| 125 |
$this->set( 'description' ); |
| 126 |
$this->set( 'parent_term' ); |
| 127 |
|
| 128 |
$term_id = $this->generate()->save(); |
| 129 |
|
| 130 |
if ( $has_metas && $term_id && is_numeric( $term_id ) ){ |
| 131 |
foreach ( $metas as $meta_index => $meta ) { |
| 132 |
if ( ! isset( $meta['type'], $meta['name'] ) ) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
$type = get( $meta, 'type' ); |
| 137 |
$name = get( $meta, 'name' ); |
| 138 |
unset( $meta['type'], $meta['name'] ); |
| 139 |
|
| 140 |
if ( isset( $meta['weight'] ) ) { |
| 141 |
$meta['weight'] = absint( $meta['weight'] ); |
| 142 |
$meta['weight'] = $meta['weight'] > 0 ? $meta['weight'] : 100; |
| 143 |
} else { |
| 144 |
$meta['weight'] = 100; |
| 145 |
} |
| 146 |
|
| 147 |
make( Meta::class )->object( $term_id, 'term' )->with( $type, $name, $meta )->generate()->save(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$results[] = $term_id; |
| 152 |
} |
| 153 |
|
| 154 |
$results = array_filter( (array) $results, 'absint' ); |
| 155 |
|
| 156 |
return $results; |
| 157 |
} |
| 158 |
} |
| 159 |
|