Changeset 2824192
- Timestamp:
- 11/25/2022 12:04:30 PM (3 years ago)
- Location:
- wpgancio/trunk
- Files:
-
- 1 added
- 5 edited
-
gancio.php (modified) (3 diffs)
-
network_settings.php (added)
-
oauth.php (modified) (8 diffs)
-
readme.txt (modified) (2 diffs)
-
settings.php (modified) (7 diffs)
-
wc.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wpgancio/trunk/gancio.php
r2675565 r2824192 4 4 Plugin URI: https://gancio.org 5 5 Description: Connects an user of a gancio instance to a Wordpress user so that published events are automatically pushed with Gancio API. 6 Version: 1. 36 Version: 1.5 7 7 Author: Gancio 8 8 License: AGPL 3.0 … … 20 20 */ 21 21 22 defined( 'ABSPATH' ) or die( 'Nope, not accessing this' ); 23 require_once('settings.php'); 24 require_once('wc.php'); 25 require_once('oauth.php'); 22 defined('ABSPATH') or die('Nope, not accessing this'); 23 define('WPGANCIO_DIR', plugin_dir_path(__FILE__)); 24 require_once(WPGANCIO_DIR . 'settings.php'); 25 require_once(WPGANCIO_DIR . 'network_settings.php'); 26 require_once(WPGANCIO_DIR . 'wc.php'); 27 require_once(WPGANCIO_DIR . 'oauth.php'); 26 28 27 29 … … 39 41 * - Send each new / updated events to the selected instance via Gancio API 40 42 */ 41 -
wpgancio/trunk/oauth.php
r2560249 r2824192 9 9 function wpgancio_delete_post ($post_id) { 10 10 $post = get_post($post_id); 11 $instance_url = get_option('wpgancio_instance_url' );11 $instance_url = get_option('wpgancio_instance_url', get_site_option('wpgancio_instance_url')); 12 12 13 13 if ($post->post_type == 'event') { … … 15 15 if ($gancio_id) { 16 16 $http = _wp_http_get_object(); 17 $ response = $http->request( "${instance_url}/api/event/${gancio_id}", array(17 $http->request( "${instance_url}/api/event/${gancio_id}", array( 18 18 'method' => 'DELETE', 19 19 'headers' => array ( 20 'Authorization' => 'Bearer ' . get_option('wpgancio_token' )20 'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')) 21 21 ))); 22 22 } … … 27 27 $event = get_post( $post_id ); 28 28 29 function tagName ($tag) {30 return sanitize_title($tag->name);31 }32 33 $tmp_tags = get_the_terms( $event, 'event-tag' );34 $tags = array_map('tagName', $tmp_tags);35 36 29 // do not save if it's a draft 37 30 if ($event->post_status != 'publish') { … … 39 32 } 40 33 34 function tagName ($tag) { 35 return sanitize_title($tag->name); 36 } 37 38 // TODO: merge event tags with post tags 39 $tmp_tags = get_the_terms( $event, 'event-tag' ); 40 $tags = array_map('tagName', $tmp_tags); 41 42 41 43 $gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE); 42 44 43 45 // when 44 $date = eo_get_schedule_start( 'U', $post_id ); 46 $start_datetime = eo_get_the_start( 'U', $post_id ); 47 $end_datetime = eo_get_the_end('U', $post_id); 45 48 46 49 // get place details … … 48 51 $place_name = eo_get_venue_name($venue_id); 49 52 $place_address = eo_get_venue_address($venue_id); 50 $instance_url = get_option('wpgancio_instance_url'); 53 $instance_url = get_option('wpgancio_instance_url', get_site_option('wpgancio_instance_url')); 54 51 55 52 56 $body = array ( … … 54 58 'tags' => $tags, 55 59 'description' => $event->post_content, 56 'start_datetime' => intval($date), 60 'start_datetime' => intval($start_datetime), 61 'end_datetime' => intval($end_datetime), 57 62 'place_name' => $place_name, 58 'place_address' => "${place_address['address']} ${place_address['city']}"63 'place_address' => "${place_address['address']}, ${place_address['city']}" 59 64 ); 60 65 … … 72 77 'method' => 'PUT', 73 78 'headers' => array ( 74 'Authorization' => 'Bearer ' . get_option('wpgancio_token' ),79 'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')), 75 80 'Content-Type' => 'application/json' 76 81 ), 'body' => wp_json_encode($body) )); … … 78 83 $response = wp_remote_post($instance_url . '/api/event', array( 79 84 'headers' => array ( 80 'Authorization' => 'Bearer ' . get_option('wpgancio_token' ),85 'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')), 81 86 'Content-Type' => 'application/json' 82 87 ), 'body' => wp_json_encode($body) )); -
wpgancio/trunk/readme.txt
r2747168 r2824192 4 4 Tags: events, gancio, fediverse, AP, activity pub 5 5 Requires at least: 4.7 6 Tested up to: 5.97 Stable tag: 1. 46 Tested up to: 6.0 7 Stable tag: 1.5 8 8 Requires PHP: 7.0 9 9 License: AGPLv3 or later … … 19 19 20 20 == Changelog == 21 = 1.4 = 21 = 1.4 = 22 22 use `WP_GANCIO_DEFAULT_INSTANCEURL` as default instance url 23 23 -
wpgancio/trunk/settings.php
r2560249 r2824192 4 4 5 5 // Fires as an admin screen or script is being initialized. Register out settings 6 add_action( 'admin_init', 'wpgancio_settings_init' ); 6 if (is_network_admin()) { 7 add_action('network_admin_menu', 'wpgancio_settings_init'); 8 } else { 9 add_action('admin_menu', 'wpgancio_settings_init'); 10 } 11 12 add_action('add_meta_boxes_event', 'wpgancio_remove_meta_boxes', 10, 2); 13 function wpgancio_remove_meta_boxes () { 14 remove_meta_box('postcustom', 'event', 'normal'); 15 } 16 7 17 function wpgancio_settings_init() { 8 18 9 19 // register a new settings page 10 add_settings_section('wpgancio_settings', __('Settings'), FALSE, 'wpgancio');20 add_settings_section('wpgancio_settings', __('Settings'), false, 'wpgancio'); 11 21 12 22 // register a new field in the 'wpgancio_settings' section 13 add_settings_field('wpgancio_instance_url', __( 'Instance URL', 'wpgancio' ), 23 add_settings_field('wpgancio_instance_url', 24 __('Instance URL', 'wpgancio'), 14 25 'wpgancio_instance_url_cb', 'wpgancio', 15 'wpgancio_settings'); 26 'wpgancio_settings' 27 ); 16 28 17 register_setting( 'wpgancio', 'wpgancio_instance_url', 'wpgancio_instance_url_validate' ); 29 register_setting('wpgancio', 'wpgancio_instance_url', 'wpgancio_instance_url_validate'); 30 register_setting('wpgancio', 'wpgancio_client_id'); 31 register_setting('wpgancio', 'wpgancio_client_secret'); 32 register_setting('wpgancio', 'wpgancio_token'); 18 33 } 34 19 35 20 36 add_action( 'update_option_wpgancio_instance_url', 'wpgancio_update_options', 15, 2); 21 37 function wpgancio_update_options ($old_value, $instance_url) { 22 $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' ); 38 if (!is_network_admin()) { 39 $redirect_uri = admin_url('options-general.php?page=wpgancio'); 40 } else { 41 $redirect_uri = network_admin_url('settings.php?page=wpgancio'); 42 } 23 43 $query = join('&', array( 24 44 'response_type=code', … … 28 48 )); 29 49 30 wp_redirect("${instance_url}/authorize?${query}"); 50 wp_redirect("${instance_url}/oauth/authorize?${query}"); 51 // return $instance_url; 31 52 exit; 32 53 } 33 54 55 34 56 // Fires before the administration menu loads in the admin, add our options page 35 add_action( 'admin_menu', 'wpgancio_options_page');57 add_action('admin_menu', 'wpgancio_options_page'); 36 58 37 59 function wpgancio_instance_url_validate ($instance_url) { 38 $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio' ); 60 61 $old_instance_url = get_option('wpgancio_instance_url'); 62 if ($instance_url === $old_instance_url) { 63 return $instance_url; 64 } 65 66 if (!is_network_admin()) { 67 $redirect_uri = get_site_url(null, '/wp-admin/options-general.php?page=wpgancio'); 68 } else { 69 $redirect_uri = get_site_url(null, '/wp-admin/network/settings.php?page=wpgancio'); 70 } 39 71 40 72 // create this WP instance as a new client in selected gancio instance 41 $response = wp_remote_post( "$instance_url/api/client", array(73 $response = wp_remote_post("$instance_url/api/client", array( 42 74 'method' => 'POST', 43 75 'body' => array( … … 49 81 )); 50 82 51 if ( is_wp_error( $response )) {83 if (is_wp_error($response)) { 52 84 add_settings_error('wpgancio_messages', 'wpgancio_messages', 53 85 $response->get_error_message()); 54 86 } else { 55 87 $data = json_decode( wp_remote_retrieve_body($response), true); 56 update_option('wpgancio_client_secret', sanitize_key($data['client_secret'])); 57 update_option('wpgancio_client_id', sanitize_key($data['client_id'])); 88 if (!is_network_admin()) { 89 update_option('wpgancio_client_secret', sanitize_key($data['client_secret'])); 90 update_option('wpgancio_client_id', sanitize_key($data['client_id'])); 91 } else { 92 update_site_option('wpgancio_client_secret', sanitize_key($data['client_secret'])); 93 update_site_option('wpgancio_client_id', sanitize_key($data['client_id'])); 94 } 58 95 return $instance_url; 59 96 } … … 80 117 function wpgancio_instance_url_cb( $args ) { 81 118 // get the value of the setting we've registered with register_setting() 82 $instance_url = get_option( 'wpgancio_instance_url' ); 83 // output the field 84 ?> 119 if (is_network_admin()) { 120 $instance_url = get_site_option( 'wpgancio_instance_url' ); 121 } else { 122 $instance_url = get_option( 'wpgancio_instance_url' ); 123 } 124 125 // output the field 126 ?> 85 127 86 128 <input id="wpgancio_instance_url" … … 102 144 function wpgancio_options_page_html() { 103 145 // check user capabilities 104 if ( ! current_user_can( 'manage_options' )) { return; }146 if (! current_user_can('manage_options')) { return; } 105 147 106 148 // show error/update messages 107 $code = sanitize_key( $_GET['code']);149 $code = sanitize_key(isset($_GET['code']) ? $_GET['code'] : ''); 108 150 if ( $code ) { 109 151 update_option('wpgancio_code', $code); 110 $instance_url = get_option( 'wpgancio_instance_url');152 $instance_url = get_option('wpgancio_instance_url'); 111 153 112 154 $response = wp_remote_post($instance_url . "/oauth/token", array( … … 118 160 'code' => $code 119 161 ))); 120 if ( is_wp_error( $response )) {162 if (is_wp_error($response)) { 121 163 add_settings_error('wpgancio_messages', 'wpgancio_messages', $response->get_error_message()); 122 settings_errors( 'wpgancio_messages');123 } else if ( $response['response']['code'] == 500) {164 settings_errors('wpgancio_messages'); 165 } elseif ($response['response']['code'] == 500) { 124 166 add_settings_error('wpgancio_messages', 'wpgancio_messages', wp_remote_retrieve_body($response)); 125 settings_errors( 'wpgancio_messages');167 settings_errors('wpgancio_messages'); 126 168 } else { 127 $data = json_decode( wp_remote_retrieve_body($response), true);169 $data = json_decode(wp_remote_retrieve_body($response), true); 128 170 update_option('wpgancio_token', sanitize_key($data['access_token'])); 129 171 update_option('wpgancio_refresh', sanitize_key($data['refresh_token'])); 130 172 add_settings_error('wpgancio_messages', 'wpgancio_messages', 'Association completed!', 'success'); 131 settings_errors( 'wpgancio_messages');173 settings_errors('wpgancio_messages'); 132 174 } 133 175 } … … 137 179 138 180 <div class="wrap"> 139 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> 181 182 <h1><?php echo esc_html(get_admin_page_title()); ?></h1> 140 183 <form action="options.php" method="post"> 141 184 <?php 142 185 143 186 // output security fields for the registered setting "wpgancio" 144 settings_fields( 'wpgancio');187 settings_fields('wpgancio'); 145 188 146 189 // output setting sections and their fields 147 190 // (sections are registered for "wpgancio", each field is registered to a specific section) 148 do_settings_sections( 'wpgancio');191 do_settings_sections('wpgancio'); 149 192 150 193 // output save settings button 151 submit_button( 'Save Settings');194 submit_button('Save Settings'); 152 195 ?> 153 196 </form> -
wpgancio/trunk/wc.php
r2658720 r2824192 27 27 /** ADD SHORTCODES */ 28 28 function gancio_event_handler_function( $atts, $content, $tag) { 29 var_dump('asdfaoisdfo iajsdofij');30 29 $a = shortcode_atts( array( 31 30 'baseurl' => 'https://demo.gancio.org', … … 33 32 ), $atts); 34 33 return '<gancio-event baseurl="' . $a['baseurl'] . '" id=' . $a['id'] . '></gancio-event>'; 35 } ;34 } 36 35 37 36 function gancio_events_handler_function( $atts, $content, $tag) { … … 41 40 'tags' => '', 42 41 'theme' => 'dark', 43 'max' => NULL42 'max' => null 44 43 ), $atts); 45 44 return '<gancio-events baseurl="' . $a['baseurl'] . '" theme="' . $a['theme'] . '" places="' . $a['places'] . '" tags="' . $a['tags'] . '"></gancio-events>'; 46 } ;45 } 47 46 48 47 add_action( 'init', function () {
Note: See TracChangeset
for help on using the changeset viewer.