| 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 |
$plugin['app_path'] = 'https://localhost:8080/'; |
| 79 |
$plugin['app_js'] = 'https://localhost:8080/scripts.js'; |
| 80 |
$plugin['app_css'] = 'https://localhost:8080/styles.css'; |
| 81 |
} else { |
| 82 |
$manifest = $plugin['plugin_path'] . 'dist/manifest.json'; |
| 83 |
$manifest = file_get_contents( $manifest ); |
| 84 |
$manifest = (array) json_decode( $manifest ); |
| 85 |
|
| 86 |
$plugin['app_path'] = plugins_url( 'dist/', $plugin_path ); |
| 87 |
$plugin['app_js'] = plugins_url( sprintf( 'dist/%s', $manifest['main.js'] ), $plugin_path ); |
| 88 |
|
| 89 |
if ( isset( $manifest['main.css'] ) ) { |
| 90 |
$plugin['app_css'] = plugins_url( sprintf( 'dist/%s', $manifest['main.css'] ), $plugin_path ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
$this->themes[] = $plugin; |
| 97 |
} |
| 98 |
|
| 99 |
// Load all addons into the public addons array |
| 100 |
$this->installed_addons[] = $plugin; |
| 101 |
|
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Retrieves the meta data from the headers of a given plugin file. |
| 107 |
* |
| 108 |
* @access private |
| 109 |
* @static |
| 110 |
* @param mixed $plugin_file |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
private static function _get_plugin_data($plugin_file) { |
| 114 |
|
| 115 |
$default_headers = array( |
| 116 |
'name' => 'Theme Name', |
| 117 |
'theme-uri' => 'Theme URI', |
| 118 |
'version' => 'Version', |
| 119 |
'description' => 'Description', |
| 120 |
'author' => 'Author', |
| 121 |
'author-uri' => 'Author URI', |
| 122 |
); |
| 123 |
|
| 124 |
return get_file_data( $plugin_file, $default_headers ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Sets the active theme if it is found to be installed |
| 129 |
* and activated. |
| 130 |
* |
| 131 |
* @param string $theme_name |
| 132 |
*/ |
| 133 |
public function set_active_theme($theme_name) { |
| 134 |
|
| 135 |
foreach ( $this->themes as $index => $theme) { |
| 136 |
if ( strtolower( $theme['name'] ) === strtolower( $theme_name ) ) { |
| 137 |
$this->active_theme = $theme; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! $this->active_theme ) { |
| 142 |
throw new Vimeography_Exception( |
| 143 |
__('The Vimeography theme you are trying to use is not installed or activated.', 'vimeography') |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
return $this; |
| 148 |
} |
| 149 |
} |
| 150 |
|