class-media-library-organizer.php 12 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
<?php
/**
 * Media Library Organizer class.
 *
 * @package Media_Library_Organizer
 * @author WP Media Library
 */

/**
 * Main Media Library Organizer class, used to load the Plugin.
 *
 * @package   Media_Library_Organizer
 * @author    WP Media Library
 * @version   1.0.0
 */
class Media_Library_Organizer {

	/**
	 * Holds the class object.
	 *
	 * @since   1.0.0
	 *
	 * @var     object
	 */
	public static $instance;

	/**
	 * Plugin
	 *
	 * @since   1.0.0
	 *
	 * @var     object
	 */
	public $plugin = '';

	/**
	 * Dashboard
	 *
	 * @since   3.1.4
	 *
	 * @var     object
	 */
	public $dashboard = '';

	/**
	 * Classes
	 *
	 * @since   1.0.5
	 *
	 * @var     array
	 */
	public $classes = '';

	/**
	 * Constructor. Acts as a bootstrap to load the rest of the plugin
	 *
	 * @since    1.0.0
	 */
	public function __construct() {

		// Plugin Details.
		$this->plugin                    = new stdClass();
		$this->plugin->name              = 'media-library-organizer';
		$this->plugin->displayName       = 'Media Library Organizer';
		$this->plugin->author_name       = 'Media Library Organizer';
		$this->plugin->version           = MEDIA_LIBRARY_ORGANIZER_PLUGIN_VERSION;
		$this->plugin->buildDate         = MEDIA_LIBRARY_ORGANIZER_PLUGIN_BUILD_DATE;
		$this->plugin->requires          = '5.0';
		$this->plugin->tested            = '5.9.3';
		$this->plugin->folder            = MEDIA_LIBRARY_ORGANIZER_PLUGIN_PATH;
		$this->plugin->url               = MEDIA_LIBRARY_ORGANIZER_PLUGIN_URL;
		$this->plugin->documentation_url = 'https://wpmedialibrary.com/documentation';
		$this->plugin->support_url       = 'https://wpmedialibrary.com/support';
		$this->plugin->upgrade_url       = 'https://wpmedialibrary.com/pricing';
		$this->plugin->review_name       = 'media-library-organizer';
		$this->plugin->review_notice     = sprintf(
			/* translators: Plugin Name */
			__( 'Thanks for using %s to organize your Media Library!', 'media-library-organizer' ),
			$this->plugin->displayName
		);

		// Dashboard Submodule.
		if ( ! class_exists( 'WPZincDashboardWidget' ) ) {
			require_once $this->plugin->folder . '_modules/dashboard/class-wpzincdashboardwidget.php';
		}
		$this->dashboard = new WPZincDashboardWidget( $this->plugin, 'https://wpmedialibrary.com/wp-content/plugins/lum-deactivation' );

		// License Submodule.
		// If the Pro version is installed, make its licensing object available here for screens.
		if ( function_exists( 'Media_Library_Organizer_Pro' ) && class_exists( 'LicensingUpdateManager' ) ) {
			$this->licensing = Media_Library_Organizer_Pro()->licensing;
		}

		// Initialize Free Addons.
		$this->initialize_free_addons();

		// Defer loading of Plugin Classes.
		add_action( 'init', array( $this, 'initialize' ), 1 );
		add_action( 'init', array( $this, 'upgrade' ), 2 );

		// Localization.
		add_action( 'plugins_loaded', array( $this, 'load_language_files' ) );

	}

