| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Module Cost Tracker Admin class. |
| 4 |
* |
| 5 |
* @package MainWP\Dashboard |
| 6 |
* @version 4.6 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace MainWP\Dashboard\Module\CostTracker; |
| 10 |
|
| 11 |
use MainWP\Dashboard\MainWP_Utility; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Cost_Tracker_Manager |
| 20 |
*/ |
| 21 |
class Cost_Tracker_Manager { |
| 22 |
|
| 23 |
/** |
| 24 |
* Static variable to hold the single instance of the class. |
| 25 |
* |
| 26 |
* @static |
| 27 |
* |
| 28 |
* @var mixed Default null |
| 29 |
*/ |
| 30 |
public static $instance = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Get Instance |
| 34 |
* |
| 35 |
* Creates public static instance. |
| 36 |
* |
| 37 |
* @static |
| 38 |
* |
| 39 |
* @return Cost_Tracker_Manager |
| 40 |
*/ |
| 41 |
public static function get_instance() { |
| 42 |
if ( null === static::$instance ) { |
| 43 |
static::$instance = new self(); |
| 44 |
} |
| 45 |
return static::$instance; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor |
| 50 |
* |
| 51 |
* Runs each time the class is called. |
| 52 |
*/ |
| 53 |
public function __construct() { |
| 54 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 55 |
Cost_Tracker_Admin::get_instance(); |
| 56 |
$base_dir = static::get_location_path(); |
| 57 |
// includes rest api work. |
| 58 |
require_once $base_dir . 'rest-api/version1/class-cost-tracker-rest-api-v1.php'; // NOSONAR - WP compatible. |
| 59 |
require_once $base_dir . 'rest-api/version1/class-cost-tracker-rest-api-handle-v1.php'; // NOSONAR - WP compatible. |
| 60 |
|
| 61 |
require_once $base_dir . 'rest-api/version2/class-mainwp-rest-costs-controller.php'; // NOSONAR - WP compatible. |
| 62 |
|
| 63 |
Cost_Tracker_Rest_Api_V1::instance()->init(); |
| 64 |
add_filter( 'mainwp_rest_api_get_rest_namespaces', array( $this, 'hook_rest_api_get_rest_namespaces' ), 10, 1 ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Method hook_rest_api_get_rest_namespaces(). |
| 69 |
* |
| 70 |
* @param string $namespaces namespaces. |
| 71 |
* |
| 72 |
* @return string namespaces. |
| 73 |
*/ |
| 74 |
public function hook_rest_api_get_rest_namespaces( $namespaces ) { |
| 75 |
$namespaces['mainwp/v2/costs'] = array( 'costs' => \MainWP_Rest_Costs_Controller::class ); |
| 76 |
return $namespaces; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Autoloader for classes. |
| 81 |
* |
| 82 |
* @param string $class_name class name. |
| 83 |
*/ |
| 84 |
public function autoload( $class_name ) { |
| 85 |
|
| 86 |
if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class_name, $matches ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
static $reflection; |
| 91 |
|
| 92 |
if ( empty( $reflection ) ) { |
| 93 |
$reflection = new \ReflectionObject( $this ); |
| 94 |
} |
| 95 |
|
| 96 |
if ( $reflection->getNamespaceName() !== $matches['namespace'] ) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
$autoload_name = $matches['autoload']; |
| 101 |
$autoload_dir = static::get_location_path(); |
| 102 |
|
| 103 |
$load_dirs = array( |
| 104 |
'classes' => 'class', |
| 105 |
'pages' => 'page', |
| 106 |
'widgets' => 'widget', |
| 107 |
); |
| 108 |
foreach ( $load_dirs as $dir => $prefix ) { |
| 109 |
$dir = $dir . DIRECTORY_SEPARATOR; |
| 110 |
$autoload_path = sprintf( '%s%s%s-%s.php', $autoload_dir, $dir, $prefix, strtolower( str_replace( '_', '-', $autoload_name ) ) ); |
| 111 |
if ( is_readable( $autoload_path ) ) { |
| 112 |
require_once $autoload_path; // NOSONAR - WP compatible. |
| 113 |
return; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Method get_location(). |
| 120 |
* |
| 121 |
* @param string $path what to get path/url. |
| 122 |
* |
| 123 |
* @return string value |
| 124 |
*/ |
| 125 |
public static function get_location_path( $path = 'dir' ) { |
| 126 |
$location = array( |
| 127 |
'dir' => MAINWP_MODULES_DIR . 'cost-tracker' . DIRECTORY_SEPARATOR, |
| 128 |
'url' => MAINWP_MODULES_URL . 'cost-tracker/', |
| 129 |
); |
| 130 |
return isset( $location[ $path ] ) ? $location[ $path ] : ''; |
| 131 |
} |
| 132 |
} |
| 133 |
|