View source
<?php
require_once './' . drupal_get_path('module', 'seo_checker') . '/inc/seo_checker.admin.inc';
require_once './' . drupal_get_path('module', 'seo_checker') . '/inc/seo_checker.theme.inc';
function seo_checker_menu() {
$items['admin/settings/seo_checker'] = array(
'title' => t('SEO Checker'),
'description' => t('Manage SEO Checker'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'seo_checker_settings',
),
'access arguments' => array(
'administer seo_checker configuration',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/seo_checker/thresholds'] = array(
'title' => t('SEO Rule Thresholds'),
'description' => t('Set the tresholds for the different SEO rules.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'seo_checker_settings',
),
'access arguments' => array(
'administer seo_checker configuration',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
return $items;
}
function seo_checker_node_form_validate($form, &$form_state) {
seo_checker_perform_checks($node, $form_state['values']);
}
function seo_checker_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'node_form') && variable_get('seo_checker_' . $form['type']['#value'], 0) != 0) {
$form['#validate'][] = 'seo_checker_node_form_validate';
}
}
function seo_checker_get_rule_threshold($rule, $rid) {
$threshold = variable_get('seo_threshold_' . $rid, !empty($rule['default threshold']) ? $rule['default threshold'] : array(
1000,
100,
));
if (!is_array($threshold)) {
$threshold = array(
$threshold,
100,
);
}
return $threshold;
}
function seo_checker_perform_checks(&$form, &$form_values) {
$checks_passed = TRUE;
$results = array();
$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;
}
if (($threshold = seo_checker_get_rule_threshold($rule, $rid)) == array(
0,
100,
)) {
continue;
}
$threshold_text = '';
if ($threshold[1] == 100) {
$threshold_text = '≥ ' . $threshold[0] . '%';
}
else {
$threshold_text = '∈ [' . $threshold[0] . '%,' . $threshold[1] . '%]';
}
$check_result = round($rule['callback']($form_values));
$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 . '" />',
);
$results[] = array(
'data' => $data,
'class' => 'seo_checker_result_' . $passed,
);
$checks_passed &= $check_result >= $threshold[0] && $check_result <= $threshold[1];
}
$seo_check_results = array(
'#type' => 'seo_check_results',
'#title' => t('SEO Check Results'),
'#results' => $results,
);
$message = drupal_render($seo_check_results);
$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);
}
}
}
function seo_checker_form_node_type_form_alter(&$form, $form_status) {
$form['workflow']['seo_checker'] = array(
'#type' => 'checkbox',
'#title' => t('Enable SEO checking'),
'#default_value' => variable_get('seo_checker_' . $form['#node_type']->type, 0),
'#description' => t('Check this box to enable SEO checking for this node type.'),
);
}
function seo_checker_wordipos($haystack, $needle, $offset = 0) {
$pos = -1;
$found = FALSE;
while ($found === FALSE) {
$pos = stripos($haystack, $needle, $offset);
if ($pos === FALSE) {
return FALSE;
}
$expanded_string = (isset($haystack[$pos - 1]) ? $haystack[$pos - 1] : ' ') . (isset($haystack[$pos + strlen($needle)]) ? $haystack[$pos + strlen($needle)] : ' ');
if (preg_match_all('/[\\W_]/', $expanded_string, $null) == 2) {
$found = TRUE;
}
$offset = $pos + 1;
}
return $pos;
}