| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\FluentCommunity\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_fluentcommunity_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( 'fluentcommunity_spaces', $_REQUEST['post_type'] ) ) { |
| 35 |
|
| 36 |
$spaces = \FluentCommunity\App\Functions\Utility::getSpaces(); |
| 37 |
|
| 38 |
foreach ( $spaces as $space ) { |
| 39 |
|
| 40 |
// Results should meet same structure like posts |
| 41 |
$results[] = array( |
| 42 |
'ID' => $space['id'], |
| 43 |
'post_title' => $space['title'], |
| 44 |
); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
// Return our results |
| 49 |
wp_send_json_success( $results ); |
| 50 |
die; |
| 51 |
|
| 52 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'fluentcommunity_courses', $_REQUEST['post_type'] ) ) { |
| 53 |
|
| 54 |
$courses = \FluentCommunity\App\Functions\Utility::getCourses(); |
| 55 |
|
| 56 |
foreach ( $courses as $course ) { |
| 57 |
|
| 58 |
// Results should meet same structure like posts |
| 59 |
$results[] = array( |
| 60 |
'ID' => $course['id'], |
| 61 |
'post_title' => $course['title'], |
| 62 |
); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
// Return our results |
| 67 |
wp_send_json_success( $results ); |
| 68 |
die; |
| 69 |
|
| 70 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'fluentcommunity_quizzes', $_REQUEST['post_type'] ) ) { |
| 71 |
|
| 72 |
// Return quizzes |
| 73 |
$quizzes = $wpdb->get_results( |
| 74 |
$wpdb->prepare( |
| 75 |
"SELECT id, title FROM {$wpdb->prefix}fcom_posts WHERE type = %s AND content_type = %s AND status = %s", |
| 76 |
'course_lesson', |
| 77 |
'quiz', |
| 78 |
'published' |
| 79 |
) |
| 80 |
); |
| 81 |
|
| 82 |
foreach ( $quizzes as $quiz ) { |
| 83 |
|
| 84 |
// Results should meet same structure like posts |
| 85 |
$results[] = array( |
| 86 |
'ID' => $quiz->id, |
| 87 |
'post_title' => $quiz->title, |
| 88 |
); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
// Return our results |
| 93 |
wp_send_json_success( $results ); |
| 94 |
die; |
| 95 |
|
| 96 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'fluentcommunity_lessons', $_REQUEST['post_type'] ) ) { |
| 97 |
|
| 98 |
// Return lessons |
| 99 |
$lessons = $wpdb->get_results( |
| 100 |
$wpdb->prepare( |
| 101 |
"SELECT id, title FROM {$wpdb->prefix}fcom_posts WHERE type = %s AND content_type = %s AND status = %s", |
| 102 |
'course_lesson', |
| 103 |
'text', |
| 104 |
'published' |
| 105 |
) |
| 106 |
); |
| 107 |
|
| 108 |
foreach ( $lessons as $lesson ) { |
| 109 |
|
| 110 |
// Results should meet same structure like posts |
| 111 |
$results[] = array( |
| 112 |
'ID' => $lesson->id, |
| 113 |
'post_title' => $lesson->title, |
| 114 |
); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
// Return our results |
| 119 |
wp_send_json_success( $results ); |
| 120 |
die; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_fluentcommunity_ajax_get_posts', 5 ); |
| 126 |
|
| 127 |
/** |
| 128 |
* Get the space title |
| 129 |
* |
| 130 |
* @param int $space_id |
| 131 |
* |
| 132 |
* @since 1.0.0 |
| 133 |
*/ |
| 134 |
function gamipress_fluentcommunity_get_space_title( $space_id ) { |
| 135 |
|
| 136 |
if( absint( $space_id ) === 0 ) { |
| 137 |
return ''; |
| 138 |
} |
| 139 |
|
| 140 |
$spaces = \FluentCommunity\App\Functions\Utility::getSpaces(); |
| 141 |
|
| 142 |
foreach ( $spaces as $space ) { |
| 143 |
if ( absint( $space['id'] ) === absint( $space_id ) ) { |
| 144 |
return $space['title']; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get the course title |
| 152 |
* |
| 153 |
* @param int $course_id |
| 154 |
* |
| 155 |
* @since 1.0.0 |
| 156 |
*/ |
| 157 |
function gamipress_fluentcommunity_get_course_title( $course_id ) { |
| 158 |
|
| 159 |
if( absint( $course_id ) === 0 ) { |
| 160 |
return ''; |
| 161 |
} |
| 162 |
|
| 163 |
$courses = \FluentCommunity\App\Functions\Utility::getCourses(); |
| 164 |
|
| 165 |
foreach ( $courses as $course ) { |
| 166 |
if ( absint( $course['id'] ) === absint( $course_id ) ) { |
| 167 |
return $course['title']; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Get the quiz title |
| 175 |
* |
| 176 |
* @param int $quiz_id |
| 177 |
* |
| 178 |
* @since 1.0.0 |
| 179 |
*/ |
| 180 |
function gamipress_fluentcommunity_get_quiz_title( $quiz_id ) { |
| 181 |
|
| 182 |
global $wpdb; |
| 183 |
|
| 184 |
// Empty title if no ID provided |
| 185 |
if( absint( $quiz_id ) === 0 ) { |
| 186 |
return ''; |
| 187 |
} |
| 188 |
|
| 189 |
// Get quiz title |
| 190 |
$quiz = $wpdb->get_var( |
| 191 |
$wpdb->prepare( |
| 192 |
"SELECT title FROM {$wpdb->prefix}fcom_posts WHERE id = %d", |
| 193 |
absint( $quiz_id ) |
| 194 |
) |
| 195 |
); |
| 196 |
|
| 197 |
return $quiz; |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get the lesson title |
| 203 |
* |
| 204 |
* @param int $lesson_id |
| 205 |
* |
| 206 |
* @since 1.0.0 |
| 207 |
*/ |
| 208 |
function gamipress_fluentcommunity_get_lesson_title( $lesson_id ) { |
| 209 |
|
| 210 |
global $wpdb; |
| 211 |
|
| 212 |
// Empty title if no ID provided |
| 213 |
if( absint( $lesson_id ) === 0 ) { |
| 214 |
return ''; |
| 215 |
} |
| 216 |
|
| 217 |
// Get lesson title |
| 218 |
$lesson = $wpdb->get_var( |
| 219 |
$wpdb->prepare( |
| 220 |
"SELECT title FROM {$wpdb->prefix}fcom_posts WHERE id = %d", |
| 221 |
absint( $lesson_id ) |
| 222 |
) |
| 223 |
); |
| 224 |
|
| 225 |
return $lesson; |
| 226 |
|
| 227 |
} |