public function get_args_hash(): string { return $this->args_hash; } /** * Gets the task's data. * * @since 0.0.1 * * @return string The task's data. */ public function get_data(): string { return $this->data; } /** * Gets the task's current try. * * @since 0.0.1 * * @return int The task's current try. */ public function get_current_try(): int { return $this->current_try; } /** * Gets the task's arguments. * * @since 0.0.1 * * @return array The task's arguments. */ public function get_args(): array { return $this->args; } /** * Gets the table interface for the task. * * @since 0.0.1 * * @return Table_Abstract The table interface. */ public function get_table_interface(): Table_Abstract { return Config::get_container()->get(static::TABLE_INTERFACE); } /** * Saves the task. * * @since 0.0.1 * @since 0.0.8 Updated to delete stale tasks from the database. * * @return int The id of the saved task. * * @throws ShepherdTaskAlreadyExistsException If multiple tasks are found with the same arguments hash. * @throws RuntimeException If multiple tasks are found with the same arguments hash. */ public function save(): int { $task_id = parent::save(); $table_interface = Config::get_container()->get(static::TABLE_INTERFACE); $tasks = $table_interface::get_by_args_hash($this->get_args_hash()); if (count($tasks) === 1) { return $task_id; } $action_ids = array_map(fn(Task $task) => $task->get_action_id(), $tasks); [$pending_actions, $non_pending_actions] = Action_Scheduler_Methods::get_pending_and_non_pending_actions_by_ids($action_ids); $stale_task_ids = array_map(fn(Task $task) => $task->get_id(), array_filter($tasks, fn(Task $task) => in_array($task->get_action_id(), array_keys($non_pending_actions), true))); if (!empty($stale_task_ids)) { Herding::delete_data_of_tasks($stale_task_ids); } if (count($pending_actions) > 1) { throw new RuntimeException(esc_html_x('Multiple tasks found with the same arguments hash.', 'This error is thrown when multiple tasks are found with the same arguments hash while they are also pending.', 'stellarwp-shepherd')); } $number_of_actions = count($action_ids); $number_of_unique_actions = count(array_unique($action_ids)); if ($number_of_actions !== $number_of_unique_actions) { throw new ShepherdTaskAlreadyExistsException(esc_html_x('Multiple tasks found with the same arguments hash.', 'This error is thrown when multiple tasks are found with the same arguments hash.', 'stellarwp-shepherd')); } return $task_id; } }