class JFI_Klanten { public static function register_post_types(){ register_post_type('jfi_client', [ 'label' => 'Opdrachtgevers', 'public' => false, 'show_ui' => true, 'supports' => ['title','custom-fields'], 'show_in_rest' => false ]); register_post_type('jfi_location', [ 'label' => 'Locaties', 'public' => false, 'show_ui' => true, 'supports' => ['title','custom-fields'], 'show_in_rest' => false ]); register_post_type('jfi_plan', [ 'label' => 'Plannen', 'public' => false, 'show_ui' => true, 'supports' => ['title','custom-fields'], 'show_in_rest' => false ]); } // hier komen pas de andere methods } /* ========================= * Helpers * ========================= */ private static function can_edit(){ return current_user_can('edit_posts'); } private static function require_login(){ check_ajax_referer('jfi_inspectie_nonce', 'nonce'); if (!is_user_logged_in()) { wp_send_json_error(['msg' => 'Niet ingelogd.'], 403); } } private static function map_entity($post){ $data = get_post_meta($post->ID, '_jfi_data', true); if (!is_array($data)) $data = []; return [ 'id' => (int)$post->ID, 'type' => $post->post_type, 'title' => $post->post_title, 'data' => $data ]; } private static function set_index_meta($id, $post_type, $payload){ if ($post_type === 'jfi_location' && isset($payload['opdrachtgeverId'])) { update_post_meta($id, '_jfi_client_id', (int)$payload['opdrachtgeverId']); } if ($post_type === 'jfi_plan' && isset($payload['locatieId'])) { update_post_meta($id, '_jfi_location_id', (int)$payload['locatieId']); } } /* ========================= * AJAX: KLANTEN * ========================= */ public static function ajax_list_klanten(){ self::require_login(); $q = new WP_Query([ 'post_type' => 'jfi_client', 'posts_per_page' => -1, 'post_status' => 'any' ]); $items = array_map([__CLASS__, 'map_entity'], $q->posts); wp_send_json_success(['items' => $items]); } public static function ajax_get_klant(){ self::require_login(); $id = (int)($_POST['id'] ?? 0); $p = get_post($id); if (!$p || $p->post_type !== 'jfi_client') { wp_send_json_error(['msg' => 'Klant niet gevonden'], 404); } wp_send_json_success(['item' => self::map_entity($p)]); } public static function ajax_save_klant(){ self::require_login(); if (!self::can_edit()) { wp_send_json_error(['msg' => 'Geen rechten'], 403); } $id = (int)($_POST['id'] ?? 0); $data_json = (string)($_POST['data_json'] ?? '{}'); $data = json_decode($data_json, true); if (!is_array($data)) $data = []; $naam = sanitize_text_field($data['naam'] ?? 'Nieuwe Opdrachtgever'); if ($id > 0) { wp_update_post(['ID' => $id, 'post_title' => $naam]); } else { $id = wp_insert_post([ 'post_type' => 'jfi_client', 'post_title' => $naam, 'post_status' => 'publish' ]); } update_post_meta($id, '_jfi_data', $data); wp_send_json_success(['item' => self::map_entity(get_post($id))]); } /* ========================= * AJAX: LOCATIES * ========================= */ public static function ajax_list_locaties(){ self::require_login(); $clientId = (int)($_POST['clientId'] ?? 0); $args = [ 'post_type' => 'jfi_location', 'posts_per_page' => -1, 'post_status' => 'any' ]; if ($clientId) { $args['meta_query'] = [[ 'key' => '_jfi_client_id', 'value' => $clientId, 'compare' => '=' ]]; } $q = new WP_Query($args); $items = array_map([__CLASS__, 'map_entity'], $q->posts); wp_send_json_success(['items' => $items]); } public static function ajax_get_locatie(){ self::require_login(); $id = (int)($_POST['id'] ?? 0); $p = get_post($id); if (!$p || $p->post_type !== 'jfi_location') { wp_send_json_error(['msg' => 'Locatie niet gevonden'], 404); } wp_send_json_success(['item' => self::map_entity($p)]); } public static function ajax_save_locatie(){ self::require_login(); if (!self::can_edit()) { wp_send_json_error(['msg' => 'Geen rechten'], 403); } $id = (int)($_POST['id'] ?? 0); $data_json = (string)($_POST['data_json'] ?? '{}'); $data = json_decode($data_json, true); if (!is_array($data)) $data = []; $naam = sanitize_text_field($data['naam'] ?? 'Nieuwe Locatie'); if ($id > 0) { wp_update_post(['ID' => $id, 'post_title' => $naam]); } else { $id = wp_insert_post([ 'post_type' => 'jfi_location', 'post_title' => $naam, 'post_status' => 'publish' ]); } update_post_meta($id, '_jfi_data', $data); self::set_index_meta($id, 'jfi_location', $data); wp_send_json_success(['item' => self::map_entity(get_post($id))]); } /* ========================= * AJAX: PLANNEN * ========================= */ public static function ajax_list_plannen(){ self::require_login(); $locationId = (int)($_POST['locationId'] ?? 0); $args = [ 'post_type' => 'jfi_plan', 'posts_per_page' => -1, 'post_status' => 'any' ]; if ($locationId) { $args['meta_query'] = [[ 'key' => '_jfi_location_id', 'value' => $locationId, 'compare' => '=' ]]; } $q = new WP_Query($args); $items = array_map([__CLASS__, 'map_entity'], $q->posts); wp_send_json_success(['items' => $items]); } public static function ajax_get_plan(){ self::require_login(); $id = (int)($_POST['id'] ?? 0); $p = get_post($id); if (!$p || $p->post_type !== 'jfi_plan') { wp_send_json_error(['msg' => 'Plan niet gevonden'], 404); } wp_send_json_success(['item' => self::map_entity($p)]); } public static function ajax_save_plan(){ self::require_login(); if (!self::can_edit()) { wp_send_json_error(['msg' => 'Geen rechten'], 403); } $id = (int)($_POST['id'] ?? 0); $data_json = (string)($_POST['data_json'] ?? '{}'); $data = json_decode($data_json, true); if (!is_array($data)) $data = []; $type = sanitize_text_field($data['type'] ?? 'Scope 10'); $datum = sanitize_text_field($data['datum'] ?? current_time('Y-m-d')); $title = $type . ' - ' . $datum; if ($id > 0) { wp_update_post(['ID' => $id, 'post_title' => $title]); } else { $id = wp_insert_post([ 'post_type' => 'jfi_plan', 'post_title' => $title, 'post_status' => 'publish' ]); } update_post_meta($id, '_jfi_data', $data); self::set_index_meta($id, 'jfi_plan', $data); wp_send_json_success(['item' => self::map_entity(get_post($id))]); } } /* ========================= * Post types (zoals oude klanten plugin) * ========================= */ public static function register_post_types(){ register_post_type('jfi_client', [ 'label' => 'Opdrachtgevers', 'public' => false, 'show_ui' => true, 'supports' => ['title','custom-fields'], 'show_in_rest' => false ]); register_post_type('jfi_location', [ 'label' => 'Locaties', 'public' => false, 'show_ui' => true, https://mentevisser.nl/wp-sitemap-posts-page-1.xmlhttps://mentevisser.nl/wp-sitemap-users-1.xml