| 1 |
<?php |
| 2 |
|
| 3 |
if( !class_exists( 'wp_mapit_admin_ajax' ) ) { |
| 4 |
/** |
| 5 |
* Class to manage all ajax requests for admin. |
| 6 |
*/ |
| 7 |
class wp_mapit_admin_ajax |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Function to initialize all the ajax requests for admin |
| 11 |
* |
| 12 |
* @since 1.0 |
| 13 |
* @static |
| 14 |
* @access public |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
add_action( 'wp_ajax_wp_mapit_location_search', __CLASS__ . '::wp_mapit_location_search' ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Hook to handle wp_mapit_location_search ajax call |
| 22 |
* |
| 23 |
* @since 1.0 |
| 24 |
* @static |
| 25 |
* @access public |
| 26 |
* @return string JSON string including the status of the call, data for the location search and message in case of error |
| 27 |
*/ |
| 28 |
public static function wp_mapit_location_search() { |
| 29 |
$status = '0'; |
| 30 |
$message = ''; |
| 31 |
$data = array(); |
| 32 |
|
| 33 |
if( isset( $_REQUEST['q'] ) && trim( $_REQUEST['q'] ) != '' ) { |
| 34 |
|
| 35 |
$requestUrl = utf8_encode( 'https://nominatim.openstreetmap.org/search?q='.$_REQUEST['q'].'&format=json' ); |
| 36 |
|
| 37 |
$content = wp_remote_retrieve_body( wp_remote_get( $requestUrl ) ); |
| 38 |
|
| 39 |
if( $content != '' ){ |
| 40 |
$content = json_decode( $content, true ); |
| 41 |
if( is_array( $content ) && count( $content ) > 0 ) { |
| 42 |
$status = '1'; |
| 43 |
$data = $content; |
| 44 |
} else { |
| 45 |
$message = __( 'Location not found.', WP_MAPIT_TEXTDOMAIN ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
} else { |
| 50 |
$message = __( 'Please enter a value to search. ', WP_MAPIT_TEXTDOMAIN ); |
| 51 |
} |
| 52 |
|
| 53 |
echo json_encode( array( 'status' => $status, 'message' => $message, 'data' => $data ) ); |
| 54 |
die; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Calling init function to activate hooks and filters. |
| 60 |
*/ |
| 61 |
wp_mapit_admin_ajax::init(); |
| 62 |
} |