Files
crille-schema-viewer/crille-schema.php

119 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Plugin Name: Crille Schema Viewer
* Description: Visar kommande schemahändelser från Högskolan Västs KronoX-system via server-side ICS-hämtning. Pluginet har en egen inställningssida där du kan välja vilken signatur (resurskod) schemat ska hämtas för samt ange hur många kommande schemaposter som ska visas.
* Version: 1.1
* Author: Christian Ohlsson
*/
// === SETTINGS REGISTRATION ===
add_action('admin_init', function(){
// Signatur / resurser (t.ex. s.COH)
register_setting('hv_schema_settings_group', 'hv_schema_signatur');
// Antal poster att visa
register_setting('hv_schema_settings_group', 'hv_schema_antal');
});
// === ADMIN MENU PAGE ===
add_action('admin_menu', function(){
add_options_page(
'HV Schema Inställningar',
'HV Schema',
'manage_options',
'hv-schema-settings',
'hv_schema_settings_page'
);
});
function crille_schema_viewer_enqueue_fontawesome() {
wp_enqueue_style(
'fontawesome',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css',
array(),
'6.5.1'
);
}
add_action('wp_enqueue_scripts', 'crille_schema_viewer_enqueue_fontawesome');
function hv_schema_register_block(){
wp_register_script('hv-schema-editor',plugins_url('src/block.js',__FILE__),['wp-blocks','wp-element'],true);
wp_register_script('hv-schema-frontend',plugins_url('src/frontend.js',__FILE__),['wp-api-fetch'],true);
wp_register_style('hv-schema-style',plugins_url('css/style.css',__FILE__));
register_block_type(__DIR__,['render_callback'=>'hv_schema_render']);
}
add_action('init','hv_schema_register_block');
add_action('wp_enqueue_scripts',function(){ wp_enqueue_script('hv-schema-frontend'); wp_enqueue_style('hv-schema-style');});
function hv_schema_render(){ return '<div id="hv-schema"></div>'; }
// server-side ICS fetch
add_action('rest_api_init', function(){
register_rest_route('hv/v1','/schema',[ 'methods'=>'GET', 'callback'=>'hv_schema_fetch' ]);
});
function hv_schema_fetch(){
// Hämta settings
$signatur = get_option('hv_schema_signatur', 's.COH');
$antal = get_option('hv_schema_antal', 7);
// Bygg ICS-URL dynamiskt
$url = 'https://schema.hv.se/setup/jsp/SchemaICAL.ics'
. '?startDatum=idag'
. '&intervallTyp=m'
. '&intervallAntal=6'
. '&sprak=SV'
. '&sokMedAND=true'
. '&forklaringar=true'
. '&resurser=' . urlencode($signatur);
$res = wp_remote_get($url);
if(is_wp_error($res)) return [];
// Skicka både ICS OCH antal
return [
'ics' => wp_remote_retrieve_body($res),
'antal' => intval($antal)
];
}
function hv_schema_settings_page(){ ?>
<div class="wrap">
<h1>HV Schema Inställningar</h1>
<form method="post" action="options.php">
<?php settings_fields('hv_schema_settings_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Signatur / Resurskod</th>
<td>
<input type="text"
name="hv_schema_signatur"
value="<?php echo esc_attr(get_option('hv_schema_signatur', 's.COH')); ?>"
style="width:200px;">
<p class="description">Exempel: s.COH (din signatur)</p>
</td>
</tr>
<tr valign="top">
<th scope="row">Antal poster</th>
<td>
<input type="number"
name="hv_schema_antal"
value="<?php echo esc_attr(get_option('hv_schema_antal', 7)); ?>"
min="1" max="30" style="width:80px;">
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
?>