Tribe__Image__Plus__Main


Source #

File: src/Tribe/Main.php

class Tribe__Image__Plus__Main {
	const VERSION = '1.0.3';

	/**
	 * Static Singleton Holder
	 * @var self
	 */
	protected static $instance;

	/**
	 * Path of the plugin.
	 */
	public $plugin_path;

	/**
	 * Plugin directory
	 */
	public $plugin_dir;

	/**
	 * Plugin slug
	 */
	public $plugin_slug;

	/**
	 * Plugin url
	 */
	public $plugin_url;

	/**
	 * PUE checker
	 */
	protected $pue;

	/**
     * The placeholder image src.
     */
	public static $placeholder = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw';

	/**
	 * Get (and instantiate, if necessary) the instance of the class.
	 *
	 * @return self
	 */
	public static function instance() {
		if ( ! self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	}

	/**
	 * Initialize the widget and much of the plugin in general.
	 *
	 * @since 1.0
	 */
	public function __construct() {

		$this->plugin_path = trailingslashit( IMAGE_WIDGET_PLUS_DIR );
		$this->plugin_dir  = trailingslashit( basename( $this->plugin_path ) );
		$this->plugin_url  = trailingslashit( plugins_url( $this->plugin_dir ) );
		$this->plugin_slug = 'image-widget-plus';

		// let's initialize after TEC and ET to prevent common conflicts
		add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 1 );
	}

	/**
	 * To avoid duplication of our own methods and to provide a underlying system
	 * Modern Tribe maintains a Library called Common to store a base for our plugins
	 *
	 * Currently we will read the File `common/package.json` to determine which version
	 * of the Common Lib we will pass to the Auto-Loader of PHP.
	 *
	 * In the past we used to parse `common/src/Tribe/Main.php` for the Common Lib version.
	 *
	 * @link https://github.com/moderntribe/tribe-common
	 * @see  self::init_autoloading
	 *
	 * @return void
	 */
	public function maybe_set_common_lib_info() {
		// if always set common version to something super low because ANY common should override the
		// one in image-widget-plus
		$common_version = '1.0';

		/**
		 * If we don't have a version of Common or an Older version of the Lib
		 * overwrite what should be loaded by the auto-loader
		 */
		if (
			empty( $GLOBALS['tribe-common-info'] ) ||
			version_compare( $GLOBALS['tribe-common-info']['version'], $common_version, '<' )
		) {
			$GLOBALS['tribe-common-info'] = array(
				'dir' => "{$this->plugin_path}common/src/Tribe",
				'version' => $common_version,
			);
		}
	}

	/**
	 * Bootstrap this plugin and all of its pieces
	 *
	 * Fired during plugins_loaded action
	 *
	 * @since 1.0
	 */
	public function plugins_loaded() {

		$this->maybe_set_common_lib_info();

		$this->init_autoloading();

		// Tribe common resources
		require_once $this->plugin_path . 'vendor/tribe-common-libraries/tribe-common-libraries.class.php';

		Tribe__Main::instance();

		// if running an old version of TEC (pre PUE 2.0 with embedded licensing), fake the install key
		if ( version_compare( Tribe__Main::VERSION, '4.5.1', '<' ) ) {
			add_filter( 'pre_option_pue_install_key_image_widget_plus', array( $this, 'filter_install_key' ) );
		}

		if ( class_exists( 'Tribe__Main' ) && ! is_admin() && ! class_exists( 'Tribe__Image__Plus__Main' ) ) {
			tribe_main_pue_helper();
		}

		// Load language files.
		$mopath = $this->plugin_dir . 'lang/';
		$domain = 'image-widget-plus';

		// If we don't have Common classes load the old fashioned way
		if ( ! class_exists( 'Tribe__Main' ) ) {
			load_plugin_textdomain( $domain, false, $mopath );
		} else {
			// This will load `wp-content/languages/plugins` files first
			Tribe__Main::instance()->load_text_domain( $domain, $mopath );
		}

		// Load other files.
		require_once $this->plugin_path . 'src/Tribe/Attachment.php';
		require_once $this->plugin_path . 'src/Tribe/Image.php';
		require_once $this->plugin_path . 'src/Tribe/Widget.php';
		require_once $this->plugin_path . 'src/Tribe/PUE/Helper.php';
		require_once $this->plugin_path . 'src/Tribe/PUE.php';

		// Initialize other classes.
		Tribe__Image__Plus__Attachment::instance();
		Tribe__Image__Plus__Widget::instance();

		// Register the actual Image Widget Plus widget.
		add_action( 'widgets_init', array( $this, 'register_widget' ) );

		// Misc. hooks.
		add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 );

