| @@ -1,72 +1,232 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * The plugin bootstrap file | |
| 4 | - * | |
| 5 | - * This file is read by WordPress to generate the plugin information in the plugin | |
| 6 | - * admin area. This file also includes all of the dependencies used by the plugin, | |
| 7 | - * registers the activation and deactivation functions, and defines a function | |
| 8 | - * that starts the plugin. | |
| 9 | - * | |
| 10 | - * @link https://bootstrapped.ventures/ | |
| 11 | - * @since 3.0.0 | |
| 12 | - * @package WP_Ultimate_Post_Grid | |
| 13 | - * | |
| 14 | - * @wordpress-plugin | |
| 15 | - * Plugin Name: WP Ultimate Post Grid | |
| 16 | - * Plugin URI: https://bootstrapped.ventures/wp-ultimate-post-grid/ | |
| 17 | - * Description: Easily create filterable responsive grids for your posts, pages or custom post types. | |
| 18 | - * Version: 3.3.0 | |
| 19 | - * Author: Bootstrapped Ventures | |
| 20 | - * Author URI: https://bootstrapped.ventures/ | |
| 21 | - * License: GPL-2.0+ | |
| 22 | - * License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
| 23 | - * Text Domain: wp-ultimate-post-grid | |
| 24 | - * Domain Path: /languages | |
| 25 | - */ | |
| 2 | +/* | |
| 3 | +Plugin Name: WP Ultimate Post Grid | |
| 4 | +Plugin URI: http://bootstrapped.ventures/wp-ultimate-post-grid/ | |
| 5 | +Description: Easily create filterable responsive grids for your posts, pages or custom post types | |
| 6 | +Version: 2.0 | |
| 7 | +Author: Bootstrapped Ventures | |
| 8 | +Author URI: http://bootstrapped.ventures | |
| 9 | +License: GPLv3 | |
| 10 | +*/ | |
| 11 | +define( 'WPUPG_VERSION', '2.0' ); | |
| 12 | +define( 'WPUPG_POST_TYPE', 'wpupg_grid' ); | |
| 26 | 13 | |
| 27 | -// If this file is called directly, abort. | |
| 28 | -if ( ! defined( 'WPINC' ) ) { | |
| 29 | - die; | |
| 30 | -} | |
| 14 | +class WPUltimatePostGrid { | |
| 31 | 15 | |
| 32 | -/** | |
| 33 | - * The code that runs during plugin activation. | |
| 34 | - * This action is documented in includes/class-wpupg-activator.php | |
| 35 | - */ | |
| 36 | -function activate_wp_ultimate_post_grid() { | |
| 37 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wpupg-activator.php'; | |
| 38 | - WPUPG_Activator::activate(); | |
| 39 | -} | |
| 16 | + private static $instance; | |
| 17 | + private static $instantiated_by_premium; | |
| 18 | + private static $addons = array(); | |
| 40 | 19 | |
| 41 | -/** | |
| 42 | - * The code that runs during plugin deactivation. | |
| 43 | - * This action is documented in includes/class-wpupg-deactivator.php | |
| 44 | - */ | |
| 45 | -function deactivate_wp_ultimate_post_grid() { | |
| 46 | - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wpupg-deactivator.php'; | |
| 47 | - WPUPG_Deactivator::deactivate(); | |
| 48 | -} | |
| 20 | + /** | |
| 21 | + * Return instance of self | |
| 22 | + */ | |
| 23 | + public static function get( $instantiated_by_premium = false ) | |
| 24 | + { | |
| 25 | + // Instantiate self only once | |
| 26 | + if( is_null( self::$instance ) ) { | |
| 27 | + self::$instantiated_by_premium = $instantiated_by_premium; | |
| 28 | + self::$instance = new self; | |
| 29 | + self::$instance->init(); | |
| 30 | + } | |
| 49 | 31 | |
| 50 | -register_activation_hook( __FILE__, 'activate_wp_ultimate_post_grid' ); | |
| 51 | -register_deactivation_hook( __FILE__, 'deactivate_wp_ultimate_post_grid' ); | |
| 32 | + return self::$instance; | |
| 33 | + } | |
| 52 | 34 | |
| 53 | -/** | |
| 54 | - * The core plugin class that is used to define internationalization, | |
| 55 | - * admin-specific hooks, and public-facing site hooks. | |
| 56 | - */ | |
| 57 | -require plugin_dir_path( __FILE__ ) . 'includes/class-wp-ultimate-post-grid.php'; | |
| 35 | + /** | |
| 36 | + * Returns true if we are using the Premium version | |
| 37 | + */ | |
| 38 | + public static function is_premium_active() | |
| 39 | + { | |
| 40 | + return self::$instantiated_by_premium; | |
| 41 | + } | |
| 58 | 42 | |
| 59 | -/** | |
| 60 | - * Begins execution of the plugin. | |
| 61 | - * | |
| 62 | - * Since everything within the plugin is registered via hooks, | |
| 63 | - * then kicking off the plugin from this point in the file does | |
| 64 | - * not affect the page life cycle. | |
| 65 | - * | |
| 66 | - * @since 3.0.0 | |
| 67 | - */ | |
| 68 | -function run_wp_ultimate_post_grid() { | |
| 69 | - $plugin = new WP_Ultimate_Post_Grid(); | |
| 70 | - add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ) , array( $plugin, 'plugin_action_links' ), 1 ); | |
| 43 | + /** | |
| 44 | + * Add loaded addon to array of loaded addons | |
| 45 | + */ | |
| 46 | + public static function loaded_addon( $addon, $instance ) | |
| 47 | + { | |
| 48 | + if( !array_key_exists( $addon, self::$addons ) ) { | |
| 49 | + self::$addons[$addon] = $instance; | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Returns true if the specified addon has been loaded | |
| 55 | + */ | |
| 56 | + public static function is_addon_active( $addon ) | |
| 57 | + { | |
| 58 | + return array_key_exists( $addon, self::$addons ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + public static function addon( $addon ) | |
| 62 | + { | |
| 63 | + if( isset( self::$addons[$addon] ) ) { | |
| 64 | + return self::$addons[$addon]; | |
| 65 | + } | |
| 66 | + | |
| 67 | + return false; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Access a VafPress option with optional default value | |
| 72 | + */ | |
| 73 | + public static function option( $name, $default = null ) | |
| 74 | + { | |
| 75 | + $option = vp_option( 'wpupg_option.' . $name ); | |
| 76 | + | |
| 77 | + return is_null( $option ) ? $default : $option; | |
| 78 | + } | |
| 79 | + | |
| 80 | + | |
| 81 | + public $pluginName = 'wp-ultimate-post-grid'; | |
| 82 | + public $coreDir; | |
| 83 | + public $corePath; | |
| 84 | + public $coreUrl; | |
| 85 | + public $pluginFile; | |
| 86 | + | |
| 87 | + protected $helper_dirs = array(); | |
| 88 | + protected $helpers = array(); | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Initialize | |
| 92 | + */ | |
| 93 | + public function init() | |
| 94 | + { | |
| 95 | + // Load external libraries | |
| 96 | + if( !class_exists( 'VP_AutoLoader' ) ) { | |
| 97 | + require_once( 'vendor/vafpress/bootstrap.php' ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + // Update plugin version | |
| 101 | + update_option( $this->pluginName . '_version', WPUPG_VERSION ); | |
| 102 | + | |
| 103 | + // Set core directory, URL and main plugin file | |
| 104 | + $this->corePath = str_replace( '/wp-ultimate-post-grid.php', '', plugin_basename( __FILE__ ) ); | |
| 105 | + $this->coreDir = apply_filters( 'wpupg_core_dir', WP_PLUGIN_DIR . '/' . $this->corePath ); | |
| 106 | + $this->coreUrl = apply_filters( 'wpupg_core_url', plugins_url() . '/' . $this->corePath ); | |
| 107 | + $this->pluginFile = apply_filters( 'wpupg_plugin_file', __FILE__ ); | |
| 108 | + | |
| 109 | + // Load textdomain | |
| 110 | + if( !self::is_premium_active() ) { | |
| 111 | + $domain = 'wp-ultimate-post-grid'; | |
| 112 | + $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); | |
| 113 | + | |
| 114 | + load_textdomain( $domain, WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo' ); | |
| 115 | + load_plugin_textdomain( $domain, false, $this->corePath . '/lang/' ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Add core helper directory | |
| 119 | + $this->add_helper_directory( $this->coreDir . '/helpers' ); | |
| 120 | + | |
| 121 | + // Migrate first if needed | |
| 122 | + $this->helper( 'migration' ); | |
| 123 | + | |
| 124 | + // Load required helpers | |
| 125 | + $this->helper( 'activate' ); | |
| 126 | + $this->helper( 'ajax' ); | |
| 127 | + $this->helper( 'content' ); | |
| 128 | + $this->helper( 'css' ); | |
| 129 | + $this->helper( 'faq' ); | |
| 130 | + $this->helper( 'giveaway' ); | |
| 131 | + $this->helper( 'grid_cache' ); | |
| 132 | + $this->helper( 'grid_save' ); | |
| 133 | + $this->helper( 'meta_box' ); | |
| 134 | + $this->helper( 'meta_box_post' ); | |
| 135 | + $this->helper( 'notices' ); | |
| 136 | + $this->helper( 'pagination' ); | |
| 137 | + $this->helper( 'plugin_action_link' ); | |
| 138 | + $this->helper( 'post_save' ); | |
| 139 | + $this->helper( 'post_type' ); | |
| 140 | + $this->helper( 'support_tab' ); | |
| 141 | + $this->helper( 'vafpress_menu' ); | |
| 142 | + $this->helper( 'vafpress_shortcode' ); | |
| 143 | + $this->helper( 'shortcodes/filter_shortcode' ); | |
| 144 | + $this->helper( 'shortcodes/grid_shortcode' ); | |
| 145 | + | |
| 146 | + // Include required helpers but don't instantiate | |
| 147 | + $this->include_helper( 'addons/addon' ); | |
| 148 | + $this->include_helper( 'addons/premium_addon' ); | |
| 149 | + $this->include_helper( 'models/grid' ); | |
| 150 | + | |
| 151 | + // Load core addons | |
| 152 | + $this->helper( 'addon_loader' )->load_addons( $this->coreDir . '/addons' ); | |
| 153 | + | |
| 154 | + // Load default assets | |
| 155 | + $this->helper( 'assets' ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Access a helper. Will instantiate if helper hasn't been loaded before. | |
| 160 | + */ | |
| 161 | + public function helper( $helper ) | |
| 162 | + { | |
| 163 | + // Lazy instantiate helper | |
| 164 | + if( !isset( $this->helpers[$helper] ) ) { | |
| 165 | + $this->include_helper( $helper ); | |
| 166 | + | |
| 167 | + // Get class name from filename | |
| 168 | + $class_name = 'WPUPG'; | |
| 169 | + | |
| 170 | + $dirs = explode( '/', $helper ); | |
| 171 | + $file = end( $dirs ); | |
| 172 | + $name_parts = explode( '_', $file ); | |
| 173 | + foreach( $name_parts as $name_part ) { | |
| 174 | + $class_name .= '_' . ucfirst( $name_part ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + // Instantiate class if exists | |
| 178 | + if( class_exists( $class_name ) ) { | |
| 179 | + $this->helpers[$helper] = new $class_name(); | |
| 180 | + } | |
| 181 | + } | |
| 182 | + | |
| 183 | + // Return helper instance | |
| 184 | + return $this->helpers[$helper]; | |
| 185 | + } | |
| 186 | + | |
| 187 | + /** | |
| 188 | + * Include a helper. Looks through all helper directories that have been added. | |
| 189 | + */ | |
| 190 | + public function include_helper( $helper ) | |
| 191 | + { | |
| 192 | + foreach( $this->helper_dirs as $dir ) | |
| 193 | + { | |
| 194 | + $file = $dir . '/'.$helper.'.php'; | |
| 195 | + | |
| 196 | + if( file_exists( $file ) ) { | |
| 197 | + require_once( $file ); | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 203 | + * Add a directory to look for helpers. | |
| 204 | + */ | |
| 205 | + public function add_helper_directory( $dir ) | |
| 206 | + { | |
| 207 | + if( is_dir( $dir ) ) { | |
| 208 | + $this->helper_dirs[] = $dir; | |
| 209 | + } | |
| 210 | + } | |
| 211 | + | |
| 212 | + /* | |
| 213 | + * Quick access functions | |
| 214 | + */ | |
| 215 | + | |
| 216 | + public function template( $template ) | |
| 217 | + { | |
| 218 | + return $this->addon( 'custom-templates' )->get_template( $template ); | |
| 219 | + } | |
| 71 | 220 | } |
| 72 | -run_wp_ultimate_post_grid(); | |
| 221 | + | |
| 222 | +// Backward compatibility for older versions of WordPress | |
| 223 | +if( !function_exists( 'get_term_meta' ) ) { | |
| 224 | + function get_term_meta ( $term_id = 0, $key = '', $single = false ) { | |
| 225 | + return ''; | |
| 226 | + } | |
| 227 | +} | |
| 228 | + | |
| 229 | +// Premium version is responsible for instantiating if available | |
| 230 | +if( !class_exists( 'WPUltimatePostGridPremium' ) ) { | |
| 231 | + WPUltimatePostGrid::get(); | |
| 232 | +} | |