-
Notifications
You must be signed in to change notification settings - Fork 0
/
seooptim.module
78 lines (71 loc) · 2.53 KB
/
seooptim.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Implements hook_filter().
*/
function seooptim_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
switch ($op) {
case 'list':
return array(0 => t('SEO Optim Filter'));
case 'description':
return t('Make SEO optimizations on content');
case "process":
$text = preg_replace_callback('/<img[^>]*(alt="")[^>]*src="([^"]*)"[^>]*>/', '_seooptim_filter_imgalt', $text);
return $text;
default:
return $text;
}
}
function _seooptim_filter_imgalt($matches) {
$src = $matches[2];
$src_array = explode("/", $src);
$alt = array_pop($src_array); // get file name
$src_array = explode(".", $alt);
array_pop($src_array); // Delete extension
$alt = implode(' ', $src_array);
$alt = str_replace(array('-','_'), array(' ',' '), $alt);
return str_replace('alt=""', 'alt="' . check_markup($alt) . '"', $matches[0]);
}
function seooptim_preprocess_content_field(&$variables) {
if($variables['field']['widget']['type'] == 'imagefield_widget') {
foreach($variables['items'] as $key=>$image) {
if(empty($variables['items'][$key]['data']['alt'])) {
if(!empty($variables['items'][$key]['data']['description'])) {
$alt = $variables['items'][$key]['data']['description'];
}
else if(!empty($variables['items'][$key]['data']['title'])) {
$alt = $variables['items'][$key]['data']['title'];
}
else {
$alt = $variables['node']->title;
}
$alt = check_plain($alt);
$variables['items'][$key]['data']['alt'] = $alt;
$variables['items'][$key]['view'] = str_replace('alt=""', 'alt="' . $alt . '"', $variables['items'][$key]['view']);
}
}
}
}
function seooptim_preprocess_node(&$variables) {
foreach($variables as $key=>$value) {
if(strpos($key, 'field_') !== FALSE && is_array($value)) {
if(isset($value[0]['fid']) && strpos($value[0]['filemime'], 'image') !== FALSE) { // It's an imagefield
foreach($value as $itimage=>$image) {
if(empty($variables[$key][$itimage]['data']['alt'])) {
if(!empty($variables[$key][$itimage]['data']['description'])) {
$alt = $variables[$key][$itimage]['data']['description'];
}
else if(!empty($variables[$key][$itimage]['data']['title'])) {
$alt = $variables[$key][$itimage]['data']['title'];
}
else {
$alt = $variables['title'];
}
$alt = check_plain($alt);
$variables[$key][$itimage]['data']['alt'] = $alt;
$variables[$key][$itimage]['view'] = str_replace('alt=""', 'alt="'. $alt .'"', $variables[$key][$itimage]['view']);
}
}
}
}
}
}