PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp/scripts.php

https://gitlab.com/mattyhead/RPB-chessboard
PHP | 186 lines | 97 code | 30 blank | 59 comment | 4 complexity | d1e437556ec2d9e8fa64fc507e62cf74 MD5 | raw file
  1. <?php
  2. /******************************************************************************
  3. * *
  4. * This file is part of RPB Chessboard, a WordPress plugin. *
  5. * Copyright (C) 2013-2015 Yoann Le Montagner <yo35 -at- melix.net> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  19. * *
  20. ******************************************************************************/
  21. /**
  22. * Register the plugin JavaScript scripts.
  23. *
  24. * This class is not constructible. Call the static method `register()`
  25. * to trigger the registration operations (must be called only once).
  26. */
  27. abstract class RPBChessboardScripts
  28. {
  29. public static function register()
  30. {
  31. $ext = self::getJSFileExtension();
  32. // Moment.js (http://momentjs.com/)
  33. wp_register_script('rpbchessboard-momentjs', RPBCHESSBOARD_URL . 'third-party-libs/moment-js/moment' . $ext, false, '2.8.1');
  34. $momentjs = self::localizeJavaScriptLib('rpbchessboard-momentjs', 'third-party-libs/moment-js/locales/%1$s.js', '2.8.1');
  35. // RPBChess
  36. wp_register_script('rpbchessboard-core', RPBCHESSBOARD_URL . 'js/rpbchess' . $ext, false, RPBCHESSBOARD_VERSION);
  37. wp_register_script('rpbchessboard-pgn' , RPBCHESSBOARD_URL . 'js/rpbchess-pgn' . $ext, array(
  38. 'rpbchessboard-core'
  39. ), RPBCHESSBOARD_VERSION);
  40. // Chessboard widget
  41. wp_register_script('rpbchessboard-chessboard', RPBCHESSBOARD_URL . 'js/uichess-chessboard' . $ext, array(
  42. 'rpbchessboard-core',
  43. 'jquery-ui-widget',
  44. 'jquery-ui-selectable'
  45. ), RPBCHESSBOARD_VERSION);
  46. // Chessgame widget
  47. wp_register_script('rpbchessboard-chessgame', RPBCHESSBOARD_URL . 'js/uichess-chessgame' . $ext, array(
  48. 'rpbchessboard-core',
  49. 'rpbchessboard-pgn',
  50. 'rpbchessboard-chessboard',
  51. $momentjs,
  52. 'jquery-ui-widget',
  53. 'jquery-color',
  54. 'jquery-ui-dialog',
  55. 'jquery-ui-resizable'
  56. ), RPBCHESSBOARD_VERSION);
  57. // Plugin specific
  58. wp_register_script('rpbchessboard-backend', RPBCHESSBOARD_URL . 'js/backend' . $ext, array(
  59. 'rpbchessboard-chessboard',
  60. 'jquery-ui-dialog',
  61. 'jquery-ui-accordion',
  62. 'jquery-ui-draggable',
  63. 'jquery-ui-droppable'
  64. ), RPBCHESSBOARD_VERSION);
  65. // Enqueue the scripts.
  66. wp_enqueue_script('rpbchessboard-chessboard');
  67. wp_enqueue_script('rpbchessboard-chessgame' );
  68. // Additional scripts for the backend.
  69. if(is_admin()) {
  70. wp_enqueue_script('jquery-ui-slider');
  71. wp_enqueue_script('jquery-ui-tabs' );
  72. wp_enqueue_script('rpbchessboard-backend');
  73. }
  74. // Inlined scripts
  75. add_action(is_admin() ? 'admin_print_footer_scripts' : 'wp_print_footer_scripts', array(__CLASS__, 'callbackInlinedScripts'));
  76. // TinyMCE editor
  77. add_filter('mce_external_plugins', array(__CLASS__, 'callbackRegisterTinyMCEPlugin'));
  78. add_filter('mce_buttons', array(__CLASS__, 'callbackRegisterTinyMCEButtons'));
  79. // QuickTags editor
  80. add_action('admin_print_footer_scripts', array(__CLASS__, 'callbackRegisterQuickTagsButtons'));
  81. }
  82. public static function callbackInlinedScripts()
  83. {
  84. $model = RPBChessboardHelperLoader::loadModel('Common/Compatibility');
  85. RPBChessboardHelperLoader::printTemplate('Localization', $model);
  86. }
  87. public static function callbackRegisterTinyMCEPlugin($plugins)
  88. {
  89. $plugins['RPBChessboard'] = RPBCHESSBOARD_URL . 'js/tinymce' . self::getJSFileExtension();
  90. return $plugins;
  91. }
  92. public static function callbackRegisterTinyMCEButtons($buttons)
  93. {
  94. array_push($buttons, 'rpb-chessboard');
  95. return $buttons;
  96. }
  97. public static function callbackRegisterQuickTagsButtons()
  98. {
  99. $url = RPBCHESSBOARD_URL . 'js/quicktags' . self::getJSFileExtension();
  100. echo '<script type="text/javascript" src="' . $url . '"></script>';
  101. }
  102. /**
  103. * Return the extension to use for the included JS files.
  104. *
  105. * @return string
  106. */
  107. private static function getJSFileExtension()
  108. {
  109. return WP_DEBUG ? '.js' : '.min.js';
  110. }
  111. /**
  112. * Determine the language code to use to configure a given JavaScript library, and enqueue the required file.
  113. *
  114. * @param string $handle Handle of the file to localize.
  115. * @param string $relativeFilePathTemplate Where the localized files should be searched.
  116. * @param string $version Version the library.
  117. * @return string Handle of the localized file a suitable translation has been found, original handle otherwise.
  118. */
  119. private static function localizeJavaScriptLib($handle, $relativeFilePathTemplate, $version)
  120. {
  121. foreach(self::getBlogLangCodes() as $langCode)
  122. {
  123. // Does the translation script file exist for the current language code?
  124. $relativeFilePath = sprintf($relativeFilePathTemplate, $langCode);
  125. if(!file_exists(RPBCHESSBOARD_ABSPATH . $relativeFilePath)) {
  126. continue;
  127. }
  128. // If it exists, register it, and return a handle pointing to the localization file.
  129. $localizedHandle = $handle . '-localized';
  130. wp_register_script($localizedHandle, RPBCHESSBOARD_URL . $relativeFilePath, array($handle), $version);
  131. return $localizedHandle;
  132. }
  133. // Otherwise, if no translation file exists, return the handle of the original library.
  134. return $handle;
  135. }
  136. /**
  137. * Return an array of language codes that may be relevant for the blog.
  138. *
  139. * @return array
  140. */
  141. private static function getBlogLangCodes()
  142. {
  143. if(!isset(self::$blogLangCodes)) {
  144. $mainLanguage = str_replace('_', '-', strtolower(get_locale()));
  145. self::$blogLangCodes = array($mainLanguage);
  146. if(preg_match('/([a-z]+)\\-([a-z]+)/', $mainLanguage, $m)) {
  147. self::$blogLangCodes[] = $m[1];
  148. }
  149. }
  150. return self::$blogLangCodes;
  151. }
  152. /**
  153. * Blog language codes.
  154. */
  155. private static $blogLangCodes;
  156. }