| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
class Vimeography_Addons { |
| 7 |
|
| 8 |
/** |
| 9 |
* Meta headers of the registered Vimeography themes |
| 10 |
* |
| 11 |
* @access public |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
public $themes = array(); |
| 15 |
|
| 16 |
/** |
| 17 |
* Meta headers of all installed Vimeography addons |
| 18 |
* |
| 19 |
* @access public |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
public $installed_addons = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Meta headers of the current Vimeography theme marked as active. |
| 26 |
* |
| 27 |
* @access public |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
public $active_theme = NULL; |
| 31 |
|
| 32 |
/** |
| 33 |
* Add the plugin registration hook in the constructor. |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
// First hook is kept for legacy purposes, do not remove. |
| 37 |
add_action( 'vimeography/load-theme', array( $this, 'vimeography_load_addon_plugin') ); |
| 38 |
|
| 39 |
// The actual hook to use moving forward |
| 40 |
add_action( 'vimeography/load-addon-plugin', array( $this, 'vimeography_load_addon_plugin') ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Captures the metadata from the calling Vimeography addon plugin |
| 45 |
* that is installed. This is used to send to the updater class and |
| 46 |
* to register all of the installed themes in the theme array. |
| 47 |
* |
| 48 |
* @param string $plugin_path the PHP __FILE__ constant from the calling plugin |
| 49 |
* @return object [description] |
| 50 |
*/ |
| 51 |
public function vimeography_load_addon_plugin($plugin_path) { |
| 52 |
$plugin = self::_get_plugin_data( $plugin_path ); |
| 53 |
|
| 54 |
$plugin['basename'] = plugin_basename( $plugin_path ); |
| 55 |
$plugin['slug'] = substr($plugin['basename'], 0, strpos($plugin['basename'], "/")); |
| 56 |
$plugin['thumbnail'] = plugins_url(strtolower($plugin['name']) .'.jpg', $plugin_path); |
| 57 |
$plugin['file_path'] = $plugin_path; |
| 58 |
$plugin['plugin_path'] = plugin_dir_path($plugin_path); |
| 59 |
|
| 60 |
// Hacky way of figuring this out, but will do for now. |
| 61 |
$plugin['type'] = file_exists( plugin_dir_path( $plugin_path ) . 'settings.php' ) ? |
| 62 |
'theme' : |
| 63 |
'extension'; |
| 64 |
|
| 65 |
if ( $plugin['type'] === 'theme' ) { |
| 66 |
$plugin['partials_path'] = plugin_dir_path( $plugin_path ) . 'partials'; |
| 67 |
$plugin['plugin_override_path'] = get_stylesheet_directory() . '/vimeography/' . trailingslashit( strtolower( $plugin['name'] ) ); |
| 68 |
$plugin['partials_override_path'] = get_stylesheet_directory() . '/vimeography/' . trailingslashit( strtolower( $plugin['name'] ) ) . 'partials'; |
| 69 |
$plugin['settings_file'] = plugin_dir_path( $plugin_path ) . 'settings.php'; |
| 70 |
|
| 71 |
$default_theme = apply_filters('vimeography.gallery.new.theme', 'harvestone' ); |
| 72 |
$plugin['is_default'] = strtolower( $plugin['name'] ) === strtolower( $default_theme ); |
| 73 |
|
| 74 |
// Provide path to Javascript bundle if theme supports it. |
| 75 |
if ( version_compare( $plugin['version'], '2.0', '>=' ) ) { |
| 76 |
|
| 77 |
if ( defined('VIMEOGRAPHY_DEV') && VIMEOGRAPHY_DEV ) { |
| 78 |
$slug = strtolower($plugin['name']); |
| 79 |
$port = $slug === 'harvestone' ? "8153" : "8346"; |
| 80 |
$bundle = $slug === 'harvestone' ? "scripts.js" : "vimeography-$slug/dist/$slug.js"; |
| 81 |
|
| 82 |
// Use the gitpod env vars if they are defined |
| 83 |
if (defined('VIMEOGRAPHY_THEME_BUNDLE_JS_URL') && $port === "8346") { |
| 84 |
$plugin['app_path'] = VIMEOGRAPHY_THEME_BUNDLE_JS_URL . "/"; |
| 85 |
$plugin['app_js'] = VIMEOGRAPHY_THEME_BUNDLE_JS_URL . '/' . $bundle; |
| 86 |
$plugin['app_css'] = VIMEOGRAPHY_THEME_BUNDLE_JS_URL . "/styles.css"; |
| 87 |
} elseif (defined('VIMEOGRAPHY_HARVESTONE_JS_URL') && $port === "8153") { |
| 88 |
$plugin['app_path'] = VIMEOGRAPHY_HARVESTONE_JS_URL . "/"; |
| 89 |
$plugin['app_js'] = VIMEOGRAPHY_HARVESTONE_JS_URL . '/' . $bundle; |
| 90 |
$plugin['app_css'] = VIMEOGRAPHY_HARVESTONE_JS_URL . "/styles.css"; |
| 91 |
} else { |
| 92 |
$plugin['app_path'] = "http://localhost:$port/"; |
| 93 |
$plugin['app_js'] = "http://localhost:$port/$bundle"; |
| 94 |
$plugin['app_css'] = "http://localhost:8080/styles.css"; |
| 95 |
} |
| 96 |
} else { |
| 97 |
// note: can use $plugin['version'] for cachebusting. |
| 98 |
|
| 99 |
$manifest = $plugin['plugin_path'] . 'dist/manifest.json'; |
| 100 |
$manifest = file_get_contents( $manifest ); |
| 101 |
$manifest = (array) json_decode( $manifest ); |
| 102 |
|
| 103 |
$plugin['app_path'] = plugins_url( 'dist/', $plugin_path ); |
| 104 |
$plugin['app_js'] = plugins_url( sprintf( 'dist/%s', $manifest['main.js'] ), $plugin_path ); |
| 105 |
|
| 106 |
if ( isset( $manifest['main.css'] ) ) { |
| 107 |
$plugin['app_css'] = plugins_url( sprintf( 'dist/%s', $manifest['main.css'] ), $plugin_path ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
$this->themes[] = $plugin; |
| 114 |
} |
| 115 |
|
| 116 |
// Load all addons into the public addons array |
| 117 |
$this->installed_addons[] = $plugin; |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Retrieves the meta data from the headers of a given plugin file. |
| 124 |
* |
| 125 |
* @access private |
| 126 |
* @static |
| 127 |
* @param mixed $plugin_file |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private static function _get_plugin_data($plugin_file) { |
| 131 |
|
| 132 |
$default_headers = array( |
| 133 |
'name' => 'Theme Name', |
| 134 |
'theme-uri' => 'Theme URI', |
| 135 |
'version' => 'Version', |
| 136 |
'description' => 'Description', |
| 137 |
'author' => 'Author', |
| 138 |
'author-uri' => 'Author URI', |
| 139 |
); |
| 140 |
|
| 141 |
return get_file_data( $plugin_file, $default_headers ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Sets the active theme if it is found to be installed |
| 146 |
* and activated. |
| 147 |
* |
| 148 |
* @param string $theme_name |
| 149 |
*/ |
| 150 |
public function set_active_theme($theme_name) { |
| 151 |
|
| 152 |
foreach ( $this->themes as $index => $theme) { |
| 153 |
if ( strtolower( $theme['name'] ) === strtolower( $theme_name ) ) { |
| 154 |
$this->active_theme = $theme; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! $this->active_theme ) { |
| 159 |
throw new Vimeography_Exception( |
| 160 |
esc_html__('The Vimeography theme you are trying to use is not installed or activated.', 'vimeography') |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
return $this; |
| 165 |
} |
| 166 |
} |
| 167 |
|