| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Database Site Actions |
| 4 |
* |
| 5 |
* This file handles all interactions with the Site Actions DB. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard\Module\Log; |
| 11 |
|
| 12 |
use MainWP\Dashboard\MainWP_Execution_Helper; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Log_Manager |
| 16 |
* |
| 17 |
* @package MainWP\Dashboard |
| 18 |
*/ |
| 19 |
class Log_Manager { |
| 20 |
|
| 21 |
/** |
| 22 |
* Version |
| 23 |
* |
| 24 |
* @const string Plugin version number. |
| 25 |
* */ |
| 26 |
const VERSION = '5.0.0'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Log_Admin |
| 30 |
* |
| 31 |
* @var \MainWP\Dashboard\Module\Log\Log_Admin Admin class. |
| 32 |
* */ |
| 33 |
public $admin; |
| 34 |
|
| 35 |
/** |
| 36 |
* MainWP_Execution_Helper |
| 37 |
* |
| 38 |
* @var \MainWP\Dashboard\MainWP_Execution_Helper class. |
| 39 |
* */ |
| 40 |
public $executor; |
| 41 |
|
| 42 |
/** |
| 43 |
* Holds Instance of settings object |
| 44 |
* |
| 45 |
* @var Log_Settings |
| 46 |
*/ |
| 47 |
public $settings; |
| 48 |
|
| 49 |
/** |
| 50 |
* Log_Connectors |
| 51 |
* |
| 52 |
* @var \MainWP\Dashboard\Module\Log\Log_Connectors Connectors class. |
| 53 |
* */ |
| 54 |
public $connectors; |
| 55 |
|
| 56 |
/** |
| 57 |
* Log_DB |
| 58 |
* |
| 59 |
* @var \MainWP\Dashboard\Module\Log\Log_DB DB Class. |
| 60 |
* */ |
| 61 |
public $db; |
| 62 |
|
| 63 |
/** |
| 64 |
* Log |
| 65 |
* |
| 66 |
* @var \MainWP\Dashboard\Module\Log\Log Log Class. |
| 67 |
* */ |
| 68 |
public $log; |
| 69 |
|
| 70 |
/** |
| 71 |
* Log_Install class. |
| 72 |
* |
| 73 |
* @var \MainWP\Dashboard\Module\Log\Log_Install Install class. |
| 74 |
* */ |
| 75 |
public $install; |
| 76 |
|
| 77 |
/** |
| 78 |
* Locations. |
| 79 |
* |
| 80 |
* @var array URLs and Paths used by the plugin. |
| 81 |
*/ |
| 82 |
public $locations = array(); |
| 83 |
|
| 84 |
/** |
| 85 |
* Protected static variable to hold the single instance of the class. |
| 86 |
* |
| 87 |
* @var mixed Default null |
| 88 |
*/ |
| 89 |
protected static $instance = null; |
| 90 |
|
| 91 |
/** |
| 92 |
* Return the single instance of the class. |
| 93 |
* |
| 94 |
* @return mixed $instance The single instance of the class. |
| 95 |
*/ |
| 96 |
public static function instance() { |
| 97 |
if ( is_null( self::$instance ) ) { |
| 98 |
self::$instance = new self(); |
| 99 |
} |
| 100 |
return self::$instance; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Plugin constructor. |
| 105 |
* |
| 106 |
* Run each time the class is called. |
| 107 |
*/ |
| 108 |
public function __construct() { |
| 109 |
|
| 110 |
$mod_log_dir = MAINWP_MODULES_DIR . 'logs/'; |
| 111 |
$this->locations = array( |
| 112 |
'dir' => $mod_log_dir, |
| 113 |
'url' => MAINWP_MODULES_URL . 'logs/', |
| 114 |
'inc_dir' => $mod_log_dir . 'includes/', |
| 115 |
'class_dir' => $mod_log_dir . 'classes/', |
| 116 |
); |
| 117 |
|
| 118 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 119 |
|
| 120 |
// Load helper functions. |
| 121 |
require_once $this->locations['inc_dir'] . 'functions.php'; |
| 122 |
|
| 123 |
$driver = new Log_DB_Driver_WPDB(); |
| 124 |
$this->db = new Log_DB( $driver ); |
| 125 |
$this->executor = MainWP_Execution_Helper::instance(); |
| 126 |
$this->settings = new Log_Settings( $this ); |
| 127 |
|
| 128 |
// Load logger class. |
| 129 |
$this->log = new Log( $this ); |
| 130 |
|
| 131 |
// Load settings and connectors after widgets_init and before the default init priority. |
| 132 |
add_action( 'init', array( $this, 'init' ), 9 ); |
| 133 |
|
| 134 |
// Change DB driver after plugin loaded if any add-ons want to replace. |
| 135 |
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 ); |
| 136 |
|
| 137 |
// Load admin area classes. |
| 138 |
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 139 |
$this->admin = new Log_Admin( $this ); |
| 140 |
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 141 |
$this->admin = new Log_Admin( $this, $driver ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Autoloader for classes. |
| 147 |
* |
| 148 |
* @param string $class_name class name. |
| 149 |
*/ |
| 150 |
public function autoload( $class_name ) { |
| 151 |
|
| 152 |
if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class_name, $matches ) ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
static $reflection; |
| 157 |
|
| 158 |
if ( empty( $reflection ) ) { |
| 159 |
$reflection = new \ReflectionObject( $this ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( $reflection->getNamespaceName() !== $matches['namespace'] ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$autoload_name = $matches['autoload']; |
| 167 |
$autoload_dir = \trailingslashit( $this->locations['dir'] ); |
| 168 |
$load_dirs = array( |
| 169 |
'classes' => 'class', |
| 170 |
'pages' => 'page', |
| 171 |
'widgets' => 'widget', |
| 172 |
); |
| 173 |
foreach ( $load_dirs as $dir => $prefix ) { |
| 174 |
$dir = $dir . '/'; |
| 175 |
$autoload_path = sprintf( '%s%s%s-%s.php', $autoload_dir, $dir, $prefix, strtolower( str_replace( '_', '-', $autoload_name ) ) ); |
| 176 |
if ( is_readable( $autoload_path ) ) { |
| 177 |
require_once $autoload_path; |
| 178 |
return; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Get internal connectors. |
| 186 |
*/ |
| 187 |
public function get_internal_connectors() { |
| 188 |
return array( |
| 189 |
'compact', |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Load Log_Connectors. |
| 195 |
* |
| 196 |
* @action init |
| 197 |
* |
| 198 |
* @uses \MainWP\Dashboard\Module\Log\Log_Connectors |
| 199 |
*/ |
| 200 |
public function init() { |
| 201 |
$this->connectors = new Log_Connectors( $this ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Getter for the version number. |
| 206 |
* |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
public function get_version() { |
| 210 |
return self::VERSION; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Change plugin database driver in case driver plugin loaded after logs. |
| 215 |
* |
| 216 |
* @uses \MainWP\Dashboard\Module\Log\Log_DB |
| 217 |
* @uses \MainWP\Dashboard\Module\Log\Log_DB_Driver_WPDB |
| 218 |
*/ |
| 219 |
public function plugins_loaded() { |
| 220 |
// Load DB helper interface/class. |
| 221 |
$driver_class = '\MainWP\Dashboard\Module\Log\Log_DB_Driver_WPDB'; |
| 222 |
|
| 223 |
if ( class_exists( $driver_class ) ) { |
| 224 |
$driver = new $driver_class(); |
| 225 |
$this->db = new Log_DB( $driver ); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|