|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustCoded\WP\Framework\Supports; |
| 4 | + |
| 5 | +use Faker\Provider\Uuid; |
| 6 | +use Faker\Provider\TextBase; |
| 7 | +use Faker\Factory; |
| 8 | +use JustCoded\WP\Framework\Objects\Singleton; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class FakerContent |
| 12 | + * FakerPress plugin extension which allows to generated faker content for custom fields. |
| 13 | + * |
| 14 | + * @method static FakerContent instance() Singleton design pattern instance creation. |
| 15 | + */ |
| 16 | +class FakerContent { |
| 17 | + use Singleton; |
| 18 | + |
| 19 | + /** |
| 20 | + * Faker object |
| 21 | + * |
| 22 | + * @var $faker |
| 23 | + */ |
| 24 | + public $faker; |
| 25 | + |
| 26 | + /** |
| 27 | + * FakerPress plugin custom providers |
| 28 | + * |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + public $providers = array( |
| 32 | + '\Faker\Provider\Lorem', |
| 33 | + '\Faker\Provider\DateTime', |
| 34 | + '\Faker\Provider\HTML', |
| 35 | + ); |
| 36 | + |
| 37 | + /** |
| 38 | + * FakerContent construct |
| 39 | + */ |
| 40 | + protected function __construct() { |
| 41 | + $this->faker = Factory::create(); |
| 42 | + |
| 43 | + // register custom providers. |
| 44 | + foreach ( $this->providers as $provider_class ) { |
| 45 | + $provider = new $provider_class( $this->faker ); |
| 46 | + $this->faker->addProvider( $provider ); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Generated array for flexible content. |
| 52 | + * |
| 53 | + * @param array $data Flexible content data. |
| 54 | + * |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + public function flexible_content( $data = array() ) { |
| 58 | + if ( empty( $data ) || ! class_exists( 'acf' ) ) { |
| 59 | + return array(); |
| 60 | + } |
| 61 | + |
| 62 | + return $data; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Prepare flexible layout data array |
| 67 | + * |
| 68 | + * @param string $layout_name Flexible content layout name. |
| 69 | + * @param array $data Layout fields data. |
| 70 | + * |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + public function flexible_layout( $layout_name, $data ) { |
| 74 | + return array_merge( $data, array( 'acf_fc_layout' => $layout_name ) ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Generated array for repeater fields. |
| 79 | + * |
| 80 | + * @param int|int[] $qty Min/max qty to generate. |
| 81 | + * @param callable $callback Repeater fields data generator callback. |
| 82 | + * |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function repeater( $qty, $callback ) { |
| 86 | + // validate qty. |
| 87 | + $qty = $this->normalize_qty( $qty ); |
| 88 | + if ( $qty[0] < 1 ) { |
| 89 | + return array(); |
| 90 | + } |
| 91 | + |
| 92 | + // generate data. |
| 93 | + $data = array(); |
| 94 | + $num = $this->faker->numberBetween( $qty[0], $qty[1] ); |
| 95 | + for ( $i = 0; $i < $num; $i++ ) { |
| 96 | + $data[] = $callback(); |
| 97 | + } |
| 98 | + |
| 99 | + return $data; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Normalize qty to standard range array. |
| 104 | + * |
| 105 | + * @param int|int[] $qty Number or range. |
| 106 | + * |
| 107 | + * @return array|bool |
| 108 | + */ |
| 109 | + protected function normalize_qty( $qty ) { |
| 110 | + if ( ! is_array( $qty ) ) { |
| 111 | + $qty = array( $qty ); |
| 112 | + } |
| 113 | + if ( count( $qty ) < 2 && (int) $qty[1] < (int) $qty[0] ) { |
| 114 | + $qty[1] = $qty[0]; |
| 115 | + } |
| 116 | + $qty[0] = (int) $qty[0]; |
| 117 | + $qty[1] = (int) $qty[1]; |
| 118 | + |
| 119 | + if ( $qty[0] < 1 ) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + return [ |
| 124 | + $qty[0], |
| 125 | + $qty[1], |
| 126 | + ]; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Get fake text. |
| 131 | + * |
| 132 | + * @param int $max_chars Chars number. |
| 133 | + * |
| 134 | + * @return string |
| 135 | + */ |
| 136 | + public function text( $max_chars = 200 ) { |
| 137 | + return TextBase::text( $max_chars ); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * Get fake html. |
| 143 | + * |
| 144 | + * @param array $content_size Min/max number of paragraphs to generate. |
| 145 | + * @param string $html_tags Allowed html tags. |
| 146 | + * |
| 147 | + * @return string |
| 148 | + */ |
| 149 | + public function html_text( |
| 150 | + $content_size = [ 5, 10 ], |
| 151 | + $html_tags = 'p,p,p,p,p,p,p,h2,h3,h4,h5,h6,ul,ol,p,blockquote,img,hr,b,i,a' // increase "p" possibility. |
| 152 | + ) { |
| 153 | + $tags = $this->faker->html_elements([ |
| 154 | + 'qty' => $content_size, |
| 155 | + 'elements' => explode( ',', $html_tags ), |
| 156 | + ]); |
| 157 | + return implode( "\n", $tags ); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Get fake words. |
| 162 | + * |
| 163 | + * @param int|int[] $qty Words number or range. |
| 164 | + * |
| 165 | + * @return string |
| 166 | + */ |
| 167 | + public function words( $qty = 3 ) { |
| 168 | + $qty = $this->normalize_qty( $qty ); |
| 169 | + if ( $qty[0] < 1 ) { |
| 170 | + return ''; |
| 171 | + } |
| 172 | + |
| 173 | + $nb = $this->faker->numberBetween( $qty[0], $qty[1] ); |
| 174 | + return $this->faker->words( $nb, true ); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Generated and save attachment file. |
| 179 | + * |
| 180 | + * @param int $width Attachment width. |
| 181 | + * @param int $height Attachment height. |
| 182 | + * @param string $type Attachment type( id, url ). |
| 183 | + * |
| 184 | + * @return int|string |
| 185 | + */ |
| 186 | + public function image_attachment( $width = 1100, $height = 800, $type = 'id' ) { |
| 187 | + $color = substr( md5( microtime( true ), false ), 0, 6 ); |
| 188 | + $attach_url = "http://via.placeholder.com/{$width}x{$height}/$color"; |
| 189 | + |
| 190 | + if ( 'id' !== $type ) { |
| 191 | + return $attach_url; |
| 192 | + } |
| 193 | + |
| 194 | + $response = wp_remote_get( $attach_url, array( 'timeout' => 5 ) ); |
| 195 | + |
| 196 | + // Bail early if we have an error. |
| 197 | + if ( is_wp_error( $response ) ) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + $bits = wp_remote_retrieve_body( $response ); |
| 202 | + |
| 203 | + // Prevent Weird bits. |
| 204 | + if ( false === $bits ) { |
| 205 | + return false; |
| 206 | + } |
| 207 | + |
| 208 | + $filename = Uuid::uuid() . '.jpg'; |
| 209 | + $upload = wp_upload_bits( $filename, '', $bits ); |
| 210 | + |
| 211 | + $attachment = array( |
| 212 | + 'guid' => $upload['url'], |
| 213 | + 'post_mime_type' => 'image/jpeg', |
| 214 | + 'post_title' => $filename, |
| 215 | + 'post_content' => '', |
| 216 | + 'post_status' => 'inherit', |
| 217 | + ); |
| 218 | + |
| 219 | + // Insert the attachment. |
| 220 | + $attach_id = wp_insert_attachment( $attachment, $upload['file'], 0 ); |
| 221 | + |
| 222 | + return $attach_id; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Get fake number. |
| 227 | + * |
| 228 | + * @return int |
| 229 | + */ |
| 230 | + public function percent() { |
| 231 | + return $this->faker->numberBetween( 0, 100 ); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Generate random number. |
| 236 | + * |
| 237 | + * @param int $min min value. |
| 238 | + * @param int $max max value. |
| 239 | + * |
| 240 | + * @return int |
| 241 | + */ |
| 242 | + public function number( $min = 1, $max = 99 ) { |
| 243 | + return $this->faker->numberBetween( $min, $max ); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Get fake date. |
| 248 | + * |
| 249 | + * @param string $format Date format. |
| 250 | + * |
| 251 | + * @return string |
| 252 | + */ |
| 253 | + public function date( $format = 'Y-m-d' ) { |
| 254 | + return $this->faker->date( $format ); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Get fake timezone. |
| 259 | + * |
| 260 | + * @return string |
| 261 | + */ |
| 262 | + public function timezone() { |
| 263 | + return $this->faker->timezone; |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Get fake person name. |
| 268 | + * |
| 269 | + * @return string |
| 270 | + */ |
| 271 | + public function person() { |
| 272 | + return $this->faker->name; |
| 273 | + } |
| 274 | + |
| 275 | + /** |
| 276 | + * Get fake company name. |
| 277 | + * |
| 278 | + * @return string |
| 279 | + */ |
| 280 | + public function company() { |
| 281 | + return $this->faker->company; |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Get fake job title. |
| 286 | + * |
| 287 | + * @return string |
| 288 | + */ |
| 289 | + public function job_title() { |
| 290 | + return $this->faker->jobTitle; |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * Get fake email. |
| 295 | + * |
| 296 | + * @return string |
| 297 | + */ |
| 298 | + public function email() { |
| 299 | + return $this->faker->safeEmail; |
| 300 | + } |
| 301 | + |
| 302 | +} |
0 commit comments