diff --git a/wp-plugin/gancio.php b/wp-plugin/gancio.php new file mode 100644 index 00000000..5a3bf4e4 --- /dev/null +++ b/wp-plugin/gancio.php @@ -0,0 +1,245 @@ + 'wpgancio_field_url', + 'class' => 'wpgancio_row', + 'wpgancio_custom_data' => 'custom', + ] + ); +} + +/** + * register our wpgancio_settings_init to the admin_init action hook + */ +add_action( 'admin_init', 'wpgancio_settings_init' ); +add_action( 'update_option_wpgancio_options', 'wpgancio_update_options', 15, 2); + +function wpgancio_update_options ($old_value, $new_value) { + $instance_url = $new_value['wpgancio_field_url']; + $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' ); + // wp_redirect(get_option('wpgancio_field_url')); + $response = wp_remote_post( "$instance_url/api/client", array( + 'method' => 'POST', + 'body' => array( + 'client_name' => 'WPGancio', + 'redirect_uris' => $redirect_uri, + 'scopes' => 'event:write', + 'website' => 'https://gancio.org' + ) + )); + + if ( is_wp_error( $response ) ) { + $error_message = $response->get_error_message(); + echo "Something went wrong: $error_message"; + exit; + } + $data = json_decode( wp_remote_retrieve_body($response), true); + // var_dump($data); + update_option('wpgancio_client_secret', $data['client_secret']); + update_option('wpgancio_client_id', $data['client_id']); + wp_redirect($instance_url . "/authorize?response_type=code&redirect_uri=$redirect_uri&scope=event:write&client_id=" . get_option('wpgancio_client_id')); + exit; +}; + +function wpgancio_update_event ($post_id) { + $event = get_post( $post_id ); + $date = eo_get_schedule_start( 'U', $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']; + $response = wp_remote_post($instance_url . '/api/event', array( + 'headers' => array ( + 'Authorization' => 'Bearer ' . get_option('wpgancio_token') + ), + 'body' => array( + 'title' => $event->post_title, + 'description' => $event->post_content, + 'start_datetime' => intval($date), + 'place_name' => $place_name, + 'place_address' => $place_address + ) + )); + if ( is_wp_error( $response ) ) { + $error_message = $response->get_error_message(); + echo "Something went wrong: $error_message"; + exit; + } + var_dump($response); + $data = wp_remote_retrieve_body($response); + // syslog(1, "sono qua!"); +} + +add_action('eventorganiser_save_event', 'wpgancio_update_event', 15); + +/** + * custom option and settings: + * callback functions + */ + +// developers section cb +// section callbacks can accept an $args parameter, which is an array. +// $args have the following keys defined: title, id, callback. +// the values are defined at the add_settings_section() function. +function wpgancio_section_developers_cb( $args ) { + ?> +
+ . +// the "class" key value is used for the "class" attribute of the+ +
+ array( + 'client_id' => get_option('wpgancio_client_id'), + 'client_secret' => get_option('wpgancio_client_secret'), + 'scope' => 'event:write', + 'grant_type' => 'authorization_code', + 'code' => $code, + 'state' => 'antani' + ))); + if ( is_wp_error( $response ) ) { + $error_message = $response->get_error_message(); + echo "Something went wrong: $error_message"; + exit; + } + $data = json_decode( wp_remote_retrieve_body($response), true); + update_option('wpgancio_token', $data['access_token']); + update_option('wpgancio_refresh', $data['refresh_token']); + } + + // check user capabilities + if ( ! current_user_can( 'manage_options' ) ) { return; } + + // add error/update messages + // check if the user have submitted the settings + // wordpress will add the "settings-updated" $_GET parameter to the url + if ( isset( $_GET['settings-updated'] ) ) { + // add settings saved message with the class of "updated" + add_settings_error( 'wpgancio_messages', 'wpgancio_message', __( 'Settings Saved', 'wpgancio' ), 'updated' ); + } + + // show error/update messages + settings_errors( 'wpgancio_messages' ); + +?> + +