PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / trunk
MainWP Child Reports vtrunk
0.0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9.1 1.9.2 1.9.3 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1 2.1.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.3 2.3.1 trunk
mainwp-child-reports / classes / class-plugin.php
mainwp-child-reports / classes Last commit date
class-admin.php 3 months ago class-author.php 3 months ago class-cli.php 3 months ago class-connector.php 3 months ago class-connectors.php 3 months ago class-date-interval.php 3 months ago class-db-driver-wpdb.php 2 months ago class-db-driver.php 3 months ago class-db.php 3 months ago class-export.php 3 months ago class-exporter.php 3 months ago class-filter-input.php 3 months ago class-form-generator.php 3 months ago class-install.php 3 months ago class-list-table.php 3 months ago class-live-update.php 3 months ago class-log.php 2 months ago class-mainwp-child-report-helper.php 3 months ago class-network.php 3 months ago class-plugin.php 3 months ago class-preview-list-table.php 3 months ago class-query.php 3 months ago class-record.php 3 months ago class-settings.php 3 months ago class-uninstall.php 3 months ago
class-plugin.php
227 lines
1 <?php
2 /** MainWP Child Reports plugin. */
3
4 namespace WP_MainWP_Stream;
5
6 // Exit if accessed directly.
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 /**
12 * Class Plugin.
13 *
14 * @package WP_MainWP_Stream
15 */
16 class Plugin {
17
18 /** @const string Plugin version number. */
19 const VERSION = '3.5.5';
20
21 /** @const string WP-CLI command. */
22 const WP_CLI_COMMAND = 'mainwp_stream';
23
24 /** @var \WP_MainWP_Stream\Admin Admin class. */
25 public $admin;
26
27 /** @var \WP_MainWP_Stream\Connectors Connectors class. */
28 public $connectors;
29
30 /** @var \WP_MainWP_Stream\DB DB Class. */
31 public $db;
32
33 /** @var \WP_MainWP_Stream\Log Log Class. */
34 public $log;
35
36 /** @var \WP_MainWP_Stream\Settings Settings class. */
37 public $settings;
38
39 /** @var \WP_MainWP_Stream\Install Install class. */
40 public $install;
41
42 /** @var array URLs and Paths used by the plugin. */
43 public $locations = array();
44
45
46 /** @var \WP_MainWP_Stream\Child_Helper Child_Helper class. */
47 public $child_helper;
48
49 /**
50 * Plugin constructor.
51 *
52 * Run each time the class is called.
53 *
54 * @throws \Exception
55 *
56 * @uses \WP_MainWP_Stream\Admin
57 * @uses \WP_MainWP_Stream\CLI
58 * @uses \WP_MainWP_Stream\DB
59 * @uses \WP_MainWP_Stream\DB_Driver
60 * @uses \WP_MainWP_Stream\DB_Driver_WPDB
61 * @uses \WP_MainWP_Stream\Log
62 * @uses \WP_MainWP_Stream\MainWP_Child_Report_Helper
63 */
64 public function __construct() {
65 $locate = $this->locate_plugin();
66
67 $this->locations = array(
68 'plugin' => $locate['plugin_basename'],
69 'dir' => $locate['dir_path'],
70 'url' => $locate['dir_url'],
71 'inc_dir' => $locate['dir_path'] . 'includes/',
72 'class_dir' => $locate['dir_path'] . 'classes/',
73 );
74
75 spl_autoload_register( array( $this, 'autoload' ) );
76
77 // Load helper functions.
78 require_once $this->locations['inc_dir'] . 'functions.php';
79
80 // Load DB helper interface/class.
81 $driver_class = apply_filters( 'wp_mainwp_stream_db_driver', '\WP_MainWP_Stream\DB_Driver_WPDB' );
82 $driver = null;
83
84 if ( class_exists( $driver_class ) ) {
85 $driver = new $driver_class();
86 $this->db = new DB( $driver );
87 }
88
89 $error = false;
90 if ( ! $this->db ) {
91 $error = esc_html__( 'Stream: Could not load chosen DB driver.', 'mainwp-child-reports' );
92 } elseif ( ! $driver instanceof DB_Driver ) {
93 $error = esc_html__( 'Stream: DB driver must implement DB Driver interface.', 'mainwp-child-reports' );
94 }
95
96 if ( $error ) {
97 wp_die(
98 esc_html( $error ),
99 esc_html__( 'Reports DB Error', 'mainwp-child-reports' )
100 );
101 }
102
103 // Load languages.
104 add_action( 'plugins_loaded', array( $this, 'i18n' ) );
105
106 // Load logger class
107 $this->log = apply_filters( 'wp_mainwp_stream_log_handler', new Log( $this ) );
108
109 // Load settings and connectors after widgets_init and before the default init priority.
110 add_action( 'init', array( $this, 'init' ), 9 );
111
112 // Change DB driver after plugin loaded if any add-ons want to replace.
113 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 );
114
115 // Load admin area classes.
116 if ( is_admin() || ( defined( 'WP_MAINWP_STREAM_DEV_DEBUG' ) && WP_MAINWP_STREAM_DEV_DEBUG ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
117 $this->admin = new Admin( $this );
118 $this->install = $driver->setup_storage( $this );
119 } elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
120 $this->admin = new Admin( $this, $driver );
121 }
122 $this->child_helper = new MainWP_Child_Report_Helper( $this );
123
124 // Load WP-CLI command.
125 if ( defined( 'WP_CLI' ) && WP_CLI ) {
126 \WP_CLI::add_command( self::WP_CLI_COMMAND, '\WP_MainWP_Stream\CLI' );
127 }
128
129 add_action( 'mainwp_child_reports_add_log', array( $this->log, 'hook_log' ), 10, 7 );
130 }
131
132 /**
133 * Autoloader for classes.
134 *
135 * @param string $class
136 */
137 public function autoload( $class ) {
138 if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class, $matches ) ) {
139 return;
140 }
141
142 static $reflection;
143
144 if ( empty( $reflection ) ) {
145 $reflection = new \ReflectionObject( $this );
146 }
147
148 if ( $reflection->getNamespaceName() !== $matches['namespace'] ) {
149 return;
150 }
151
152 $autoload_name = $matches['autoload'];
153 $autoload_dir = \trailingslashit( $this->locations['class_dir'] );
154 $autoload_path = sprintf( '%sclass-%s.php', $autoload_dir, strtolower( str_replace( '_', '-', $autoload_name ) ) );
155
156 if ( is_readable( $autoload_path ) ) {
157 require_once $autoload_path;
158 }
159 }
160
161 /**
162 * Loads the translation files.
163 *
164 * @action plugins_loaded
165 */
166 public function i18n() {
167 load_plugin_textdomain( 'mainwp-child-reports', false, dirname( $this->locations['plugin'] ) . '/languages/' );
168 }
169
170 /**
171 * Load Settings, Notifications, and Connectors.
172 *
173 * @action init
174 *
175 * @uses \WP_MainWP_Stream\Connectors
176 * @uses \WP_MainWP_Stream\Settings
177 */
178 public function init() {
179 $this->settings = new Settings( $this );
180 $this->connectors = new Connectors( $this );
181 }
182
183 /**
184 * Version of plugin_dir_url() which works for plugins installed in the plugins directory,
185 * and for plugins bundled with themes.
186 *
187 * @throws \Exception
188 *
189 * @return array
190 */
191 private function locate_plugin() {
192 $dir_url = trailingslashit( plugins_url( '', __DIR__ ) );
193 $dir_path = plugin_dir_path( __DIR__ );
194 $dir_basename = basename( $dir_path );
195 $plugin_basename = trailingslashit( $dir_basename ) . $dir_basename . '.php';
196
197 return compact( 'dir_url', 'dir_path', 'dir_basename', 'plugin_basename' );
198 }
199
200 /**
201 * Getter for the version number.
202 *
203 * @return string
204 */
205 public function get_version() {
206 return self::VERSION;
207 }
208
209
210
211 /**
212 * Change plugin database driver in case driver plugin loaded after stream.
213 *
214 * @uses \WP_MainWP_Stream\DB
215 * @uses \WP_MainWP_Stream\DB_Driver_WPDB
216 */
217 public function plugins_loaded() {
218 // Load DB helper interface/class
219 $driver_class = apply_filters( 'wp_mainwp_stream_db_driver', '\WP_MainWP_Stream\DB_Driver_WPDB' );
220
221 if ( class_exists( $driver_class ) ) {
222 $driver = new $driver_class();
223 $this->db = new DB( $driver );
224 }
225 }
226 }
227