You are here

function seo_checker_perform_checks in SEO Compliance Checker 6.2

Same name and namespace in other branches
  1. 6 seo_checker.module \seo_checker_perform_checks()

This is the main seo checker function. It checks the submitted node by applying all the rules and includes the summary to the node form.

1 call to seo_checker_perform_checks()
seo_checker_node_form_validate in ./seo_checker.module

File

./seo_checker.module, line 79

Code

function seo_checker_perform_checks(&$form, &$form_values) {
  global $user;

  /* overall status */
  $checks_passed = TRUE;

  /* the results will be prepended to the node_preview block */
  $results = array();

  /* loop over all the rules and apply them to the form_values */
  $rules = module_invoke_all('register_seo_rules');
  foreach ($rules as $rid => $rule) {
    if (!function_exists($rule['callback'])) {
      drupal_set_message(sprintf(t('The function <i>%s()</i> for seo_checker rule <b>%s</b> was not found.'), $rule['callback'], strip_tags(check_markup($rule['name']))), 'error');
      continue;
    }

    /* threshold == 0 implies that this rule is desabled. */
    if (($threshold = seo_checker_get_rule_threshold($rule, $rid)) == array(
      0,
      100,
    )) {
      continue;
    }
    $threshold_text = '';
    if ($threshold[1] == 100) {
      $threshold_text = '&ge;&nbsp;' . $threshold[0] . '%';
    }
    else {
      $threshold_text = '&#8712;&nbsp;[' . $threshold[0] . '%,' . $threshold[1] . '%]';
    }

    /* here we apply the rules to the values. It should return an integer result. */
    $arguments = isset($rule['callback arguments']) ? $rule['callback arguments'] : array();
    array_unshift($arguments, $form_values);
    $check_result = call_user_func_array($rule['callback'], $arguments);
    if ($check_result === FALSE) {

      /* skip this rule */
      continue;
    }
    $check_result = round($check_result);

    /* check if the submitted node has passed the test. */
    $passed = $check_result >= $threshold[0] && $check_result <= $threshold[1] ? 'passed' : 'failed';
    $data = array(
      'rule' => strip_tags(check_markup($rule['name'])),
      'message' => $rule[$passed . ' feedback'],
      'achieved' => $check_result . '%',
      'required' => $threshold_text,
      'passed' => '<img src="' . file_create_url(drupal_get_path('module', 'seo_checker') . '/img/' . $passed) . '.gif" alt="' . $passed . '" />',
    );

    /* append the results to the array and update the overall status variable */
    $results[] = array(
      'data' => $data,
      'class' => 'seo_checker_result_' . $passed,
    );
    $checks_passed &= $check_result >= $threshold[0] && $check_result <= $threshold[1];
  }

  /* append the check results just before the title field */
  $seo_check_results = array(
    '#type' => 'seo_check_results',
    '#title' => t('SEO Check Results'),
    '#results' => $results,
  );
  $message = drupal_render($seo_check_results);
  if (!$checks_passed && (!user_access('allow seo check failures', $user) || $user->uid == 1 && variable_get('seo_checker_admin_allow_failurs', 0) == 0)) {
    form_set_error('check_results', t('The SEO compliance check was not successful. Check the table and modify your content as required.'));
    drupal_set_message($message);
  }
  else {

    /* throw an error if failures are not allowed */
    $check_policy = variable_get('seo_checker_allow_failures', 'show-preview-only');
    switch ($check_policy) {
      case 'show-always':
        drupal_set_message($message);
        break;
      case 'show-preview-only':
        if ($form_values['op'] == 'Preview') {
          drupal_set_message($message);
        }
    }
  }
}