quiz.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * Quiz shortcode.
  4. *
  5. * Usage:
  6. *
  7. * [quiz]
  8. * [question]What's the right answer?[/question]
  9. * [wrong]This one?[explanation]Nope[/explanation][/wrong]
  10. * [answer]Yes, this is the one![explanation]Yay![/explanation][/answer]
  11. * [wrong]Maybe this one[explanation]Keep trying[/explanation][/wrong]
  12. * [wrong]How about this one?[explanation]Try again[/explanation][/wrong]
  13. * [/quiz]
  14. */
  15. class Quiz_Shortcode {
  16. /**
  17. * Parameters admitted by [quiz] shortcode.
  18. *
  19. * @since 4.5.0
  20. *
  21. * @var array
  22. */
  23. private static $quiz_params = array();
  24. /**
  25. * Whether the scripts were enqueued.
  26. *
  27. * @since 4.5.0
  28. *
  29. * @var bool
  30. */
  31. private static $scripts_enqueued = false;
  32. /**
  33. * In a8c training, store user currently logged in.
  34. *
  35. * @since 4.5.0
  36. *
  37. * @var null
  38. */
  39. private static $username = null;
  40. /**
  41. * Whether the noscript tag was already printed.
  42. *
  43. * @since 4.5.0
  44. *
  45. * @var bool
  46. */
  47. private static $noscript_info_printed = false;
  48. /**
  49. * Whether JavaScript is available.
  50. *
  51. * @since 4.5.0
  52. *
  53. * @var null
  54. */
  55. private static $javascript_unavailable = null;
  56. /**
  57. * Register all shortcodes.
  58. *
  59. * @since 4.5.0
  60. */
  61. public static function init() {
  62. add_shortcode( 'quiz', array( __CLASS__, 'shortcode' ) );
  63. add_shortcode( 'question', array( __CLASS__, 'question_shortcode' ) );
  64. add_shortcode( 'answer', array( __CLASS__, 'answer_shortcode' ) );
  65. add_shortcode( 'wrong', array( __CLASS__, 'wrong_shortcode' ) );
  66. add_shortcode( 'explanation', array( __CLASS__, 'explanation_shortcode' ) );
  67. }
  68. /**
  69. * Enqueue assets needed by the quiz,
  70. *
  71. * @since 4.5.0
  72. */
  73. private static function enqueue_scripts() {
  74. wp_enqueue_style( 'quiz', plugins_url( 'css/quiz.css', __FILE__ ) );
  75. wp_enqueue_script(
  76. 'quiz',
  77. Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/quiz.min.js', 'modules/shortcodes/js/quiz.js' ),
  78. array( 'jquery' ),
  79. null,
  80. true
  81. );
  82. }
  83. /**
  84. * Check if this is a feed and thus JS is unavailable.
  85. *
  86. * @since 4.5.0
  87. *
  88. * @return bool|null
  89. */
  90. private static function is_javascript_unavailable() {
  91. if ( ! is_null( self::$javascript_unavailable ) ) {
  92. return self::$javascript_unavailable;
  93. }
  94. if ( is_feed() ) {
  95. return self::$javascript_unavailable = true;
  96. }
  97. return self::$javascript_unavailable = false;
  98. }
  99. /**
  100. * Display message when JS is not available.
  101. *
  102. * @since 4.5.0
  103. *
  104. * @return string
  105. */
  106. private static function noscript_info() {
  107. if ( self::$noscript_info_printed ) {
  108. return '';
  109. }
  110. self::$noscript_info_printed = true;
  111. return '<noscript><div><i>' . esc_html__( 'Please view this post in your web browser to complete the quiz.', 'jetpack' ) . '</i></div></noscript>';
  112. }
  113. /**
  114. * Check if we're in WordPress.com.
  115. *
  116. * @since 4.5.0
  117. *
  118. * @return bool
  119. */
  120. public static function is_wpcom() {
  121. return defined( 'IS_WPCOM' ) && IS_WPCOM;
  122. }
  123. /**
  124. * Parse shortcode arguments and render its output.
  125. *
  126. * @since 4.5.0
  127. *
  128. * @param array $atts Shortcode parameters.
  129. * @param string $content Content enclosed by shortcode tags.
  130. *
  131. * @return string
  132. */
  133. public static function shortcode( $atts, $content = null ) {
  134. // There's nothing to do if there's nothing enclosed.
  135. if ( null == $content ) {
  136. return '';
  137. }
  138. $id = '';
  139. if ( self::is_javascript_unavailable() ) {
  140. // in an e-mail print the question and the info sentence once per question, too
  141. self::$noscript_info_printed = false;
  142. } else {
  143. if ( ! self::$scripts_enqueued ) {
  144. // lazy enqueue cannot use the wp_enqueue_scripts action anymore
  145. self::enqueue_scripts();
  146. self::$scripts_enqueued = true;
  147. }
  148. $default_atts = self::is_wpcom()
  149. ? array(
  150. 'trackid' => '',
  151. 'a8ctraining' => '',
  152. )
  153. : array(
  154. 'trackid' => '',
  155. );
  156. self::$quiz_params = shortcode_atts( $default_atts, $atts );
  157. if ( ! empty( self::$quiz_params[ 'trackid' ] ) ) {
  158. $id .= ' data-trackid="' . esc_attr( self::$quiz_params[ 'trackid' ] ) . '"';
  159. }
  160. if ( self::is_wpcom() && ! empty( self::$quiz_params[ 'a8ctraining' ] ) ) {
  161. if ( is_null( self::$username ) ) {
  162. self::$username = wp_get_current_user()->user_login;
  163. }
  164. $id .= ' data-a8ctraining="'. esc_attr( self::$quiz_params[ 'a8ctraining' ] ) . '" data-username="' . esc_attr( self::$username ) . '"';
  165. }
  166. }
  167. $quiz = self::do_shortcode( $content );
  168. return '<div class="jetpack-quiz quiz"' . $id . '>' . $quiz . '</div>';
  169. }
  170. /**
  171. * Strip line breaks, restrict allowed HTML to a few whitelisted tags and execute nested shortcodes.
  172. *
  173. * @since 4.5.0
  174. *
  175. * @param string $content
  176. *
  177. * @return mixed|string
  178. */
  179. private static function do_shortcode( $content ) {
  180. // strip autoinserted line breaks
  181. $content = preg_replace( '#(<(?:br /|/?p)>\n?)*(\[/?[a-z]+\])(<(?:br /|/?p)>\n?)*#', '$2', $content );
  182. // Add internal parameter so it's only rendered when it has it
  183. $content = preg_replace( '/\[(question|answer|wrong|explanation)\]/i', '[$1 quiz_item="true"]', $content );
  184. $content = do_shortcode( $content );
  185. $content = wp_kses( $content, array(
  186. 'tt' => array(),
  187. 'pre' => array(),
  188. 'strong' => array(),
  189. 'i' => array(),
  190. 'br' => array(),
  191. 'img' => array( 'src' => true),
  192. 'div' => array( 'class' => true, 'data-correct' => 1, 'data-track-id' => 1, 'data-a8ctraining' => 1, 'data-username' => 1 ),
  193. ) );
  194. return $content;
  195. }
  196. /**
  197. * Render question.
  198. *
  199. * @since 4.5.0
  200. *
  201. * @param array $atts
  202. * @param null $content
  203. *
  204. * @return string
  205. */
  206. public static function question_shortcode( $atts, $content = null ) {
  207. return isset( $atts['quiz_item'] )
  208. ? '<div class="jetpack-quiz-question question">' . self::do_shortcode( $content ) . '</div>'
  209. : '';
  210. }
  211. /**
  212. * Render correct answer.
  213. *
  214. * @since 4.5.0
  215. *
  216. * @param array $atts
  217. * @param null $content
  218. *
  219. * @return string
  220. */
  221. public static function answer_shortcode( $atts, $content = null ) {
  222. if ( self::is_javascript_unavailable() ) {
  223. return self::noscript_info();
  224. }
  225. return isset( $atts['quiz_item'] )
  226. ? '<div class="jetpack-quiz-answer answer" data-correct="1">' . self::do_shortcode( $content ) . '</div>'
  227. : '';
  228. }
  229. /**
  230. * Render wrong response.
  231. *
  232. * @since 4.5.0
  233. *
  234. * @param array $atts
  235. * @param null $content
  236. *
  237. * @return string
  238. */
  239. public static function wrong_shortcode( $atts, $content = null ) {
  240. if ( self::is_javascript_unavailable() ) {
  241. return self::noscript_info();
  242. }
  243. return isset( $atts['quiz_item'] )
  244. ? '<div class="jetpack-quiz-answer answer">' . self::do_shortcode( $content ) . '</div>'
  245. : '';
  246. }
  247. /**
  248. * Render explanation for wrong or right answer.
  249. *
  250. * @since 4.5.0
  251. *
  252. * @param array $atts
  253. * @param null $content
  254. *
  255. * @return string
  256. */
  257. public static function explanation_shortcode( $atts, $content = null ) {
  258. if ( self::is_javascript_unavailable() ) {
  259. return self::noscript_info();
  260. }
  261. return isset( $atts['quiz_item'] )
  262. ? '<div class="jetpack-quiz-explanation explanation">' . self::do_shortcode( $content ) . '</div>'
  263. : '';
  264. }
  265. }
  266. Quiz_Shortcode::init();