post-modifier-core.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Actions\Methods\Post_Modification; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Actions\Methods\Abstract_Modifier; |
| 8 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 9 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 10 | |
| 11 | abstract class Post_Modifier_Core extends Abstract_Modifier { |
| 12 | |
| 13 | /** @var int|\WP_Error */ |
| 14 | public $inserted_post_id; |
| 15 | |
| 16 | public $suppress_filters = true; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * @param bool $suppress |
| 21 | * |
| 22 | * @return $this |
| 23 | */ |
| 24 | public function suppress_filters( bool $suppress ) { |
| 25 | $this->suppress_filters = $suppress; |
| 26 | |
| 27 | return $this; |
| 28 | } |
| 29 | |
| 30 | public function get_action() { |
| 31 | if ( is_null( $this->action ) ) { |
| 32 | $this->action = 'insert'; |
| 33 | } |
| 34 | |
| 35 | return parent::get_action(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param $post_type |
| 40 | * |
| 41 | * @return Post_Modifier_Core |
| 42 | * @throws Action_Exception |
| 43 | */ |
| 44 | public function set_post_type( $post_type ) { |
| 45 | if ( $post_type && post_type_exists( $post_type ) ) { |
| 46 | $this->source_arr['post_type'] = $post_type; |
| 47 | |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | throw new Action_Exception( |
| 52 | 'failed', |
| 53 | array( |
| 54 | 'post_type' => $post_type, |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param string $action |
| 61 | * |
| 62 | * @return Post_Modifier_Core |
| 63 | * @throws Silence_Exception |
| 64 | */ |
| 65 | public function set_action_once( string $action ) { |
| 66 | if ( 'insert' !== $this->get_action() ) { |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | return $this->set_action( $action ); |
| 71 | } |
| 72 | |
| 73 | public function set_meta( $meta ) { |
| 74 | if ( empty( $this->source_arr['meta_input'] ) || ! is_array( $this->source_arr['meta_input'] ) ) { |
| 75 | $this->source_arr['meta_input'] = array(); |
| 76 | } |
| 77 | |
| 78 | foreach ( $meta as $meta_key => $meta_row ) { |
| 79 | if ( ! empty( $meta_row['key'] ) ) { |
| 80 | $meta[ $meta_row['key'] ] = $meta_row['value']; |
| 81 | unset( $meta[ $meta_key ] ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | $this->source_arr['meta_input'] = array_merge( $this->source_arr['meta_input'], $meta ); |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * @throws Silence_Exception |
| 94 | */ |
| 95 | public function pre_check() { |
| 96 | if ( $this->suppress_filters ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $pre_post_check = apply_filters( |
| 101 | 'jet-form-builder/action/insert-post/pre-check', |
| 102 | true, |
| 103 | $this->source_arr, |
| 104 | jet_form_builder()->form_handler->action_handler->get_current_action() |
| 105 | ); |
| 106 | |
| 107 | if ( ! $pre_post_check ) { |
| 108 | throw new Silence_Exception( 'Pre check filter has returned FALSE.' ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | } |