You are here

function _scheduler_publish in Scheduler 7

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

Publish scheduled nodes.

Return value

bool TRUE if any node has been published, FALSE otherwise.

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

File

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

Code

function _scheduler_publish() {
  $result = FALSE;

  // If the time now is greater than the time to publish a node, publish 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.publish_on', 0, '>');
  $query
    ->condition('s.publish_on', REQUEST_TIME, '<=');
  $query_result = $query
    ->execute();
  $nids = array();
  while ($node = $query_result
    ->fetchObject()) {
    $nids[] = $node->nid;
  }
  $action = 'publish';

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

  // Allow other modules to alter the list of nodes to be published.
  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_publish_enable_' . $n->type, 0)) {
      continue;
    }

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

    // Update timestamps.
    $n->changed = $n->publish_on;
    $old_creation_date = $n->created;
    if (variable_get('scheduler_publish_touch_' . $n->type, 0) == 1) {
      $n->created = $n->publish_on;
    }
    $create_publishing_revision = variable_get('scheduler_publish_revision_' . $n->type, 0) == 1;
    if ($create_publishing_revision) {
      $n->revision = TRUE;

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

    // Unset publish_on so the node will not get rescheduled by subsequent calls
    // to node_save(). Save the value for use when calling Rules.
    $publish_on = $n->publish_on;
    $n->publish_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 publish the node.
    watchdog('scheduler', '@type: scheduled publishing of %title.', array(
      '@type' => $n->type,
      '%title' => $n->title,
    ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid, array(
      'alias' => TRUE,
    )));
    $actions = array(
      'node_publish_action',
      'node_save_action',
    );
    $context['node'] = $n;
    actions_do($actions, $n, $context, NULL, NULL);

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

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