Plugin Directory

Changeset 2824192


Ignore:
Timestamp:
11/25/2022 12:04:30 PM (3 years ago)
Author:
lesion
Message:

support MU

Location:
wpgancio/trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • wpgancio/trunk/gancio.php

    r2675565 r2824192  
    44Plugin URI:  https://gancio.org
    55Description: Connects an user of a gancio instance to a Wordpress user so that published events are automatically pushed with Gancio API.
    6 Version:     1.3
     6Version:     1.5
    77Author:      Gancio
    88License:  AGPL 3.0
     
    2020*/
    2121
    22 defined( 'ABSPATH' ) or die( 'Nope, not accessing this' );
    23 require_once('settings.php');
    24 require_once('wc.php');
    25 require_once('oauth.php');
     22defined('ABSPATH') or die('Nope, not accessing this');
     23define('WPGANCIO_DIR', plugin_dir_path(__FILE__));
     24require_once(WPGANCIO_DIR . 'settings.php');
     25require_once(WPGANCIO_DIR . 'network_settings.php');
     26require_once(WPGANCIO_DIR . 'wc.php');
     27require_once(WPGANCIO_DIR . 'oauth.php');
    2628
    2729
     
    3941 * - Send each new / updated events to the selected instance via Gancio API
    4042 */
    41 
  • wpgancio/trunk/oauth.php

    r2560249 r2824192  
    99function wpgancio_delete_post ($post_id) {
    1010  $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'));
    1212
    1313  if ($post->post_type == 'event') {
     
    1515    if ($gancio_id) {
    1616      $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(
    1818        'method' => 'DELETE',
    1919        'headers' => array (
    20           'Authorization' => 'Bearer ' . get_option('wpgancio_token')
     20          'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token'))
    2121        )));
    2222    }
     
    2727  $event = get_post( $post_id );
    2828
    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 
    3629  // do not save if it's a draft
    3730  if ($event->post_status != 'publish') {
     
    3932  }
    4033
     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
    4143  $gancio_id = get_post_meta($post_id, 'wpgancio_gancio_id', TRUE);
    4244
    4345  // 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);
    4548
    4649  // get place details
     
    4851  $place_name = eo_get_venue_name($venue_id);
    4952  $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
    5155
    5256  $body = array (
     
    5458    'tags' => $tags,
    5559    'description' => $event->post_content,
    56     'start_datetime' => intval($date),
     60    'start_datetime' => intval($start_datetime),
     61    'end_datetime' => intval($end_datetime),
    5762    'place_name' => $place_name,
    58     'place_address' => "${place_address['address']}${place_address['city']}"
     63    'place_address' => "${place_address['address']}, ${place_address['city']}"
    5964  );
    6065
     
    7277      'method' => 'PUT',
    7378      'headers' => array (
    74         'Authorization' => 'Bearer ' . get_option('wpgancio_token'),
     79        'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')),
    7580        'Content-Type' => 'application/json'
    7681      ), 'body' => wp_json_encode($body) ));
     
    7883    $response = wp_remote_post($instance_url . '/api/event', array(
    7984      'headers' => array (
    80         'Authorization' => 'Bearer ' . get_option('wpgancio_token'),
     85        'Authorization' => 'Bearer ' . get_option('wpgancio_token', get_site_option('wpgancio_token')),
    8186        'Content-Type' => 'application/json'
    8287      ), 'body' => wp_json_encode($body) ));
  • wpgancio/trunk/readme.txt

    r2747168 r2824192  
    44Tags: events, gancio, fediverse, AP, activity pub
    55Requires at least: 4.7
    6 Tested up to: 5.9
    7 Stable tag: 1.4
     6Tested up to: 6.0
     7Stable tag: 1.5
    88Requires PHP: 7.0
    99License: AGPLv3 or later
     
    1919
    2020== Changelog ==
    21 = 1.4 = 
     21= 1.4 =
    2222use `WP_GANCIO_DEFAULT_INSTANCEURL` as default instance url
    2323
  • wpgancio/trunk/settings.php

    r2560249 r2824192  
    44
    55// Fires as an admin screen or script is being initialized. Register out settings
    6 add_action( 'admin_init', 'wpgancio_settings_init' );
     6if (is_network_admin()) {
     7  add_action('network_admin_menu', 'wpgancio_settings_init');
     8} else {
     9  add_action('admin_menu', 'wpgancio_settings_init');
     10}
     11
     12add_action('add_meta_boxes_event', 'wpgancio_remove_meta_boxes', 10, 2);
     13function wpgancio_remove_meta_boxes () {
     14  remove_meta_box('postcustom', 'event', 'normal');
     15}
     16
    717function wpgancio_settings_init() {
    8 
     18 
    919  // register a new settings page
    10   add_settings_section('wpgancio_settings', __('Settings'), FALSE, 'wpgancio');
     20  add_settings_section('wpgancio_settings', __('Settings'), false, 'wpgancio');
    1121
    1222  // 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'),
    1425    'wpgancio_instance_url_cb', 'wpgancio',
    15     'wpgancio_settings');
     26    'wpgancio_settings'
     27  );
    1628
    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');
    1833}
     34
    1935
    2036add_action( 'update_option_wpgancio_instance_url', 'wpgancio_update_options', 15, 2);
    2137function 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  }
    2343  $query = join('&', array(
    2444    'response_type=code',
     
    2848  ));
    2949
    30   wp_redirect("${instance_url}/authorize?${query}");
     50  wp_redirect("${instance_url}/oauth/authorize?${query}");
     51  // return $instance_url;
    3152  exit;
    3253}
    3354
     55
    3456// Fires before the administration menu loads in the admin, add our options page
    35 add_action( 'admin_menu', 'wpgancio_options_page' );
     57add_action('admin_menu', 'wpgancio_options_page');
    3658
    3759function 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  }
    3971
    4072  // 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(
    4274    'method' => 'POST',
    4375    'body' => array(
     
    4981  ));
    5082
    51   if ( is_wp_error( $response ) ) {
     83  if (is_wp_error($response)) {
    5284    add_settings_error('wpgancio_messages', 'wpgancio_messages',
    5385      $response->get_error_message());
    5486  } else {
    5587    $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    }
    5895    return $instance_url;
    5996  }
     
    80117function wpgancio_instance_url_cb( $args ) {
    81118  // 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?>
    85127
    86128  <input id="wpgancio_instance_url"
     
    102144function wpgancio_options_page_html() {
    103145  // check user capabilities
    104   if ( ! current_user_can( 'manage_options' ) ) { return; }
     146 if (! current_user_can('manage_options')) { return; }
    105147
    106148  // show error/update messages
    107   $code = sanitize_key($_GET['code']);
     149  $code = sanitize_key(isset($_GET['code']) ? $_GET['code'] : '');
    108150  if ( $code ) {
    109151    update_option('wpgancio_code', $code);
    110     $instance_url = get_option( 'wpgancio_instance_url' );
     152    $instance_url = get_option('wpgancio_instance_url');
    111153
    112154    $response = wp_remote_post($instance_url . "/oauth/token", array(
     
    118160        'code' => $code
    119161      )));
    120     if ( is_wp_error( $response ) ) {
     162    if (is_wp_error($response)) {
    121163      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) {
    124166      add_settings_error('wpgancio_messages', 'wpgancio_messages', wp_remote_retrieve_body($response));
    125       settings_errors( 'wpgancio_messages' );
     167      settings_errors('wpgancio_messages');
    126168    } else {
    127       $data = json_decode( wp_remote_retrieve_body($response), true);
     169      $data = json_decode(wp_remote_retrieve_body($response), true);
    128170      update_option('wpgancio_token', sanitize_key($data['access_token']));
    129171      update_option('wpgancio_refresh', sanitize_key($data['refresh_token']));
    130172      add_settings_error('wpgancio_messages', 'wpgancio_messages', 'Association completed!', 'success');
    131       settings_errors( 'wpgancio_messages' );
     173      settings_errors('wpgancio_messages');
    132174    }
    133175  }
     
    137179
    138180 <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>
    140183 <form action="options.php" method="post">
    141184 <?php
    142185
    143186  // output security fields for the registered setting "wpgancio"
    144   settings_fields( 'wpgancio' );
     187  settings_fields('wpgancio');
    145188
    146189  // output setting sections and their fields
    147190  // (sections are registered for "wpgancio", each field is registered to a specific section)
    148   do_settings_sections( 'wpgancio' );
     191  do_settings_sections('wpgancio');
    149192
    150193  // output save settings button
    151   submit_button( 'Save Settings' );
     194  submit_button('Save Settings');
    152195 ?>
    153196 </form>
  • wpgancio/trunk/wc.php

    r2658720 r2824192  
    2727/** ADD SHORTCODES */
    2828function gancio_event_handler_function( $atts, $content, $tag) {
    29   var_dump('asdfaoisdfo iajsdofij');
    3029  $a = shortcode_atts( array(
    3130    'baseurl' => 'https://demo.gancio.org',
     
    3332  ), $atts);
    3433  return '<gancio-event baseurl="' . $a['baseurl'] . '" id=' . $a['id'] . '></gancio-event>';
    35 };
     34}
    3635
    3736function gancio_events_handler_function( $atts, $content, $tag) {
     
    4140    'tags' => '',
    4241    'theme' => 'dark',
    43     'max' => NULL
     42    'max' => null
    4443  ), $atts);
    4544  return '<gancio-events baseurl="' . $a['baseurl'] . '" theme="' . $a['theme'] . '" places="' . $a['places'] . '" tags="' . $a['tags'] . '"></gancio-events>';
    46 };
     45}
    4746
    4847add_action( 'init', function () {
Note: See TracChangeset for help on using the changeset viewer.