' . esc_html__( 'Please view this post in your web browser to complete the quiz.', 'jetpack' ) . '
';
}
/**
* Check if we're in WordPress.com.
*
* @since 4.5.0
*
* @return bool
*/
public static function is_wpcom() {
return defined( 'IS_WPCOM' ) && IS_WPCOM;
}
/**
* Parse shortcode arguments and render its output.
*
* @since 4.5.0
*
* @param array $atts Shortcode parameters.
* @param string $content Content enclosed by shortcode tags.
*
* @return string
*/
public static function shortcode( $atts, $content = null ) {
// There's nothing to do if there's nothing enclosed.
if ( null == $content ) {
return '';
}
$id = '';
if ( self::is_javascript_unavailable() ) {
// in an e-mail print the question and the info sentence once per question, too
self::$noscript_info_printed = false;
} else {
if ( ! self::$scripts_enqueued ) {
// lazy enqueue cannot use the wp_enqueue_scripts action anymore
self::enqueue_scripts();
self::$scripts_enqueued = true;
}
$default_atts = self::is_wpcom()
? array(
'trackid' => '',
'a8ctraining' => '',
)
: array(
'trackid' => '',
);
self::$quiz_params = shortcode_atts( $default_atts, $atts );
if ( ! empty( self::$quiz_params[ 'trackid' ] ) ) {
$id .= ' data-trackid="' . esc_attr( self::$quiz_params[ 'trackid' ] ) . '"';
}
if ( self::is_wpcom() && ! empty( self::$quiz_params[ 'a8ctraining' ] ) ) {
if ( is_null( self::$username ) ) {
self::$username = wp_get_current_user()->user_login;
}
$id .= ' data-a8ctraining="'. esc_attr( self::$quiz_params[ 'a8ctraining' ] ) . '" data-username="' . esc_attr( self::$username ) . '"';
}
}
$quiz = self::do_shortcode( $content );
return '' . $quiz . '
';
}
/**
* Strip line breaks, restrict allowed HTML to a few whitelisted tags and execute nested shortcodes.
*
* @since 4.5.0
*
* @param string $content
*
* @return mixed|string
*/
private static function do_shortcode( $content ) {
// strip autoinserted line breaks
$content = preg_replace( '#(<(?:br /|/?p)>\n?)*(\[/?[a-z]+\])(<(?:br /|/?p)>\n?)*#', '$2', $content );
// Add internal parameter so it's only rendered when it has it
$content = preg_replace( '/\[(question|answer|wrong|explanation)\]/i', '[$1 quiz_item="true"]', $content );
$content = do_shortcode( $content );
$content = wp_kses( $content, array(
'tt' => array(),
'pre' => array(),
'strong' => array(),
'i' => array(),
'br' => array(),
'img' => array( 'src' => true),
'div' => array( 'class' => true, 'data-correct' => 1, 'data-track-id' => 1, 'data-a8ctraining' => 1, 'data-username' => 1 ),
) );
return $content;
}
/**
* Render question.
*
* @since 4.5.0
*
* @param array $atts
* @param null $content
*
* @return string
*/
public static function question_shortcode( $atts, $content = null ) {
return isset( $atts['quiz_item'] )
? '' . self::do_shortcode( $content ) . '
'
: '';
}
/**
* Render correct answer.
*
* @since 4.5.0
*
* @param array $atts
* @param null $content
*
* @return string
*/
public static function answer_shortcode( $atts, $content = null ) {
if ( self::is_javascript_unavailable() ) {
return self::noscript_info();
}
return isset( $atts['quiz_item'] )
? '' . self::do_shortcode( $content ) . '
'
: '';
}
/**
* Render wrong response.
*
* @since 4.5.0
*
* @param array $atts
* @param null $content
*
* @return string
*/
public static function wrong_shortcode( $atts, $content = null ) {
if ( self::is_javascript_unavailable() ) {
return self::noscript_info();
}
return isset( $atts['quiz_item'] )
? '' . self::do_shortcode( $content ) . '
'
: '';
}
/**
* Render explanation for wrong or right answer.
*
* @since 4.5.0
*
* @param array $atts
* @param null $content
*
* @return string
*/
public static function explanation_shortcode( $atts, $content = null ) {
if ( self::is_javascript_unavailable() ) {
return self::noscript_info();
}
return isset( $atts['quiz_item'] )
? '' . self::do_shortcode( $content ) . '
'
: '';
}
}
Quiz_Shortcode::init();