-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWPTools.php
116 lines (93 loc) · 2.81 KB
/
WPTools.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* @author kofimokome
*/
require_once 'KMEnv.php';
require_once 'KMRouteManager.php';
require_once 'KMMigrationManager.php';
require_once 'KMBlueprint.php';
require_once 'KMBuilder.php';
require_once 'KMMenuPage.php';
require_once 'KMSubMenuPage.php';
require_once 'KMSetting.php';
require_once 'KMColumn.php';
require_once 'KMModel.php';
require_once 'KMMigration.php';
require_once 'KMRoute.php';
require_once 'KMValidator.php';
require_once 'lib/plural/Plural.php';
if ( ! class_exists('WPTools') ) {
class WPTools {
public $env;
public $route_manager;
public $migration_manager;
private $plugin_basename;
private $context;
private static $instances = [];
public function __construct( string $context ) {
$this->env = ( new KMEnv( $context ) )->getEnv();
$this->route_manager = new KMRouteManager( $context );
$this->plugin_basename = plugin_basename( $context );
$this->context = $context;
$this->migration_manager = new KMMigrationManager( $this->getPluginDir(), $context );
self::$instances[ explode( '/', $this->plugin_basename )[0] ] = $this;
}
/**
* @author kofimokome
*/
public static function getInstance( string $context ): WPTools {
$plugin_basename = plugin_basename( $context );
$plugin = explode( '/', $plugin_basename )[0];
if ( ! isset( self::$instances[ $plugin ] ) ) {
throw new Exception( 'WPTools instance not found' );
}
return self::$instances[ $plugin ];
}
/**
* @author kofimokome
*/
public function routes(): KMRoute {
$route = new KMRoute( $this->route_manager );
return $route;
}
/**
* @author kofimokome
*/
public function renderView( string $view, $echo = true ) {
return $this->route_manager->renderView( $view, $echo );
}
/**
* @author kofimokome
*/
public function viewPath( string $view ) {
return $this->route_manager->viewPath( $view );
}
/**
* @author kofimokome
*/
public function route( string $name, array $params = [] ): string {
return $this->route_manager->route( $name, $params );
}
/**
* @throws Exception
* @author kofimokome
*/
public function getPluginDir() {
$chars = explode( '/', $this->plugin_basename );
if ( sizeof( $chars ) > 0 ) {
$plugin_basename = $chars[0];
return WP_PLUGIN_DIR . '/' . $plugin_basename;
}
throw new Exception( 'Could not get plugin directory' );
}
/**
* @author kofimokome
*/
public function getPluginURL(): string {
return rtrim( plugin_dir_url( $this->context ), '/' );
}
public function env() {
return ( new KMEnv( $this->context ) )->getEnv();
}
}
}