types
4 years ago
action-handler.php
4 years ago
action-localize.php
4 years ago
manager.php
4 years ago
manager.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Actions; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | |
| 7 | use Jet_Form_Builder\Actions\Types; |
| 8 | use Jet_Form_Builder\Admin\Editor; |
| 9 | |
| 10 | if ( ! defined( 'WPINC' ) ) { |
| 11 | die; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Define Manager class |
| 16 | */ |
| 17 | class Manager { |
| 18 | |
| 19 | private $_types = array(); |
| 20 | private $localized_actions = array(); |
| 21 | |
| 22 | const ENGINE_HANDLE = 'jet-fb-action-localize-helper'; |
| 23 | |
| 24 | public function __construct() { |
| 25 | add_action( 'init', array( $this, 'register_action_types' ), 99 ); |
| 26 | add_action( 'jet-form-builder/editor-package/before', array( $this, 'register_assets' ), 10, 2 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Register allowed action types |
| 31 | * |
| 32 | * @return [type] [description] |
| 33 | */ |
| 34 | public function register_action_types() { |
| 35 | |
| 36 | $actions = array( |
| 37 | new Types\Send_Email(), |
| 38 | new Types\Insert_Post(), |
| 39 | new Types\Register_User(), |
| 40 | new Types\Update_User(), |
| 41 | new Types\Update_Options(), |
| 42 | new Types\Call_Hook(), |
| 43 | new Types\Call_Webhook(), |
| 44 | new Types\Redirect_To_Page(), |
| 45 | new Types\Mailchimp(), |
| 46 | new Types\Getresponse(), |
| 47 | new Types\Active_Campaign() |
| 48 | ); |
| 49 | |
| 50 | foreach ( $actions as $action ) { |
| 51 | $this->register_action_type( $action ); |
| 52 | } |
| 53 | |
| 54 | do_action( 'jet-form-builder/actions/register', $this ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register new action type |
| 59 | * |
| 60 | * @param Types\Base $type |
| 61 | * |
| 62 | * @return void [description] |
| 63 | */ |
| 64 | public function register_action_type( Types\Base $type ) { |
| 65 | if ( $type->dependence() ) { |
| 66 | $this->_types[ $type->get_id() ] = $type; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param string $type |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function get_actions( $type = '' ) { |
| 76 | if ( $type ) { |
| 77 | return $this->_types[ $type ]; |
| 78 | } |
| 79 | |
| 80 | return $this->_types; |
| 81 | } |
| 82 | |
| 83 | public function has_action_type( $type ) { |
| 84 | return isset( $this->_types[ $type ] ); |
| 85 | } |
| 86 | |
| 87 | public function prepare_actions_data( $source ) { |
| 88 | $prepared_types = array(); |
| 89 | |
| 90 | foreach ( $source as $type ) { |
| 91 | |
| 92 | $type_script_name = $type->self_script_name(); |
| 93 | |
| 94 | $prepared_types[] = array( |
| 95 | 'id' => $type->get_id(), |
| 96 | 'name' => $type->get_name(), |
| 97 | 'self' => $type_script_name, |
| 98 | 'callback' => false, // should be rewritten from JS |
| 99 | ); |
| 100 | $action_localize = $type->action_data(); |
| 101 | |
| 102 | $action_localize['__messages'] = $type->get_messages_default(); |
| 103 | $action_localize['__labels'] = $type->editor_labels(); |
| 104 | $action_localize['__help_messages'] = $type->editor_labels_help(); |
| 105 | $action_localize['__gateway_attrs'] = $type->visible_attributes_for_gateway_editor(); |
| 106 | |
| 107 | if ( ! empty( $action_localize ) && $type_script_name ) { |
| 108 | $this->localized_actions[ $type->self_script_name() ] = $action_localize; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $prepared_types; |
| 113 | } |
| 114 | |
| 115 | public function register_action_types_assets( $handle ) { |
| 116 | foreach ( $this->localized_actions as $name => $localized_action ) { |
| 117 | wp_localize_script( |
| 118 | $handle, |
| 119 | $name, |
| 120 | $localized_action |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public function register_assets( $editor, $handle ) { |
| 126 | wp_enqueue_script( |
| 127 | self::ENGINE_HANDLE, |
| 128 | JET_FORM_BUILDER_URL . 'assets/js/action-localize-helper.js', |
| 129 | array(), |
| 130 | JET_FORM_BUILDER_VERSION |
| 131 | ); |
| 132 | $data = self::prepare_actions_data( $this->_types ); |
| 133 | |
| 134 | wp_localize_script( |
| 135 | self::ENGINE_HANDLE, |
| 136 | 'jetFormActionTypes', |
| 137 | $data |
| 138 | ); |
| 139 | |
| 140 | $this->register_action_types_assets( self::ENGINE_HANDLE ); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | } |
| 145 |