This repository was archived by the owner on Feb 19, 2024. It is now read-only.
forked from roborourke/quizzlestick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquizzlestick.php
More file actions
1635 lines (1324 loc) · 55 KB
/
quizzlestick.php
File metadata and controls
1635 lines (1324 loc) · 55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Quizzlestick
Plugin URI: http://interconnectit.com
Description: Quizzlestick wrapper plugin to make creating quizzes within WordPress easy
Version: 1.0
Author: interconnect/it
Author URI: http://interconnectit.com
*/
if ( ! defined( 'QUIZZLESTICK_DIR' ) )
define( 'QUIZZLESTICK_DIR', dirname( __FILE__ ) );
if ( ! defined( 'QUIZZLESTICK_URL' ) )
define( 'QUIZZLESTICK_URL', plugins_url( '', __FILE__ ) );
// Load ICIT core
require_once( 'core/core.php' );
// Make sure core knows where core is.
icit_core::instance()->set_core_url( QUIZZLESTICK_URL . '/core/' );
if ( ! class_exists( 'quizzlestick' ) && class_exists( 'icit_plugin' ) ) {
add_action( 'plugins_loaded', array( 'quizzlestick', 'instance' ) );
class quizzlestick extends icit_plugin {
const DOM = __CLASS__;
const FILE = __FILE__;
/**
* @var string Post type for quizzes
*/
public $post_type = 'quiz';
/**
* @var bool
*/
public $doing_excerpt = false;
/**
* @var bool override settings page creation
*/
public $create_options_page = false;
/**
* @var quizzlestick self
*/
protected static $instance = null;
/**
* Create and return an instance of this class
*
* @return quizzlestick An Singleton instance of self
*/
public static function instance( ) {
null === self::$instance && self::$instance = new self;
return self::$instance;
}
public function __construct( ) {
parent::__construct( array(
'title' => 'Quizzlestick',
'menu_title' => 'Quizzes',
'option_group' => 'quizzlestick',
) );
// load custom embedder class
require_once( 'inc/wp-embed.php' );
// load preview meta class
//require_once( 'inc/wp-preview-meta.php' );
// add meta boxes for our quiz
// add custom fields to quiz pages
add_action( 'add_meta_boxes', array( $this, 'meta_boxes' ), 10, 2 );
add_action( 'save_post', array( $this, 'save_post' ), 10, 3 );
// shortcode
add_shortcode( 'quizzlestick', array( $this, 'shortcode' ) );
// add quiz to content for quiz post type
add_filter( 'the_content', array( $this, 'quiz_content' ), 10 );
// ajax handler for the stats tracking
add_action( 'wp_ajax_quizzlestick_api', array( $this, 'api' ) );
add_action( 'wp_ajax_nopriv_quizzlestick_api', array( $this, 'api' ) );
//add_filter( 'quizzlestick-quickfire-delay', array( $this, 'set_quiz_nextdelay' ), 10, 2 );
}
public function init() {
// register post type
$this->quiz_post_type();
// default filters
foreach( array( 'quizzlestick_question', 'quizzlestick_answer' ) as $filter ) {
add_filter( $filter, 'wptexturize' );
add_filter( $filter, 'convert_smilies' );
add_filter( $filter, 'convert_chars' );
//add_filter( $filter, 'wpautop' );
add_filter( $filter, 'shortcode_unautop' );
// add autoembed to our new filters
new WP_Custom_Embed( $filter );
}
foreach( array( 'quizzlestick_result', 'quizzlestick_graduated_result' ) as $filter ) {
add_filter( $filter, 'wptexturize' );
add_filter( $filter, 'convert_smilies' );
add_filter( $filter, 'convert_chars' );
add_filter( $filter, 'wpautop' );
add_filter( $filter, 'shortcode_unautop' );
// add autoembed to our new filters
new WP_Custom_Embed( $filter );
}
// add to stupid defaults
add_filter( 'embed_cache_oembed_types', function( $types ) {
$types[] = 'quiz';
return $types;
} );
}
public function enqueue_scripts() {
if ( ! is_admin() ) {
// Allow theme override of css
if( !current_theme_supports( 'quizzlestick-css') ) {
//wp_register_style( 'quizzlestick', QUIZZLESTICK_URL . '/js/quizzlestick/quizzlestick.css', array() );
wp_register_style( 'quizzlestick-css', QUIZZLESTICK_URL . '/css/default.css', array() );
wp_enqueue_style( 'quizzlestick-css' );
}
// But the js we *really* need to keep
//$prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script( 'quizzlestick', QUIZZLESTICK_URL . "/js/quizzlestick/quizzlestick.min.js", array( 'jquery' ) );
wp_register_script( 'quizzlestick-wp', QUIZZLESTICK_URL . "/js/quizzlestick-wp.min.js", array( 'quizzlestick' ) );
wp_localize_script( 'quizzlestick-wp', 'quizzlestickwp', array(
'ajaxurl' => admin_url( '/admin-ajax.php' ),
'nonce' => wp_create_nonce( 'quizzlestick' . AUTH_SALT )
) );
// load front end scripts
wp_enqueue_script( 'quizzlestick-wp' );
}
}
public function admin_enqueue_scripts( $hook = '' ) {
wp_enqueue_style( 'quizzlestick-admin', QUIZZLESTICK_URL . '/css/admin.css', array() );
wp_enqueue_script( 'quizzlestick-admin', QUIZZLESTICK_URL . '/js/admin.js', array( 'jquery' ) );
}
/**
* Create a new post type for the quiz
*
* @return string Return the updated quiz message
*/
public function quiz_post_type() {
register_post_type( 'quiz', array(
'hierarchical' => false,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'supports' => array( 'title', 'excerpt', 'thumbnail' ),
'has_archive' => true,
'query_var' => true,
'rewrite' => true,
'menu_icon' => 'dashicons-welcome-learn-more',
'labels' => array(
'name' => __( 'Quizzes', $this::DOM ),
'singular_name' => __( 'Quiz', $this::DOM ),
'all_items' => __( 'Quizzes', $this::DOM ),
'new_item' => __( 'New Quiz', $this::DOM ),
'add_new' => __( 'Add New', $this::DOM ),
'add_new_item' => __( 'Add New Quiz', $this::DOM ),
'edit_item' => __( 'Edit Quiz', $this::DOM ),
'view_item' => __( 'View Quiz', $this::DOM ),
'search_items' => __( 'Search Quizzes', $this::DOM ),
'not_found' => __( 'No Quizzes found', $this::DOM ),
'not_found_in_trash' => __( 'No Quizzes found in trash', $this::DOM ),
'parent_item_colon' => __( 'Parent Quiz', $this::DOM ),
'menu_name' => __( 'Quizzes', $this::DOM ),
),
) );
add_filter( 'post_updated_messages', function( $messages ) {
global $post;
$permalink = get_permalink( $post );
$messages['quiz'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('Quiz updated. <a target="_blank" href="%s">View Quiz</a>', $this::DOM), esc_url( $permalink ) ),
2 => __('Custom field updated.', $this::DOM),
3 => __('Custom field deleted.', $this::DOM),
4 => __('Quiz updated.', $this::DOM),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('Quiz restored to revision from %s', $this::DOM), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Quiz published. <a href="%s">View Quiz</a>', $this::DOM), esc_url( $permalink ) ),
7 => __('Quiz saved.', $this::DOM),
8 => sprintf( __('Quiz submitted. <a target="_blank" href="%s">Preview Quiz</a>', $this::DOM), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
9 => sprintf( __('Quiz scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Quiz</a>', $this::DOM),
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ),
10 => sprintf( __('Quiz draft updated. <a target="_blank" href="%s">Preview Quiz</a>', $this::DOM), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
);
return $messages;
} );
}
public function extract_meta( $key, $meta ) {
if ( ! isset( $meta[ $key ] ) )
return false;
return maybe_unserialize( $meta[ $key ][ 0 ] );
}
/**
* Register shortcode
*
* @param array $atts ID of the quiz
* @param string $content The post content
*
* @return quizzlestick Returns the quiz found with the ID supplied
*/
public function shortcode( $atts, $content = '' ) {
extract( shortcode_atts( array(
'id' => false
), $atts ) );
if ( $id )
$post = get_post( $id );
if ( !( $post && $post->post_type === 'quiz' ) ) {
return '<p>No Quiz ID supplied or failed to find quiz.</p>';
}
return $this->quiz( $post );
}
/**
* Create the quiz
*
* @param WP_Post $post Quiz post type
*
* @return array Returns the quiz with processed questions and answers
*/
public function quiz( WP_Post $post ) {
// prevent content filtering anywhere in the function
remove_filter( 'the_content', array( $this, 'quiz_content' ), 10 );
// fix alt attribute escaping
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'texturise_atts' ), 10, 2 );
// main quiz config
$config = array(
'id' => 'quiz-' . $post->ID,
'title' => get_the_title( $post->ID )
);
$result_title = get_post_meta( $post->ID, 'result_title', true );
// add template modifications for the api data
$config[ 'templates' ] = array();
$config[ 'templates' ][ 'result' ] = '
<div class="quizzlestick-result-text">
<h3>' . (!empty($result_title) ? $result_title : __('Results')) . '</h3>
{{helpers.getresult}}
</div>
{{templates.share}}';
// fetch necessary elements to create our config
$meta = get_post_meta( $post->ID );
// handle type mapping
$type = $this->extract_meta( 'type', $meta );
$config[ 'type' ] = $type;
if ( in_array( $type, array( /*'quickfire',*/ 'single' ) ) ) {
$config[ 'type' ] = 'single';
}
// additional config
switch( $type ) {
//case 'quickfire':
// $config[ 'classname' ] = 'quizzlestick-quickfire';
// $config[ 'nextdelay' ] = apply_filters( 'quizzlestick-quickfire-delay', 500, $post->ID );
// $config[ 'templates' ][ 'answer' ] = '
// <li class="quizzlestick-answer">
// <a href="#">
// {{answer}}
// </a>
// </li>';
// $config[ 'templates' ][ 'question' ] = '
// <li class="quizzlestick-question">
// {{question}}
// <ul class="quizzlestick-answers">
// {{answers}}
// </ul>
// {{templates.toolbar}}
// <div class="quizzlestick-result quizzlestick-result-question quizzlestick-hidden"></div>
// </li>';
// break;
case 'single':
break;
case 'which':
break;
//case 'poll':
// $config[ 'nextdelay' ] = apply_filters( 'quizzlestick-quickfire-delay', 750, $post->ID );
// $config[ 'templates' ][ 'scaffold' ] = '
// <div class="quizzlestick-description">
// {{templates.description}}
// </div>
// <div class="quizzlestick-timer">
// {{templates.timer}}
// </div>
// <div class="quizzlestick-start-screen">
// {{templates.timerstart}}
// </div>
// <ol class="quizzlestick-questions">
// {{templates.questions}}
// </ol>
// {{templates.toolbar}}
// <div class="quizzlestick-result quizzlestick-result-final quizzlestick-hidden">
// {{templates.result}}
// </div>';
// $config[ 'templates' ][ 'answer' ] = '
// <li class="quizzlestick-answer">
// <a href="#">
// {{answer}}
// </a>
// <span class="quizzlestick-answer-total">{{total}}</span>
// </li>';
// break;
}
// get general config
$config[ 'description' ] = apply_filters( 'the_content', $post->post_excerpt );
//$config[ 'timelimit' ] = intval( $this->extract_meta( 'timelimit', $meta ) );
// process questions and answers
$questions = array();
$meta_questions = $this->extract_meta( 'questions', $meta );
if ( $meta_questions ) {
foreach( $meta_questions as $i => &$question ) {
foreach( $question[ 'answers' ] as $j => &$answer ) {
if ( ! empty( $answer[ 'answer' ] ) ) {
$answer[ 'answer' ] = '<div class="quizzlestick-answer-text">' . $answer[ 'answer' ] . '</div>';
$answer_has_text = true;
} else {
$answer_has_text = false;
}
if ( isset( $answer[ 'image' ] ) && $answer[ 'image' ] ) {
$answer_has_image = true;
$answer_image_size = apply_filters( 'answer_image_size', 'large', $answer, $post );
if( $answer_has_text ) {
$class = array( 'class' => 'quizzlestick-answer-image' );
} else {
$class = array( 'class' => 'quizzlestick-answer-image no-text' );
}
$answer_image = wp_get_attachment_image( $answer[ 'image' ], $answer_image_size, false, $class );
$answer[ 'answer' ] .= '<div class="quizzlestick-answer-image-wrap">' . $answer_image . '</div>';
} else {
$answer_has_image = false;
}
$total = $this->extract_meta( "total_{$i}_{$j}", $meta );
if ( $total )
$answer[ 'total' ] = $total;
//if ( $type === 'poll' )
// $answer[ 'correct' ] = false;
$answer[ 'answer' ] = apply_filters( 'quizzlestick_answer', $answer[ 'answer' ], $answer, $config, $post );
}
if ( ! empty( $question[ 'question' ] ) )
$question[ 'question' ] = '<div class="quizzlestick-question-text">' . $question[ 'question' ] . '</div>';
if ( isset( $question[ 'image' ] ) && $question[ 'image' ] ) {
$question_image_size = apply_filters( 'question_image_size', 'large', $question, $post );
$question_image = wp_get_attachment_image( $question[ 'image' ], $question_image_size, false, array( 'class' => 'quizzlestick-question-image' ) );
$question[ 'question' ] .= '<div class="quizzlestick-question-image-wrap">' . $question_image . '</div>';
}
// edit question result template?
//if ( $type === 'poll' ) {
//
//}
// filter question
$question[ 'question' ] = apply_filters( 'quizzlestick_question', $question[ 'question' ], $question, $config, $post );
$questions[ $i ] = $question;
}
$config[ 'questions' ] = $questions;
}
// results
$meta_result = apply_filters( 'quizzlestick_result', $this->extract_meta( 'result', $meta ), $config, $post );
$result_description = apply_filters( 'quizzlestick_result', $this->extract_meta( 'result', $meta ), $config, $post );
$result_introduction = apply_filters( 'quizzlestick_result_introduction', $this->extract_meta( 'result_introduction', $meta ), $config, $post );
$result_heading = apply_filters( 'quizzlestick_result_heading', $this->extract_meta( 'result_heading', $meta ), $config, $post );
$result_introduction = !empty( $result_introduction ) ? $result_introduction : __('Here are your results');
$result_heading = !empty( $result_heading ) ? $result_heading : __('You scored ...');
$result_description = !empty( $result_description ) ? $result_description : __('Congratulations on your score of <strong>{{state.points}}</strong> out of <strong>{{state.maxpoints}}</strong>!');
// Graduated results
$meta_results = $this->extract_meta( 'results', $meta );
if ( is_array( $meta_results ) && ! empty( $meta_results ) ) {
$results = array();
foreach( $meta_results as $result ) {
$template = '';
if ( $result[ 'image' ] ) {
$result_image_size = apply_filters( 'result_image_size', 'large', $result, $post );
$result_image = wp_get_attachment_image( $result[ 'image' ], $result_image_size, false, array( 'class' => 'quizzlestick-result-image' ) );
$result[ 'result' ] = '<div class="quizzlestick-result-image-wrap">' . $result_image . '</div> ' . $result[ 'result' ];
}
$result[ 'result' ] = apply_filters( 'quizzlestick_graduated_result', $result[ 'result' ], $result, $meta_result, $config, $post );
if ( is_string( $result[ 'result' ] ) && ! empty( $result[ 'result' ] ) ) {
// Add in parts here
if( !empty($result['short']) ) {
$template = '<div class="quizzlestick-result-intro">' . $result['short'] . '</div> ';
} else {
if( !empty( $result_introduction ) ) {
$template = '<div class="quizzlestick-result-intro">' . $result_introduction . '</div> ';
} else {
$template = "";
}
}
// We only want the score if it's not a Which is quiz
if( $type != 'which' ) {
$template .= '<div class="quizzlestick-result-score"><span class="heading">' . $result_heading . '</span><span class="numeric-results"><span class="score">{{state.points}}</span>/<span class="total">{{state.maxpoints}}</span></span></div>';
}
if( !empty( $result[ 'result' ] ) ) {
$template .= '<div class="quizzlestick-result-description">' . $result[ 'result' ] . '</div>';
} else {
if( !empty( $result_description ) ) {
$template .= '<div class="quizzlestick-result-description">' . $result_description . '</div>';
}
}
} else {
// This should never be hit due to defaults
$template = $meta_result;
}
$result[ 'template' ] = $template;
unset( $result[ 'result' ] );
$results[] = $result;
}
$config[ 'results' ] = $results;
} elseif ( is_string( $result_description ) ) {
// Add in the parts here
if( !empty( $result_introduction ) ) {
$template = '<div class="quizzlestick-result-intro">' . $result_introduction . '</div> ';
} else {
$template = "";
}
if( $type != 'which' /*&& $type != 'poll'*/ ) {
$template .= '<div class="quizzlestick-result-score"><span class="heading">' . $result_heading . '</span><span class="numeric-results"><span class="score">{{state.points}}</span>/<span class="total">{{state.maxpoints}}</span></span></div>';
}
//if( $type == 'poll' ) {
// $pollquestions = get_post_meta( $post->ID, 'questions', true );
// if( isset( $pollquestions[0]['answers'] ) && !empty( $pollquestions[0]['answers'] ) ) {
// $pollanswers = array();
// foreach( $pollquestions[0]['answers'] as $key => $pollanswer ) {
// $pollresult = get_post_meta( $post->ID, 'total_0_' . $key, true );
// $pollresults[$key] = ( !empty($pollresult) ) ? $pollresult : 0;
// }
//
// $totalresults = array_sum( $pollresults );
//
// $template .= '<div class="quizzlestick-result-score quizzlestick-poll-results" data-total="' . $totalresults . '">';
// if( $totalresults == 0) $totalresults = 1; // removes divide by zero below
// // And now loop again
// foreach( $pollquestions[0]['answers'] as $key => $pollanswer ) {
//
// $template .= '<div class="quizzlestick-poll-result-answer" id="quizzlestick-poll-result-answer-' . $key . '">';
// $template .= '<span class="answer-title">' . strip_tags( $pollanswer['answer'] ) . '</span>';
// $template .= '<div class="answer-bar-holder" id="answer-bar-holder-' . $key . '">';
// $template .= '<div class="answer-bar" data-total="' . $pollresults[$key] . '" data-percentagetotal="' . (int)( (100 / $totalresults) * (int)$pollresults[$key] ) . '" style="width: ' . (int)( (100 / $totalresults) * (int)$pollresults[$key] ) . '%;">';
// $template .= '</div>';
// $template .= '<span class="answer-value" id="answer-value-' . $key . '">' . (int)( (100 / $totalresults) * (int)$pollresults[$key] ) . '%</span>';
// $template .= '</div>';
// $template .= '</div>';
//
// }
// $template .= '</div>';
//
// }
//}
if( !empty( $result_description ) ) {
$template .= '<div class="quizzlestick-result-description">' . $result_description . '</div>';
}
$config[ 'results' ] = $template;
}
// templates
if ( $correct = $this->extract_meta( 'correct', $meta ) ) {
$config[ 'templates' ][ 'correct' ] = '<div class="quizzlestick-response-correct">' . $correct . '</div>';
}
if ( $incorrect = $this->extract_meta( 'incorrect', $meta ) ) {
$config[ 'templates' ][ 'incorrect' ] = '<div class="quizzlestick-response-incorrect">' . $incorrect . '</div>';
}
// allow modification for share template etc...
$config = apply_filters( 'quizzlestick_config', $config, $post );
// add filter back
add_filter( 'the_content', array( $this, 'quiz_content' ), 10 );
// remove filter
remove_filter( 'wp_get_attachment_image_attributes', array( $this, 'texturise_atts' ), 10 );
$output = '
<div id="quizzlestick-' . $post->ID . '"></div>
<script>(function($){$("#quizzlestick-' . $post->ID . '").quizzlestick(' . json_encode( $config, JSON_UNESCAPED_SLASHES ). ')})(jQuery)</script>';
return $output;
}
public function texturise_atts( $attr, $attachment ) {
$attr[ 'alt' ] = wptexturize( $attr[ 'alt' ] );
$attr[ 'alt' ] = convert_chars( $attr[ 'alt' ] );
return $attr;
}
/**
* Populates the content of a quiz post with the quiz HTML tag
*
* @param string $content The post content
*
* @return string
*/
public function quiz_content( $content ) {
global $post, $wp_query;
if ( $content === '' && ! is_admin() && $post->post_type === $this->post_type )
$content = $this->quiz( $post );
return $content;
}
public function meta_boxes( $post_type, WP_Post $post ) {
$type = get_post_meta( $post->ID, 'type', true );
// settings
add_meta_box( 'settings', __( 'Settings' ), array( $this, 'meta_box_settings' ), $this->post_type, 'normal', 'default' );
// questions
add_meta_box( 'questions', __( 'Questions' ), array( $this, 'meta_box_questions' ), $this->post_type, 'normal', 'default' );
// results
add_meta_box( 'results', __( 'Results' ), array( $this, 'meta_box_results' ), $this->post_type, 'normal', 'default' );
// Add in our poll results metabox
//if( $type == 'poll' ) {
// add_meta_box( 'pollresults', __( 'Poll Results' ), array( $this, 'meta_box_poll_results' ), $this->post_type, 'normal', 'default' );
//}
// embed box
add_meta_box( 'embed', __( 'Embed' ), array( $this, 'meta_box_embed' ), $this->post_type, 'side', 'default' );
// remove excerpt box
remove_meta_box( 'postexcerpt', $this->post_type, 'normal' );
// remove featured image box
remove_meta_box( 'postimagediv', $this->post_type, 'side' );
}
public function meta_box_textarea( WP_Post $post, $meta_box ) {
icit_fields::field_textarea( array(
'name' => 'description',
'value' => get_post_meta( $post->ID, $meta_box[ 'args' ][ 'name' ], true ),
'description' => $meta_box[ 'args' ][ 'description' ],
'tiny_mce' => true,
'edit_args' => array(
'media_buttons' => true,
'teeny' => true,
'textarea_rows' => 2,
'wpautop' => true
)
) );
}
public function meta_box_settings( WP_Post $post, $meta_box ) {
$type = get_post_meta( $post->ID, 'type', true );
echo '
<table class="form-table">
<tbody>';
echo '
<tr class="quiz-field quiz-field-radio">
<th><label>' . __( 'Type' ) . '</label></th>
<td>';
icit_fields::field_radio( array(
'name' => 'type',
'value' => $type,
'options' => apply_filters( 'quizzlestick_question_types', array(
//'quickfire' => __( 'Quickfire <span class="description"> quizzes submit answers immediately on click and automatically progress to the next question if available.</span><br /><br />' ),
'single' => __( 'Single Answer <span class="description"> quizzes submit answers immediately on click and show the question result and a \'next\' button.</span><br /><br />' ),
//'multi' => __( 'Multiple Answers <span class="description"> quizzes can have more than one correct answer per question which all have to be selected to get the question right.</span><br /><br />' ),
'which' => __( 'Which are you / is it? <span class="description"> quizzes have no incorrect answers but must use graduated results based on the points scored.</span><br /><br />' ),
//'poll' => __( 'Poll <span class="description"> are a single questions the results of which are shown immediately after.</span>' )
) ),
'description' => __( 'Choose the type of quiz' ),
'default' => 'single'
) );
//<span class="instruction-quickfire">Quickfire quizzes submit answers immediately on click and automatically progress to the next question if available.</span><br />
// <span class="instruction-single">Single answer quizzes submit answers immediately on click and show the question result and a \'next\' button.</span><br />
// <span class="instruction-multi">Multiple answer quizzes can have more than one correct answer per question which all have to be selected to get the question right.</span><br />
// <span class="instruction-which">Which are you / is it quizzes have no incorrect answers but must use graduated results based on the points scored.</span><br />
// <span class="instruction-poll">Polls are a single questions the results of which are shown immediately after.</span>
echo '
</td>
</tr>';
echo '
<tr class="quiz-field quiz-field-radio">
<th><label>' . __( 'Description' ) . '</label></th>
<td>';
icit_fields::field_textarea( array(
'name' => 'excerpt',
'value' => html_entity_decode( $post->post_excerpt ),
'description' => '<p>' . __( 'If the quiz needs an introduction or instructions add them here' ) . '</p>',
'tiny_mce' => true,
'edit_args' => array(
'media_buttons' => false,
'teeny' => true,
'textarea_rows' => 2,
'wpautop' => true
)
) );
echo '
</td>
</tr>';
if( !apply_filters( 'quizzlestick_enable_timelime', false ) ) {
echo '<input type="hidden" name="timelimit" value="0" />';
} //else {
//if ( $type !== 'poll' ) {
//echo '
//<tr class="quiz-field quiz-field-number">
// <th><label>' . __( 'Time limit (seconds)' ) . '</label></th>
// <td>';
//
//icit_fields::field_numeric( array(
// 'name' => 'timelimit',
// 'value' => get_post_meta( $post->ID, 'timelimit', true ),
// 'description' => __( 'The time limit for this quiz in seconds. Zero means there is no time limit.' ),
// 'default' => 0,
// 'min' => 0
//) );
//
//echo '
// </td>
//</tr>';
//}
//}
//if ( $type == 'quickfire' ) {
// echo '
// <tr class="quiz-field quiz-field-number">
// <th><label>' . __( 'Delay before next question (miliseconds)' ) . '</label></th>
// <td>';
//
// icit_fields::field_numeric( array(
// 'name' => 'nextdelay',
// 'value' => get_post_meta( $post->ID, 'nextdelay', true ),
// 'description' => __( 'The delay before moving to the next question after answering default is 500 miliseconds.' ),
// 'default' => 500,
// 'min' => 0
// ) );
//
// echo '
// </td>
// </tr>';
//}
//echo '
//<tr class="quiz-field quiz-field-number">
// <th><label>' . __( 'Next question delay (seconds)' ) . '</label></th>
// <td>';
//
//icit_fields::field_numeric( array(
// 'name' => 'nextdelay',
// 'value' => get_post_meta( $post->ID, 'nextdelay', true ),
// 'description' => __( 'If set then the quiz will automatically advance after a question has been answered. You can set this to a decimal value eg 0.5' ),
// 'default' => 0,
// 'min' => 0
//) );
//
//echo '
// </td>
//</tr>';
if ( ! in_array( $type, array( 'which'/*, 'poll'*/ ) ) ) {
echo '
<tr class="quiz-field quiz-field-number">
<th><label>' . __( 'Text to show for correct answer' ) . '</label></th>
<td>';
$correct = get_post_meta( $post->ID, 'correct', true );
icit_fields::field_text( array(
'name' => 'correct',
'value' => $correct ? $correct : __( 'Correct!' ),
'default' => __( 'Correct!' )
) );
echo '
</td>
</tr>';
echo '
<tr class="quiz-field quiz-field-number">
<th><label>' . __( 'Text to show for incorrect answer' ) . '</label></th>
<td>';
$incorrect = get_post_meta( $post->ID, 'incorrect', true );
icit_fields::field_text( array(
'name' => 'incorrect',
'value' => $incorrect ? $incorrect : __( 'Wrong!' ),
'default' => __( 'Wrong!' )
) );
echo '
</td>
</tr>';
}
do_action( 'quiz_settings_metabox', $post, $meta_box );
echo '
</tbody>
</table>';
echo '<p><input type="submit" class="button button-primary" name="updatesettings" value="' . __( 'Update settings' ) . '" /></p>';
}
public function meta_box_questions( WP_Post $post, $meta_box ) {
$quiz_type = get_post_meta( $post->ID, 'type', true );
$questions = get_post_meta( $post->ID, 'questions', true );
if ( ! $questions )
$questions = array(
array()
);
// add empty one for js
$questions[ '__i__' ] = array();
$question_num = 0;
$qid = $aid = 0;
foreach( $questions as $i => $question ) {
if ( $i !== '__i__' )
$qid = $i;
// only 1 question for polls
//if ( $quiz_type === 'poll' && $i > 0 )
// break;
$question = wp_parse_args( $question, array(
'question' => '',
'image' => false,
'result' => '',
'resultcorrect' => '',
'resultincorrect' => '',
'answers' => array( array() ),
'total' => 0
) );
echo '
<div class="postbox question-field"' . ( $i === '__i__' ? ' style="display:none" id="question-template"' : '' ) . '>
<table class="form-table">
<tbody>';
// question content
// input & or image
echo '
<tr>
<th class="quiz-field"><label>' . __( 'Question' ) . ' ' . (++$question_num) . '</label><br /> <span class="description">' . __( 'You can also use an embeddable URL' ) . '</span></th>
<td>';
icit_fields::field_text( array(
'name' => "questions[$i][question]",
'value' => $question[ 'question' ],
'class' => 'widefat'
) );
icit_fields::field_image( array(
'name' => "questions[$i][image]",
'value' => $question[ 'image' ],
'size' => apply_filters( 'question_image_size', 'large', $question, $post ),
'post_parent' => $post->ID
) );
echo '
</td>
</tr>';
if ( ! in_array( $quiz_type, array( 'which'/*, 'poll'*/ ) ) ) {
echo '
<tr>
<th><label>' . __( 'Result text (optional)' ) . '</label></th>
<td>';
echo '<div class="question-result">';
echo '<div class="question-result-default">';
icit_fields::field_textarea( array(
'name' => "questions_{$i}_result",
'value' => $question[ 'result' ],
'tiny_mce' => $i === '__i__' ? false : true,
'edit_args' => array(
'media_buttons' => false,
'teeny' => true,
'textarea_rows' => 2,
'wpautop' => true
)
) );
echo '</div>';
echo '<div class="question-result-correct hide-if-js"><label>' . __( 'Result text if correct (optional)' ) . '</label>';
icit_fields::field_textarea( array(
'name' => "questions_{$i}_resultcorrect",
'value' => $question[ 'resultcorrect' ],
'tiny_mce' => $i === '__i__' ? false : true,
'edit_args' => array(
'media_buttons' => false,
'teeny' => true,
'textarea_rows' => 2,
'wpautop' => true
)
) );
echo '</div>';
echo '<div class="question-result-incorrect hide-if-js"><label>' . __( 'Result text if incorrect (optional)' ) . '</label>';
icit_fields::field_textarea( array(
'name' => "questions_{$i}_resultincorrect",
'value' => $question[ 'resultincorrect' ],
'tiny_mce' => $i === '__i__' ? false : true,
'edit_args' => array(
'media_buttons' => false,
'teeny' => true,
'textarea_rows' => 2,
'wpautop' => true
)
) );
echo '</div>';
echo '</div>';
echo '
</td>
</tr>';
}
echo '
<tr class="question-answers">
<th><label>Answers</label></th>
<td>';
// add empty answer template for js
if ( $i === '__i__' )
$question[ 'answers' ] = array( '__j__' => array() );
foreach( $question[ 'answers' ] as $j => $answer ) {
if ( $j !== '__j__' )
$aid = $j;
$answer = wp_parse_args( $answer, array(
'answer' => '',
'image' => false,
'points' => 0,
'correct' => false,
'result' => '',
'resultcorrect' => '',
'resultincorrect' => '',
'total' => 0
) );
echo '
<div class="postbox answer-field"' . ( $j === '__j__' ? ' id="answer-template"' : '' ) . '>
<table class="form-table">
<tbody>';
// answer content
// input & or image
echo '
<tr>
<th><label>' . __( 'Answer' ) . '</label><br /> <span class="description">' . __( 'You can also use an embeddable URL' ) . '</span></th>
<td>';
icit_fields::field_text( array(
'name' => "questions[$i][answers][$j][answer]",
'value' => $answer[ 'answer' ],
'class' => 'widefat'
) );
do_action( 'quizzlestick_answers_preimage', $i, $j, $question, "questions[$i][answers][$j][answer]", $post );
icit_fields::field_image( array(
'name' => "questions[$i][answers][$j][image]",
'value' => $answer[ 'image' ],
'size' => apply_filters( 'answer_image_size', 'large', $answer, $post ),
'post_parent' => $post->ID
) );
echo '
</td>
</tr>';
do_action( 'quizzlestick_answers_postimage', $i, $j, $question, "questions[$i][answers][$j][answer]", $post );
//if( $quiz_type == 'poll' ) {
//
// echo '
// <tr>
// <th><label>' . __( 'Long Answer' ) . '</label><br /> <span class="description">' . __( 'For a longer, more complete answer' ) . '</span></th>
// <td>';
//
// icit_fields::field_textarea( array(
// 'name' => "answer_{$j}_answer_description",
// 'value' => $question[ 'answers' ][$j]['description'],
// 'tiny_mce' => $i !== '__i__',
// 'edit_args' => array(
// 'media_buttons' => false,
// 'teeny' => true,
// 'textarea_rows' => 4,
// 'wpautop' => true
// )
// ) );
//
// echo '
// </td>
// </tr>';
//
//} else {
if ( $quiz_type !== 'which' ) {
echo '
<tr>
<th><label>' . __( 'Correct answer?' ) . '</label></th>
<td>';
icit_fields::field_boolean( array(
'name' => "questions[$i][answers][$j][correct]",
'value' => $answer[ 'correct' ]
) );