{\rtf1\ansi\ansicpg1252\cocoartf2639 \cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} {\*\expandedcolortbl;;} \paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0 \f0\fs24 \cf0 \ \ 'Shift Reports',\ 'singular_name' => 'Shift Report'\ );\ register_post_type( 'shift_report', array(\ 'labels'=>$labels,\ 'public' => false,\ 'show_ui' => true,\ 'supports' => array('title','custom-fields'),\ 'capability_type' => 'post',\ ));\ \}\ \ public function enqueue_assets()\{\ $base = plugin_dir_url(__FILE__);\ wp_enqueue_style( 'shift-hours-css', $base . 'assets/css/shift-hours.css', array(), '1.0' );\ wp_enqueue_script( 'jspdf', 'https://cdn.jsdelivr.net/npm/jspdf@2.5.1/dist/jspdf.umd.min.js', array(), null, true );\ wp_enqueue_script( 'shift-hours-js', $base . 'assets/js/shift-hours.js', array('jquery','jspdf'), '1.0', true );\ wp_localize_script( 'shift-hours-js', 'ShiftTracker', array(\ 'ajax_url' => admin_url('admin-ajax.php'),\ 'nonce' => wp_create_nonce('shift_tracker_nonce'),\ ));\ \}\ \ public function shortcode_form()\{\ ob_start(); ?>\
\ \ \
\ \
\ \
\
\ \ \ \ \
\ \ \ \
\ \ \ \
\ \ \
\ \
\
\
\
\ \
\
\ \ \ \ \ \ \
\ \
\ \
No data loaded. Select a filter and click Load.
\
\
\ validate_nonce();\ \ $date = sanitize_text_field( $_POST['date'] ?? '' );\ $shift = sanitize_text_field( $_POST['shift'] ?? '' );\ $start = sanitize_text_field( $_POST['start_time'] ?? '' );\ $end = sanitize_text_field( $_POST['end_time'] ?? '' );\ $note = sanitize_textarea_field( $_POST['note'] ?? '' );\ \ if ( ! $date || ! $start || ! $end ) \{\ wp_send_json_error('missing_fields');\ \}\ \ // compute hours (decimal)\ $start_dt = DateTime::createFromFormat('Y-m-d H:i', $date . ' ' . $start);\ $end_dt = DateTime::createFromFormat('Y-m-d H:i', $date . ' ' . $end);\ \ // if end < start assume same day (but we won't handle multi-day in this plugin)\ if ( $end_dt < $start_dt ) \{\ // try adding one day to end\ $end_dt->modify('+1 day');\ \}\ \ $diff = $end_dt->getTimestamp() - $start_dt->getTimestamp();\ $hours = round( $diff / 3600, 2 );\ \ $title = $date . ' - Shift ' . $shift;\ \ $post_id = wp_insert_post(array(\ 'post_type' => 'shift_report',\ 'post_title' => $title,\ 'post_status' => 'publish'\ ));\ \ if ( is_wp_error($post_id) ) \{\ wp_send_json_error('db_error');\ \}\ \ update_post_meta($post_id, 'sht_date', $date);\ update_post_meta($post_id, 'sht_shift', $shift);\ update_post_meta($post_id, 'sht_start', $start);\ update_post_meta($post_id, 'sht_end', $end);\ update_post_meta($post_id, 'sht_hours', $hours);\ update_post_meta($post_id, 'sht_note', $note);\ \ wp_send_json_success(array('message'=>'saved','hours'=>$hours));\ \}\ \ public function ajax_get_reports()\{\ $this->validate_nonce();\ \ $type = sanitize_text_field( $_GET['type'] ?? 'date' );\ $date = sanitize_text_field( $_GET['date'] ?? '' ); // YYYY-MM-DD\ \ $args = array(\ 'post_type' => 'shift_report',\ 'posts_per_page' => -1,\ 'post_status' => 'publish',\ 'meta_query' => array()\ );\ \ if ( $type === 'date' && $date ) \{\ $args['meta_query'][] = array(\ 'key' => 'sht_date',\ 'value' => $date,\ 'compare' => '='\ );\ \} elseif ( $type === 'week' && $date ) \{\ $dt = new DateTime($date);\ // find monday\ $monday = clone $dt;\ $monday->modify('monday this week');\ $sunday = clone $monday;\ $sunday->modify('sunday this week');\ $args['meta_query'][] = array(\ 'key' => 'sht_date',\ 'value' => array( $monday->format('Y-m-d'), $sunday->format('Y-m-d') ),\ 'compare' => 'BETWEEN',\ 'type' => 'DATE'\ );\ \} elseif ( $type === 'month' && $date ) \{\ $dt = new DateTime($date);\ $first = $dt->format('Y-m-01');\ $last = $dt->format('Y-m-t');\ $args['meta_query'][] = array(\ 'key' => 'sht_date',\ 'value' => array( $first, $last ),\ 'compare' => 'BETWEEN',\ 'type' => 'DATE'\ );\ \}\ \ $q = new WP_Query($args);\ $rows = array();\ $total_hours = 0;\ if ( $q->have_posts() ) \{\ foreach ( $q->posts as $p ) \{\ $d = get_post_meta($p->ID,'sht_date',true);\ $s = get_post_meta($p->ID,'sht_shift',true);\ $st = get_post_meta($p->ID,'sht_start',true);\ $en = get_post_meta($p->ID,'sht_end',true);\ $h = (float) get_post_meta($p->ID,'sht_hours',true);\ $note = get_post_meta($p->ID,'sht_note',true);\ $rows[] = array(\ 'date'=>$d,\ 'shift'=>$s,\ 'start'=>$st,\ 'end'=>$en,\ 'hours'=>$h,\ 'note'=>$note\ );\ $total_hours += $h;\ \}\ \}\ \ wp_send_json_success(array('rows'=>$rows,'total_hours'=>round($total_hours,2)));\ \}\ \}\ \ new Shift_Hours_Tracker();\ }