← All changes
|
integrations/thrive-quiz-builder/includes/requirements.php
+113
-0
7.9.2
→
8.0.5
View file →
| @@ -1,0 +1,113 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Requirements | |
| 4 | + * | |
| 5 | + * @package GamiPress\Thrive_Quiz_Builder\Requirements | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Add custom fields to the requirement object | |
| 14 | + * | |
| 15 | + * @param $requirement | |
| 16 | + * @param $requirement_id | |
| 17 | + * | |
| 18 | + * @return array | |
| 19 | + */ | |
| 20 | +function gamipress_thrive_quiz_builder_requirement_object( $requirement, $requirement_id ) { | |
| 21 | + | |
| 22 | + if( isset( $requirement['trigger_type'] ) | |
| 23 | + && ( $requirement['trigger_type'] === 'gamipress_thrive_quiz_builder_complete_percentage_quiz' | |
| 24 | + || $requirement['trigger_type'] === 'gamipress_thrive_quiz_builder_complete_specific_percentage_quiz' ) ) { | |
| 25 | + | |
| 26 | + // Percentage | |
| 27 | + $requirement['thrive_quiz_builder_percentage_condition'] = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage_condition', true ); | |
| 28 | + $requirement['thrive_quiz_builder_percentage'] = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage', true ); | |
| 29 | + | |
| 30 | + } | |
| 31 | + | |
| 32 | + if( isset( $requirement['trigger_type'] ) | |
| 33 | + && ( $requirement['trigger_type'] === 'gamipress_thrive_quiz_builder_complete_quiz_type' ) ) { | |
| 34 | + | |
| 35 | + // Quiz type | |
| 36 | + $requirement['thrive_quiz_builder_quiz_type'] = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_quiz_type', true ); | |
| 37 | + | |
| 38 | + } | |
| 39 | + | |
| 40 | + return $requirement; | |
| 41 | +} | |
| 42 | +add_filter( 'gamipress_requirement_object', 'gamipress_thrive_quiz_builder_requirement_object', 10, 2 ); | |
| 43 | + | |
| 44 | +/** | |
| 45 | + * Category field on requirements UI | |
| 46 | + * | |
| 47 | + * @param $requirement_id | |
| 48 | + * @param $post_id | |
| 49 | + */ | |
| 50 | +function gamipress_thrive_quiz_builder_requirement_ui_fields( $requirement_id, $post_id ) { | |
| 51 | + | |
| 52 | + $percentage_conditions = gamipress_thrive_quiz_builder_get_percentage_conditions(); | |
| 53 | + $percentage_condition = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage_condition', true ); | |
| 54 | + $percentage = absint( get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage', true ) ); | |
| 55 | + | |
| 56 | + $quiz_types = gamipress_thrive_quiz_builder_get_quiz_types(); | |
| 57 | + $quiz_type = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_quiz_type', true ); | |
| 58 | + ?> | |
| 59 | + | |
| 60 | + <span class="thrive-quiz-builder-quiz-percentage"> | |
| 61 | + <select> | |
| 62 | + <?php foreach( $percentage_conditions as $id => $name ) : ?> | |
| 63 | + <option value="<?php echo esc_attr( $id ); ?>" <?php selected( $percentage_condition, $id ); ?>><?php echo esc_html( $name ); ?></option> | |
| 64 | + <?php endforeach; ?> | |
| 65 | + </select> | |
| 66 | + <input type="text" value="<?php echo esc_attr( $percentage ); ?>" size="3" maxlength="3" placeholder="100" />% | |
| 67 | + </span> | |
| 68 | + | |
| 69 | + <span class="thrive-quiz-builder-quiz-type"> | |
| 70 | + <select> | |
| 71 | + <?php foreach( $quiz_types as $id => $name ) : ?> | |
| 72 | + <option value="<?php echo esc_attr( $id ); ?>" <?php selected( $quiz_type, $id ); ?>><?php echo esc_html( $name ); ?></option> | |
| 73 | + <?php endforeach; ?> | |
| 74 | + </select> | |
| 75 | + </span> | |
| 76 | + | |
| 77 | + <?php | |
| 78 | +} | |
| 79 | +add_action( 'gamipress_requirement_ui_html_after_achievement_post', 'gamipress_thrive_quiz_builder_requirement_ui_fields', 10, 2 ); | |
| 80 | + | |
| 81 | +/** | |
| 82 | + * Custom handler to save the custom fields on requirements UI | |
| 83 | + * | |
| 84 | + * @param $requirement_id | |
| 85 | + * @param $requirement | |
| 86 | + */ | |
| 87 | +function gamipress_thrive_quiz_builder_ajax_update_requirement( $requirement_id, $requirement ) { | |
| 88 | + | |
| 89 | + $requirement = wp_parse_args( $requirement, array( | |
| 90 | + 'thrive_quiz_builder_percentage_condition' => '', | |
| 91 | + 'thrive_quiz_builder_percentage' => '', | |
| 92 | + 'thrive_quiz_builder_quiz_type' => '', | |
| 93 | + ) ); | |
| 94 | + | |
| 95 | + if( isset( $requirement['trigger_type'] ) | |
| 96 | + && ( $requirement['trigger_type'] === 'gamipress_thrive_quiz_builder_complete_percentage_quiz' | |
| 97 | + || $requirement['trigger_type'] === 'gamipress_thrive_quiz_builder_complete_specific_percentage_quiz' ) ) { | |
| 98 | + | |
| 99 | + // Percentage | |
| 100 | + update_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage_condition', $requirement['thrive_quiz_builder_percentage_condition'] ); | |
| 101 | + update_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage', $requirement['thrive_quiz_builder_percentage'] ); | |
| 102 | + | |
| 103 | + } | |
| 104 | + | |
| 105 | + if( isset( $requirement['trigger_type'] ) | |
| 106 | + && ( $requirement['trigger_type'] === 'gamipress_thrive_quiz_builder_complete_quiz_type' ) ) { | |
| 107 | + | |
| 108 | + // Quiz type | |
| 109 | + update_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_quiz_type', $requirement['thrive_quiz_builder_quiz_type'] ); | |
| 110 | + | |
| 111 | + } | |
| 112 | +} | |
| 113 | +add_action( 'gamipress_ajax_update_requirement', 'gamipress_thrive_quiz_builder_ajax_update_requirement', 10, 2 ); | |