PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | integrations/formatter/includes/actions/format-string.php +178 -0 5.6.1 → 6.0.3 View file →
@@ -1,0 +1,178 @@
1 +<?php
2 +/**
3 + * Format String
4 + *
5 + * @package AutomatorWP\Integrations\Formatter\Actions\Format_String
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_Format_String extends AutomatorWP_Integration_Action {
14 +
15 + public $integration = 'formatter';
16 + public $action = 'formatter_format_string';
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' => __( 'Convert string into format', 'automatorwp' ),
29 + 'select_option' => __( 'Convert <strong>string</strong> into <strong>format</strong>', 'automatorwp' ),
30 + /* translators: %1$s: String. %2$s: Format */
31 + 'edit_label' => sprintf( __( 'Convert %1$s into %2$s', 'automatorwp' ), '{string}', '{format}' ),
32 + 'log_label' => sprintf( __( 'Convert %1$s into %2$s', 'automatorwp' ), '{string}', '{format}' ),
33 + 'options' => array(
34 + 'string' => array(
35 + 'default' => __( 'string', 'automatorwp' ),
36 + 'fields' => array(
37 + 'string' => array(
38 + 'name' => __( 'String:', 'automatorwp' ),
39 + 'desc' => __( 'String to format.', 'automatorwp' ),
40 + 'type' => 'text',
41 + 'default' => ''
42 + ),
43 + ) ),
44 + 'format' => array(
45 + 'from' => 'format',
46 + 'default' => __( 'format', 'automatorwp' ),
47 + 'fields' => array(
48 + 'format' => array(
49 + 'name' => __( 'Format:', 'automatorwp' ),
50 + 'type' => 'select',
51 + 'classes' => 'automatorwp-selector',
52 + 'options_cb' => 'automatorwp_formatter_get_string_formats',
53 + 'attributes' => array(
54 + 'data-placeholder' => __( 'Select a format', 'automatorwp' ),
55 + ),
56 + 'default' => '',
57 + ),
58 + )
59 + ),
60 + ),
61 + 'tags' => automatorwp_formatter_get_actions_string_tags()
62 + ) );
63 +
64 + }
65 +
66 + /**
67 + * Action execution function
68 + *
69 + * @since 1.0.0
70 + *
71 + * @param stdClass $action The action object
72 + * @param int $user_id The user ID
73 + * @param array $action_options The action's stored options (with tags already passed)
74 + * @param stdClass $automation The action's automation object
75 + */
76 + public function execute( $action, $user_id, $action_options, $automation ) {
77 +
78 + // Shorthand
79 + $string = $action_options['string'];
80 + $format = $action_options['format'];
81 +
82 + // Bail if not data
83 + if ( empty( $string ) ) {
84 + $this->result = __( 'Please, insert a string to be formatted', 'automatorwp' );
85 + return;
86 + }
87 +
88 + // Bail if not position
89 + if ( empty( $format ) ) {
90 + $this->result = __( 'Please, select a format', 'automatorwp' );
91 + return;
92 + }
93 +
94 + // Format string
95 + $this->formatted = automatorwp_formatter_get_formatted_string( $format, $string );
96 +
97 + $this->result = sprintf( __( '%s', 'automatorwp' ), $this->formatted );
98 +
99 + }
100 +
101 + /**
102 + * Register required hooks
103 + *
104 + * @since 1.0.0
105 + */
106 + public function hooks() {
107 +
108 + // Log meta data
109 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
110 +
111 + // Log fields
112 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
113 +
114 + parent::hooks();
115 +
116 + }
117 +
118 + /**
119 + * Action custom log meta
120 + *
121 + * @since 1.0.0
122 + *
123 + * @param array $log_meta Log meta data
124 + * @param stdClass $action The action object
125 + * @param int $user_id The user ID
126 + * @param array $action_options The action's stored options (with tags already passed)
127 + * @param stdClass $automation The action's automation object
128 + *
129 + * @return array
130 + */
131 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
132 +
133 + // Bail if action type don't match this action
134 + if( $action->type !== $this->action ) {
135 + return $log_meta;
136 + }
137 +
138 + // Store the action's result
139 + $log_meta['result'] = $this->result;
140 + $log_meta['formatted'] = ( isset( $this->formatted ) ? $this->formatted : '' );
141 +
142 + return $log_meta;
143 + }
144 +
145 + /**
146 + * Action custom log fields
147 + *
148 + * @since 1.0.0
149 + *
150 + * @param array $log_fields The log fields
151 + * @param stdClass $log The log object
152 + * @param stdClass $object The trigger/action/automation object attached to the log
153 + *
154 + * @return array
155 + */
156 + public function log_fields( $log_fields, $log, $object ) {
157 +
158 + // Bail if log is not assigned to an action
159 + if( $log->type !== 'action' ) {
160 + return $log_fields;
161 + }
162 +
163 + // Bail if action type don't match this action
164 + if( $object->type !== $this->action ) {
165 + return $log_fields;
166 + }
167 +
168 + $log_fields['result'] = array(
169 + 'name' => __( 'Result:', 'automatorwp' ),
170 + 'type' => 'text',
171 + );
172 +
173 + return $log_fields;
174 + }
175 +
176 +}
177 +
178 +new AutomatorWP_Formatter_Format_String();