/plugins/system/languagecode/languagecode.php

https://github.com/rvsjoen/joomla-cms · PHP · 141 lines · 95 code · 11 blank · 35 comment · 13 complexity · debef3d1cf447aa8ec24b479421ca557 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage System.languagecode
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Language Code plugin class.
  12. *
  13. * @package Joomla.Plugin
  14. * @subpackage Content.languagecode
  15. */
  16. class plgSystemLanguagecode extends JPlugin
  17. {
  18. /**
  19. * Plugin that change the language code used in the <html /> tag
  20. */
  21. public function onAfterRender()
  22. {
  23. // Use this plugin only in site application
  24. if (JFactory::getApplication()->isSite())
  25. {
  26. // Get the response body
  27. $body = JResponse::getBody();
  28. // Get the current language code
  29. $code = JFactory::getDocument()->getLanguage();
  30. // Get the new code
  31. $new_code = $this->params->get($code);
  32. // Replace the old code by the new code in the <html /> tag
  33. if ($new_code)
  34. {
  35. // Replace the new code in the HTML document
  36. $patterns = array(
  37. chr(1) . '(<html.*\s+xml:lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
  38. chr(1) . '(<html.*\s+lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
  39. );
  40. $replace = array(
  41. '${1}' . strtolower($new_code) . '${3}',
  42. '${1}' . strtolower($new_code) . '${3}'
  43. );
  44. }
  45. else
  46. {
  47. $patterns = array();
  48. $replace = array();
  49. }
  50. // Replace codes in <link hreflang="" /> attributes
  51. preg_match_all(chr(1) . '(<link.*\s+hreflang=")([0-9a-z\-]*)(".*\s+rel="alternate".*/>)' . chr(1) . 'i', $body, $matches);
  52. foreach ($matches[2] as $match)
  53. {
  54. $new_code = $this->params->get(strtolower($match));
  55. if ($new_code)
  56. {
  57. $patterns[] = chr(1) . '(<link.*\s+hreflang=")(' . $match . ')(".*\s+rel="alternate".*/>)' . chr(1) . 'i';
  58. $replace[] = '${1}' . $new_code . '${3}';
  59. }
  60. }
  61. preg_match_all(chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")([0-9A-Za-z\-]*)(".*/>)' . chr(1) . 'i', $body, $matches);
  62. foreach ($matches[2] as $match)
  63. {
  64. $new_code = $this->params->get(strtolower($match));
  65. if ($new_code)
  66. {
  67. $patterns[] = chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")(' . $match . ')(".*/>)' . chr(1) . 'i';
  68. $replace[] = '${1}' . $new_code . '${3}';
  69. }
  70. }
  71. JResponse::setBody(preg_replace($patterns, $replace, $body));
  72. }
  73. }
  74. /**
  75. * @param JForm $form The form to be altered.
  76. * @param array $data The associated data for the form.
  77. *
  78. * @return boolean
  79. * @since 2.5
  80. */
  81. public function onContentPrepareForm($form, $data)
  82. {
  83. // Check we have a form
  84. if (!($form instanceof JForm))
  85. {
  86. $this->_subject->setError('JERROR_NOT_A_FORM');
  87. return false;
  88. }
  89. // Check we are manipulating a valid form.
  90. $app = JFactory::getApplication();
  91. if ($form->getName() != 'com_plugins.plugin'
  92. || isset($data->name) && $data->name != 'plg_system_languagecode'
  93. || empty($data) && !$app->getUserState('plg_system_language_code.edit')
  94. )
  95. {
  96. return true;
  97. }
  98. // Mark the plugin as being edited
  99. $app->setUserState('plg_system_language_code.edit', $data->name == 'plg_system_languagecode');
  100. // Get site languages
  101. $languages = JLanguage::getKnownLanguages(JPATH_SITE);
  102. // Inject fields into the form
  103. foreach ($languages as $tag => $language)
  104. {
  105. $form->load('
  106. <form>
  107. <fields name="params">
  108. <fieldset
  109. name="languagecode"
  110. label="PLG_SYSTEM_LANGUAGECODE_FIELDSET_LABEL"
  111. description="PLG_SYSTEM_LANGUAGECODE_FIELDSET_DESC"
  112. >
  113. <field
  114. name="'.strtolower($tag).'"
  115. type="text"
  116. description="' . htmlspecialchars(JText::sprintf('PLG_SYSTEM_LANGUAGECODE_FIELD_DESC', $language['name']), ENT_COMPAT, 'UTF-8') . '"
  117. translate_description="false"
  118. label="' . $tag . '"
  119. translate_label="false"
  120. size="7"
  121. filter="cmd"
  122. />
  123. </fieldset>
  124. </fields>
  125. </form>
  126. ');
  127. }
  128. return true;
  129. }
  130. }