metatag_favicons.module 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @file
  4. * Primary hook implementations for Metatag:favicons.
  5. */
  6. /**
  7. * Implements hook_ctools_plugin_api().
  8. */
  9. function metatag_favicons_ctools_plugin_api($owner, $api) {
  10. if ($owner == 'metatag' && $api == 'metatag') {
  11. return array('version' => 1);
  12. }
  13. }
  14. /**
  15. * Implements hook_theme().
  16. */
  17. function metatag_favicons_theme() {
  18. $info['metatag_favicon'] = array(
  19. 'render element' => 'element',
  20. );
  21. $info['metatag_mask_icon'] = array(
  22. 'render element' => 'element',
  23. );
  24. $info['metatag_shortcut_icon'] = array(
  25. 'render element' => 'element',
  26. );
  27. return $info;
  28. }
  29. /**
  30. * Theme callback for a favicon meta tag.
  31. *
  32. * The format is:
  33. * <link rel="[rel]" href="[value]" sizes="[sizes]" />
  34. */
  35. function theme_metatag_favicon($variables) {
  36. $element = &$variables['element'];
  37. $args = array(
  38. '#rel' => 'rel',
  39. '#value' => 'href',
  40. '#sizes' => 'sizes',
  41. '#mask' => 'mask',
  42. );
  43. element_set_attributes($element, $args);
  44. unset($element['#value']);
  45. return theme('html_tag', $variables);
  46. }
  47. /**
  48. * Theme callback for the mask-icon meta tag.
  49. *
  50. * The format is:
  51. * <link rel="mask-icon" href="[value]" sizes="[sizes]" />
  52. */
  53. function theme_metatag_mask_icon($variables) {
  54. $element = &$variables['element'];
  55. $args = array(
  56. '#rel' => 'rel',
  57. '#value' => 'href',
  58. '#color' => 'color',
  59. );
  60. element_set_attributes($element, $args);
  61. unset($element['#value']);
  62. return theme('html_tag', $variables);
  63. }
  64. /**
  65. * Theme callback for a shortcut icon meta tag.
  66. *
  67. * The format is:
  68. * <link rel="[rel]" href="[value]" type="[type]" />
  69. */
  70. function theme_metatag_shortcut_icon($variables) {
  71. $element = &$variables['element'];
  72. // Extract the MIME type.
  73. $element['#type'] = metatag_favicons_get_mime_type($element['#value']);
  74. $args = array(
  75. '#rel' => 'rel',
  76. '#value' => 'href',
  77. '#type' => 'type',
  78. );
  79. element_set_attributes($element, $args);
  80. unset($element['#value']);
  81. return theme('html_tag', $variables);
  82. }
  83. /**
  84. * Helper function to get the theme's favicon URL.
  85. *
  86. * @return string
  87. * The absolute URL to the favicon, empty string if not found.
  88. */
  89. function metatag_favicons_get_theme_favicon() {
  90. $favicon_url = '';
  91. // Is the favicon enabled?
  92. if (theme_get_setting('toggle_favicon')) {
  93. $favicon_url = theme_get_setting('favicon');
  94. }
  95. return $favicon_url;
  96. }
  97. /**
  98. * Returns the correct MIME type for favicons.
  99. *
  100. * @param string $uri
  101. * The URI, or URL, of the favicon to be checked.
  102. *
  103. * @return string
  104. * The MIME type on success, an empty string on failure.
  105. */
  106. function metatag_favicons_get_mime_type($uri) {
  107. // Look for the last period in the URL.
  108. $extension_dot = strrpos($uri, '.');
  109. $type = '';
  110. // URLs must have a file extension in order for this to work.
  111. if ($extension_dot) {
  112. $extension = strtolower(substr($uri, $extension_dot + 1));
  113. // Work out the file's extension.
  114. switch ($extension) {
  115. case 'ico':
  116. $type = 'vnd.microsoft.icon';
  117. break;
  118. // Rename JPEG images as JPG.
  119. case 'jpeg':
  120. $extension = 'jpeg';
  121. // Basic image types.
  122. case 'gif':
  123. case 'jpg':
  124. case 'png':
  125. // Keep the extension as it is.
  126. break;
  127. // This shouldn't happen, only GIF, JPG, ICO or PNG files are supported.
  128. default:
  129. $extension = '';
  130. }
  131. // Only compile the MIME type if a supported extension was found.
  132. if (!empty($extension)) {
  133. $type = 'image/' . $extension;
  134. }
  135. }
  136. return $type;
  137. }