You are here

function seo_checker_perform_checks in SEO Compliance Checker 6

Same name and namespace in other branches
  1. 6.2 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 76

Code

function seo_checker_perform_checks(&$form, &$form_values) {

  /* 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. */
    $check_result = round($rule['callback']($form_values));

    /* 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="' . 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);

  /* throw an error if failures are not allowed */
  $check_policy = variable_get('seo_checker_allow_failures', 'show-preview-only');
  switch ($check_policy) {
    case 'no-failures':
      if (!$checks_passed) {
        form_set_error('check_results', t('The SEO compliance check was not successful. Check the table and modify your content as required.'));
      }
    case 'show-always':
      drupal_set_message($message);
      break;
    case 'show-preview-only':
      if ($form_values['op'] == 'Preview') {
        drupal_set_message($message);
      }
  }
}