| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Actions_V2\Insert_Term; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Action_Handler; |
| 6 |
use JFB_Modules\Actions_V2\Insert_Term\Properties\Abstract_Term_Modifier; |
| 7 |
use JFB_Modules\Actions_V2\Insert_Term\Properties\Term_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_Term_Action extends Base { |
| 21 |
|
| 22 |
public function get_name() { |
| 23 |
return __( 'Insert/Update Term', 'jet-form-builder' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_id() { |
| 27 |
return 'insert_term'; |
| 28 |
} |
| 29 |
|
| 30 |
public function action_attributes() { |
| 31 |
return array( |
| 32 |
'taxonomy' => array( |
| 33 |
'default' => '', |
| 34 |
), |
| 35 |
'fields_map' => array( |
| 36 |
'default' => array(), |
| 37 |
), |
| 38 |
'default_meta' => array( |
| 39 |
'default' => array(), |
| 40 |
), |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param array $request |
| 46 |
* @param Action_Handler $handler |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function do_action( array $request, Action_Handler $handler ) { |
| 51 |
$modifiers = array_reverse( $this->get_modifiers() ); |
| 52 |
|
| 53 |
/** @var Abstract_Term_Modifier $modifier */ |
| 54 |
foreach ( $modifiers as $modifier ) { |
| 55 |
if ( ! $modifier->is_supported( $this ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$modifier->before_run( $this ); |
| 60 |
$modifier->run(); |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function get_taxonomy(): string { |
| 66 |
return $this->settings['taxonomy'] ?? ''; |
| 67 |
} |
| 68 |
|
| 69 |
public static function get_context_term_key( $term_id ) { |
| 70 |
return "term-id-{$term_id}"; |
| 71 |
} |
| 72 |
|
| 73 |
public function editor_labels() { |
| 74 |
return array( |
| 75 |
'taxonomy' => __( 'Taxonomy:', 'jet-form-builder' ), |
| 76 |
'fields_map' => __( 'Fields Map:', 'jet-form-builder' ), |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Regsiter custom action data for the editor |
| 82 |
* |
| 83 |
* @return [type] [description] |
| 84 |
*/ |
| 85 |
public function action_data() { |
| 86 |
$properties = array(); |
| 87 |
$modifiers = $this->get_modifiers(); |
| 88 |
|
| 89 |
foreach ( $modifiers as $modifier ) { |
| 90 |
$properties[ $modifier->get_id() ] = Tools::with_placeholder( |
| 91 |
Array_Tools::to_array( $modifier->properties->all() ) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
return array( |
| 96 |
'taxonomies' => Tools::get_taxonomies_for_modify(), |
| 97 |
'properties' => $properties, |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Just add a new element to the end. |
| 103 |
* When the action is executed, this array is flipped, |
| 104 |
* which puts the \Jet_Form_Builder\Actions\Methods\Post_Modifier at the end. |
| 105 |
* |
| 106 |
* @return Abstract_Post_Modifier[] |
| 107 |
* @since 2.1.4 |
| 108 |
*/ |
| 109 |
public function get_modifiers(): array { |
| 110 |
return apply_filters( |
| 111 |
'jet-form-builder/action/insert-term/modifiers', |
| 112 |
array( |
| 113 |
new Term_Modifier(), |
| 114 |
) |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|