Skip to content

Commit

Permalink
Fix: update cron class.
Browse files Browse the repository at this point in the history
  • Loading branch information
ardavydov committed Jul 16, 2021
1 parent 39b8c8d commit 3186aaf
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions cleantalk.antispam/lib/Cleantalk/Common/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,22 @@ public function addTask( $task, $handler, $period, $first_call = null, $params =
{
// First call time() + period
$first_call = ! $first_call ? time() + $period : $first_call;

if( isset( $this->tasks[ $task ] ) ){

$tasks = ! empty( $this->tasks ) ? $this->tasks : $this->getTasks();

if( isset( $tasks[ $task ] ) ){
return false;
}

// Task entry
$this->tasks[$task] = array(
$tasks[$task] = array(
'handler' => $handler,
'next_call' => $first_call,
'period' => $period,
'params' => $params,
);

return $this->saveTasks( $this->tasks );
return $this->saveTasks( $tasks );
}

/**
Expand All @@ -122,13 +124,15 @@ public function addTask( $task, $handler, $period, $first_call = null, $params =
*/
public function removeTask( $task )
{
if( ! isset( $this->tasks[ $task ] ) ){
$tasks = ! empty( $this->tasks ) ? $this->tasks : $this->getTasks();

if( ! isset( $tasks[ $task ] ) ){
return false;
}

unset( $this->tasks[ $task ] );
unset( $tasks[ $task ] );

return $this->saveTasks( $this->tasks );
return $this->saveTasks( $tasks );
}

/**
Expand All @@ -144,8 +148,18 @@ public function removeTask( $task )
*/
public function updateTask( $task, $handler, $period, $first_call = null, $params = array() )
{
$this->removeTask( $task );
return $this->addTask( $task, $handler, $period, $first_call, $params );
$tasks = ! empty( $this->tasks ) ? $this->tasks : $this->getTasks();
if( isset( $tasks[ $task ] ) ){
// Rewrite the task
$tasks[$task] = array(
'handler' => $handler,
'next_call' => is_null( $first_call ) ? time() + $period : $first_call,
'period' => $period,
'params' => $params,
);
return $this->saveTasks( $tasks );
}
return false;
}

/**
Expand Down

0 comments on commit 3186aaf

Please sign in to comment.