PageRenderTime 2337ms CodeModel.GetById 61ms RepoModel.GetById 111ms app.codeStats 0ms

/src/engine/includes/base.php

https://github.com/PressCrew/infinity
PHP | 562 lines | 332 code | 64 blank | 166 comment | 33 complexity | 416561d0e6e3c7cd42f069adfbe52e7c MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Infinity Theme: base
  4. *
  5. * @author Bowe Frankema <bowe@presscrew.com>
  6. * @link http://infinity.presscrew.com/
  7. * @copyright Copyright (C) 2010-2013 Bowe Frankema
  8. * @license http://www.gnu.org/licenses/gpl.html GPLv2 or later
  9. * @package Infinity
  10. * @subpackage base
  11. * @since 1.1
  12. */
  13. /**
  14. * Add support for base features.
  15. */
  16. function infinity_base_features()
  17. {
  18. // WordPress Support
  19. add_theme_support( 'title-tag' );
  20. add_theme_support( 'menus' );
  21. add_theme_support( 'post-thumbnails' );
  22. // WordPress Setup
  23. add_theme_support( 'infinity:post', 'intro-boxes', 'avatars', 'author-boxes', 'pagination' );
  24. add_theme_support( 'infinity:wp', 'support', 'style' );
  25. add_theme_support( 'infinity:base', 'script' );
  26. // Fonts
  27. add_theme_support( 'infinity:font', 'iconsweets' );
  28. // Responsive Support
  29. add_theme_support( 'infinity:resp', 'support', 'menu', 'videos' );
  30. // Extension Layouts
  31. add_theme_support( 'infinity:ext', 'support' );
  32. add_theme_support( 'infinity:typog', 'style' );
  33. add_theme_support( 'infinity:layout', 'style' );
  34. add_theme_support( 'infinity:buttons', 'style', 'color' );
  35. add_theme_support( 'infinity:design', 'style' );
  36. add_theme_support( 'infinity:icons', 'style' );
  37. // Extension Options
  38. add_theme_support( 'infinity:header', 'layout' );
  39. add_theme_support( 'infinity:header-logo', 'support' );
  40. add_theme_support( 'infinity:sidebar', 'setup', 'layout' );
  41. add_theme_support( 'infinity:top-menu', 'setup', 'layout' );
  42. add_theme_support( 'infinity:main-menu', 'setup', 'layout' );
  43. add_theme_support( 'infinity:sub-menu', 'setup', 'layout' );
  44. add_theme_support( 'infinity:footer-menu', 'setup' );
  45. add_theme_support( 'infinity:core', 'options' );
  46. add_theme_support( 'infinity:body', 'layout' );
  47. add_theme_support( 'infinity:content', 'layout' );
  48. add_theme_support( 'infinity:footer', 'layout' );
  49. add_theme_support( 'infinity:widget', 'layout' );
  50. add_theme_support( 'infinity:custom', 'css' );
  51. add_theme_support( 'infinity:slider', 'custom', 'category' );
  52. // BuddyPress
  53. add_theme_support( 'infinity:bp', 'support', 'style', 'sidebar-setup' );
  54. add_theme_support( 'infinity:bp-fbconnect', 'support' );
  55. add_theme_support( 'infinity:bp-protect', 'support' );
  56. add_theme_support( 'infinity:bp-tour', 'activity' );
  57. // Other plugins
  58. add_theme_support( 'infinity:plugins', 'other' );
  59. }
  60. add_action( 'after_setup_theme', 'infinity_base_features' );
  61. // register base assets
  62. function infinity_base_assets()
  63. {
  64. // style.css
  65. ice_register_style(
  66. 'infinity-style',
  67. array(
  68. 'src' => get_stylesheet_uri(),
  69. 'priority' => 99999,
  70. 'condition' => 'is_not_admin'
  71. )
  72. );
  73. // superfish script
  74. ice_register_script(
  75. 'superfish',
  76. array(
  77. 'src' => INFINITY_THEME_URL . '/assets/js/superfish.js',
  78. 'in_footer' => true,
  79. 'condition' => 'is_not_admin'
  80. )
  81. );
  82. }
  83. add_action( 'after_setup_theme', 'infinity_base_assets' );
  84. // load infinity text domain
  85. function infinity_base_textdomain()
  86. {
  87. load_theme_textdomain( 'infinity-engine', INFINITY_LANGUAGES_PATH );
  88. }
  89. add_action( 'after_setup_theme', 'infinity_base_textdomain' );
  90. // add post formats
  91. function infinity_base_post_formats()
  92. {
  93. add_theme_support(
  94. 'post-formats',
  95. array(
  96. 'aside',
  97. 'audio',
  98. 'chat',
  99. 'gallery',
  100. 'image',
  101. 'link',
  102. 'quote',
  103. 'status',
  104. 'video'
  105. )
  106. );
  107. }
  108. add_action( 'after_setup_theme', 'infinity_base_post_formats' );
  109. /**
  110. * Set the content width based on the theme's design and stylesheet.
  111. * Used to set the width of images and content. Should be equal to the
  112. * width the theme is designed for, generally via the style.css stylesheet.
  113. */
  114. function infinity_base_set_content_width()
  115. {
  116. global $content_width;
  117. if ( false === isset( $content_width ) ) {
  118. $content_width = 760;
  119. add_theme_support( 'automatic-feed-links' );
  120. }
  121. }
  122. add_action( 'after_setup_theme', 'infinity_base_set_content_width' );
  123. /**
  124. * Render a <title> tag if current version of WordPress doesn't support the title-tag feature.
  125. */
  126. function infinity_base_title()
  127. {
  128. // determine if we need to inject a title tag into head ourselves
  129. if (
  130. // is title-tag feature support missing from running version of WP?
  131. false === function_exists( '_wp_render_title_tag' )
  132. ) {
  133. // no native support, render a title tag
  134. ?>
  135. <title><?php wp_title( '|', true, 'right' ); ?></title>
  136. <?php
  137. }
  138. }
  139. add_filter( 'wp_head', 'infinity_base_title', 1 );
  140. /**
  141. * For WordPress versions older than 4.1, filter contents of
  142. * the <title> tag based on what is being viewed.
  143. *
  144. * @uses infinity_plugin_supported()
  145. *
  146. * @global int $page
  147. * @global int $paged
  148. *
  149. * @param string $title Incoming title string.
  150. * @param string $sep Separator string.
  151. * @param string $seplocation Separator location (left or right).
  152. */
  153. function infinity_base_title_filter( $title, $sep, $seplocation )
  154. {
  155. global $page, $paged;
  156. // should we filter the title?
  157. if (
  158. // is title-tag support missing from the running copy of WordPress?
  159. false === function_exists( '_wp_render_title_tag' ) &&
  160. // is the current screen NOT a feed?
  161. false === is_feed() &&
  162. // is WordPress SEO plugin NOT supported?
  163. false === infinity_plugin_supported( 'wordpress-seo' )
  164. ) {
  165. // we are formatting our own custom title string
  166. // first, append the blog name.
  167. $title .= get_bloginfo( 'name' );
  168. // now try to get the blog description
  169. $site_description = get_bloginfo( 'description', 'display' );
  170. // did we get a site desc AND on home/front page?
  171. if (
  172. false === empty( $site_description ) &&
  173. true === (
  174. true === is_home() ||
  175. true === is_front_page()
  176. )
  177. ) {
  178. // yes, append it (including separator)
  179. $title .= sprintf( ' %s %s', $sep, $site_description );
  180. }
  181. // last thing... do we have a page number?
  182. if ( $paged >= 2 || $page >= 2 ) {
  183. // yes, append it (including separator)
  184. $title .=
  185. sprintf( ' %s ', $sep ) .
  186. sprintf( __( 'Page %s', 'infinity-engine' ), max( $paged, $page ) );
  187. }
  188. }
  189. // return title
  190. return $title;
  191. }
  192. add_filter( 'wp_title', 'infinity_base_title_filter', 1, 3 );
  193. /**
  194. * Add special "admin bar is showing" body class
  195. */
  196. function infinity_base_admin_bar_class( $classes )
  197. {
  198. if ( is_admin_bar_showing() ) {
  199. // *append* class to the array
  200. $classes[] = 'admin-bar-showing';
  201. }
  202. // return it!
  203. return $classes;
  204. }
  205. add_filter( 'body_class', 'infinity_base_admin_bar_class' );
  206. /**
  207. * Setup post thumbnail sizes
  208. *
  209. * @package Infinity
  210. * @subpackage base
  211. */
  212. function infinity_base_post_thumb_sizes()
  213. {
  214. if (
  215. current_theme_supports( 'post-thumbnails' )
  216. ) {
  217. set_post_thumbnail_size( 35, 35, true );
  218. add_image_size( 'post-image', 674, 140, true );
  219. add_image_size( 'thumbnail-large', 600, 200, true );
  220. add_image_size( 'thumbnail-post', 210, 160, true );
  221. }
  222. }
  223. add_action( 'after_setup_theme', 'infinity_base_post_thumb_sizes', 20 );
  224. /**
  225. * Enqueue Comment Script
  226. *
  227. * @package Infinity
  228. * @subpackage base
  229. */
  230. function infinity_enqueue_comments_reply()
  231. {
  232. if( get_option( 'thread_comments' ) ) {
  233. wp_enqueue_script( 'comment-reply' );
  234. }
  235. }
  236. add_action( 'comment_form_before', 'infinity_enqueue_comments_reply' );
  237. /**
  238. * Register menus
  239. *
  240. * @package Infinity
  241. * @subpackage base
  242. */
  243. function infinity_base_register_menus()
  244. {
  245. if ( current_theme_supports( 'infinity:top-menu', 'setup' ) ) {
  246. register_nav_menu( 'over-menu', __( 'Above Header', 'infinity-engine' ) );
  247. }
  248. if ( current_theme_supports( 'infinity:main-menu', 'setup' ) ) {
  249. register_nav_menu( 'main-menu', __( 'Inside Header', 'infinity-engine' ) );
  250. }
  251. if ( current_theme_supports( 'infinity:sub-menu', 'setup' ) ) {
  252. register_nav_menu( 'sub-menu', __( 'Below Header', 'infinity-engine' ) );
  253. }
  254. if ( current_theme_supports( 'infinity:footer-menu', 'setup' ) ) {
  255. register_nav_menu( 'footer-menu', __( 'Inside Footer', 'infinity-engine' ) );
  256. }
  257. }
  258. add_action( 'init', 'infinity_base_register_menus' );
  259. /**
  260. * Display a nav menu using a custom walker
  261. *
  262. * @author Marshall Sorenson <marshall@presscrew.com>
  263. * @package Infinity
  264. * @subpackage base
  265. * @see wp_nav_menu()
  266. * @param string $theme_location Theme location, 'theme_location' arg passed to wp_nav_menu()\
  267. */
  268. function infinity_base_nav_menu( $theme_location )
  269. {
  270. wp_nav_menu( array(
  271. 'theme_location' => $theme_location,
  272. 'menu_class' => 'sf-menu',
  273. 'container' => '',
  274. 'fallback_cb' => 'infinity_base_page_menu',
  275. 'walker' => new Infinity_Base_Walker_Nav_Menu()
  276. ));
  277. }
  278. /**
  279. * Display a page menu using a custom page walker
  280. *
  281. * @author Marshall Sorenson <marshall@presscrew.com>
  282. * @package Infinity
  283. * @subpackage base
  284. * @see wp_list_pages()
  285. */
  286. function infinity_base_page_menu()
  287. {
  288. // open the list ?>
  289. <ul class="sf-menu"><?php
  290. wp_list_pages(array(
  291. 'title_li' => null,
  292. 'walker' => new Infinity_Base_Walker_Page_Menu()
  293. ));
  294. // close list?>
  295. </ul><?php
  296. }
  297. /**
  298. * Render a single superfish list item
  299. *
  300. * @param array $args
  301. * @param boolean $output
  302. * @return void|string
  303. */
  304. function infinity_base_superfish_list_item( $args, $output = true )
  305. {
  306. // default values
  307. $defaults = array(
  308. 'id' => null,
  309. 'title' => null,
  310. 'close_item' => true,
  311. 'li_id' => null,
  312. 'li_classes' => array(),
  313. 'a_id' => null,
  314. 'a_classes' => array(),
  315. 'a_target' => null,
  316. 'a_rel' => null,
  317. 'a_href' => null,
  318. 'a_before' => null,
  319. 'a_after' => null,
  320. 'a_open' => null,
  321. 'a_close' => null
  322. );
  323. // parse and extract
  324. $r = wp_parse_args( $args, $defaults );
  325. extract( $r );
  326. // handle empty list id
  327. if ( empty( $li_id ) ) {
  328. $li_id = 'menu-item-' . $id;
  329. }
  330. // list attributes
  331. $attr_li[] = sprintf( 'id="%s"', esc_attr( $li_id ) );
  332. if ( count( $li_classes ) ) {
  333. $attr_li[] = sprintf( 'class="%s"', esc_attr( implode( ' ', $li_classes ) ) );
  334. }
  335. // anchor attributes
  336. $attr_anchor = array();
  337. if ( $a_id ) {
  338. $attr_anchor[] = sprintf( 'id="%s"', esc_attr( $a_id ) );
  339. }
  340. if ( count( $a_classes ) ) {
  341. $attr_anchor[] = sprintf( 'class="%s"', esc_attr( implode( ' ', $a_classes ) ) );
  342. }
  343. if ( $a_href ) {
  344. $attr_anchor[] = sprintf( 'href="%s"', esc_attr( $a_href ) );
  345. }
  346. if ( $a_title ) {
  347. $attr_anchor[] = sprintf( 'title="%s"', esc_attr( $a_title ) );
  348. }
  349. if ( $a_target ) {
  350. $attr_anchor[] = sprintf( 'target="%s"', esc_attr( $a_target ) );
  351. }
  352. if ( $a_rel ) {
  353. $attr_anchor[] = sprintf( 'rel="%s"', esc_attr( $a_rel ) );
  354. }
  355. // turn on output buffering if we are returning a string
  356. if ( !$output ) {
  357. ob_start();
  358. }
  359. // render the list item ?>
  360. <li <?php print implode( ' ', $attr_li ) ?>>
  361. <?php print $a_before ?>
  362. <a <?php print implode( ' ', $attr_anchor ) ?>>
  363. <?php print $a_open ?>
  364. <span><?php print esc_html( $title ) ?></span>
  365. <?php print $a_close ?>
  366. </a>
  367. <?php print $a_after ?>
  368. <?php if ( $close_item ): ?>
  369. </li>
  370. <?php endif; ?>
  371. <?php
  372. if ( !$output ) {
  373. return ob_get_clean();
  374. }
  375. }
  376. /**
  377. * Render custom pagination links
  378. *
  379. * @package Infinity
  380. * @subpackage base
  381. * @todo write a paginator from scratch, this is mental
  382. */
  383. function infinity_base_paginate()
  384. {
  385. global $wp_query, $wp_rewrite;
  386. // is pagination feature enabled?
  387. if ( false == current_theme_supports( 'infinity:post', 'pagination' ) ) {
  388. // not enabled, abort
  389. return;
  390. }
  391. $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
  392. $pagination = array(
  393. 'base' => @add_query_arg('page','%#%'),
  394. 'format' => '',
  395. 'total' => $wp_query->max_num_pages,
  396. 'current' => $current,
  397. 'show_all' => false,
  398. 'end_size' => 3,
  399. 'mid_size' => 5,
  400. 'type' => 'list'
  401. );
  402. if ( $wp_rewrite->using_permalinks() ) {
  403. $pagination['base'] =
  404. user_trailingslashit(
  405. trailingslashit(
  406. remove_query_arg( 's', get_pagenum_link( 1 ) )
  407. ) . 'page/%#%/', 'paged'
  408. );
  409. }
  410. if ( !empty( $wp_query->query_vars['s'] ) ) {
  411. $pagination['add_args'] = array(
  412. 's' => urlencode( get_query_var( 's' ) )
  413. );
  414. }
  415. print paginate_links( $pagination );
  416. }
  417. /**
  418. * Add Breadcrumb functionality for WordPress SEO
  419. *
  420. * @package Infinity
  421. * @subpackage base
  422. * @todo move this to a feature extension, no direct plugin support
  423. */
  424. function infinity_base_yoast_breadcrumbs()
  425. {
  426. if ( function_exists( 'yoast_breadcrumb' ) ) {
  427. yoast_breadcrumb( '<p id="breadcrumbs">', '</p>' );
  428. }
  429. }
  430. add_action( 'open_content', 'infinity_base_yoast_breadcrumbs' );
  431. /**
  432. * Clean up image output and turn it into nice HTML5 Fig captions
  433. *
  434. * @package Infinity
  435. * @subpackage base
  436. */
  437. function infinity_base_cleaner_caption( $output, $attr, $content )
  438. {
  439. /* We're not worried abut captions in feeds, so just return the output here. */
  440. if ( is_feed() )
  441. return $output;
  442. //Set up the default arguments.
  443. $defaults = array(
  444. 'id' => '',
  445. 'align' => 'alignnone',
  446. 'width' => '',
  447. 'caption' => ''
  448. );
  449. // Merge the defaults with user input.
  450. $attr = shortcode_atts( $defaults, $attr );
  451. // If the width is less than 1 or there is no caption, return the content wrapped between the [caption]< tags.
  452. if ( 1 > $attr['width'] || empty( $attr['caption'] ) ) {
  453. return $content;
  454. }
  455. // allow filtering of content since we are overriding caption template
  456. $content = apply_filters( 'infinity_base_cleaner_caption_content', $content );
  457. // Set up the attributes for the caption <div>.
  458. $attributes = ' class="figure ' . esc_attr( $attr['align'] ) . '"';
  459. // is image center aligned?
  460. if ( 'aligncenter' === $attr['align'] ) {
  461. // yes, add fixed width
  462. $attributes .= ' style="max-width:'. esc_attr( $attr['width'] ) . 'px"';
  463. }
  464. // Open the caption <div>
  465. $output = '<figure' . $attributes .'>';
  466. // Allow shortcodes for the content the caption was created for.
  467. $output .= do_shortcode( $content );
  468. // Append the caption text.
  469. $output .= '<figcaption class="wp-caption">' . $attr['caption'] . '</figcaption>';
  470. // Close the caption </div>
  471. $output .= '</figure>';
  472. // Return the formatted, clean caption.
  473. return $output;
  474. }
  475. add_filter( 'img_caption_shortcode', 'infinity_base_cleaner_caption', 10, 3 );
  476. /**
  477. * Remove hard coded width/height attr from caption images
  478. *
  479. * @param string $content
  480. * @return string
  481. */
  482. function infinity_base_cleaner_caption_content( $content )
  483. {
  484. return preg_replace( '/\s+(width|height)="[^"]*"/', '', $content );
  485. }
  486. add_filter( 'infinity_base_cleaner_caption_content', 'infinity_base_cleaner_caption_content' );
  487. /**
  488. * Clean the output of attributes of images in editor. Courtesy of SitePoint. http://www.sitepoint.com/wordpress-change-img-tag-html/
  489. *
  490. * @package Infinity
  491. * @subpackage base
  492. */
  493. function infinity_base_get_image_tag_class( $class, $id, $align, $size )
  494. {
  495. $align = 'align' . esc_attr( $align );
  496. return $align;
  497. }
  498. add_filter( 'get_image_tag_class', 'infinity_base_get_image_tag_class', 0, 4 );