class-media-library-organizer-tree-view.php
6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* Media Library Organizer Tree View class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Media Library Organizer Tree View Addon
*
* @since 1.2.7
*/
class Media_Library_Organizer_Tree_View {
/**
* Holds the class object.
*
* @since 1.1.1
*
* @var object
*/
public static $instance;
/**
* Holds the plugin information object.
*
* @since 1.1.1
*
* @var object
*/
public $plugin = '';
/**
* Flag denoting whether this Addon is enabled
*
* @since 1.1.1
*
* @var bool
*/
public $enabled = false;
/**
* Constructor. Acts as a bootstrap to load the rest of the plugin
*
* @since 1.1.1
*/
public function __construct() {
// Plugin Details.
$this->plugin = new stdClass();
$this->plugin->name = 'media-library-organizer-tree-view';
$this->plugin->displayName = __( 'Tree View', 'media-library-organizer' );
$this->plugin->folder = plugin_dir_path( __FILE__ );
$this->plugin->url = plugin_dir_url( __FILE__ );
$this->plugin->documentation_url = 'https://wpmedialibrary.com/documentation/tree-view';
// Defer loading of Plugin Classes.
add_action( 'init', array( $this, 'initialize' ), 2 );
}
/**
* Initializes required and licensed classes
*
* @since 1.1.1
*/
public function initialize() {
$this->classes = new stdClass();
$this->initialize_admin();
$this->initialize_frontend();
$this->initialize_admin_or_frontend_editor();
$this->initialize_cli();
$this->initialize_global();
}
/**
* Initialize classes for the WordPress Administration interface
*
* @since 1.1.1
*/
private function initialize_admin() {
// Bail if this request isn't for the WordPress Administration interface.
if ( ! is_admin() ) {
return;
}
$this->classes->admin = new Media_Library_Organizer_Tree_View_Admin( self::$instance );
}
/**
* Initialize classes for the frontend web site
*
* @since 1.1.1
*/
private function initialize_frontend() {
// Bail if this request isn't for the frontend web site.
if ( is_admin() ) {
return;
}
}
/**
* Initialize classes for WP-CLI
*
* @since 1.1.1
*/
private function initialize_cli() {
// Bail if WP-CLI isn't installed on the server.
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
// In CLI mode, is_admin() is not called, so we need to require the classes that
// the CLI commands may use.
}
/**
* Initialize classes for the WordPress Administration interface or a frontend Page Builder
*
* @since 1.1.1
*/
private function initialize_admin_or_frontend_editor() {
// Bail if this request isn't for the WordPress Administration interface and isn't for a frontend Page Builder.
if ( ! Media_Library_Organizer()->is_admin_or_frontend_editor() ) {
return;
}
$this->classes->ajax = new Media_Library_Organizer_Tree_View_AJAX( self::$instance );
$this->classes->media = new Media_Library_Organizer_Tree_View_Media( self::$instance );
$this->classes->settings = new Media_Library_Organizer_Tree_View_Settings( self::$instance );
}
/**
* Initialize classes used everywhere
*
* @since 1.1.1
*/
private function initialize_global() {
}
/**
* Returns the given class
*
* @since 1.1.1
*
* @param string $name Class Name.
*/
public function get_class( $name ) {
// If the class hasn't been loaded, throw a WordPress die screen
// to avoid a PHP fatal error.
if ( ! isset( $this->classes->{ $name } ) ) {
// Define the error.
$error = new WP_Error(
'media_library_organizer_tree_view_get_class',
sprintf(
/* translators: PHP class name */
__( 'Media Library Organizer: Tree View: Error: Could not load Plugin class <strong>%s</strong>', 'media-library-organizer' ),
$name
)
);
// Depending on the request, return or display an error.
// Admin UI.
if ( is_admin() ) {
wp_die(
esc_html( $error->get_error_message() ),
esc_html__( 'Media Library Organizer: Tree View: Error', 'media-library-organizer' ),
array(
'back_link' => true,
)
);
}
// Cron / CLI.
return $error;
}
// Return the class object.
return $this->classes->{ $name };
}
/**
* Returns the singleton instance of the class.
*
* @since 1.1.1
*
* @return object Class.
*/
public static function get_instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
/**
* Define the autoloader for this Plugin
*
* @since 1.1.1
*
* @param string $class_name The class to load.
*/
function media_library_organizer_tree_view_autoloader( $class_name ) {
// Define the required start of the class name.
$class_start_name = 'Media_Library_Organizer_Tree_View';
// Get the number of parts the class start name has.
$class_parts_count = count( explode( '_', $class_start_name ) );
// Break the class name into an array.
$class_path = explode( '_', $class_name );
// Bail if it's not a minimum length (i.e. doesn't potentially have Media_Library_Organizer_Tree_View).
if ( count( $class_path ) < $class_parts_count ) {
return;
}
// Build the base class path for this class.
$base_class_path = '';
for ( $i = 0; $i < $class_parts_count; $i++ ) {
$base_class_path .= $class_path[ $i ] . '_';
}
$base_class_path = trim( $base_class_path, '_' );
// Bail if the first parts don't match what we expect.
if ( $base_class_path !== $class_start_name ) {
return;
}
// Define the file name.
$file_name = 'class-' . str_replace( '_', '-', strtolower( $class_name ) ) . '.php';
// Define the paths with file name we need to include.
$include_paths = array(
dirname( __FILE__ ) . '/includes/admin/' . $file_name,
dirname( __FILE__ ) . '/includes/global/' . $file_name,
);
// Iterate through the include paths to find the file.
foreach ( $include_paths as $path_file ) {
if ( file_exists( $path_file ) ) {
require_once $path_file;
return;
}
}
}
spl_autoload_register( 'media_library_organizer_tree_view_autoloader' );
/**
* Main function to return Plugin instance.
*
* @since 1.1.1
*/
function Media_Library_Organizer_Tree_View() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName
return Media_Library_Organizer_Tree_View::get_instance();
}
// Finally, initialize the Plugin.
$media_library_organizer_tree_view = Media_Library_Organizer_Tree_View();