squash new oauth2 flow

This commit is contained in:
lesion
2022-11-04 12:22:21 +01:00
parent 19ca1b0283
commit 44e9533828
24 changed files with 935 additions and 427 deletions

View File

@@ -3,7 +3,7 @@
Plugin Name: WPGancio
Plugin URI: https://gancio.org
Description: Connects an user of a gancio instance to a Wordpress user so that published events are automatically pushed with Gancio API.
Version: 1.0
Version: 1.4
Author: Gancio
License: AGPL 3.0
@@ -20,9 +20,11 @@ along with (WPGancio). If not, see (https://www.gnu.org/licenses/agpl-3.0.html).
*/
defined( 'ABSPATH' ) or die( 'Nope, not accessing this' );
require_once('settings.php');
require_once('wc.php');
require_once('oauth.php');
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');
/**

View File

@@ -0,0 +1,112 @@
<?php
defined( 'ABSPATH' ) or die( 'Nope, not accessing this' );
// https://codex.wordpress.org/Settings_API
if (!is_network_admin()) { return; }
// Fires before the administration menu loads in the admin, add our options page
// add_action( 'admin_menu', 'wpgancio_options_page' );
add_action('network_admin_menu', 'wpgancio_network_options_page');
add_action('network_admin_edit_wpgancio', 'wpgancio_update');
// add_action( 'update_option_wpgancio_instance_url', 'wpgancio_update_options', 15, 2);
function wpgancio_update () {
$instance_url = get_site_option('wpgancio_instance_url');
// // check_admin_referer( $this->settings_slug . '-page-options' );
// function wpgancio_update_options ($old_value, $instance_url) {
$redirect_uri = network_admin_url('settings.php?page=wpgancio');
$query = join('&', array(
'response_type=code',
'redirect_uri=' . esc_url($redirect_uri),
'scope=event:write',
'client_id=' . get_site_option('wpgancio_client_id'),
));
wp_redirect("${instance_url}/oauth/authorize?${query}");
exit;
}
function wpgancio_network_options_page () {
add_submenu_page('settings.php', 'Gancio', 'Gancio', 'manage_options', 'wpgancio', 'wpgancio_network_options_page_html');
}
// 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.
// $args is defined at the add_settings_field() function.
// wordpress has magic interaction with the following keys: label_for, class.
// the "label_for" key value is used for the "for" attribute of the <label>.
// the "class" key value is used for the "class" attribute of the <tr> containing the field.
// you can add custom key value pairs to be used inside your callbacks.
/**
* top level menu:
* callback functions
*/
function wpgancio_network_options_page_html() {
// check user capabilities
if (! current_user_can('manage_network_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 ( $response['response']['code'] != 200 ) {
add_settings_error('wpgancio_messages', 'wpgancio_messages', $response['response']['code'] . ' ' . 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' );
}
}
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="edit.php?action=wpgancio" method="post">
<?php
// output security fields for the registered setting "wpgancio"
settings_fields('wpgancio');
// output setting sections and their fields
// (sections are registered for "wpgancio", each field is registered to a specific section)
do_settings_sections('wpgancio');
// output save settings button
submit_button('Save Settings');
?>
</form>
</div>
<?php
}

View File

@@ -8,7 +8,7 @@ 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');
$instance_url = get_option('wpgancio_instance_url', get_site_option('wpgancio_instance_url'));
if ($post->post_type == 'event') {
$gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE);
@@ -17,7 +17,7 @@ function wpgancio_delete_post ($post_id) {
$http->request( "${instance_url}/api/event/${gancio_id}", array(
'method' => 'DELETE',
'headers' => array (
'Authorization' => 'Bearer ' . get_option('wpgancio_token')
'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token'))
)));
}
}
@@ -48,7 +48,8 @@ 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);
$instance_url = get_option('wpgancio_instance_url');
$instance_url = get_option('wpgancio_instance_url', get_site_option('wpgancio_instance_url'));
$body = array (
'title' => $event->post_title,
@@ -56,7 +57,7 @@ function wpgancio_save_event ($post_id) {
'description' => $event->post_content,
'start_datetime' => intval($date),
'place_name' => $place_name,
'place_address' => "${place_address['address']}${place_address['city']}"
'place_address' => "${place_address['address']}, ${place_address['city']}"
);
// add image if specified
@@ -72,13 +73,13 @@ function wpgancio_save_event ($post_id) {
$response = $http->request( $instance_url . '/api/event', array(
'method' => 'PUT',
'headers' => array (
'Authorization' => 'Bearer ' . get_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'),
'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')),
'Content-Type' => 'application/json'
), 'body' => wp_json_encode($body) ));
}

