Created
April 14, 2023 12:34
-
-
Save adczk/1d9b789f302d1a38dbc7daeb12034898 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [Forminator] redirect knowledge quiz based on score | |
| * Plugin URI: https://premium.wpmudev.org/ | |
| * Description: redirect knowledge quiz based on score; a bit "tricky" setup but seems to wrok fine | |
| * Author: adczk (with adjustments of Prashant Singh) | |
| * Author URI: https://premium.wpmudev.org/ | |
| * License: GPLv2 or later | |
| * | |
| * Tested with Forminator 1.23.3 | |
| * | |
| * use as MU plugin | |
| * | |
| * config explained in code comment | |
| * | |
| * | |
| */ | |
| add_action( 'after_setup_theme', 'wpmudev_forminator_quizes_redirect_results_func', 100 ); | |
| function wpmudev_forminator_quizes_redirect_results_func() { | |
| if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
| add_filter( 'forminator_quizzes_render_knowledge_result', 'wpmu_forminator_quizzes_redirect_knowledge' , 10, 3 ); | |
| function wpmu_forminator_quizzes_redirect_knowledge( $result_html, $text, $model ) { | |
| $quizzes = array( 16385 ); // quizzes IDs, comma separated | |
| $redirect_default = "https//github.com"; // default redirect | |
| // redirects for other results (number is maximum percentage for given redirect) | |
| // so e.g. 25 means redirect to google will go if percentage is lower than 25%; | |
| $redirects = array( | |
| '25' => 'https://google.com', | |
| '90' => 'https://wpmudev.com', | |
| ); | |
| $open_new_tab = false; // if to open redirect url in new tab | |
| $delay_time = 2000;//delay 2s | |
| $result_phrase = "you got \d+\/\d+ correct"; //the test set in "Final Count Message" | |
| $delimiter = "/"; //must much character between right answers and total answers number in messge (as set above) | |
| if ( !in_array( $model->id, $quizzes ) ) { | |
| return $result_html; | |
| } | |
| $matches = array(); | |
| $results = array(); | |
| $result_phrase = "/" . $result_phrase . "/i"; | |
| if ( preg_match( $result_phrase, $result_html, $matches ) ) { | |
| preg_match( "/\d+\/\d+/", $matches[0], $results ); | |
| $result = explode( $delimiter, $results[0]); | |
| $right = $result[0]; | |
| $total = $result[1]; | |
| // min/max boundaries to 0-100 - not really neccessary but in case of | |
| // unexpected issues and percentage going over 100 or below 0; | |
| $percentage = max( min( round( ( $right * 100) / $total ), 100 ), 0 ); | |
| krsort( $redirects, 1); | |
| foreach ( $redirects as $percent=>$url ) { | |
| if ( $percentage < $percent ) { | |
| $redirect_default = $url; | |
| } | |
| } | |
| } | |
| if( $open_new_tab ){ | |
| $result_html .= sprintf('<script> | |
| setTimeout(function(){ | |
| window.open( "%s", "_blank"); | |
| }, %d) | |
| </script>', $redirect_default, $delay_time ); | |
| }else{ | |
| $result_html .= sprintf('<script> | |
| setTimeout(function(){ | |
| window.location.href= "%s"; | |
| }, %d) | |
| </script>', $redirect_default, $delay_time ); | |
| } | |
| return $result_html; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment