PluginProbe
ShiftController Employee Shift Scheduling / 4.9.71
ShiftController Employee Shift Scheduling v4.9.71
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.71, at hc3/_wordpress/abstract/plugin.php

739 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( defined('HC3_PROFILER') && HC3_PROFILER ){
469 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
470 // echo $html;
471 return $html;
472 // exit;
473 }
474 else {
475 wp_redirect( $to );
476 }
477 }
478 else {
479 if( 0 && defined('HC3_PROFILER') && HC3_PROFILER ){
480 $html = "REDIRECT<br><a href=\"$to\">$to</a>";
481 echo $html;
482 exit;
483 }
484 else {
485 $html = "<META http-equiv=\"refresh\" content=\"0;URL=$to\">";
486 }
487 echo $html;
488 }
489 }
490 exit;
491 }
492
493 $enqueuer = $root->make('HC3_Enqueuer');
494 $enqueuer
495 ->addStyle('hc', 'hc3/assets/css/hc.css?hcver=' . HC3_VERSION)
496 ;
497 $profiler->mark('action_end');
498
499 if( $request->isPrintView() ){
500 $this->actionResult = $result;
501 echo $this->render();
502 exit;
503 }
504
505 $enqueuer
506 ->addScript('hc', 'hc3/assets/js/hc2.js?hcver=' . HC3_VERSION)
507 ;
508
509 return $result;
510 }
511
512 public function render()
513 {
514 $root = $this->root();
515
516 $request = $root->make('HC3_Request');
517 $isPrintView = $request->isPrintView();
518
519 $slug = $request->getSlug();
520
521 $ajax = false;
522 if( substr($slug, 0, strlen('ajax/')) == 'ajax/' ){
523 $ajax = true;
524 }
525 else {
526 $ajax = $request->isAjax() ? true : false;
527 }
528
529 $profiler = $root->make('HC3_Profiler');
530 $profiler->mark('render_start');
531
532 $result = $this->actionResult;
533
534 $profiler = $root->make('HC3_Profiler');
535 $csrf = $root->make('HC3_Csrf');
536
537 // view
538 // add announce
539 if( ! $ajax ){
540 $announce = $root->make('HC3_Ui_Announce');
541 $result = $announce->render( $result );
542
543 // add top menu
544 // $layout = $root->make('HC3_Ui_Layout');
545 // $result = $layout->render( $result );
546 }
547
548 // filter output
549 $filter = $root->make('HC3_Ui_Filter');
550 $result = $filter->filter( $result );
551
552 if( ! $ajax ){
553 $layout = $root->make('HC3_Layout');
554 if( $isPrintView ){
555 $result = $layout->renderPrint( $result );
556 }
557 else {
558 $result = $layout->render( $result );
559 }
560 }
561
562 $translate = $root->make('HC3_Translate');
563 $result = $translate->translate( $result );
564
565 $result = $csrf->prepareOutput( $result );
566
567 $profiler->mark('render_end');
568 $profiler->mark('total_end');
569
570 if( defined('HC3_PROFILER') && HC3_PROFILER && (! $isPrintView) && (! $ajax) ){
571 $result = $profiler->render( $result );
572 }
573
574 echo $result;
575 }
576
577 public function adminInit()
578 {
579 if( $this->isMeAdmin() ){
580 $this->actionResult = $this->handleRequest();
581 }
582 }
583
584 public function isMeAdmin()
585 {
586 $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
587 if( isset($_REQUEST['page']) ){
588 $page = sanitize_text_field($_REQUEST['page']);
589 }
590
591 if( $page && ($page == $this->slug) ){
592 $return = TRUE;
593 }
594 else {
595 $return = FALSE;
596 }
597
598 return $return;
599 }
600
601 public function autoload( $inclass )
602 {
603 $class = strtolower( $inclass );
604 // check & strip prefix
605 if( substr($class, 0, strlen('hc3_')) == 'hc3_' ){
606 $is_lib = TRUE;
607 $class = substr($class, strlen('hc3_'));
608 }
609 elseif( substr($class, 0, strlen($this->prefix)) == $this->prefix ){
610 $is_lib = FALSE;
611 $class = substr($class, strlen($this->prefix));
612 }
613 else {
614 return;
615 }
616
617 $class_array = explode('_', $class);
618 $path = $class_array;
619
620 $start_dirs = array();
621 if( $is_lib ){
622 $lib_dir = defined('HC3_DEV_INSTALL') ? HC3_DEV_INSTALL : dirname($this->file) . '/hc3';
623 $start_dirs[] = $lib_dir;
624 $start_dirs[] = $lib_dir . '/_interface';
625 $start_dirs[] = $lib_dir . '/_wordpress';
626
627 $libDirs = $this->libDirs;
628 $filter_name = $this->prefix . 'libdirs';
629 $libDirs = apply_filters( $filter_name, $libDirs );
630
631 foreach( $libDirs as $dir ){
632 $start_dirs[] = $dir;
633 $start_dirs[] = $dir . '/_interface';
634 $start_dirs[] = $dir . '/_wordpress';
635 }
636 }
637 else {
638 // $start_dirs = $this->dirs;
639 reset( $this->dirs );
640 if( count($path) > 1 ){
641 $module = array_shift( $path );
642
643 foreach( $this->dirs as $dir ){
644 $start_dirs[] = $dir . '/' . $module;
645 $start_dirs[] = $dir . '/_wordpress/' . $module;
646 // $start_dirs[] = $dir . '/' . $module . '/common';
647 // $start_dirs[] = $dir . '/' . $module . '/wordpress';
648 }
649 }
650 else {
651 foreach( $this->dirs as $dir ){
652 $start_dirs[] = $dir;
653 $start_dirs[] = $dir . '/_wordpress';
654 }
655 }
656 }
657
658 // _print_r( $start_dirs );
659 // exit;
660
661 // _print_r( $path );
662 $file = array_pop( $path );
663 if( $path ){
664 $file = join('/', $path) . '/' . $file;
665 }
666 $file .= '.php';
667
668 reset( $start_dirs );
669 foreach( $start_dirs as $start_dir ){
670 $this_file = $start_dir . '/' . $file;
671 // echo "FOR $inclass TRY $this_file<br>\n";
672 if( file_exists($this_file) ){
673 require $this_file;
674 return;
675 }
676 }
677 }
678
679 // intercepts if in the front page our slug is given then it's ours
680 public function intercept()
681 {
682 if( ! $this->isIntercepted() ){
683 return;
684 }
685
686 $this->actionResult = $this->handleRequest();
687 echo $this->render();
688 exit;
689 }
690
691 public function isIntercepted()
692 {
693 $ret = FALSE;
694
695 if( array_key_exists($this->hcs, $_GET) ){
696 $hcs = sanitize_text_field($_GET[$this->hcs]);
697
698 if( ($hcs == $this->slug) OR ($hcs == $this->prfx) ){
699 $ret = TRUE;
700 return $ret;
701 }
702 }
703
704 if( array_key_exists($this->hcj, $_GET) ){
705 $hcj = sanitize_text_field($_GET[$this->hcj]);
706
707 if( $hcj == $this->prfx ){
708 $ret = TRUE;
709 return $ret;
710 }
711 }
712
713 return $ret;
714 }
715
716 public function root()
717 {
718 return $this->dic;
719 }
720 }
721
722 if( ! function_exists('_print_r') ){
723 function _print_r( $thing )
724 {
725 $wpAdmin = defined('WPINC') && is_admin();
726
727 if( $wpAdmin ){
728 echo '<div style="margin-left: 15em;">';
729 }
730
731 echo '<pre>';
732 print_r( $thing );
733 echo '</pre>';
734
735 if( $wpAdmin ){
736 echo '</div>';
737 }
738 }
739 }