Overview

Namespaces

  • None
  • WPGMZA
    • Integration
    • Selector

Classes

  • WPGMZA\AutoLoader
  • WPGMZA\Crud
  • WPGMZA\DOMDocument
  • WPGMZA\DOMElement
  • WPGMZA\Factory
  • WPGMZA\GDPRCompliance
  • WPGMZA\GoogleGeocoder
  • WPGMZA\GoogleMap
  • WPGMZA\GoogleMapsAPILoader
  • WPGMZA\GoogleMapsLoader
  • WPGMZA\Integration\Gutenberg
  • WPGMZA\Integration\WPMigrateDB
  • WPGMZA\LatLng
  • WPGMZA\Map
  • WPGMZA\MapsEngineDialog
  • WPGMZA\Marker
  • WPGMZA\ModalDialog
  • WPGMZA\NominatimGeocodeCache
  • WPGMZA\OLLoader
  • WPGMZA\Plugin
  • WPGMZA\RestAPI
  • WPGMZA\ScriptLoader
  • WPGMZA\Selector\AttributeSelector
  • WPGMZA\Selector\Parser
  • WPGMZA\Selector\PseudoSelector
  • WPGMZA\Selector\Selector
  • WPGMZA\Selector\Token
  • WPGMZA\Selector\Tokenizer
  • WPGMZA\Selector\TokenStream
  • WPGMZA\Selector\XPathConverter
  • WPGMZA\Strings

Exceptions

  • WPGMZA\Selector\ConvertException
  • WPGMZA\Selector\ParseException

Functions

  • WPGMZA\create_marker_instance_delegate
  • WPGMZA\create_plugin_instance
  • WPGMZA\query_nominatim_cache
  • WPGMZA\Selector\trace
  • WPGMZA\store_nominatim_cache
  • wpgmza_backwards_compat_get_all_circle_data
  • wpgmza_backwards_compat_get_all_rectangle_data
  • wpgmza_check_admin_head_backwards_compat_v6
  • wpgmza_check_map_editor_backwards_compat_v6
  • wpgmza_check_pro_compat_required_v6
  • wpgmza_check_user_backwards_compat_v6
  • Overview
  • Namespace
  • Class
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 
<?php

namespace WPGMZA;

/**
 * This class facilitates all communication between the client and any server side modules which can be interacted with through the WordPress REST API.
 */
class RestAPI
{
    /**
     * @const The plugins REST API namespace
     */
    const NS = 'wpgmza/v1';
    
    /**
     * Constructor
     */
    public function __construct()
    {
        add_action('wp_enqueue_scripts', array($this, 'onEnqueueScripts'));
        add_action('admin_enqueue_scripts', array($this, 'onEnqueueScripts'));
        add_action('enqueue_block_assets', array($this, 'onEnqueueScripts'));
        
        add_action('rest_api_init', array($this, 'onRestAPIInit'));
    }
    
    /**
     * Enqueues the wp-api script, required to use the Rest API client side.
     * @return void
     */
    public function onEnqueueScripts()
    {
        wp_enqueue_script('wp-api');
    }
    
    /**
     * Callback for the rest_api_init action, this function registers the plugins REST API routes.
     * @return void
     */
    public function onRestAPIInit()
    {
        register_rest_route(RestAPI::NS, '/markers(\/\d+)?/', array(
            'methods' => 'GET',
            'callback' => array($this, 'markers')
        ));
    }
    
    /**
     * Callback for the /markers REST API route.
     * @param \WP_REST_Request The REST request.
     * @return mixed Where an ID is specified on the URL, a single marker is returned. Where no ID is specified, an array of all markers are returned.
     */
    public function markers($request)
    {
        global $wpdb;
        global $wpgmza_tblname;
        
        $route = $request->get_route();
        
        if(preg_match('#/wpgmza/v1/markers/(\d+)#', $route, $m))
        {
            $marker = new Marker($m[1]);
            return $marker;
        }
        
        $results = $wpdb->get_results("SELECT * FROM $wpgmza_tblname");
        
        // TODO: Select all custom field data too, in one query, and add that to the marker data in the following loop. Ideally we could add a bulk get function to the CRUD classes which takes IDs?
        
        foreach($results as $obj)
            unset($obj->latlng);
        
        return $results;
    }
}
API documentation generated by ApiGen