Skip to content

Commit 523e327

Browse files
committed
Added html generator, fix multiple entry generation duplicating content
1 parent ee4b5a0 commit 523e327

File tree

3 files changed

+91
-29
lines changed

3 files changed

+91
-29
lines changed

framework/Objects/Post_Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,6 @@ public function views( $template ) {
342342
* Init FakerPress.
343343
*/
344344
public function init_faker() {
345-
new FakerPress( $this::$ID, $this );
345+
new FakerPress( $this );
346346
}
347347
}

framework/Supports/FakerContent.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
/**
1111
* Class FakerContent
12-
* Fakerpress plugin extension which allows to generated faker content for custom fields.
12+
* FakerPress plugin extension which allows to generated faker content for custom fields.
13+
*
14+
* @method static FakerContent instance() Singleton design pattern instance creation.
1315
*/
1416
class FakerContent {
1517
use Singleton;
@@ -21,11 +23,28 @@ class FakerContent {
2123
*/
2224
public $faker;
2325

26+
/**
27+
* Faker Press 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+
2437
/**
2538
* FakerContent construct
2639
*/
2740
protected function __construct() {
2841
$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+
}
2948
}
3049

3150
/**
@@ -79,6 +98,26 @@ public function text( $max_chars = 200 ) {
7998
return TextBase::text( $max_chars );
8099
}
81100

101+
102+
/**
103+
* Get fake html.
104+
*
105+
* @param array $content_size Min/max number of paragraphs to generate.
106+
* @param string $html_tags Allowed html tags.
107+
*
108+
* @return string
109+
*/
110+
public function html_text(
111+
$content_size = [ 5, 10 ],
112+
$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.
113+
) {
114+
$tags = $this->faker->html_elements([
115+
'qty' => $content_size,
116+
'elements' => explode( ',', $html_tags ),
117+
]);
118+
return implode( "\n", $tags );
119+
}
120+
82121
/**
83122
* Get fake words.
84123
*
@@ -176,14 +215,23 @@ public function person() {
176215
}
177216

178217
/**
179-
* Get fake name.
218+
* Get fake company name.
180219
*
181220
* @return string
182221
*/
183222
public function company() {
184223
return $this->faker->company;
185224
}
186225

226+
/**
227+
* Get fake job title.
228+
*
229+
* @return string
230+
*/
231+
public function job_title() {
232+
return $this->faker->jobTitle;
233+
}
234+
187235
/**
188236
* Get fake email.
189237
*

framework/Supports/FakerPress.php

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
namespace JustCoded\WP\Framework\Supports;
33

4+
use FakerPress\Module\Post;
5+
use JustCoded\WP\Framework\Objects\Post_Type;
6+
47
/**
58
* Class FakerPress
69
* Fakerpress plugin extension which allows to generated faker content for custom fields.
@@ -12,7 +15,14 @@ class FakerPress {
1215
*
1316
* @var string $ID
1417
*/
15-
public $ID;
18+
protected $ID;
19+
20+
/**
21+
* Post type definition object.
22+
*
23+
* @var Post_Type
24+
*/
25+
public $post_type;
1626

1727
/**
1828
* Post Type faker data.
@@ -24,31 +34,18 @@ class FakerPress {
2434
/**
2535
* FakerContent construct.
2636
*
27-
* @param string $id Post Type ID.
28-
* @param object $post_type Object.
37+
* @param Post_Type $post_type Object.
2938
*/
30-
public function __construct( $id, $post_type ) {
39+
public function __construct( $post_type ) {
3140
if ( $this->do_fakerpress() ) {
32-
$this->ID = $id;
33-
$this->data = $post_type->faker();
41+
$this->ID = $post_type::$ID;
42+
$this->post_type = $post_type;
43+
3444
add_action( 'wp_insert_post_data', array( $this, 'pre_insert_post' ), 20, 2 );
3545
add_action( 'wp_insert_post', array( $this, 'insert_post' ), 10, 3 );
3646
}
3747
}
3848

39-
/**
40-
* Fires once a post has been saved.
41-
*
42-
* @param int $post_id Post ID.
43-
* @param \WP_Post $post Post object.
44-
* @param bool $update Whether this is an existing post being updated or not.
45-
*/
46-
public function insert_post( $post_id, $post, $update ) {
47-
if ( $this->ID === $post->post_type ) {
48-
$this->do_save( $post_id, $this->data );
49-
}
50-
}
51-
5249
/**
5350
* Pre-save post content.
5451
*
@@ -58,19 +55,35 @@ public function insert_post( $post_id, $post, $update ) {
5855
* @return array
5956
*/
6057
public function pre_insert_post( $data, $postarr ) {
58+
$this->data = $this->post_type->faker();
59+
6160
if ( $this->ID === $data['post_type'] ) {
62-
$faker_data = $this->data;
63-
if ( isset( $faker_data['post_title'] ) ) {
64-
$data['post_title'] = $faker_data['post_title'];
61+
if ( isset( $this->data['post_title'] ) ) {
62+
$data['post_title'] = $this->data['post_title'];
63+
unset( $this->data['post_title'] );
6564
}
66-
if ( isset( $faker_data['post_content'] ) ) {
67-
$data['post_content'] = $faker_data['post_content'];
65+
if ( isset( $this->data['post_content'] ) ) {
66+
$data['post_content'] = $this->data['post_content'];
67+
unset( $this->data['post_content'] );
6868
}
6969
}
7070

7171
return $data;
7272
}
7373

74+
/**
75+
* Fires once a post has been saved.
76+
*
77+
* @param int $post_id Post ID.
78+
* @param \WP_Post $post Post object.
79+
* @param bool $update Whether this is an existing post being updated or not.
80+
*/
81+
public function insert_post( $post_id, $post, $update ) {
82+
if ( $this->ID === $post->post_type ) {
83+
$this->update_meta( $post_id, $this->data );
84+
}
85+
}
86+
7487
/**
7588
* Saved faker content.
7689
*
@@ -79,11 +92,12 @@ public function pre_insert_post( $data, $postarr ) {
7992
*
8093
* @return bool
8194
*/
82-
public function do_save( $post_id, $data ) {
95+
public function update_meta( $post_id, $data ) {
8396
if ( isset( $data['post_featured_image'] ) ) {
8497
set_post_thumbnail( $post_id, $data['post_featured_image'] );
98+
unset( $data['post_featured_image'] );
8599
}
86-
unset( $data['post_featured_image'], $data['post_title'], $data['post_content'] );
100+
87101
foreach ( $data as $meta_key => $meta_value ) {
88102
if ( class_exists( 'acf' ) ) {
89103
update_field( $meta_key, $meta_value, $post_id );

0 commit comments

Comments
 (0)