PluginProbe ʕ •ᴥ•ʔ
AI Copilot – Content Generator / 1.4.9
AI Copilot – Content Generator v1.4.9
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 4 months ago common 4 months ago languages 4 months ago logs 4 months ago modules 4 months ago ai-copilot-content-generator.php 4 months ago config.php 4 months ago functions.php 4 months ago license.txt 4 months ago readme.txt 4 months ago
functions.php
388 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 = mt_rand($lenFrom, $lenTo);
39 if ($len) {
40 for ($i = 0; $i < $len; $i++) {
41 $res .= chr(mt_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']) . '_' . 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 function aiwuSimpleTextQuery( $prompt, $options = false ) {
271 if (empty($prompt) || !class_exists('WaicFrame')) {
272 return false;
273 }
274 $frame = WaicFrame::_();
275 $apiOptions = $frame->getModule('options')->getModel()->getDefaults('api');
276 if (!empty($options) && is_array($options)) {
277 $apiOptions = array_merge($apiOptions, $options);
278 }
279 $aiProvider = $frame->getModule('workspace')->getModel('aiprovider')->getInstance($apiOptions);
280 if (!$aiProvider) {
281 return false;
282 }
283 $aiProvider->init();
284 $aiProvider->setSaveError(false);
285
286 if ($aiProvider->setApiOptions($apiOptions)) {
287 $opts = array('prompt' => $prompt);
288 $result = $aiProvider->getText($opts);
289 } else {
290 $result = array(
291 'error' => 1,
292 'msg' => $frame->getLastError(),
293 );
294 }
295 return $result;
296 }
297 }
298 if (!function_exists('aiwuAskChatbot')) {
299 function aiwuAskChatbot( $botId, $message, $options = false ) {
300 $botId = (int) $botId;
301 if (empty($botId) || empty($message) || !class_exists('WaicFrame')) {
302 return false;
303 }
304 if (isset($options['user_id'])) {
305 $options['user_id'] = (int) $options['user_id'];
306 }
307 unset($options['ip']);
308 if (isset($options['uniq'])) {
309 $options['ip'] = substr(str_replace(array("'", '"'), array('', ''), $options['uniq']), 0, 20);
310 $options['user_id'] = 0;
311 }
312
313 $options['use_log'] = isset($options['user_id']) || isset($options['ip']);
314 $mode = 0;
315 if (!$options['use_log']) {
316 $mode = 2;
317 $options['user_id'] = 0;
318 $options['ip'] = '';
319 }
320 $result = WaicFrame::_()->getModule('chatbots')->getModel()->sendMessage($message, $botId, (int) $mode, '', false, $options);
321
322 return $result;
323 }
324 }
325
326 add_action('rest_api_init', function() {
327 register_rest_route( 'aiwu/v1', '/simple-text-query/', array(
328 'methods' => 'POST',
329 'callback' => 'aiwuSimpleTextQueryRest',
330 'permission_callback' => function( $request ) {
331 return aiwuAllowPublicApi('simple-text-query', $request );
332 },
333 ));
334 });
335 if (!function_exists('aiwuSimpleTextQueryRest')) {
336 function aiwuSimpleTextQueryRest( $request ) {
337 try {
338 $params = $request->get_params();
339 $prompt = isset( $params['prompt'] ) ? $params['prompt'] : '';
340 if (empty($prompt)) {
341 throw new Exception( 'The promp is required.' );
342 }
343 $options = isset( $params['options'] ) ? $params['options'] : [];
344 $result = aiwuSimpleTextQuery($prompt, $options);
345 return new WP_REST_Response(array('success' => true, 'data' => $result), 200);
346 }
347 catch (Exception $e) {
348 return new WP_REST_Response(array('success' => false, 'message' => $e->getMessage()), 500 );
349 }
350 }
351 }
352 add_action('rest_api_init', function() {
353 register_rest_route( 'aiwu/v1', '/ask-chatbot/', array(
354 'methods' => 'POST',
355 'callback' => 'aiwuAskChatbotRest',
356 'permission_callback' => function( $request ) {
357 return aiwuAllowPublicApi('ask-chatbot', $request );
358 },
359 ));
360 });
361 if (!function_exists('aiwuAskChatbotRest')) {
362 function aiwuAskChatbotRest( $request ) {
363 try {
364 $params = $request->get_params();
365 $message = isset( $params['message'] ) ? $params['message'] : '';
366 if (empty($message)) {
367 throw new Exception( 'The message is required.' );
368 }
369 $botId = isset( $params['bot_id'] ) ? $params['bot_id'] : '';
370 if (empty($botId)) {
371 throw new Exception( 'The bot_id is required.' );
372 }
373 $options = isset( $params['options'] ) ? $params['options'] : [];
374 $result = aiwuAskChatbot($botId, $message, $options);
375 return new WP_REST_Response(array('success' => true, 'data' => $result), 200);
376 }
377 catch (Exception $e) {
378 return new WP_REST_Response(array('success' => false, 'message' => $e->getMessage()), 500 );
379 }
380 }
381 }
382 if (!function_exists('aiwuAllowPublicApi')) {
383 function aiwuAllowPublicApi($feature, $extra) {
384 $isAdmin = current_user_can('manage_options');
385 return apply_filters('aiwu_allow_public_api', $isAdmin, $feature, $extra);
386 }
387 }
388