/wp-content/plugins/achievements/includes/class-dpa-shortcodes.php

https://github.com/livinglab/openlab · PHP · 292 lines · 99 code · 55 blank · 138 comment · 7 complexity · e0b49c32c636cc70ec940b8698363343 MD5 · raw file

  1. <?php
  2. /**
  3. * Achievements shortcodes class
  4. *
  5. * @package Achievements
  6. * @subpackage CoreClasses
  7. */
  8. // Exit if accessed directly
  9. if ( ! defined( 'ABSPATH' ) ) exit;
  10. if ( ! class_exists( 'DPA_Shortcodes' ) ) :
  11. /**
  12. * Achievements shortcode class
  13. *
  14. * @since Achievements (3.0)
  15. */
  16. class DPA_Shortcodes {
  17. /**
  18. * @since Achievements (3.0)
  19. * @var array Shortcode => function
  20. */
  21. public $codes = array();
  22. /**
  23. * Constructor
  24. *
  25. * @since Achievements (3.0)
  26. */
  27. public function __construct() {
  28. $this->setup_globals();
  29. $this->add_shortcodes();
  30. }
  31. /**
  32. * Shortcode globals
  33. *
  34. * @since Achievements (3.0)
  35. */
  36. private function setup_globals() {
  37. // Setup the shortcodes
  38. $this->codes = apply_filters( 'dpa_shortcodes', array(
  39. // Achievements index
  40. 'dpa-achievements-index' => array( $this, 'display_achievements_index' ),
  41. // User achievement index
  42. 'dpa-user-achievements-index' => array( $this, 'display_user_achievements' ),
  43. // Specific achievement - pass an 'id' attribute
  44. 'dpa-single-achievement' => array( $this, 'display_achievement' ),
  45. // Widgets
  46. 'dpa-leaderboard' => array( $this, 'display_leaderboard' ),
  47. 'dpa-redeem-achievement-form' => array( $this, 'display_redeem_achievement_form' ),
  48. // Misc
  49. 'dpa-breadcrumb' => array( $this, 'display_breadcrumb' ),
  50. ) );
  51. }
  52. /**
  53. * Register Achievements' shortcodes
  54. *
  55. * @since Achievements (3.0)
  56. */
  57. private function add_shortcodes() {
  58. foreach( (array) $this->codes as $code => $function )
  59. add_shortcode( $code, $function );
  60. }
  61. /**
  62. * Unset some globals in the achievements() object that hold query related info
  63. *
  64. * @since Achievements (3.0)
  65. */
  66. private function unset_globals() {
  67. // Unset global queries
  68. achievements()->achievement_query = new WP_Query();
  69. achievements()->leaderboard_query = new ArrayObject();
  70. achievements()->progress_query = new WP_Query();
  71. // Unset global IDs
  72. achievements()->current_achievement_id = 0;
  73. wp_reset_postdata();
  74. }
  75. /**
  76. * Output Buffers
  77. */
  78. /**
  79. * Start an output buffer.
  80. *
  81. * This is used to put the contents of the shortcode into a variable rather
  82. * than outputting the HTML at run-time. This allows shortcodes to appear
  83. * in the correct location in the_content() instead of when it's created.
  84. *
  85. * @param string $query_name Optional
  86. * @since Achievements (3.0)
  87. */
  88. private function start( $query_name = '' ) {
  89. dpa_set_query_name( $query_name );
  90. // Start output buffer
  91. ob_start();
  92. }
  93. /**
  94. * Return the contents of the output buffer and flush its contents.
  95. *
  96. * @return string Contents of output buffer
  97. * @since Achievements (3.0)
  98. */
  99. private function end() {
  100. $this->unset_globals();
  101. dpa_reset_query_name();
  102. // Return and flush the output buffer
  103. return ob_get_clean();
  104. }
  105. /**
  106. * Achievement shortcodes
  107. */
  108. /**
  109. * Display an index of all achievement posts in an output buffer
  110. * and return to ensure that post/page contents are displayed first.
  111. *
  112. * @return string Contents of output buffer
  113. * @since Achievements (3.0)
  114. */
  115. public function display_achievements_index() {
  116. $this->unset_globals();
  117. $this->start( 'dpa_achievement_archive' );
  118. dpa_get_template_part( 'content-archive-achievement' );
  119. return $this->end();
  120. }
  121. /**
  122. * Display the contents of a specific achievement ID in an output buffer
  123. * and return to ensure that post/page contents are displayed first.
  124. *
  125. * @param array $attr
  126. * @param string $content Optional
  127. * @return string Contents of output buffer
  128. * @since Achievements (3.0)
  129. */
  130. public function display_achievement( $attr, $content = '' ) {
  131. // Sanity check required info
  132. if ( ! empty( $content ) || ( empty( $attr['id'] ) || ! is_numeric( $attr['id'] ) ) )
  133. return $content;
  134. $this->unset_globals();
  135. // Set passed attribute to $achievement_id for clarity
  136. $achievement_id = achievements()->current_achievement_id = absint( $attr['id'] );
  137. // Bail if ID passed is not an achievement
  138. if ( ! dpa_is_achievement( $achievement_id ) )
  139. return $content;
  140. // If not in theme compat, reset necessary achievement_query attributes for achievements loop to function
  141. if ( ! dpa_is_theme_compat_active() ) {
  142. achievements()->achievement_query->query_vars['post_type'] = dpa_get_achievement_post_type();
  143. achievements()->achievement_query->in_the_loop = true;
  144. achievements()->achievement_query->post = get_post( $achievement_id );
  145. }
  146. $this->start( 'dpa_single_achievement' );
  147. dpa_get_template_part( 'content-single-achievement' );
  148. return $this->end();
  149. }
  150. /**
  151. * For the current author, display an index of all their unlocked achievements
  152. * in an output buffer and return to ensure that post/page contents are displayed first.
  153. *
  154. * @return string Contents of output buffer
  155. * @since Achievements (3.0)
  156. */
  157. public function display_user_achievements() {
  158. $this->unset_globals();
  159. $this->start( 'dpa_single_user_achievements' );
  160. dpa_get_template_part( 'content-author-achievement' );
  161. return $this->end();
  162. }
  163. /**
  164. * Widget shortcodes
  165. */
  166. /**
  167. * Display the leaderboard widget in an output buffer and return to ensure that post/page contents are displayed first.
  168. *
  169. * @return string Contents of output buffer
  170. * @since Achievements (3.4)
  171. */
  172. public function display_leaderboard() {
  173. $this->unset_globals();
  174. $this->start();
  175. dpa_get_template_part( 'content-leaderboard', 'widget' );
  176. return $this->end();
  177. }
  178. /**
  179. * Display the redeem achievements widget in an output buffer and return to ensure that post/page contents are displayed first.
  180. *
  181. * @return string Contents of output buffer
  182. * @since Achievements (3.1)
  183. */
  184. public function display_redeem_achievement_form() {
  185. $this->unset_globals();
  186. $this->start();
  187. dpa_get_template_part( 'form-redeem-code' );
  188. return $this->end();
  189. }
  190. /**
  191. * Other templates
  192. */
  193. /**
  194. * Display a breadcrumb in an output buffer and return to ensure that post/page contents are displayed first.
  195. *
  196. * @return string Contents of output buffer
  197. * @since Achievements (3.0)
  198. */
  199. public function display_breadcrumb() {
  200. $this->unset_globals();
  201. $this->start();
  202. dpa_breadcrumb();
  203. return $this->end();
  204. }
  205. /**
  206. * Display the "achievement unlocked" feedback template and return to ensure that post/page contents are displayed first.
  207. *
  208. * This function is redundant. The "feedback-achievement-unlocked" template has been removed from the plugin.
  209. * Notifications were overhauled in version 3.5 and were replaced with the heartbeat-powered "live notifications" system.
  210. *
  211. * @deprecated Achievements (3.5)
  212. * @return string Contents of output buffer
  213. * @since Achievements (3.0)
  214. */
  215. public function display_feedback_achievement_unlocked() {
  216. $this->unset_globals();
  217. $this->start();
  218. dpa_get_template_part( 'feedback-achievement-unlocked' );
  219. return $this->end();
  220. }
  221. /**
  222. * Display the "achievement unlocked" javascript template and return to ensure that post/page contents are displayed first.
  223. *
  224. * Note: this is a JS template, not a HTML template. This template is wrapped inside <script> tags which will be used with
  225. * underscore.js' _.template() method. It compiles these JS templates into functions that can be evaluated for rendering.
  226. *
  227. * @return string Contents of output buffer
  228. * @since Achievements (3.5)
  229. */
  230. public function display_notifications_template() {
  231. $this->unset_globals();
  232. $this->start();
  233. dpa_get_template_part( 'feedback-notifications' );
  234. return $this->end();
  235. }
  236. }
  237. endif; // class_exists