PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/functions/shortcodes.php

https://bitbucket.org/lgorence/quickpress
PHP | 474 lines | 206 code | 69 blank | 199 comment | 19 complexity | e9427cb2ae612030212916db5685fb09 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Shortcodes bundled for use with themes. These shortcodes are not meant to be used with the post content
  4. * editor. Their purpose is to make it easier for users to filter hooks without having to know too much PHP code
  5. * and to provide access to specific functionality in other (non-post content) shortcode-aware areas. Note that
  6. * some shortcodes are specific to posts and comments and would be useless outside of the post and comment
  7. * loops. To use the shortcodes, a theme must register support for 'hybrid-core-shortcodes'.
  8. *
  9. * @package HybridCore
  10. * @subpackage Functions
  11. * @author Justin Tadlock <justin@justintadlock.com>
  12. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  13. * @link http://themehybrid.com/hybrid-core
  14. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  15. */
  16. /* Register shortcodes. */
  17. add_action( 'init', 'hybrid_add_shortcodes' );
  18. /**
  19. * Creates new shortcodes for use in any shortcode-ready area. This function uses the add_shortcode()
  20. * function to register new shortcodes with WordPress.
  21. *
  22. * @since 0.8.0
  23. * @access private
  24. * @uses add_shortcode() to create new shortcodes.
  25. * @link http://codex.wordpress.org/Shortcode_API
  26. * @return void
  27. */
  28. function hybrid_add_shortcodes() {
  29. /* Add theme-specific shortcodes. */
  30. add_shortcode( 'the-year', 'hybrid_the_year_shortcode' );
  31. add_shortcode( 'site-link', 'hybrid_site_link_shortcode' );
  32. add_shortcode( 'wp-link', 'hybrid_wp_link_shortcode' );
  33. add_shortcode( 'theme-link', 'hybrid_theme_link_shortcode' );
  34. add_shortcode( 'child-link', 'hybrid_child_link_shortcode' );
  35. add_shortcode( 'loginout-link', 'hybrid_loginout_link_shortcode' );
  36. add_shortcode( 'query-counter', 'hybrid_query_counter_shortcode' );
  37. add_shortcode( 'nav-menu', 'hybrid_nav_menu_shortcode' );
  38. /* Add entry-specific shortcodes. */
  39. add_shortcode( 'entry-title', 'hybrid_entry_title_shortcode' );
  40. add_shortcode( 'entry-author', 'hybrid_entry_author_shortcode' );
  41. add_shortcode( 'entry-terms', 'hybrid_entry_terms_shortcode' );
  42. add_shortcode( 'entry-comments-link', 'hybrid_entry_comments_link_shortcode' );
  43. add_shortcode( 'entry-published', 'hybrid_entry_published_shortcode' );
  44. add_shortcode( 'entry-edit-link', 'hybrid_entry_edit_link_shortcode' );
  45. add_shortcode( 'entry-shortlink', 'hybrid_entry_shortlink_shortcode' );
  46. add_shortcode( 'entry-permalink', 'hybrid_entry_permalink_shortcode' );
  47. add_shortcode( 'post-format-link', 'hybrid_post_format_link_shortcode' );
  48. /* Add comment-specific shortcodes. */
  49. add_shortcode( 'comment-published', 'hybrid_comment_published_shortcode' );
  50. add_shortcode( 'comment-author', 'hybrid_comment_author_shortcode' );
  51. add_shortcode( 'comment-edit-link', 'hybrid_comment_edit_link_shortcode' );
  52. add_shortcode( 'comment-reply-link', 'hybrid_comment_reply_link_shortcode' );
  53. add_shortcode( 'comment-permalink', 'hybrid_comment_permalink_shortcode' );
  54. }
  55. /**
  56. * Shortcode to display the current year.
  57. *
  58. * @since 0.6.0
  59. * @access public
  60. * @uses date() Gets the current year.
  61. * @return string
  62. */
  63. function hybrid_the_year_shortcode() {
  64. return date( __( 'Y', 'hybrid-core' ) );
  65. }
  66. /**
  67. * Shortcode to display a link back to the site.
  68. *
  69. * @since 0.6.0
  70. * @access public
  71. * @uses get_bloginfo() Gets information about the install.
  72. * @return string
  73. */
  74. function hybrid_site_link_shortcode() {
  75. return '<a class="site-link" href="' . home_url() . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . '" rel="home"><span>' . get_bloginfo( 'name' ) . '</span></a>';
  76. }
  77. /**
  78. * Shortcode to display a link to WordPress.org.
  79. *
  80. * @since 0.6.0
  81. * @access public
  82. * @return string
  83. */
  84. function hybrid_wp_link_shortcode() {
  85. return '<a class="wp-link" href="http://wordpress.org" title="' . esc_attr__( 'State-of-the-art semantic personal publishing platform', 'hybrid-core' ) . '"><span>' . __( 'WordPress', 'hybrid-core' ) . '</span></a>';
  86. }
  87. /**
  88. * Shortcode to display a link to the parent theme page.
  89. *
  90. * @since 0.6.0
  91. * @access public
  92. * @uses get_theme_data() Gets theme (parent theme) information.
  93. * @return string
  94. */
  95. function hybrid_theme_link_shortcode() {
  96. $theme = wp_get_theme( get_template(), get_theme_root( get_template_directory() ) );
  97. return '<a class="theme-link" href="' . esc_url( $theme->get( 'ThemeURI' ) ) . '" title="' . sprintf( esc_attr__( '%s WordPress Theme', 'hybrid-core' ), $theme->get( 'Name' ) ) . '"><span>' . esc_attr( $theme->get( 'Name' ) ) . '</span></a>';
  98. }
  99. /**
  100. * Shortcode to display a link to the child theme's page.
  101. *
  102. * @since 0.6.0
  103. * @access public
  104. * @uses get_theme_data() Gets theme (child theme) information.
  105. * @return string
  106. */
  107. function hybrid_child_link_shortcode() {
  108. $theme = wp_get_theme( get_stylesheet(), get_theme_root( get_stylesheet_directory() ) );
  109. return '<a class="child-link" href="' . esc_url( $theme->get( 'ThemeURI' ) ) . '" title="' . esc_attr( $theme->get( 'Name' ) ) . '"><span>' . esc_html( $theme->get( 'Name' ) ) . '</span></a>';
  110. }
  111. /**
  112. * Shortcode to display a login link or logout link.
  113. *
  114. * @since 0.6.0
  115. * @access public
  116. * @uses is_user_logged_in() Checks if the current user is logged into the site.
  117. * @uses wp_logout_url() Creates a logout URL.
  118. * @uses wp_login_url() Creates a login URL.
  119. * @return string
  120. */
  121. function hybrid_loginout_link_shortcode() {
  122. if ( is_user_logged_in() )
  123. $out = '<a class="logout-link" href="' . esc_url( wp_logout_url( site_url( $_SERVER['REQUEST_URI'] ) ) ) . '" title="' . esc_attr__( 'Log out', 'hybrid-core' ) . '">' . __( 'Log out', 'hybrid-core' ) . '</a>';
  124. else
  125. $out = '<a class="login-link" href="' . esc_url( wp_login_url( site_url( $_SERVER['REQUEST_URI'] ) ) ) . '" title="' . esc_attr__( 'Log in', 'hybrid-core' ) . '">' . __( 'Log in', 'hybrid-core' ) . '</a>';
  126. return $out;
  127. }
  128. /**
  129. * Displays query count and load time if the current user can edit themes.
  130. *
  131. * @since 0.6.0
  132. * @access public
  133. * @uses current_user_can() Checks if the current user can edit themes.
  134. * @return string
  135. */
  136. function hybrid_query_counter_shortcode() {
  137. if ( current_user_can( 'edit_theme_options' ) )
  138. return sprintf( __( 'This page loaded in %1$s seconds with %2$s database queries.', 'hybrid-core' ), timer_stop( 0, 3 ), get_num_queries() );
  139. return '';
  140. }
  141. /**
  142. * Displays a nav menu that has been created from the Menus screen in the admin.
  143. *
  144. * @since 0.8.0
  145. * @access public
  146. * @uses wp_nav_menu() Displays the nav menu.
  147. * @return string
  148. */
  149. function hybrid_nav_menu_shortcode( $attr ) {
  150. $attr = shortcode_atts(
  151. array(
  152. 'menu' => '',
  153. 'container' => 'div',
  154. 'container_id' => '',
  155. 'container_class' => 'nav-menu',
  156. 'menu_id' => '',
  157. 'menu_class' => '',
  158. 'link_before' => '',
  159. 'link_after' => '',
  160. 'before' => '',
  161. 'after' => '',
  162. 'fallback_cb' => 'wp_page_menu',
  163. 'walker' => ''
  164. ),
  165. $attr
  166. );
  167. $attr['echo'] = false;
  168. return wp_nav_menu( $attr );
  169. }
  170. /**
  171. * Displays the edit link for an individual post.
  172. *
  173. * @since 0.7.0
  174. * @access public
  175. * @param array $attr
  176. * @return string
  177. */
  178. function hybrid_entry_edit_link_shortcode( $attr ) {
  179. $post_type = get_post_type_object( get_post_type() );
  180. if ( !current_user_can( $post_type->cap->edit_post, get_the_ID() ) )
  181. return '';
  182. $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
  183. return $attr['before'] . '<span class="edit"><a class="post-edit-link" href="' . esc_url( get_edit_post_link( get_the_ID() ) ) . '" title="' . sprintf( esc_attr__( 'Edit %1$s', 'hybrid-core' ), $post_type->labels->singular_name ) . '">' . __( 'Edit', 'hybrid-core' ) . '</a></span>' . $attr['after'];
  184. }
  185. /**
  186. * Displays the published date of an individual post.
  187. *
  188. * @since 0.7.0
  189. * @access public
  190. * @param array $attr
  191. * @return string
  192. */
  193. function hybrid_entry_published_shortcode( $attr ) {
  194. $attr = shortcode_atts( array( 'before' => '', 'after' => '', 'format' => get_option( 'date_format' ) ), $attr );
  195. $published = '<abbr class="published" title="' . sprintf( get_the_time( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) ) . '">' . sprintf( get_the_time( $attr['format'] ) ) . '</abbr>';
  196. return $attr['before'] . $published . $attr['after'];
  197. }
  198. /**
  199. * Displays a post's number of comments wrapped in a link to the comments area.
  200. *
  201. * @since 0.7.0
  202. * @access public
  203. * @param array $attr
  204. * @return string
  205. */
  206. function hybrid_entry_comments_link_shortcode( $attr ) {
  207. $comments_link = '';
  208. $number = doubleval( get_comments_number() );
  209. $attr = shortcode_atts( array( 'zero' => __( 'Leave a response', 'hybrid-core' ), 'one' => __( '%1$s Response', 'hybrid-core' ), 'more' => __( '%1$s Responses', 'hybrid-core' ), 'css_class' => 'comments-link', 'none' => '', 'before' => '', 'after' => '' ), $attr );
  210. if ( 0 == $number && !comments_open() && !pings_open() ) {
  211. if ( $attr['none'] )
  212. $comments_link = '<span class="' . esc_attr( $attr['css_class'] ) . '">' . sprintf( $attr['none'], number_format_i18n( $number ) ) . '</span>';
  213. }
  214. elseif ( 0 == $number )
  215. $comments_link = '<a class="' . esc_attr( $attr['css_class'] ) . '" href="' . get_permalink() . '#respond" title="' . sprintf( esc_attr__( 'Comment on %1$s', 'hybrid-core' ), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( $attr['zero'], number_format_i18n( $number ) ) . '</a>';
  216. elseif ( 1 == $number )
  217. $comments_link = '<a class="' . esc_attr( $attr['css_class'] ) . '" href="' . get_comments_link() . '" title="' . sprintf( esc_attr__( 'Comment on %1$s', 'hybrid-core' ), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( $attr['one'], number_format_i18n( $number ) ) . '</a>';
  218. elseif ( 1 < $number )
  219. $comments_link = '<a class="' . esc_attr( $attr['css_class'] ) . '" href="' . get_comments_link() . '" title="' . sprintf( esc_attr__( 'Comment on %1$s', 'hybrid-core' ), the_title_attribute( 'echo=0' ) ) . '">' . sprintf( $attr['more'], number_format_i18n( $number ) ) . '</a>';
  220. if ( $comments_link )
  221. $comments_link = $attr['before'] . $comments_link . $attr['after'];
  222. return $comments_link;
  223. }
  224. /**
  225. * Displays an individual post's author with a link to his or her archive.
  226. *
  227. * @since 0.7.0
  228. * @access public
  229. * @param array $attr
  230. * @return string
  231. */
  232. function hybrid_entry_author_shortcode( $attr ) {
  233. $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
  234. $author = '<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '" title="' . esc_attr( get_the_author_meta( 'display_name' ) ) . '">' . get_the_author_meta( 'display_name' ) . '</a></span>';
  235. return $attr['before'] . $author . $attr['after'];
  236. }
  237. /**
  238. * Displays a list of terms for a specific taxonomy.
  239. *
  240. * @since 0.7.0
  241. * @access public
  242. * @param array $attr
  243. * @return string
  244. */
  245. function hybrid_entry_terms_shortcode( $attr ) {
  246. $attr = shortcode_atts( array( 'id' => get_the_ID(), 'taxonomy' => 'post_tag', 'separator' => ', ', 'before' => '', 'after' => '' ), $attr );
  247. $attr['before'] = ( empty( $attr['before'] ) ? '<span class="' . $attr['taxonomy'] . '">' : '<span class="' . $attr['taxonomy'] . '"><span class="before">' . $attr['before'] . '</span>' );
  248. $attr['after'] = ( empty( $attr['after'] ) ? '</span>' : '<span class="after">' . $attr['after'] . '</span></span>' );
  249. return get_the_term_list( $attr['id'], $attr['taxonomy'], $attr['before'], $attr['separator'], $attr['after'] );
  250. }
  251. /**
  252. * Displays a post's title with a link to the post.
  253. *
  254. * @since 0.7.0
  255. * @access public
  256. * @return string
  257. */
  258. function hybrid_entry_title_shortcode( $attr ) {
  259. $attr = shortcode_atts( array( 'permalink' => true ), $attr );
  260. $tag = is_singular() ? 'h1' : 'h2';
  261. $class = sanitize_html_class( get_post_type() ) . '-title entry-title';
  262. if ( false == (bool)$attr['permalink'] )
  263. $title = the_title( "<{$tag} class='{$class}'>", "</{$tag}>", false );
  264. else
  265. $title = the_title( "<{$tag} class='{$class}'><a href='" . get_permalink() . "'>", "</a></{$tag}>", false );
  266. if ( empty( $title ) && !is_singular() )
  267. $title = "<{$tag} class='{$class}'><a href='" . get_permalink() . "'>" . __( '(Untitled)', 'hybrid-core' ) . "</a></{$tag}>";
  268. return $title;
  269. }
  270. /**
  271. * Displays the shortlink of an individual entry.
  272. *
  273. * @since 0.8.0
  274. * @access public
  275. * @return string
  276. */
  277. function hybrid_entry_shortlink_shortcode( $attr ) {
  278. $attr = shortcode_atts(
  279. array(
  280. 'text' => __( 'Shortlink', 'hybrid-core' ),
  281. 'title' => the_title_attribute( array( 'echo' => false ) ),
  282. 'before' => '',
  283. 'after' => ''
  284. ),
  285. $attr
  286. );
  287. $shortlink = esc_url( wp_get_shortlink( get_the_ID() ) );
  288. return "{$attr['before']}<a class='shortlink' href='{$shortlink}' title='" . esc_attr( $attr['title'] ) . "' rel='shortlink'>{$attr['text']}</a>{$attr['after']}";
  289. }
  290. /**
  291. * Returns the output of the [entry-permalink] shortcode, which is a link back to the post permalink page.
  292. *
  293. * @since 1.3.0.
  294. * @param array $attr The shortcode arguments.
  295. * @return string A permalink back to the post.
  296. */
  297. function hybrid_entry_permalink_shortcode( $attr ) {
  298. $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
  299. return $attr['before'] . '<a href="' . esc_url( get_permalink() ) . '" class="permalink">' . __( 'Permalink', 'hybrid-core' ) . '</a>' . $attr['after'];
  300. }
  301. /**
  302. * Returns the output of the [post-format-link] shortcode. This shortcode is for use when a theme uses the
  303. * post formats feature.
  304. *
  305. * @since 1.3.0.
  306. * @param array $attr The shortcode arguments.
  307. * @return string A link to the post format archive.
  308. */
  309. function hybrid_post_format_link_shortcode( $attr ) {
  310. $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
  311. $format = get_post_format();
  312. $url = ( empty( $format ) ? get_permalink() : get_post_format_link( $format ) );
  313. return $attr['before'] . '<a href="' . esc_url( $url ) . '" class="post-format-link">' . get_post_format_string( $format ) . '</a>' . $attr['after'];
  314. }
  315. /**
  316. * Displays the published date and time of an individual comment.
  317. *
  318. * @since 0.7.0
  319. * @access public
  320. * @return string
  321. */
  322. function hybrid_comment_published_shortcode() {
  323. $link = '<span class="published">' . sprintf( __( '%1$s at %2$s', 'hybrid-core' ), '<abbr class="comment-date" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . get_comment_date() . '</abbr>', '<abbr class="comment-time" title="' . get_comment_date( esc_attr__( 'l, F jS, Y, g:i a', 'hybrid-core' ) ) . '">' . get_comment_time() . '</abbr>' ) . '</span>';
  324. return $link;
  325. }
  326. /**
  327. * Displays the comment author of an individual comment.
  328. *
  329. * @since 0.8.0
  330. * @access public
  331. * @global $comment The current comment's DB object.
  332. * @return string
  333. */
  334. function hybrid_comment_author_shortcode( $attr ) {
  335. global $comment;
  336. $attr = shortcode_atts(
  337. array(
  338. 'before' => '',
  339. 'after' => '',
  340. 'tag' => 'span' // @deprecated 1.2.0 Back-compatibility. Please don't use this argument.
  341. ),
  342. $attr
  343. );
  344. $author = esc_html( get_comment_author( $comment->comment_ID ) );
  345. $url = esc_url( get_comment_author_url( $comment->comment_ID ) );
  346. /* Display link and cite if URL is set. Also, properly cites trackbacks/pingbacks. */
  347. if ( $url )
  348. $output = '<cite class="fn" title="' . $url . '"><a href="' . $url . '" title="' . esc_attr( $author ) . '" class="url" rel="external nofollow">' . $author . '</a></cite>';
  349. else
  350. $output = '<cite class="fn">' . $author . '</cite>';
  351. $output = '<' . tag_escape( $attr['tag'] ) . ' class="comment-author vcard">' . $attr['before'] . apply_filters( 'get_comment_author_link', $output ) . $attr['after'] . '</' . tag_escape( $attr['tag'] ) . '><!-- .comment-author .vcard -->';
  352. return $output;
  353. }
  354. /**
  355. * Displays the permalink to an individual comment.
  356. *
  357. * @since 0.7.0
  358. * @access public
  359. * @return string
  360. */
  361. function hybrid_comment_permalink_shortcode( $attr ) {
  362. global $comment;
  363. $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
  364. $link = '<a class="permalink" href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '" title="' . sprintf( esc_attr__( 'Permalink to comment %1$s', 'hybrid-core' ), $comment->comment_ID ) . '">' . __( 'Permalink', 'hybrid-core' ) . '</a>';
  365. return $attr['before'] . $link . $attr['after'];
  366. }
  367. /**
  368. * Displays a comment's edit link to users that have the capability to edit the comment.
  369. *
  370. * @since 0.7.0
  371. * @access public
  372. * @return string
  373. */
  374. function hybrid_comment_edit_link_shortcode( $attr ) {
  375. global $comment;
  376. $edit_link = get_edit_comment_link( $comment->comment_ID );
  377. if ( !$edit_link )
  378. return '';
  379. $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr );
  380. $link = '<a class="comment-edit-link" href="' . esc_url( $edit_link ) . '" title="' . sprintf( esc_attr__( 'Edit %1$s', 'hybrid-core' ), $comment->comment_type ) . '"><span class="edit">' . __( 'Edit', 'hybrid-core' ) . '</span></a>';
  381. $link = apply_filters( 'edit_comment_link', $link, $comment->comment_ID );
  382. return $attr['before'] . $link . $attr['after'];
  383. }
  384. /**
  385. * Displays a reply link for the 'comment' comment_type if threaded comments are enabled.
  386. *
  387. * @since 0.7.0
  388. * @access public
  389. * @return string
  390. */
  391. function hybrid_comment_reply_link_shortcode( $attr ) {
  392. if ( !get_option( 'thread_comments' ) || 'comment' !== get_comment_type() )
  393. return '';
  394. $defaults = array(
  395. 'reply_text' => __( 'Reply', 'hybrid-core' ),
  396. 'login_text' => __( 'Log in to reply.', 'hybrid-core' ),
  397. 'depth' => intval( $GLOBALS['comment_depth'] ),
  398. 'max_depth' => get_option( 'thread_comments_depth' ),
  399. 'before' => '',
  400. 'after' => ''
  401. );
  402. $attr = shortcode_atts( $defaults, $attr );
  403. return get_comment_reply_link( $attr );
  404. }
  405. ?>