1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: meta |
---|
4 | Version: auto |
---|
5 | Description: Allows to add metadata |
---|
6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=220 |
---|
7 | Author: ddtddt |
---|
8 | Author URI: http://piwigo.org/ |
---|
9 | */ |
---|
10 | |
---|
11 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
12 | |
---|
13 | global $prefixeTable; |
---|
14 | |
---|
15 | define('meta_DIR' , basename(dirname(__FILE__))); |
---|
16 | define('meta_PATH' , PHPWG_PLUGINS_PATH . meta_DIR . '/'); |
---|
17 | define('meta_TABLE' , $prefixeTable . 'meta'); |
---|
18 | define('meta_img_TABLE' , $prefixeTable . 'meta_img'); |
---|
19 | define('meta_cat_TABLE' , $prefixeTable . 'meta_cat'); |
---|
20 | |
---|
21 | add_event_handler('get_admin_plugin_menu_links', 'meta_admin_menu'); |
---|
22 | function meta_admin_menu($menu) |
---|
23 | { |
---|
24 | array_push($menu, array( |
---|
25 | 'NAME' => 'Meta', |
---|
26 | 'URL' => get_admin_plugin_menu_link(meta_PATH . 'admin/admin.php'))); |
---|
27 | return $menu; |
---|
28 | } |
---|
29 | |
---|
30 | //Gestion des meta dans le header |
---|
31 | add_event_handler('loc_begin_page_header', 'Change_Meta',20 ); |
---|
32 | add_event_handler('loc_end_page_header', 'add_meta',56 ); |
---|
33 | add_event_handler('loc_end_page_header', 'add_metacat',61 ); |
---|
34 | add_event_handler('loc_end_page_header', 'add_metaimg',71 ); |
---|
35 | add_event_handler('loc_after_page_header', 'set_meta_back'); |
---|
36 | |
---|
37 | function Change_Meta() |
---|
38 | { |
---|
39 | global $template; |
---|
40 | $template->set_prefilter('header', 'upmata'); |
---|
41 | } |
---|
42 | |
---|
43 | function upmata ($content, &$smarty) |
---|
44 | { |
---|
45 | $search = '#<meta name="description" content=".*?">#'; |
---|
46 | |
---|
47 | $replacement = '<meta name="description" content="{$PLUG_META}">'; |
---|
48 | |
---|
49 | return preg_replace($search, $replacement, $content); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | function add_meta() |
---|
55 | { |
---|
56 | global $template, $page, $meta_infos; |
---|
57 | $meta_infos = array(); |
---|
58 | $meta_infos['author'] = $template->get_template_vars('INFO_AUTHOR'); |
---|
59 | $meta_infos['related_tags'] = $template->get_template_vars('related_tags'); |
---|
60 | $meta_infos['info'] = $template->get_template_vars('INFO_FILE'); |
---|
61 | $meta_infos['title'] = $template->get_template_vars('PAGE_TITLE'); |
---|
62 | |
---|
63 | $query = ' |
---|
64 | select id,metaname,metaval |
---|
65 | FROM ' . meta_TABLE . ' |
---|
66 | WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\') |
---|
67 | ;'; |
---|
68 | $result = pwg_query($query); |
---|
69 | $meta = array(); |
---|
70 | while ($row = mysql_fetch_assoc($result)) |
---|
71 | { |
---|
72 | $meta[$row['metaname']] = $row['metaval']; |
---|
73 | } |
---|
74 | |
---|
75 | // Authors |
---|
76 | if (!empty($meta_infos['author']) and !empty($meta['author'])) |
---|
77 | { |
---|
78 | $template->assign('INFO_AUTHOR', $meta_infos['author'] . ' - ' . $meta['author']); |
---|
79 | } |
---|
80 | elseif (!empty($meta['author'])) |
---|
81 | { |
---|
82 | $template->assign('INFO_AUTHOR', $meta['author']); |
---|
83 | } |
---|
84 | |
---|
85 | // Keywords |
---|
86 | if (!empty($meta['keywords'])) |
---|
87 | { |
---|
88 | $template->append('related_tags', array('name' => $meta['keywords'])); |
---|
89 | } |
---|
90 | |
---|
91 | // Description |
---|
92 | if (!empty($meta_infos['title']) and !empty($meta_infos['info']) and !empty($meta['Description'])) |
---|
93 | { |
---|
94 | $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta_infos['info'] . ', ' . $meta['Description']); |
---|
95 | } |
---|
96 | elseif (!empty($meta_infos['title']) and !empty($meta['Description'])) |
---|
97 | { |
---|
98 | $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta['Description']); |
---|
99 | } |
---|
100 | elseif (!empty($meta['Description'])) |
---|
101 | { |
---|
102 | $template->assign('PLUG_META', $meta['Description']); |
---|
103 | } |
---|
104 | |
---|
105 | // Robots |
---|
106 | if (!empty($meta['robots'])) |
---|
107 | { |
---|
108 | $template->append('head_elements', '<meta name="robots" content="'.$meta['robots'].'">'); |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | function add_metacat() |
---|
113 | { |
---|
114 | global $template, $page, $meta_infos; |
---|
115 | |
---|
116 | //meta categories |
---|
117 | if ( !empty($page['category']['id']) ) |
---|
118 | { |
---|
119 | $query = ' |
---|
120 | select id,metaKeycat |
---|
121 | FROM ' . meta_cat_TABLE . ' |
---|
122 | WHERE id = \''.$page['category']['id'].'\' |
---|
123 | ;'; |
---|
124 | $result = pwg_query($query); |
---|
125 | $row = mysql_fetch_array($result); |
---|
126 | if (!empty($row['metaKeycat'])) |
---|
127 | { |
---|
128 | $template->append('related_tags', array('name' => $row['metaKeycat'])); |
---|
129 | } |
---|
130 | |
---|
131 | $query = ' |
---|
132 | select id,metadescat |
---|
133 | FROM ' . meta_cat_TABLE . ' |
---|
134 | WHERE id = \''.$page['category']['id'].'\' |
---|
135 | ;'; |
---|
136 | $result = pwg_query($query); |
---|
137 | $row = mysql_fetch_array($result); |
---|
138 | if (!empty($row['metadescat'])) |
---|
139 | { |
---|
140 | $template->assign('PLUG_META', $row['metadescat']); |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | function add_metaimg() |
---|
146 | { |
---|
147 | global $template, $page, $meta_infos; |
---|
148 | |
---|
149 | //meta images |
---|
150 | if ( !empty($page['image_id']) ) |
---|
151 | { |
---|
152 | $query = ' |
---|
153 | select id,metaKeyimg |
---|
154 | FROM ' . meta_img_TABLE . ' |
---|
155 | WHERE id = \''.$page['image_id'].'\' |
---|
156 | ;'; |
---|
157 | $result = pwg_query($query); |
---|
158 | $row = mysql_fetch_array($result); |
---|
159 | if (!empty($row['metaKeyimg'])) |
---|
160 | { |
---|
161 | $template->append('related_tags', array('name' => $row['metaKeyimg'])); |
---|
162 | } |
---|
163 | |
---|
164 | $query = ' |
---|
165 | select id,metadesimg |
---|
166 | FROM ' . meta_img_TABLE . ' |
---|
167 | WHERE id = \''.$page['image_id'].'\' |
---|
168 | ;'; |
---|
169 | $result = pwg_query($query); |
---|
170 | $row = mysql_fetch_array($result); |
---|
171 | if (!empty($row['metadesimg'])) |
---|
172 | { |
---|
173 | $template->assign('PLUG_META', $row['metadesimg']); |
---|
174 | } |
---|
175 | |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | function set_meta_back() |
---|
181 | { |
---|
182 | global $template, $meta_infos; |
---|
183 | |
---|
184 | $template->assign |
---|
185 | (array |
---|
186 | ( |
---|
187 | 'INFO_AUTHOR' => $meta_infos['author'], |
---|
188 | 'related_tags' => $meta_infos['related_tags'], |
---|
189 | 'INFO_FILE' => $meta_infos['info'], |
---|
190 | ) |
---|
191 | ); |
---|
192 | } |
---|
193 | ?> |
---|