| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Insert_Post; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 6 |
use JFB_Modules\Actions_V2\Insert_Post\Properties\Abstract_Post_Modifier; |
| 7 |
use JFB_Modules\Actions_V2\Insert_Post\Properties\Post_Modifier; |
| 8 |
use Jet_Form_Builder\Actions\Types\Base; |
| 9 |
use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 10 |
use Jet_Form_Builder\Classes\Tools; |
| 11 |
|
| 12 |
// If this file is called directly, abort. |
| 13 |
if ( ! defined( 'WPINC' ) ) { |
| 14 |
die; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Define Base_Type class |
| 19 |
*/ |
| 20 |
class Insert_Post_Action extends Base { |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return __( 'Insert/Update Post', 'jet-form-builder' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_id() { |
| 27 |
return 'insert_post'; |
| 28 |
} |
| 29 |
|
| 30 |
public function action_attributes() { |
| 31 |
return array( |
| 32 |
'post_type' => array( |
| 33 |
'default' => '', |
| 34 |
), |
| 35 |
'post_status' => array( |
| 36 |
'default' => '', |
| 37 |
), |
| 38 |
'fields_map' => array( |
| 39 |
'default' => array(), |
| 40 |
), |
| 41 |
'default_meta' => array( |
| 42 |
'default' => array(), |
| 43 |
), |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param array $request |
| 49 |
* @param Action_Handler $handler |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function do_action( array $request, Action_Handler $handler ) { |
| 54 |
$modifiers = array_reverse( $this->get_modifiers() ); |
| 55 |
|
| 56 |
/** @var Abstract_Post_Modifier $modifier */ |
| 57 |
foreach ( $modifiers as $modifier ) { |
| 58 |
if ( ! $modifier->is_supported( $this ) ) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
$modifier->before_run( $this ); |
| 63 |
$modifier->run(); |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function get_post_type(): string { |
| 69 |
return $this->settings['post_type'] ?? ''; |
| 70 |
} |
| 71 |
|
| 72 |
public function get_inserted_post_context( $post_id = false ) { |
| 73 |
$post_id = $post_id ?: jet_fb_action_handler()->get_inserted_post_id(); |
| 74 |
|
| 75 |
return jet_fb_action_handler()->get_context( |
| 76 |
'insert_post', |
| 77 |
self::get_context_post_key( $post_id ) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public static function get_context_post_key( $post_id ) { |
| 82 |
return "post-id-{$post_id}"; |
| 83 |
} |
| 84 |
|
| 85 |
public function editor_labels() { |
| 86 |
return array( |
| 87 |
'post_type' => __( 'Post Type:', 'jet-form-builder' ), |
| 88 |
'post_status' => __( 'Post Status:', 'jet-form-builder' ), |
| 89 |
'fields_map' => __( 'Fields Map:', 'jet-form-builder' ), |
| 90 |
'default_meta' => __( 'Default Fields:', 'jet-form-builder' ), |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
public function editor_labels_help() { |
| 95 |
return array( |
| 96 |
'fields_map' => __( 'Set meta fields names or post properties to save appropriate form fields into', 'jet-form-builder' ), |
| 97 |
'default_meta' => __( 'Set default meta values which should be set on post insert/update', 'jet-form-builder' ), |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Regsiter custom action data for the editor |
| 103 |
* |
| 104 |
* @return [type] [description] |
| 105 |
*/ |
| 106 |
public function action_data() { |
| 107 |
$properties = array(); |
| 108 |
$modifiers = $this->get_modifiers(); |
| 109 |
|
| 110 |
foreach ( $modifiers as $modifier ) { |
| 111 |
$properties[ $modifier->get_id() ] = Tools::with_placeholder( |
| 112 |
Array_Tools::to_array( $modifier->properties->all() ) |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
return array( |
| 117 |
'postTypes' => Tools::get_post_types_for_js(), |
| 118 |
'taxonomies' => Tools::get_taxonomies_for_modify(), |
| 119 |
'postStatuses' => $this->get_post_statuses_for_options(), |
| 120 |
'properties' => $properties, |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Just add a new element to the end. |
| 126 |
* When the action is executed, this array is flipped, |
| 127 |
* which puts the \Jet_Form_Builder\Actions\Methods\Post_Modifier at the end. |
| 128 |
* |
| 129 |
* @return Abstract_Post_Modifier[] |
| 130 |
* @since 2.1.4 |
| 131 |
*/ |
| 132 |
public function get_modifiers(): array { |
| 133 |
return apply_filters( |
| 134 |
'jet-form-builder/action/insert-post/modifiers', |
| 135 |
array( |
| 136 |
new Post_Modifier(), |
| 137 |
) |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns post statuses list for the options |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public function get_post_statuses_for_options() { |
| 147 |
|
| 148 |
$statuses = get_post_statuses(); |
| 149 |
$result = array(); |
| 150 |
|
| 151 |
foreach ( $statuses as $name => $label ) { |
| 152 |
$result[] = array( |
| 153 |
'value' => $name, |
| 154 |
'label' => $label, |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
$result = array_merge( |
| 159 |
$result, |
| 160 |
array( |
| 161 |
array( |
| 162 |
'value' => 'trash', |
| 163 |
'label' => __( 'Move to Trash', 'jet-form-builder' ), |
| 164 |
), |
| 165 |
array( |
| 166 |
'value' => 'from-field', |
| 167 |
'label' => __( 'Get from the form field', 'jet-form-builder' ), |
| 168 |
), |
| 169 |
array( |
| 170 |
'value' => 'keep-current', |
| 171 |
'label' => __( 'Keep current status (when updating post)', 'jet-form-builder' ), |
| 172 |
), |
| 173 |
) |
| 174 |
); |
| 175 |
|
| 176 |
return Tools::with_placeholder( apply_filters( 'jet-form-builder/actions/insert-post/allowed-post-statuses', $result ) ); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|