PluginProbe
Premmerce / 1.0
Premmerce v1.0
trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.19 1.3.2 1.3.20 1.3.21 1.3.22 1.3.23 1.3.3 1.3.4 1.3.5 1.3.6 All 28 releases
premmerce / src / Api / WizardApi.php

WizardApi.php in Premmerce 1.0, at src/Api/WizardApi.php

329 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace Premmerce\Premmerce\Api;
2
3
4 use Premmerce\Premmerce\Data\Tasks;
5
6 class WizardApi{
7
8 const KEY_POSITIONS = 'premmerce-wizard-tips-positions';
9
10 const KEY_STATES = 'premmerce-wizard-tips-states';
11
12 const KEY_SWITCHER_STATE = 'premmerce-wizard-switcher-state';
13
14
15 /**
16 * @var PluginApi
17 */
18 private $api;
19
20 /**
21 * @var array
22 */
23 private $tasks;
24
25 /**
26 * Wizard constructor.
27 *
28 * @param PluginApi $api
29 */
30 public function __construct(PluginApi $api){
31 $this->api = $api;
32 }
33
34 /**
35 * @param $state
36 */
37 public function updateContainerState($state){
38 update_option(self::KEY_SWITCHER_STATE, $state);
39 }
40
41 /**
42 * @param $positions
43 */
44 public function updatePositions($positions){
45 update_option(self::KEY_POSITIONS, $positions);
46 }
47
48 /**
49 * @param $key
50 * @param $state
51 */
52 public function updateState($key, $state){
53 $states = get_option(self::KEY_STATES, []);
54 $states[ $key ] = $state;
55 update_option(self::KEY_STATES, $states);
56
57 }
58
59 /**
60 * @return int
61 */
62 public function getUncompletedTipsCount(){
63 $tasks = $this->getTasks();
64 $checked = 0;
65
66 foreach($tasks as $task){
67 if($task['state'] !== Tasks::STATE_DEFAULT){
68 $checked ++;
69 }
70 }
71
72 return count($tasks) - $checked;
73 }
74
75 /**
76 * @return string
77 */
78 public function getContainerState(){
79 return get_option(self::KEY_SWITCHER_STATE, Tasks::STATE_DEFAULT);
80 }
81
82 /**
83 * @return array
84 */
85 private function getPositions(){
86 return get_option(self::KEY_POSITIONS, []);
87 }
88
89 /**
90 * @return array
91 */
92 private function getStates(){
93 return get_option(self::KEY_STATES, []);
94 }
95
96 /**
97 * @return array
98 */
99 public function getTasks(){
100 if(is_null($this->tasks)){
101
102 $tasks = Tasks::getAll();
103 $tasks = $this->sortTasks($tasks);
104 $tasks = $this->setStates($tasks);
105 $tasks = $this->setActions($tasks);
106 $tasks = $this->checkDependencies($tasks);
107
108 $this->tasks = $tasks;
109
110 // $this->tasks = $this->checkDependencies(
111 // $this->setActions(
112 // $this->setStates(
113 // $this->sortTasks(
114 // Tasks::getAll()
115 // )
116 // )
117 // )
118 // );
119 }
120
121 return $this->tasks;
122 }
123
124 /**
125 * @todo run last in getTasks chain
126 *
127 * @param $tasks
128 *
129 * @return mixed
130 */
131 private function removeActionsFromAutoChecked($tasks){
132 foreach($tasks as $key => &$task){
133 if(Tasks::STATE_CHECKED === $task['state'] && Tasks::TYPE_AUTO === $task['type']){
134 unset($task['actions']);
135 }
136 }
137
138 return $tasks;
139 }
140
141
142 /**
143 *
144 * @param array $tasks
145 *
146 * @return array
147 */
148 private function setStates($tasks){
149 $states = $this->getStates();
150
151 foreach($tasks as $key => &$item){
152
153 $state = isset($states[ $key ])? $states[ $key ] : Tasks::STATE_DEFAULT;
154 $type = $item['type'];
155
156 if(Tasks::TYPE_AUTO === $type){
157
158 $stateCallBack = isset($item['state_callback'])? $item['state_callback'] : null;
159
160 if(is_callable($stateCallBack)){
161 $auto = call_user_func($stateCallBack, $this->api);
162 }else{
163 $auto = $this->checkAuto($key);
164 }
165
166 if(Tasks::STATE_CHECKED === $auto){
167 $state = $auto;
168 }
169 }
170
171
172 $item['state'] = $state;
173 $item['type'] = $type;
174 }
175
176
177 return $tasks;
178 }
179
180 /**
181 * @param $tasks
182 *
183 * @return array
184 */
185 private function checkDependencies($tasks){
186 foreach($tasks as $key => &$item){
187 $dependencies = isset($item['depends'])? $item['depends'] : null;
188
189 if(is_array($dependencies)){
190 $resolved = true;
191 foreach($dependencies as $dependency){
192 if(isset($tasks[ $dependency ])){
193 $depResolved = $tasks[ $dependency ]['state'] === Tasks::STATE_CHECKED;
194 $resolved = $resolved && $depResolved;
195 if(!$depResolved){
196 $item['unresolved'][ $dependency ] = $tasks[ $dependency ]['title'];
197 }
198 }
199 }
200
201 if(!$resolved){
202 $item['locked'] = true;
203 }
204 }
205 }
206
207 return $tasks;
208 }
209
210 /**
211 * @param array $tasks
212 *
213 * @return array
214 */
215 private function setActions($tasks){
216 foreach($tasks as $key => &$item){
217
218 $action = $this->getActions($key);
219
220 if(!is_null($action)){
221 $item['actions'][] = $action;
222 }
223
224 }
225
226 return $tasks;
227 }
228
229 /**
230 * Sort tasks by user positions
231 *
232 * @param $tasks
233 *
234 * @return array
235 */
236 private function sortTasks($tasks){
237 $ordered = [];
238 $positions = $this->getPositions();
239
240 foreach($positions as $item){
241 if(isset($tasks[ $item ])){
242 $ordered[ $item ] = $tasks[ $item ];
243 unset($tasks[ $item ]);
244 }
245 }
246
247 return array_merge($tasks, $ordered);
248 }
249
250 /**
251 * @param string $key
252 *
253 * @return array
254 */
255 public function getActions($key){
256 if(strpos($key, Tasks::MAGIC_INSTALL_PLUGIN) === 0){
257 $plugin = str_replace(Tasks::MAGIC_INSTALL_PLUGIN, '', $key);
258 $installed = $this->api->isInstalledPlugin($plugin);
259 if(!$installed){
260 return [
261 'label' => __('Install', 'premmerce'),
262 'data' => [
263 'handler' => 'premmerce.plugins',
264 'option' => 'install',
265 'action' => 'premmerce_actions',
266 'value' => $plugin,
267 ],
268 ];
269 }elseif($installed && !$this->api->isActivePlugin($plugin)){
270 return [
271 'label' => __('Activate', 'premmerce'),
272 'data' => [
273 'handler' => 'premmerce.plugins',
274 'option' => 'activate',
275 'action' => 'premmerce_actions',
276 'value' => $plugin,
277 ],
278 ];
279 }
280 };
281 if(strpos($key, Tasks::MAGIC_INSTALL_THEME) === 0){
282 $theme = str_replace(Tasks::MAGIC_INSTALL_THEME, '', $key);
283 $isInstalled = $this->api->isInstalledTheme($theme);
284 if(!$isInstalled){
285 return [
286 'label' => __('Install', 'premmerce'),
287 'data' => [
288 'handler' => 'wp.updates',
289 'action' => 'install-theme',
290 'slug' => $theme,
291 ],
292 ];
293 }elseif($isInstalled && !$this->api->isActiveTheme('storefront')){
294 return [
295 'label' => __('Activate', 'premmerce'),
296 'data' => [
297 'handler' => 'premmerce.plugins',
298 'option' => "activate_theme",
299 'action' => 'premmerce_actions',
300 'value' => $theme,
301 ],
302 ];
303 }
304 }
305 }
306
307 /**
308 * @param string $key
309 *
310 * @return null|string
311 */
312 public function checkAuto($key){
313
314 if(strpos($key, Tasks::MAGIC_INSTALL_PLUGIN) === 0){
315 $plugin = str_replace(Tasks::MAGIC_INSTALL_PLUGIN, '', $key);
316
317 return $this->api->isInstalledPlugin($plugin) && $this->api->isActivePlugin($plugin)? Tasks::STATE_CHECKED : Tasks::STATE_DEFAULT;
318 }
319
320 if(strpos($key, Tasks::MAGIC_INSTALL_THEME) === 0){
321 $theme = str_replace(Tasks::MAGIC_INSTALL_THEME, '', $key);
322
323 return $this->api->isInstalledTheme($theme) && $this->api->isActiveTheme($theme)? Tasks::STATE_CHECKED : Tasks::STATE_DEFAULT;
324 }
325
326 return null;
327 }
328
329 }