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