11<?php
22namespace 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