PluginProbe
ShiftController Employee Shift Scheduling / 4.9.96
ShiftController Employee Shift Scheduling v4.9.96
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.96, at hc3/_wordpress/abstract/plugin.php

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