| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\WS_Form\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_ws_form_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( 'ws_form', $_REQUEST['post_type'] ) ) { |
| 30 |
|
| 31 |
// Pull back the search string |
| 32 |
$search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; |
| 33 |
$results = array(); |
| 34 |
|
| 35 |
if( gamipress_is_network_wide_active() ) { |
| 36 |
|
| 37 |
// Look for results on all sites on a multisite install |
| 38 |
|
| 39 |
foreach( gamipress_get_network_site_ids() as $site_id ) { |
| 40 |
|
| 41 |
// Switch to site |
| 42 |
switch_to_blog( $site_id ); |
| 43 |
|
| 44 |
// Get the current site name to append it to results |
| 45 |
$site_name = get_bloginfo( 'name' ); |
| 46 |
|
| 47 |
// Get the forms |
| 48 |
$forms = wsf_form_get_all( $published = false, $order_by = 'label' ); |
| 49 |
|
| 50 |
foreach( $forms as $form ) { |
| 51 |
|
| 52 |
if( $search && ( strpos( strtolower( $form['label'] ), $search) === false) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
// Results should meet the Select2 structure |
| 57 |
$results[] = array( |
| 58 |
'ID' => $form['id'], |
| 59 |
'post_title' => $form['label'], |
| 60 |
'site_id' => $site_id, |
| 61 |
'site_name' => $site_name, |
| 62 |
); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
// Restore current site |
| 67 |
restore_current_blog(); |
| 68 |
|
| 69 |
} |
| 70 |
} else { |
| 71 |
|
| 72 |
// Get the forms |
| 73 |
$forms = wsf_form_get_all( $published = false, $order_by = 'label' ); |
| 74 |
|
| 75 |
foreach( $forms as $form ) { |
| 76 |
|
| 77 |
if( $search && ( strpos( strtolower( $form['label'] ), $search) === false) ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
// Results should meet the Select2 structure |
| 82 |
$results[] = array( |
| 83 |
'ID' => $form['id'], |
| 84 |
'post_title' => $form['label'], |
| 85 |
); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
// Return our results |
| 92 |
wp_send_json_success( $results ); |
| 93 |
die; |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_ws_form_ajax_get_posts', 5 ); |