| 1 |
<?php |
| 2 |
/** |
| 3 |
* FakerPress Post Provider |
| 4 |
* |
| 5 |
* @package FakerPress |
| 6 |
* |
| 7 |
* @since TBD |
| 8 |
*/ |
| 9 |
namespace FakerPress\Provider; |
| 10 |
|
| 11 |
use FakerPress\ThirdParty\Faker\Provider\Base; |
| 12 |
use FakerPress\ThirdParty\Cake\Chronos\Chronos; |
| 13 |
use FakerPress\Utils; |
| 14 |
use function FakerPress\make; |
| 15 |
|
| 16 |
/** |
| 17 |
* FakerPress WP_Post Provider |
| 18 |
* |
| 19 |
* @package FakerPress |
| 20 |
* |
| 21 |
* @since TBD |
| 22 |
*/ |
| 23 |
class WP_Post extends Base { |
| 24 |
|
| 25 |
protected static $default = [ |
| 26 |
'ping_status' => [ 'closed', 'open' ], |
| 27 |
'comment_status' => [ 'closed', 'open' ], |
| 28 |
]; |
| 29 |
|
| 30 |
public function post_title( $qty_words = 5 ) { |
| 31 |
$title = $this->generator->sentence( $qty_words ); |
| 32 |
$title = substr( $title, 0, strlen( $title ) - 1 ); |
| 33 |
|
| 34 |
return $title; |
| 35 |
} |
| 36 |
|
| 37 |
public function post_type( $haystack = [] ) { |
| 38 |
if ( empty( $haystack ) ) { |
| 39 |
// Later on we will remove the Attachment rule |
| 40 |
$haystack = array_diff( get_post_types( [ 'public' => true, 'show_ui' => true ], 'names' ), [ 'attachment' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
return $this->generator->randomElement( (array) $haystack ); |
| 44 |
} |
| 45 |
|
| 46 |
public function post_status( $haystack = [ 'draft', 'publish', 'private' ] ) { |
| 47 |
if ( empty( $haystack ) ) { |
| 48 |
$haystack = array_values( get_post_stati() ); |
| 49 |
} |
| 50 |
|
| 51 |
return $this->generator->randomElement( (array) $haystack ); |
| 52 |
} |
| 53 |
|
| 54 |
public function post_date( $interval = 'now' ) { |
| 55 |
$format = 'Y-m-d H:i:s'; |
| 56 |
$interval = (array) $interval; |
| 57 |
|
| 58 |
// Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime |
| 59 |
try { |
| 60 |
$min = new Chronos( array_shift( $interval ) ); |
| 61 |
} catch ( \Exception $e ) { |
| 62 |
$min = new Chronos( 'today' ); |
| 63 |
$min = $min->startOfDay(); |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! empty( $interval ) ) { |
| 67 |
// Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime |
| 68 |
try { |
| 69 |
$max = new Chronos( array_shift( $interval ) ); |
| 70 |
} catch ( \Exception $e ) { |
| 71 |
|
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! isset( $max ) ) { |
| 76 |
$max = new Chronos( 'now' ); |
| 77 |
} |
| 78 |
|
| 79 |
// If max has no Time set it to the end of the day |
| 80 |
$max_has_time = array_filter( [ $max->hour, $max->minute, $max->second ] ); |
| 81 |
$max_has_time = ! empty( $max_has_time ); |
| 82 |
if ( ! $max_has_time ) { |
| 83 |
$max = $max->endOfDay(); |
| 84 |
} |
| 85 |
|
| 86 |
$selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( $format ); |
| 87 |
|
| 88 |
return $selected; |
| 89 |
} |
| 90 |
|
| 91 |
public function post_content( $html = true, $args = [] ) { |
| 92 |
$defaults = [ |
| 93 |
'qty' => [ 5, 15 ], |
| 94 |
]; |
| 95 |
$args = wp_parse_args( $args, $defaults ); |
| 96 |
|
| 97 |
if ( true === $html ) { |
| 98 |
$content = implode( "\n", $this->generator->html_elements( $args ) ); |
| 99 |
} else { |
| 100 |
$content = implode( "\r\n\r\n", $this->generator->paragraphs( make( Utils::class )->get_qty_from_range( $args['qty'] ) ) ); |
| 101 |
} |
| 102 |
|
| 103 |
return $content; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Configures a Excerpt for the Post |
| 108 |
* |
| 109 |
* @since 0.4.9 |
| 110 |
* |
| 111 |
* @param array|int $qty How many words we should generate (range with Array) |
| 112 |
* @param boolean $html Should use HTML or not (currently not used) |
| 113 |
* @param integer $weight Percentage of times where we will setup a Excerpt |
| 114 |
* |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public function post_excerpt( $qty = [ 25, 75 ], $html = false, $weight = 60 ) { |
| 118 |
$words = make( Utils::class )->get_qty_from_range( $qty ); |
| 119 |
$paragraphs = $this->generator->randomElement( [ 1, 1, 1, 1, 1, 2, 2, 2, 3, 4 ] ); |
| 120 |
|
| 121 |
for ( $i = 0; $i < $paragraphs; $i++ ) { |
| 122 |
$excerpt[ $i ] = $this->generator->sentence( $words ); |
| 123 |
} |
| 124 |
|
| 125 |
$excerpt = implode( "\n\n", $excerpt ); |
| 126 |
|
| 127 |
return $this->generator->optional( $weight / 100, '' )->randomElement( (array) $excerpt ); |
| 128 |
} |
| 129 |
|
| 130 |
public function post_author( $haystack = [] ) { |
| 131 |
if ( empty( $haystack ) ) { |
| 132 |
$haystack = get_users( |
| 133 |
[ |
| 134 |
'blog_id' => get_current_blog_id(), |
| 135 |
'count_total' => false, |
| 136 |
'fields' => 'ID', // When you pass only one field it returns an array of the values |
| 137 |
] |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
return $this->generator->randomElement( (array) $haystack ); |
| 142 |
} |
| 143 |
|
| 144 |
public function post_parent( $haystack = [], $weight = 70 ) { |
| 145 |
return $this->generator->optional( $weight / 100, 0 )->randomElement( (array) $haystack ); |
| 146 |
} |
| 147 |
|
| 148 |
public function ping_status( $haystack = [] ) { |
| 149 |
if ( empty( $haystack ) ) { |
| 150 |
$haystack = static::$default['ping_status']; |
| 151 |
} |
| 152 |
|
| 153 |
return $this->generator->randomElement( (array) $haystack ); |
| 154 |
} |
| 155 |
|
| 156 |
public function comment_status( $haystack = [] ) { |
| 157 |
if ( empty( $haystack ) ) { |
| 158 |
$haystack = static::$default['comment_status']; |
| 159 |
} |
| 160 |
|
| 161 |
return $this->generator->randomElement( (array) $haystack ); |
| 162 |
} |
| 163 |
|
| 164 |
public function menu_order( $haystack = [] ) { |
| 165 |
if ( empty( $haystack ) ) { |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
return $this->generator->randomElement( (array) $haystack ); |
| 170 |
} |
| 171 |
|
| 172 |
public function post_password( $generator = null, $args = [] ) { |
| 173 |
if ( is_null( $generator ) ) { |
| 174 |
return ''; |
| 175 |
} |
| 176 |
|
| 177 |
return call_user_func_array( $generator, $args ); |
| 178 |
} |
| 179 |
|
| 180 |
public function tax_input( $config = null ) { |
| 181 |
$output = []; |
| 182 |
if ( is_null( $config ) ) { |
| 183 |
return $output; |
| 184 |
} |
| 185 |
|
| 186 |
// The percentage of change in which the terms will be applied |
| 187 |
$rates = apply_filters( 'fakerpress/provider/WP_Post/tax_input.rates', [ |
| 188 |
'category' => 50, |
| 189 |
'post_tag' => 45, |
| 190 |
'__default' => 35, |
| 191 |
] ); |
| 192 |
|
| 193 |
// The amount of terms that might have, provide a number for exact and [ int, int ] to range |
| 194 |
$ranges = apply_filters( 'fakerpress/provider/WP_Post/tax_input.ranges', [ |
| 195 |
'category' => [ 1, 3 ], |
| 196 |
'post_tag' => [ 0, 15 ], |
| 197 |
'__default' => [ 0, 3 ], |
| 198 |
] ); |
| 199 |
|
| 200 |
foreach ( $config as $settings ) { |
| 201 |
$settings = (object) $settings; |
| 202 |
|
| 203 |
if ( ! empty( $settings->taxonomies ) && is_string( $settings->taxonomies ) ) { |
| 204 |
$settings->taxonomies = explode( ',', $settings->taxonomies ); |
| 205 |
} |
| 206 |
$settings->taxonomies = array_filter( (array) $settings->taxonomies ); |
| 207 |
|
| 208 |
if ( ! empty( $settings->terms ) && is_string( $settings->terms ) ) { |
| 209 |
$settings->terms = explode( ',', $settings->terms ); |
| 210 |
} |
| 211 |
$settings->terms = array_filter( (array) $settings->terms ); |
| 212 |
|
| 213 |
foreach ( $settings->taxonomies as $taxonomy ) { |
| 214 |
if ( empty( $settings->terms ) ) { |
| 215 |
$terms = get_terms( $taxonomy, [ 'fields' => 'ids', 'hide_empty' => false ] ); |
| 216 |
} else { |
| 217 |
$terms = $settings->terms; |
| 218 |
} |
| 219 |
|
| 220 |
// Get all the term ids |
| 221 |
$terms = array_filter( array_map( 'absint', $terms ) ); |
| 222 |
|
| 223 |
if ( ! isset( $settings->qty ) ) { |
| 224 |
$qty = make( Utils::class )->get_qty_from_range( ( isset( $ranges[ $taxonomy ] ) ? $ranges[ $taxonomy ] : $ranges['__default'] ), $terms ); |
| 225 |
} else { |
| 226 |
$qty = (int) make( Utils::class )->get_qty_from_range( $settings->qty, $terms ); |
| 227 |
} |
| 228 |
|
| 229 |
if ( ! isset( $settings->rate ) ) { |
| 230 |
$rate = isset( $rates[ $taxonomy ] ) ? $rates[ $taxonomy ] : $rates['__default']; |
| 231 |
} else { |
| 232 |
$rate = (int) $settings->rate; |
| 233 |
} |
| 234 |
|
| 235 |
// Select the elements based on qty |
| 236 |
$output[ $taxonomy ] = $this->generator->optional( ( (int) $rate ) / 100, null )->randomElements( $terms, (int) $qty ); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return $output; |
| 241 |
} |
| 242 |
} |
| 243 |
|