| 1 |
<?php |
| 2 |
/** |
| 3 |
* Round Number |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Formatter\Actions\Round_Number |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
|
| 11 |
if( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
class AutomatorWP_Formatter_Round_Number extends AutomatorWP_Integration_Action { |
| 14 |
|
| 15 |
public $integration = 'formatter'; |
| 16 |
public $action = 'formatter_round_number'; |
| 17 |
public $formatted = ''; |
| 18 |
|
| 19 |
/** |
| 20 |
* Register the trigger |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
public function register() { |
| 25 |
|
| 26 |
automatorwp_register_action( $this->action, array( |
| 27 |
'integration' => $this->integration, |
| 28 |
'label' => __( 'Round number into format', 'automatorwp' ), |
| 29 |
'select_option' => __( 'Round <strong>number</strong> into <strong>format</strong>', 'automatorwp' ), |
| 30 |
/* translators: %1$s: Number. %2$s: Format. */ |
| 31 |
'edit_label' => sprintf( __( 'Round %1$s into %2$s', 'automatorwp' ), '{number}', '{format}' ), |
| 32 |
/* translators: %1$s: Number. %2$s: Format. */ |
| 33 |
'log_label' => sprintf( __( 'Round %1$s into %2$s', 'automatorwp' ), '{number}', '{format}' ), |
| 34 |
'options' => array( |
| 35 |
'number' => array( |
| 36 |
'from' => 'number', |
| 37 |
'default' => __( 'number', 'automatorwp' ), |
| 38 |
'fields' => array( |
| 39 |
'number' => array( |
| 40 |
'name' => __( 'Number:', 'automatorwp' ), |
| 41 |
'desc' => __( 'Number to round.', 'automatorwp' ), |
| 42 |
'type' => 'text', |
| 43 |
'required' => true, |
| 44 |
'default' => '' |
| 45 |
), |
| 46 |
) |
| 47 |
), |
| 48 |
'format' => array( |
| 49 |
'from' => 'format', |
| 50 |
'default' => __( 'format', 'automatorwp' ), |
| 51 |
'fields' => array( |
| 52 |
'format' => array( |
| 53 |
'name' => __( 'Format:', 'automatorwp' ), |
| 54 |
'type' => 'select', |
| 55 |
'classes' => 'automatorwp-selector', |
| 56 |
'options_cb' => 'automatorwp_formatter_get_number_formats', |
| 57 |
'attributes' => array( |
| 58 |
'data-placeholder' => __( 'Select a format', 'automatorwp' ), |
| 59 |
), |
| 60 |
'default' => '', |
| 61 |
), |
| 62 |
) |
| 63 |
), |
| 64 |
), |
| 65 |
'tags' => automatorwp_formatter_get_actions_string_tags() |
| 66 |
) ); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Action execution function |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @param stdClass $action The action object |
| 76 |
* @param int $user_id The user ID |
| 77 |
* @param array $action_options The action's stored options (with tags already passed) |
| 78 |
* @param stdClass $automation The action's automation object |
| 79 |
*/ |
| 80 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 81 |
|
| 82 |
// Shorthand |
| 83 |
$number = (float)$action_options['number']; |
| 84 |
$format = $action_options['format']; |
| 85 |
|
| 86 |
// Bail if not number |
| 87 |
if ( empty( $number ) ) { |
| 88 |
$this->result = __( 'Please, insert a number to be rounded', 'automatorwp' ); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Bail if not position |
| 93 |
if ( empty( $format ) ) { |
| 94 |
$this->result = __( 'Please, select a format', 'automatorwp' ); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$this->formatted = automatorwp_formatter_get_formatted_number( $format, $number ); |
| 99 |
|
| 100 |
$this->result = sprintf( __( '%s', 'automatorwp' ), $this->formatted ); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Register required hooks |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
*/ |
| 109 |
public function hooks() { |
| 110 |
|
| 111 |
// Log meta data |
| 112 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 113 |
|
| 114 |
// Log fields |
| 115 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 116 |
|
| 117 |
parent::hooks(); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Action custom log meta |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
* |
| 126 |
* @param array $log_meta Log meta data |
| 127 |
* @param stdClass $action The action object |
| 128 |
* @param int $user_id The user ID |
| 129 |
* @param array $action_options The action's stored options (with tags already passed) |
| 130 |
* @param stdClass $automation The action's automation object |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 135 |
|
| 136 |
// Bail if action type don't match this action |
| 137 |
if( $action->type !== $this->action ) { |
| 138 |
return $log_meta; |
| 139 |
} |
| 140 |
|
| 141 |
// Store the action's result |
| 142 |
$log_meta['result'] = $this->result; |
| 143 |
$log_meta['formatted'] = ( isset( $this->formatted ) ? $this->formatted : '' ); |
| 144 |
|
| 145 |
return $log_meta; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Action custom log fields |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* |
| 153 |
* @param array $log_fields The log fields |
| 154 |
* @param stdClass $log The log object |
| 155 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 156 |
* |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
public function log_fields( $log_fields, $log, $object ) { |
| 160 |
|
| 161 |
// Bail if log is not assigned to an action |
| 162 |
if( $log->type !== 'action' ) { |
| 163 |
return $log_fields; |
| 164 |
} |
| 165 |
|
| 166 |
// Bail if action type don't match this action |
| 167 |
if( $object->type !== $this->action ) { |
| 168 |
return $log_fields; |
| 169 |
} |
| 170 |
|
| 171 |
$log_fields['result'] = array( |
| 172 |
'name' => __( 'Result:', 'automatorwp' ), |
| 173 |
'type' => 'text', |
| 174 |
); |
| 175 |
|
| 176 |
return $log_fields; |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
new AutomatorWP_Formatter_Round_Number(); |