| @@ -1,0 +1,75 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\BuddyBoss\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 groups | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + */ | |
| 16 | +function automatorwp_buddyboss_ajax_get_groups() { | |
| 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 | + $results = array(); | |
| 28 | + | |
| 29 | + // Pull back the search string | |
| 30 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 31 | + $page = isset( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : 1; | |
| 32 | + | |
| 33 | + if( bp_is_active( 'groups' ) ) { | |
| 34 | + | |
| 35 | + // Bail if BuddyBoss function does not exist | |
| 36 | + if ( ! function_exists( 'groups_get_group' ) ) { | |
| 37 | + return $results; | |
| 38 | + } | |
| 39 | + | |
| 40 | + // Get the groups | |
| 41 | + $groups = groups_get_groups( array( | |
| 42 | + 'search_terms' => $search, | |
| 43 | + 'show_hidden' => true, | |
| 44 | + 'per_page' => 20, | |
| 45 | + 'page' => $page | |
| 46 | + ) ); | |
| 47 | + | |
| 48 | + if( isset( $groups['groups'] ) && ! empty( $groups['groups'] ) ) { | |
| 49 | + foreach ( $groups['groups'] as $group ) { | |
| 50 | + | |
| 51 | + // Results should meet Select2 structure | |
| 52 | + $results[] = array( | |
| 53 | + 'id' => $group->id, | |
| 54 | + 'text' => $group->name, | |
| 55 | + ); | |
| 56 | + | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + } | |
| 61 | + | |
| 62 | + // Prepend option none | |
| 63 | + $results = automatorwp_ajax_get_ajax_results_option_none( $results ); | |
| 64 | + | |
| 65 | + $response = array( | |
| 66 | + 'results' => $results, | |
| 67 | + 'more_results' => count( $results ), | |
| 68 | + ); | |
| 69 | + | |
| 70 | + // Return our results | |
| 71 | + wp_send_json_success( $response ); | |
| 72 | + die; | |
| 73 | + | |
| 74 | +} | |
| 75 | +add_action( 'wp_ajax_automatorwp_buddyboss_get_groups', 'automatorwp_buddyboss_ajax_get_groups' ); | |