ai-copilot-content-generator
Last commit date
classes
1 year ago
common
1 year ago
languages
1 year ago
logs
1 year ago
modules
1 year ago
ai-copilot-content-generator.php
1 year ago
config.php
1 year ago
functions.php
1 year ago
license.txt
1 year ago
readme.txt
1 year ago
functions.php
269 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 |