diff --git a/wp-plugin/gancio.php b/wp-plugin/gancio.php index 4a33a539..2b117932 100644 --- a/wp-plugin/gancio.php +++ b/wp-plugin/gancio.php @@ -22,11 +22,12 @@ along with (WPGancio). If not, see (https://www.gnu.org/licenses/agpl-3.0.html). defined('ABSPATH') or die('Nope, not accessing this'); define('WPGANCIO_DIR', plugin_dir_path(__FILE__)); require_once(WPGANCIO_DIR . 'settings.php'); -require_once(WPGANCIO_DIR . 'network_settings.php'); + require_once(WPGANCIO_DIR . 'wc.php'); require_once(WPGANCIO_DIR . 'oauth.php'); + /** * What does WPGancio do? * This plugin connects a user of a gancio instance to a Wordpress user so that events published diff --git a/wp-plugin/oauth.php b/wp-plugin/oauth.php index ea64928c..41fe35c1 100644 --- a/wp-plugin/oauth.php +++ b/wp-plugin/oauth.php @@ -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 "

" . esc_html($error_message) . "

"; + 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. {$event_url}"); 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}" ) ) { ?> +
+

[WPGancio]

+
+
+

[WPGancio]

+
"code", + "redirect_uri" => $redirect_uri, + "scope" => "event:write", + "client_id" => $client_id ), "${instance_url}/oauth/authorize")); // return $instance_url; exit; } +add_action( 'network_admin_edit_wpgancio_instance_url', 'wpgancio_instance_url_save_settings' ); + +function wpgancio_instance_url_save_settings(){ + + // check_admin_referer( 'misha-validate' ); // Nonce security check + + if (isset($_POST['wpgancio_instance_url'])) { + update_site_option( 'wpgancio_instance_url', $_POST['wpgancio_instance_url'] ); + } else { + delete_site_option( 'wpgancio_instance_url'); + } + + + wp_redirect( add_query_arg( array( + 'page' => 'wpgancio', + 'updated' => true ), network_admin_url('settings.php?page=wpgancio') + )); + + exit; + +} + + + // Fires before the administration menu loads in the admin, add our options page -add_action('admin_menu', 'wpgancio_options_page'); function wpgancio_instance_url_validate ($instance_url) { - $old_instance_url = get_option('wpgancio_instance_url'); - if ($instance_url === $old_instance_url) { - return $instance_url; + + if (!is_network_admin()) { + $old_instance_url = get_option('wpgancio_instance_url'); + if ($instance_url === $old_instance_url) { + return $instance_url; + } + } else { + $old_instance_url = get_site_option('wpgancio_instance_url'); + if ($instance_url === $old_instance_url) { + return $instance_url; + } } if (!is_network_admin()) { @@ -96,16 +143,7 @@ function wpgancio_instance_url_validate ($instance_url) { } } -function wpgancio_options_page() { - // add top level menu page - add_options_page( - 'Gancio', - 'Gancio', - 'manage_options', - 'wpgancio', - 'wpgancio_options_page_html' - ); -} + // instance url field cb // field callbacks can accept an $args parameter, which is an array. @@ -129,9 +167,7 @@ function wpgancio_instance_url_cb( $args ) { value="" name="wpgancio_instance_url"> -

- -

+

Instance URL you want to publish events to.

get_error_message()); settings_errors('wpgancio_messages'); - } elseif ($response['response']['code'] == 500) { + } elseif ($response['response']['code'] != 200) { add_settings_error('wpgancio_messages', 'wpgancio_messages', wp_remote_retrieve_body($response)); settings_errors('wpgancio_messages'); } else { @@ -173,15 +209,56 @@ function wpgancio_options_page_html() { settings_errors('wpgancio_messages'); } } + wpgancio_general_options_page_html(); +} +function wpgancio_network_options_page_html() { + // check user capabilities + if (! current_user_can('manage_site_options')) { return; } + + // show error/update messages + $code = sanitize_key(isset($_GET['code']) ? $_GET['code'] : ''); + if ( $code ) { + update_site_option('wpgancio_code', $code); + $instance_url = get_site_option('wpgancio_instance_url'); + + $response = wp_remote_post($instance_url . "/oauth/token", array( + 'body' => array( + 'client_id' => get_site_option('wpgancio_client_id'), + 'client_secret' => get_site_option('wpgancio_client_secret'), + 'scope' => 'event:write', + 'grant_type' => 'authorization_code', + 'code' => $code + ))); + + if (is_wp_error($response)) { + add_settings_error('wpgancio_messages', 'wpgancio_messages', $response->get_error_message()); + settings_errors('wpgancio_messages'); + } elseif (wp_remote_retrieve_response_code($response) != 200) { + add_settings_error('wpgancio_messages', 'wpgancio_messages', wp_remote_retrieve_body($response)); + settings_errors('wpgancio_messages'); + } else { + $data = json_decode(wp_remote_retrieve_body($response), true); + update_site_option('wpgancio_token', sanitize_key($data['access_token'])); + update_site_option('wpgancio_refresh', sanitize_key($data['refresh_token'])); + add_settings_error('wpgancio_messages', 'wpgancio_messages', 'Association completed!', 'success'); + settings_errors('wpgancio_messages'); + } + } + wpgancio_general_options_page_html(); +} + +function wpgancio_general_options_page_html () { ?> -

-
- + + + +