	/**
	 * Initialize Free Addons
	 *
	 * @since   1.1.4
	 */
	private function initialize_free_addons() {

		// Define Addons Directory.
		$addons_dir = MEDIA_LIBRARY_ORGANIZER_PLUGIN_PATH . '/addons/';

		// Iterate through Addons Directory.
		$files_dirs = scandir( $addons_dir );
		foreach ( $files_dirs as $file_dir ) {
			// Skip dot folders.
			if ( $file_dir === '.' || $file_dir === '..' ) {
				continue;
			}

			// Skip if Addon directory doesn't exist.
			if ( ! is_dir( $addons_dir . $file_dir ) ) {
				continue;
			}

			// Skip if Addon bootstrap file doesn't exist.
			$file = $addons_dir . $file_dir . '/class-media-library-organizer-' . $file_dir . '.php';
			if ( ! file_exists( $file ) ) {
				continue;
			}

			// Load Addon.
			require_once $file;
		}

	}

	/**
	 * Initializes classes and Free Addons.
	 *
	 * @since   1.0.5
	 */
	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.0.9
	 */
	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_Admin( self::$instance );
		$this->classes->admin_ajax = new Media_Library_Organizer_Admin_AJAX( self::$instance );
		$this->classes->export     = new Media_Library_Organizer_Export( self::$instance );
		$this->classes->import     = new Media_Library_Organizer_Import( self::$instance );

	}

	/**
	 * Initialize classes for the frontend web site
	 *
	 * @since   1.0.9
	 */
	private function initialize_frontend() {

		// Bail if this request isn't for the frontend web site.
		if ( is_admin() ) {
			return;
		}

		$this->classes->frontend = new Media_Library_Organizer_Frontend( self::$instance );

	}

	/**
	 * Initialize classes for WP-CLI
	 *
	 * @since   1.0.9
	 */
	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.
		$this->classes->cli = new Media_Library_Organizer_CLI( self::$instance );

	}

	/**
	 * Initialize classes for the WordPress Administration interface or a frontend Page Builder
	 *
	 * @since   1.0.9
	 */
	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 ( ! $this->is_admin_or_frontend_editor() ) {
			return;
		}

		$this->classes->ajax          = new Media_Library_Organizer_AJAX( self::$instance );
		$this->classes->editor        = new Media_Library_Organizer_Editor( self::$instance );
		$this->classes->page_builders = new Media_Library_Organizer_Page_Builders( self::$instance );
		$this->classes->tinymce       = new Media_Library_Organizer_TinyMCE( self::$instance );

	}

	/**
	 * Initialize classes used everywhere
	 *
	 * @since   1.0.9
	 */
	private function initialize_global() {

		$this->classes->common       = new Media_Library_Organizer_Common( self::$instance );
		$this->classes->dynamic_tags = new Media_Library_Organizer_Dynamic_Tags( self::$instance );
		$this->classes->filesystem   = new Media_Library_Organizer_Filesystem( self::$instance );
		$this->classes->install      = new Media_Library_Organizer_Install( self::$instance );
		$this->classes->media        = new Media_Library_Organizer_Media( self::$instance );
		$this->classes->mime         = new Media_Library_Organizer_MIME( self::$instance );
		$this->classes->notices      = new Media_Library_Organizer_Notices( self::$instance );
		$this->classes->settings     = new Media_Library_Organizer_Settings( self::$instance );
		$this->classes->shortcode    = new Media_Library_Organizer_Shortcode( self::$instance );
		$this->classes->taxonomies   = new Media_Library_Organizer_Taxonomies( self::$instance );
		$this->classes->upload       = new Media_Library_Organizer_Upload( self::$instance );
		$this->classes->user_option  = new Media_Library_Organizer_User_Option( self::$instance );

	}

	/**
	 * Improved version of WordPress' is_admin(), which includes whether we're
	 * editing on the frontend using a Page Builder, or a developer / Addon
	 * wants to load Editor, Media Management and Upload classes on the frontend
	 * of the site.
	 *
	 * @since   1.0.7
	 *
	 * @return  bool    Is Admin or Frontend Editor Request
	 */
	public function is_admin_or_frontend_editor() {

		// If we're in the wp-admin, return true.
		if ( is_admin() ) {
			return true;
		}

		// Pro.
		if ( isset( $_SERVER ) ) {
			if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), '/pro/' ) !== false ) {
				return true;
			}
			if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), '/x/' ) !== false ) {
				return true;
			}
			if ( strpos( sanitize_text_field( $_SERVER['REQUEST_URI'] ), 'cornerstone-endpoint' ) !== false ) {
				return true;
			}
		}

		// If the request global exists, check for specific request keys which tell us
		// that we're using a frontend editor.
		if ( isset( $_REQUEST ) && ! empty( $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			// Sanitize request.
			$request = array_map( 'sanitize_text_field', $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification

			// Beaver Builder.
			if ( array_key_exists( 'fl_builder', $request ) ) {
				return true;
			}

			// Cornerstone (AJAX).
			if ( array_key_exists( '_cs_nonce', $request ) ) {
				return true;
			}

			// Divi.
			if ( array_key_exists( 'et_fb', $request ) ) {
				return true;
			}

			// Elementor.
			if ( array_key_exists( 'action', $request ) && $request['action'] === 'elementor' ) {
				return true;
			}

			// Kallyas.
			if ( array_key_exists( 'zn_pb_edit', $request ) ) {
				return true;
			}

			// Oxygen.
			if ( array_key_exists( 'ct_builder', $request ) ) {
				return true;
			}

			// Themify Builder.
			if ( array_key_exists( 'tb-preview', $request ) && array_key_exists( 'tb-id', $request ) ) {
				return true;
			}

			// Thrive Architect.
			if ( array_key_exists( 'tve', $request ) ) {
				return true;
			}

			// Visual Composer.
			if ( array_key_exists( 'vcv-editable', $request ) ) {
				return true;
			}

			// WPBakery Page Builder.
			if ( array_key_exists( 'vc_editable', $request ) ) {
				return true;
			}
		} else {
			$request = false;
		}

		// Assume we're not in the Administration interface.
		$is_admin_or_frontend_editor = false;

		/**
		 * Filters whether the current request is a WordPress Administration / Frontend Editor request or not.
		 *
		 * Page Builders can set this to true to allow Media Library Organizer and its Addons to load its
		 * functionality.
		 *
		 * @since   1.0.7
		 *
		 * @param   bool    $is_admin_or_frontend_editor    Is WordPress Administration / Frontend Editor request.
		 * @param   array   $request                        Sanitized request data.
		 */
		$is_admin_or_frontend_editor = apply_filters( 'media_library_organizer_is_admin_or_frontend_editor', $is_admin_or_frontend_editor, $request );

		// Return filtered result.
		return $is_admin_or_frontend_editor;

	}

	/**
	 * Runs the upgrade routine once the plugin has loaded
	 *
	 * @since   1.0.5
	 */
	public function upgrade() {

		// Bail if we're not in the WordPress Admin.
		if ( ! is_admin() ) {
			return;
		}

		// Run upgrade routine.
		$this->get_class( 'install' )->upgrade();

	}

	/**
	 * Loads plugin textdomain
	 *
	 * @since   1.2.6
	 */
	public function load_language_files() {

		load_plugin_textdomain( 'media-library-organizer', false, 'media-library-organizer/languages/' );

	}

	/**
	 * Returns the given class
	 *
	 * @since   1.0.5
	 *
	 * @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_get_class',
				sprintf(
					/* translators: %1$s: Plugin Name, %2$s: PHP class name */
					__( '%1$s: Error: Could not load Plugin class %2$s', 'media-library-organizer' ),
					$this->plugin->displayName,
					$name
				)
			);

			// Depending on the request, return or display an error.
			// Admin UI.
			if ( is_admin() ) {
				wp_die(
					esc_html( $error->get_error_message() ),
					sprintf(
						/* translators: Plugin Name */
						esc_html__( '%s: Error', 'media-library-organizer' ),
						esc_html( $this->plugin->displayName )
					),
					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.0.0
	 *
	 * @return  object Class.
	 */
	public static function get_instance() {

		if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
			self::$instance = new self();
		}

		return self::$instance;

	}

}