PluginProbe ʕ •ᴥ•ʔ
AI Copilot – Content Generator / 1.5.4
AI Copilot – Content Generator v1.5.4
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 week ago common 1 week ago languages 1 week ago logs 1 week ago modules 1 week ago ai-copilot-content-generator.php 1 week ago config.php 1 week ago functions.php 1 week ago license.txt 1 week ago readme.txt 1 week ago
functions.php
395 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 $aiProvider->setSessionId('api-' . substr(md5($prompt), 0, 16));
287
288 if ($aiProvider->setApiOptions($apiOptions)) {
289 $opts = array('prompt' => $prompt);
290 $result = $aiProvider->getText($opts);
291 } else {
292 $result = array(
293 'error' => 1,
294 'msg' => $frame->getLastError(),
295 );
296 }
297 return $result;
298 }
299 }
300 if (!function_exists('aiwuAskChatbot')) {
301 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
302 function aiwuAskChatbot( $botId, $message, $options = false ) {
303 $botId = (int) $botId;
304 if (empty($botId) || empty($message) || !class_exists('WaicFrame')) {
305 return false;
306 }
307 if (isset($options['user_id'])) {
308 $options['user_id'] = (int) $options['user_id'];
309 }
310 unset($options['ip']);
311 if (isset($options['uniq'])) {
312 $options['ip'] = substr(str_replace(array("'", '"'), array('', ''), $options['uniq']), 0, 20);
313 $options['user_id'] = 0;
314 }
315
316 $options['use_log'] = isset($options['user_id']) || isset($options['ip']);
317 $mode = 0;
318 if (!$options['use_log']) {
319 $mode = 2;
320 $options['user_id'] = 0;
321 $options['ip'] = '';
322 }
323 $result = WaicFrame::_()->getModule('chatbots')->getModel()->sendMessage($message, $botId, (int) $mode, '', false, $options);
324
325 return $result;
326 }
327 }
328
329 add_action('rest_api_init', function() {
330 register_rest_route( 'aiwu/v1', '/simple-text-query/', array(
331 'methods' => 'POST',
332 'callback' => 'aiwuSimpleTextQueryRest',
333 'permission_callback' => function( $request ) {
334 return aiwuAllowPublicApi('simple-text-query', $request );
335 },
336 ));
337 });
338 if (!function_exists('aiwuSimpleTextQueryRest')) {
339 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
340 function aiwuSimpleTextQueryRest( $request ) {
341 try {
342 $params = $request->get_params();
343 $prompt = isset( $params['prompt'] ) ? $params['prompt'] : '';
344 if (empty($prompt)) {
345 throw new Exception( 'The promp is required.' );
346 }
347 $options = isset( $params['options'] ) ? $params['options'] : [];
348 $result = aiwuSimpleTextQuery($prompt, $options);
349 return new WP_REST_Response(array('success' => true, 'data' => $result), 200);
350 }
351 catch (Exception $e) {
352 return new WP_REST_Response(array('success' => false, 'message' => $e->getMessage()), 500 );
353 }
354 }
355 }
356 add_action('rest_api_init', function() {
357 register_rest_route( 'aiwu/v1', '/ask-chatbot/', array(
358 'methods' => 'POST',
359 'callback' => 'aiwuAskChatbotRest',
360 'permission_callback' => function( $request ) {
361 return aiwuAllowPublicApi('ask-chatbot', $request );
362 },
363 ));
364 });
365 if (!function_exists('aiwuAskChatbotRest')) {
366 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
367 function aiwuAskChatbotRest( $request ) {
368 try {
369 $params = $request->get_params();
370 $message = isset( $params['message'] ) ? $params['message'] : '';
371 if (empty($message)) {
372 throw new Exception( 'The message is required.' );
373 }
374 $botId = isset( $params['bot_id'] ) ? $params['bot_id'] : '';
375 if (empty($botId)) {
376 throw new Exception( 'The bot_id is required.' );
377 }
378 $options = isset( $params['options'] ) ? $params['options'] : [];
379 $result = aiwuAskChatbot($botId, $message, $options);
380 return new WP_REST_Response(array('success' => true, 'data' => $result), 200);
381 }
382 catch (Exception $e) {
383 return new WP_REST_Response(array('success' => false, 'message' => $e->getMessage()), 500 );
384 }
385 }
386 }
387 if (!function_exists('aiwuAllowPublicApi')) {
388 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
389 function aiwuAllowPublicApi($feature, $extra) {
390 $isAdmin = current_user_can('manage_options');
391 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
392 return apply_filters('aiwu_allow_public_api', $isAdmin, $feature, $extra);
393 }
394 }
395