| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\H5P\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_h5p_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 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'h5p_contents', $_REQUEST['post_type'] ) ) { |
| 30 |
|
| 31 |
$results = array(); |
| 32 |
|
| 33 |
// Pull back the search string |
| 34 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; |
| 35 |
|
| 36 |
// Get the content title |
| 37 |
$contents = $wpdb->get_results( $wpdb->prepare( |
| 38 |
"SELECT c.id, c.title, l.title AS library |
| 39 |
FROM {$wpdb->prefix}h5p_contents AS c |
| 40 |
JOIN {$wpdb->prefix}h5p_libraries AS l ON l.id = c.library_id |
| 41 |
WHERE c.title LIKE %s", |
| 42 |
"%%{$search}%%" |
| 43 |
) ); |
| 44 |
|
| 45 |
foreach ( $contents as $content ) { |
| 46 |
|
| 47 |
// Results should meet same structure like posts |
| 48 |
$results[] = array( |
| 49 |
'ID' => $content->id, |
| 50 |
'post_title' => $content->title, |
| 51 |
'post_type' => $content->library, |
| 52 |
); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
// Return our results |
| 57 |
wp_send_json_success( $results ); |
| 58 |
die; |
| 59 |
|
| 60 |
} else if( isset( $_REQUEST['post_type'] ) && in_array( 'h5p_tags', $_REQUEST['post_type'] ) ) { |
| 61 |
|
| 62 |
$results = array(); |
| 63 |
|
| 64 |
// Pull back the search string |
| 65 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 66 |
|
| 67 |
// Get the tag title |
| 68 |
$tags = $wpdb->get_results( $wpdb->prepare( |
| 69 |
"SELECT t.id, t.name |
| 70 |
FROM {$wpdb->prefix}h5p_tags AS t |
| 71 |
WHERE t.name LIKE %s", |
| 72 |
"%%{$search}%%" |
| 73 |
) ); |
| 74 |
|
| 75 |
foreach ( $tags as $tag ) { |
| 76 |
|
| 77 |
// Results should meet same structure like posts |
| 78 |
$results[] = array( |
| 79 |
'ID' => $tag->id, |
| 80 |
'post_title' => $tag->name, |
| 81 |
); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
// Return our results |
| 86 |
wp_send_json_success( $results ); |
| 87 |
die; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_h5p_ajax_get_posts', 5 ); |
| 93 |
|
| 94 |
// Get the content title |
| 95 |
function gamipress_h5p_get_content_title( $content_id ) { |
| 96 |
|
| 97 |
if( absint( $content_id ) === 0 ) return ''; |
| 98 |
|
| 99 |
global $wpdb; |
| 100 |
|
| 101 |
return $wpdb->get_var( $wpdb->prepare( |
| 102 |
"SELECT c.title |
| 103 |
FROM {$wpdb->prefix}h5p_contents c |
| 104 |
WHERE c.id = %d", |
| 105 |
$content_id |
| 106 |
) ); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
// Get the content type title |
| 111 |
function gamipress_h5p_get_content_type_title( $content_type ) { |
| 112 |
|
| 113 |
if( empty( $content_type ) ) return ''; |
| 114 |
|
| 115 |
global $wpdb; |
| 116 |
|
| 117 |
return $wpdb->get_var( $wpdb->prepare( |
| 118 |
"SELECT l.title |
| 119 |
FROM {$wpdb->prefix}h5p_libraries AS l |
| 120 |
WHERE l.name = %s", |
| 121 |
$content_type |
| 122 |
) ); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
// Get the tag title |
| 127 |
function gamipress_h5p_get_tag_title( $tag_id ) { |
| 128 |
|
| 129 |
if( absint( $tag_id ) === 0 ) return ''; |
| 130 |
|
| 131 |
global $wpdb; |
| 132 |
|
| 133 |
return $wpdb->get_var( $wpdb->prepare( |
| 134 |
"SELECT t.name |
| 135 |
FROM {$wpdb->prefix}h5p_tags t |
| 136 |
WHERE t.id = %d", |
| 137 |
$tag_id |
| 138 |
) ); |
| 139 |
|
| 140 |
} |