Keine Beschreibung

quiz.php 9.2KB

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