| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax Functions |
| 4 |
* |
| 5 |
* @package GamiPress\WP_Courseware\Ajax_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_wpcw_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'] ) ? sanitize_text_field( $_REQUEST['q'] ) : ''; |
| 33 |
$search = $wpdb->esc_like( $search ); |
| 34 |
|
| 35 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'wpcw_modules', $_REQUEST['post_type'] ) ) { |
| 36 |
|
| 37 |
// Get the modules |
| 38 |
$modules = $wpdb->get_results( $wpdb->prepare( |
| 39 |
"SELECT m.module_id, m.module_title |
| 40 |
FROM {$wpdb->prefix}wpcw_modules AS m |
| 41 |
WHERE m.module_title LIKE %s", |
| 42 |
"%%{$search}%%" |
| 43 |
) ); |
| 44 |
|
| 45 |
foreach ( $modules as $module ) { |
| 46 |
|
| 47 |
// Results should meet same structure like posts |
| 48 |
$results[] = array( |
| 49 |
'ID' => $module->module_id, |
| 50 |
'post_title' => $module->module_title, |
| 51 |
); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
// Return our results |
| 56 |
wp_send_json_success( $results ); |
| 57 |
die; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
} |
| 63 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_wpcw_ajax_get_posts', 5 ); |
| 64 |
|
| 65 |
// Get the module title |
| 66 |
function gamipress_wpcw_get_module_title( $module_id ) { |
| 67 |
|
| 68 |
if( absint( $module_id ) === 0 ) return ''; |
| 69 |
|
| 70 |
global $wpdb; |
| 71 |
|
| 72 |
return $wpdb->get_var( $wpdb->prepare( |
| 73 |
"SELECT m.module_title FROM {$wpdb->prefix}wpcw_modules AS m WHERE m.module_id = %d", |
| 74 |
absint( $module_id ) |
| 75 |
) ); |
| 76 |
|
| 77 |
} |