Advertisement
sarahn

WPML Links & Link Categories Widget

Nov 7th, 2012
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. add_action('widgets_init', array('LinkTranslationWidget','register_widget'));
  2.  
  3. class LinkTranslationWidget extends WP_Widget
  4. {
  5.  
  6. function LinkTranslationWidget() {
  7.  
  8. /* Widget settings. */
  9. $widget_ops = array( 'classname' => 'LinkTranslationWidget', 'description' => __('This widget allows you to set different link categories to display based on a language setting.', $this->plugin_name) );
  10.  
  11. /* Widget control settings. */
  12.  
  13. /* Create the widget. */
  14. $this->WP_Widget( 'LinkTranslationWidget', __('WPML Links'), $widget_ops );
  15.  
  16. }
  17.  
  18. function register_widget()
  19. {
  20. register_widget('LinkTranslationWidget');
  21. }
  22.  
  23. function widget( $args, $instance ) {
  24.  
  25. global $sitepress;
  26.  
  27. if(class_exists('WP_Widget_Links')) {
  28.  
  29. $link_widget = new WP_Widget_Links();
  30.  
  31. if(isset($sitepress))
  32. {
  33.  
  34. $lang = $sitepress->get_current_language();
  35. $instance['category'] = $instance[$lang.'_category'];
  36. $link_widget->widget($args, $instance);
  37.  
  38. }
  39.  
  40. }
  41.  
  42. }
  43.  
  44. function update($new_instance, $old_instance) {
  45.  
  46. $new_instance = (array) $new_instance;
  47. $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0);
  48. foreach ( $instance as $field => $val ) {
  49. if ( isset($new_instance[$field]) )
  50. $instance[$field] = 1;
  51. }
  52.  
  53. $langs = $this->GetLangs();
  54. foreach($langs as $lang=>$lang_id)
  55. {
  56. $instance[$lang.'_category'] = intval($new_instance[$lang.'_category']);
  57. }
  58.  
  59. return $instance;
  60.  
  61. }
  62.  
  63. function form($instance) {
  64.  
  65. $settings = $this->GetSettings();
  66. $langs = $this->GetLangs();
  67.  
  68. $instance = wp_parse_args( (array) $instance, $settings );
  69.  
  70. $link_cats = get_terms( 'link_category', array( 'taxonomy' => 'link_category' ) );
  71.  
  72. foreach($langs as $lang=>$lang_id)
  73. {
  74. echo '
  75.  
  76. <br /><label for="'. $this->get_field_id($lang.'_category').'">'. __('Select Link Category') . ' (' .$lang.'): </label><br />';
  77. echo '
  78. <select id="'. $this->get_field_id($lang.'_category').'" name="'. $this->get_field_name($lang.'_category').'">';
  79. foreach ( $link_cats as $link_cat ) {
  80.  
  81. echo '<option value="' . intval($link_cat->term_id) . '" ';
  82. echo ($link_cat->term_id == $instance[$lang.'_category']) ? ' selected="selected"' : '';
  83. echo '>' . $link_cat->name . "</option>
  84.  
  85. \n";
  86. }
  87. echo '</select>
  88.  
  89. ';
  90.  
  91. }
  92. ?>
  93. <br />
  94. <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" />
  95. <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
  96. <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" />
  97. <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
  98. <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
  99. <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
  100. <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
  101. <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label><br />
  102.  
  103. <?php
  104.  
  105. }
  106.  
  107. function GetLangs()
  108. {
  109. global $sitepress_settings;
  110. $langs = $sitepress_settings['default_categories'];
  111. return $langs;
  112. }
  113.  
  114. function GetSettings()
  115. {
  116. $settings = array();
  117.  
  118. $settings['images'] = true;
  119. $settings['name'] = true;
  120. $settings['description'] = false;
  121. $settings['rating'] = false;
  122. $langs = $this->GetLangs();
  123. foreach($langs as $lang=>$lang_id)
  124. {
  125. $settings[$lang.'_category'] = '';
  126. }
  127. return $settings;
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement