Skip to content

Commit

Permalink
Merge pull request #2536 from QuizandSurveyMaster/fix-display-shortco…
Browse files Browse the repository at this point in the history
…de-php-buffer

Fix display shortcode php buffer
  • Loading branch information
zubairraeen authored May 1, 2024
2 parents 151ff38 + b1d65ba commit c4a18da
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 37 deletions.
66 changes: 66 additions & 0 deletions php/classes/class-qmn-plugin-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,72 @@ public function __construct() {
add_filter( 'qsm_language_support', array( $this, 'qsm_language_support' ), 10, 3 );
}

/**
* Calls all class functions to check if quiz is setup properly
*
* @param int $quiz_id The ID of the quiz or survey to load.
* @return array An array which contains boolean result of has_proper_quiz, message and/or qmn_quiz_options
*/
public function has_proper_quiz( $quiz_id ) {
if ( empty( $quiz_id ) ) {
return array(
'res' => false,
'message' => __( 'Empty Quiz ID.', 'quiz-master-next' ),
);
}

$quiz_id = intval( $quiz_id );

// Tries to load quiz name to ensure this is a valid ID.
global $mlwQuizMasterNext, $qmn_allowed_visit, $qmn_json_data;
$qmn_json_data = array();
$qmn_allowed_visit = true;
if ( false === $this->prepare_quiz( $quiz_id ) ) {
return array(
'res' => false,
'message' => __( 'It appears that this quiz is not set up correctly.', 'quiz-master-next' ),
);
}

$has_result_id = ( ! isset( $_GET['result_id'] ) || '' === $_GET['result_id'] );

if ( $has_result_id ) {
global $mlw_qmn_quiz;
$mlw_qmn_quiz = $quiz_id;
}

$qmn_quiz_options = $mlwQuizMasterNext->quiz_settings->get_quiz_options();

if ( $has_result_id ) {
/**
* Filter Quiz Options before Quiz Display
*/
$qmn_quiz_options = apply_filters( 'qsm_shortcode_quiz_options', $qmn_quiz_options );
}

// If quiz options isn't found, stop function.
if ( is_null( $qmn_quiz_options ) || ( ! empty( $qmn_quiz_options->deleted ) && 1 == $qmn_quiz_options->deleted ) ) {
return array(
'res' => false,
'message' => __( 'This quiz is no longer available.', 'quiz-master-next' ),
);
}

// If quiz options isn't found, stop function.
if ( is_null( $qmn_quiz_options ) || empty( $qmn_quiz_options->quiz_name ) ) {
return array(
'res' => false,
'message' => __( 'It appears that this quiz is not set up correctly.', 'quiz-master-next' ),
);
}

return array(
'res' => true,
'message' => __( 'Quiz is setup properly.', 'quiz-master-next' ),
'qmn_quiz_options' => $qmn_quiz_options,
);
}

/**
* Calls all class functions to initialize quiz
*
Expand Down
51 changes: 14 additions & 37 deletions php/classes/class-qmn-quiz-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,29 +331,33 @@ public function qsm_clear_audit_data() {
* @return string The content for the shortcode
*/
public function display_shortcode( $atts ) {
global $wpdb, $mlwQuizMasterNext;
$shortcode_args = shortcode_atts(
array(
'quiz' => 0,
'question_amount' => 0,
),
$atts
);

// Quiz ID.
$quiz = intval( $shortcode_args['quiz'] );
$question_amount = intval( $shortcode_args['question_amount'] );

// Check, if quiz is setup properly.
$has_proper_quiz = $mlwQuizMasterNext->pluginHelper->has_proper_quiz( $quiz );
if ( false === $has_proper_quiz['res'] ) {
return $has_proper_quiz['message'];
}

$qmn_quiz_options = $has_proper_quiz['qmn_quiz_options'];
$return_display = '';

ob_start();
global $wpdb, $mlwQuizMasterNext;
if ( isset( $_GET['result_id'] ) && '' !== $_GET['result_id'] ) {
$result_unique_id = sanitize_text_field( wp_unslash( $_GET['result_id'] ) );
$result = $wpdb->get_row( $wpdb->prepare( "SELECT `result_id`, `quiz_id` FROM {$wpdb->prefix}mlw_results WHERE unique_id = %s", $result_unique_id ), ARRAY_A );
if ( ! empty( $result ) && isset( $result['result_id'] ) ) {
$mlwQuizMasterNext->pluginHelper->prepare_quiz( $result['quiz_id'] );
$qmn_quiz_options = $mlwQuizMasterNext->quiz_settings->get_quiz_options();

// If quiz options isn't found, stop function.
if ( is_null( $qmn_quiz_options ) || 1 == $qmn_quiz_options->deleted ) {
return __( 'This quiz is no longer available.', 'quiz-master-next' );
}

wp_enqueue_style( 'qmn_quiz_common_style', $this->common_css, array(), $mlwQuizMasterNext->version );
wp_style_add_data( 'qmn_quiz_common_style', 'rtl', 'replace' );
Expand All @@ -376,35 +380,8 @@ public function display_shortcode( $atts ) {
}
$return_display .= ob_get_clean();
} else {
global $qmn_allowed_visit;
global $qmn_json_data;
$qmn_json_data = array();
$qmn_allowed_visit = true;
$success = $mlwQuizMasterNext->pluginHelper->prepare_quiz( $quiz );
if ( false === $success ) {
return __( 'It appears that this quiz is not set up correctly', 'quiz-master-next' );
}

global $mlw_qmn_quiz;
$mlw_qmn_quiz = $quiz;
$return_display = '';
$qmn_quiz_options = $mlwQuizMasterNext->quiz_settings->get_quiz_options();
// Legacy variable.
/**
* Filter Quiz Options before Quiz Display
*/
$qmn_quiz_options = apply_filters( 'qsm_shortcode_quiz_options', $qmn_quiz_options );

// If quiz options isn't found, stop function.
if ( is_null( $qmn_quiz_options ) || 1 == $qmn_quiz_options->deleted ) {
return __( 'This quiz is no longer available.', 'quiz-master-next' );
}

// If quiz options isn't found, stop function.
if ( is_null( $qmn_quiz_options ) || empty( $qmn_quiz_options->quiz_name ) ) {
return __( 'It appears that this quiz is not set up correctly', 'quiz-master-next' );
}

global $qmn_allowed_visit, $qmn_json_data, $mlw_qmn_quiz;

// Loads Quiz Template.
wp_enqueue_style( 'qmn_quiz_animation_style', QSM_PLUGIN_CSS_URL . '/animate.css', array(), $mlwQuizMasterNext->version );
wp_enqueue_style( 'qmn_quiz_common_style', $this->common_css, array(), $mlwQuizMasterNext->version );
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ This is usually a theme conflict. You can [checkout out our common conflict solu
18. Database

== Changelog ==
= 9.0.2 ( Beta ) =
* Bug: Fixed display shortcode php buffer due to invalid quiz id

= 9.0.1 (April 25, 2024) =
* Bug: Fixed date format in %ANSWER_X% variable
* Bug: Resolved PHP warning in Quiz Block editor
Expand Down

0 comments on commit c4a18da

Please sign in to comment.