PluginProbe
ShiftController Employee Shift Scheduling / 4.9.78
ShiftController Employee Shift Scheduling v4.9.78
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.78, at hc3/_wordpress/abstract/plugin.php

742 lines 16.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', 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 if( 'get:myschedule/dates' == $slug ){
366 $slug = 'post:myschedule/dates';
367 }
368
369 // first acl
370 $acl = $root->make('HC3_Acl');
371 if( ! $acl->check($slug, $requestParams) ){
372 // if not logged in then it may need to log in
373 if( ! get_current_user_id() ){
374 $returnTo = $uri->currentUrl();
375 $to = wp_login_url( $returnTo );
376
377 if( ! headers_sent() ){
378 if( 0 && defined('HC3_PROFILER') && HC3_PROFILER ){
379 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
380 echo $html;
381 exit;
382 }
383 else {
384 wp_redirect( $to );
385 }
386 }
387 else {
388 if( defined('HC3_PROFILER') && HC3_PROFILER ){
389 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
390 echo $html;
391 exit;
392 }
393 else {
394 $html = "<META http-equiv=\"refresh\" content=\"0;URL=$to\">";
395 echo $html;
396 exit;
397 }
398 }
399 }
400 else {
401 $result = 'not allowed';
402 return $result;
403 }
404 }
405
406 list( $handler, $args ) = $router->getHandlerArgs( $slug );
407
408 if( 'post' == $requestMethod ){
409 $csrf->checkInput();
410 }
411
412 // echo "HANDLER = '$handler' FOR SLUG = '$slug'<br>";
413 try {
414 list( $handler, $method ) = is_array($handler) ? $handler : array( $handler, 'execute' );
415 // echo "HANDLER = $handler";
416 if( $handler ){
417 $handler = $root->make( $handler );
418 $result = call_user_func_array( array($handler, $method), $args );
419 }
420 else {
421 $result = "nothing to handle this request: '$slug'";
422 $result = esc_html( $result );
423 }
424 }
425 catch( HC3_ExceptionArray $e ){
426 $errors = $e->getErrors();
427 $session->setFlashdata('form_errors', $errors);
428 $result = array('-referrer-', NULL);
429 $post = $root->make('HC3_Post')
430 ->get()
431 ;
432 $session->setFlashdata('post', $post);
433 }
434
435 // message and redirect
436 if( is_array($result) ){
437 $isError = false;
438
439 if( count($result) > 2 ){
440 list( $to, $msg, $isError ) = $result;
441 }
442 else {
443 list( $to, $msg ) = $result;
444 }
445
446 $post = $root->make('HC3_Post')
447 ->get()
448 ;
449
450 if( $isError ){
451 $session->setFlashdata('error', $msg);
452 }
453 else {
454 $session->setFlashdata('message', $msg);
455 }
456
457 // $session->setFlashdata('post', $post);
458
459 $to = $uri->makeUrl( $to );
460
461 $notificator = $root->make('HC3_Notificator');
462 $notificator->send();
463
464 if( $ajax ){
465 $out = array('redirect' => $to);
466 $out = json_encode( $out );
467 echo $out;
468 }
469 else {
470 if( ! headers_sent() ){
471 if( defined('HC3_PROFILER') && HC3_PROFILER ){
472 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
473 // echo $html;
474 return $html;
475 // exit;
476 }
477 else {
478 wp_redirect( $to );
479 }
480 }
481 else {
482 if( 0 && defined('HC3_PROFILER') && HC3_PROFILER ){
483 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
484 echo $html;
485 exit;
486 }
487 else {
488 $html = "<META http-equiv=\"refresh\" content=\"0;URL=$to\">";
489 }
490 echo $html;
491 }
492 }
493 exit;
494 }
495
496 $enqueuer = $root->make('HC3_Enqueuer');
497 $enqueuer
498 ->addStyle('hc', 'hc3/assets/css/hc.css?hcver=' . HC3_VERSION)
499 ;
500 $profiler->mark('action_end');
501
502 if( $request->isPrintView() ){
503 $this->actionResult = $result;
504 echo $this->render();
505 exit;
506 }
507
508 $enqueuer
509 ->addScript('hc', 'hc3/assets/js/hc2.js?hcver=' . HC3_VERSION)
510 ;
511
512 return $result;
513 }
514
515 public function render()
516 {
517 $root = $this->root();
518
519 $request = $root->make('HC3_Request');
520 $isPrintView = $request->isPrintView();
521
522 $slug = $request->getSlug();
523
524 $ajax = false;
525 if( substr($slug, 0, strlen('ajax/')) == 'ajax/' ){
526 $ajax = true;
527 }
528 else {
529 $ajax = $request->isAjax() ? true : false;
530 }
531
532 $profiler = $root->make('HC3_Profiler');
533 $profiler->mark('render_start');
534
535 $result = $this->actionResult;
536
537 $profiler = $root->make('HC3_Profiler');
538 $csrf = $root->make('HC3_Csrf');
539
540 // view
541 // add announce
542 if( ! $ajax ){
543 $announce = $root->make('HC3_Ui_Announce');
544 $result = $announce->render( $result );
545
546 // add top menu
547 // $layout = $root->make('HC3_Ui_Layout');
548 // $result = $layout->render( $result );
549 }
550
551 // filter output
552 $filter = $root->make('HC3_Ui_Filter');
553 $result = $filter->filter( $result );
554
555 if( ! $ajax ){
556 $layout = $root->make('HC3_Layout');
557 if( $isPrintView ){
558 $result = $layout->renderPrint( $result );
559 }
560 else {
561 $result = $layout->render( $result );
562 }
563 }
564
565 $translate = $root->make('HC3_Translate');
566 $result = $translate->translate( $result );
567
568 $result = $csrf->prepareOutput( $result );
569
570 $profiler->mark('render_end');
571 $profiler->mark('total_end');
572
573 if( defined('HC3_PROFILER') && HC3_PROFILER && (! $isPrintView) && (! $ajax) ){
574 $result = $profiler->render( $result );
575 }
576
577 echo $result;
578 }
579
580 public function adminInit()
581 {
582 if( $this->isMeAdmin() ){
583 $this->actionResult = $this->handleRequest();
584 }
585 }
586
587 public function isMeAdmin()
588 {
589 $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
590 if( isset($_REQUEST['page']) ){
591 $page = sanitize_text_field($_REQUEST['page']);
592 }
593
594 if( $page && ($page == $this->slug) ){
595 $return = TRUE;
596 }
597 else {
598 $return = FALSE;
599 }
600
601 return $return;
602 }
603
604 public function autoload( $inclass )
605 {
606 $class = strtolower( $inclass );
607 // check & strip prefix
608 if( substr($class, 0, strlen('hc3_')) == 'hc3_' ){
609 $is_lib = TRUE;
610 $class = substr($class, strlen('hc3_'));
611 }
612 elseif( substr($class, 0, strlen($this->prefix)) == $this->prefix ){
613 $is_lib = FALSE;
614 $class = substr($class, strlen($this->prefix));
615 }
616 else {
617 return;
618 }
619
620 $class_array = explode('_', $class);
621 $path = $class_array;
622
623 $start_dirs = array();
624 if( $is_lib ){
625 $lib_dir = defined('HC3_DEV_INSTALL') ? HC3_DEV_INSTALL : dirname($this->file) . '/hc3';
626 $start_dirs[] = $lib_dir;
627 $start_dirs[] = $lib_dir . '/_interface';
628 $start_dirs[] = $lib_dir . '/_wordpress';
629
630 $libDirs = $this->libDirs;
631 $filter_name = $this->prefix . 'libdirs';
632 $libDirs = apply_filters( $filter_name, $libDirs );
633
634 foreach( $libDirs as $dir ){
635 $start_dirs[] = $dir;
636 $start_dirs[] = $dir . '/_interface';
637 $start_dirs[] = $dir . '/_wordpress';
638 }
639 }
640 else {
641 // $start_dirs = $this->dirs;
642 reset( $this->dirs );
643 if( count($path) > 1 ){
644 $module = array_shift( $path );
645
646 foreach( $this->dirs as $dir ){
647 $start_dirs[] = $dir . '/' . $module;
648 $start_dirs[] = $dir . '/_wordpress/' . $module;
649 // $start_dirs[] = $dir . '/' . $module . '/common';
650 // $start_dirs[] = $dir . '/' . $module . '/wordpress';
651 }
652 }
653 else {
654 foreach( $this->dirs as $dir ){
655 $start_dirs[] = $dir;
656 $start_dirs[] = $dir . '/_wordpress';
657 }
658 }
659 }
660
661 // _print_r( $start_dirs );
662 // exit;
663
664 // _print_r( $path );
665 $file = array_pop( $path );
666 if( $path ){
667 $file = join('/', $path) . '/' . $file;
668 }
669 $file .= '.php';
670
671 reset( $start_dirs );
672 foreach( $start_dirs as $start_dir ){
673 $this_file = $start_dir . '/' . $file;
674 // echo "FOR $inclass TRY $this_file<br>\n";
675 if( file_exists($this_file) ){
676 require $this_file;
677 return;
678 }
679 }
680 }
681
682 // intercepts if in the front page our slug is given then it's ours
683 public function intercept()
684 {
685 if( ! $this->isIntercepted() ){
686 return;
687 }
688
689 $this->actionResult = $this->handleRequest();
690 echo $this->render();
691 exit;
692 }
693
694 public function isIntercepted()
695 {
696 $ret = FALSE;
697
698 if( array_key_exists($this->hcs, $_GET) ){
699 $hcs = sanitize_text_field($_GET[$this->hcs]);
700
701 if( ($hcs == $this->slug) OR ($hcs == $this->prfx) ){
702 $ret = TRUE;
703 return $ret;
704 }
705 }
706
707 if( array_key_exists($this->hcj, $_GET) ){
708 $hcj = sanitize_text_field($_GET[$this->hcj]);
709
710 if( $hcj == $this->prfx ){
711 $ret = TRUE;
712 return $ret;
713 }
714 }
715
716 return $ret;
717 }
718
719 public function root()
720 {
721 return $this->dic;
722 }
723 }
724
725 if( ! function_exists('_print_r') ){
726 function _print_r( $thing )
727 {
728 $wpAdmin = defined('WPINC') && is_admin();
729
730 if( $wpAdmin ){
731 echo '<div style="margin-left: 15em;">';
732 }
733
734 echo '<pre>';
735 print_r( $thing );
736 echo '</pre>';
737
738 if( $wpAdmin ){
739 echo '</div>';
740 }
741 }
742 }