Add: REST API walk

This commit is contained in:
lrullo 2022-12-26 18:34:21 +01:00
parent 01849116d7
commit 4b424f3872
3 changed files with 75 additions and 10 deletions

17
README.md Normal file
View File

@ -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/<id>
Return walks with ID

View File

@ -88,14 +88,15 @@ if (!class_exists('Soundwalk_DB_Helper')){
$table_name = $wpdb->prefix . self::TABLE_NAME; $table_name = $wpdb->prefix . self::TABLE_NAME;
$sql = "SELECT * FROM $table_name WHERE id = '$id'"; $sql = "SELECT * FROM $table_name WHERE id = '$id'";
$res = $wpdb->get_row($sql,OBJECT); $res = $wpdb->get_row($sql,OBJECT);
if (isset($res)):
if($geoJSON){ if($geoJSON){
$r = json_decode($res->points); $r = json_decode($res->points);
$res->geoJSONpoints = $this->toGeoJSON($r); $res->geoJSONpoints = $this->toGeoJSON($r);
$res->points = $r; $res->points = $r;
}else{ }else{
$res->points = json_decode($res->points); $res->points = json_decode($res->points);
} }
endif;
return $res; return $res;
}// get_walk }// get_walk

View File

@ -33,7 +33,8 @@ if (!class_exists('SoundWalk')){
function register_actions(){ function register_actions(){
add_action('admin_menu',array($this, 'admin_menu')); add_action('admin_menu',array($this, 'admin_menu'));
add_action('wp_ajax_save-walk',array($this,'save_walk')); 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_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); 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 } //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<id>\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(){ function get_walk(){
if (!isset($_POST['id'])){ if (!isset($_POST['id'])){
wp_send_json_error("ID not available"); 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_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('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 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(); $params = array();
if (isset($soundmap)) : if (isset($soundmap)) :
$params += $soundmap->config['origin']; $params += $soundmap->config['origin'];
$params['mapType'] = $soundmap->config['mapType']; $params['mapType'] = $soundmap->config['mapType'];
if (isset($_GET['walk']) && isset($_GET['action'])){ if (isset($_GET['walk']) && isset($_GET['action'])){
if($_GET['action'] = "edit"){ if($_GET['action'] = "edit"){
$params['edit_Walk'] = $_GET['walk']; $params['edit_Walk'] = $_GET['walk'];
} }
} }
endif; endif;
wp_localize_script('soundwalk_views','SoundwalkOptions',$params); wp_localize_script('soundwalk_views','SoundwalkOptions',$params);
@ -247,7 +296,5 @@ function soundwalk_install(){
}// areago_install }// areago_install
add_action("init", array($soundwalk , "init")); add_action("init", array($soundwalk , "init"));
add_filter( 'generate_rewrite_rules',array($soundwalk, 'generate_rewrite_rules' )); add_filter( 'generate_rewrite_rules',array($soundwalk, 'generate_rewrite_rules' ));