		$this->pue = new Tribe__Image__Plus__PUE( "{$this->plugin_path}/image-widget-plus.php" );
	}

	/**
	 * Requires the autoloader class from the main plugin class and sets up
	 * autoloading.
	 */
	public function init_autoloading() {
		$prefixes = array();

		if ( ! class_exists( 'Tribe__Autoloader' ) ) {
			require_once $GLOBALS['tribe-common-info']['dir'] . '/Autoloader.php';

			$prefixes['Tribe__'] = $GLOBALS['tribe-common-info']['dir'];
		}

		$autoloader = Tribe__Autoloader::instance();

		// include the fake Tribe__Main if a preferred one from TEC/ET hasn't been prepped for use
		if ( ! empty( $prefixes['Tribe__'] ) && "{$this->plugin_path}common/src/Tribe" === $prefixes['Tribe__'] ) {
			require_once $this->plugin_path . 'Tribe__Main.php';
		} else {
			$autoloader->register_prefixes( $prefixes );
		}

		$autoloader->register_prefix( 'Tribe__Image__Plus__', dirname( __FILE__ ) . '/src/Tribe', 'image-widget-plus' );

		// deprecated classes are registered in a class to path fashion
		foreach ( glob( $this->plugin_path . 'src/deprecated/*.php' ) as $file ) {
			$class_name = str_replace( '.php', '', basename( $file ) );
			$autoloader->register_class( $class_name, $file );
		}

		$autoloader->register_autoloader();
	}

	public function register_widget() {
		register_widget( 'Tribe__Image__Plus__Widget' );
	}

	/**
	 * Loads theme files in appropriate hierarchy: First the child theme, then
	 * the parent template, and finally plugin resources. Will look in the
	 * image-widget/ directory in a theme, the views/ directory in the plugin.
	 *
	 * @param string $template Template file to search for.
	 *
	 * @return template path
	 */
	public function template( $template ) {

		// Whether or not .php was added.
		$template_slug = rtrim( $template, '.php' );
		$template      = $template_slug . '.php';

		if ( $theme_file = locate_template( array( "image-widget/{$template}" ) ) ) {
			$file = $theme_file;
		} else {
			$file = "{$this->plugin_path}src/views/{$template}";
		}

		return apply_filters( 'tribe_image_plus_template_' . $template, $file );
	}

	/**
	 * Loads admin views, which aren't "overrideable", so no hierarchy.
	 *
	 * @param string $template Template file to search for.
	 *
	 * @return template path
	 */
	public function admin_template( $template ) {

		// Whether or not .php was added.
		$template_slug = rtrim( $template, '.php' );
		$template      = $template_slug . '.php';

		return "{$this->plugin_path}src/admin-views/{$template}";
	}

	/**
	 * Display an informational section in the plugin admin UI.
	 *
	 * @param array $meta Array of existing meta links.
	 * @param string $file Filename of plugin whose meta row is being filtered.
	 *
	 * @return array The filtered array of meta links.
	 */
	public function plugin_row_meta( $meta, $file ) {

		if ( strpos( $file, 'image-widget-plus.php' ) !== false ) {
			$meta[] = '<a href="http://m.tri.be/19nl" target="_blank">' . esc_html__( 'Support', 'image-widget-plus' ) . '</a>';
		}

		return $meta;
	}

	/**
	 * Filter the PUE install key for image widget plus to use the PUE__Helper class
	 *
	 * Note: this only gets run if running an older version of TEC (pre PUE 2.0 embedded licensing)
	 *
	 * @param string $value
	 *
	 * @return string
	 */
	public function filter_install_key( $value ) {
		if ( $value ) {
			return $value;
		}

		return Tribe__Image__Plus__PUE__Helper::DATA;
	}
}

Top ↑

Methods #

  • __construct — Initialize the widget and much of the plugin in general.
  • admin_template — Loads admin views, which aren't "overrideable", so no hierarchy.
  • filter_install_key — Filter the PUE install key for image widget plus to use the PUE__Helper class
  • init_autoloading — Requires the autoloader class from the main plugin class and sets up autoloading.
  • instance — Get (and instantiate, if necessary) the instance of the class.
  • maybe_set_common_lib_info — To avoid duplication of our own methods and to provide a underlying system Modern Tribe maintains a Library called Common to store a base for our plugins
  • plugin_row_meta — Display an informational section in the plugin admin UI.
  • plugins_loaded — Bootstrap this plugin and all of its pieces
  • register_widget
  • template — Loads theme files in appropriate hierarchy: First the child theme, then the parent template, and finally plugin resources. Will look in the image-widget/ directory in a theme, the views/ directory in the plugin.