← All changes
|
integrations/automatorwp/actions/call-function.php
+209
-0
5.7.9.1
→
6.0.4
View file →
| @@ -1,0 +1,209 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Call Function | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Actions\Call_Function | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_WordPress_Call_Function extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Initialize the trigger | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + */ | |
| 19 | + public function __construct( $integration ) { | |
| 20 | + | |
| 21 | + $this->integration = $integration; | |
| 22 | + $this->action = $integration . '_call_function'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Register the trigger | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + */ | |
| 33 | + public function register() { | |
| 34 | + | |
| 35 | + automatorwp_register_action( $this->action, array( | |
| 36 | + 'integration' => $this->integration, | |
| 37 | + 'label' => __( 'Call a function', 'automatorwp' ), | |
| 38 | + 'select_option' => __( 'Call a <strong>function</strong>', 'automatorwp' ), | |
| 39 | + /* translators: %1$s: Function name. */ | |
| 40 | + 'edit_label' => sprintf( __( 'Call %1$s', 'automatorwp' ), '{function}' ), | |
| 41 | + /* translators: %1$s: Function name */ | |
| 42 | + 'log_label' => sprintf( __( 'Call %1$s', 'automatorwp' ), '{function}' ), | |
| 43 | + 'options' => array( | |
| 44 | + 'function' => array( | |
| 45 | + 'from' => 'function_name', | |
| 46 | + 'default' => __( 'a function', 'automatorwp' ), | |
| 47 | + 'fields' => array( | |
| 48 | + 'function_name' => array( | |
| 49 | + 'name' => __( 'Function name:', 'automatorwp' ), | |
| 50 | + 'desc' => __( 'The function to call.', 'automatorwp' ), | |
| 51 | + 'type' => 'text', | |
| 52 | + 'default' => '' | |
| 53 | + ), | |
| 54 | + 'function_args' => array( | |
| 55 | + 'name' => __( 'Variables:', 'automatorwp' ), | |
| 56 | + 'desc' => __( 'The variables to pass to the function.', 'automatorwp' ), | |
| 57 | + 'type' => 'group', | |
| 58 | + 'classes' => 'automatorwp-fields-table automatorwp-fields-table-col-1', | |
| 59 | + 'options' => array( | |
| 60 | + 'add_button' => __( 'Add variable', 'automatorwp' ), | |
| 61 | + 'remove_button' => '<span class="dashicons dashicons-no-alt"></span>', | |
| 62 | + ), | |
| 63 | + 'fields' => array( | |
| 64 | + 'value' => array( | |
| 65 | + 'name' => __( 'Value:', 'automatorwp' ), | |
| 66 | + 'type' => 'text', | |
| 67 | + 'default' => '' | |
| 68 | + ), | |
| 69 | + ), | |
| 70 | + ), | |
| 71 | + ) | |
| 72 | + ) | |
| 73 | + ), | |
| 74 | + ) ); | |
| 75 | + | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Action execution function | |
| 80 | + * | |
| 81 | + * @since 1.0.0 | |
| 82 | + * | |
| 83 | + * @param stdClass $action The action object | |
| 84 | + * @param int $user_id The user ID | |
| 85 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 86 | + * @param stdClass $automation The action's automation object | |
| 87 | + */ | |
| 88 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 89 | + | |
| 90 | + // Shorthand | |
| 91 | + $function_name = $action_options['function_name']; | |
| 92 | + $function_args = $action_options['function_args']; | |
| 93 | + | |
| 94 | + // Bail if empty meta key | |
| 95 | + if( empty( $function_name ) ) { | |
| 96 | + $this->result = __( 'Empty function name.', 'automatorwp' ); | |
| 97 | + return; | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Bail if empty meta key | |
| 101 | + if( ! function_exists( $function_name ) ) { | |
| 102 | + $this->result = sprintf( __( 'Function "%s" not found.', 'automatorwp' ), $function_name ); | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + | |
| 106 | + if( ! is_array( $function_args ) ) { | |
| 107 | + $function_args = array(); | |
| 108 | + } | |
| 109 | + | |
| 110 | + // Parse the function args option | |
| 111 | + $function_args = automatorwp_parse_function_args_option( $function_args, $action, $user_id, $action_options, $automation ); | |
| 112 | + | |
| 113 | + try { | |
| 114 | + // Try to call to the function | |
| 115 | + $function_result = call_user_func_array( $function_name, $function_args ); | |
| 116 | + | |
| 117 | + if( ( is_numeric( $function_result ) && automatorwp_starts_with( $function_result, 0 ) ) === false ) { | |
| 118 | + $function_result = automatorwp_parse_function_arg_value( $function_result ); | |
| 119 | + } | |
| 120 | + | |
| 121 | + $this->result = sprintf( __( 'Function "%s" called successfully.', 'automatorwp' ), $function_name ); | |
| 122 | + | |
| 123 | + // Store the function result | |
| 124 | + if( ! empty( $function_result ) ) { | |
| 125 | + $this->result .= ' ' . sprintf( __( 'The function returned: %s', 'automatorwp' ), $function_result ); | |
| 126 | + } | |
| 127 | + } catch ( Error $e ) { | |
| 128 | + // Notify about any error handled | |
| 129 | + $this->result = sprintf( __( 'Function "%s" throw the error: %s', 'automatorwp' ), $function_name, $e->getMessage() ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Register required hooks | |
| 136 | + * | |
| 137 | + * @since 1.0.0 | |
| 138 | + */ | |
| 139 | + public function hooks() { | |
| 140 | + | |
| 141 | + // Log meta data | |
| 142 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 143 | + | |
| 144 | + // Log fields | |
| 145 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 146 | + | |
| 147 | + parent::hooks(); | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Action custom log meta | |
| 152 | + * | |
| 153 | + * @since 1.0.0 | |
| 154 | + * | |
| 155 | + * @param array $log_meta Log meta data | |
| 156 | + * @param stdClass $action The action object | |
| 157 | + * @param int $user_id The user ID | |
| 158 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 159 | + * @param stdClass $automation The action's automation object | |
| 160 | + * | |
| 161 | + * @return array | |
| 162 | + */ | |
| 163 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 164 | + | |
| 165 | + // Bail if action type don't match this action | |
| 166 | + if( $action->type !== $this->action ) { | |
| 167 | + return $log_meta; | |
| 168 | + } | |
| 169 | + | |
| 170 | + $log_meta['result'] = $this->result; | |
| 171 | + | |
| 172 | + return $log_meta; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Action custom log fields | |
| 177 | + * | |
| 178 | + * @since 1.0.0 | |
| 179 | + * | |
| 180 | + * @param array $log_fields The log fields | |
| 181 | + * @param stdClass $log The log object | |
| 182 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 183 | + * | |
| 184 | + * @return array | |
| 185 | + */ | |
| 186 | + public function log_fields( $log_fields, $log, $object ) { | |
| 187 | + | |
| 188 | + // Bail if log is not assigned to an action | |
| 189 | + if( $log->type !== 'action' ) { | |
| 190 | + return $log_fields; | |
| 191 | + } | |
| 192 | + | |
| 193 | + // Bail if action type don't match this action | |
| 194 | + if( $object->type !== $this->action ) { | |
| 195 | + return $log_fields; | |
| 196 | + } | |
| 197 | + | |
| 198 | + $log_fields['result'] = array( | |
| 199 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 200 | + 'type' => 'text', | |
| 201 | + ); | |
| 202 | + | |
| 203 | + return $log_fields; | |
| 204 | + } | |
| 205 | + | |
| 206 | +} | |
| 207 | + | |
| 208 | +new AutomatorWP_WordPress_Call_Function( 'automatorwp' ); | |
| 209 | +new AutomatorWP_WordPress_Call_Function( 'code' ); | |