| 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_digimember_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'] ) && |
| 30 |
( in_array( 'digimember_product', $_REQUEST['post_type'] ) |
| 31 |
|| in_array( 'digimember_membership_product', $_REQUEST['post_type'] ) |
| 32 |
|| in_array( 'digimember_download_product', $_REQUEST['post_type'] ) ) ) { |
| 33 |
|
| 34 |
$results = array(); |
| 35 |
|
| 36 |
// Pull back the search string |
| 37 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 38 |
|
| 39 |
if( in_array( 'digimember_product', $_REQUEST['post_type'] ) ) { |
| 40 |
|
| 41 |
// Get products |
| 42 |
$products = $wpdb->get_results( $wpdb->prepare( |
| 43 |
"SELECT p.id, p.name, p.type |
| 44 |
FROM {$wpdb->prefix}digimember_product AS p |
| 45 |
WHERE p.name LIKE %s", |
| 46 |
"%%{$search}%%" |
| 47 |
) ); |
| 48 |
|
| 49 |
} else if( in_array( 'digimember_membership_product', $_REQUEST['post_type'] ) ) { |
| 50 |
|
| 51 |
// Get membership products |
| 52 |
$products = $wpdb->get_results( $wpdb->prepare( |
| 53 |
"SELECT p.id, p.name, p.type |
| 54 |
FROM {$wpdb->prefix}digimember_product AS p |
| 55 |
WHERE p.name LIKE %s |
| 56 |
AND p.type = 'membership'", |
| 57 |
"%%{$search}%%" |
| 58 |
) ); |
| 59 |
|
| 60 |
} else if( in_array( 'digimember_download_product', $_REQUEST['post_type'] ) ) { |
| 61 |
|
| 62 |
// Get download products |
| 63 |
$products = $wpdb->get_results( $wpdb->prepare( |
| 64 |
"SELECT p.id, p.name, p.type |
| 65 |
FROM {$wpdb->prefix}digimember_product AS p |
| 66 |
WHERE p.name LIKE %s |
| 67 |
AND p.type = 'download'", |
| 68 |
"%%{$search}%%" |
| 69 |
) ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
foreach ( $products as $product ) { |
| 74 |
|
| 75 |
// Results should meet same structure like posts |
| 76 |
$results[] = array( |
| 77 |
'ID' => $product->id, |
| 78 |
'post_title' => $product->name, |
| 79 |
'post_type' => ucfirst( $product->type ), |
| 80 |
); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
// Return our results |
| 85 |
wp_send_json_success( $results ); |
| 86 |
die; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_digimember_ajax_get_posts', 5 ); |
| 92 |
|
| 93 |
// Get the product title |
| 94 |
function gamipress_digimember_get_product_title( $product_id ) { |
| 95 |
|
| 96 |
if( absint( $product_id ) === 0 ) return ''; |
| 97 |
|
| 98 |
global $wpdb; |
| 99 |
|
| 100 |
return $wpdb->get_var( $wpdb->prepare( |
| 101 |
"SELECT p.name FROM {$wpdb->prefix}digimember_product AS p WHERE p.id = %d", |
| 102 |
$product_id |
| 103 |
) ); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get the product type |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
* |
| 112 |
* @param int $product_id |
| 113 |
* |
| 114 |
* @return string|null |
| 115 |
*/ |
| 116 |
function gamipress_digimember_get_product_type( $product_id ) { |
| 117 |
|
| 118 |
// Empty title if no ID provided |
| 119 |
if( absint( $product_id ) === 0 ) { |
| 120 |
return ''; |
| 121 |
} |
| 122 |
|
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
return $wpdb->get_var( $wpdb->prepare( |
| 126 |
"SELECT p.type FROM {$wpdb->prefix}digimember_product AS p WHERE p.id = %d", |
| 127 |
$product_id |
| 128 |
) ); |
| 129 |
|
| 130 |
} |