ARISTREAMQUIZ_THEME_DEFAULT, 'smart_scroll' => true, 'scroll_duration' => 600, 'scroll_offset' => 0, 'custom_styles' => '', 'show_results' => '', // empty, 'immediately', 'on_complete' 'show_questions_oncomplete' => true, 'share_trivia_title' => 'You got {{userScore}} out of {{maxScore}} correct', 'warning_on_exit' => false, ); public static function init() { register_setting( ARISTREAMQUIZ_SETTINGS_GROUP, ARISTREAMQUIZ_SETTINGS_NAME, array( __CLASS__, 'sanitize' ) ); add_settings_section( ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION, '', // Title array( __CLASS__, 'render_general_section_info' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE ); add_settings_section( ARISTREAMQUIZ_SETTINGS_SHARING_TRIVIACONTENT_SECTION, '', // Title array( __CLASS__, 'render_sharing_triviacontent_section_info' ), ARISTREAMQUIZ_SETTINGS_SHARING_PAGE ); add_settings_section( ARISTREAMQUIZ_SETTINGS_ADVANCED_SECTION, '', // Title array( __CLASS__, 'render_advanced_section_info' ), ARISTREAMQUIZ_SETTINGS_ADVANCED_PAGE ); add_settings_field( 'theme', self::format_option_name( __( 'Default theme', 'ari-stream-quiz' ), __( 'The selected theme will be used for all quizzes by default if it is not overridden in quiz settings.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_general_theme' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); add_settings_field( 'smart_scroll', self::format_option_name( __( 'Smart scroll', 'ari-stream-quiz' ), __( 'If the parameter is enabled, the extension will automatically scroll to next element (question, quiz result and etc.) during quiz session.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_general_smart_scroll' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); add_settings_field( 'scroll_duration', self::format_option_name( __( 'Scroll duration', 'ari-stream-quiz' ), __( 'The duration in milliseconds of scrolling animation.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_general_scroll_duration' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); add_settings_field( 'scroll_offset', self::format_option_name( __( 'Scroll offset', 'ari-stream-quiz' ), __( 'The defined offset in pixels will be added to final top position, useful if template contains fixed elements. Possible to use negative values.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_general_scroll_offset' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); add_settings_field( 'show_questions_oncomplete', self::format_option_name( __( 'Show questions at the end', 'ari-stream-quiz' ), __( 'If it is enabled, all questions will be shown on quiz final page otherwise questions will be hidden.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_general_show_questions_oncomplete' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); add_settings_field( 'warning_on_exit', self::format_option_name( __( 'Warning on exit', 'ari-stream-quiz' ), __( 'Warning message will be shown if a user leaves non-completed quiz.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_general_warning_on_exit' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); // Trivia quiz parameters add_settings_field( 'show_results', self::format_option_name( __( 'Show result per question', 'ari-stream-quiz' ), __( 'Specify should quiz takers see correct answers or not.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_triviaquiz_show_results' ), ARISTREAMQUIZ_SETTINGS_GENERAL_PAGE, ARISTREAMQUIZ_SETTINGS_GENERAL_SECTION ); // Sharing content - trivia quiz add_settings_field( 'share_trivia_description', '', array( __CLASS__, 'render_share_triviaquiz_description' ), ARISTREAMQUIZ_SETTINGS_SHARING_PAGE, ARISTREAMQUIZ_SETTINGS_SHARING_TRIVIACONTENT_SECTION ); add_settings_field( 'share_trivia_title', __( 'Title on result page', 'ari-stream-quiz' ), array( __CLASS__, 'render_share_triviaquiz_title' ), ARISTREAMQUIZ_SETTINGS_SHARING_PAGE, ARISTREAMQUIZ_SETTINGS_SHARING_TRIVIACONTENT_SECTION ); // Advanced parameters add_settings_field( 'custom_styles', self::format_option_name( __( 'Custom CSS styles', 'ari-stream-quiz' ), __( 'The defined CSS rules will be added on frontend pages with quizzes. Can be used to resolve style conflicts or for customization.', 'ari-stream-quiz' ) ), array( __CLASS__, 'render_advanced_custom_styles' ), ARISTREAMQUIZ_SETTINGS_ADVANCED_PAGE, ARISTREAMQUIZ_SETTINGS_ADVANCED_SECTION ); } public static function options() { if ( ! is_null( self::$options ) ) return self::$options; self::$options = get_option( ARISTREAMQUIZ_SETTINGS_NAME ); return self::$options; } public static function get_option( $name, $default = null ) { $options = self::options(); $val = $default; if ( isset( $options[$name] ) ) { $val = $options[$name]; } else if ( is_null( $default) && isset( self::$default_settings[$name] ) ) { $val = self::$default_settings[$name]; } return $val; } public static function format_option_name( $title, $tooltip = '' ) { $html = $title; if ( $tooltip ) { $html = sprintf( '%1$s', $title, esc_attr( $tooltip ) ); } return $html; } public static function render_header( $message, $class = '' ) { printf( '
%1$s
', $message, $class ); } public static function render_general_section_info() { self::render_header( __( 'Contains global parameters for configuration quizzes look\'n\'feel.', 'ari-stream-quiz' ), 'top' ); } public static function render_sharing_triviacontent_section_info() { self::render_header( __( 'This parameters section is used to configure content for quiz final page.', 'ari-stream-quiz' ), 'top' ); } public static function render_share_triviaquiz_description() { printf( '
%s
', __( 'The following predefined variables are supported: ', 'ari-stream-quiz' ) ); } public static function render_advanced_section_info() { self::render_header( __( 'This section contains advanced parameters for fine tuning of the plugin', 'ari-stream-quiz' ), 'top' ); } public static function render_general_theme() { $val = Helper::resolve_theme_name( self::get_option( 'theme' ) ); $themes = Helper::get_themes(); $html = sprintf( ''; echo $html; } public static function render_general_scroll_offset() { $val = self::get_option( 'scroll_offset' ); printf( ' %3$s', ARISTREAMQUIZ_SETTINGS_NAME, esc_attr( $val ), __( 'pixels', 'ari-stream-quiz' ) ); } public static function render_general_scroll_duration() { $val = self::get_option( 'scroll_duration' ); printf( ' %3$s', ARISTREAMQUIZ_SETTINGS_NAME, esc_attr( $val ), __( 'milliseconds', 'ari-stream-quiz' ) ); } public static function render_general_smart_scroll() { $val = self::get_option( 'smart_scroll' ); printf( '', ARISTREAMQUIZ_SETTINGS_NAME, $val ? ' checked="checked"' : '' ); } public static function render_general_show_questions_oncomplete() { $val = self::get_option( 'show_questions_oncomplete' ); printf( '', ARISTREAMQUIZ_SETTINGS_NAME, $val ? ' checked="checked"' : '' ); } public static function render_general_warning_on_exit() { $val = self::get_option( 'warning_on_exit' ); printf( '', ARISTREAMQUIZ_SETTINGS_NAME, $val ? ' checked="checked"' : '' ); } public static function render_triviaquiz_show_results() { $val = self::get_option( 'show_results' ); $html = sprintf( ''; echo $html; } public static function render_share_triviaquiz_title() { $val = self::get_option( 'share_trivia_title' ); printf( '', ARISTREAMQUIZ_SETTINGS_NAME, esc_attr( $val ) ); } public static function render_advanced_custom_styles() { $val = self::get_option( 'custom_styles' ); printf( '', ARISTREAMQUIZ_SETTINGS_NAME, esc_attr( $val ) ); } public static function sanitize( $input ) { $new_input = array(); foreach ( self::$default_settings as $key => $val ) { $type = gettype( $val ); if ( 'boolean' == $type && ! isset( $input[$key] ) ) { $new_input[$key] = false; } else if ( 'array' == $type && ! isset( $input[$key] ) ) { $new_input[$key] = array(); } else if ( isset( $input[$key] ) ) { $input_val = $input[$key]; $filtered_val = null; switch ( $type ) { case 'boolean': $filtered_val = (bool) $input_val; break; case 'integer': $filtered_val = intval( $input_val, 10 ); break; case 'double': $filtered_val = floatval( $input_val ); break; case 'array': $filtered_val = $input_val; break; case 'string': $filtered_val = trim( $input_val ); break; } if ( ! is_null( $filtered_val) ) { $new_input[$key] = $filtered_val; } } } return $new_input; } }