Admin
8 years ago
Ajax
8 years ago
Asset
8 years ago
Customizer
8 years ago
Documentation
8 years ago
Duplicate
8 years ago
Image
8 years ago
JSON_LD
8 years ago
Languages
8 years ago
Log
8 years ago
Meta
8 years ago
PUE
8 years ago
Process
8 years ago
REST
8 years ago
Service_Providers
8 years ago
Support
8 years ago
Tabbed_View
8 years ago
Utils
8 years ago
Validator
8 years ago
Abstract_Deactivation.php
8 years ago
App_Shop.php
8 years ago
Assets.php
8 years ago
Assets_Pipeline.php
8 years ago
Autoloader.php
8 years ago
Cache.php
8 years ago
Cache_Listener.php
8 years ago
Changelog_Reader.php
8 years ago
Container.php
8 years ago
Context.php
8 years ago
Cost_Utils.php
8 years ago
Credits.php
8 years ago
Customizer.php
8 years ago
Data.php
8 years ago
Date_Utils.php
8 years ago
Debug.php
8 years ago
Dependency.php
8 years ago
Deprecation.php
8 years ago
Error.php
8 years ago
Exception.php
8 years ago
Extension.php
8 years ago
Extension_Loader.php
8 years ago
Field.php
8 years ago
Field_Conditional.php
8 years ago
Log.php
8 years ago
Main.php
8 years ago
Notices.php
8 years ago
Plugin_Meta_Links.php
8 years ago
Plugins.php
8 years ago
Plugins_API.php
8 years ago
Post_History.php
8 years ago
Post_Transient.php
8 years ago
Rewrite.php
8 years ago
Settings.php
8 years ago
Settings_Manager.php
8 years ago
Settings_Tab.php
8 years ago
Simple_Table.php
8 years ago
Support.php
8 years ago
Tabbed_View.php
8 years ago
Template.php
8 years ago
Template_Factory.php
8 years ago
Template_Part_Cache.php
8 years ago
Templates.php
8 years ago
Terms.php
8 years ago
Timezones.php
8 years ago
Tracker.php
8 years ago
Validate.php
8 years ago
View_Helpers.php
8 years ago
Template_Factory.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Template Factory |
| 4 | * |
| 5 | * The parent class for managing the view methods in core and addons |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | die( '-1' ); |
| 11 | } |
| 12 | |
| 13 | if ( class_exists( 'Tribe__Template_Factory' ) ) { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | class Tribe__Template_Factory { |
| 18 | |
| 19 | /** |
| 20 | * Array of asset packages needed for this template |
| 21 | * |
| 22 | * @var array |
| 23 | **/ |
| 24 | protected $asset_packages = array(); |
| 25 | |
| 26 | /** |
| 27 | * Static variable that holds array of vendor script handles, for adding to later deps. |
| 28 | * |
| 29 | * @static |
| 30 | * @var array |
| 31 | */ |
| 32 | protected static $vendor_scripts = array(); |
| 33 | |
| 34 | /** |
| 35 | * Constant that holds the ajax hook suffix for the view |
| 36 | * |
| 37 | * @static |
| 38 | * @var string |
| 39 | */ |
| 40 | const AJAX_HOOK = ''; |
| 41 | |
| 42 | /** |
| 43 | * Run include packages, set up hooks |
| 44 | * |
| 45 | * @return void |
| 46 | **/ |
| 47 | public function __construct() { |
| 48 | $this->asset_packages(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Manage the asset packages defined for this template |
| 53 | * |
| 54 | * @return void |
| 55 | **/ |
| 56 | protected function asset_packages() { |
| 57 | foreach ( $this->asset_packages as $asset_package ) { |
| 58 | $this->asset_package( $asset_package ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Handles an asset package request. |
| 64 | * |
| 65 | * @param string $name The asset name in the `hyphen-separated-format` |
| 66 | * @param array $deps An array of dependency handles |
| 67 | * @param string $vendor_url URL to vendor scripts and styles dir |
| 68 | * @param string $prefix MT script and style prefix |
| 69 | * @param Tribe__Main $tec An instance of the main plugin class |
| 70 | */ |
| 71 | protected static function handle_asset_package_request( $name, $deps, $vendor_url, $prefix, $tec ) { |
| 72 | |
| 73 | $asset = self::get_asset_factory_instance( $name ); |
| 74 | |
| 75 | self::prepare_asset_package_request( $asset, $name, $deps, $vendor_url, $prefix, $tec ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * initializes asset package request |
| 80 | * |
| 81 | * @param object $asset The Tribe__*Asset object |
| 82 | * @param string $name The asset name in the `hyphen-separated-format` |
| 83 | * @param array $deps An array of dependency handles |
| 84 | * @param string $vendor_url URL to vendor scripts and styles dir |
| 85 | * @param string $prefix MT script and style prefix |
| 86 | * @param Tribe__Main $common An instance of the main plugin class |
| 87 | */ |
| 88 | protected static function prepare_asset_package_request( $asset, $name, $deps, $vendor_url, $prefix, $common ) { |
| 89 | if ( ! $asset ) { |
| 90 | do_action( $prefix . '-' . $name ); |
| 91 | |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $asset->set_name( $name ); |
| 96 | $asset->set_deps( $deps ); |
| 97 | $asset->set_vendor_url( $vendor_url ); |
| 98 | $asset->set_prefix( $prefix ); |
| 99 | $asset->set_tec( $common ); |
| 100 | |
| 101 | $asset->handle(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Retrieves the appropriate asset factory instance |
| 106 | */ |
| 107 | protected static function get_asset_factory_instance( $name ) { |
| 108 | $asset = Tribe__Asset__Factory::instance()->make_for_name( $name ); |
| 109 | return $asset; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param string $script_handle A registered script handle. |
| 114 | */ |
| 115 | public static function add_vendor_script( $script_handle ) { |
| 116 | if ( in_array( $script_handle, self::$vendor_scripts ) ) { |
| 117 | return; |
| 118 | } |
| 119 | self::$vendor_scripts[] = $script_handle; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return string[] An array of registered vendor script handles. |
| 124 | */ |
| 125 | public static function get_vendor_scripts() { |
| 126 | return self::$vendor_scripts; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Asset calls for vendor packages |
| 131 | * |
| 132 | * @param string $name |
| 133 | * @param array $deps Dependents |
| 134 | */ |
| 135 | public static function asset_package( $name, $deps = array() ) { |
| 136 | |
| 137 | $common = Tribe__Main::instance(); |
| 138 | $prefix = 'tribe-events'; |
| 139 | |
| 140 | // setup plugin resources & 3rd party vendor urls |
| 141 | $vendor_url = trailingslashit( $common->plugin_url ) . 'vendor/'; |
| 142 | |
| 143 | self::handle_asset_package_request( $name, $deps, $vendor_url, $prefix, $common ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the path to a minified version of a js or css file, if it exists. |
| 148 | * If the file does not exist, returns false. |
| 149 | * |
| 150 | * @param string $url The path or URL to the un-minified file. |
| 151 | * @param bool $default_to_original Whether to just return original path if min version not found. |
| 152 | * |
| 153 | * @return string|false The path/url to minified version or false, if file not found. |
| 154 | */ |
| 155 | public static function getMinFile( $url, $default_to_original = false ) { |
| 156 | if ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) { |
| 157 | if ( substr( $url, - 3, 3 ) == '.js' ) { |
| 158 | $url_new = substr_replace( $url, '.min', - 3, 0 ); |
| 159 | } |
| 160 | if ( substr( $url, - 4, 4 ) == '.css' ) { |
| 161 | $url_new = substr_replace( $url, '.min', - 4, 0 ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ( isset( $url_new ) && file_exists( str_replace( content_url(), WP_CONTENT_DIR, $url_new ) ) ) { |
| 166 | return $url_new; |
| 167 | } elseif ( $default_to_original ) { |
| 168 | return $url; |
| 169 | } else { |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Playing ping-pong with WooCommerce. They keep changing their script. |
| 176 | * See https://github.com/woothemes/woocommerce/issues/3623 |
| 177 | */ |
| 178 | public static function get_placeholder_handle() { |
| 179 | $placeholder_handle = 'jquery-placeholder'; |
| 180 | |
| 181 | global $woocommerce; |
| 182 | if ( |
| 183 | class_exists( 'Woocommerce' ) && |
| 184 | version_compare( $woocommerce->version, '2.0.11', '>=' ) && |
| 185 | version_compare( $woocommerce->version, '2.0.13', '<=' ) |
| 186 | ) { |
| 187 | $placeholder_handle = 'tribe-placeholder'; |
| 188 | } |
| 189 | |
| 190 | return $placeholder_handle; |
| 191 | } |
| 192 | } |
| 193 |