PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/bns-bio.php

https://github.com/Cais/bns-bio
PHP | 453 lines | 105 code | 96 blank | 252 comment | 11 complexity | efa9327a9437f442a96ca3be041f7b6d MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: BNS Bio
  4. Plugin URI: http://buynowshop.com/plugins/bns-bio/
  5. Description: An author details shortcode plugin with extensions that modify output
  6. Version: 0.3.1
  7. Text Domain: bns-bio
  8. Author: Edward Caissie
  9. Author URI: http://edwardcaissie.com/
  10. License: GNU General Public License v2
  11. License URI: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  12. */
  13. /**
  14. * BNS Bio
  15. * An author details shortcode plugin with multiple extensions that can modify
  16. * the output. The extension plugins, using some of the many available hooks,
  17. * can add a rounded corner border; display the details as an unordered list;
  18. * and/or hide the author email address details.
  19. *
  20. * @package BNS_Bio
  21. * @link http://buynowshop.com/plugins/bns-bio/
  22. * @link https://github.com/Cais/bns-bio/
  23. * @link http://wordpress.org/extend/plugins/bns-bio/
  24. * @version 0.3.1
  25. * @author Edward Caissie <edward.caissie@gmail.com>
  26. * @copyright Copyright (c) 2012-2013, Edward Caissie
  27. *
  28. * This program is free software; you can redistribute it and/or modify it under
  29. * the terms of the GNU General Public License version 2, as published by the
  30. * Free Software Foundation.
  31. *
  32. * You may NOT assume that you can use any other version of the GPL.
  33. *
  34. * This program is distributed in the hope that it will be useful, but WITHOUT
  35. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  36. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
  37. *
  38. * You should have received a copy of the GNU General Public License along with
  39. * this program; if not, write to:
  40. *
  41. * Free Software Foundation, Inc.
  42. * 51 Franklin St, Fifth Floor
  43. * Boston, MA 02110-1301 USA
  44. *
  45. * The license for this software can also likely be found here:
  46. * http://www.gnu.org/licenses/gpl-2.0.html
  47. *
  48. * @version 0.3
  49. * @date February 11, 2013
  50. * Refactoring without functionality changes
  51. * Documentation updates
  52. * Added code block termination comments
  53. * Added a 10px bottom margin to the general output
  54. *
  55. * @version 0.3.1
  56. * @date May 6, 2013
  57. * Version number compatibility updates
  58. */
  59. class BNS_Bio {
  60. /** Constructor */
  61. function __construct() {
  62. /** Add Scripts and Styles */
  63. add_action( 'wp_enqueue_scripts', array( $this, 'scripts_and_styles' ) );
  64. /** Create Shortcode */
  65. add_shortcode( 'bns_bio', array( $this, 'author_block' ) );
  66. }
  67. /** End function - construct */
  68. /**
  69. * Enqueue Plugin Scripts and Styles
  70. * Adds plugin stylesheet and allows for custom stylesheet to be added by end-user.
  71. *
  72. * @package BNS_Bio
  73. * @since 0.1
  74. *
  75. * @uses plugin_dir_path
  76. * @uses plugin_dir_url
  77. * @uses wp_enqueue_style
  78. */
  79. function scripts_and_styles() {
  80. /** Get the plugin data */
  81. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  82. $bns_bio_data = get_plugin_data( __FILE__ );
  83. /** Enqueue Styles */
  84. wp_enqueue_style( 'BNS-Bio-Style', plugin_dir_url( __FILE__ ) . 'bns-bio-style.css', array(), $bns_bio_data['Version'], 'screen' );
  85. /** Check if custom stylesheet is readable (exists) */
  86. if ( is_readable( plugin_dir_path( __FILE__ ) . 'bns-bio-custom-style.css' ) ) {
  87. wp_enqueue_style( 'BNS-Bio-Custom-Style', plugin_dir_url( __FILE__ ) . 'bns-bio-custom-style.css', array(), $bns_bio_data['Version'], 'screen' );
  88. }
  89. /** End if - is readable */
  90. }
  91. /** End function - scripts and styles */
  92. /**
  93. * Author Details
  94. * Collects the author details via a query then returns the specific value
  95. * based on what detail is passed to the function
  96. *
  97. * @package BNS_Bio
  98. * @since 0.3
  99. *
  100. * @param $value - name|url|email|about
  101. *
  102. * @uses get_query_var
  103. * @uses get_the_author_meta
  104. * @uses get_user_by
  105. * @uses get_userdata
  106. *
  107. * @return null|string - details as per value passed
  108. */
  109. function author_details( $value ) {
  110. /** @var $current_author - author object */
  111. $current_author = ( get_query_var( 'author_name ' ) ) ? get_user_by( 'id', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
  112. /** Get the various details to be displayed */
  113. if ( 'name' == $value ) {
  114. return get_the_author_meta( 'display_name', $current_author );
  115. } elseif ( 'url' == $value ) {
  116. return get_the_author_meta( 'user_url', $current_author );
  117. } elseif ( 'email' == $value ) {
  118. return get_the_author_meta( 'user_email', $current_author );
  119. } elseif ( 'about' == $value ) {
  120. return get_the_author_meta( 'description', $current_author );
  121. } else {
  122. return null;
  123. }
  124. /** End if - value */
  125. }
  126. /** End function - author details */
  127. /**
  128. * Wrapper Open
  129. * Adds the opening hook
  130. *
  131. * @package BNS_Bio
  132. * @since 0.3
  133. *
  134. * @uses do_action
  135. *
  136. * @return string
  137. */
  138. function wrapper_open() {
  139. ob_start();
  140. do_action( 'bns_bio_before_all' );
  141. return ob_get_clean();
  142. }
  143. /** End function - wrapper open */
  144. /**
  145. * Wrapper Close
  146. * Adds the closing hook
  147. *
  148. * @package BNS_Bio
  149. * @since 0.3
  150. *
  151. * @uses do_action
  152. *
  153. * @return string
  154. */
  155. function wrapper_close() {
  156. ob_start();
  157. do_action( 'bns_bio_after_all' );
  158. return ob_get_clean();
  159. }
  160. /** End function - wrapper close */
  161. /**
  162. * Get Author Name
  163. * Returns the author details name value
  164. *
  165. * @package BNS_Bio
  166. * @since 0.3
  167. *
  168. * @uses BNS_Bio::author_details
  169. * @uses apply_filters
  170. *
  171. * @return string
  172. */
  173. function get_author_name() {
  174. return apply_filters( 'bns_bio_author_name_text', sprintf( '<span class="bns-bio-author-name-text">%1$s</span>', __( 'Written by: ', 'bns-bio' ) ) )
  175. . apply_filters( 'bns_bio_author_name', sprintf( '<span class="bns-bio-author-name">%1$s</span>', $this->author_details( 'name' ) . '<br />' ) );
  176. }
  177. /** End function - get author name */
  178. /**
  179. * Author Name
  180. * Wraps the author name value in action hooks and returns everything as a
  181. * string for use in the shortcode output
  182. *
  183. * @package BNS_Bio
  184. * @since 0.3
  185. *
  186. * @uses BNS_Bio::get_author_name
  187. * @uses do_action
  188. *
  189. * @return string
  190. */
  191. function author_name() {
  192. ob_start();
  193. do_action( 'bns_bio_before_author_name' );
  194. echo $this->get_author_name();
  195. do_action( 'bns_bio_after_author_name' );
  196. return ob_get_clean();
  197. }
  198. /** End function - author name */
  199. /**
  200. * Get Author URL
  201. * Returns the author details url value
  202. *
  203. * @package BNS_Bio
  204. * @since 0.3
  205. *
  206. * @uses BNS_Bio::author_details
  207. * @uses apply_filters
  208. *
  209. * @return string
  210. */
  211. function get_author_url() {
  212. $author_url = $this->author_details( 'url' );
  213. if ( ! empty( $author_url ) ) {
  214. return apply_filters( 'bns_bio_author_url_text', sprintf( '<span class="bns-bio-author-url-text">%1$s</span>', __( 'From: ', 'bns-bio' ) ) )
  215. . apply_filters( 'bns_bio_author_url', sprintf( '<span class="bns-bio-author-url">%1$s</span>', $this->author_details( 'url' ) . '<br />' ) );
  216. } else {
  217. return null;
  218. }
  219. /** End if - not empty */
  220. }
  221. /** End function - get author url */
  222. /**
  223. * Author URL
  224. * Wraps the author url value in action hooks and returns everything as a
  225. * string for use in the shortcode output
  226. *
  227. * @package BNS_Bio
  228. * @since 0.3
  229. *
  230. * @uses BNS_Bio::get_author_url
  231. * @uses do_action
  232. *
  233. * @return string
  234. */
  235. function author_url() {
  236. ob_start();
  237. do_action( 'bns_bio_before_author_url' );
  238. echo $this->get_author_url();
  239. do_action( 'bns_bio_after_author_url' );
  240. return ob_get_clean();
  241. }
  242. /** End function - author url */
  243. /**
  244. * Get Author Email
  245. * Returns the author details email value
  246. *
  247. * @package BNS_Bio
  248. * @since 0.3
  249. *
  250. * @uses BNS_Bio::author_details
  251. * @uses apply_filters
  252. *
  253. * @return string
  254. */
  255. function get_author_email() {
  256. return apply_filters( 'bns_bio_author_email_text', sprintf( '<span class="bns-bio-author-email-text">%1$s</span>', __( 'Email: ', 'bns-bio' ) ) )
  257. . apply_filters( 'bns_bio_author_email', sprintf( '<span class="bns-bio-author-email">%1$s</span>', $this->author_details( 'email' ) . '<br />' ) );
  258. }
  259. /** End function - get author email */
  260. /**
  261. * Author Email
  262. * Wraps the author email value in action hooks and returns everything as a
  263. * string for use in the shortcode output
  264. *
  265. * @package BNS_Bio
  266. * @since 0.3
  267. *
  268. * @uses BNS_Bio::get_author_email
  269. * @uses do_action
  270. *
  271. * @return string
  272. */
  273. function author_email() {
  274. ob_start();
  275. do_action( 'bns_bio_before_author_email' );
  276. echo $this->get_author_email();
  277. do_action( 'bns_bio_after_author_email' );
  278. return ob_get_clean();
  279. }
  280. /** End function - author email */
  281. /**
  282. * Get Author Bio
  283. * Returns the author details description value
  284. *
  285. * @package BNS_Bio
  286. * @since 0.3
  287. *
  288. * @uses BNS_Bio::author_details
  289. * @uses apply_filters
  290. *
  291. * @return string
  292. */
  293. function get_author_bio() {
  294. $author_bio = $this->author_details( 'about' );
  295. if ( ! empty( $author_bio ) ) {
  296. return apply_filters( 'bns_bio_author_desc_text', sprintf( '<span class="bns-bio-author-desc-text">%1$s</span>', __( 'About: ', 'bns-bio' ) ) )
  297. . apply_filters( 'bns_bio_author_desc', sprintf( '<span class="bns-bio-author-desc">%1$s</span>', $this->author_details( 'about' ) ) );
  298. } else {
  299. return null;
  300. }
  301. /** End if - not empty */
  302. }
  303. /** End function - get author bio */
  304. /**
  305. * Author Bio
  306. * Wraps the author description value in action hooks and returns everything
  307. * as a string for use in the shortcode output
  308. *
  309. * @package BNS_Bio
  310. * @since 0.3
  311. *
  312. * @uses BNS_Bio::get_author_desc
  313. * @uses do_action
  314. *
  315. * @return string
  316. */
  317. function author_bio() {
  318. ob_start();
  319. do_action( 'bns_bio_before_author_desc' );
  320. echo $this->get_author_bio();
  321. do_action( 'bns_bio_after_author_desc' );
  322. return ob_get_clean();
  323. }
  324. /**
  325. * Author Block
  326. * Gets the author details and builds the basic structures of the output
  327. *
  328. * @package BNS_Bio
  329. * @since 0.1
  330. *
  331. * @uses BNS_Bio::author_bio
  332. * @uses BNS_Bio::author_email
  333. * @uses BNS_Bio::author_name
  334. * @uses BNS_Bio::author_url
  335. * @uses BNS_Bio::wrapper_close
  336. * @uses BNS_Bio::wrapper_open
  337. *
  338. * @version 0.3
  339. * @date February 11, 2013
  340. * Refactored to use individual `author_*` methods
  341. */
  342. function author_block() {
  343. $output = '<div class="bns-bio">';
  344. $output .= $this->wrapper_open();
  345. $output .= $this->author_name();
  346. $output .= $this->author_url();
  347. $output .= $this->author_email();
  348. $output .= $this->author_bio();
  349. $output .= $this->wrapper_close();
  350. $output .= '</div><!-- .bns-bio -->';
  351. return $output;
  352. }
  353. /** End function - author block */
  354. }
  355. /** End class - BNS Bio */
  356. /** @var $bns_bio - new instance of class */
  357. $bns_bio = new BNS_Bio();