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