fix wpgancio wp MU

This commit is contained in:
lesion
2022-12-12 15:37:54 +01:00
parent 8804a76478
commit 9bd2fcf2f1
4 changed files with 153 additions and 45 deletions

View File

@@ -8,16 +8,16 @@ add_action('wp_trash_post', 'wpgancio_delete_post', 15);
function wpgancio_delete_post ($post_id) {
$post = get_post($post_id);
$instance_url = get_option('wpgancio_instance_url', get_site_option('wpgancio_instance_url'));
if ($post->post_type == 'event') {
$instance_url = get_option('wpgancio_instance_url') ?: get_site_option('wpgancio_instance_url');
$gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE);
if ($gancio_id) {
$http = _wp_http_get_object();
$http->request( "${instance_url}/api/event/${gancio_id}", array(
'method' => 'DELETE',
'headers' => array (
'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token'))
'Authorization' => 'Bearer ' . (get_option('wpgancio_token') ?: get_site_option('wpgancio_token'))
)));
}
}
@@ -42,16 +42,13 @@ function wpgancio_save_event ($post_id) {
$gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE);
// when
$start_datetime = eo_get_the_start( 'U', $post_id );
$end_datetime = eo_get_the_end('U', $post_id);
$start_datetime = eo_get_schedule_start( 'U', $post_id );
// 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);
$instance_url = get_option('wpgancio_instance_url', get_site_option('wpgancio_instance_url'));
$instance_url = get_option('wpgancio_instance_url') ?: get_site_option('wpgancio_instance_url');
$body = array (
'title' => $event->post_title,
@@ -76,22 +73,52 @@ function wpgancio_save_event ($post_id) {
$response = $http->request( $instance_url . '/api/event', array(
'method' => 'PUT',
'headers' => array (
'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')),
'Authorization' => 'Bearer ' . (get_option('wpgancio_token') ?: get_site_option('wpgancio_token')),
'Content-Type' => 'application/json'
), 'body' => wp_json_encode($body) ));
} else { // or create
$response = wp_remote_post($instance_url . '/api/event', array(
'headers' => array (
'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')),
'Authorization' => 'Bearer ' . (get_option('wpgancio_token') ?: get_site_option('wpgancio_token')),
'Content-Type' => 'application/json'
), 'body' => wp_json_encode($body) ));
}
if ( is_wp_error( $response ) ) {
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
echo "<div class='error notice'><p>" . esc_html($error_message) . "</p></div>";
set_transient("wpgancio_error_", esc_html($error_message), 45);
return;
}
if (wp_remote_retrieve_response_code($response) != 200) {
set_transient("wpgancio_error_{$post_id}", wp_remote_retrieve_body($response), 45);
return;
}
$data = json_decode(wp_remote_retrieve_body($response));
$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>");
update_post_meta($post_id, 'wpgancio_gancio_id', intval($data->id));
}
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}");
}
}