| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\BuddyPress\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Compatibility function to check if a BuddyPress module is active |
| 14 |
* |
| 15 |
* @since 1.2.0 |
| 16 |
* |
| 17 |
* @param string $component The component name. |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
function gamipress_bp_is_active( $component = '' ) { |
| 22 |
|
| 23 |
if( function_exists( 'bp_is_active' ) ) { |
| 24 |
return bp_is_active( $component ); |
| 25 |
} |
| 26 |
|
| 27 |
return true; |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Overrides GamiPress AJAX Helper for selecting posts |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
function gamipress_bp_ajax_get_posts() { |
| 37 |
|
| 38 |
// Security check, forces to die if not security passed |
| 39 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 40 |
|
| 41 |
// Check if user can manage GamiPress |
| 42 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 43 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
global $wpdb; |
| 47 |
|
| 48 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'bp_groups', $_REQUEST['post_type'] ) ) { |
| 49 |
|
| 50 |
$results = array(); |
| 51 |
|
| 52 |
// Pull back the search string |
| 53 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : false; |
| 54 |
|
| 55 |
$bp_groups = groups_get_groups( array( |
| 56 |
'search_terms' => $search, |
| 57 |
'show_hidden' => true, |
| 58 |
'per_page' => 300 |
| 59 |
) ); |
| 60 |
|
| 61 |
if ( ! empty( $bp_groups ) ) { |
| 62 |
foreach ( $bp_groups['groups'] as $group ) { |
| 63 |
// Results should meet same structure like posts |
| 64 |
$results[] = array( |
| 65 |
'ID' => $group->id, |
| 66 |
'post_title' => $group->name, |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Return our results |
| 72 |
wp_send_json_success( $results ); |
| 73 |
die; |
| 74 |
} else if ( isset( $_REQUEST['post_type'] ) && in_array( 'bp_fields', $_REQUEST['post_type'] ) ) { |
| 75 |
$results = array(); |
| 76 |
|
| 77 |
// Pull back the search string |
| 78 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 79 |
$page = isset( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : 1; |
| 80 |
$limit = 20; |
| 81 |
$offset = $limit * ( $page - 1 ); |
| 82 |
$prefix = gamipress_bp_get_table_prefix(); |
| 83 |
|
| 84 |
if( ! empty( $search ) ) { |
| 85 |
$profile_fields = $wpdb->get_results( $wpdb->prepare( |
| 86 |
"SELECT * |
| 87 |
FROM {$prefix}bp_xprofile_fields |
| 88 |
WHERE name LIKE %s |
| 89 |
ORDER BY field_order ASC |
| 90 |
LIMIT {$offset}, {$limit}", |
| 91 |
"%%{$search}%%" |
| 92 |
) ); |
| 93 |
|
| 94 |
$count = absint( $wpdb->get_var( $wpdb->prepare( |
| 95 |
"SELECT COUNT(*) FROM {$prefix}bp_xprofile_fields WHERE name LIKE %s", |
| 96 |
"%%{$search}%%" |
| 97 |
) ) ); |
| 98 |
|
| 99 |
} else { |
| 100 |
$profile_fields = $wpdb->get_results( $wpdb->prepare( |
| 101 |
"SELECT * |
| 102 |
FROM {$prefix}bp_xprofile_fields |
| 103 |
ORDER BY field_order ASC |
| 104 |
LIMIT {$offset}, {$limit}" |
| 105 |
) ); |
| 106 |
|
| 107 |
$count = absint( $wpdb->get_var( $wpdb->prepare( |
| 108 |
"SELECT COUNT(*) FROM {$prefix}bp_xprofile_fields" |
| 109 |
) ) ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! empty( $profile_fields ) ) { |
| 113 |
foreach ( $profile_fields as $profile_field ) { |
| 114 |
|
| 115 |
// Results should meet Select2 structure |
| 116 |
$results[] = array( |
| 117 |
'ID' => $profile_field->id, |
| 118 |
'post_title' => $profile_field->name, |
| 119 |
); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
$response = array( |
| 126 |
'results' => $results, |
| 127 |
'more_results' => $count > $limit && $count > $offset, |
| 128 |
); |
| 129 |
|
| 130 |
// Return our results |
| 131 |
wp_send_json_success( $response ); |
| 132 |
die; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_bp_ajax_get_posts', 5 ); |
| 137 |
|
| 138 |
/** |
| 139 |
* Helper function to get the table prefix |
| 140 |
* |
| 141 |
* @since 1.0.0 |
| 142 |
* |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
function gamipress_bp_get_table_prefix() { |
| 146 |
|
| 147 |
global $wpdb; |
| 148 |
|
| 149 |
if( function_exists( 'bp_core_get_table_prefix' ) ) { |
| 150 |
return bp_core_get_table_prefix(); |
| 151 |
} |
| 152 |
|
| 153 |
return $wpdb->prefix; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
function gamipress_bp_get_profile_field_name( $field_id ) { |
| 158 |
|
| 159 |
global $wpdb; |
| 160 |
$prefix = gamipress_bp_get_table_prefix(); |
| 161 |
|
| 162 |
$name_field = $wpdb->get_var( $wpdb->prepare( |
| 163 |
"SELECT name |
| 164 |
FROM {$prefix}bp_xprofile_fields |
| 165 |
WHERE id = %d", |
| 166 |
$field_id |
| 167 |
) ); |
| 168 |
|
| 169 |
return $name_field; |
| 170 |
} |