PluginProbe ʕ •ᴥ•ʔ
AI Copilot – Content Generator / 1.4.21
AI Copilot – Content Generator v1.4.21
1.5.4 1.4.21 1.4.18 1.4.19 1.4.20 trunk 1.0.4 1.1.0 1.2.0 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.17 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.9
ai-copilot-content-generator / functions.php
ai-copilot-content-generator Last commit date
classes 1 month ago common 1 month ago languages 1 month ago logs 1 month ago modules 1 month ago ai-copilot-content-generator.php 1 month ago config.php 1 month ago functions.php 1 month ago license.txt 1 month ago readme.txt 1 month ago
functions.php
394 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 /**
6 * Set first leter in a string as UPPERCASE
7 *
8 * @param string $str string to modify
9 * @return string string with first Uppercase letter
10 */
11 if (!function_exists('waicStrFirstUp')) {
12 function waicStrFirstUp( $str ) {
13 return strtoupper(substr($str, 0, 1)) . strtolower(substr($str, 1, strlen($str)));
14 }
15 }
16 /**
17 * Deprecated - class must be created
18 */
19 if (!function_exists('waicDateToTimestamp')) {
20 function waicDateToTimestamp( $date ) {
21 if (empty($a)) {
22 return false;
23 }
24 $a = explode(WAIC_DATE_DL, $date);
25 return mktime(0, 0, 0, $a[1], $a[0], $a[2]);
26 }
27 }
28 /**
29 * Generate random string name
30 *
31 * @param int $lenFrom min len
32 * @param int $lenTo max len
33 * @return string random string with length from $lenFrom to $lenTo
34 */
35 if (!function_exists('waicGetRandName')) {
36 function waicGetRandName( $lenFrom = 6, $lenTo = 9 ) {
37 $res = '';
38 $len = wp_rand($lenFrom, $lenTo);
39 if ($len) {
40 for ($i = 0; $i < $len; $i++) {
41 $res .= chr(wp_rand(97, 122)); /*rand symbol from a to z*/
42 }
43 }
44 return $res;
45 }
46 }
47 if (!function_exists('waicImport')) {
48 function waicImport( $path ) {
49 if (file_exists($path)) {
50 require $path;
51 return true;
52 }
53 return false;
54 }
55 }
56 if (!function_exists('waicSetDefaultParams')) {
57 function waicSetDefaultParams( $params, $default ) {
58 foreach ($default as $k => $v) {
59 $params[$k] = isset($params[$k]) ? $params[$k] : $default[$k];
60 }
61 return $params;
62 }
63 }
64 if (!function_exists('waicImportClass')) {
65 function waicImportClass( $class, $path = '' ) {
66 if (!class_exists($class)) {
67 if (!$path) {
68 $classFile = lcfirst($class);
69 if (strpos(strtolower($classFile), WAIC_CODE) !== false) {
70 $classFile = preg_replace('/' . WAIC_CODE . '/i', '', $classFile);
71 }
72 $path = WAIC_CLASSES_DIR . lcfirst($classFile) . '.php';
73 }
74 return waicImport($path);
75 }
76 return false;
77 }
78 }
79 /**
80 * Check if class name exist with prefix or not
81 *
82 * @param strin $class preferred class name
83 * @return string existing class name
84 */
85 if (!function_exists('waicToeGetClassName')) {
86 function waicToeGetClassName( $class ) {
87 $className = '';
88 if (class_exists(waicStrFirstUp(WAIC_CODE) . $class)) {
89 $className = waicStrFirstUp(WAIC_CODE) . $class;
90 } else if (class_exists(WAIC_CLASS_PREFIX . $class)) {
91 $className = WAIC_CLASS_PREFIX . $class;
92 } else {
93 $className = $class;
94 }
95 return $className;
96 }
97 }
98 /**
99 * Create object of specified class
100 *
101 * @param string $class class that you want to create
102 * @param array $params array of arguments for class __construct function
103 * @return object new object of specified class
104 */
105 if (!function_exists('waicToeCreateObj')) {
106 function waicToeCreateObj( $class, $params ) {
107 $className = waicToeGetClassName($class);
108 $obj = null;
109 if (class_exists('ReflectionClass')) {
110 $reflection = new ReflectionClass($className);
111 try {
112 $obj = $reflection->newInstanceArgs($params);
113 } catch (ReflectionException $e) { // If class have no constructor
114 $obj = $reflection->newInstanceArgs();
115 }
116 } else {
117 $obj = new $className();
118 call_user_func_array(array($obj, '__construct'), $params);
119 }
120 return $obj;
121 }
122 }
123 /**
124 * Redirect user to specified location. Be advised that it should redirect even if headers alredy sent.
125 *
126 * @param string $url where page must be redirected
127 */
128 if (!function_exists('waicRedirect')) {
129 function waicRedirect( $url ) {
130 if (headers_sent()) {
131 if ( class_exists('WaicFrame') ) {
132 WaicFrame::_()->printInlineJs('document.location.href="' . esc_url($url) . '";');
133 }
134 } else {
135 header('Location: ' . $url);
136 }
137 exit();
138 }
139 }
140 if (!function_exists('waicJsonEncodeUTFnormal')) {
141 function waicJsonEncodeUTFnormal( $value, $ent = false ) {
142 if (is_int($value)) {
143 return (string) $value;
144 } elseif (is_string($value)) {
145 if ($ent) {
146 $value = stripslashes($value);
147 }
148
149 $value = str_replace(array('\\', '/', '"', "\r", "\n", "\b", "\f", "\t"),
150 $ent ? array('\\\\', '\/', '\"', '\\\\r', '\\\\n', '\\\\b', '\\\\f', '\\\\t') : array('\\\\', '\/', '\"', '\r', '\n', '\b', '\f', '\t'), $value);
151 $convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
152 $result = '';
153 for ($i = strlen($value) - 1; $i >= 0; $i--) {
154 $mb_char = substr($value, $i, 1);
155 $result = $mb_char . $result;
156 }
157 return '"' . ( $ent ? htmlspecialchars($result, ENT_QUOTES) : $result ) . '"';
158 } elseif (is_float($value)) {
159 return str_replace(',', '.', $value);
160 } elseif (is_null($value)) {
161 return 'null';
162 } elseif (is_bool($value)) {
163 return $value ? 'true' : 'false';
164 } elseif (is_array($value)) {
165 $with_keys = false;
166 $n = count($value);
167 for ($i = 0, reset($value); $i < $n; $i++, next($value)) {
168 if (key($value) !== $i) {
169 $with_keys = true;
170 break;
171 }
172 }
173 } elseif (is_object($value)) {
174 $with_keys = true;
175 } else {
176 return '';
177 }
178 $result = array();
179 if ($with_keys) {
180 foreach ($value as $key => $v) {
181 $result[] = waicJsonEncodeUTFnormal((string) $key, $ent) . ':' . waicJsonEncodeUTFnormal($v, $ent);
182 }
183 return '{' . implode(',', $result) . '}';
184 } else {
185 foreach ($value as $key => $v) {
186 $result[] = waicJsonEncodeUTFnormal($v, $ent);
187 }
188 return '[' . implode(',', $result) . ']';
189 }
190 }
191 }
192 /**
193 * Prepares the params values to store into db
194 *
195 * @param array $d $_POST array
196 * @return array
197 */
198 if (!function_exists('waicPrepareParams')) {
199 function waicPrepareParams( &$d = array(), &$options = array() ) {
200 if (!empty($d['params'])) {
201 if (isset($d['params']['options'])) {
202 $options = $d['params']['options'];
203 }
204 if (is_array($d['params'])) {
205 $params = WaicUtils::jsonEncode($d['params']);
206 $params = str_replace(array('\n\r', "\n\r", '\n', "\r", '\r', "\r"), '<br />', $params);
207 $params = str_replace(array('<br /><br />', '<br /><br /><br />'), '<br />', $params);
208 $d['params'] = $params;
209 }
210 } elseif (isset($d['params'])) {
211 $d['params']['attr']['class'] = '';
212 $d['params']['attr']['id'] = '';
213 $params = WaicUtils::jsonEncode($d['params']);
214 $d['params'] = $params;
215 }
216 if (empty($options)) {
217 $options = array('value' => array('EMPTY'), 'data' => array());
218 }
219 if (isset($d['code'])) {
220 if ('' == $d['code']) {
221 $d['code'] = waicPrepareFieldCode($d['label']) . '_' . wp_rand(0, 9999999);
222 }
223 }
224 return $d;
225 }
226 }
227 if (!function_exists('waicPrepareFieldCode')) {
228 function waicPrepareFieldCode( $string ) {
229 $string = preg_replace('/[^a-zA-Z0-9\s]/', ' ', $string);
230 $string = preg_replace('/\s+/', ' ', $string);
231 $string = preg_replace('/ /', '', $string);
232
233 $code = substr($string, 0, 8);
234 $code = strtolower($code);
235 if ('' == $code) {
236 $code = 'field_' . gmdate('dhis');
237 }
238 return $code;
239 }
240 }
241 /**
242 * Recursive implode of array
243 *
244 * @param string $glue imploder
245 * @param array $array array to implode
246 * @return string imploded array in string
247 */
248 if (!function_exists('waicRecImplode')) {
249 function waicRecImplode( $glue, $array ) {
250 $res = '';
251 $i = 0;
252 $count = count($array);
253 foreach ($array as $el) {
254 $str = '';
255 if (is_array($el)) {
256 $str = waicRecImplode('', $el);
257 } else {
258 $str = $el;
259 }
260 $res .= $str;
261 if ($i < ( $count-1 )) {
262 $res .= $glue;
263 }
264 $i++;
265 }
266 return $res;
267 }
268 }
269 if (!function_exists('aiwuSimpleTextQuery')) {
270 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
271 function aiwuSimpleTextQuery( $prompt, $options = false ) {
272 if (empty($prompt) || !class_exists('WaicFrame')) {
273 return false;
274 }
275 $frame = WaicFrame::_();
276 $apiOptions = $frame->getModule('options')->getModel()->getDefaults('api');
277 if (!empty($options) && is_array($options)) {
278 $apiOptions = array_merge($apiOptions, $options);
279 }
280 $aiProvider = $frame->getModule('workspace')->getModel('aiprovider')->getInstance($apiOptions);
281 if (!$aiProvider) {
282 return false;
283 }
284 $aiProvider->init();
285 $aiProvider->setSaveError(false);
286
287 if ($aiProvider->setApiOptions($apiOptions)) {
288 $opts = array('prompt' => $prompt);
289 $result = $aiProvider->getText($opts);
290 } else {
291 $result = array(
292 'error' => 1,
293 'msg' => $frame->getLastError(),
294 );
295 }
296 return $result;
297 }
298 }
299 if (!function_exists('aiwuAskChatbot')) {
300 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
301 function aiwuAskChatbot( $botId, $message, $options = false ) {
302 $botId = (int) $botId;
303 if (empty($botId) || empty($message) || !class_exists('WaicFrame')) {
304 return false;
305 }
306 if (isset($options['user_id'])) {
307 $options['user_id'] = (int) $options['user_id'];
308 }
309 unset($options['ip']);
310 if (isset($options['uniq'])) {
311 $options['ip'] = substr(str_replace(array("'", '"'), array('', ''), $options['uniq']), 0, 20);
312 $options['user_id'] = 0;
313 }
314
315 $options['use_log'] = isset($options['user_id']) || isset($options['ip']);
316 $mode = 0;
317 if (!$options['use_log']) {
318 $mode = 2;
319 $options['user_id'] = 0;
320 $options['ip'] = '';
321 }
322 $result = WaicFrame::_()->getModule('chatbots')->getModel()->sendMessage($message, $botId, (int) $mode, '', false, $options);
323
324 return $result;
325 }
326 }
327
328 add_action('rest_api_init', function() {
329 register_rest_route( 'aiwu/v1', '/simple-text-query/', array(
330 'methods' => 'POST',
331 'callback' => 'aiwuSimpleTextQueryRest',
332 'permission_callback' => function( $request ) {
333 return aiwuAllowPublicApi('simple-text-query', $request );
334 },
335 ));
336 });
337 if (!function_exists('aiwuSimpleTextQueryRest')) {
338 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
339 function aiwuSimpleTextQueryRest( $request ) {
340 try {
341 $params = $request->get_params();
342 $prompt = isset( $params['prompt'] ) ? $params['prompt'] : '';
343 if (empty($prompt)) {
344 throw new Exception( 'The promp is required.' );
345 }
346 $options = isset( $params['options'] ) ? $params['options'] : [];
347 $result = aiwuSimpleTextQuery($prompt, $options);
348 return new WP_REST_Response(array('success' => true, 'data' => $result), 200);
349 }
350 catch (Exception $e) {
351 return new WP_REST_Response(array('success' => false, 'message' => $e->getMessage()), 500 );
352 }
353 }
354 }
355 add_action('rest_api_init', function() {
356 register_rest_route( 'aiwu/v1', '/ask-chatbot/', array(
357 'methods' => 'POST',
358 'callback' => 'aiwuAskChatbotRest',
359 'permission_callback' => function( $request ) {
360 return aiwuAllowPublicApi('ask-chatbot', $request );
361 },
362 ));
363 });
364 if (!function_exists('aiwuAskChatbotRest')) {
365 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
366 function aiwuAskChatbotRest( $request ) {
367 try {
368 $params = $request->get_params();
369 $message = isset( $params['message'] ) ? $params['message'] : '';
370 if (empty($message)) {
371 throw new Exception( 'The message is required.' );
372 }
373 $botId = isset( $params['bot_id'] ) ? $params['bot_id'] : '';
374 if (empty($botId)) {
375 throw new Exception( 'The bot_id is required.' );
376 }
377 $options = isset( $params['options'] ) ? $params['options'] : [];
378 $result = aiwuAskChatbot($botId, $message, $options);
379 return new WP_REST_Response(array('success' => true, 'data' => $result), 200);
380 }
381 catch (Exception $e) {
382 return new WP_REST_Response(array('success' => false, 'message' => $e->getMessage()), 500 );
383 }
384 }
385 }
386 if (!function_exists('aiwuAllowPublicApi')) {
387 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
388 function aiwuAllowPublicApi($feature, $extra) {
389 $isAdmin = current_user_can('manage_options');
390 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
391 return apply_filters('aiwu_allow_public_api', $isAdmin, $feature, $extra);
392 }
393 }
394