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 / classes / frame.php
ai-copilot-content-generator / classes Last commit date
helpers 1 week ago tables 1 week ago aIProviderInterface.php 1 week ago assets.php 1 week ago baseObject.php 1 week ago builderBlock.php 1 week ago controller.php 1 week ago date.php 1 week ago db.php 1 week ago dispatcher.php 1 week ago errors.php 1 week ago field.php 1 week ago fieldAdapter.php 1 week ago frame.php 1 week ago helper.php 1 week ago html.php 1 week ago installer.php 1 week ago installerDbUpdater.php 1 week ago integration.php 1 week ago modInstaller.php 1 week ago model.php 1 week ago module.php 1 week ago req.php 1 week ago response.php 1 week ago table.php 1 week ago uri.php 1 week ago user.php 1 week ago utils.php 1 week ago validator.php 1 week ago view.php 1 week ago
frame.php
552 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 class WaicFrame extends WaicBaseObject {
6 private $_modules = array();
7 private $_tables = array();
8 private $_allModules = array();
9 /**
10 * Uses to know if we are on one of the plugin pages
11 */
12 private $_inPlugin = false;
13 /**
14 * Array to hold all scripts and add them in one time in addScripts method
15 */
16 private $_scripts = array();
17 private $_scriptsInitialized = false;
18 private $_styles = array();
19 private $_stylesInitialized = false;
20 private $_useFootAssets = false;
21
22 private $_scriptsVars = array();
23 private $_mod = '';
24 private $_action = '';
25 /**
26 * Object with result of executing non-ajax module request
27 */
28 private $_res = null;
29
30 public function __construct() {
31 $this->_res = waicToeCreateObj('response', array());
32 }
33 public static function getInstance() {
34 static $instance;
35 if (!$instance) {
36 $instance = new WaicFrame();
37 }
38 return $instance;
39 }
40 public static function _() {
41 return self::getInstance();
42 }
43 public function parseRoute() {
44 // Check plugin
45 $pl = WaicReq::getVar('pl');
46 if (WAIC_CODE == $pl) {
47 $mod = WaicReq::getMode();
48 if ($mod) {
49 $this->_mod = $mod;
50 }
51 $action = WaicReq::getVar('action');
52 if ($action) {
53 $this->_action = $action;
54 }
55 }
56 }
57 public function setMod( $mod ) {
58 $this->_mod = $mod;
59 }
60 public function getMod() {
61 return $this->_mod;
62 }
63 public function setAction( $action ) {
64 $this->_action = $action;
65 }
66 public function getAction() {
67 return $this->_action;
68 }
69 protected function _extractModules() {
70 $activeModules = $this->getTable('modules')->get($this->getTable('modules')->alias() . '.*');
71 if ($activeModules) {
72 foreach ($activeModules as $m) {
73 $code = $m['code'];
74 $moduleLocationDir = WAIC_MODULES_DIR;
75 if (!empty($m['ex_plug_dir'])) {
76 $moduleLocationDir = WaicUtils::getExtModDir( $m['ex_plug_dir'] );
77 }
78 if (is_dir($moduleLocationDir . $code)) {
79 $this->_allModules[$m['code']] = 1;
80 if ((bool) $m['active']) {
81 waicImportClass(waicStrFirstUp(WAIC_CODE) . $code, $moduleLocationDir . $code . WAIC_DS . 'mod.php');
82 $moduleClass = waicToeGetClassName($code);
83 if (class_exists($moduleClass)) {
84 $this->_modules[$code] = new $moduleClass($m);
85 if (is_dir($moduleLocationDir . $code . WAIC_DS . 'tables')) {
86 $this->_extractTables($moduleLocationDir . $code . WAIC_DS . 'tables' . WAIC_DS);
87 }
88 }
89 }
90 }
91 }
92 }
93 }
94 protected function _initModules() {
95 if (!empty($this->_modules)) {
96 foreach ($this->_modules as $mod) {
97 $mod->init();
98 }
99 }
100 }
101 public function init() {
102 WaicReq::init();
103 //WaicCache::_()->init();
104
105 $this->_extractTables();
106
107 $this->_extractModules();
108
109 $this->_initModules();
110
111 WaicDispatcher::doAction('afterModulesInit');
112
113 WaicModInstaller::checkActivationMessages();
114
115 $this->_execModules();
116 if ($this->isSuccessInit()) {
117 WaicAssets::_()->init();
118 }
119
120 $addAssetsAction = $this->usePackAssets() && !is_admin() ? 'wp_footer' : 'init';
121
122 add_action($addAssetsAction, array($this, 'addScripts'));
123 add_action($addAssetsAction, array($this, 'addStyles'));
124 //add_action('wp_enqueue_scripts', array($this, 'addStyles'));
125 global $waicLangOK;
126 register_activation_hook(WAIC_DIR . WAIC_DS . WAIC_MAIN_FILE, array('WaicUtils', 'activatePlugin')); //See classes/install.php file
127 register_uninstall_hook(WAIC_DIR . WAIC_DS . WAIC_MAIN_FILE, array('WaicUtils', 'deletePlugin'));
128 register_deactivation_hook(WAIC_DIR . WAIC_DS . WAIC_MAIN_FILE, array( 'WaicUtils', 'deactivatePlugin' ) );
129
130 //add_action('init', array($this, 'connectLang'));
131 $waicLangOK = true;
132 //WaicUtils::setTimeZone();
133 }
134 public function isSuccessInit() {
135 return !empty($this->_modules) && $this->getModule('options') && $this->getModule('adminmenu');
136 }
137 /*public function connectLang() {
138 global $waicLangOK;
139 $waicLangOK = load_plugin_textdomain('ai-copilot-content-generator', false, WAIC_PLUG_NAME . '/languages/');
140 }*/
141 /**
142 * Check permissions for action in controller by $code and made corresponding action
143 *
144 * @param string $code Code of controller that need to be checked
145 * @param string $action Action that need to be checked
146 * @return bool true if ok, else - should exit from application
147 */
148 public function checkPermissions( $code, $action ) {
149 //return true;
150 if ($this->havePermissions($code, $action)) {
151 return true;
152 } else {
153 exit(esc_html_e('You have no permissions to view this page', 'ai-copilot-content-generator'));
154 }
155 }
156 /**
157 * Check permissions for action in controller by $code
158 *
159 * @param string $code Code of controller that need to be checked
160 * @param string $action Action that need to be checked
161 * @return bool true if ok, else - false
162 */
163 public function havePermissions( $code, $action ) {
164 $res = true;
165 $mod = $this->getModule($code);
166 $action = strtolower($action);
167 if ($mod) {
168 $permissions = $mod->getController()->getPermissions();
169 if (!empty($permissions)) { // Special permissions
170 $user = new WaicUser();
171 if (isset($permissions[WAIC_METHODS]) && !empty($permissions[WAIC_METHODS])) {
172 foreach ($permissions[WAIC_METHODS] as $method => $permissions) { // Make case-insensitive
173 $permissions[WAIC_METHODS][strtolower($method)] = $permissions;
174 }
175 if (array_key_exists($action, $permissions[WAIC_METHODS])) { // Permission for this method exists
176 $currentUserPosition = $user->getCurrentUserPosition();
177 if ( ( is_array($permissions[ WAIC_METHODS ][ $action ] ) && !in_array($currentUserPosition, $permissions[ WAIC_METHODS ][ $action ]) )
178 || ( !is_array($permissions[ WAIC_METHODS ][ $action ]) && $permissions[WAIC_METHODS][$action] != $currentUserPosition )
179 ) {
180 $res = false;
181 }
182 }
183 }
184 if (isset($permissions[WAIC_USERLEVELS]) && !empty($permissions[WAIC_USERLEVELS])) {
185 $currentUserPosition = $user->getCurrentUserPosition();
186 // For multi-sites network admin role is undefined, let's do this here
187 if (is_multisite() && is_admin() && is_super_admin()) {
188 $currentUserPosition = WAIC_ADMIN;
189 }
190 foreach ($permissions[WAIC_USERLEVELS] as $userlevel => $methods) {
191 if (is_array($methods)) {
192 $lowerMethods = array_map('strtolower', $methods); // Make case-insensitive
193 if (in_array($action, $lowerMethods)) { // Permission for this method exists
194 if ($currentUserPosition != $userlevel) {
195 $res = false;
196 }
197 break;
198 }
199 } else {
200 $lowerMethod = strtolower($methods); // Make case-insensitive
201 if ($lowerMethod == $action) { // Permission for this method exists
202 if ($currentUserPosition != $userlevel) {
203 $res = false;
204 }
205 break;
206 }
207 }
208 }
209 }
210 }
211 if ($res) { // Additional check for nonces
212 $noncedMethods = $mod->getController()->getNoncedMethods();
213 if (!empty($noncedMethods)) {
214 $noncedMethods = array_map('strtolower', $noncedMethods);
215 if (in_array($action, $noncedMethods)) {
216 check_ajax_referer('waic-nonce', 'waicNonce');
217 }
218 }
219 }
220 }
221 return $res;
222 }
223 public function getRes() {
224 return $this->_res;
225 }
226 public function execAfterWpInit() {
227 $this->_doExec();
228 }
229 /**
230 * Check if method for module require some special permission. We can detect users permissions only after wp init action was done.
231 */
232 protected function _execOnlyAfterWpInit() {
233 $res = false;
234 $mod = $this->getModule( $this->_mod );
235 $action = strtolower( $this->_action );
236 if ($mod) {
237 $permissions = $mod->getController()->getPermissions();
238 if (!empty($permissions)) { // Special permissions
239 if (isset($permissions[WAIC_METHODS]) && !empty($permissions[WAIC_METHODS])) {
240 foreach ($permissions[WAIC_METHODS] as $method => $permissions) { // Make case-insensitive
241 $permissions[WAIC_METHODS][strtolower($method)] = $permissions;
242 }
243 if (array_key_exists($action, $permissions[WAIC_METHODS])) { // Permission for this method exists
244 $res = true;
245 }
246 }
247 if (isset($permissions[WAIC_USERLEVELS]) && !empty($permissions[WAIC_USERLEVELS])) {
248 $res = true;
249 }
250 }
251
252 if (!$res) {
253 $noncedMethods = $mod->getController()->getNoncedMethods();
254 if (!empty($noncedMethods)) {
255 $noncedMethods = array_map('strtolower', $noncedMethods);
256 if (in_array($action, $noncedMethods)) {
257 $res = true;
258 }
259 }
260 }
261 }
262 return $res;
263 }
264 protected function _execModules() {
265 if ($this->_mod) {
266 // If module exist and is active
267 $mod = $this->getModule($this->_mod);
268 if ($mod && !empty($this->_action)) {
269 if ($this->_execOnlyAfterWpInit()) {
270 add_action('init', array($this, 'execAfterWpInit'));
271 } else {
272 $this->_doExec();
273 }
274 }
275 }
276 }
277 protected function _doExec() {
278 $mod = $this->getModule($this->_mod);
279 if ($mod && $this->checkPermissions($this->_mod, $this->_action)) {
280 switch (WaicReq::getVar('reqType')) {
281 case 'ajax':
282 $controller = $mod->getController();
283 add_action('wp_ajax_' . $this->_action, array($controller, $this->_action));
284 if (true === $controller->allowNoprivAjax($this->_action)) {
285 add_action('wp_ajax_nopriv_' . $this->_action, array($controller, $this->_action));
286 }
287 break;
288 default:
289 $this->_res = $mod->exec($this->_action);
290 break;
291 }
292 }
293 }
294 protected function _extractTables( $tablesDir = WAIC_TABLES_DIR ) {
295 $mDirHandle = opendir($tablesDir);
296 while ( ( $file = readdir($mDirHandle) ) !== false ) {
297 if ( is_file($tablesDir . $file) && ( '.' != $file ) && ( '..' != $file ) && strpos($file, '.php') ) {
298 $this->_extractTable( str_replace('.php', '', $file), $tablesDir );
299 }
300 }
301 }
302 protected function _extractTable( $tableName, $tablesDir = WAIC_TABLES_DIR ) {
303 waicImportClass('noClassNameHere', $tablesDir . $tableName . '.php');
304 $this->_tables[$tableName] = WaicTable::_($tableName);
305 }
306 /**
307 * Public alias for _extractTables method
308 *
309 * @see _extractTables
310 */
311 public function extractTables( $tablesDir ) {
312 if (!empty($tablesDir)) {
313 $this->_extractTables($tablesDir);
314 }
315 }
316 public function exec() {
317 //deprecated
318 }
319 public function getTables() {
320 return $this->_tables;
321 }
322 /**
323 * Return table by name
324 *
325 * @param string $tableName table name in database
326 * @return object table
327 * @example WaicFrame::_()->getTable('products')->getAll()
328 */
329 public function getTable( $tableName ) {
330 if (empty($this->_tables[$tableName])) {
331 $this->_extractTable($tableName);
332 }
333 return $this->_tables[$tableName];
334 }
335 public function getModules( $filter = array() ) {
336 $res = array();
337 if (empty($filter)) {
338 $res = $this->_modules;
339 } else {
340 foreach ($this->_modules as $code => $mod) {
341 if (isset($filter['type'])) {
342 if (is_numeric($filter['type']) && $filter['type'] == $mod->getTypeID()) {
343 $res[$code] = $mod;
344 } elseif ($filter['type'] == $mod->getType()) {
345 $res[$code] = $mod;
346 }
347 }
348 }
349 }
350 return $res;
351 }
352
353 public function getModule( $code ) {
354 return ( isset($this->_modules[$code]) ? $this->_modules[$code] : null );
355 }
356 public function inPlugin() {
357 return $this->_inPlugin;
358 }
359 public function usePackAssets() {
360 if (!$this->_useFootAssets && $this->getModule('options') && $this->getModule('options')->get('foot_assets')) {
361 $this->_useFootAssets = true;
362 }
363 return $this->_useFootAssets;
364 }
365 /**
366 * Push data to script array to use it all in addScripts method
367 *
368 * @see wp_enqueue_script definition
369 */
370 public function addScript( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false, $vars = array() ) {
371 $src = empty($src) ? $src : WaicUri::_($src);
372 if (!$ver) {
373 $ver = WAIC_VERSION;
374 }
375 if ($this->_scriptsInitialized) {
376 wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);
377 } else {
378 $this->_scripts[] = array(
379 'handle' => $handle,
380 'src' => $src,
381 'deps' => $deps,
382 'ver' => $ver,
383 'in_footer' => $in_footer,
384 'vars' => $vars,
385 );
386 }
387 }
388 /**
389 * Add all scripts from _scripts array to wordpress
390 */
391 public function addScripts() {
392 if (!empty($this->_scripts)) {
393 foreach ($this->_scripts as $s) {
394 wp_enqueue_script($s['handle'], $s['src'], $s['deps'], $s['ver'], $s['in_footer']);
395
396 if ($s['vars'] || isset($this->_scriptsVars[$s['handle']])) {
397 $vars = array();
398 if ($s['vars']) {
399 $vars = $s['vars'];
400 }
401 if ($this->_scriptsVars[$s['handle']]) {
402 $vars = array_merge($vars, $this->_scriptsVars[$s['handle']]);
403 }
404 if ($vars) {
405 foreach ($vars as $k => $v) {
406 wp_localize_script($s['handle'], $k, is_array($v) ? $v : array($v));
407 }
408 }
409 }
410 }
411 }
412 $this->_scriptsInitialized = true;
413 }
414 public function addJSVar( $script, $name, $val ) {
415 if ($this->_scriptsInitialized) {
416 wp_localize_script($script, $name, is_array($val) ? $val : array($val));
417 } else {
418 $this->_scriptsVars[$script][$name] = $val;
419 }
420 }
421
422 public function addStyle( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
423 $src = empty($src) ? $src : WaicUri::_($src);
424 if (!$ver) {
425 $ver = WAIC_VERSION;
426 }
427 if ($this->_stylesInitialized) {
428 wp_enqueue_style($handle, $src, $deps, $ver, $media);
429 } else {
430 $this->_styles[] = array(
431 'handle' => $handle,
432 'src' => $src,
433 'deps' => $deps,
434 'ver' => $ver,
435 'media' => $media,
436 );
437 }
438 }
439 public function addStyles() {
440 if (!empty($this->_styles)) {
441 foreach ($this->_styles as $s) {
442 wp_enqueue_style($s['handle'], $s['src'], $s['deps'], $s['ver'], $s['media']);
443 }
444 }
445 $this->_stylesInitialized = true;
446 }
447 //Very interesting thing going here.............
448 public function loadPlugins() {
449 require_once ABSPATH . 'wp-includes/pluggable.php';
450 }
451 public function loadWPSettings() {
452 require_once ABSPATH . 'wp-settings.php';
453 }
454 public function loadLocale() {
455 require_once ABSPATH . 'wp-includes/locale.php';
456 }
457 public function moduleActive( $code ) {
458 return isset($this->_modules[$code]);
459 }
460 public function moduleExists( $code ) {
461 if ($this->moduleActive($code)) {
462 return true;
463 }
464 return isset($this->_allModules[$code]);
465 }
466 public function isTplEditor() {
467 $tplEditor = WaicReq::getVar('tplEditor');
468 return (bool) $tplEditor;
469 }
470 /**
471 * This is custom method for each plugin and should be modified if you create copy from this instance.
472 */
473 public function isAdminPlugOptsPage() {
474 $page = WaicReq::getVar('page');
475 if (is_admin() && !empty($page) && strpos($page, self::_()->getModule('adminmenu')->getMainSlug()) !== false) {
476 return true;
477 }
478 return false;
479 }
480 public function isAdminPlugPage() {
481 if ($this->isAdminPlugOptsPage()) {
482 return true;
483 }
484 return false;
485 }
486 public function licenseDeactivated() {
487 return ( !$this->getModule('license') && $this->moduleExists('license') );
488 }
489 public function savePluginActivationErrors() {
490 update_option(WAIC_CODE . '_plugin_activation_errors', ob_get_contents());
491 }
492 public function getActivationErrors() {
493 return get_option(WAIC_CODE . '_plugin_activation_errors');
494 }
495 public function isPro( $isActive = true ) {
496 if ($isActive) {
497 return $this->moduleExists('license') && $this->getModule('license') && $this->moduleExists('postscreatepro') && $this->getModule('postscreatepro');
498 }
499 return $this->moduleExists('license') && $this->getModule('license');
500 }
501 public function getProUrl() {
502 return 'https://aiwuplugin.com';
503 }
504 private function _writeLog( $data, $type ) {
505 if (!file_exists(WAIC_LOG_DIR)) {
506 wp_mkdir_p(WAIC_LOG_DIR);
507 $indexFile = WAIC_LOG_DIR . WAIC_DS . 'index.php';
508 file_put_contents($indexFile, '');
509 }
510
511 /*if (is_array($data)) {
512 ob_start();
513 var_dump($data);
514 $data = ob_get_clean();
515 }*/
516 if (is_array($data)) {
517 //$data = print_r($data, true);
518 $data = wp_json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
519 }
520 $data = gmdate('c') . ' ' . $type . ': ' . $data . PHP_EOL;
521 file_put_contents(WAIC_LOG_DIR . WAIC_DS . 'waic' . gmdate('Y-m-d') . '.log', $data, FILE_APPEND);
522 }
523 public function saveDebugLogging( $debug = '', $wc = true, $typ = 'DEBUG' ) {
524 if ($this->getModule('options')->getModel()->get('plugin', 'logging') == 1) {
525 if (!empty($debug)) {
526 if ($wc && function_exists('wc_get_logger')) {
527 $logger = wc_get_logger();
528 if ($logger) {
529 $logger->debug(wc_print_r($debug, true), array('source' => 'waic-debug-logging'));
530 }
531 } else {
532 $this->_writeLog($debug, $typ);
533 }
534 } else if ($this->haveErrors()) {
535 if ($wc && function_exists('wc_get_logger')) {
536 $logger = wc_get_logger();
537 if ($logger) {
538 $logger->Warning(wc_print_r($this->getErrors(), true), array('source' => 'waic-debug-logging'));
539 }
540 } else {
541 $this->_writeLog($this->getErrors(), 'WARNING');
542 }
543 }
544 }
545 }
546 public function printInlineJs( $js, $attrs = array() ) {
547 if (function_exists('wp_print_inline_script_tag')) {
548 wp_print_inline_script_tag($js, $attrs);
549 }
550 }
551 }
552