PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/storefront/inc/class-storefront.php

https://gitlab.com/hunt9310/ras
PHP | 401 lines | 209 code | 62 blank | 130 comment | 18 complexity | cbce80ba60855a3533977ac91cbbfeb2 MD5 | raw file
  1. <?php
  2. /**
  3. * Storefront Class
  4. *
  5. * @author WooThemes
  6. * @since 2.0.0
  7. * @package storefront
  8. */
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. if ( ! class_exists( 'Storefront' ) ) :
  13. /**
  14. * The main Storefront class
  15. */
  16. class Storefront {
  17. private static $structured_data;
  18. /**
  19. * Setup class.
  20. *
  21. * @since 1.0
  22. */
  23. public function __construct() {
  24. add_action( 'after_setup_theme', array( $this, 'setup' ) );
  25. add_action( 'widgets_init', array( $this, 'widgets_init' ) );
  26. add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ), 10 );
  27. add_action( 'wp_enqueue_scripts', array( $this, 'child_scripts' ), 30 ); // After WooCommerce.
  28. add_filter( 'body_class', array( $this, 'body_classes' ) );
  29. add_filter( 'wp_page_menu_args', array( $this, 'page_menu_args' ) );
  30. add_filter( 'navigation_markup_template', array( $this, 'navigation_markup_template' ) );
  31. add_action( 'enqueue_embed_scripts', array( $this, 'print_embed_styles' ) );
  32. add_action( 'wp_footer', array( $this, 'get_structured_data' ) );
  33. }
  34. /**
  35. * Sets up theme defaults and registers support for various WordPress features.
  36. *
  37. * Note that this function is hooked into the after_setup_theme hook, which
  38. * runs before the init hook. The init hook is too late for some features, such
  39. * as indicating support for post thumbnails.
  40. */
  41. public function setup() {
  42. /*
  43. * Load Localisation files.
  44. *
  45. * Note: the first-loaded translation file overrides any following ones if the same translation is present.
  46. */
  47. // Loads wp-content/languages/themes/storefront-it_IT.mo.
  48. load_theme_textdomain( 'storefront', trailingslashit( WP_LANG_DIR ) . 'themes/' );
  49. // Loads wp-content/themes/child-theme-name/languages/it_IT.mo.
  50. load_theme_textdomain( 'storefront', get_stylesheet_directory() . '/languages' );
  51. // Loads wp-content/themes/storefront/languages/it_IT.mo.
  52. load_theme_textdomain( 'storefront', get_template_directory() . '/languages' );
  53. /**
  54. * Add default posts and comments RSS feed links to head.
  55. */
  56. add_theme_support( 'automatic-feed-links' );
  57. /*
  58. * Enable support for Post Thumbnails on posts and pages.
  59. *
  60. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  61. */
  62. add_theme_support( 'post-thumbnails' );
  63. /**
  64. * Enable support for site logo
  65. */
  66. add_theme_support( 'custom-logo', array(
  67. 'height' => 110,
  68. 'width' => 470,
  69. 'flex-width' => true,
  70. ) );
  71. // This theme uses wp_nav_menu() in two locations.
  72. register_nav_menus( array(
  73. 'primary' => __( 'Primary Menu', 'storefront' ),
  74. 'secondary' => __( 'Secondary Menu', 'storefront' ),
  75. 'handheld' => __( 'Handheld Menu', 'storefront' ),
  76. ) );
  77. /*
  78. * Switch default core markup for search form, comment form, comments, galleries, captions and widgets
  79. * to output valid HTML5.
  80. */
  81. add_theme_support( 'html5', array(
  82. 'search-form',
  83. 'comment-form',
  84. 'comment-list',
  85. 'gallery',
  86. 'caption',
  87. 'widgets',
  88. ) );
  89. // Setup the WordPress core custom background feature.
  90. add_theme_support( 'custom-background', apply_filters( 'storefront_custom_background_args', array(
  91. 'default-color' => apply_filters( 'storefront_default_background_color', 'ffffff' ),
  92. 'default-image' => '',
  93. ) ) );
  94. /**
  95. * Add support for the Site Logo plugin and the site logo functionality in JetPack
  96. * https://github.com/automattic/site-logo
  97. * http://jetpack.me/
  98. */
  99. add_theme_support( 'site-logo', array( 'size' => 'full' ) );
  100. // Declare WooCommerce support.
  101. add_theme_support( 'woocommerce' );
  102. // Declare support for title theme feature.
  103. add_theme_support( 'title-tag' );
  104. // Declare support for selective refreshing of widgets.
  105. add_theme_support( 'customize-selective-refresh-widgets' );
  106. }
  107. /**
  108. * Register widget area.
  109. *
  110. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  111. */
  112. public function widgets_init() {
  113. $sidebar_args['header'] = array(
  114. 'name' => __( 'Below Header', 'storefront' ),
  115. 'id' => 'header-1',
  116. 'description' => __( 'Widgets added to this region will appear beneath the header and above the main content.', 'storefront' ),
  117. );
  118. $sidebar_args['sidebar'] = array(
  119. 'name' => __( 'Sidebar', 'storefront' ),
  120. 'id' => 'sidebar-1',
  121. 'description' => ''
  122. );
  123. $footer_widget_regions = apply_filters( 'storefront_footer_widget_regions', 4 );
  124. for ( $i = 1; $i <= intval( $footer_widget_regions ); $i++ ) {
  125. $footer = sprintf( 'footer_%d', $i );
  126. $sidebar_args[ $footer ] = array(
  127. 'name' => sprintf( __( 'Footer %d', 'storefront' ), $i ),
  128. 'id' => sprintf( 'footer-%d', $i ),
  129. 'description' => sprintf( __( 'Widgetized Footer Region %d.', 'storefront' ), $i )
  130. );
  131. }
  132. foreach ( $sidebar_args as $sidebar => $args ) {
  133. $widget_tags = array(
  134. 'before_widget' => '<div id="%1$s" class="widget %2$s">',
  135. 'after_widget' => '</div>',
  136. 'before_title' => '<span class="gamma widget-title">',
  137. 'after_title' => '</span>'
  138. );
  139. /**
  140. * Dynamically generated filter hooks. Allow changing widget wrapper and title tags. See the list below.
  141. *
  142. * 'storefront_header_widget_tags'
  143. * 'storefront_sidebar_widget_tags'
  144. *
  145. * storefront_footer_1_widget_tags
  146. * storefront_footer_2_widget_tags
  147. * storefront_footer_3_widget_tags
  148. * storefront_footer_4_widget_tags
  149. */
  150. $filter_hook = sprintf( 'storefront_%s_widget_tags', $sidebar );
  151. $widget_tags = apply_filters( $filter_hook, $widget_tags );
  152. if ( is_array( $widget_tags ) ) {
  153. register_sidebar( $args + $widget_tags );
  154. }
  155. }
  156. }
  157. /**
  158. * Enqueue scripts and styles.
  159. *
  160. * @since 1.0.0
  161. */
  162. public function scripts() {
  163. global $storefront_version;
  164. /**
  165. * Styles
  166. */
  167. wp_enqueue_style( 'storefront-style', get_template_directory_uri() . '/style.css', '', $storefront_version );
  168. wp_style_add_data( 'storefront-style', 'rtl', 'replace' );
  169. /**
  170. * Fonts
  171. */
  172. $google_fonts = apply_filters( 'storefront_google_font_families', array(
  173. 'source-sans-pro' => 'Source+Sans+Pro:400,300,300italic,400italic,700,900',
  174. ) );
  175. $query_args = array(
  176. 'family' => implode( '|', $google_fonts ),
  177. 'subset' => urlencode( 'latin,latin-ext' ),
  178. );
  179. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  180. wp_enqueue_style( 'storefront-fonts', $fonts_url, array(), null );
  181. /**
  182. * Scripts
  183. */
  184. wp_enqueue_script( 'storefront-navigation', get_template_directory_uri() . '/assets/js/navigation.min.js', array( 'jquery' ), '20120206', true );
  185. wp_enqueue_script( 'storefront-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.min.js', array(), '20130115', true );
  186. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  187. wp_enqueue_script( 'comment-reply' );
  188. }
  189. }
  190. /**
  191. * Enqueue child theme stylesheet.
  192. * A separate function is required as the child theme css needs to be enqueued _after_ the parent theme
  193. * primary css and the separate WooCommerce css.
  194. *
  195. * @since 1.5.3
  196. */
  197. public function child_scripts() {
  198. if ( is_child_theme() ) {
  199. wp_enqueue_style( 'storefront-child-style', get_stylesheet_uri(), '' );
  200. }
  201. }
  202. /**
  203. * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
  204. *
  205. * @param array $args Configuration arguments.
  206. * @return array
  207. */
  208. public function page_menu_args( $args ) {
  209. $args['show_home'] = true;
  210. return $args;
  211. }
  212. /**
  213. * Adds custom classes to the array of body classes.
  214. *
  215. * @param array $classes Classes for the body element.
  216. * @return array
  217. */
  218. public function body_classes( $classes ) {
  219. // Adds a class of group-blog to blogs with more than 1 published author.
  220. if ( is_multi_author() ) {
  221. $classes[] = 'group-blog';
  222. }
  223. if ( ! function_exists( 'woocommerce_breadcrumb' ) ) {
  224. $classes[] = 'no-wc-breadcrumb';
  225. }
  226. /**
  227. * What is this?!
  228. * Take the blue pill, close this file and forget you saw the following code.
  229. * Or take the red pill, filter storefront_make_me_cute and see how deep the rabbit hole goes...
  230. */
  231. $cute = apply_filters( 'storefront_make_me_cute', false );
  232. if ( true === $cute ) {
  233. $classes[] = 'storefront-cute';
  234. }
  235. // If our main sidebar doesn't contain widgets, adjust the layout to be full-width.
  236. if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  237. $classes[] = 'storefront-full-width-content';
  238. }
  239. return $classes;
  240. }
  241. /**
  242. * Custom navigation markup template hooked into `navigation_markup_template` filter hook.
  243. */
  244. public function navigation_markup_template() {
  245. $template = '<nav id="post-navigation" class="navigation %1$s" role="navigation" aria-label="Post Navigation">';
  246. $template .= '<span class="screen-reader-text">%2$s</span>';
  247. $template .= '<div class="nav-links">%3$s</div>';
  248. $template .= '</nav>';
  249. return apply_filters( 'storefront_navigation_markup_template', $template );
  250. }
  251. /**
  252. * Add styles for embeds
  253. */
  254. public function print_embed_styles() {
  255. wp_enqueue_style( 'source-sans-pro', '//fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,300italic,400italic,700,900' );
  256. $accent_color = get_theme_mod( 'storefront_accent_color' );
  257. $background_color = storefront_get_content_background_color();
  258. ?>
  259. <style type="text/css">
  260. .wp-embed {
  261. padding: 2.618em !important;
  262. border: 0 !important;
  263. border-radius: 3px !important;
  264. font-family: "Source Sans Pro", "Open Sans", sans-serif !important;
  265. -webkit-font-smoothing: antialiased;
  266. background-color: <?php echo storefront_adjust_color_brightness( $background_color, -7 ); ?> !important;
  267. }
  268. .wp-embed .wp-embed-featured-image {
  269. margin-bottom: 2.618em;
  270. }
  271. .wp-embed .wp-embed-featured-image img,
  272. .wp-embed .wp-embed-featured-image.square {
  273. min-width: 100%;
  274. margin-bottom: .618em;
  275. }
  276. a.wc-embed-button {
  277. padding: .857em 1.387em !important;
  278. font-weight: 600;
  279. background-color: <?php echo esc_attr( $accent_color ); ?>;
  280. color: #fff !important;
  281. border: 0 !important;
  282. line-height: 1;
  283. border-radius: 0 !important;
  284. box-shadow:
  285. inset 0 -1px 0 rgba(#000,.3);
  286. }
  287. a.wc-embed-button + a.wc-embed-button {
  288. background-color: #60646c;
  289. }
  290. </style>
  291. <?php
  292. }
  293. /**
  294. * Check if the passed $json variable is an array and store it into the property...
  295. */
  296. public static function set_structured_data( $json ) {
  297. if ( ! is_array( $json ) ) {
  298. return;
  299. }
  300. self::$structured_data[] = $json;
  301. }
  302. /**
  303. * If self::$structured_data is set, wrap and echo it...
  304. * Hooked into the `wp_footer` action.
  305. */
  306. public function get_structured_data() {
  307. if ( ! self::$structured_data ) {
  308. return;
  309. }
  310. $structured_data['@context'] = 'http://schema.org/';
  311. if ( count( self::$structured_data ) > 1 ) {
  312. $structured_data['@graph'] = self::$structured_data;
  313. } else {
  314. $structured_data = $structured_data + self::$structured_data[0];
  315. }
  316. $structured_data = $this->sanitize_structured_data( $structured_data );
  317. echo '<script type="application/ld+json">' . wp_json_encode( $structured_data ) . '</script>';
  318. }
  319. /**
  320. * Sanitize structured data.
  321. *
  322. * @param array $data
  323. * @return array
  324. */
  325. public function sanitize_structured_data( $data ) {
  326. $sanitized = array();
  327. foreach ( $data as $key => $value ) {
  328. if ( is_array( $value ) ) {
  329. $sanitized_value = $this->sanitize_structured_data( $value );
  330. } else {
  331. $sanitized_value = sanitize_text_field( $value );
  332. }
  333. $sanitized[ sanitize_text_field( $key ) ] = $sanitized_value;
  334. }
  335. return $sanitized;
  336. }
  337. }
  338. endif;
  339. return new Storefront();