PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/nonus/framework/createit/ctThemeLoader.php

https://github.com/alniko009/magic
PHP | 539 lines | 266 code | 132 blank | 141 comment | 24 complexity | e7110a9cab87616adb9d9fbdfa8178b5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once 'ctFilesLoader.class.php';
  3. /**
  4. * Theme Loader
  5. * @author alex
  6. */
  7. class ctThemeLoader {
  8. /**
  9. * @var string
  10. */
  11. protected $projectName;
  12. /**
  13. * Is theme activation?
  14. * @var bool
  15. */
  16. protected $themeActivation = false;
  17. /**
  18. * Bundled childs
  19. * @var array
  20. */
  21. protected $prepackedChildThemes = array();
  22. /**
  23. * @var ctFilesLoader
  24. */
  25. protected static $filesLoader;
  26. /**
  27. * Inits
  28. */
  29. public function init($projectName) {
  30. $this->projectName = str_replace(array(' '), '', strtolower($projectName));
  31. $this->constants();
  32. $this->initRoots();
  33. $this->initCreateit();
  34. //shortcodes
  35. $this->initShortcodes();
  36. //widgets
  37. $this->initWidgets();
  38. //custom types
  39. $this->initCustomTypes();
  40. //breadcrumbs
  41. $this->initBreadcrumbs();
  42. $this->validateSetup();
  43. //stylesheets
  44. add_action('wp_enqueue_scripts', array($this, 'scripts'), 100);
  45. add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
  46. $this->clickDemoImport();
  47. add_action('init', array($this, 'initThemeOptions'), 0);
  48. $this->initFrontendOptions();
  49. $this->initAdminOptions();
  50. $this->initPlugins();
  51. self::getFilesLoader()->requireOnce(CT_THEME_SETTINGS_MAIN_DIR . '/config.php');
  52. add_action('after_switch_theme', array($this, 'themeActivation'), 10, 2);
  53. add_action('admin_init', array($this, 'addAutoUpdate'));
  54. //listens to options loader
  55. if (self::getFilesLoader()->tryIncludeOnce(CT_THEME_SETTINGS_MAIN_DIR . '/plugin/deps.php')) {
  56. self::getFilesLoader()->requireOnce(CT_THEME_LIB_DIR . '/plugin/class-tgm-plugin-activation.php');
  57. add_action('tgmpa_register', array($this, 'registerPluginDeps'));
  58. }
  59. apply_filters('ct_loader.init', $projectName);
  60. }
  61. /**
  62. * FilesLoader - loads required files
  63. * @return ctFilesLoader
  64. */
  65. public static function getFilesLoader() {
  66. if (!self::$filesLoader) {
  67. self::$filesLoader = new ctFilesLoader();
  68. }
  69. return self::$filesLoader;
  70. }
  71. /**
  72. * Handle click demo import
  73. */
  74. public function clickDemoImport() {
  75. self::getFilesLoader()->requireOnce(CT_THEME_LIB_DIR . '/demo/install.php');
  76. }
  77. /**
  78. * Set child theme names which are bundled with these theme
  79. * @see automatic update - these child themes will be updateable by internal udpdated
  80. * @param $names
  81. */
  82. public function setPrepackedChildThemes($names) {
  83. $this->prepackedChildThemes = $names;
  84. }
  85. /**
  86. * Checks is eveyrthing ok
  87. */
  88. protected function validateSetup() {
  89. add_action('admin_notices', array($this, 'showLongDirNameError'));
  90. }
  91. /**
  92. * Inits plugins
  93. */
  94. public function registerPluginDeps() {
  95. $plugins = array();
  96. //load $plugins variable
  97. require self::getFilesLoader()->getFilePath(CT_THEME_SETTINGS_MAIN_DIR . '/plugin/deps.php');
  98. // Change this to your theme text domain, used for internationalising strings
  99. /**
  100. * Array of configuration settings. Amend each line as needed.
  101. * If you want the default strings to be available under your own theme domain,
  102. * leave the strings uncommented.
  103. * Some of the strings are added into a sprintf, so see the comments at the
  104. * end of each line for what each argument will be.
  105. */
  106. $config = array(
  107. 'domain' => 'ct_theme', // Text domain - likely want to be the same as your theme.
  108. 'default_path' => '', // Default absolute path to pre-packaged plugins
  109. 'parent_menu_slug' => 'themes.php', // Default parent menu slug
  110. 'parent_url_slug' => 'themes.php', // Default parent URL slug
  111. 'menu' => 'install-required-plugins', // Menu slug
  112. 'has_notices' => true, // Show admin notices or not
  113. 'is_automatic' => false, // Automatically activate plugins after installation or not
  114. 'message' => '', // Message to output right before the plugins table
  115. 'strings' => array(
  116. 'page_title' => __('Install Required Plugins', 'ct_theme'),
  117. 'menu_title' => __('Install Plugins', 'ct_theme'),
  118. 'installing' => __('Installing Plugin: %s', 'ct_theme'), // %1$s = plugin name
  119. 'oops' => __('Something went wrong with the plugin API.', 'ct_theme'),
  120. 'notice_can_install_required' => _n_noop('This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.'), // %1$s = plugin name(s)
  121. 'notice_can_install_recommended' => _n_noop('This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.'), // %1$s = plugin name(s)
  122. 'notice_cannot_install' => _n_noop('Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.'), // %1$s = plugin name(s)
  123. 'notice_can_activate_required' => _n_noop('The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.'), // %1$s = plugin name(s)
  124. 'notice_can_activate_recommended' => _n_noop('The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.'), // %1$s = plugin name(s)
  125. 'notice_cannot_activate' => _n_noop('Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.'), // %1$s = plugin name(s)
  126. 'notice_ask_to_update' => _n_noop('The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.'), // %1$s = plugin name(s)
  127. 'notice_cannot_update' => _n_noop('Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.'), // %1$s = plugin name(s)
  128. 'install_link' => _n_noop('Begin installing plugin', 'Begin installing plugins'),
  129. 'activate_link' => _n_noop('Activate installed plugin', 'Activate installed plugins'),
  130. 'return' => __('Return to Required Plugins Installer', 'ct_theme'),
  131. 'plugin_activated' => __('Plugin activated successfully.', 'ct_theme'),
  132. 'complete' => __('All plugins installed and activated successfully. %s', 'ct_theme'), // %1$s = dashboard link
  133. 'nag_type' => 'updated' // Determines admin notice type - can only be 'updated' or 'error'
  134. )
  135. );
  136. tgmpa($plugins, $config);
  137. }
  138. /**
  139. * Listener for theme activation
  140. */
  141. public function themeActivation() {
  142. $this->themeActivation = true;
  143. }
  144. /**
  145. * Displays a message when theme is deployed not correctly
  146. */
  147. public function showLongDirNameError() {
  148. $name = basename($this->getThemeDir());
  149. if (strlen($name) > 20) {
  150. echo '<div class="error">
  151. <p><strong>' . sprintf(__("Your theme's directory name %s is too long! It will cause unpredictable errors in your Wordpres installation. Please change it's directory name to maximum 20 characters.", 'ct_theme'), $name) . '</strong></p>
  152. </div>';
  153. }
  154. }
  155. /**
  156. * Returns theme dir (or child dir)
  157. * @return string
  158. */
  159. public function getThemeDir() {
  160. return is_child_theme() ? get_stylesheet_directory() : CT_THEME_DIR;
  161. }
  162. /**
  163. * Add auto update
  164. */
  165. public function addAutoUpdate() {
  166. self::getFilesLoader()->requireOnce(CT_THEME_LIB_DIR . '/updater/ctThemeUpdater.class.php');
  167. //check child theme update
  168. $mainTheme = $this->projectName;
  169. if (is_child_theme() && (in_array($this->projectName, $this->prepackedChildThemes))) {
  170. $u = new ctThemeUpdater(basename(get_stylesheet_directory()));
  171. $u->setInternalName($this->projectName);
  172. if ($this->themeActivation) {
  173. $u->checkForUpdates();
  174. }
  175. //we will guess that that is it's name
  176. $mainTheme = basename(CT_THEME_DIR);
  177. }
  178. $u = new ctThemeUpdater(basename(CT_THEME_DIR));
  179. $u->setInternalName($mainTheme);
  180. if ($this->themeActivation) {
  181. $u->checkForUpdates();
  182. }
  183. /** @var $NHP_Options NHP_Options */
  184. global $NHP_Options;
  185. //add options listener for license
  186. add_action('nhp-opts-options-validate-' . $NHP_Options->args['opt_name'], array($u, 'handleLicenseKeySaved'));
  187. }
  188. /**
  189. * Inits frontend options
  190. */
  191. protected function initFrontendOptions() {
  192. //registers additional option events
  193. self::getFilesLoader()->requireOnce(dirname(__FILE__) . '/ctFrontendOptionsHandler.class.php');
  194. $h = new ctFrontendOptionsHandler();
  195. $h->init();
  196. }
  197. /**
  198. * Inits admin options
  199. */
  200. protected function initAdminOptions() {
  201. //registers additional option events
  202. self::getFilesLoader()->requireOnce(dirname(__FILE__) . '/ctAdminOptionsHandler.class.php');
  203. $h = new ctAdminOptionsHandler();
  204. $h->init();
  205. }
  206. /**
  207. * Setup constants
  208. */
  209. protected function constants() {
  210. define("CT_THEME_DIR", get_template_directory());
  211. define("CT_THEME_DIR_URI", get_template_directory_uri());
  212. define("CT_THEME_ASSETS", CT_THEME_DIR_URI . '/assets');
  213. //lib dir
  214. define("CT_THEME_LIB_DIR", CT_THEME_DIR . '/framework');
  215. define("CT_THEME_LIB_DIR_URI", CT_THEME_DIR_URI . '/framework');
  216. define("CT_THEME_ADMIN_ASSETS_URI", CT_THEME_LIB_DIR_URI . '/admin/assets');
  217. //theme lib dir
  218. define("CT_THEME_LIB_WIDGETS", CT_THEME_LIB_DIR . '/widgets');
  219. //theme settings
  220. define("CT_THEME_SETTINGS_MAIN_DIR", CT_THEME_DIR . '/theme');
  221. //theme demo content
  222. $demoDir = CT_THEME_SETTINGS_MAIN_DIR . '/demo';
  223. if (is_child_theme()) {
  224. $dir = get_stylesheet_directory() . '/demo';
  225. if (file_exists($dir)) {
  226. $demoDir = $dir;
  227. }
  228. }
  229. define("CT_THEME_DEMO_DIR", $demoDir);
  230. define("CT_THEME_SETTINGS_MAIN_DIR_URI", CT_THEME_DIR_URI . '/theme');
  231. //theme shortcodes
  232. define("CT_THEME_SHORTCODE_DIR", CT_THEME_SETTINGS_MAIN_DIR . '/shortcodes');
  233. //custom types
  234. define("CT_THEME_CUSTOM_TYPES_DIR", CT_THEME_SETTINGS_MAIN_DIR . '/types');
  235. //breadcrumbs
  236. define("CT_THEME_BREADCRUMBS_DIR", CT_THEME_SETTINGS_MAIN_DIR . '/breadcrumbs');
  237. //theme widgets
  238. define("CT_THEME_WIDGETS_DIR", CT_THEME_SETTINGS_MAIN_DIR . '/widgets');
  239. }
  240. /**
  241. * Do we need upgrade?
  242. * @return bool
  243. */
  244. protected function isUpgradeRequired() {
  245. $theme_data = wp_get_theme();
  246. $newVersion = (float)$theme_data->get('Version');
  247. if (!$currentVersion = (float)get_option($this->projectName . '_theme_version', 0)) {
  248. return false;
  249. }
  250. return version_compare($newVersion, $currentVersion, '>');
  251. }
  252. /**
  253. * Inits Option tree
  254. */
  255. public function initThemeOptions() {
  256. if (!class_exists('ctNHP_Options')) {
  257. self::getFilesLoader()->requireOnce(CT_THEME_LIB_DIR . '/createit/ctNHP_Options.class.php');
  258. }
  259. //load our theme options
  260. $dir = CT_THEME_SETTINGS_MAIN_DIR . '/options';
  261. //these variables are for being filled in theme
  262. $sections = array(); //section structure
  263. $tabs = array(); //additional, custom tabs
  264. $args = array(); //options configuration
  265. //include because we use injected variables. Otherwise we could use ctFilesLoader::includeOnce
  266. include self::getFilesLoader()->getFilePath($dir . '/init.php');
  267. /** @var $order array */
  268. foreach ($order as $e) {
  269. include self::getFilesLoader()->getFilePath($dir . '/_' . $e . '.php');
  270. }
  271. //add options
  272. if ($s = apply_filters('ct_theme_loader.options.load', $sections)) {
  273. $sections = $s;
  274. }
  275. //lets initialize options
  276. global $NHP_Options;
  277. $NHP_Options = new ctNHP_Options($sections, $args, $tabs);
  278. if ($this->isUpgradeRequired()) {
  279. $NHP_Options->refresh();
  280. }
  281. $theme_data = wp_get_theme();
  282. //add current version
  283. update_option($this->projectName . '_theme_version', (float)$theme_data->get('Version'));
  284. add_action('nhp-opts-load-page-' . $NHP_Options->getOptionsPageName(), array($this, 'themeOptionsCustomAssets'));
  285. }
  286. public function themeOptionsCustomAssets() {
  287. wp_register_script('ct_admin_theme_options_js_codemirror', CT_THEME_ADMIN_ASSETS_URI . '/js/codemirror.compressed.js');
  288. wp_register_script('ct_admin_theme_options_js', CT_THEME_ADMIN_ASSETS_URI . '/js/options.js', array('ct_admin_theme_options_js_codemirror'));
  289. wp_enqueue_script('ct_admin_theme_options_js');
  290. wp_enqueue_style('ct_admin_theme_options_js_codemirror', CT_THEME_ADMIN_ASSETS_URI . '/css/codemirror/codemirror.css');
  291. }
  292. /**
  293. * Counts theme options qty
  294. * @param array $sections
  295. * @return int
  296. */
  297. protected function countThemeOptionsQty($sections) {
  298. $total = 0;
  299. foreach ($sections as $s) {
  300. if (isset($s['fields'])) {
  301. $total += count($s['fields']);
  302. }
  303. }
  304. return $total;
  305. }
  306. /**
  307. * Init shortcodes
  308. */
  309. protected function initShortcodes() {
  310. require_once CT_THEME_LIB_DIR . '/shortcodes/ctShortcodeHandler.class.php';
  311. ctShortcodeHandler::getInstance(); //initialize shortcodes
  312. }
  313. /**
  314. * Add scripts
  315. */
  316. public function scripts() {
  317. wp_enqueue_script('jquery');
  318. if (file_exists(CT_THEME_DIR . '/assets/css/style.css')) {
  319. wp_enqueue_style('ct_theme', CT_THEME_DIR_URI . '/assets/css/style.css', false, null);
  320. } elseif (file_exists(CT_THEME_DIR . '/assets/less/style.less')) {
  321. wp_enqueue_style('ct_theme', CT_THEME_DIR_URI . '/ct/css.php?assets/css/style.css', false, null);
  322. } else {
  323. wp_enqueue_style('ct_bootstrap', CT_THEME_DIR_URI . '/assets/css/bootstrap.css', false, null);
  324. wp_enqueue_style('ct_bootstrap_responsive', CT_THEME_DIR_URI . '/assets/css/bootstrap-responsive.css', array('ct_bootstrap'), null);
  325. wp_enqueue_style('ct_app', CT_THEME_DIR_URI . '/assets/css/app.css', false, null);
  326. }
  327. // Load style.css from child theme
  328. if (is_child_theme()) {
  329. wp_enqueue_style('ct_child', get_stylesheet_uri(), false, null);
  330. }
  331. if (is_single() && comments_open() && get_option('thread_comments')) {
  332. wp_enqueue_script('comment-reply');
  333. }
  334. wp_register_script('ct_plugins', CT_THEME_DIR_URI . '/assets/js/plugins.js', array('jquery'), null, true);
  335. wp_enqueue_script('ct_plugins');
  336. //custom user style
  337. if (file_exists(CT_THEME_DIR . '/assets/js/main.js')) {
  338. wp_register_script('ct_main', CT_THEME_DIR_URI . '/assets/js/main.js', array('jquery'), null, true);
  339. wp_enqueue_script('ct_main');
  340. }
  341. }
  342. /**
  343. * Adds admin scripts
  344. */
  345. public function admin_scripts() {
  346. wp_register_style('ct_admin_style', CT_THEME_ADMIN_ASSETS_URI . '/css/style.css');
  347. wp_enqueue_style('ct_admin_style');
  348. wp_register_script('ct_admin_js', CT_THEME_ADMIN_ASSETS_URI . '/js/admin.js');
  349. wp_enqueue_script('ct_admin_js');
  350. }
  351. /**
  352. * Roots
  353. */
  354. protected function initRoots() {
  355. if (!defined('__DIR__')) {
  356. define('__DIR__', dirname(__FILE__));
  357. }
  358. //load core files
  359. $files = array('utils', 'config', 'cleanup');
  360. foreach ($files as $f) {
  361. self::getFilesLoader()->includeOnce(CT_THEME_LIB_DIR . '/roots/' . $f . '.php');
  362. }
  363. }
  364. /**
  365. * Roots
  366. */
  367. protected function initCreateit() {
  368. if (!defined('__DIR__')) {
  369. define('__DIR__', dirname(__FILE__));
  370. }
  371. //load core files
  372. $files = array('cleanup', 'tools');
  373. foreach ($files as $f) {
  374. self::getFilesLoader()->includeOnce(CT_THEME_LIB_DIR . '/createit/' . $f . '.php');
  375. }
  376. //load additional optional libs - internal use, not available in production theme
  377. if (file_exists(CT_THEME_DIR . '/ct/functions.php')) {
  378. require_once CT_THEME_DIR . '/ct/functions.php';
  379. }
  380. add_filter('widget_text', 'do_shortcode');
  381. }
  382. /**
  383. * Init plugins
  384. */
  385. protected function initPlugins() {
  386. self::getFilesLoader()->includeOnceByPattern(CT_THEME_LIB_DIR . '/plugin');
  387. self::getFilesLoader()->includeOnceByPattern(CT_THEME_SETTINGS_MAIN_DIR . '/plugin');
  388. }
  389. /**
  390. * Init widgets
  391. */
  392. protected function initWidgets() {
  393. self::getFilesLoader()->includeOnceByPattern(CT_THEME_WIDGETS_DIR, '/*/*.php');
  394. }
  395. /**
  396. * Init custom types
  397. */
  398. protected function initCustomTypes() {
  399. self::getFilesLoader()->includeOnceByPattern(CT_THEME_CUSTOM_TYPES_DIR);
  400. }
  401. /**
  402. * Init breadcrumbs
  403. */
  404. protected function initBreadcrumbs() {
  405. self::getFilesLoader()->includeOnceByPattern(CT_THEME_BREADCRUMBS_DIR);
  406. }
  407. }