wp-pro-quiz.php 12.2 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
<?php
/*
Plugin Name: WP-Pro-Quiz
Plugin URI: http://wordpress.org/extend/plugins/wp-pro-quiz
Description: A powerful and beautiful quiz plugin for WordPress.
Version: 0.28
Author: Julius Fischer
Author URI: http://www.it-gecko.de
Text Domain: wp-pro-quiz
Domain Path: /languages
// phpcs:disable WordPress.NamingConventions.ValidVariableName,WordPress.NamingConventions.ValidFunctionName,WordPress.NamingConventions.ValidHookName
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @ignore
 */
define( 'WPPROQUIZ_VERSION', '0.28' );

/**
 * @ignore
 */
define( 'WPPROQUIZ_PATH', dirname( __FILE__ ) );

/**
 * @ignore
 */
define( 'WPPROQUIZ_URL', plugins_url( '', __FILE__ ) );

/**
 * @ignore
 */
define( 'WPPROQUIZ_FILE', __FILE__ );

$wpproquiz_upload_dir = wp_upload_dir();

/**
 * @ignore
 */
define( 'WPPROQUIZ_CAPTCHA_DIR', $wpproquiz_upload_dir['basedir'] . '/wp_pro_quiz_captcha' );

/**
 * @ignore
 */
define( 'WPPROQUIZ_CAPTCHA_URL', $wpproquiz_upload_dir['baseurl'] . '/wp_pro_quiz_captcha' );

spl_autoload_register( 'wpProQuiz_autoload' );

add_action( 'plugins_loaded', 'wpProQuiz_pluginLoaded' );

if ( is_admin() ) {
	new WpProQuiz_Controller_Admin();
} else {
	new WpProQuiz_Controller_Front();
}

/**
 * Handles the wp pro quiz class autoloading.
 *
 * Callback for `spl_autoload_register` function.
 *
 * @param string $class Class name.
 */
function wpProQuiz_autoload( $class ) {
	$c = explode( '_', $class );

	if ( false === $c || count( $c ) != 3 || 'WpProQuiz' !== $c[0] ) {
		return;
	}

	$dir = '';

	switch ( $c[1] ) {
		case 'View':
			$dir = 'view';
			break;
		case 'Model':
			$dir = 'model';
			break;
		case 'Helper':
			$dir = 'helper';
			break;
		case 'Controller':
			$dir = 'controller';
			break;
		case 'Plugin':
			$dir = 'plugin';
			break;
		default:
			return;
	}

	if ( file_exists( WPPROQUIZ_PATH . '/lib/' . $dir . '/' . $class . '.php' ) ) {
		include_once WPPROQUIZ_PATH . '/lib/' . $dir . '/' . $class . '.php';
	}
}

/**
 * Runs the wp pro quiz upgrade after the plugins are loaded.
 *
 * Fires on `plugins_loaded` hook.
 */
function wpProQuiz_pluginLoaded() {

	if ( get_option( 'wpProQuiz_version' ) !== WPPROQUIZ_VERSION ) {
		WpProQuiz_Helper_Upgrade::upgrade();
	}
}

/**
 * Formats the quiz cloze type answers into an array to be used when comparing responses.
 *
 * The function is copied from `WpProQuiz_View_FrontQuiz` class.
 *
 * @since 2.5.0
 *
 * @param string  $answer_text      Answer text.
 * @param boolean $convert_to_lower Optional. Whether to convert anwser to lowercase. Default true.
 *
 * @return array An array of cloze question data.
 */
