repository = $repository ?? new WorkflowRepository(); } /** * {@inheritdoc} */ public function save( array $workflow_data ): array { $workflow_id = isset( $workflow_data['workflow_id'] ) ? absint( $workflow_data['workflow_id'] ) : 0; if ( $workflow_id > 0 ) { $existing = $this->find( $workflow_id ); $remote_uuid = ''; if ( $existing ) { $remote_uuid = $existing->getRemoteWorkflowUuid(); } if ( '' === trim( $remote_uuid ) ) { $remote_uuid = $this->generate_workflow_uuid(); } $updated = $this->repository->update( $workflow_id, (string) $workflow_data['name'], (string) $workflow_data['trigger'], (array) $workflow_data['steps'], (array) $workflow_data['conditions'], ! empty( $workflow_data['is_active'] ), $remote_uuid ); return array( 'success' => $updated, 'workflow_id' => $workflow_id, 'remote_workflow_uuid' => $remote_uuid, 'error_message' => $updated ? '' : __( 'Failed to update workflow locally.', 'afroit-messaging-workflows' ), ); } $remote_uuid = $this->generate_workflow_uuid(); $new_id = $this->repository->insert( (string) $workflow_data['name'], (string) $workflow_data['trigger'], (array) $workflow_data['steps'], (array) $workflow_data['conditions'], ! empty( $workflow_data['is_active'] ), $remote_uuid ); return array( 'success' => $new_id > 0, 'workflow_id' => $new_id, 'remote_workflow_uuid' => $remote_uuid, 'error_message' => $new_id > 0 ? '' : __( 'Failed to save workflow locally.', 'afroit-messaging-workflows' ), ); } /** * Stable per-workflow identifier (set once at creation, kept on update). * * @return string */ private function generate_workflow_uuid(): string { if ( function_exists( 'wp_generate_uuid4' ) ) { return wp_generate_uuid4(); } return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', wp_rand( 0, 0xffff ), wp_rand( 0, 0xffff ), wp_rand( 0, 0xffff ), wp_rand( 0, 0x0fff ) | 0x4000, wp_rand( 0, 0x3fff ) | 0x8000, wp_rand( 0, 0xffff ), wp_rand( 0, 0xffff ), wp_rand( 0, 0xffff ) ); } /** * Update an existing workflow row, preserving remote UUID if the payload omits it. * * @param array $workflow_data Workflow payload. * @return array */ public function sync_workflow( array $workflow_data ): array { $workflow_id = isset( $workflow_data['workflow_id'] ) ? absint( $workflow_data['workflow_id'] ) : 0; if ( $workflow_id <= 0 ) { return $this->save( $workflow_data ); } $existing = $this->find( $workflow_id ); if ( ! $existing ) { return array( 'success' => false, 'workflow_id' => $workflow_id, 'error_message' => __( 'Workflow not found locally.', 'afroit-messaging-workflows' ), ); } if ( empty( $workflow_data['remote_workflow_uuid'] ) ) { $workflow_data['remote_workflow_uuid'] = $existing->getRemoteWorkflowUuid(); } return $this->save( $workflow_data ); } /** * {@inheritdoc} */ public function delete( int $workflow_id ): array { $deleted = $this->repository->delete( $workflow_id ); return array( 'success' => $deleted, 'workflow_id' => $workflow_id, 'error_message' => $deleted ? '' : __( 'Failed to delete workflow locally.', 'afroit-messaging-workflows' ), ); } /** * {@inheritdoc} */ public function update_active_status( int $workflow_id, bool $is_active ): array { $updated = $this->repository->updateActiveStatus( $workflow_id, $is_active ); return array( 'success' => $updated, 'workflow_id' => $workflow_id, 'error_message' => $updated ? '' : __( 'Failed to update workflow status locally.', 'afroit-messaging-workflows' ), ); } /** * {@inheritdoc} */ public function find( int $workflow_id ): ?Workflow { return $this->repository->findById( $workflow_id ); } }