From 1ee9cdc1fafb4fea903b2d3f420d1feb471e75c3 Mon Sep 17 00:00:00 2001 From: les Date: Fri, 22 May 2020 00:28:42 +0200 Subject: [PATCH] [wpgancio] remove event when trashed --- assets/style.less | 10 +++--- components/Search.vue | 3 +- wp-plugin/oauth.php | 25 ++++++++++++--- wp-plugin/settings.php | 73 +++++++++++++++++++++--------------------- 4 files changed, 64 insertions(+), 47 deletions(-) diff --git a/assets/style.less b/assets/style.less index 800b555a..3e7e6f18 100644 --- a/assets/style.less +++ b/assets/style.less @@ -18,7 +18,7 @@ code { background: rgba(0,0,0,.1); color: #888; } - + html, body { font-family: sans-serif; scroll-behavior: smooth; @@ -38,7 +38,7 @@ p { display: flex; flex-direction: column; } - + #main { min-height: 200px; overflow: hidden; @@ -56,7 +56,7 @@ p { width: 100%; margin: 0 auto; } - + #header, #footer { color: white; background-color: #222; @@ -67,7 +67,7 @@ p { // } } - + #footer { a { color: orangered; @@ -85,7 +85,7 @@ p { padding-top: 2em; font-size: 1em; } - + #header .el-menu--horizontal { background-color: #222; color: white; diff --git a/components/Search.vue b/components/Search.vue index ce47e73d..1247bf54 100644 --- a/components/Search.vue +++ b/components/Search.vue @@ -46,7 +46,8 @@ export default { }, keywords () { const tags = this.tags.filter(t => !this.filters.tags.includes(t.tag)).map(t => ({ type: 'tag', label: t.tag, weigth: t.weigth, id: t.tag })) - const places = this.places.filter(p => !this.filters.places.includes(p.id)).map(p => ({ type: 'place', label: p.name, weigth: p.weigth, id: p.id })) + const places = this.places.filter(p => !this.filters.places.includes(p.id)) + .map(p => ({ type: 'place', label: p.name, weigth: p.weigth, id: p.id })) const keywords = tags.concat(places).sort((a, b) => b.weigth - a.weigth) return keywords }, diff --git a/wp-plugin/oauth.php b/wp-plugin/oauth.php index c3fd954b..75470b52 100644 --- a/wp-plugin/oauth.php +++ b/wp-plugin/oauth.php @@ -4,6 +4,25 @@ defined( 'ABSPATH' ) or die( 'Nope, not accessing this' ); // eventorganizer / triggered after an event has been updated // http://codex.wp-event-organiser.com/hook-eventorganiser_save_event.html add_action('eventorganiser_save_event', 'wpgancio_save_event', 15); +add_action('wp_trash_post', 'delete_post', 15); + +function delete_post ($post_id) { + $post = get_post($post_id); + $instance_url = get_option('wpgancio_instance_url'); + + if ($post->post_type == 'event') { + $gancio_id = get_post_meta($post_id, 'gancio_id', TRUE); + if ($gancio_id) { + $body['id'] = $gancio_id; + $http = _wp_http_get_object(); + $response = $http->request( "${instance_url}/api/event/${gancio_id}", array( + 'method' => 'DELETE', + 'headers' => array ( + 'Authorization' => 'Bearer ' . get_option('wpgancio_token') + ))); + } + } +} function wpgancio_save_event ($post_id) { $event = get_post( $post_id ); @@ -22,15 +41,14 @@ function wpgancio_save_event ($post_id) { $venue_id = eo_get_venue($post_id); $place_name = eo_get_venue_name($venue_id); $place_address = eo_get_venue_address($venue_id); - $options = get_option('wpgancio_options'); - $instance_url = $options['wpgancio_field_url']; + $instance_url = get_option('wpgancio_instance_url'); $body = array ( 'title' => $event->post_title, 'description' => $event->post_content, 'start_datetime' => intval($date), 'place_name' => $place_name, - 'place_address' => $place_address, + 'place_address' => "${place_address['address']}${place_address['city']}" ); // add image if specified @@ -60,7 +78,6 @@ function wpgancio_save_event ($post_id) { echo "

${error_message}

"; return; } - $data = json_decode(wp_remote_retrieve_body($response)); update_post_meta($post_id, 'gancio_id', $data->id); } diff --git a/wp-plugin/settings.php b/wp-plugin/settings.php index 057770ec..55e57712 100644 --- a/wp-plugin/settings.php +++ b/wp-plugin/settings.php @@ -1,28 +1,40 @@ 'wpgancio_field_url' ] ); + add_settings_field('wpgancio_instance_url', __( 'Instance URL', 'wpgancio' ), + 'wpgancio_instance_url_cb', 'wpgancio', + 'wpgancio_settings'); + + register_setting( 'wpgancio', 'wpgancio_instance_url', 'wpgancio_instance_url_validate' ); } -add_action( 'update_option_wpgancio_options', 'wpgancio_update_options', 15, 2); +add_action( 'update_option_wpgancio_instance_url', 'wpgancio_update_options', 15, 2); +function wpgancio_update_options ($old_value, $instance_url) { + $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' ); + $query = join('&', array( + 'response_type=code', + 'redirect_uri=' . esc_html($redirect_uri), + 'scope=event:write', + 'client_id=' . get_option('wpgancio_client_id'), + )); + + wp_redirect("${instance_url}/authorize?${query}"); + exit; +} // Fires before the administration menu loads in the admin, add our options page add_action( 'admin_menu', 'wpgancio_options_page' ); -function wpgancio_update_options ($old_value, $new_value) { - $instance_url = $new_value['wpgancio_field_url']; +function wpgancio_instance_url_validate ($instance_url) { $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' ); // create this WP instance as a new client in selected gancio instance @@ -43,29 +55,18 @@ function wpgancio_update_options ($old_value, $new_value) { $data = json_decode( wp_remote_retrieve_body($response), true); update_option('wpgancio_client_secret', $data['client_secret']); update_option('wpgancio_client_id', $data['client_id']); - $query = join('&', array( - 'response_type=code', - 'redirect_uri=' . esc_html($redirect_uri), - 'scope=event:write', - 'client_id=' . get_option('wpgancio_client_id'), - 'state=antani' - )); - - wp_redirect("${instance_url}/authorize?${query}"); - exit; + return $instance_url; } -}; - - +} function wpgancio_options_page() { // add top level menu page add_options_page( - 'Gancio', - 'Gancio', - 'manage_options', - 'wpgancio', - 'wpgancio_options_page_html' + 'Gancio', + 'Gancio', + 'manage_options', + 'wpgancio', + 'wpgancio_options_page_html' ); } @@ -76,16 +77,15 @@ function wpgancio_options_page() { // the "label_for" key value is used for the "for" attribute of the