PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/transposh-translation-filter-for-wordpress/wp/transposh_widget.php

https://bitbucket.org/lgorence/quickpress
PHP | 410 lines | 236 code | 63 blank | 111 comment | 57 complexity | d62be38de140e493d7bfe9bda917c370 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. * Transposh v0.8.3
  4. * http://transposh.org/
  5. *
  6. * Copyright 2012, Team Transposh
  7. * Licensed under the GPL Version 2 or higher.
  8. * http://transposh.org/license
  9. *
  10. * Date: Mon, 28 May 2012 14:38:35 +0300
  11. */
  12. /*
  13. * Provides the sidebar widget instance for selecting a language and switching between edit/view
  14. * mode.
  15. */
  16. //Define subwidget files prefix
  17. define('TRANSPOSH_WIDGET_PREFIX', 'tpw_');
  18. /**
  19. * Class for subwidgets to inherit from
  20. */
  21. class transposh_base_widget {
  22. /**
  23. * Function that performs the actual subwidget rendering
  24. */
  25. static function tp_widget_do() {
  26. echo "you should override this function in your widget";
  27. }
  28. /**
  29. * Attempts inclusion of css needed for the subwidget
  30. * @param string $file
  31. * @param string $plugin_dir
  32. * @param string $plugin_url
  33. */
  34. static function tp_widget_css($file, $plugin_dir, $plugin_url) {
  35. $basefile = substr($file, 0, -4);
  36. $widget_css = TRANSPOSH_DIR_WIDGETS . '/' . $basefile . ".css";
  37. if (file_exists($plugin_dir . $widget_css)) {
  38. wp_enqueue_style($basefile, $plugin_url . '/' . $widget_css, '', TRANSPOSH_PLUGIN_VER);
  39. }
  40. }
  41. /**
  42. * Attempts inclusion of javascript needed for the subwidget
  43. * @param string $file
  44. * @param string $plugin_dir
  45. * @param string $plugin_url
  46. */
  47. static function tp_widget_js($file, $plugin_dir, $plugin_url) {
  48. $basefile = substr($file, 0, -4);
  49. $widget_js = TRANSPOSH_DIR_WIDGETS . '/' . $basefile . ".js";
  50. if (file_exists($plugin_dir . $widget_js)) {
  51. wp_enqueue_script('transposh_widget', $plugin_url . '/' . $widget_js, '', TRANSPOSH_PLUGIN_VER);
  52. }
  53. }
  54. }
  55. // END class
  56. //class that reperesent the complete widget
  57. class transposh_plugin_widget extends WP_Widget {
  58. /** @var transposh_plugin Container class */
  59. private $transposh;
  60. /** @staticvar boolean Contains the fact that this is our first run */
  61. static $first_init = true;
  62. /** @staticvar int Counts call to the widget do to generate unique IDs */
  63. static $draw_calls = '';
  64. function transposh_plugin_widget() {
  65. // We get the transposh details from the global variable
  66. $this->transposh = &$GLOBALS['my_transposh_plugin'];
  67. // Widget control defenitions
  68. $widget_ops = array('classname' => 'widget_transposh', 'description' => __('Transposh language selection widget', TRANSPOSH_TEXT_DOMAIN));
  69. $control_ops = array('width' => 200, 'height' => 300);
  70. $this->WP_Widget('transposh', __('Transposh'), $widget_ops, $control_ops);
  71. add_action('widgets_init', create_function('', 'register_widget("transposh_plugin_widget");'));
  72. // We only need to add those actions once, makes life simpler
  73. if (is_active_widget(false, false, $this->id_base) && self::$first_init) {
  74. self::$first_init = false;
  75. add_action('wp_print_styles', array(&$this, 'add_transposh_widget_css'));
  76. add_action('wp_print_scripts', array(&$this, 'add_transposh_widget_js'));
  77. }
  78. }
  79. /**
  80. * Saves the widgets settings. (override of wp_widget)
  81. */
  82. function update($new_instance, $old_instance) {
  83. $instance = $old_instance;
  84. $instance['title'] = strip_tags(stripslashes($new_instance['title']));
  85. $instance['widget_file'] = strip_tags(stripslashes($new_instance['widget_file']));
  86. return $instance;
  87. }
  88. /**
  89. * Creates the edit form for the widget. (override of wp_widget)
  90. *
  91. */
  92. function form($instance) {
  93. // Defaults
  94. /* TRANSLATORS: this will be the default widget title */
  95. $instance = wp_parse_args((array) $instance, array('title' => __('Translation', TRANSPOSH_TEXT_DOMAIN)));
  96. // Output the options - title first
  97. $title = htmlspecialchars($instance['title']);
  98. echo '<p><label for="' . $this->get_field_name('title') . '">' . __('Title:', TRANSPOSH_TEXT_DOMAIN) . ' <input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></label></p>';
  99. // Followed by subwisgets selection
  100. $widgets = $this->get_widgets();
  101. echo '<p><label for="' . $this->get_field_name('widget_file') . '">' . __('Style:', TRANSPOSH_TEXT_DOMAIN) .
  102. '<select id="' . $this->get_field_id('widget_file') . '" name="' . $this->get_field_name('widget_file') . '">';
  103. foreach ($widgets as $file => $widget) {
  104. $selected = ($instance['widget_file'] == $file) ? ' selected="selected"' : '';
  105. echo "<option value=\"$file\"$selected>{$widget['Name']}</option>";
  106. }
  107. echo '</select>' .
  108. '</label></p>'
  109. ;
  110. }
  111. /**
  112. * Loads the subwidget class code
  113. */
  114. function load_widget($file) {
  115. $widget_src = $this->transposh->transposh_plugin_dir . TRANSPOSH_DIR_WIDGETS . '/' . $file;
  116. if ($file && file_exists($widget_src)) {
  117. include_once $widget_src;
  118. } else {
  119. $file = 'default/tpw_default.php';
  120. include_once $this->transposh->transposh_plugin_dir . TRANSPOSH_DIR_WIDGETS . '/' . $file;
  121. }
  122. return substr($file, strpos($file, '/') + 1, -4);
  123. }
  124. /**
  125. * Add custom css, i.e. transposh_widget.css, flags now override widget
  126. */
  127. function add_transposh_widget_css() {
  128. // first we discover all active widgets of ours, and aggregate the files
  129. $activewidgets = array();
  130. $settings = $this->get_settings();
  131. foreach ($settings as $key => $value) {
  132. if (is_active_widget(false, $this->id_base . '-' . $key, $this->id_base)) {
  133. $activewidgets[$value['widget_file']] = true;
  134. }
  135. }
  136. // we than load the classes and perform the css queueing
  137. foreach ($activewidgets as $key => $v) {
  138. $class = $this->load_widget($key);
  139. if (class_exists($class)) {
  140. $tmpclass = new $class;
  141. $tmpclass->tp_widget_css($key, $this->transposh->transposh_plugin_dir, $this->transposh->transposh_plugin_url);
  142. }
  143. }
  144. }
  145. /**
  146. * Add custom js, i.e. transposh_widget.js
  147. */
  148. function add_transposh_widget_js() {
  149. $activewidgets = array();
  150. $settings = $this->get_settings();
  151. foreach ($settings as $key => $value) {
  152. if (is_active_widget(false, $this->id_base . '-' . $key, $this->id_base)) {
  153. $activewidgets[$value['widget_file']] = true;
  154. }
  155. }
  156. // we than load the classes and perform the css queueing
  157. foreach ($activewidgets as $key => $v) {
  158. $class = $this->load_widget($key);
  159. if (class_exists($class)) {
  160. $tmpclass = new $class;
  161. $tmpclass->tp_widget_js($key, $this->transposh->transposh_plugin_dir, $this->transposh->transposh_plugin_url);
  162. }
  163. }
  164. }
  165. /**
  166. * Calculate arguments needed by subwidgets
  167. * @param string $clean_page_url
  168. * @return array
  169. */
  170. function create_widget_args($clean_page_url) {
  171. // only calculate urls once even for multiple instances
  172. static $widget_args;
  173. if (is_array($widget_args)) return $widget_args;
  174. $widget_args = array();
  175. $page_url = '';
  176. if (is_404()) {
  177. $clean_page_url = transposh_utils::cleanup_url($this->transposh->home_url, $this->transposh->home_url, true);
  178. }
  179. // loop on the languages
  180. foreach ($this->transposh->options->get_sorted_langs() as $code => $langrecord) {
  181. list ($langname, $language, $flag) = explode(',', $langrecord);
  182. // Only send languages which are viewable or (editable and the user is a translator)
  183. if ($this->transposh->options->is_viewable_language($code) ||
  184. ($this->transposh->options->is_editable_language($code) && $this->transposh->is_translator()) ||
  185. ($this->transposh->options->is_default_language($code))) {
  186. // now we alway do this... maybe cache this to APC/Memcache
  187. if ($this->transposh->options->get_enable_url_translate() && !$this->transposh->options->is_default_language($code)) {
  188. $page_url = transposh_utils::translate_url($clean_page_url, '', $code, array(&$this->transposh->database, 'fetch_translation'));
  189. } else {
  190. $page_url = $clean_page_url;
  191. }
  192. // clean $code in default lanaguge
  193. $page_url = transposh_utils::rewrite_url_lang_param($page_url, $this->transposh->home_url, $this->transposh->enable_permalinks_rewrite, $this->transposh->options->is_default_language($code) ? '' : $code, $this->transposh->edit_mode);
  194. $widget_args[] = array(
  195. 'lang' => $langname,
  196. 'langorig' => $language,
  197. 'flag' => $flag,
  198. 'isocode' => $code,
  199. 'url' => $page_url,
  200. 'active' => ($this->transposh->target_language == $code));
  201. }
  202. }
  203. return $widget_args;
  204. }
  205. /**
  206. * Creates the widget html
  207. * @param array $args Contains such as $before_widget, $after_widget, $before_title, $after_title, etc
  208. */
  209. function widget($args, $instance) {
  210. // extract args given by wordpress
  211. extract($args);
  212. // we load the class needed and get its base name for later
  213. $class = $this->load_widget($instance['widget_file']);
  214. if (!class_exists($class)) {
  215. echo __('Transposh subwidget was not loaded correctly', TRANSPOSH_TEXT_DOMAIN) . ": $class";
  216. }
  217. $clean_page = $this->transposh->get_clean_url();
  218. $widget_args = $this->create_widget_args($clean_page);
  219. // at this point the widget args are ready
  220. // widget default title
  221. //echo $before_widget . $before_title . __('Translation', TRANSPOSH_TEXT_DOMAIN) . $after_title; - hmm? po/mo?
  222. echo $before_widget;
  223. if ($instance['title']) {
  224. /* TRANSLATORS: no need to translate this string */
  225. echo $before_title . __($instance['title'], TRANSPOSH_TEXT_DOMAIN) . $after_title;
  226. }
  227. // actually run the external widget code
  228. //if (version_compare(PHP_VERSION, '5.3.0','gt')) { (for the future)
  229. // $class::tp_widget_do($widget_args);
  230. //} else {
  231. $tmpclass = new $class;
  232. $tmpclass->tp_widget_do($widget_args);
  233. //}
  234. //at least one language showing - add the edit box if applicable
  235. if (!empty($widget_args)) {
  236. // this is the set default language line
  237. if ($this->transposh->options->get_widget_allow_set_default_language()) {
  238. If ((isset($_COOKIE['TR_LNG']) && $_COOKIE['TR_LNG'] != $this->transposh->target_language) || (!isset($_COOKIE['TR_LNG']) && !$this->transposh->options->is_default_language($this->transposh->target_language))) {
  239. echo '<a id="' . SPAN_PREFIX . 'setdeflang' . self::$draw_calls . '" class="' . SPAN_PREFIX . 'setdeflang' . '" onClick="return false;" href="' . admin_url('admin-ajax.php') . '?action=tp_cookie_bck">' . __('Set as default language', TRANSPOSH_TEXT_DOMAIN) . '</a><br/>';
  240. }
  241. }
  242. // add the edit checkbox only for translators for languages marked as editable
  243. if ($this->transposh->is_editing_permitted()) {
  244. $ref = transposh_utils::rewrite_url_lang_param($_SERVER["REQUEST_URI"], $this->transposh->home_url, $this->transposh->enable_permalinks_rewrite, ($this->transposh->options->is_default_language($this->transposh->target_language) ? "" : $this->transposh->target_language), !$this->transposh->edit_mode);
  245. echo '<input type="checkbox" name="' . EDIT_PARAM . '" value="1" ' .
  246. ($this->transposh->edit_mode ? 'checked="checked" ' : '') .
  247. ' onclick="document.location.href=\'' . $ref . '\';"/>&nbsp;Edit Translation';
  248. }
  249. } else {
  250. //no languages configured - error message
  251. echo '<p>No languages available for display. Check the Transposh settings (Admin).</p>';
  252. }
  253. // Now this is a comment for those wishing to remove our logo (tplogo.png) and link (transposh.org) from the widget
  254. // first - according to the gpl, you may do so - but since the code has changed - please make in available under the gpl
  255. // second - we did invest a lot of time and effort into this, and the link is a way to help us grow and show your appreciation, if it
  256. // upsets you, feel more than free to move this link somewhere else on your page, such as the footer etc.
  257. // third - feel free to write your own widget, the translation core will work
  258. // forth - you can ask for permission, with a good reason, if you contributed to the code - it's a good enough reason :)
  259. // fifth - if you just delete the following line, it means that you have little respect to the whole copyright thing, which as far as we
  260. // understand means that by doing so - you are giving everybody else the right to do the same and use your work without any attribution
  261. // last - you can now remove the logo in exchange to a few percentage of ad and affiliate revenues on your pages, isn't that better?
  262. $plugpath = parse_url($this->transposh->transposh_plugin_url, PHP_URL_PATH);
  263. if (!$this->transposh->options->get_widget_remove_logo()) {
  264. $tagline = esc_attr__('Transposh', TRANSPOSH_TEXT_DOMAIN) . ' - ';
  265. switch (ord(md5($_SERVER['REQUEST_URI'])) % 5) {
  266. case 0:
  267. $tagline .= esc_attr__('translation plugin for wordpress', TRANSPOSH_TEXT_DOMAIN);
  268. break;
  269. case 1:
  270. $tagline .= esc_attr__('wordpress translation plugin', TRANSPOSH_TEXT_DOMAIN);
  271. break;
  272. case 2:
  273. $tagline .= esc_attr__('translate your blog to 60+ languages', TRANSPOSH_TEXT_DOMAIN);
  274. break;
  275. case 3:
  276. $tagline .= esc_attr__('website crowdsourcing translation plugin', TRANSPOSH_TEXT_DOMAIN);
  277. break;
  278. case 4:
  279. $tagline .= esc_attr__('google translate and bing translate plugin for wordpress', TRANSPOSH_TEXT_DOMAIN);
  280. break;
  281. }
  282. $extralang = '';
  283. if ($this->transposh->target_language != 'en') {
  284. $extralang = $this->transposh->target_language . '/';
  285. }
  286. }
  287. echo '<div id="' . SPAN_PREFIX . 'credit' . self::$draw_calls . '">';
  288. if (!$this->transposh->options->get_widget_remove_logo()) {
  289. echo 'by <a href="http://tran' . 'sposh.org/' . $extralang . '"><img height="16" width="16" src="' .
  290. $plugpath . '/img/tplog' . 'o.png" style="padding:1px;border:0px" title="' . $tagline . '" alt="' . $tagline . '"/></a>';
  291. }
  292. echo '</div>';
  293. echo $after_widget;
  294. // increase the number of calls for unique IDs
  295. self::$draw_calls++;
  296. }
  297. /**
  298. * Inspired (and used code) from the get_plugins function of wordpress
  299. */
  300. function get_widgets($widget_folder = '') {
  301. get_plugins();
  302. $tp_widgets = array();
  303. $widget_root = $this->transposh->transposh_plugin_dir . "widgets";
  304. if (!empty($widget_folder)) $widget_root .= $widget_folder;
  305. // Files in wp-content/widgets directory
  306. $widgets_dir = @opendir($widget_root);
  307. $widget_files = array();
  308. if ($widgets_dir) {
  309. while (($file = readdir($widgets_dir) ) !== false) {
  310. if (substr($file, 0, 1) == '.') continue;
  311. if (is_dir($widget_root . '/' . $file)) {
  312. $widgets_subdir = @ opendir($widget_root . '/' . $file);
  313. if ($widgets_subdir) {
  314. while (($subfile = readdir($widgets_subdir) ) !== false) {
  315. if (substr($subfile, 0, 1) == '.') continue;
  316. if (substr($subfile, 0, 4) == TRANSPOSH_WIDGET_PREFIX && substr($subfile, -4) == '.php')
  317. $widget_files[] = "$file/$subfile";
  318. }
  319. }
  320. }
  321. if (substr($file, 0, 4) == TRANSPOSH_WIDGET_PREFIX && substr($file, -4) == '.php')
  322. $widget_files[] = $file;
  323. }
  324. } else {
  325. return $tp_widgets;
  326. }
  327. @closedir($widgets_dir);
  328. @closedir($widgets_subdir);
  329. if (empty($widget_files)) return $tp_widgets;
  330. foreach ($widget_files as $widget_file) {
  331. if (!is_readable("$widget_root/$widget_file")) continue;
  332. $widget_data = get_plugin_data("$widget_root/$widget_file", false, false); //Do not apply markup/translate as it'll be cached.
  333. if (empty($widget_data['Name'])) continue;
  334. $tp_widgets[plugin_basename($widget_file)] = $widget_data;
  335. }
  336. uasort($tp_widgets, create_function('$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );'));
  337. return $tp_widgets;
  338. }
  339. }
  340. /**
  341. * Function provided for old widget include code compatability
  342. * @param array $args Not needed
  343. */
  344. function transposh_widget($args = array(), $instance= array('title' => 'Translation')) {
  345. $GLOBALS['my_transposh_plugin']->widget->widget($args, $instance);
  346. }
  347. ?>