[3978] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | Plugin Name: meta |
---|
[7468] | 4 | Version: auto |
---|
[3978] | 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'); |
---|
[4174] | 18 | define('meta_img_TABLE' , $prefixeTable . 'meta_img'); |
---|
| 19 | define('meta_cat_TABLE' , $prefixeTable . 'meta_cat'); |
---|
[3978] | 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 |
---|
[7624] | 31 | add_event_handler('loc_begin_page_header', 'Change_Meta',20 ); |
---|
| 32 | add_event_handler('loc_end_page_header', 'add_meta',56 ); |
---|
[7636] | 33 | add_event_handler('loc_end_page_header', 'add_metacat',61 ); |
---|
| 34 | add_event_handler('loc_end_page_header', 'add_metaimg',71 ); |
---|
[7651] | 35 | add_event_handler('loc_after_page_header', 'set_meta_back'); |
---|
[7518] | 36 | |
---|
[7624] | 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 | |
---|
[3978] | 54 | function add_meta() |
---|
| 55 | { |
---|
[7518] | 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'); |
---|
[7624] | 61 | $meta_infos['title'] = $template->get_template_vars('PAGE_TITLE'); |
---|
[3978] | 62 | |
---|
[7518] | 63 | $query = ' |
---|
[3978] | 64 | select id,metaname,metaval |
---|
| 65 | FROM ' . meta_TABLE . ' |
---|
| 66 | WHERE metaname IN (\'author\', \'keywords\', \'Description\', \'robots\') |
---|
| 67 | ;'; |
---|
[7518] | 68 | $result = pwg_query($query); |
---|
| 69 | $meta = array(); |
---|
| 70 | while ($row = mysql_fetch_assoc($result)) |
---|
| 71 | { |
---|
| 72 | $meta[$row['metaname']] = $row['metaval']; |
---|
| 73 | } |
---|
[3978] | 74 | |
---|
[7518] | 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 | { |
---|
[3978] | 82 | $template->assign('INFO_AUTHOR', $meta['author']); |
---|
[7518] | 83 | } |
---|
[3978] | 84 | |
---|
[7518] | 85 | // Keywords |
---|
| 86 | if (!empty($meta['keywords'])) |
---|
| 87 | { |
---|
[3978] | 88 | $template->append('related_tags', array('name' => $meta['keywords'])); |
---|
[7518] | 89 | } |
---|
[3978] | 90 | |
---|
[7518] | 91 | // Description |
---|
[7624] | 92 | if (!empty($meta_infos['title']) and !empty($meta_infos['info']) and !empty($meta['Description'])) |
---|
[7518] | 93 | { |
---|
[7624] | 94 | $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta_infos['info'] . ', ' . $meta['Description']); |
---|
[7518] | 95 | } |
---|
[7624] | 96 | elseif (!empty($meta_infos['title']) and !empty($meta['Description'])) |
---|
| 97 | { |
---|
| 98 | $template->assign('PLUG_META', $meta_infos['title']. ' - ' .$meta['Description']); |
---|
| 99 | } |
---|
[7518] | 100 | elseif (!empty($meta['Description'])) |
---|
| 101 | { |
---|
[7624] | 102 | $template->assign('PLUG_META', $meta['Description']); |
---|
[7518] | 103 | } |
---|
[7624] | 104 | |
---|
[7518] | 105 | // Robots |
---|
| 106 | if (!empty($meta['robots'])) |
---|
| 107 | { |
---|
[3978] | 108 | $template->append('head_elements', '<meta name="robots" content="'.$meta['robots'].'">'); |
---|
[7518] | 109 | } |
---|
[3978] | 110 | |
---|
[7518] | 111 | } |
---|
| 112 | function add_metacat() |
---|
| 113 | { |
---|
| 114 | global $template, $page, $meta_infos; |
---|
| 115 | |
---|
| 116 | //meta categories |
---|
[4174] | 117 | if ( !empty($page['category']['id']) ) |
---|
[3978] | 118 | { |
---|
| 119 | $query = ' |
---|
[4174] | 120 | select id,metaKeycat |
---|
| 121 | FROM ' . meta_cat_TABLE . ' |
---|
[3978] | 122 | WHERE id = \''.$page['category']['id'].'\' |
---|
| 123 | ;'; |
---|
| 124 | $result = pwg_query($query); |
---|
| 125 | $row = mysql_fetch_array($result); |
---|
[4174] | 126 | if (!empty($row['metaKeycat'])) |
---|
[3978] | 127 | { |
---|
[4174] | 128 | $template->append('related_tags', array('name' => $row['metaKeycat'])); |
---|
[3978] | 129 | } |
---|
[7468] | 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 | { |
---|
[7624] | 140 | $template->assign('PLUG_META', $row['metadescat']); |
---|
[7468] | 141 | } |
---|
[3978] | 142 | } |
---|
[7518] | 143 | } |
---|
[4174] | 144 | |
---|
[7518] | 145 | function add_metaimg() |
---|
| 146 | { |
---|
| 147 | global $template, $page, $meta_infos; |
---|
[7624] | 148 | |
---|
[4174] | 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 | } |
---|
[7468] | 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 | { |
---|
[7624] | 173 | $template->assign('PLUG_META', $row['metadesimg']); |
---|
[7468] | 174 | } |
---|
| 175 | |
---|
[4174] | 176 | } |
---|
[3978] | 177 | } |
---|
| 178 | |
---|
[7518] | 179 | |
---|
[3978] | 180 | function set_meta_back() |
---|
| 181 | { |
---|
| 182 | global $template, $meta_infos; |
---|
| 183 | |
---|
[7518] | 184 | $template->assign |
---|
| 185 | (array |
---|
| 186 | ( |
---|
[3978] | 187 | 'INFO_AUTHOR' => $meta_infos['author'], |
---|
| 188 | 'related_tags' => $meta_infos['related_tags'], |
---|
[7518] | 189 | 'INFO_FILE' => $meta_infos['info'], |
---|
[3978] | 190 | ) |
---|
| 191 | ); |
---|
| 192 | } |
---|
| 193 | ?> |
---|