Skip to content

Commit 448ff64

Browse files
committed
Merge branch 'master' into feature/fields_page_builder
2 parents ae78c04 + c6a96fb commit 448ff64

File tree

5 files changed

+453
-3
lines changed

5 files changed

+453
-3
lines changed

framework/Objects/Post_Type.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use JustCoded\WP\Framework\Web\View;
55
use JustCoded\WP\Framework\Web\Views_Rule;
6+
use JustCoded\WP\Framework\Supports\FakerPress;
67

78
/**
89
* Custom post type class to simplify the process of registering post type.
@@ -239,6 +240,9 @@ protected function __construct() {
239240
}
240241

241242
add_action( 'init', array( $this, 'init' ) );
243+
if ( method_exists( $this, 'faker' ) && is_admin() && FakerPress::check_requirements() ) {
244+
add_action( 'init', array( $this, 'init_faker' ) );
245+
}
242246
add_filter( 'template_include', array( $this, 'views' ), 10 );
243247
}
244248

@@ -333,4 +337,11 @@ public function views( $template ) {
333337

334338
return $template;
335339
}
336-
}
340+
341+
/**
342+
* Init FakerPress.
343+
*/
344+
public function init_faker() {
345+
new FakerPress( $this );
346+
}
347+
}
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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 = 1200, $height = 800, $type = 'id' ) {
187+
$color = substr( md5( microtime( true ), false ), 0, 6 );
188+
$text = $this->faker->words( 2, true );
189+
$attach_url = "http://via.placeholder.com/{$width}x{$height}/$color/?text=$text";
190+
191+
if ( 'id' !== $type ) {
192+
return $attach_url;
193+
}
194+
195+
$response = wp_remote_get( $attach_url, array( 'timeout' => 5 ) );
196+
197+
// Bail early if we have an error.
198+
if ( is_wp_error( $response ) ) {
199+
return false;
200+
}
201+
202+
$bits = wp_remote_retrieve_body( $response );
203+
204+
// Prevent Weird bits.
205+
if ( false === $bits ) {
206+
return false;
207+
}
208+
209+
$filename = Uuid::uuid() . '.jpg';
210+
$upload = wp_upload_bits( $filename, '', $bits );
211+
212+
$attachment = array(
213+
'guid' => $upload['url'],
214+
'post_mime_type' => 'image/jpeg',
215+
'post_title' => $filename,
216+
'post_content' => '',
217+
'post_status' => 'inherit',
218+
);
219+
220+
// Insert the attachment.
221+
$attach_id = wp_insert_attachment( $attachment, $upload['file'], 0 );
222+
223+
return $attach_id;
224+
}
225+
226+
/**
227+
* Get fake number.
228+
*
229+
* @return int
230+
*/
231+
public function percent() {
232+
return $this->faker->numberBetween( 0, 100 );
233+
}
234+
235+
/**
236+
* Generate random number.
237+
*
238+
* @param int $min min value.
239+
* @param int $max max value.
240+
*
241+
* @return int
242+
*/
243+
public function number( $min = 1, $max = 99 ) {
244+
return $this->faker->numberBetween( $min, $max );
245+
}
246+
247+
/**
248+
* Get fake date.
249+
*
250+
* @param string $format Date format.
251+
*
252+
* @return string
253+
*/
254+
public function date( $format = 'Y-m-d' ) {
255+
return $this->faker->date( $format );
256+
}
257+
258+
/**
259+
* Get fake timezone.
260+
*
261+
* @return string
262+
*/
263+
public function timezone() {
264+
return $this->faker->timezone;
265+
}
266+
267+
/**
268+
* Get fake person name.
269+
*
270+
* @return string
271+
*/
272+
public function person() {
273+
return $this->faker->name;
274+
}
275+
276+
/**
277+
* Get fake company name.
278+
*
279+
* @return string
280+
*/
281+
public function company() {
282+
return $this->faker->company;
283+
}
284+
285+
/**
286+
* Get fake job title.
287+
*
288+
* @return string
289+
*/
290+
public function job_title() {
291+
return $this->faker->jobTitle;
292+
}
293+
294+
/**
295+
* Get fake email.
296+
*
297+
* @return string
298+
*/
299+
public function email() {
300+
return $this->faker->safeEmail;
301+
}
302+
303+
}

0 commit comments

Comments
 (0)