function learndash_question_cloze_fetch_data( $answer_text, $convert_to_lower = true ) {

	/**
	 * Filters the value of quiz question answer before processing.
	 *
	 * @param string $answer  The quiz question anser text.
	 * @param string $context The context of type of question.
	 */
	$answer_text = apply_filters( 'learndash_quiz_question_answer_preprocess', $answer_text, 'cloze' );

	preg_match_all( '#\{(.*?)\}#im', $answer_text, $matches, PREG_SET_ORDER );

	$data = array();

	foreach ( $matches as $k => $v ) {
		$text          = $v[1];
		$points        = array();
		$rowText       = array();
		$multiTextData = array();
		$len           = array();

		if ( preg_match_all( '#\[(.*?)\]#im', $text, $multiTextMatches ) ) {
			foreach ( $multiTextMatches[1] as $multiText ) {
				$item_points = 1;
				if ( strpos( $multiText, '|' ) !== false ) {
					list( $multiText, $item_points ) = explode( '|', $multiText );
				}
				$multiText_clean = trim( html_entity_decode( $multiText, ENT_QUOTES ) );

				$item_points = absint( $item_points );
				if ( ! $item_points ) {
					$item_points = 1;
				}

				/**
				 * Filters whether to convert quiz question cloze to lowercase or not.
				 *
				 * @param boolean $conver_to_lower Whether to convert quiz question cloze to lower case.
				 */
				if ( apply_filters( 'learndash_quiz_question_cloze_answers_to_lowercase', $convert_to_lower ) ) {
					if ( function_exists( 'mb_strtolower' ) ) {
						$x = mb_strtolower( $multiText_clean );
					} else {
						$x = strtolower( $multiText_clean );
					}
				} else {
					$x = $multiText_clean;
				}

				$len[]           = strlen( $x );
				$multiTextData[] = $x;
				$rowText[]       = $multiText;
				$points[]        = $item_points;
			}
		} elseif ( strpos( $text, '|' ) !== false ) {
			list( $multiText, $item_points ) = explode( '|', $text );

			$multiText_clean = trim( html_entity_decode( $multiText, ENT_QUOTES ) );
			$item_points     = absint( $item_points );

			/**
			 * Filters whether to convert quiz question cloze to lowercase or not.
			 *
			 * @param boolean $conver_to_lower Whether to convert quiz question cloze to lower case.
			 */
			if ( apply_filters( 'learndash_quiz_question_cloze_answers_to_lowercase', $convert_to_lower ) ) {
				if ( function_exists( 'mb_strtolower' ) ) {
					$x = mb_strtolower( $multiText_clean );
				} else {
					$x = strtolower( $multiText_clean );
				}
			} else {
				$x = $multiText_clean;
			}

			$len[]           = strlen( $x );
			$multiTextData[] = $x;
			$rowText[]       = $multiText;
			$points[]        = $item_points;

		} else {
			$item_points = ! empty( $v[2] ) ? (int) $v[2] : 1;
			$text_clean  = trim( html_entity_decode( $text, ENT_QUOTES ) );
			/** This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */
			if ( apply_filters( 'learndash_quiz_question_cloze_answers_to_lowercase', $convert_to_lower ) ) {
				if ( function_exists( 'mb_strtolower' ) ) {
					$x = mb_strtolower( trim( html_entity_decode( $text_clean, ENT_QUOTES ) ) );
				} else {
					$x = strtolower( trim( html_entity_decode( $text_clean, ENT_QUOTES ) ) );
				}
			} else {
				$x = $text_clean;
			}

			$len[]           = strlen( $x );
			$multiTextData[] = $x;
			$rowText[]       = $text;
			$points[]        = $item_points;
		}

		if ( ! isset( $data['replace'] ) ) {
			$data['replace'] = $answer_text;
		}
		$input_size = absint( max( $len ) );
		if ( $input_size < 1 ) {
			$input_size = 1;
		}

		$input_max = $input_size + 5;

		$a  = '<span class="wpProQuiz_cloze"><input autocomplete="off" data-wordlen="' . absint( $input_size ) . '" type="text" value="" size="' . absint( $input_size ) . '" maxlength="' . absint( $input_max ) . '"> ';

		$a .= '<span class="wpProQuiz_clozeCorrect" style="display: none;"></span></span>';

		$replace_key = '@@wpProQuizCloze-' . $k . '@@';

		$data['correct'][]            = $multiTextData;
		$data['points'][]             = $points;
		$data['data'][ $replace_key ] = $a;

		$pos = strpos( $data['replace'], $v[0] );
		if ( false !== $pos ) {
    		$data['replace'] = substr_replace( $data['replace'], $replace_key, $pos, strlen($v[0] ) );
		}
	}

	/**
	 * Filters the value of quiz question answer after processing.
	 *
	 * @param string $answer  The quiz question anser text.
	 * @param string $context The context of type of question.
	 */
	$data['replace'] = apply_filters( 'learndash_quiz_question_answer_postprocess', $data['replace'], 'cloze' );

	return $data;
}


function learndash_question_cloze_prepare_output( $cloze_data = array() ) {
	$cloze_output = '';

	if ( ! isset( $cloze_data['data'] ) ) {
		$cloze_data['data'] = [];
	}

	if ( ! isset( $cloze_data['replace'] ) ) {
		$cloze_data['replace'] = [];
	}

	if ( ( ! empty( $cloze_data['replace'] ) ) && ( ! empty( $cloze_data['data'] ) ) ) {
		$cloze_data['replace'] = wpautop( $cloze_data['replace'] );
		$cloze_data['replace'] = sanitize_post_field( 'post_content', $cloze_data['replace'], 0, 'display' );
		$cloze_data['replace'] = do_shortcode( $cloze_data['replace'] );

		$cloze_output = str_replace( array_keys( $cloze_data['data'] ), array_values( $cloze_data['data'] ), $cloze_data['replace'] );
	}

	return $cloze_output;
}

