getInputs(); $id = (int) ( $inputs['schedule_id'] ?? 0 ); $post = $id ? get_post( $id ) : null; $title = $post ? get_post_meta( $id, 'etn_schedule_title', true ) : ''; $display = $title ? $title : ( $post ? $post->post_title : "ID {$id}" ); return sprintf( 'Update schedule "%s"', $display ); } /** * Human-readable version of tool inputs for the approval card. * Resolves schedule_id to schedule title. * * @return array */ public function get_approval_inputs(): array { $inputs = $this->getInputs(); $id = (int) ( $inputs['schedule_id'] ?? 0 ); if ( $id ) { $title = get_post_meta( $id, 'etn_schedule_title', true ); $inputs['schedule'] = ( $title ? $title : "ID {$id}" ) . ' (ID: ' . $id . ')'; unset( $inputs['schedule_id'] ); } return $inputs; } /** * Execute the tool * * @param int $schedule_id Schedule post ID. * @param string|null $program_title New title. * @param string|null $date New date (YYYY-MM-DD). * @param string|null $day_name New day label. * @param string|null $schedule_slot JSON-encoded session array. * @param bool|null $hide_date_on_event_page Whether to hide the date. * @return array */ public function __invoke( int $schedule_id, ?string $program_title = null, ?string $date = null, ?string $day_name = null, ?string $schedule_slot = null, ?bool $hide_date_on_event_page = null ): array { $check = $this->guard( 'etn_manage_schedule' ); if ( $check ) { return $check; } try { if ( $schedule_id <= 0 ) { return [ 'error' => 'schedule_id must be a positive integer.' ]; } $post = get_post( $schedule_id ); if ( ! $post || 'etn-schedule' !== $post->post_type ) { return [ 'error' => "No Eventin schedule found with ID {$schedule_id}." ]; } $existing_title = get_post_meta( $schedule_id, 'etn_schedule_title', true ); $existing_date = get_post_meta( $schedule_id, 'etn_schedule_date', true ); $body = [ 'program_title' => sanitize_text_field( null !== $program_title ? $program_title : $existing_title ), 'date' => sanitize_text_field( null !== $date ? $date : $existing_date ), ]; if ( null !== $day_name ) { $body['day_name'] = sanitize_text_field( $day_name ); } if ( null !== $schedule_slot ) { // schedule_slot REPLACES all slots; reject bad JSON instead of wiping them. $decoded = json_decode( $schedule_slot, true ); if ( JSON_ERROR_NONE !== json_last_error() ) { return [ 'error' => 'schedule_slot must be a valid JSON array of session objects.' ]; } if ( ! is_array( $decoded ) || empty( $decoded ) ) { return [ 'error' => 'schedule_slot must be a non-empty JSON array of session objects; sending an empty array would delete all existing slots.' ]; } $body['schedule_slot'] = $decoded; } if ( null !== $hide_date_on_event_page ) { $body['hide_date_on_event_page'] = (bool) $hide_date_on_event_page; } $updated_fields = array_keys( array_filter( [ 'program_title' => null !== $program_title, 'date' => null !== $date, 'day_name' => null !== $day_name, 'schedule_slot' => null !== $schedule_slot, 'hide_date_on_event_page' => null !== $hide_date_on_event_page, ] ) ); if ( empty( $updated_fields ) ) { return [ 'error' => 'No fields provided to update.' ]; } $request = new \WP_REST_Request( 'PUT' ); $request->set_param( 'id', $schedule_id ); $request->set_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( $body ) ); $response = ( new \Eventin\Schedule\Api\ScheduleController() )->update_item( $request ); if ( is_wp_error( $response ) ) { return [ 'error' => $response->get_error_message() ]; } $display_title = null !== $program_title ? $program_title : $existing_title; return [ 'success' => true, 'schedule_id' => $schedule_id, 'updated_fields' => $updated_fields, 'message' => "Schedule \"{$display_title}\" (ID: {$schedule_id}) updated successfully.", 'edit_url' => get_edit_post_link( $schedule_id, 'raw' ), ]; } catch ( \Exception $e ) { Log::error( 'Eventin_Update_Schedule_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update schedule: ' . $e->getMessage() ]; } } }