classes/Tasks/BatchManager.php
public function addToBatchAndRun($batch, $postIDs) {
        if (!isset(static::$batchClasses[$batch])) {
            throw new \Exception("Batch '$batch' is not registered.");
        }

        $this->reset($batch);

        if (count($postIDs) == 0) {
            return;
        }

        $testResult = $this->testConnectivity();
        if ($testResult !== true) {
            $error = 'Unknown';

            if (is_wp_error($testResult)) {
                $error = $testResult->get_error_message();
            } else if (is_array($testResult)) {
                $error = 'HTTP response code was '.$testResult['response']['code'];
            }

            $storageSettingsURL = admin_url('admin.php?page=media-tools-s3#ilab-media-s3-batch-settings');
            $message = "There was an error attempting to run your batch.  Try changing the <strong>Connection Timeout</strong> in <a href='$storageSettingsURL'>Storage Settings</a> to a higher number like 0.1 or 1 to see if that helps.  The actual error was: $error";
            $this->setErrorMessage($batch, $message);

            throw new \Exception($message);
        }

        $firstPostFile = get_attached_file($postIDs[0]);
        $fileName = basename($firstPostFile);

        $this->setCurrent($batch, 1);
        $this->setTotalCount($batch, count($postIDs));
        $this->setCurrentFile($batch, $fileName);
        $this->setShouldCancel($batch, false);
        $this->setStatus($batch, true);

        /** @var BackgroundProcess $batchProcess */
        $batchProcess = new static::$batchClasses[$batch]();

        $index = 0;
        foreach($postIDs as $postID) {
            $batchProcess->push_to_queue(['index' => $index, 'post' => $postID]);
            $index++;
        }

        $batchProcess->save();
        $batchProcess->dispatch();

        $this->setLastRun($batch, time());
    }
public function addToBatchAndRun($batch, $postIDs) {
        if (!isset(static::$batchClasses[$batch])) {
            throw new \Exception("Batch '$batch' is not registered.");
        }

        $this->reset($batch);

        if (count($postIDs) == 0) {
            return;
        }

        $testResult = $this->testConnectivity();
        if ($testResult !== true) {
            $error = 'Unknown';

            if (is_wp_error($testResult)) {
                $error = $testResult->get_error_message();
            } else if (is_array($testResult)) {
                $error = 'HTTP response code was '.$testResult['response']['code'];
            }

            $storageSettingsURL = admin_url('admin.php?page=media-tools-s3#ilab-media-s3-batch-settings');
            $message = "There was an error attempting to run your batch.  Try changing the <strong>Connection Timeout</strong> in <a href='$storageSettingsURL'>Storage Settings</a> to a higher number like 0.1 or 1 to see if that helps.  The actual error was: $error";
            $this->setErrorMessage($batch, $message);

            throw new \Exception($message);
        }

        $firstPostFile = get_attached_file($postIDs[0]);
        $fileName = basename($firstPostFile);

        $this->setCurrent($batch, 1);
        $this->setTotalCount($batch, count($postIDs));
        $this->setCurrentFile($batch, $fileName);
        $this->setShouldCancel($batch, false);
        $this->setStatus($batch, true);

        /** @var BackgroundProcess $batchProcess */
        $batchProcess = new static::$batchClasses[$batch]();

        $index = 0;
        foreach($postIDs as $postID) {
            $batchProcess->push_to_queue(['index' => $index, 'post' => $postID]);
            $index++;
        }

        $batchProcess->save();
        $batchProcess->dispatch();

        $this->setLastRun($batch, time());
    }
public function addToBatchAndRun ( $ batch , $ postIDs ) { if ( ! isset ( static :: $ batchClasses [ $ batch ] ) ) { throw new \ Exception ( "Batch '$batch' is not registered." ) ; } $ this -> reset ( $ batch ) ; if ( count ( $ postIDs ) == 0 ) { return ; } $ testResult = $ this -> testConnectivity ( ) ; if ( $ testResult !== true ) { $ error = 'Unknown' ; if ( is_wp_error ( $ testResult ) ) { $ error = $ testResult -> get_error_message ( ) ; } else if ( is_array ( $ testResult ) ) { $ error = 'HTTP response code was ' . $ testResult [ 'response' ] [ 'code' ] ; } $ storageSettingsURL = admin_url ( 'admin.php?page=media-tools-s3#ilab-media-s3-batch-settings' ) ; $ message = "There was an error attempting to run your batch.  Try changing the <strong>Connection Timeout</strong> in <a href='$storageSettingsURL'>Storage Settings</a> to a higher number like 0.1 or 1 to see if that helps.  The actual error was: $error" ; $ this -> setErrorMessage ( $ batch , $ message ) ; throw new \ Exception ( $ message ) ; } $ firstPostFile = get_attached_file ( $ postIDs [ 0 ] ) ; $ fileName = basename ( $ firstPostFile ) ; $ this -> setCurrent ( $ batch , 1 ) ; $ this -> setTotalCount ( $ batch , count ( $ postIDs ) ) ; $ this -> setCurrentFile ( $ batch , $ fileName ) ; $ this -> setShouldCancel ( $ batch , false ) ; $ this -> setStatus ( $ batch , true ) ; /** @var BackgroundProcess $batchProcess */ $ batchProcess = new static :: $ batchClasses [ $ batch ] ( ) ; $ index = 0 ; foreach ( $ postIDs as $ postID ) { $ batchProcess -> push_to_queue ( [ 'index' => $ index , 'post' => $ postID ] ) ; $ index ++ ; } $ batchProcess -> save ( ) ; $ batchProcess -> dispatch ( ) ; $ this -> setLastRun ( $ batch , time ( ) ) ; }
Adds posts to a batch and runs it.  If another one of this batch type is running, it will be cancelled.
@param $batch
@param $postIDs
@throws \Exception
Adds posts to a batch and runs it . If another one of this batch type is running it will be cancelled .