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