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,7 +88,7 @@ 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);
@ -96,6 +96,7 @@ if (!class_exists('Soundwalk_DB_Helper')){
}else{
$res->points = json_decode($res->points);
}
endif;
return $res;
}// get_walk

View File

@ -34,6 +34,7 @@ if (!class_exists('SoundWalk')){
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( '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<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(){
if (!isset($_POST['id'])){
wp_send_json_error("ID not available");
@ -119,6 +167,7 @@ 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;
$params = array();
if (isset($soundmap)) :
@ -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' ));