You are here

public function Drafty::restorePublishedRevisions in Drafty 7

Publish revisions previously set with setRevisionToBePublished().

File

./drafty.module, line 327
Hook implementations and API functions for the Drafty module.

Class

Drafty
Handles tracking, selecting and publishing revisions.

Code

public function restorePublishedRevisions() {
  $delete_old_revisions = variable_get('drafty_delete_old_revisions', FALSE);
  $delete_with_cron = variable_get('drafty_delete_with_cron', TRUE);
  $queue = NULL;
  $operations = array();
  foreach ($this->revisionsToPublish as $type => $value) {
    foreach ($value as $id => $vid) {
      unset($this->revisionsToPublish[$type][$id]);
      $published_revision = $this
        ->publishRevision($type, $id, $vid);

      // Now that the revision is deleted, there are two identical copies of
      // the revision in the system. The original 'draft' revision and the
      // newly saved published revision.
      if ($delete_old_revisions) {
        list(, $replaced_by) = entity_extract_ids($type, $published_revision);

        // Deletion must be done on a different request thread, or any other
        // hooks called for the revision-to-be-deleted will fail.
        $drafty_queue_item = array(
          'entity_type' => $type,
          'entity_id' => $id,
          'revision_id' => $vid,
          'replaced_by' => $replaced_by,
        );
        if ($delete_with_cron) {
          if (empty($queue)) {
            $queue = DrupalQueue::get('drafty_revision_delete');
          }
          $queue
            ->createItem($drafty_queue_item);
        }
        else {
          $operations[] = array(
            'drafty_queue_delete_revision',
            array(
              $drafty_queue_item,
            ),
          );
        }
      }

      // @todo: when restoring a published revision, should the revision
      // timestamp be set to the old value?
    }
  }
  if (!empty($operations)) {
    $batch = array(
      'operations' => $operations,
      'finished' => 'drafty_delete_batch_finished',
    );
    batch_set($batch);
  }
}