PluginProbe
ShiftController Employee Shift Scheduling / 4.9.66
ShiftController Employee Shift Scheduling v4.9.66
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.66, at hc3/_wordpress/abstract/plugin.php

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