| @@ -1,0 +1,92 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package GamiPress\Pretty-Link\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_pretty_link_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( 'pretty-link', $_REQUEST['post_type'] ) ) { | |
| 30 | + | |
| 31 | + $links = array(); | |
| 32 | + $linksController = new PrliLocalApiController(); | |
| 33 | + $all_links = $linksController->get_all_links(); | |
| 34 | + | |
| 35 | + // Pull back the search string | |
| 36 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; | |
| 37 | + $results = array(); | |
| 38 | + | |
| 39 | + if( gamipress_is_network_wide_active() ) { | |
| 40 | + | |
| 41 | + // Look for results on all sites on a multisite install | |
| 42 | + | |
| 43 | + foreach( gamipress_get_network_site_ids() as $site_id ) { | |
| 44 | + | |
| 45 | + // Switch to site | |
| 46 | + switch_to_blog( $site_id ); | |
| 47 | + | |
| 48 | + // Get the current site name to append it to results | |
| 49 | + $site_name = get_bloginfo( 'name' ); | |
| 50 | + | |
| 51 | + // Check if plugin is active on this site | |
| 52 | + if( function_exists( 'prli_plugin_info' ) ) { | |
| 53 | + | |
| 54 | + foreach( $all_links as $link ) { | |
| 55 | + | |
| 56 | + // Results should meet the same structure like posts | |
| 57 | + $results[] = array( | |
| 58 | + 'id' => $link['id'], | |
| 59 | + 'post_title' => $link['name'], | |
| 60 | + 'site_id' => $site_id, | |
| 61 | + 'site_name' => $site_name | |
| 62 | + ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + // Restore current site | |
| 68 | + restore_current_blog(); | |
| 69 | + | |
| 70 | + } | |
| 71 | + | |
| 72 | + } else { | |
| 73 | + | |
| 74 | + foreach( $all_links as $link ) { | |
| 75 | + $results[] = array( | |
| 76 | + 'id' => $link['id'], | |
| 77 | + 'text' => $link['name'], | |
| 78 | + ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + } | |
| 82 | + | |
| 83 | + // Return our results | |
| 84 | + wp_send_json_success( $results ); | |
| 85 | + | |
| 86 | + die; | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | + | |
| 91 | +} | |
| 92 | +add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_pretty_link_ajax_get_posts', 5 ); | |