manager.php
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Actions; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use Jet_Form_Builder\Form_Messages\Action_Messages_Manager; |
| 9 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 10 | |
| 11 | if ( ! defined( 'WPINC' ) ) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Define Manager class |
| 17 | */ |
| 18 | class Manager { |
| 19 | |
| 20 | use Repository_Pattern_Trait; |
| 21 | |
| 22 | private $localized_actions = array(); |
| 23 | |
| 24 | public function __construct() { |
| 25 | add_action( 'init', array( $this, 'register_action_types' ), 99 ); |
| 26 | add_action( 'jet-form-builder/editor-assets/before', array( $this, 'register_assets' ) ); |
| 27 | } |
| 28 | |
| 29 | public function rep_instances(): array { |
| 30 | return array(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register allowed action types |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function register_action_types() { |
| 39 | $this->rep_install(); |
| 40 | |
| 41 | do_action( 'jet-form-builder/actions/register', $this ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param $type |
| 46 | * |
| 47 | * @return $this |
| 48 | */ |
| 49 | public function register_action_type( $type ) { |
| 50 | $this->rep_install_item_soft( $type ); |
| 51 | |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param $type |
| 57 | * |
| 58 | * @throws Repository_Exception |
| 59 | */ |
| 60 | public function rep_before_install_item( $type ) { |
| 61 | if ( ! $type->dependence() ) { |
| 62 | $this->_rep_abort_this(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * @param string $type |
| 69 | * |
| 70 | * @return Types\Base|array |
| 71 | */ |
| 72 | public function get_actions( $type = '' ) { |
| 73 | if ( ! $type ) { |
| 74 | return $this->rep_get_items(); |
| 75 | } |
| 76 | |
| 77 | try { |
| 78 | return $this->get_action( $type ); |
| 79 | } catch ( Repository_Exception $exception ) { |
| 80 | return array(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param $type |
| 86 | * |
| 87 | * @return Types\Base |
| 88 | * @throws Repository_Exception |
| 89 | */ |
| 90 | public function get_action( $type ): Types\Base { |
| 91 | return $this->rep_get_item( $type ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param $type |
| 96 | * |
| 97 | * @return Types\Base |
| 98 | * @throws Repository_Exception |
| 99 | */ |
| 100 | public function get_action_clone( $type ): Types\Base { |
| 101 | return $this->rep_clone_item( $type ); |
| 102 | } |
| 103 | |
| 104 | public function has_action_type( $type ): bool { |
| 105 | return $this->rep_isset_item( $type ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param Types\Base[] $source |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | public function prepare_actions_data( $source ) { |
| 114 | $prepared_types = array(); |
| 115 | |
| 116 | foreach ( $source as $type ) { |
| 117 | $type_script_name = $type->self_script_name(); |
| 118 | |
| 119 | $prepared_types[] = array( |
| 120 | 'id' => $type->get_id(), |
| 121 | 'name' => $type->get_name(), |
| 122 | 'disabled' => $type->is_disabled(), |
| 123 | 'self' => $type_script_name, |
| 124 | 'callback' => false, // should be rewritten from JS |
| 125 | ); |
| 126 | $action_localize = $type->action_data(); |
| 127 | |
| 128 | $action_localize['__messages'] = Action_Messages_Manager::instance()->get_messages( $type ); |
| 129 | $action_localize['__labels'] = $type->editor_labels(); |
| 130 | $action_localize['__help_messages'] = $type->editor_labels_help(); |
| 131 | $action_localize['__unsupported_events'] = $type->unsupported_events(); |
| 132 | $action_localize['__supported_events'] = $type->supported_events(); |
| 133 | $action_localize['__global_tab'] = $type->option_name; |
| 134 | |
| 135 | if ( ! empty( $action_localize ) && $type_script_name ) { |
| 136 | $this->localized_actions[ $type->self_script_name() ] = $action_localize; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $prepared_types; |
| 141 | } |
| 142 | |
| 143 | public function register_action_types_assets( $handle ) { |
| 144 | foreach ( $this->localized_actions as $name => $localized_action ) { |
| 145 | wp_localize_script( |
| 146 | $handle, |
| 147 | $name, |
| 148 | $localized_action |
| 149 | ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | public function register_assets() { |
| 154 | $data = self::prepare_actions_data( $this->rep_get_items() ); |
| 155 | |
| 156 | wp_localize_script( |
| 157 | 'jet-fb-components', |
| 158 | 'jetFormActionTypes', |
| 159 | $data |
| 160 | ); |
| 161 | |
| 162 | $this->register_action_types_assets( 'jet-fb-components' ); |
| 163 | } |
| 164 | } |
| 165 |