| 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 |
} |
| 149 |
|