From 4b424f387202b192cf44b33fb0e9a9b143a65049 Mon Sep 17 00:00:00 2001 From: lrullo Date: Mon, 26 Dec 2022 18:34:21 +0100 Subject: [PATCH] Add: REST API walk --- README.md | 17 +++++++++++++ libs/db_helper.php | 5 ++-- soundwalk.php | 63 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c0b0d93 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Wordpress Plugin Soundwalk for Soinumapa + +Developed by Xavier Balderas +Updated by Luca Rullo + +## News +* Add: Access walks via REST API + +## REST API + +GET soundwalk/v1/walk + +Return all walks + +GET soundwalk/v1/walk/ + +Return walks with ID diff --git a/libs/db_helper.php b/libs/db_helper.php index 3c27146..5c8ba38 100644 --- a/libs/db_helper.php +++ b/libs/db_helper.php @@ -88,14 +88,15 @@ if (!class_exists('Soundwalk_DB_Helper')){ $table_name = $wpdb->prefix . self::TABLE_NAME; $sql = "SELECT * FROM $table_name WHERE id = '$id'"; $res = $wpdb->get_row($sql,OBJECT); - + if (isset($res)): if($geoJSON){ $r = json_decode($res->points); $res->geoJSONpoints = $this->toGeoJSON($r); $res->points = $r; }else{ $res->points = json_decode($res->points); - } + } + endif; return $res; }// get_walk diff --git a/soundwalk.php b/soundwalk.php index 7f0949f..f6c98d2 100644 --- a/soundwalk.php +++ b/soundwalk.php @@ -33,7 +33,8 @@ if (!class_exists('SoundWalk')){ function register_actions(){ add_action('admin_menu',array($this, 'admin_menu')); add_action('wp_ajax_save-walk',array($this,'save_walk')); - add_action('wp_ajax_get-walk',array($this,'get_walk')); + add_action('wp_ajax_get-walk',array($this,'get_walk')); + add_action( 'rest_api_init', array($this,'soundwalk_rest_api_init')); /* add_action('wp_ajax_areago_file_uploaded',array($this,'areago_ajax_file_uploaded')); add_filter('media_send_to_editor', array($this, 'areago_media_send_to_editor'), 50, 3); @@ -41,6 +42,53 @@ if (!class_exists('SoundWalk')){ } //register_actions + function soundwalk_rest_api_init() { + // List Walks + register_rest_route('soundwalk/v1', '/walk', array( + 'methods' => 'GET', + 'callback' => array($this,'soundwalk_rest_get_walk'), + 'permission_callback' => '__return_true' + ) + ); + + register_rest_route('soundwalk/v1', '/walk/(?P\d+)', array( + 'methods' => 'GET', + 'callback' => array($this,'soundwalk_rest_get_walk'), + 'permission_callback' => '__return_true' + ) + ); + + + + } + + function soundwalk_rest_get_walk ($data) { + $walks = array(); + $db_helper = new Soundwalk_DB_Helper(); + if (!isset($data['id'])) : + //get walks; + $walks = $db_helper->get_list_walks(); + else : + // get walk id + $w = $db_helper->get_walk($data['id']); + if (isset($w)): + $walks = array( + 'points' => $w->points, + 'info' => array( + 'title' => $w->name, + 'description' => $w->description, + 'language' => $w->language, + 'fID' => $w->pic_id, + 'fileName' => wp_get_attachment_thumb_url( $w->pic_id ), + ), + 'id' => $data['id'] + ); + endif; + endif; + $response = new WP_REST_Response( $walks ); + return $response; + } + function get_walk(){ if (!isset($_POST['id'])){ wp_send_json_error("ID not available"); @@ -119,17 +167,18 @@ if (!class_exists('SoundWalk')){ wp_enqueue_script('soundwalk_app', plugins_url('js/app.js', __FILE__), array('soundwalk_views', 'leafletjs', 'backbone', 'underscore', 'jquery'), '0.1', TRUE); wp_enqueue_style('leafletcss','http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css',array(),'0.6.4','all'); // add CSS Leaflet wp_enqueue_style('soundwalk_app_css',plugins_url('soundwalk.css', __FILE__),array(),'0.1','all'); // add CSS Soundwalk app - global $soundmap; + + global $soundmap; $params = array(); if (isset($soundmap)) : - $params += $soundmap->config['origin']; - $params['mapType'] = $soundmap->config['mapType']; + $params += $soundmap->config['origin']; + $params['mapType'] = $soundmap->config['mapType']; - if (isset($_GET['walk']) && isset($_GET['action'])){ + if (isset($_GET['walk']) && isset($_GET['action'])){ if($_GET['action'] = "edit"){ $params['edit_Walk'] = $_GET['walk']; } - } + } endif; wp_localize_script('soundwalk_views','SoundwalkOptions',$params); @@ -247,7 +296,5 @@ function soundwalk_install(){ }// areago_install - - add_action("init", array($soundwalk , "init")); add_filter( 'generate_rewrite_rules',array($soundwalk, 'generate_rewrite_rules' ));