| @@ -1,0 +1,127 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\Gravity_Kit\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_gravity_kit_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( 'gravity_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 | + $site_forms = $wpdb->get_results( $wpdb->prepare( | |
| 48 | + "SELECT p.id AS ID, p.title AS post_title | |
| 49 | + FROM {$wpdb->prefix}gf_form AS p | |
| 50 | + WHERE p.title LIKE %s", | |
| 51 | + "%%{$search}%%" | |
| 52 | + ) ); | |
| 53 | + | |
| 54 | + foreach ( $site_forms as $form ) { | |
| 55 | + | |
| 56 | + // Results should meet same structure like posts | |
| 57 | + $results[] = array( | |
| 58 | + 'ID' => $form->ID, | |
| 59 | + 'post_title' => $form->post_title, | |
| 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 | + $results = $wpdb->get_results( $wpdb->prepare( | |
| 73 | + "SELECT p.id AS ID, p.title AS post_title | |
| 74 | + FROM {$wpdb->prefix}gf_form AS p | |
| 75 | + WHERE p.title LIKE %s", | |
| 76 | + "%%{$search}%%" | |
| 77 | + ) ); | |
| 78 | + | |
| 79 | + } | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + // Return our results | |
| 84 | + wp_send_json_success( $results ); | |
| 85 | + die; | |
| 86 | + } | |
| 87 | + | |
| 88 | +} | |
| 89 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_gravity_kit_ajax_get_posts', 5 ); | |
| 90 | + | |
| 91 | +/** | |
| 92 | + * Get entry form | |
| 93 | + * | |
| 94 | + * @since 1.0.0 | |
| 95 | + * | |
| 96 | + * @param int $entry_id The entry ID | |
| 97 | + * | |
| 98 | + * @return int | |
| 99 | + */ | |
| 100 | +function gamipress_gravity_kit_get_entry_form ( $entry_id ) { | |
| 101 | + | |
| 102 | + global $wpdb; | |
| 103 | + | |
| 104 | + $form_id = $wpdb->get_var( $wpdb->prepare( "SELECT form_id from {$wpdb->prefix}gf_entry WHERE id=%d", $entry_id ) ); | |
| 105 | + | |
| 106 | + return $form_id; | |
| 107 | + | |
| 108 | +} | |
| 109 | + | |
| 110 | +/** | |
| 111 | + * Get entry user | |
| 112 | + * | |
| 113 | + * @since 1.0.0 | |
| 114 | + * | |
| 115 | + * @param int $entry_id The entry ID | |
| 116 | + * | |
| 117 | + * @return int|NULL | |
| 118 | + */ | |
| 119 | +function gamipress_gravity_kit_get_entry_user ( $entry_id ) { | |
| 120 | + | |
| 121 | + global $wpdb; | |
| 122 | + | |
| 123 | + $user_id = $wpdb->get_var( $wpdb->prepare( "SELECT created_by from {$wpdb->prefix}gf_entry WHERE id=%d", $entry_id ) ); | |
| 124 | + | |
| 125 | + return $user_id; | |
| 126 | + | |
| 127 | +} | |