You are here

function _scheduler_unpublish in Scheduler 7

Same name and namespace in other branches
  1. 6 scheduler.module \_scheduler_unpublish()

Unpublish scheduled nodes.

Return value

bool TRUE is any node has been unpublished, FALSE otherwise.

1 call to _scheduler_unpublish()
scheduler_cron in ./scheduler.module
Implements hook_cron().

File

./scheduler.cron.inc, line 108
Scheduler cron functions.

Code

function _scheduler_unpublish() {
  $result = FALSE;

  // If the time is greater than the time to unpublish a node, unpublish it.
  // The INNER join on 'node' and 'users' is just to ensure the nodes are valid.
  $query = db_select('scheduler', 's');
  $query
    ->addField('s', 'nid');
  $query
    ->addJoin('INNER', 'node', 'n', 's.nid = n.nid');
  $query
    ->addJoin('INNER', 'users', 'u', 'u.uid = n.uid');
  $query
    ->condition('s.unpublish_on', 0, '>');
  $query
    ->condition('s.unpublish_on', REQUEST_TIME, '<=');
  $query_result = $query
    ->execute();
  $nids = array();
  while ($node = $query_result
    ->fetchObject()) {
    $nids[] = $node->nid;
  }
  $action = 'unpublish';

  // Allow other modules to add to the list of nodes to be unpublished.
  $nids = array_unique(array_merge($nids, _scheduler_scheduler_nid_list($action)));

  // Allow other modules to alter the list of nodes to be unpublished.
  drupal_alter('scheduler_nid_list', $nids, $action);
  foreach ($nids as $nid) {
    $n = node_load($nid);

    // Check that scheduled publishing is (still) enabled for this type.
    if (!variable_get('scheduler_unpublish_enable_' . $n->type, 0)) {
      continue;
    }

    // Check that other modules allow the action on this node.
    if (!_scheduler_allow($n, $action)) {
      continue;
    }

    // Do not process the node if it has a publish_on time which is in the past,
    // as this implies that scheduled publishing has been blocked by one of the
    // API functions we provide. Hence unpublishing should be halted too.
    if (!empty($n->publish_on) && $n->publish_on <= REQUEST_TIME) {
      continue;
    }

    // Update timestamps.
    $old_change_date = $n->changed;
    $n->changed = $n->unpublish_on;
    $create_unpublishing_revision = variable_get('scheduler_unpublish_revision_' . $n->type, 0) == 1;
    if ($create_unpublishing_revision) {
      $n->revision = TRUE;

      // Use a core date format to guarantee a time is included.
      $n->log = t('Node unpublished by Scheduler on @now. Previous change date was @date.', array(
        '@now' => format_date(REQUEST_TIME, 'short'),
        '@date' => format_date($old_change_date, 'short'),
      ));
    }

    // Unset unpublish_on so the node will not get rescheduled by subsequent
    // calls to node_save(). Save the value for use when calling Rules.
    $unpublish_on = $n->unpublish_on;
    $n->unpublish_on = NULL;

    // Invoke scheduler API to allow modules to alter the node before it is
    // saved.
    // For 8.x this 'pre' call is moved up to just before 'Update timestamps'.
    // See https://www.drupal.org/node/2311273
    _scheduler_scheduler_api($n, 'pre_' . $action);

    // Use the actions system to unpublish the node.
    watchdog('scheduler', '@type: scheduled unpublishing of %title.', array(
      '@type' => $n->type,
      '%title' => $n->title,
    ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid, array(
      'alias' => TRUE,
    )));
    $actions = array(
      'node_unpublish_action',
      'node_save_action',
    );
    $context['node'] = $n;
    actions_do($actions, $n, $context, NULL, NULL);

    // Invoke event to tell Rules that Scheduler has unpublished this node.
    if (module_exists('rules')) {
      rules_invoke_event('scheduler_node_has_been_unpublished_event', $n, $n->publish_on, $unpublish_on);
    }

    // Invoke scheduler API for modules to react after the node is unpublished.
    _scheduler_scheduler_api($n, 'unpublish');
    $result = TRUE;
  }
  return $result;
}