This repository has been archived on 2024-01-12. You can view files and clone it, but cannot push or open issues or pull requests.
WolKal3000/gcal-import.php
2019-03-27 21:29:59 +01:00

185 lines
6 KiB
PHP

<?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');
define ('GCAL_GEO_TABLE', 'gcal_import_geocache');
// The real work goes here.
require_once dirname( __FILE__ ) . "/gcal-import-worker.php";
require_once dirname( __FILE__ ) . "/gcal-import-admin.php";
// create custom scheduler from custom option
add_filter( 'cron_schedules', 'gcal_cron_interval' );
function gcal_cron_interval( $schedules ) {
$interval = 60 * get_option( '_gcal_interval' ); // wir speichern Minuten
$schedules['gcal_interval'] = array(
'interval' => $interval,
'display' => esc_html__( 'GCal fetch interval' ),
);
return $schedules;
}
/**
* 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.
*
* Since there is no install hook in WP, we will use the activation hook for both.
*/
function gcal_import_activate()
{
error_log ("gcal_import_activate started");
global $wpdb;
// CREATE table if it does not exist already.
$table = $wpdb->prefix.GCAL_TABLE;
$query = "CREATE TABLE IF NOT EXISTS $table (
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,
UNIQUE KEY id (id)
);";
$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)
VALUES('Kreisverband', 'https://calendar.google.com/calendar/ical/gruene.freising%40gmail.com/public/basic.ics', '1')");
$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')");
/*
$wpdb->query("INSERT INTO $table(gcal_category, gcal_link, gcal_active)
VALUES('ov-freising', '/tmp/neufahrn.ics', '1')");
*/
// Create some plugin options in wp_options if they don't exist already
// 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
// CREATE geocaching table if it does not exist already.
// the location field will be used only during development and debugging, and will be omitted in production.
$table = $wpdb->prefix.GCAL_GEO_TABLE;
$query = "CREATE TABLE IF NOT EXISTS $table (
id INT(9) NOT NULL AUTO_INCREMENT,
gcal_geo_location VARCHAR(128) NOT NULL,
gcal_geo_hash VARCHAR(40) NOT NULL,
gcal_geo_lat VARCHAR(20) NOT NULL,
gcal_geo_lon VARCHAR(20) NOT NULL,
gcal_geo_timestamp INT(16) NOT NULL,
UNIQUE KEY id (id)
);";
$wpdb->query($query);
$table = $wpdb->prefix.GCAL_TABLE;
// do it once now! Won't work if the table hasn't been populated yet.
$result = $wpdb->query("SELECT gcal_category FROM $table");
if ($result != 0) {
// gcal_import_worker();
}
// and start the scheduler;
// in production, we will do this hourly.
if ( ! wp_next_scheduled( 'gcal_import_worker_hook' ) ) {
wp_schedule_event( time(), 'gcal_interval', 'gcal_import_worker_hook' );
}
error_log ("gcal_import_activate finished");
}
register_activation_hook( __FILE__, 'gcal_import_activate' );
/**
* Deactivate unregisters the scheduling function.
*
* @since 0.1.0
*
*/
function gcal_import_deactivate()
{
error_log ("gcal_import_deactivate started");
wp_clear_scheduled_hook('gcal_import_worker_hook');
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;
// 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.
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");
}
register_uninstall_hook( __FILE__, 'gcal_import_uninstall' );