View File

@@ -3,7 +3,7 @@ Contributors: lesion
Donate link: https://gancio.org
Tags: events, gancio, fediverse, AP, activity pub
Requires at least: 4.7
Tested up to: 5.9
Tested up to: 6.0
Stable tag: 1.4
Requires PHP: 7.0
License: AGPLv3 or later

View File

@@ -3,23 +3,43 @@ defined( 'ABSPATH' ) or die( 'Nope, not accessing this' );
// https://codex.wordpress.org/Settings_API
// Fires as an admin screen or script is being initialized. Register out settings
add_action( 'admin_init', 'wpgancio_settings_init' );
function wpgancio_settings_init() {
if (is_network_admin()) {
add_action('network_admin_menu', 'wpgancio_settings_init');
} else {
add_action('admin_menu', 'wpgancio_settings_init');
}
add_action('add_meta_boxes_event', 'wpgancio_remove_meta_boxes', 10, 2);
function wpgancio_remove_meta_boxes () {
remove_meta_box('postcustom', 'event', 'normal');
}
function wpgancio_settings_init() {
// register a new settings page
add_settings_section('wpgancio_settings', __('Settings'), FALSE, 'wpgancio');
add_settings_section('wpgancio_settings', __('Settings'), false, 'wpgancio');
// register a new field in the 'wpgancio_settings' section
add_settings_field('wpgancio_instance_url', __( 'Instance URL', 'wpgancio' ),
add_settings_field('wpgancio_instance_url',
__('Instance URL', 'wpgancio'),
'wpgancio_instance_url_cb', 'wpgancio',
'wpgancio_settings');
'wpgancio_settings'
);
register_setting( 'wpgancio', 'wpgancio_instance_url', 'wpgancio_instance_url_validate' );
register_setting('wpgancio', 'wpgancio_instance_url', 'wpgancio_instance_url_validate');
register_setting('wpgancio', 'wpgancio_client_id');
register_setting('wpgancio', 'wpgancio_client_secret');
register_setting('wpgancio', 'wpgancio_token');
}
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' );
if (!is_network_admin()) {
$redirect_uri = admin_url('options-general.php?page=wpgancio');
} else {
$redirect_uri = network_admin_url('settings.php?page=wpgancio');
}
$query = join('&', array(
'response_type=code',
'redirect_uri=' . esc_url($redirect_uri),
@@ -27,18 +47,30 @@ function wpgancio_update_options ($old_value, $instance_url) {
'client_id=' . get_option('wpgancio_client_id'),
));
wp_redirect("${instance_url}/authorize?${query}");
wp_redirect("${instance_url}/oauth/authorize?${query}");
// return $instance_url;
exit;
}
// Fires before the administration menu loads in the admin, add our options page
add_action( 'admin_menu', 'wpgancio_options_page' );
add_action('admin_menu', 'wpgancio_options_page');
function wpgancio_instance_url_validate ($instance_url) {
$redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' );
$old_instance_url = get_option('wpgancio_instance_url');
if ($instance_url === $old_instance_url) {
return $instance_url;
}
if (!is_network_admin()) {
$redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio');
} else {
$redirect_uri = get_site_url(null, '/wp-admin/network/settings.php?page=wpgancio');
}
// create this WP instance as a new client in selected gancio instance
$response = wp_remote_post( "$instance_url/api/client", array(
$response = wp_remote_post("$instance_url/api/client", array(
'method' => 'POST',
'body' => array(
'client_name' => 'WPGancio',
@@ -48,13 +80,18 @@ function wpgancio_instance_url_validate ($instance_url) {
)
));
if ( is_wp_error( $response ) ) {
if (is_wp_error($response)) {
add_settings_error('wpgancio_messages', 'wpgancio_messages',
$response->get_error_message());
} else {
$data = json_decode( wp_remote_retrieve_body($response), true);
update_option('wpgancio_client_secret', sanitize_key($data['client_secret']));
update_option('wpgancio_client_id', sanitize_key($data['client_id']));
if (!is_network_admin()) {
update_option('wpgancio_client_secret', sanitize_key($data['client_secret']));
update_option('wpgancio_client_id', sanitize_key($data['client_id']));
} else {
update_site_option('wpgancio_client_secret', sanitize_key($data['client_secret']));
update_site_option('wpgancio_client_id', sanitize_key($data['client_id']));
}
return $instance_url;
}
}
@@ -79,12 +116,14 @@ function wpgancio_options_page() {
// you can add custom key value pairs to be used inside your callbacks.
function wpgancio_instance_url_cb( $args ) {
// get the value of the setting we've registered with register_setting()
$instance_url = get_option( 'wpgancio_instance_url' );
if (empty($instance_url)) {
$instance_url = WP_GANCIO_DEFAULT_INSTANCEURL;
if (is_network_admin()) {
$instance_url = get_site_option( 'wpgancio_instance_url' );
} else {
$instance_url = get_option( 'wpgancio_instance_url' );
}
// output the field
?>
// output the field
?>
<input id="wpgancio_instance_url"
value="<?php echo esc_attr($instance_url); ?>"
@@ -104,13 +143,13 @@ function wpgancio_instance_url_cb( $args ) {
*/
function wpgancio_options_page_html() {
// check user capabilities
if ( ! current_user_can( 'manage_options' ) ) { return; }
if (! current_user_can('manage_options')) { return; }
// show error/update messages
$code = sanitize_key($_GET['code']);
$code = sanitize_key(isset($_GET['code']) ? $_GET['code'] : '');
if ( $code ) {
update_option('wpgancio_code', $code);
$instance_url = get_option( 'wpgancio_instance_url' );
$instance_url = get_option('wpgancio_instance_url');
$response = wp_remote_post($instance_url . "/oauth/token", array(
'body' => array(
@@ -120,18 +159,18 @@ function wpgancio_options_page_html() {
'grant_type' => 'authorization_code',
'code' => $code
)));
if ( is_wp_error( $response ) ) {
if (is_wp_error($response)) {
add_settings_error('wpgancio_messages', 'wpgancio_messages', $response->get_error_message());
settings_errors( 'wpgancio_messages' );
} else if ( $response['response']['code'] == 500 ) {
settings_errors('wpgancio_messages');
} elseif ($response['response']['code'] == 500) {
add_settings_error('wpgancio_messages', 'wpgancio_messages', wp_remote_retrieve_body($response));
settings_errors( 'wpgancio_messages' );
settings_errors('wpgancio_messages');
} else {
$data = json_decode( wp_remote_retrieve_body($response), true);
$data = json_decode(wp_remote_retrieve_body($response), true);
update_option('wpgancio_token', sanitize_key($data['access_token']));
update_option('wpgancio_refresh', sanitize_key($data['refresh_token']));
add_settings_error('wpgancio_messages', 'wpgancio_messages', 'Association completed!', 'success');
settings_errors( 'wpgancio_messages' );
settings_errors('wpgancio_messages');
}
}
@@ -139,19 +178,20 @@ function wpgancio_options_page_html() {
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
// output security fields for the registered setting "wpgancio"
settings_fields( 'wpgancio' );
settings_fields('wpgancio');
// output setting sections and their fields
// (sections are registered for "wpgancio", each field is registered to a specific section)
do_settings_sections( 'wpgancio' );
do_settings_sections('wpgancio');
// output save settings button
submit_button( 'Save Settings' );
submit_button('Save Settings');
?>
</form>
</div>

View File

@@ -39,10 +39,10 @@ function gancio_events_handler_function( $atts, $content, $tag) {
'places' => '',
'tags' => '',
'theme' => 'dark',
'max' => NULL
'max' => null
), $atts);
return '<gancio-events baseurl="' . $a['baseurl'] . '" theme="' . $a['theme'] . '" places="' . $a['places'] . '" tags="' . $a['tags'] . '"></gancio-events>';
};
}
add_action( 'init', function () {
global $allowedposttags;