2019-03-22 20:48:56 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Plugin Name: Kal3000 Google Calender Importer
|
|
|
|
* Plugin URI: https://github.com/hmilz/kal3000-gcal-import
|
|
|
|
* Description: Imports and Merges an Arbitrary Number of Public Google Calendars into Kal3000
|
|
|
|
* Version: 0.1
|
|
|
|
* Author: Harald Milz <hm@seneca.muc.de>
|
|
|
|
* License: GPLv3
|
|
|
|
* License URI: https://www.gnu.org/licenses/gpl-3.0
|
|
|
|
* Domain Path: /languages
|
|
|
|
*
|
|
|
|
* {Plugin Name} is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* {Plugin Name} is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with {Plugin Name}. If not, see {License URI}.
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|
|
|
|
|
|
|
define ('GCAL_TABLE', 'gcal_import');
|
2019-03-25 14:48:06 +01:00
|
|
|
define ('GCAL_GEO_TABLE', 'gcal_import_geocache');
|
2019-03-22 20:48:56 +01:00
|
|
|
|
|
|
|
|
2019-03-23 08:30:33 +01:00
|
|
|
// The real work goes here.
|
2019-03-26 21:37:49 +01:00
|
|
|
require_once dirname( __FILE__ ) . "/gcal-import-worker.php";
|
2019-03-27 13:49:46 +01:00
|
|
|
require_once dirname( __FILE__ ) . "/gcal-import-admin.php";
|
2019-03-23 08:30:33 +01:00
|
|
|
|
2019-03-23 17:28:30 +01:00
|
|
|
|
2019-03-27 18:13:25 +01:00
|
|
|
// create custom scheduler from custom option
|
2019-03-27 13:49:46 +01:00
|
|
|
add_filter( 'cron_schedules', 'gcal_cron_interval' );
|
2019-03-23 17:28:30 +01:00
|
|
|
|
2019-03-27 13:49:46 +01:00
|
|
|
function gcal_cron_interval( $schedules ) {
|
2019-03-27 18:13:25 +01:00
|
|
|
$interval = 60 * get_option( '_gcal_interval' ); // wir speichern Minuten
|
2019-03-27 13:49:46 +01:00
|
|
|
$schedules['gcal_interval'] = array(
|
2019-03-27 18:13:25 +01:00
|
|
|
'interval' => $interval,
|
|
|
|
'display' => esc_html__( 'GCal fetch interval' ),
|
2019-03-23 17:28:30 +01:00
|
|
|
);
|
|
|
|
return $schedules;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-27 13:49:46 +01:00
|
|
|
|
2019-03-22 20:48:56 +01:00
|
|
|
/**
|
|
|
|
* Initializes the plugin and creates a table
|
|
|
|
*
|
|
|
|
* @since 0.1.0
|
|
|
|
*
|
|
|
|
* - gcal_category - name of the calendar, for later per-unit display
|
|
|
|
* - gcal_link - the public or private .ics link
|
|
|
|
* - gcal_veranstalter - ?
|
|
|
|
* - gcal_active - flag if a calendar is active or not. Default active.
|
2019-03-24 18:33:00 +01:00
|
|
|
*
|
|
|
|
* Since there is no install hook in WP, we will use the activation hook for both.
|
2019-03-22 20:48:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
function gcal_import_activate()
|
|
|
|
{
|
|
|
|
error_log ("gcal_import_activate started");
|
|
|
|
global $wpdb;
|
2019-03-24 18:33:00 +01:00
|
|
|
// CREATE table if it does not exist already.
|
2019-03-22 20:48:56 +01:00
|
|
|
$table = $wpdb->prefix.GCAL_TABLE;
|
2019-03-23 17:28:30 +01:00
|
|
|
$query = "CREATE TABLE IF NOT EXISTS $table (
|
2019-03-22 20:48:56 +01:00
|
|
|
id INT(9) NOT NULL AUTO_INCREMENT,
|
|
|
|
gcal_category VARCHAR(32) NOT NULL,
|
|
|
|
gcal_link VARCHAR(256) NOT NULL,
|
|
|
|
gcal_active INT(9) DEFAULT 1,
|
2019-03-24 18:33:00 +01:00
|
|
|
UNIQUE KEY id (id)
|
2019-03-22 20:48:56 +01:00
|
|
|
);";
|
2019-03-23 17:28:30 +01:00
|
|
|
$wpdb->query($query);
|
|
|
|
|
|
|
|
// Populate table
|
|
|
|
// this will later be set by the admin page
|
|
|
|
// empty it first to prevent doublettes
|
|
|
|
$wpdb->query("DELETE FROM $table WHERE 1=1");
|
|
|
|
$wpdb->query("INSERT INTO $table(gcal_category, gcal_link, gcal_active)
|
2019-03-26 21:37:49 +01:00
|
|
|
VALUES('Kreisverband', 'https://calendar.google.com/calendar/ical/gruene.freising%40gmail.com/public/basic.ics', '1')");
|
2019-03-27 18:13:25 +01:00
|
|
|
$wpdb->query("INSERT INTO $table(gcal_category, gcal_link, gcal_active)
|
|
|
|
VALUES('Neufahrn', 'https://calendar.google.com/calendar/ical/gruene.neufahrn%40gmail.com/public/basic.ics', '1')");
|
|
|
|
$wpdb->query("INSERT INTO $table(gcal_category, gcal_link, gcal_active)
|
|
|
|
VALUES('Holledau', 'https://calendar.google.com/calendar/ical/gruene.holledau%40gmail.com/public/basic.ics', '0')");
|
2019-03-26 21:37:49 +01:00
|
|
|
/*
|
2019-03-23 17:28:30 +01:00
|
|
|
$wpdb->query("INSERT INTO $table(gcal_category, gcal_link, gcal_active)
|
|
|
|
VALUES('ov-freising', '/tmp/neufahrn.ics', '1')");
|
2019-03-26 21:37:49 +01:00
|
|
|
*/
|
2019-03-22 20:48:56 +01:00
|
|
|
|
2019-03-27 13:49:46 +01:00
|
|
|
// Create some plugin options in wp_options if they don't exist already
|
2019-03-27 21:29:59 +01:00
|
|
|
// add_option( '_gcal_geocoding', 'off', '', 'no' ); // off, official, inofficial, or OSM (experimental)
|
|
|
|
// add_option( '_gcal_apikey', '', '', 'no' ); // default empty
|
|
|
|
// add_option( '_gcal_interval', '5', '', 'no' ); // default 60 minutes
|
2019-03-27 13:49:46 +01:00
|
|
|
|
2019-03-25 14:48:06 +01:00
|
|
|
// CREATE geocaching table if it does not exist already.
|
2019-03-25 21:40:28 +01:00
|
|
|
// the location field will be used only during development and debugging, and will be omitted in production.
|
2019-03-25 14:48:06 +01:00
|
|
|
$table = $wpdb->prefix.GCAL_GEO_TABLE;
|
|
|
|
$query = "CREATE TABLE IF NOT EXISTS $table (
|
|
|
|
id INT(9) NOT NULL AUTO_INCREMENT,
|
2019-03-25 21:40:28 +01:00
|
|
|
gcal_geo_location VARCHAR(128) NOT NULL,
|
2019-03-25 14:48:06 +01:00
|
|
|
gcal_geo_hash VARCHAR(40) NOT NULL,
|
|
|
|
gcal_geo_lat VARCHAR(20) NOT NULL,
|
|
|
|
gcal_geo_lon VARCHAR(20) NOT NULL,
|
2019-03-26 21:37:49 +01:00
|
|
|
gcal_geo_timestamp INT(16) NOT NULL,
|
2019-03-25 14:48:06 +01:00
|
|
|
UNIQUE KEY id (id)
|
|
|
|
);";
|
|
|
|
$wpdb->query($query);
|
2019-03-22 20:48:56 +01:00
|
|
|
|
2019-03-25 21:40:28 +01:00
|
|
|
$table = $wpdb->prefix.GCAL_TABLE;
|
2019-03-23 17:28:30 +01:00
|
|
|
// do it once now! Won't work if the table hasn't been populated yet.
|
2019-03-24 18:33:00 +01:00
|
|
|
$result = $wpdb->query("SELECT gcal_category FROM $table");
|
|
|
|
if ($result != 0) {
|
2019-03-27 18:13:25 +01:00
|
|
|
// gcal_import_worker();
|
2019-03-24 18:33:00 +01:00
|
|
|
}
|
2019-03-23 08:30:33 +01:00
|
|
|
// and start the scheduler;
|
2019-03-24 18:33:00 +01:00
|
|
|
// in production, we will do this hourly.
|
2019-03-22 20:48:56 +01:00
|
|
|
if ( ! wp_next_scheduled( 'gcal_import_worker_hook' ) ) {
|
2019-03-27 13:49:46 +01:00
|
|
|
wp_schedule_event( time(), 'gcal_interval', 'gcal_import_worker_hook' );
|
2019-03-22 20:48:56 +01:00
|
|
|
}
|
|
|
|
error_log ("gcal_import_activate finished");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
register_activation_hook( __FILE__, 'gcal_import_activate' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-03-24 18:33:00 +01:00
|
|
|
* Deactivate unregisters the scheduling function.
|
2019-03-22 20:48:56 +01:00
|
|
|
*
|
|
|
|
* @since 0.1.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
function gcal_import_deactivate()
|
|
|
|
{
|
|
|
|
error_log ("gcal_import_deactivate started");
|
2019-03-24 18:33:00 +01:00
|
|
|
wp_clear_scheduled_hook('gcal_import_worker_hook');
|
2019-03-22 20:48:56 +01:00
|
|
|
error_log ("gcal_import_deactivate finished");
|
|
|
|
}
|
|
|
|
|
|
|
|
register_deactivation_hook( __FILE__, 'gcal_import_deactivate' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uninstall drops our DB table
|
|
|
|
*
|
|
|
|
* @since 0.1.0
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
function gcal_import_uninstall()
|
|
|
|
{
|
|
|
|
error_log ("gcal_import_uninstall started");
|
|
|
|
// can we uninstall without deactivating first?
|
|
|
|
// gcal_import_deactivate;
|
|
|
|
global $wpdb;
|
2019-03-27 13:49:46 +01:00
|
|
|
// the category / link table and the geo cache
|
|
|
|
$tables = array( GCAL_TABLE, GCAL_GEO_TABLE );
|
|
|
|
foreach ( $tables AS $table ) {
|
|
|
|
$table = $wpdb->prefix . $table;
|
|
|
|
$wpdb->query( "DROP TABLE IF EXISTS $table" );
|
|
|
|
}
|
|
|
|
// and the options.
|
2019-03-27 21:29:59 +01:00
|
|
|
delete_option ( 'gcal_options' );
|
|
|
|
// $options = array( '_gcal_geocoding', '_gcal_apikey', '_gcal_interval' );
|
|
|
|
// foreach ( $options AS $option ) {
|
|
|
|
// delete_option( $option );
|
|
|
|
// }
|
|
|
|
// error_log ("gcal_import_uninstall finished");
|
2019-03-22 20:48:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
register_uninstall_hook( __FILE__, 'gcal_import_uninstall' );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|