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 / controller.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
controller.php
201 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 abstract class WaicController {
6 protected $_models = array();
7 protected $_views = array();
8 protected $_task = '';
9 protected $_defaultView = '';
10 protected $_code = '';
11 public function __construct( $code ) {
12 $this->setCode($code);
13 $this->_defaultView = $this->getCode();
14 }
15 public function init() {
16 /*load model and other preload data goes here*/
17 }
18 protected function _onBeforeInit() {
19 }
20 protected function _onAfterInit() {
21 }
22 public function setCode( $code ) {
23 $this->_code = $code;
24 }
25 public function getCode() {
26 return $this->_code;
27 }
28 public function exec( $task = '' ) {
29 if (method_exists($this, $task)) {
30 $this->_task = $task; //For multicontrollers module version - who know, maybe that's will be?))
31 return $this->$task();
32 }
33 return null;
34 }
35 public function getView( $name = '' ) {
36 if (empty($name)) {
37 $name = $this->getCode();
38 }
39 if (!isset($this->_views[$name])) {
40 $this->_views[$name] = $this->_createView($name);
41 }
42 return $this->_views[$name];
43 }
44 public function getModel( $name = '' ) {
45 if (!$name) {
46 $name = $this->_code;
47 }
48 if (!isset($this->_models[$name])) {
49 $this->_models[$name] = $this->_createModel($name);
50 }
51 return $this->_models[$name];
52 }
53 protected function _createModel( $name = '' ) {
54 if (empty($name)) {
55 $name = $this->getCode();
56 }
57 $parentModule = WaicFrame::_()->getModule( $this->getCode() );
58 $className = '';
59 if (waicImport($parentModule->getModDir() . 'models' . WAIC_DS . $name . '.php')) {
60 $className = waicToeGetClassName($name . 'Model');
61 }
62
63 if ($className) {
64 $model = new $className();
65 $model->setCode( $this->getCode() );
66 return $model;
67 }
68 return null;
69 }
70 protected function _createView( $name = '' ) {
71 if (empty($name)) {
72 $name = $this->getCode();
73 }
74 $parentModule = WaicFrame::_()->getModule( $this->getCode() );
75 $className = '';
76
77 if (waicImport($parentModule->getModDir() . 'views' . WAIC_DS . $name . '.php')) {
78 $className = waicToeGetClassName($name . 'View');
79 }
80
81 if ($className) {
82 $view = new $className();
83 $view->setCode( $this->getCode() );
84 return $view;
85 }
86 return null;
87 }
88 public function display( $viewName = '' ) {
89 $view = $this->getView($viewName);
90 if (null === $view) {
91 $view = $this->getView(); //Get default view
92 }
93 if ($view) {
94 $view->display();
95 }
96 }
97 public function __call( $name, $arguments ) {
98 $model = $this->getModel();
99 if (method_exists($model, $name)) {
100 return $model->$name($arguments[0]);
101 } else {
102 return false;
103 }
104 }
105 /**
106 * Retrive permissions for controller methods if exist.
107 * If need - should be redefined in each controller where it required.
108 *
109 * @return array with permissions
110 * Can be used on of sub-array - WAIC_METHODS or WAIC_USERLEVELS
111 */
112 public function getPermissions() {
113 return array();
114 }
115 /**
116 * Methods that require nonce to be generated
117 * If need - should be redefined in each controller where it required.
118 *
119 * @return array
120 */
121 public function getNoncedMethods() {
122 return array();
123 }
124 public function allowNoprivAjax( $action ) {
125 return false;
126 }
127 protected function _getAdminAjaxCap() {
128 $cap = 'manage_options';
129 $adminMenu = WaicFrame::_()->getModule('adminmenu');
130 if ($adminMenu && method_exists($adminMenu, 'getMainCap')) {
131 $mainCap = $adminMenu->getMainCap();
132 if (!empty($mainCap)) {
133 $cap = $mainCap;
134 }
135 }
136 return $cap;
137 }
138 protected function _ajaxSecurityError( $message ) {
139 if (function_exists('status_header')) {
140 status_header(403);
141 }
142 $res = new WaicResponse();
143 $res->pushError($message);
144 return $res->ajaxExec(true);
145 }
146 protected function _checkAdminAjaxSecurity() {
147 if (false === check_ajax_referer('waic-nonce', 'waicNonce', false)) {
148 return $this->_ajaxSecurityError(esc_html__('Security check failed', 'ai-copilot-content-generator'));
149 }
150 if (!current_user_can($this->_getAdminAjaxCap())) {
151 return $this->_ajaxSecurityError(esc_html__('You have no permissions to view this page', 'ai-copilot-content-generator'));
152 }
153 return true;
154 }
155 public function getModule() {
156 return WaicFrame::_()->getModule( $this->getCode() );
157 }
158 protected function _prepareTextLikeSearch( $val ) {
159 return ''; // Should be re-defined for each type
160 }
161 protected function _prepareModelBeforeListSelect( $model ) {
162 return $model->setSelectFields('*');
163 }
164 public function removeGroup() {
165 if (true !== $this->_checkAdminAjaxSecurity()) {
166 return;
167 }
168 $res = new WaicResponse();
169 if ($this->getModel()->removeGroup(WaicReq::getVar('ids', 'post'))) {
170 $res->addMessage(esc_html__('Done', 'ai-copilot-content-generator'));
171 } else {
172 $res->pushError($this->getModel()->getErrors());
173 }
174 $res->ajaxExec();
175 }
176 public function clear() {
177 if (true !== $this->_checkAdminAjaxSecurity()) {
178 return;
179 }
180 $res = new WaicResponse();
181 if ($this->getModel()->clear()) {
182 $res->addMessage(esc_html__('Done', 'ai-copilot-content-generator'));
183 } else {
184 $res->pushError($this->getModel()->getErrors());
185 }
186 $res->ajaxExec();
187 }
188 protected function _prepareListForTbl( $data ) {
189 return $data;
190 }
191 protected function _prepareSearchField( $searchField ) {
192 return $searchField;
193 }
194 protected function _prepareSearchString( $searchString ) {
195 return $searchString;
196 }
197 protected function _prepareSortOrder( $sortOrder ) {
198 return $sortOrder;
199 }
200 }
201