| @@ -1,0 +1,99 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\FluentForm\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_fluentform_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( 'fluent_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 | + // Check if plugin is active on this site | |
| 48 | + if( function_exists( 'wpFluent' ) ) { | |
| 49 | + | |
| 50 | + // Get the forms | |
| 51 | + $site_forms = wpFluent()->table( 'fluentform_forms' ) | |
| 52 | + ->select( array( 'id', 'title' ) ) | |
| 53 | + ->where( 'title', 'LIKE', "%{$search}%" ) | |
| 54 | + ->get(); | |
| 55 | + | |
| 56 | + foreach ( $site_forms as $form ) { | |
| 57 | + | |
| 58 | + // Results should meet same structure like posts | |
| 59 | + $results[] = array( | |
| 60 | + 'ID' => $form->id, | |
| 61 | + 'post_title' => $form->title, | |
| 62 | + 'site_id' => $site_id, | |
| 63 | + 'site_name' => $site_name, | |
| 64 | + ); | |
| 65 | + | |
| 66 | + } | |
| 67 | + | |
| 68 | + } | |
| 69 | + | |
| 70 | + // Restore current site | |
| 71 | + restore_current_blog(); | |
| 72 | + | |
| 73 | + } | |
| 74 | + } else { | |
| 75 | + | |
| 76 | + // Get the forms | |
| 77 | + $forms = wpFluent()->table( 'fluentform_forms' ) | |
| 78 | + ->select( array( 'id', 'title' ) ) | |
| 79 | + ->where( 'title', 'LIKE', "%{$search}%" ) | |
| 80 | + ->get(); | |
| 81 | + | |
| 82 | + foreach( $forms as $form ) { | |
| 83 | + $results[] = array( | |
| 84 | + 'id' => $form->id, | |
| 85 | + 'text' => $form->title, | |
| 86 | + ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + } | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + // Return our results | |
| 94 | + wp_send_json_success( $results ); | |
| 95 | + die; | |
| 96 | + } | |
| 97 | + | |
| 98 | +} | |
| 99 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_fluentform_ajax_get_posts', 5 ); | |