Files
on-motorrad-buchungs-plugin/includes/class-on-service-manager.php

154 lines
4.7 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
class ON_Service_Manager
{
public function __construct()
{
add_action('init', array($this, 'register_service_post_type'));
register_activation_hook(ON_BOOKING_PATH . 'on-motorrad-buchung.php', array($this, 'create_default_services'));
}
public function register_service_post_type()
{
$labels = array(
'name' => 'Services',
'singular_name' => 'Service',
'menu_name' => 'Services',
'add_new' => 'Neuer Service',
'add_new_item' => 'Neuen Service hinzufügen',
'edit_item' => 'Service bearbeiten',
'all_items' => 'Alle Services',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => false, // We'll create custom admin page
'show_in_menu' => false,
'capability_type' => 'post',
'supports' => array('title'),
'has_archive' => false,
);
register_post_type('on_service', $args);
}
public function create_default_services()
{
// Check if services already exist
$existing = get_posts(array(
'post_type' => 'on_service',
'posts_per_page' => 1,
'post_status' => 'publish'
));
if (!empty($existing)) {
return; // Services already created
}
$default_services = array(
array('name' => 'Reifenwechsel', 'duration' => 30),
array('name' => 'Reifenwechsel vorne und hinten', 'duration' => 60),
array('name' => 'Jahresinspektion', 'duration' => 90),
array('name' => 'Ölwechsel', 'duration' => 30),
array('name' => 'Kettensatzwechsel', 'duration' => 90),
array('name' => 'Gabelservice', 'duration' => 90),
array('name' => 'Bremsbeläge wechseln', 'duration' => 30),
array('name' => 'Bremsscheiben und Beläge', 'duration' => 60),
);
foreach ($default_services as $index => $service) {
$post_id = wp_insert_post(array(
'post_type' => 'on_service',
'post_title' => $service['name'],
'post_status' => 'publish',
));
if ($post_id) {
update_post_meta($post_id, 'service_duration', $service['duration']);
update_post_meta($post_id, 'service_active', 1);
update_post_meta($post_id, 'service_order', $index);
}
}
}
/**
* Get all active services
*/
public static function get_active_services()
{
$services = get_posts(array(
'post_type' => 'on_service',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'meta_value_num',
'meta_key' => 'service_order',
'order' => 'ASC'
));
$result = array();
foreach ($services as $service) {
$active = get_post_meta($service->ID, 'service_active', true);
if ($active == 1) {
$result[] = array(
'id' => $service->ID,
'name' => $service->post_title,
'duration' => (int) get_post_meta($service->ID, 'service_duration', true),
);
}
}
return $result;
}
/**
* Get all services (including inactive)
*/
public static function get_all_services()
{
$services = get_posts(array(
'post_type' => 'on_service',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'meta_value_num',
'meta_key' => 'service_order',
'order' => 'ASC'
));
$result = array();
foreach ($services as $service) {
$result[] = array(
'id' => $service->ID,
'name' => $service->post_title,
'duration' => (int) get_post_meta($service->ID, 'service_duration', true),
'active' => (int) get_post_meta($service->ID, 'service_active', true),
);
}
return $result;
}
/**
* Get service by ID
*/
public static function get_service($service_id)
{
$service = get_post($service_id);
if (!$service || $service->post_type !== 'on_service') {
return null;
}
return array(
'id' => $service->ID,
'name' => $service->post_title,
'duration' => (int) get_post_meta($service->ID, 'service_duration', true),
'active' => (int) get_post_meta($service->ID, 'service_active', true),
);
}
}
new ON_Service_Manager();