PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / hc3 / _wordpress / abstract / plugin.php

plugin.php in ShiftController Employee Shift Scheduling 4.9.26, at hc3/_wordpress/abstract/plugin.php

700 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 define( 'HC3_VERSION', 102 );
3
4 abstract class HC3_Abstract_Plugin
5 {
6 public static $instance;
7
8 public $translate;
9 public $slug;
10 public $label;
11 public $prfx;
12 public $prefix;
13
14 protected $modules = array();
15 public $dirs = array();
16 public $libDirs = array();
17
18 protected $dic = NULL;
19 protected $actionResult = NULL;
20 protected $hcs = 'hcs'; // get/post param to intercept
21 protected $hcj = 'hcj'; // get/post param to intercept
22
23 protected $file;
24 protected $menuIcon = NULL;
25
26 protected $requireCap = 'read';
27
28 public function __construct( $file )
29 {
30 self::$instance = $this;
31
32 $this->libDirs = array();
33 $this->file = $file;
34
35 $this->prefix = $this->prfx . '_';
36
37 spl_autoload_register( array($this, 'autoload') );
38
39 add_action( 'init', array($this, '_init') );
40 add_action( 'init', array($this, 'intercept') );
41 add_action( 'admin_init', array($this, 'adminInit') );
42 add_action( 'admin_menu', array($this, 'adminMenu') );
43 add_filter( 'parent_file', array($this, 'setCurrentAppMenu') );
44 if( $this->isMeAdmin() ){
45 add_action( 'admin_enqueue_scripts', array($this, 'scripts') );
46 }
47 }
48
49 public function _init()
50 {
51 $pluginDir = dirname($this->file);
52
53 $filter_name = $this->prefix . 'modules';
54 $this->modules = apply_filters( $filter_name, $this->modules );
55
56 $dir =trim( $this->prefix, '_' );
57 $this->dirs = array( $pluginDir . '/' . $dir );
58
59 $filter_name = $this->prefix . 'dirs';
60 $this->dirs = apply_filters( $filter_name, $this->dirs );
61
62 $profiler = new HC3_Profiler;
63 $profiler->mark( 'total_start' );
64
65 $dic = new HC3_Dic;
66 $dic->bind( $profiler );
67
68 $hooks = new HC3_Hooks( $dic, $this->slug, $this->prefix );
69 $dic->bind( $hooks );
70
71 $crudFactory = new HC3_CrudFactory( $this->prefix );
72 $settings = new HC3_Settings( $this->prefix );
73
74 $dic
75 ->bind( $crudFactory )
76 // ->bind( $translate )
77 ->bind( $settings )
78 ;
79
80 $lang = $settings->get('lang');
81
82 // Polylang plugin
83 if( function_exists('pll_current_language') ){
84 $lang = pll_current_language( 'locale' );
85 }
86
87 // $langDomain = $this->translate;
88 // $dir = dirname($this->file);
89 // $langDir = plugin_basename($dir) . '/languages';
90 // $langFullDir = $dir . '/languages';
91 // load_plugin_textdomain( $langDomain, '', $langDir );
92
93 $translate = new HC3_Translate( $this->translate, $pluginDir, $lang );
94 $dic
95 ->bind( $translate )
96 ;
97
98 reset( $this->modules );
99 foreach( $this->modules as $moduleName ){
100 $moduleBootClass = $this->prefix . $moduleName . '_Boot';
101 $module = $dic->make( $moduleBootClass );
102 }
103
104 $url = parse_url( site_url('/') );
105
106 $baseUrl = $url['scheme'] . '://'. $url['host'];
107 if( isset($url['port']) && (80 != $url['port']) ){
108 $baseUrl .= ':' . $url['port'];
109 }
110 $baseUrl .= $url['path'];
111
112 $actionUrl = (isset($url['query']) && $url['query']) ? '?' . $url['query'] . '&' : '?';
113 $actionUrl .= $this->hcs . '=' . $this->prfx;
114 $actionUrl = $baseUrl . $actionUrl;
115
116 $uriAction = $dic->make('HC3_UriAction');
117 // echo "SETTING '$actionUrl'<br>";
118 $uriAction->fromUrl( $actionUrl );
119
120 $this->dic = $dic;
121
122 $action_name = $this->prefix . 'init';
123 do_action( $action_name, $this );
124
125 add_filter( $this->slug, array($this, 'api'), 10, 4 );
126 }
127
128 public function api( $handler, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL )
129 {
130 $return = NULL;
131
132 $handler = trim( $handler );
133 $handler = strtolower( $handler );
134 if( FALSE === strpos($handler, '@') ){
135 $method = '__invoke';
136 }
137 else {
138 list( $handler, $method ) = explode( '@', $handler );
139 }
140
141 $handler = $this->dic->make( $handler );
142
143 try {
144 $args = array();
145 if( NULL !== $arg1 ){
146 $args[] = $arg1;
147 }
148 if( NULL !== $arg2 ){
149 $args[] = $arg2;
150 }
151 if( NULL !== $arg3 ){
152 $args[] = $arg3;
153 }
154 $return = call_user_func_array( array($handler, $method), $args );
155 }
156 catch( Exception $e ){
157 }
158
159 return $return;
160 }
161
162 public function adminMenu()
163 {
164 $mainMenuSlug = $this->slug;
165 $mainLabel = get_site_option( $this->slug . '_menu_title' );
166
167 if( ! strlen($mainLabel) ){
168 $mainLabel = $this->label;
169 }
170
171 $page = add_menu_page(
172 $mainLabel,
173 $mainLabel,
174 $this->requireCap,
175 $mainMenuSlug,
176 array($this, 'render'),
177 $this->menuIcon,
178 30
179 );
180
181 // submenu
182 $root = $this->root();
183
184 $topmenu = $root->make('HC3_Ui_Topmenu');
185 $uri = $root->make('HC3_Uri');
186 $mainHref = get_admin_url() . 'admin.php?page=' . $this->slug;
187 $uri->fromUrl( $mainHref );
188
189 $ui = $root->make('HC3_Ui');
190 $translate = $root->make('HC3_Translate');
191 $filter = $root->make('HC3_Ui_Filter');
192
193 $menuItems = $topmenu->getChildren();
194
195 $mySubmenuCount = 0;
196 global $submenu;
197
198 foreach( $menuItems as $menuItem ){
199 list( $slug, $label ) = $menuItem;
200
201 $ahref = $ui->makeAhref( $slug, $label );
202 $ahref = $filter->filter( $ahref );
203
204 $strAhref = '' . $ahref;
205 if( ! strlen($strAhref) ){
206 continue;
207 }
208
209 if( $uri->isFullUrl($slug) ){
210 $ahref->newWindow();
211 }
212
213 $href = $ahref->getAttr('href');
214 $label = $translate->translate( $label );
215
216 $pageTitle = $label;
217 $menuTitle = strip_tags( $label );
218
219 remove_submenu_page( $mainMenuSlug, $href );
220
221 $childMenuSlug = $mainMenuSlug . '-' . ($mySubmenuCount + 1);
222
223 $ret = add_submenu_page(
224 $mainMenuSlug, // parent
225 $pageTitle, // page_title
226 $menuTitle, // menu_title
227 $this->requireCap, // capability
228 $childMenuSlug, // menu_slug
229 array($this, 'render')
230 // '__return_null'
231 );
232
233 if( ! array_key_exists($mainMenuSlug, $submenu) ){
234 continue;
235 }
236
237 $mySubmenu = $submenu[$mainMenuSlug];
238 $mySubmenuIds = array_keys($mySubmenu);
239 $mySubmenuId = array_pop($mySubmenuIds);
240
241 $submenu[$mainMenuSlug][$mySubmenuId][2] = $href;
242 $mySubmenuCount++;
243 }
244
245 if( isset($submenu[$mainMenuSlug][0]) && ($submenu[$mainMenuSlug][0][2] == $mainMenuSlug) ){
246 unset($submenu[$mainMenuSlug][0]);
247 }
248
249 if( ! $mySubmenuCount ){
250 remove_menu_page( $mainMenuSlug );
251 }
252 }
253
254 public function setCurrentAppMenu( $parent_file )
255 {
256 global $submenu_file, $current_screen, $pagenow;
257
258 $menuSlug = $this->slug;
259 $typePrefix = $this->prefix;
260
261 $my = FALSE;
262 if( $current_screen->base == 'toplevel_page_' . $menuSlug ){
263 $my = TRUE;
264 }
265
266 if( substr($current_screen->post_type, 0, strlen($typePrefix)) == $typePrefix ){
267 $my = TRUE;
268 }
269
270 if( ! $my ){
271 return $parent_file;
272 }
273
274 switch( $pagenow ){
275 case 'post-new.php':
276 $submenu_file = 'edit.php?post_type=' . $current_screen->post_type;
277 break;
278
279 case 'edit-tags.php':
280 $submenu_file = 'edit-tags.php?taxonomy=' . $current_screen->taxonomy . '&post_type=' . $current_screen->post_type;
281 break;
282
283 case 'admin.php':
284 $root = $this->root();
285
286 $uri = $root->make('HC3_Uri');
287 $currentUrl = $uri->currentUrl();
288 $shortCurrentUrl = basename( $currentUrl );
289
290 global $submenu;
291 if( array_key_exists($menuSlug, $submenu) ){
292 foreach( $submenu[$menuSlug] as $sbm ){
293 if( substr($sbm[2], -strlen($shortCurrentUrl)) == $shortCurrentUrl ){
294 $submenu_file = $sbm[2];
295 break;
296 }
297 }
298 }
299 break;
300
301 default:
302 break;
303 }
304
305 $parent_file = $menuSlug;
306 return $parent_file;
307 }
308
309 public function scripts()
310 {
311 $root = $this->root();
312 $enqueuer = $root->make('HC3_Enqueuer');
313 }
314
315 public function handleRequest( $defaultSlug = '' )
316 {
317 $result = NULL;
318 $root = $this->root();
319
320 $profiler = $root->make('HC3_Profiler');
321
322 $profiler->mark('action_start');
323
324 $uri = $root->make('HC3_Uri');
325 $uri->setAssetsPath( plugins_url('', $this->file) );
326
327 // if( $this->isIntercepted() ){
328 // $mainHref = get_admin_url() . 'admin.php?page=' . $this->slug;
329 // $uri->fromUrl( $mainHref );
330 // }
331
332 $request = $root->make('HC3_Request');
333 $csrf = $root->make('HC3_Csrf');
334 $router = $root->make('HC3_Router');
335
336 $session = HC3_Session::instance();
337
338 $requestMethod = $request->getMethod();
339 $requestParams = $request->getParams();
340 $slug = $request->getSlug();
341
342 $ajax = FALSE;
343 if( substr($slug, 0, strlen('ajax/')) == 'ajax/' ){
344 $ajax = TRUE;
345 }
346 else {
347 $ajax = $request->isAjax() ? TRUE : FALSE;
348 }
349
350 if( ! $slug ){
351 $slug = $defaultSlug;
352 }
353
354 $slug = $requestMethod . ':' . $slug;
355
356 // first acl
357 $acl = $root->make('HC3_Acl');
358 if( ! $acl->check($slug, $requestParams) ){
359 // if not logged in then it may need to log in
360 if( ! get_current_user_id() ){
361 $returnTo = $uri->currentUrl();
362 $to = wp_login_url( $returnTo );
363
364 if( ! headers_sent() ){
365 wp_redirect( $to );
366 }
367 else {
368 $html = "<META http-equiv=\"refresh\" content=\"0;URL=$to\">";
369 echo $html;
370 exit;
371 }
372 }
373 else {
374 $result = 'not allowed';
375 return $result;
376 }
377 }
378
379 list( $handler, $args ) = $router->getHandlerArgs( $slug );
380
381 if( 'post' == $requestMethod ){
382 $csrf->checkInput();
383 }
384
385 // echo "HANDLER = '$handler' FOR SLUG = '$slug'<br>";
386 try {
387 list( $handler, $method ) = is_array($handler) ? $handler : array( $handler, 'execute' );
388 // echo "HANDLER = $handler";
389 if( $handler ){
390 $handler = $root->make( $handler );
391 $result = call_user_func_array( array($handler, $method), $args );
392 }
393 else {
394 $result = "nothing to handle this request: '$slug'";
395 $result = esc_html( $result );
396 }
397 }
398 catch( HC3_ExceptionArray $e ){
399 $session->setFlashdata('form_errors', $e->getErrors());
400 $result = array('-referrer-', NULL);
401 }
402
403 // message and redirect
404 if( is_array($result) ){
405 $isError = false;
406
407 if( count($result) > 2 ){
408 list( $to, $msg, $isError ) = $result;
409 }
410 else {
411 list( $to, $msg ) = $result;
412 }
413
414 $post = $root->make('HC3_Post')
415 ->get()
416 ;
417
418 if( $isError ){
419 $session->setFlashdata('error', $msg);
420 }
421 else {
422 $session->setFlashdata('message', $msg);
423 }
424
425 $session->setFlashdata('post', $post);
426
427 $to = $uri->makeUrl( $to );
428
429 $notificator = $root->make('HC3_Notificator');
430 $notificator->send();
431
432 if( $ajax ){
433 $out = array('redirect' => $to);
434 $out = json_encode( $out );
435 echo $out;
436 }
437 else {
438 if( ! headers_sent() ){
439 wp_redirect( $to );
440 }
441 else {
442 if( defined('HC3_DEV_INSTALL') ){
443 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
444 }
445 else {
446 $html = "<META http-equiv=\"refresh\" content=\"0;URL=$to\">";
447 }
448 echo $html;
449 }
450 }
451 exit;
452 }
453
454 $enqueuer = $root->make('HC3_Enqueuer');
455 $enqueuer
456 ->addStyle('hc', 'hc3/assets/css/hc.css?hcver=' . HC3_VERSION)
457 ;
458 $profiler->mark('action_end');
459
460 if( $request->isPrintView() ){
461 $this->actionResult = $result;
462 echo $this->render();
463 exit;
464 }
465
466 $enqueuer
467 ->addScript('hc', 'hc3/assets/js/hc2.js?hcver=' . HC3_VERSION)
468 ;
469
470 return $result;
471 }
472
473 public function render()
474 {
475 $root = $this->root();
476
477 $request = $root->make('HC3_Request');
478 $isPrintView = $request->isPrintView();
479
480 $slug = $request->getSlug();
481
482 $ajax = FALSE;
483 if( substr($slug, 0, strlen('ajax/')) == 'ajax/' ){
484 $ajax = TRUE;
485 }
486 else {
487 $ajax = $request->isAjax() ? TRUE : FALSE;
488 }
489
490 $profiler = $root->make('HC3_Profiler');
491 $profiler->mark('render_start');
492
493 $result = $this->actionResult;
494
495 $profiler = $root->make('HC3_Profiler');
496 $csrf = $root->make('HC3_Csrf');
497
498 // view
499 // add announce
500 if( ! $ajax ){
501 $announce = $root->make('HC3_Ui_Announce');
502 $result = $announce->render( $result );
503
504 // add top menu
505 // $layout = $root->make('HC3_Ui_Layout');
506 // $result = $layout->render( $result );
507 }
508
509 // filter output
510 $filter = $root->make('HC3_Ui_Filter');
511 $result = $filter->filter( $result );
512
513 if( ! $ajax ){
514 $layout = $root->make('HC3_Layout');
515 if( $isPrintView ){
516 $result = $layout->renderPrint( $result );
517 }
518 else {
519 $result = $layout->render( $result );
520 }
521 }
522
523 $translate = $root->make('HC3_Translate');
524 $result = $translate->translate( $result );
525
526 $result = $csrf->prepareOutput( $result );
527
528 $profiler->mark('render_end');
529 $profiler->mark('total_end');
530
531 if( defined('HC3_PROFILER') && HC3_PROFILER && (! $isPrintView) && (! $ajax) ){
532 $result = $profiler->render( $result );
533 }
534
535 echo $result;
536 }
537
538 public function adminInit()
539 {
540 if( $this->isMeAdmin() ){
541 $this->actionResult = $this->handleRequest();
542 }
543 }
544
545 public function isMeAdmin()
546 {
547 $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
548 if( isset($_REQUEST['page']) ){
549 $page = sanitize_text_field($_REQUEST['page']);
550 }
551
552 if( $page && ($page == $this->slug) ){
553 $return = TRUE;
554 }
555 else {
556 $return = FALSE;
557 }
558
559 return $return;
560 }
561
562 public function autoload( $inclass )
563 {
564 $class = strtolower( $inclass );
565 // check & strip prefix
566 if( substr($class, 0, strlen('hc3_')) == 'hc3_' ){
567 $is_lib = TRUE;
568 $class = substr($class, strlen('hc3_'));
569 }
570 elseif( substr($class, 0, strlen($this->prefix)) == $this->prefix ){
571 $is_lib = FALSE;
572 $class = substr($class, strlen($this->prefix));
573 }
574 else {
575 return;
576 }
577
578 $class_array = explode('_', $class);
579 $path = $class_array;
580
581 $start_dirs = array();
582 if( $is_lib ){
583 $lib_dir = defined('HC3_DEV_INSTALL') ? HC3_DEV_INSTALL : dirname($this->file) . '/hc3';
584 $start_dirs[] = $lib_dir;
585 $start_dirs[] = $lib_dir . '/_interface';
586 $start_dirs[] = $lib_dir . '/_wordpress';
587
588 $libDirs = $this->libDirs;
589 $filter_name = $this->prefix . 'libdirs';
590 $libDirs = apply_filters( $filter_name, $libDirs );
591
592 foreach( $libDirs as $dir ){
593 $start_dirs[] = $dir;
594 $start_dirs[] = $dir . '/_interface';
595 $start_dirs[] = $dir . '/_wordpress';
596 }
597 }
598 else {
599 // $start_dirs = $this->dirs;
600 reset( $this->dirs );
601 if( count($path) > 1 ){
602 $module = array_shift( $path );
603
604 foreach( $this->dirs as $dir ){
605 $start_dirs[] = $dir . '/' . $module;
606 $start_dirs[] = $dir . '/_wordpress/' . $module;
607 // $start_dirs[] = $dir . '/' . $module . '/common';
608 // $start_dirs[] = $dir . '/' . $module . '/wordpress';
609 }
610 }
611 else {
612 foreach( $this->dirs as $dir ){
613 $start_dirs[] = $dir;
614 $start_dirs[] = $dir . '/_wordpress';
615 }
616 }
617 }
618
619 // _print_r( $start_dirs );
620 // exit;
621
622 // _print_r( $path );
623 $file = array_pop( $path );
624 if( $path ){
625 $file = join('/', $path) . '/' . $file;
626 }
627 $file .= '.php';
628
629 reset( $start_dirs );
630 foreach( $start_dirs as $start_dir ){
631 $this_file = $start_dir . '/' . $file;
632 // echo "FOR $inclass TRY $this_file<br>\n";
633 if( file_exists($this_file) ){
634 require $this_file;
635 return;
636 }
637 }
638 }
639
640 // intercepts if in the front page our slug is given then it's ours
641 public function intercept()
642 {
643 if( ! $this->isIntercepted() ){
644 return;
645 }
646
647 $this->actionResult = $this->handleRequest();
648 echo $this->render();
649 exit;
650 }
651
652 public function isIntercepted()
653 {
654 $ret = FALSE;
655
656 if( array_key_exists($this->hcs, $_GET) ){
657 $hcs = sanitize_text_field($_GET[$this->hcs]);
658
659 if( ($hcs == $this->slug) OR ($hcs == $this->prfx) ){
660 $ret = TRUE;
661 return $ret;
662 }
663 }
664
665 if( array_key_exists($this->hcj, $_GET) ){
666 $hcj = sanitize_text_field($_GET[$this->hcj]);
667
668 if( $hcj == $this->prfx ){
669 $ret = TRUE;
670 return $ret;
671 }
672 }
673
674 return $ret;
675 }
676
677 public function root()
678 {
679 return $this->dic;
680 }
681 }
682
683 if( ! function_exists('_print_r') ){
684 function _print_r( $thing )
685 {
686 $wpAdmin = defined('WPINC') && is_admin();
687
688 if( $wpAdmin ){
689 echo '<div style="margin-left: 15em;">';
690 }
691
692 echo '<pre>';
693 print_r( $thing );
694 echo '</pre>';
695
696 if( $wpAdmin ){
697 echo '</div>';
698 }
699 }
700 }