PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.0
8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 All 536 releases
chatbot / addons / automator / includes / actions / text-formatter-actions.php

text-formatter-actions.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.8.0, at addons/automator/includes/actions/text-formatter-actions.php

363 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Text Formatter Actions
4 *
5 * @package WPbot_Automator
6 */
7
8 namespace WPbot_Automator\Actions;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class Text_Formatter_Actions
16 */
17 class Text_Formatter_Actions extends Action {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'text_formatter';
24 $this->group = __( 'Text Formatter', 'wpbot-automator' );
25 }
26
27 /**
28 * Execute the action
29 */
30 public function execute( $action_data, $trigger_data ) {
31 $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : '';
32
33 if ( empty( $action_id ) ) {
34 return false;
35 }
36
37 switch ( $action_id ) {
38 case 'extract_pattern':
39 return $this->extract_pattern( $action_data, $trigger_data );
40 case 'find_text':
41 return $this->find_text( $action_data, $trigger_data );
42 case 'replace_text':
43 return $this->replace_text( $action_data, $trigger_data );
44 case 'split_text':
45 return $this->split_text( $action_data, $trigger_data );
46 case 'default_value':
47 return $this->default_value( $action_data, $trigger_data );
48 case 'truncate':
49 return $this->truncate( $action_data, $trigger_data );
50 case 'url_encode':
51 return $this->url_encode( $action_data, $trigger_data );
52 case 'url_decode':
53 return $this->url_decode( $action_data, $trigger_data );
54 case 'join_lines':
55 return $this->join_lines( $action_data, $trigger_data );
56 case 'html_to_markdown':
57 return $this->html_to_markdown( $action_data, $trigger_data );
58 case 'markdown_to_html':
59 return $this->markdown_to_html( $action_data, $trigger_data );
60 case 'generate_slug':
61 return $this->generate_slug( $action_data, $trigger_data );
62 default:
63 return false;
64 }
65 }
66
67 /**
68 * Extract pattern using Regex
69 */
70 private function extract_pattern( $config, $trigger_data ) {
71 $values = isset( $config['config'] ) ? $config['config'] : array();
72 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
73 $pattern = isset( $values['pattern'] ) ? $this->parse_tokens( $values['pattern'], $trigger_data ) : '';
74
75 if ( empty( $pattern ) ) {
76 return array( 'success' => false, 'message' => 'Pattern is required.' );
77 }
78
79 if ( @preg_match( $pattern, $text, $matches ) ) {
80 return array(
81 'success' => true,
82 'result' => $matches[0],
83 'matches' => $matches,
84 'message' => 'Pattern extracted successfully.'
85 );
86 }
87
88 return array( 'success' => false, 'result' => '', 'message' => 'No match found.' );
89 }
90
91 /**
92 * Find text in content
93 */
94 private function find_text( $config, $trigger_data ) {
95 $values = isset( $config['config'] ) ? $config['config'] : array();
96 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
97 $search = isset( $values['search'] ) ? $this->parse_tokens( $values['search'], $trigger_data ) : '';
98
99 if ( empty( $search ) ) {
100 return array( 'success' => false, 'message' => 'Search term is required.' );
101 }
102
103 $found = ( strpos( $text, $search ) !== false );
104
105 return array(
106 'success' => true,
107 'found' => $found,
108 'message' => $found ? 'Text found.' : 'Text not found.'
109 );
110 }
111
112 /**
113 * Replace text
114 */
115 private function replace_text( $config, $trigger_data ) {
116 $values = isset( $config['config'] ) ? $config['config'] : array();
117 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
118 $search = isset( $values['search'] ) ? $this->parse_tokens( $values['search'], $trigger_data ) : '';
119 $replace = isset( $values['replace'] ) ? $this->parse_tokens( $values['replace'], $trigger_data ) : '';
120
121 $result = str_replace( $search, $replace, $text );
122
123 return array(
124 'success' => true,
125 'result' => $result,
126 'message' => 'Text replaced successfully.'
127 );
128 }
129
130 /**
131 * Split text
132 */
133 private function split_text( $config, $trigger_data ) {
134 $values = isset( $config['config'] ) ? $config['config'] : array();
135 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
136 $delimiter = isset( $values['delimiter'] ) ? $this->parse_tokens( $values['delimiter'], $trigger_data ) : '';
137 $index = isset( $values['segment_index'] ) ? (int) $values['segment_index'] : 0;
138
139 if ( empty( $delimiter ) ) {
140 return array( 'success' => false, 'message' => 'Delimiter is required.' );
141 }
142
143 $segments = explode( $delimiter, $text );
144
145 if ( $index < 0 ) {
146 $index = count( $segments ) + $index;
147 }
148
149 $result = isset( $segments[ $index ] ) ? $segments[ $index ] : '';
150
151 return array(
152 'success' => true,
153 'result' => $result,
154 'segments' => $segments,
155 'message' => 'Text split successfully.'
156 );
157 }
158
159 /**
160 * Default value if empty
161 */
162 private function default_value( $config, $trigger_data ) {
163 $values = isset( $config['config'] ) ? $config['config'] : array();
164 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
165 $default = isset( $values['default'] ) ? $this->parse_tokens( $values['default'], $trigger_data ) : '';
166
167 $result = empty( trim( $text ) ) ? $default : $text;
168
169 return array(
170 'success' => true,
171 'result' => $result,
172 'message' => 'Value processed.'
173 );
174 }
175
176 /**
177 * Truncate text
178 */
179 private function truncate( $config, $trigger_data ) {
180 $values = isset( $config['config'] ) ? $config['config'] : array();
181 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
182 $length = isset( $values['length'] ) ? (int) $values['length'] : 100;
183 $suffix = isset( $values['suffix'] ) ? $this->parse_tokens( $values['suffix'], $trigger_data ) : '';
184
185 if ( mb_strlen( $text ) <= $length ) {
186 $result = $text;
187 } else {
188 $result = mb_substr( $text, 0, $length ) . $suffix;
189 }
190
191 return array(
192 'success' => true,
193 'result' => $result,
194 'message' => 'Text truncated successfully.'
195 );
196 }
197
198 /**
199 * URL Encode
200 */
201 private function url_encode( $config, $trigger_data ) {
202 $values = isset( $config['config'] ) ? $config['config'] : array();
203 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
204
205 $result = urlencode( $text );
206
207 return array(
208 'success' => true,
209 'result' => $result,
210 'message' => 'Text encoded successfully.'
211 );
212 }
213
214 /**
215 * URL Decode
216 */
217 private function url_decode( $config, $trigger_data ) {
218 $values = isset( $config['config'] ) ? $config['config'] : array();
219 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
220
221 $result = urldecode( $text );
222
223 return array(
224 'success' => true,
225 'result' => $result,
226 'message' => 'Text decoded successfully.'
227 );
228 }
229
230 /**
231 * Join lines
232 */
233 private function join_lines( $config, $trigger_data ) {
234 $values = isset( $config['config'] ) ? $config['config'] : array();
235 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
236 $separator = isset( $values['separator'] ) ? $this->parse_tokens( $values['separator'], $trigger_data ) : ', ';
237
238 $result = preg_replace( '/\r?\n|\r/', $separator, $text );
239
240 return array(
241 'success' => true,
242 'result' => $result,
243 'message' => 'Lines joined successfully.'
244 );
245 }
246
247 /**
248 * HTML to Markdown (Basic implementation)
249 */
250 private function html_to_markdown( $config, $trigger_data ) {
251 $values = isset( $config['config'] ) ? $config['config'] : array();
252 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
253 $save_media = isset( $values['save_media'] ) ? (bool) $values['save_media'] : false;
254
255 if ( $save_media ) {
256 $text = $this->handle_media_extraction( $text );
257 }
258
259 // Simple regex-based HTML to Markdown
260 $replacements = array(
261 '/<h[1-6]>(.*?)<\/h[1-6]>/is' => "# $1\n",
262 '/<b>(.*?)<\/b>/is' => "**$1**",
263 '/<strong>(.*?)<\/strong>/is' => "**$1**",
264 '/<i>(.*?)<\/i>/is' => "*$1*",
265 '/<em>(.*?)<\/em>/is' => "*$1*",
266 '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is' => "[$2]($1)",
267 '/<img.*?src="(.*?)".*?alt="(.*?)".*?>/is' => "![$2]($1)",
268 '/<img.*?src="(.*?)".*?>/is' => "![]($1)",
269 '/<p>(.*?)<\/p>/is' => "$1\n\n",
270 '/<br\s*\/?>/is' => "\n",
271 '/<li>(.*?)<\/li>/is' => "- $1\n",
272 '/<ul>(.*?)<\/ul>/is' => "$1\n",
273 '/<ol>(.*?)<\/ol>/is' => "$1\n",
274 );
275
276 $result = preg_replace( array_keys( $replacements ), array_values( $replacements ), $text );
277 $result = wp_strip_all_tags( $result );
278 $result = trim( html_entity_decode( $result ) );
279
280 return array(
281 'success' => true,
282 'result' => $result,
283 'message' => 'HTML converted to Markdown.'
284 );
285 }
286
287 /**
288 * Markdown to HTML (Basic implementation)
289 */
290 private function markdown_to_html( $config, $trigger_data ) {
291 $values = isset( $config['config'] ) ? $config['config'] : array();
292 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
293
294 // Simple regex-based Markdown to HTML
295 $replacements = array(
296 '/^# (.*?)$/m' => '<h1>$1</h1>',
297 '/\*\*(.*?)\*\*/' => '<strong>$1</strong>',
298 '/\*(.*?)\*/' => '<em>$1</em>',
299 '/\[(.*?)\]\((.*?)\)/' => '<a href="$2">$1</a>',
300 '/!\[(.*?)\]\((.*?)\)/' => '<img src="$2" alt="$1">',
301 '/^- (.*?)$/m' => '<li>$1</li>',
302 '/\n\n/' => '</p><p>',
303 );
304
305 $result = preg_replace( array_keys( $replacements ), array_values( $replacements ), $text );
306 $result = '<p>' . str_replace( "\n", "<br>", $result ) . '</p>';
307
308 // Clean up list items
309 $result = preg_replace( '/(<li>.*?<\/li>)+/s', '<ul>$0</ul>', $result );
310
311 return array(
312 'success' => true,
313 'result' => $result,
314 'message' => 'Markdown converted to HTML.'
315 );
316 }
317
318 /**
319 * Generate Slug
320 */
321 private function generate_slug( $config, $trigger_data ) {
322 $values = isset( $config['config'] ) ? $config['config'] : array();
323 $text = isset( $values['text'] ) ? $this->parse_tokens( $values['text'], $trigger_data ) : '';
324
325 $result = sanitize_title( $text );
326
327 return array(
328 'success' => true,
329 'result' => $result,
330 'message' => 'Slug generated successfully.'
331 );
332 }
333
334 /**
335 * Handle media extraction and save to media library
336 */
337 private function handle_media_extraction( $html ) {
338 if ( ! function_exists( 'download_url' ) ) {
339 require_once ABSPATH . 'wp-admin/includes/file.php';
340 require_once ABSPATH . 'wp-admin/includes/media.php';
341 require_once ABSPATH . 'wp-admin/includes/image.php';
342 }
343
344 preg_match_all( '/<img.*?src="(.*?)".*?>/i', $html, $matches );
345
346 if ( ! empty( $matches[1] ) ) {
347 foreach ( $matches[1] as $image_url ) {
348 // Avoid re-downloading local images
349 if ( strpos( $image_url, get_site_url() ) !== false ) {
350 continue;
351 }
352
353 $new_url = media_sideload_image( $image_url, 0, null, 'src' );
354 if ( ! is_wp_error( $new_url ) ) {
355 $html = str_replace( $image_url, $new_url, $html );
356 }
357 }
358 }
359
360 return $html;
361 }
362 }
363