← All changes
|
integrations/thrive-quiz-builder/includes/functions.php
+141
-0
7.9.2
→
8.0.5
View file →
| @@ -1,0 +1,141 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\Thrive_Quiz_Builder\Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Overrides GamiPress AJAX Helper for selecting posts | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function gamipress_thrive_quiz_builder_ajax_get_posts() { | |
| 18 | + | |
| 19 | + // Security check, forces to die if not security passed | |
| 20 | + check_ajax_referer( 'gamipress_admin', 'nonce' ); | |
| 21 | + | |
| 22 | + // Check if user can manage GamiPress | |
| 23 | + if( ! current_user_can( gamipress_get_manager_capability() ) ) { | |
| 24 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + global $wpdb; | |
| 28 | + | |
| 29 | + $results = array(); | |
| 30 | + | |
| 31 | + // Pull back the search string | |
| 32 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; | |
| 33 | + | |
| 34 | + if( isset( $_REQUEST['post_type'] ) && in_array( 'percentage_quizzes', $_REQUEST['post_type'] ) ) { | |
| 35 | + | |
| 36 | + // Get the quizzes | |
| 37 | + $quizzes = gamipress_thrive_quiz_builder_get_percentage_quizzes(); | |
| 38 | + | |
| 39 | + foreach ( $quizzes as $quiz ) { | |
| 40 | + | |
| 41 | + // Results should meet same structure like posts | |
| 42 | + $results[] = array( | |
| 43 | + 'ID' => $quiz->ID, | |
| 44 | + 'post_title' => $quiz->post_title, | |
| 45 | + ); | |
| 46 | + | |
| 47 | + } | |
| 48 | + | |
| 49 | + // Return our results | |
| 50 | + wp_send_json_success( $results ); | |
| 51 | + die; | |
| 52 | + | |
| 53 | + } | |
| 54 | + | |
| 55 | +} | |
| 56 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_thrive_quiz_builder_ajax_get_posts', 5 ); | |
| 57 | + | |
| 58 | +/** | |
| 59 | + * Get percentage conditions | |
| 60 | + * | |
| 61 | + * @since 1.0.0 | |
| 62 | + * | |
| 63 | + * @return array | |
| 64 | + */ | |
| 65 | +function gamipress_thrive_quiz_builder_get_percentage_conditions() { | |
| 66 | + | |
| 67 | + return array( | |
| 68 | + 'equal' => __( 'equal to', 'gamipress'), | |
| 69 | + 'not_equal' => __( 'not equal to', 'gamipress'), | |
| 70 | + 'less_than' => __( 'less than', 'gamipress' ), | |
| 71 | + 'greater_than' => __( 'greater than', 'gamipress' ), | |
| 72 | + 'less_or_equal' => __( 'less or equal to', 'gamipress' ), | |
| 73 | + 'greater_or_equal' => __( 'greater or equal to', 'gamipress' ), | |
| 74 | + ); | |
| 75 | + | |
| 76 | +} | |
| 77 | + | |
| 78 | +/** | |
| 79 | + * Get predefined quiz types | |
| 80 | + * | |
| 81 | + * @since 1.0.0 | |
| 82 | + * | |
| 83 | + * @return array | |
| 84 | + */ | |
| 85 | +function gamipress_thrive_quiz_builder_get_quiz_types() { | |
| 86 | + | |
| 87 | + return array( | |
| 88 | + '' => __( 'Choose a quiz type', 'gamipress' ), | |
| 89 | + 'number' => __( 'Number', 'gamipress' ), | |
| 90 | + 'percentage' => __( 'Percentage', 'gamipress' ), | |
| 91 | + 'personality' => __( 'Personality', 'gamipress' ), | |
| 92 | + 'right_wrong' => __( 'Right/Wrong', 'gamipress' ), | |
| 93 | + 'survey' => __( 'Survey', 'gamipress' ) | |
| 94 | + ); | |
| 95 | + | |
| 96 | +} | |
| 97 | + | |
| 98 | +/** | |
| 99 | + * Get quizzes with percentage type | |
| 100 | + * | |
| 101 | + * @since 1.0.0 | |
| 102 | + * | |
| 103 | + * @return array | |
| 104 | + */ | |
| 105 | +function gamipress_thrive_quiz_builder_get_percentage_quizzes( ) { | |
| 106 | + | |
| 107 | + $quizzes = get_posts( array( | |
| 108 | + 'numberposts' => -1, | |
| 109 | + 'post_type' => 'tqb_quiz', | |
| 110 | + 'meta_query' => array( | |
| 111 | + 'relation' => 'OR', | |
| 112 | + array( | |
| 113 | + 'key' => 'tqb_quiz_type', | |
| 114 | + 'value' => 'percentage', | |
| 115 | + 'compare' => '=', | |
| 116 | + ), | |
| 117 | + array( | |
| 118 | + 'key' => 'tqb_quiz_type', | |
| 119 | + 'value' => 's:10:"percentage";', | |
| 120 | + 'compare' => 'LIKE', | |
| 121 | + ), | |
| 122 | + ) | |
| 123 | + ) ); | |
| 124 | + | |
| 125 | + return $quizzes; | |
| 126 | +} | |
| 127 | + | |
| 128 | +/** | |
| 129 | + * Get the percentage quiz title | |
| 130 | + * | |
| 131 | + * @since 1.0.0 | |
| 132 | + * | |
| 133 | + * @param int $quiz_id Quiz ID | |
| 134 | + */ | |
| 135 | +function gamipress_thrive_quiz_builder_get_percentage_quiz_title( $quiz_id ){ | |
| 136 | + | |
| 137 | + $percentage_quiz = get_post( $quiz_id ); | |
| 138 | + | |
| 139 | + return ( $percentage_quiz ? $percentage_quiz->post_title : '' ); | |
| 140 | + | |
| 141 | +} | |