function learndash_question_assessment_fetch_data( $answerText, $quizId = 0, $questionId = 0 ) {

	/** This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */
	$answerText = apply_filters( 'learndash_quiz_question_answer_preprocess', $answerText, 'assessment' );

	$data = array(
		'correct'     => [],
		'points'      => [],
		'data'        => [],
		'replace'     => '',
		'answer_text' => '',
	);

	if ( ! empty( $answerText ) ) {
		$data['answer_text'] = $answerText;

		preg_match_all( '#\{(.*?)\}#im', $answerText, $matches );
		if ( ( isset( $matches[1] ) ) && ( ! empty( $matches[1] ) ) ) {
			foreach ( $matches[1] as $match_idx => $match ) {
				$a = '';

				preg_match_all( '#\[([^\|\]]+)(?:\|(\d+))?\]#im', $match, $m_values );

				if ( ( isset( $m_values[1] ) ) && ( ! empty( $m_values[1] ) ) ) {
					foreach ( $m_values[1] as $label_idx => $label ) {
						$data['correct'][ $label_idx ] = $label;

						if ( ( isset( $m_values[2][ $label_idx ] ) ) && ( '' !== $m_values[2][ $label_idx ] ) ) {
							$data['points'][ $label_idx ] = absint( $m_values[2][ $label_idx ] );
						} else {
							$data['points'][ $label_idx ] = $label_idx + 1;
						}

						$field_value = $label_idx + 1;

						/**
						 * Note: The 'data-index' value is purposely set to '0' for all answers.
						 * This si because in the wpProQuiz_front.js (3.5.0) in the function
						 * fetchAllAnswerData() the value is assigned to the data-index array
						 * position. And it needs to be in the '0' position.
						 */
						$a .= '<label><input type="radio" value="' . $field_value . '" name="question_' . $quizId . '_' . $questionId . '_' . "0" . '" class="wpProQuiz_questionInput" data-index="0">' . $data['correct'][ $label_idx ] . '</label>';
					}
				}

				$replace_key                  = '@@wpProQuizAssessment-' . $match_idx . '@@';
				$count                        = 1;
				$data['data'][ $replace_key ] = $a;
				$data['replace']              = preg_replace( '#\{(.*?)\}#im', $replace_key, $answerText, $count );
			}
		}
	}
	return $data;
}

function learndash_question_assessment_prepare_output( $assessment_data = array() ) {
	$assessment_output = '';

	if ( ! isset( $assessment_data['data'] ) ) {
		$assessment_data['data'] = [];
	}

	if ( ! isset( $assessment_data['replace'] ) ) {
		$assessment_data['replace'] = [];
	}

	if ( ( ! empty( $assessment_data['replace'] ) ) && ( ! empty( $assessment_data['data'] ) ) ) {
		$assessment = sanitize_post_field( 'post_content', $assessment_data['replace'], 0, 'display' );
		$assessment = wpautop( $assessment );
		$assessment = do_shortcode( $assessment );

		$assessment = str_replace( array_keys( $assessment_data['data'] ), array_values( $assessment_data['data'] ), $assessment );

		/** This filter is documented in includes/lib/wp-pro-quiz/wp-pro-quiz.php */
		$assessment = apply_filters( 'learndash_quiz_question_answer_postprocess', $assessment, 'assessment' );
		$assessment = do_shortcode( $assessment );

		$assessment_output = $assessment;
	}

	return $assessment_output;
}


/**
 * Casts an instance of PHP stdClass to the type of given class name.
 *
 * This function will take an instance of a PHP stdClass and attempt to cast it to
 * the type of the specified $className parameter.
 * For example, we may pass 'Acme\Model\Product' as the $className.
 *
 * @param object $instance An instance of the stdClass to cast.
 * @param string $className The name of the class type to which we want to convert.
 *
 * @return mixed The instance after casting.
 */
function learndash_cast_WpProQuiz_Model_AnswerTypes( $instance, $className ) {
	return unserialize( // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
		sprintf(
			'O:%d:"%s"%s',
			\strlen( $className ),
			$className,
			strstr( strstr( serialize( $instance ), '"' ), ':' ) // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
		)
	);
}

function learndash_question_free_get_answer_data( $data, $question = null ) {
	$question_data = array();

	$t = str_replace( "\r\n", "\n", $data->getAnswer() );
	$t = str_replace( "\r", "\n", $t );
	$t = explode( "\n", $t );

	foreach ( $t as $idx => $item ) {
		if ( strpos( $item, '|' ) !== false ) {
			list( $item_value, $item_points ) = explode( '|', $item );
		} else {
			$item_value  = trim( html_entity_decode( $item, ENT_QUOTES ) );
			$item_points = 1;
		}

		if ( '' == $item ) {
			unset( $t[ $idx ] );
			continue;
		}

		//$question_data['correct'][] = esc_attr( $item_value );
		$question_data['correct'][] = $item_value;
		$question_data['points'][]  = (int) $item_points;
	}

	return $question_data;
}