framework.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /**
  3. * Vamtam Theme Framework base class
  4. *
  5. * @author Nikolay Yordanov <me@nyordanov.com>
  6. * @package vamtam/consulting
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  9. /**
  10. * This is the first loaded framework file
  11. *
  12. * VamtamFramework does the following ( in this order ):
  13. * - sets constants for the frequently used paths
  14. * - loads translations
  15. * - loads the plugins bundled with the theme
  16. * - loads some functions and helpers used in various places
  17. * - sets the custom post types
  18. * - if this is wp-admin, load admin files
  19. *
  20. * This class also loads the custom widgets and sets what the theme supports ( + custom menus )
  21. */
  22. class VamtamFramework {
  23. /**
  24. * Cache the result of some operations in memory
  25. *
  26. * @var array
  27. */
  28. private static $cache = array();
  29. /**
  30. * Post types with double sidebars
  31. */
  32. public static $complex_layout = array( 'page', 'post', 'jetpack-portfolio', 'product' );
  33. /**
  34. * Initialize the Vamtam framework
  35. * @param array $options framework options
  36. */
  37. public function __construct( $options ) {
  38. // Autoload classes on demand
  39. if ( function_exists( '__autoload' ) )
  40. spl_autoload_register( '__autoload' );
  41. spl_autoload_register( array( $this, 'autoload' ) );
  42. self::$complex_layout = apply_filters( 'vamtam_complex_layout', self::$complex_layout );
  43. $this->set_constants( $options );
  44. $this->load_languages();
  45. $this->load_functions();
  46. $this->load_admin();
  47. require_once VAMTAM_DIR . 'classes/plugin-activation.php';
  48. require_once VAMTAM_SAMPLES_DIR . 'dependencies.php';
  49. add_action( 'after_setup_theme', array( __CLASS__, 'theme_supports' ) );
  50. add_action( 'init', array( __CLASS__, 'late_init' ), 100 );
  51. add_action( 'widgets_init', array( __CLASS__, 'load_widgets' ) );
  52. add_filter( 'vamtam_purchase_code', array( __CLASS__, 'get_purchase_code' ) );
  53. add_filter( 'wpv_purchase_code', array( __CLASS__, 'get_purchase_code' ) );
  54. VamtamLoadMore::get_instance();
  55. VamtamHideWidgets::get_instance();
  56. VamtamSitemap::setup();
  57. VamtamMaintenanceMode::setup();
  58. }
  59. /**
  60. * Autoload classes when needed
  61. *
  62. * @param string $class class name
  63. */
  64. public function autoload( $class ) {
  65. $class = strtolower( preg_replace( '/([a-z])([A-Z])/', '$1-$2', str_replace( '_', '', $class ) ) );
  66. if ( strpos( $class, 'vamtam-' ) === 0 ) {
  67. $path = trailingslashit( get_template_directory() ) . 'vamtam/classes/';
  68. $file = str_replace( 'vamtam-', '', $class ) . '.php';
  69. if ( is_readable( $path . $file ) ) {
  70. include_once( $path . $file );
  71. return;
  72. }
  73. if ( is_admin() ) {
  74. $admin_path = VAMTAM_ADMIN_DIR . 'classes/';
  75. if ( is_readable( $admin_path . $file ) ) {
  76. include_once( $admin_path . $file );
  77. return;
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * Sets self::$cache[ $key ] = $value
  84. *
  85. * @param mixed $key
  86. * @param mixed $value
  87. */
  88. public static function set( $key, $value ) {
  89. self::$cache[ $key ] = $value;
  90. }
  91. /**
  92. * Returns self::$cache[ $key ]
  93. *
  94. * @param mixed $key
  95. * @return mixed value
  96. */
  97. public static function get( $key, $default = false ) {
  98. return isset( self::$cache[ $key ] ) ? self::$cache[ $key ] : $default;
  99. }
  100. /**
  101. * Get the theme version
  102. *
  103. * @return string theme version as defined in style.css
  104. */
  105. public static function get_version() {
  106. if ( isset( self::$cache['version'] ) )
  107. return self::$cache['version'];
  108. $the_theme = wp_get_theme();
  109. if ( $the_theme->parent() ) {
  110. $the_theme = $the_theme->parent();
  111. }
  112. self::$cache['version'] = $the_theme->get( 'Version' );
  113. return self::$cache['version'];
  114. }
  115. /**
  116. * Defines constants used by the theme
  117. *
  118. * @param array $options framework options
  119. */
  120. private function set_constants( $options ) {
  121. define( 'VAMTAM_THEME_NAME', $options['name'] );
  122. define( 'VAMTAM_THEME_SLUG', $options['slug'] );
  123. define( 'VAMTAM_THUMBNAIL_PREFIX', 'theme-' );
  124. // theme dir and uri
  125. define( 'VAMTAM_THEME_DIR', get_template_directory() . '/' );
  126. define( 'VAMTAM_THEME_URI', get_template_directory_uri() . '/' );
  127. // framework dir and uri
  128. define( 'VAMTAM_DIR', VAMTAM_THEME_DIR . 'vamtam/' );
  129. define( 'VAMTAM_URI', VAMTAM_THEME_URI . 'vamtam/' );
  130. // common assets dir and uri
  131. define( 'VAMTAM_ASSETS_DIR', VAMTAM_DIR . 'assets/' );
  132. define( 'VAMTAM_ASSETS_URI', VAMTAM_URI . 'assets/' );
  133. // common file paths
  134. define( 'VAMTAM_FONTS_URI', VAMTAM_ASSETS_URI . 'fonts/' );
  135. define( 'VAMTAM_HELPERS', VAMTAM_DIR . 'helpers/' );
  136. define( 'VAMTAM_JS', VAMTAM_ASSETS_URI . 'js/' );
  137. define( 'VAMTAM_METABOXES', VAMTAM_DIR . 'metaboxes/' );
  138. define( 'VAMTAM_OPTIONS', VAMTAM_DIR . 'options/' );
  139. define( 'VAMTAM_PLUGINS', VAMTAM_DIR . 'plugins/' );
  140. define( 'VAMTAM_CSS', VAMTAM_ASSETS_URI . 'css/' );
  141. define( 'VAMTAM_CSS_DIR', VAMTAM_ASSETS_DIR . 'css/' );
  142. define( 'VAMTAM_IMAGES', VAMTAM_ASSETS_URI . 'images/' );
  143. define( 'VAMTAM_IMAGES_DIR', VAMTAM_ASSETS_DIR . 'images/' );
  144. // sample content
  145. define( 'VAMTAM_SAMPLES_DIR', VAMTAM_THEME_DIR . 'samples/' );
  146. define( 'VAMTAM_SAMPLES_URI', VAMTAM_THEME_URI . 'samples/' );
  147. // cache
  148. define( 'VAMTAM_CACHE_DIR', VAMTAM_THEME_DIR . 'cache/' );
  149. define( 'VAMTAM_CACHE_URI', VAMTAM_THEME_URI . 'cache/' );
  150. // admin
  151. define( 'VAMTAM_ADMIN_DIR', VAMTAM_DIR . 'admin/' );
  152. define( 'VAMTAM_ADMIN_URI', VAMTAM_URI . 'admin/' );
  153. define( 'VAMTAM_ADMIN_AJAX', VAMTAM_ADMIN_URI . 'ajax/' );
  154. define( 'VAMTAM_ADMIN_AJAX_DIR', VAMTAM_ADMIN_DIR . 'ajax/' );
  155. define( 'VAMTAM_ADMIN_ASSETS_URI', VAMTAM_ADMIN_URI . 'assets/' );
  156. define( 'VAMTAM_ADMIN_HELPERS', VAMTAM_ADMIN_DIR . 'helpers/' );
  157. define( 'VAMTAM_ADMIN_CGEN', VAMTAM_ADMIN_HELPERS . 'config_generator/' );
  158. define( 'VAMTAM_ADMIN_METABOXES', VAMTAM_ADMIN_DIR . 'metaboxes/' );
  159. define( 'VAMTAM_ADMIN_TEMPLATES', VAMTAM_ADMIN_DIR . 'templates/' );
  160. }
  161. /**
  162. * 'init' action, but with a higher (later) priority
  163. */
  164. public static function late_init() {
  165. if ( class_exists( 'Jetpack_Portfolio' ) ) {
  166. $GLOBALS['_wp_additional_image_sizes']['jetpack-portfolio-admin-thumb'] = array(
  167. 'width' => 100,
  168. 'height' => 100,
  169. 'crop' => true,
  170. );
  171. }
  172. }
  173. /**
  174. * Register theme support for various features
  175. */
  176. public static function theme_supports() {
  177. global $content_width;
  178. self::set( 'is_responsive', apply_filters( 'vamtam-theme-responsive-mode', true ) );
  179. /**
  180. * the max content width the css is built for should equal the actual content width,
  181. * for example, the width of the text of a page without sidebars
  182. */
  183. if ( ! isset( $content_width ) ) $content_width = rd_vamtam_get_option( 'site-max-width' );
  184. if ( is_customize_preview() ) {
  185. $content_width = 1400;
  186. }
  187. $post_formats = apply_filters( 'vamtam_post_formats', array( 'aside', 'link', 'image', 'video', 'audio', 'quote', 'gallery' ) );
  188. self::set( 'post_formats', $post_formats );
  189. add_theme_support( 'post-thumbnails' );
  190. add_theme_support( 'automatic-feed-links' );
  191. add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) );
  192. add_theme_support( 'post-formats', $post_formats );
  193. add_theme_support( 'title-tag' );
  194. add_theme_support( 'vamtam-ajax-siblings' );
  195. add_theme_support( 'vamtam-page-title-style' );
  196. add_theme_support( 'vamtam-tribe-events' );
  197. add_theme_support( 'vamtam-scroll-pinning' );
  198. add_theme_support( 'customize-selective-refresh-widgets' );
  199. add_theme_support( 'fl-theme-builder-headers' );
  200. add_theme_support( 'fl-theme-builder-footers' );
  201. add_theme_support( 'woocommerce', array(
  202. 'thumbnail_image_width' => $content_width / 4,
  203. 'single_image_width' => $content_width / 2,
  204. ) );
  205. if ( class_exists( 'Jetpack_Portfolio' ) ) {
  206. add_post_type_support( Jetpack_Portfolio::CUSTOM_POST_TYPE, 'excerpt' );
  207. }
  208. if ( function_exists( 'register_nav_menus' ) ) {
  209. register_nav_menus(
  210. array(
  211. 'menu-header' => esc_html__( 'Menu Header', 'vamtam-consulting' ),
  212. 'menu-top' => esc_html__( 'Menu Top', 'vamtam-consulting' ),
  213. 'overlay-menu' => esc_html__( 'Overlay Menu', 'vamtam-consulting' ),
  214. )
  215. );
  216. }
  217. $size_info = array();
  218. $wth = wp_parse_args( get_option( 'vamtam_featured_images_ratio', array() ), array(
  219. VAMTAM_THUMBNAIL_PREFIX . 'loop' => 1.3,
  220. VAMTAM_THUMBNAIL_PREFIX . 'single' => 1.3,
  221. ) );
  222. foreach ( $wth as $name => $ratio ) {
  223. $size_info[ $name ] = (object) array(
  224. 'wth' => abs( floatval( $wth[ $name ] ) ),
  225. 'crop' => true,
  226. );
  227. }
  228. $width = $content_width;
  229. $single_sizes = array( VAMTAM_THUMBNAIL_PREFIX . 'single' );
  230. $columnated_sizes = array( VAMTAM_THUMBNAIL_PREFIX . 'loop' );
  231. foreach ( $single_sizes as $name ) {
  232. $height = $size_info[ $name ]->wth ? $width / $size_info[ $name ]->wth : false;
  233. add_image_size( $name, $width, $height, $size_info[ $name ]->crop );
  234. }
  235. for ( $num_columns = 1; $num_columns <= 4; $num_columns++ ) {
  236. $col_width = $width / $num_columns;
  237. add_image_size( VAMTAM_THUMBNAIL_PREFIX . 'normal-' . $num_columns, $col_width, 0 ); // special case where we always use the original proportions
  238. if ( $num_columns > 1 ) {
  239. add_image_size( VAMTAM_THUMBNAIL_PREFIX . 'normal-featured-' . $num_columns, $col_width * 2, 0 ); // same, but double width
  240. }
  241. foreach ( $columnated_sizes as $name ) {
  242. $height = $size_info[ $name ]->wth ? $col_width / $size_info[ $name ]->wth : false;
  243. add_image_size( $name . '-' . $num_columns, $col_width, $height, $size_info[ $name ]->crop );
  244. if ( $num_columns > 1 ) {
  245. add_image_size( $name . '-featured-' . $num_columns, $col_width * 2, $height * 2, $size_info[ $name ]->crop );
  246. }
  247. }
  248. }
  249. }
  250. /**
  251. * Load interface translations
  252. */
  253. private function load_languages() {
  254. load_theme_textdomain( 'vamtam-consulting', VAMTAM_THEME_DIR . 'languages' );
  255. }
  256. /**
  257. * Loads the main php files used by the framework
  258. */
  259. private function load_functions() {
  260. global $vamtam_defaults, $vamtam_fonts;
  261. $vamtam_defaults = include VAMTAM_SAMPLES_DIR . 'default-options.php';
  262. $vamtam_fonts = include VAMTAM_HELPERS . 'fonts.php';
  263. require_once VAMTAM_HELPERS . 'init.php';
  264. $custom_fonts = get_option( 'vamtam_custom_font_families', '' );
  265. if ( ! empty( $custom_fonts ) ) {
  266. $custom_fonts = explode( "\n", $custom_fonts );
  267. $vamtam_fonts['-- Custom fonts --'] = array(
  268. 'family' => '',
  269. );
  270. foreach ( $custom_fonts as $font ) {
  271. $font = preg_replace( '/["\']+/', '', trim( $font ) );
  272. $vamtam_fonts[ $font ] = array(
  273. 'family' => '"' . $font . '"',
  274. 'weights' => array( '300', '300 italic', 'normal', 'italic', '600', '600 italic', 'bold', 'bold italic', '800', '800 italic' ),
  275. );
  276. }
  277. }
  278. require_once VAMTAM_HELPERS . 'woocommerce-integration.php';
  279. require_once VAMTAM_HELPERS . 'megamenu-integration.php';
  280. require_once VAMTAM_HELPERS . 'icons.php';
  281. require_once VAMTAM_HELPERS . 'file.php';
  282. VamtamFormatFilter::actions();
  283. require_once VAMTAM_HELPERS . 'base.php';
  284. require_once VAMTAM_HELPERS . 'template.php';
  285. require_once VAMTAM_HELPERS . 'css.php';
  286. VamtamOverrides::filters();
  287. VamtamEnqueues::actions();
  288. if ( file_exists( VAMTAM_HELPERS . 'migrations.php' ) ) {
  289. require_once VAMTAM_HELPERS . 'migrations.php';
  290. }
  291. }
  292. /**
  293. * Load widgets
  294. */
  295. public static function load_widgets() {
  296. $vamtam_sidebars = VamtamSidebars::get_instance();
  297. $vamtam_sidebars->register_sidebars();
  298. $widgets = apply_filters( 'vamtam-enabled-widgets', array(
  299. 'beaver',
  300. ) );
  301. foreach ( $widgets as $name ) {
  302. require_once VAMTAM_DIR . "widgets/$name.php";
  303. }
  304. }
  305. /**
  306. * Loads the theme administration code
  307. */
  308. private function load_admin() {
  309. if ( ! is_admin() ) return;
  310. VamtamAdmin::actions();
  311. }
  312. public static function get_purchase_code() {
  313. return get_option( 'vamtam-envato-license-key' );
  314. }
  315. }