academylms
2 years ago
advanced-ads
2 years ago
advanced-coupons
2 years ago
advanced-custom-fields
2 years ago
affiliatewp
2 years ago
amelia
2 years ago
asgaros
2 years ago
badgeos
3 years ago
bbpress
3 years ago
beaver-builder
3 years ago
bricks-builder
2 years ago
buddyboss
2 years ago
buddypress
3 years ago
cartflows
3 years ago
contact-form7
2 years ago
convertpro
3 years ago
divi-builder
3 years ago
easy-affiliate
2 years ago
edd
2 years ago
elementor-pro
2 years ago
event-calendar
2 years ago
events-manager
2 years ago
fluent-booking
2 years ago
fluent-form
2 years ago
fluent-support
2 years ago
fluentcrm
2 years ago
formidable-forms
3 years ago
forminator
3 years ago
funnel-kit-automations
3 years ago
gamipress
2 years ago
geo-directory
2 years ago
givewp
2 years ago
gravity-form
2 years ago
gravity-kit
3 years ago
groundhogg
2 years ago
happyforms
3 years ago
jetengine
3 years ago
jetformbuilder
2 years ago
jetpack-crm
3 years ago
kadence-forms
3 years ago
late-point
2 years ago
learndash
2 years ago
learndash-achievements
2 years ago
learnpress
2 years ago
lifterlms
2 years ago
mailpoet
3 years ago
masteriyo
2 years ago
masterstudy-lms
3 years ago
memberpress
2 years ago
memberpress-course
3 years ago
metabox
2 years ago
metform
3 years ago
modern-events-calendar
2 years ago
mycred
2 years ago
newsletter
2 years ago
ninja-forms
3 years ago
paid-memberships-pro
2 years ago
peepso
3 years ago
pie-forms
3 years ago
powerful-docs
2 years ago
presto-player
3 years ago
projecthuddle
3 years ago
raffle-press
2 years ago
restrict-content
2 years ago
sureforms
2 years ago
suremembers
3 years ago
thrive-leads
2 years ago
triggerbutton
3 years ago
tutorlms
2 years ago
ultimate-addons-for-gutenberg
3 years ago
ultimate-member
3 years ago
wishlistmember
2 years ago
woocommerce
2 years ago
woocommerce-bookings
2 years ago
woocommerce-memberships
2 years ago
woocommerce-shipstation
2 years ago
woocommerce-subscriptions
2 years ago
wordpress
2 years ago
wp-all-import
3 years ago
wp-courseware
2 years ago
wp-fusion
2 years ago
wp-job-manager
3 years ago
wp-polls
2 years ago
wp-simple-pay
3 years ago
wp-user-manager
3 years ago
wpforms
3 years ago
wpforo
2 years ago
wplms
3 years ago
ws-form
2 years ago
AutomateAction.php
2 years ago
Integrations.php
3 years ago
AutomateAction.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AutomateAction. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category AutomateAction |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations; |
| 15 | |
| 16 | use Exception; |
| 17 | |
| 18 | /** |
| 19 | * AutomateAction |
| 20 | * |
| 21 | * @category AutomateAction |
| 22 | * @package SureTriggers |
| 23 | * @author BSF <username@example.com> |
| 24 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 25 | * @link https://www.brainstormforce.com/ |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | abstract class AutomateAction { |
| 29 | |
| 30 | /** |
| 31 | * Error data. |
| 32 | * |
| 33 | * @var null |
| 34 | */ |
| 35 | public $data = null; |
| 36 | |
| 37 | /** |
| 38 | * Action name. |
| 39 | * |
| 40 | * @var null|string |
| 41 | */ |
| 42 | public $action = null; |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
| 47 | * @since 1.0.0 |
| 48 | */ |
| 49 | public function __construct() { |
| 50 | |
| 51 | add_filter( 'sure_trigger_register_action', [ $this, 'register' ] ); |
| 52 | add_action( 'sure_trigger_action_' . $this->action, [ $this, 'action_listener' ], 10, 4 ); |
| 53 | |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Register action. |
| 58 | * |
| 59 | * @param array $actions action data. |
| 60 | * @return array |
| 61 | */ |
| 62 | abstract public function register( $actions ); //phpcs:ignore WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.AbstractMethod |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | abstract public function _action_listener( $user_id, $automation_id, $fields, $selected_options); |
| 75 | |
| 76 | /** |
| 77 | * Failed function callback. |
| 78 | * |
| 79 | * @param string $method method name. |
| 80 | * @param array $args method arguments. |
| 81 | * @return false|void |
| 82 | * @throws Exception Excepotion. |
| 83 | */ |
| 84 | public function __call( $method, $args ) { |
| 85 | // Allow support for only action_listener callback. |
| 86 | if ( 'action_listener' !== $method ) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Copy and remove the context from function parameters. |
| 91 | $context = isset( $args[4] ) ? $args[4] : ''; |
| 92 | unset( $args[4] ); |
| 93 | // Convert select field array values in to single value array. |
| 94 | $temp_arr = []; |
| 95 | if ( is_array( $args[3] ) ) { |
| 96 | foreach ( $args[3] as $key => $val ) { |
| 97 | if ( is_array( $val ) ) { |
| 98 | $temp_arr[ $key ] = isset( $val['value'] ) ? $val['value'] : $val; |
| 99 | } else { |
| 100 | $temp_arr[ $key ] = $val; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | $args[3] = $temp_arr; |
| 105 | } |
| 106 | |
| 107 | if ( isset( $args[3]['wp_user_email'] ) ) { |
| 108 | $args[0] = ap_get_user_id_from_email( $args[3]['wp_user_email'] ); |
| 109 | |
| 110 | if ( empty( $args[0] ) ) { |
| 111 | $args[0] = 0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $method = '_' . $method; |
| 116 | try { |
| 117 | $status = $this->$method( ...$args ); |
| 118 | } catch ( Exception $e ) { |
| 119 | throw new Exception( $e->getMessage() ); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | return $status; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check required fields. |
| 128 | * |
| 129 | * @param array $fields template field array. |
| 130 | * @param array $selected_options admin selected option fields. |
| 131 | * @return bool |
| 132 | */ |
| 133 | public function check_required_fields( $fields, $selected_options ) { |
| 134 | foreach ( $fields as $field ) { |
| 135 | if ( 'test-action' === $field['type'] ) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | $fieldname = isset( $selected_options[ $field['name'] ] ) ? $selected_options[ $field['name'] ] : false; |
| 140 | |
| 141 | if ( isset( $field['validationProps'] ) && ( empty( $fieldname ) && '0' !== $fieldname ) ) { |
| 142 | $this->set_error( |
| 143 | [ |
| 144 | 'msg' => __( 'Required field is missing: ', 'suretriggers' ) . $field['name'], |
| 145 | ] |
| 146 | ); |
| 147 | return false; |
| 148 | } |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Set the error before returning the false. |
| 155 | * |
| 156 | * @param array|object|null $data error data. |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function set_error( $data ) { |
| 161 | $this->data = $data; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 |