| @@ -1,0 +1,46 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\H5P\Ajax_Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Ajax function for selecting contents | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + */ | |
| 16 | +function automatorwp_h5p_ajax_get_contents() { | |
| 17 | + // Security check, forces to die if not security passed | |
| 18 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 19 | + | |
| 20 | + // Permissions check | |
| 21 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 22 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + global $wpdb; | |
| 26 | + | |
| 27 | + // Pull back the search string | |
| 28 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 29 | + | |
| 30 | + // Get the contents | |
| 31 | + $results = $wpdb->get_results( $wpdb->prepare( | |
| 32 | + "SELECT c.id AS id, c.title AS text | |
| 33 | + FROM {$wpdb->prefix}h5p_contents AS c | |
| 34 | + WHERE c.title LIKE %s", | |
| 35 | + "%%{$search}%%" | |
| 36 | + ) ); | |
| 37 | + | |
| 38 | + // Prepend option none | |
| 39 | + $results = automatorwp_ajax_get_ajax_results_option_none( $results ); | |
| 40 | + | |
| 41 | + // Return our results | |
| 42 | + wp_send_json_success( $results ); | |
| 43 | + die; | |
| 44 | + | |
| 45 | +} | |
| 46 | +add_action( 'wp_ajax_automatorwp_h5p_get_contents', 'automatorwp_h5p_ajax_get_contents', 5 ); | |