2020-05-14 22:36:58 +02:00
|
|
|
<?php
|
|
|
|
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);
|
2021-07-04 00:45:17 +02:00
|
|
|
add_action('wp_trash_post', 'wpgancio_delete_post', 15);
|
2020-05-22 00:28:42 +02:00
|
|
|
|
2021-07-04 00:45:17 +02:00
|
|
|
function wpgancio_delete_post ($post_id) {
|
2020-05-22 00:28:42 +02:00
|
|
|
$post = get_post($post_id);
|
2022-12-12 15:37:54 +01:00
|
|
|
|
2020-05-22 00:28:42 +02:00
|
|
|
if ($post->post_type == 'event') {
|
2022-12-12 15:37:54 +01:00
|
|
|
$instance_url = get_option('wpgancio_instance_url') ?: get_site_option('wpgancio_instance_url');
|
2021-07-04 00:45:17 +02:00
|
|
|
$gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE);
|
2020-05-22 00:28:42 +02:00
|
|
|
if ($gancio_id) {
|
|
|
|
$http = _wp_http_get_object();
|
2022-06-07 21:13:12 +02:00
|
|
|
$http->request( "${instance_url}/api/event/${gancio_id}", array(
|
2020-05-22 00:28:42 +02:00
|
|
|
'method' => 'DELETE',
|
|
|
|
'headers' => array (
|
2022-12-12 15:37:54 +01:00
|
|
|
'Authorization' => 'Bearer ' . (get_option('wpgancio_token') ?: get_site_option('wpgancio_token'))
|
2020-05-22 00:28:42 +02:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 22:36:58 +02:00
|
|
|
|
|
|
|
function wpgancio_save_event ($post_id) {
|
|
|
|
$event = get_post( $post_id );
|
|
|
|
|
2022-11-19 13:18:36 +01:00
|
|
|
// do not save if it's a draft
|
|
|
|
if ($event->post_status != 'publish') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-02 22:33:41 +02:00
|
|
|
function tagName ($tag) {
|
2021-07-04 00:45:17 +02:00
|
|
|
return sanitize_title($tag->name);
|
2021-07-02 22:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$tmp_tags = get_the_terms( $event, 'event-tag' );
|
2023-03-23 13:09:10 +01:00
|
|
|
if ($tmp_tags) {
|
|
|
|
$tags = array_map('tagName', $tmp_tags);
|
|
|
|
}
|
2021-07-02 22:33:41 +02:00
|
|
|
|
2020-05-14 22:36:58 +02:00
|
|
|
|
2021-07-04 00:45:17 +02:00
|
|
|
$gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE);
|
2020-05-14 22:36:58 +02:00
|
|
|
|
2022-12-12 15:37:54 +01:00
|
|
|
$start_datetime = eo_get_schedule_start( 'U', $post_id );
|
2020-05-14 22:36:58 +02:00
|
|
|
|
|
|
|
// get place details
|
|
|
|
$venue_id = eo_get_venue($post_id);
|
|
|
|
$place_name = eo_get_venue_name($venue_id);
|
|
|
|
$place_address = eo_get_venue_address($venue_id);
|
2022-12-12 15:37:54 +01:00
|
|
|
$instance_url = get_option('wpgancio_instance_url') ?: get_site_option('wpgancio_instance_url');
|
2020-05-14 22:36:58 +02:00
|
|
|
|
|
|
|
$body = array (
|
|
|
|
'title' => $event->post_title,
|
2021-07-02 22:33:41 +02:00
|
|
|
'tags' => $tags,
|
2020-05-14 22:36:58 +02:00
|
|
|
'description' => $event->post_content,
|
2022-11-19 13:18:36 +01:00
|
|
|
'start_datetime' => intval($start_datetime),
|
2020-05-14 22:36:58 +02:00
|
|
|
'place_name' => $place_name,
|
2023-03-23 13:09:10 +01:00
|
|
|
'place_address' => $place_address['address'] . ", " . $place_address['city']
|
2020-05-14 22:36:58 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// add image if specified
|
|
|
|
$image_url = get_the_post_thumbnail_url($post_id);
|
|
|
|
if ($image_url) {
|
2021-07-04 00:45:17 +02:00
|
|
|
$body['image_url'] = esc_url($image_url);
|
2020-05-14 22:36:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
if ($gancio_id) {
|
|
|
|
$body['id'] = $gancio_id;
|
|
|
|
$http = _wp_http_get_object();
|
|
|
|
$response = $http->request( $instance_url . '/api/event', array(
|
|
|
|
'method' => 'PUT',
|
|
|
|
'headers' => array (
|
2022-12-12 15:37:54 +01:00
|
|
|
'Authorization' => 'Bearer ' . (get_option('wpgancio_token') ?: get_site_option('wpgancio_token')),
|
2021-07-02 22:33:41 +02:00
|
|
|
'Content-Type' => 'application/json'
|
|
|
|
), 'body' => wp_json_encode($body) ));
|
2020-05-14 22:36:58 +02:00
|
|
|
} else { // or create
|
|
|
|
$response = wp_remote_post($instance_url . '/api/event', array(
|
|
|
|
'headers' => array (
|
2022-12-12 15:37:54 +01:00
|
|
|
'Authorization' => 'Bearer ' . (get_option('wpgancio_token') ?: get_site_option('wpgancio_token')),
|
2021-07-02 22:33:41 +02:00
|
|
|
'Content-Type' => 'application/json'
|
|
|
|
), 'body' => wp_json_encode($body) ));
|
2020-05-14 22:36:58 +02:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:37:54 +01:00
|
|
|
if (is_wp_error($response)) {
|
2021-07-08 20:31:32 +02:00
|
|
|
$error_message = $response->get_error_message();
|
2022-12-12 15:37:54 +01:00
|
|
|
set_transient("wpgancio_error_", esc_html($error_message), 45);
|
2020-05-14 22:36:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-12-12 15:37:54 +01:00
|
|
|
|
|
|
|
if (wp_remote_retrieve_response_code($response) != 200) {
|
|
|
|
set_transient("wpgancio_error_{$post_id}", wp_remote_retrieve_body($response), 45);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:36:58 +02:00
|
|
|
$data = json_decode(wp_remote_retrieve_body($response));
|
2022-12-12 15:37:54 +01:00
|
|
|
|
|
|
|
$event_url = $instance_url . '/event/' . ($data->slug ? $data->slug : $data->id);
|
|
|
|
set_transient("wpgancio_message_{$post_id}", "Event updated. <a href='{$event_url}'>{$event_url}</a>");
|
2021-07-04 00:45:17 +02:00
|
|
|
update_post_meta($post_id, 'wpgancio_gancio_id', intval($data->id));
|
2020-05-14 22:36:58 +02:00
|
|
|
}
|
2022-12-12 15:37:54 +01:00
|
|
|
|
|
|
|
add_action( 'admin_notices', 'wpgancio_error_message' );
|
|
|
|
function wpgancio_error_message () {
|
|
|
|
global $post_id;
|
|
|
|
if ( $error = get_transient( "wpgancio_error_{$post_id}" ) ) { ?>
|
|
|
|
<div class="error">
|
|
|
|
<p>[WPGancio] <?php echo $error; ?></p>
|
|
|
|
</div><?php
|
|
|
|
delete_transient("wpgancio_error_{$post_id}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $message = get_transient( "wpgancio_message_{$post_id}" ) ) { ?>
|
|
|
|
<div class="notice success">
|
|
|
|
<p>[WPGancio] <?php echo $message; ?></p>
|
|
|
|
</div><?php
|
|
|
|
delete_transient("wpgancio_message_{$post_id}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|