| 1 |
<?php |
| 2 |
/** |
| 3 |
* Updraft Theme. |
| 4 |
*/ |
| 5 |
|
| 6 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 7 |
|
| 8 |
define('TU_THEME_DIR', dirname(__FILE__)); |
| 9 |
define('TU_THEME_URL', trailingslashit(plugins_url('', __FILE__))); |
| 10 |
|
| 11 |
class TU_Theme { |
| 12 |
const VERSION = '0.1'; |
| 13 |
|
| 14 |
protected static $_instance = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Creates an instance of this class. Singleton Pattern |
| 18 |
* |
| 19 |
* @return object Instance of this class |
| 20 |
*/ |
| 21 |
public static function instance() { |
| 22 |
if (empty(self::$_instance)) { |
| 23 |
self::$_instance = new self(); |
| 24 |
} |
| 25 |
|
| 26 |
return self::$_instance; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns the version to use when enqueueing or loading scripts and styles. |
| 31 |
* If WP_DEBUG is on then time is padded to the VERSION constant. |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
private function get_enqueue_version() { |
| 36 |
return (defined('WP_DEBUG') && WP_DEBUG) ? self::VERSION.'.'.time() : self::VERSION; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Load the components on the front-end of the site. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function load_assets_on_frontend() { |
| 45 |
add_action('wp_enqueue_scripts', array($this, 'load_css'), 20); |
| 46 |
add_action('wp_enqueue_scripts', array($this, 'load_js'), 20); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Load the components on the wp-admin(backend) of the site. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function load_assets_on_backend() { |
| 55 |
add_action('admin_enqueue_scripts', array($this, 'load_css'), 20); |
| 56 |
add_action('admin_enqueue_scripts', array($this, 'load_js'), 20); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns all the handlebar template's contents keyed by their path and name. |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function get_handlebar_templates() { |
| 65 |
$base_directory = TU_THEME_DIR . '/components/'; |
| 66 |
$templates_array = array(); |
| 67 |
|
| 68 |
// Recursive directory iterator to find all .handlebars.html files |
| 69 |
$iterator = new RecursiveIteratorIterator( |
| 70 |
new RecursiveDirectoryIterator($base_directory), |
| 71 |
RecursiveIteratorIterator::SELF_FIRST |
| 72 |
); |
| 73 |
|
| 74 |
foreach ($iterator as $file) { |
| 75 |
// Check for '.' and '..' and skip them. |
| 76 |
if ($file->getFilename() === '.' || $file->getFilename() === '..') { |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
if ($file->isFile() && preg_match('/\.handlebars\.html$/', $file->getFilename())) { |
| 81 |
// Get relative path from base directory |
| 82 |
$relative_path = str_replace($base_directory, '', $file->getPathname()); |
| 83 |
|
| 84 |
// Remove extension |
| 85 |
$relative_path = preg_replace('/\.handlebars\.html$/', '', $relative_path); |
| 86 |
|
| 87 |
// Replace directory separators with hyphens |
| 88 |
$key = str_replace(DIRECTORY_SEPARATOR, '-', $relative_path); |
| 89 |
|
| 90 |
// Store file contents |
| 91 |
$templates_array[$key] = file_get_contents($file->getPathname()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $templates_array; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Load CSS. |
| 100 |
*/ |
| 101 |
public function load_css() { |
| 102 |
$enqueue_version = $this->get_enqueue_version(); |
| 103 |
|
| 104 |
// Global critical styles. |
| 105 |
wp_enqueue_style('tu-global-theme', TU_THEME_URL.'theme.css', array(), $enqueue_version); |
| 106 |
|
| 107 |
// Allow projects to opt-out from theme colors and enqueue there own colors. |
| 108 |
if (apply_filters('tu_theme_load_colors', true)) { |
| 109 |
wp_enqueue_style('tu-theme-colors', TU_THEME_URL.'theme-colors.css', array(), $enqueue_version); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Load JS. |
| 115 |
*/ |
| 116 |
public function load_js() { |
| 117 |
$enqueue_version = $this->get_enqueue_version(); |
| 118 |
|
| 119 |
wp_enqueue_script('tu-theme-handlebars-library', TU_THEME_URL . 'handlebar-library/handlebars.min.js', array(), $enqueue_version, false); |
| 120 |
wp_enqueue_script('tu-theme', TU_THEME_URL . 'theme.js', array('tu-theme-handlebars-library'), $enqueue_version, false); |
| 121 |
wp_localize_script( |
| 122 |
'tu-theme', |
| 123 |
'TU_Theme', |
| 124 |
array( |
| 125 |
'handlebar_templates' => $this->get_handlebar_templates(), |
| 126 |
) |
| 127 |
); |
| 128 |
} |
| 129 |
} |