122 lines
4.4 KiB
PHP
122 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Swiss Football Matchdata
|
|
* Description: WordPress plugin to consume Swiss Football Association API for team standings, schedules, and match details with shortcode support
|
|
* Version: 1.0.0
|
|
* Author: David Reindl (assisted by Claude AI)
|
|
* Text Domain: swi_foot_matchdata
|
|
* Domain Path: /languages
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
// Define plugin constants
|
|
define('SWI_FOOT_PLUGIN_URL', plugin_dir_url(__FILE__));
|
|
define('SWI_FOOT_PLUGIN_PATH', plugin_dir_path(__FILE__));
|
|
define('SWI_FOOT_PLUGIN_VERSION', '1.0.0');
|
|
|
|
// Include required files
|
|
require_once SWI_FOOT_PLUGIN_PATH . 'includes/class-swi-foot-api.php';
|
|
require_once SWI_FOOT_PLUGIN_PATH . 'includes/class-swi-foot-admin.php';
|
|
require_once SWI_FOOT_PLUGIN_PATH . 'includes/class-swi-foot-blocks.php';
|
|
require_once SWI_FOOT_PLUGIN_PATH . 'includes/class-swi-foot-shortcodes.php';
|
|
require_once SWI_FOOT_PLUGIN_PATH . 'includes/class-swi-foot-rest.php';
|
|
|
|
class Swiss_Football_Matchdata {
|
|
|
|
public function __construct() {
|
|
add_action('init', array($this, 'init'));
|
|
register_activation_hook(__FILE__, array($this, 'activate'));
|
|
register_deactivation_hook(__FILE__, array($this, 'deactivate'));
|
|
add_action('plugins_loaded', array($this, 'load_textdomain'));
|
|
}
|
|
|
|
public function init() {
|
|
// Detect version updates and flush REST API routes cache if needed
|
|
$stored_version = get_option('swi_foot_plugin_version');
|
|
if ($stored_version !== SWI_FOOT_PLUGIN_VERSION) {
|
|
// Plugin was updated, clear REST endpoints cache
|
|
delete_transient('rest_endpoints_data');
|
|
wp_cache_delete('rest_endpoints', 'swi-foot/v1');
|
|
update_option('swi_foot_plugin_version', SWI_FOOT_PLUGIN_VERSION);
|
|
}
|
|
|
|
// Register post meta to store per-post context (season/team/match) for posts and pages
|
|
$meta_args = array(
|
|
'type' => 'object',
|
|
'single' => true,
|
|
'show_in_rest' => array(
|
|
'schema' => array(
|
|
'type' => 'object',
|
|
'properties' => array(
|
|
'season' => array('type' => array('string','integer')),
|
|
'team_id' => array('type' => array('string','integer')),
|
|
'match_id' => array('type' => array('string','integer')),
|
|
),
|
|
'additionalProperties' => true,
|
|
),
|
|
),
|
|
'auth_callback' => function( $allowed, $meta_key, $post_id, $user ) {
|
|
return current_user_can('edit_post', $post_id);
|
|
}
|
|
);
|
|
|
|
register_post_meta('post', 'swi_foot_context', $meta_args);
|
|
register_post_meta('page', 'swi_foot_context', $meta_args);
|
|
|
|
new Swi_Foot_API();
|
|
new Swi_Foot_Admin();
|
|
new Swi_Foot_Blocks();
|
|
new Swi_Foot_Shortcodes();
|
|
}
|
|
|
|
public function load_textdomain() {
|
|
load_plugin_textdomain(
|
|
'swi_foot_matchdata',
|
|
false,
|
|
dirname(plugin_basename(__FILE__)) . '/languages'
|
|
);
|
|
}
|
|
|
|
public function activate() {
|
|
// Create options with defaults
|
|
add_option('swi_foot_api_base_url', 'https://club-api-services.football.ch/api/v1');
|
|
add_option('swi_foot_api_username', '');
|
|
add_option('swi_foot_api_password', '');
|
|
add_option('swi_foot_verein_id', '');
|
|
add_option('swi_foot_season_id', date('Y')); // Current year as default
|
|
add_option('swi_foot_match_cache_duration', 30);
|
|
add_option('swi_foot_plugin_version', SWI_FOOT_PLUGIN_VERSION);
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
|
|
// Clear REST API route caches
|
|
delete_transient('rest_endpoints_data');
|
|
wp_cache_delete('rest_endpoints', 'swi-foot/v1');
|
|
}
|
|
|
|
public function deactivate() {
|
|
// Clean up transient cache data (do not remove finished/permanent records)
|
|
delete_transient('swi_foot_access_token');
|
|
delete_transient('swi_foot_teams');
|
|
$keys = get_transient('swi_foot_match_keys');
|
|
if (is_array($keys)) {
|
|
foreach ($keys as $mid) {
|
|
delete_transient('swi_foot_match_' . $mid);
|
|
}
|
|
}
|
|
delete_transient('swi_foot_match_keys');
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
}
|
|
|
|
// Initialize the plugin
|
|
new Swiss_Football_Matchdata();
|
|
?>
|