iGtrends2

The Google Trends Widget iGtrends2 class.

Defined (1)

The class is defined in the following location(s).

/igtrends2.php  
  1. class iGtrends2 extends WP_Widget { 
  2. function __construct() { 
  3. parent::__construct( 
  4. // Base ID of your widget 
  5. 'iGtrends2',  
  6. // Widget name will appear in UI 
  7. __('Google Trends Widget', 'iGtrends2_domain'),  
  8. // Widget description 
  9. array( 'description' => __( 'Show latest google trends item linked to search page', 'iGtrends2_domain' ), )  
  10. ); 
  11. // Creating widget front-end 
  12. // This is where the action happens 
  13. public function clean($str, $replace=array(), $delimiter='-') { 
  14. if( !empty($replace) ) { 
  15. $str = str_replace((array)$replace, ' ', $str); 
  16.  
  17. $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str); 
  18. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean); 
  19. $clean = strtolower(trim($clean, '-')); 
  20. $clean = trim($clean); 
  21. $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean); 
  22.  
  23. return trim($clean); 
  24. public function widget( $args, $instance ) { 
  25. $title = apply_filters( 'widget_title', $instance['title'] ); 
  26. $num = $instance['country']; 
  27. if(($num=='') || ($num=='0')) { 
  28. $num = 'p1'; 
  29. $limit = $instance['limit']; 
  30. $cache = $instance['cache']; 
  31. // before and after widget arguments are defined by themes 
  32. echo $args['before_widget']; 
  33. if ( ! empty( $title ) ) 
  34. echo $args['before_title'] . $title . $args['after_title']; 
  35. // This is where you run the code and display the output 
  36. $url = 'http://www.google.com/trends/hottrends/atom/feed?pn='.$num; 
  37. if ( false === ( $data = get_transient( $num ) ) ) { 
  38. if(@simplexml_load_file($url)) { 
  39. $data = json_decode(json_encode(simplexml_load_file($url))); 
  40. set_transient( $num, $data, $cache * HOUR_IN_SECONDS ); 
  41. if($data) { 
  42. echo '<ul>'; 
  43. $i = 0; 
  44. foreach($data->channel->item as $hot) { 
  45. $i++; 
  46. $stop = $limit + 1; 
  47. if($i==$stop) { break; } 
  48. echo '<li><a href="'.get_bloginfo('url').'/?s='.$hot->title.'">'.$hot->title.'</a></li>'; 
  49. echo '</ul>'; 
  50. echo $args['after_widget']; 
  51. // Widget Backend  
  52. public function form( $instance ) { 
  53. if ( isset( $instance[ 'title' ] ) ) { 
  54. $title = $instance[ 'title' ]; 
  55. else { 
  56. $title = __( 'Trending Now', 'iGtrends2_domain' ); 
  57. if ( isset( $instance[ 'limit' ] ) ) { 
  58. $limit = $instance[ 'limit' ]; 
  59. else { 
  60. $limit = 10; 
  61. if ( isset( $instance[ 'cache' ] ) ) { 
  62. $cache = $instance[ 'cache' ]; 
  63. else { 
  64. $cache = 6; 
  65. // Widget admin form 
  66. ?> 
  67. <p> 
  68. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>  
  69. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> 
  70. </p> 
  71. <p> 
  72. <label for="<?php echo $this->get_field_id( 'country' ); ?>"><?php _e( 'Country:' ); ?></label>  
  73. <select class="widefat" id="<?php echo $this->get_field_id( 'country' ); ?>" name="<?php echo $this->get_field_name( 'country' ); ?>"> 
  74. <option value="0">— Select Country —</option> 
  75. <option value="p30" <?php if($instance['country']=='p30') { echo 'selected="selected"'; } ?> >Argentina</option> 
  76. <option value="p8" <?php if($instance['country']=='p8') { echo 'selected="selected"'; } ?> >Australia</option> 
  77. <option value="p44" <?php if($instance['country']=='p44') { echo 'selected="selected"'; } ?> >Austria</option> 
  78. <option value="p41" <?php if($instance['country']=='p41') { echo 'selected="selected"'; } ?> >Belgium</option> 
  79. <option value="p18" <?php if($instance['country']=='p18') { echo 'selected="selected"'; } ?> >Brazil</option> 
  80. <option value="p13" <?php if($instance['country']=='p13') { echo 'selected="selected"'; } ?> >Canada</option> 
  81. <option value="p38" <?php if($instance['country']=='p38') { echo 'selected="selected"'; } ?> >Chile</option> 
  82. <option value="p32" <?php if($instance['country']=='p32') { echo 'selected="selected"'; } ?> >Colombia</option> 
  83. <option value="p43" <?php if($instance['country']=='p43') { echo 'selected="selected"'; } ?> >Czech Republic</option> 
  84. <option value="p49" <?php if($instance['country']=='p49') { echo 'selected="selected"'; } ?> >Denmark</option> 
  85. <option value="p29" <?php if($instance['country']=='p29') { echo 'selected="selected"'; } ?> >Egypt</option> 
  86. <option value="p50" <?php if($instance['country']=='p50') { echo 'selected="selected"'; } ?> >Finland</option> 
  87. <option value="p16" <?php if($instance['country']=='p16') { echo 'selected="selected"'; } ?> >France</option> 
  88. <option value="p15" <?php if($instance['country']=='p15') { echo 'selected="selected"'; } ?> >Germany</option> 
  89. <option value="p48" <?php if($instance['country']=='p48') { echo 'selected="selected"'; } ?> >Greece</option> 
  90. <option value="p10" <?php if($instance['country']=='p10') { echo 'selected="selected"'; } ?> >Hong Kong</option> 
  91. <option value="p45" <?php if($instance['country']=='p45') { echo 'selected="selected"'; } ?> >Hungary</option> 
  92. <option value="p3" <?php if($instance['country']=='p3') { echo 'selected="selected"'; } ?> >India</option> 
  93. <option value="p19" <?php if($instance['country']=='p19') { echo 'selected="selected"'; } ?> >Indonesia</option> 
  94. <option value="p6" <?php if($instance['country']=='p6') { echo 'selected="selected"'; } ?> >Israel</option> 
  95. <option value="p27" <?php if($instance['country']=='p27') { echo 'selected="selected"'; } ?> >Italy</option> 
  96. <option value="p4" <?php if($instance['country']=='p4') { echo 'selected="selected"'; } ?> >Japan</option> 
  97. <option value="p37" <?php if($instance['country']=='p37') { echo 'selected="selected"'; } ?> >Kenya</option> 
  98. <option value="p34" <?php if($instance['country']=='p34') { echo 'selected="selected"'; } ?> >Malaysia</option> 
  99. <option value="p21" <?php if($instance['country']=='p21') { echo 'selected="selected"'; } ?> >Mexico</option> 
  100. <option value="p17" <?php if($instance['country']=='p17') { echo 'selected="selected"'; } ?> >Netherlands</option> 
  101. <option value="p52" <?php if($instance['country']=='p52') { echo 'selected="selected"'; } ?> >Nigeria</option> 
  102. <option value="p51" <?php if($instance['country']=='p51') { echo 'selected="selected"'; } ?> >Norway</option> 
  103. <option value="p25" <?php if($instance['country']=='p25') { echo 'selected="selected"'; } ?> >Philippines</option> 
  104. <option value="p31" <?php if($instance['country']=='p31') { echo 'selected="selected"'; } ?> >Poland</option> 
  105. <option value="p47" <?php if($instance['country']=='p47') { echo 'selected="selected"'; } ?> >Portugal</option> 
  106. <option value="p39" <?php if($instance['country']=='p39') { echo 'selected="selected"'; } ?> >Romania</option> 
  107. <option value="p14" <?php if($instance['country']=='p14') { echo 'selected="selected"'; } ?> >Russia</option> 
  108. <option value="p36" <?php if($instance['country']=='p36') { echo 'selected="selected"'; } ?> >Saudi Arabia</option> 
  109. <option value="p5" <?php if($instance['country']=='p5') { echo 'selected="selected"'; } ?> >Singapore</option> 
  110. <option value="p40" <?php if($instance['country']=='p40') { echo 'selected="selected"'; } ?> >South Africa</option> 
  111. <option value="p23" <?php if($instance['country']=='p23') { echo 'selected="selected"'; } ?> >South Korea</option> 
  112. <option value="p26" <?php if($instance['country']=='p26') { echo 'selected="selected"'; } ?> >Spain</option> 
  113. <option value="p42" <?php if($instance['country']=='p42') { echo 'selected="selected"'; } ?> >Sweden</option> 
  114. <option value="p46" <?php if($instance['country']=='p46') { echo 'selected="selected"'; } ?> >Switzerland</option> 
  115. <option value="p12" <?php if($instance['country']=='p12') { echo 'selected="selected"'; } ?> >Taiwan</option> 
  116. <option value="p33" <?php if($instance['country']=='p33') { echo 'selected="selected"'; } ?> >Thailand</option> 
  117. <option value="p24" <?php if($instance['country']=='p24') { echo 'selected="selected"'; } ?> >Turkey</option> 
  118. <option value="p35" <?php if($instance['country']=='p35') { echo 'selected="selected"'; } ?> >Ukraine</option> 
  119. <option value="p9" <?php if($instance['country']=='p9') { echo 'selected="selected"'; } ?> >United Kingdom</option> 
  120. <option value="p1" <?php if($instance['country']=='p1') { echo 'selected="selected"'; } ?> >United States</option> 
  121. <option value="p28" <?php if($instance['country']=='p28') { echo 'selected="selected"'; } ?> >Vietnam</option> 
  122. </select> 
  123. </p> 
  124. <p> 
  125. <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Number of items to show:' ); ?></label>  
  126. <input class="widefat" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo esc_attr( $limit ); ?>" /> 
  127. </p> 
  128. <p> 
  129. <label for="<?php echo $this->get_field_id( 'cache' ); ?>"><?php _e( 'Cache:' ); ?></label>  
  130. <input id="<?php echo $this->get_field_id( 'cache' ); ?>" name="<?php echo $this->get_field_name( 'cache' ); ?>" type="text" value="<?php echo esc_attr( $cache ); ?>" /> hours 
  131. </p> 
  132. <?php  
  133. // Updating widget replacing old instances with new 
  134. public function update( $new_instance, $old_instance ) { 
  135. $instance = array(); 
  136. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; 
  137. $instance['country'] = ( ! empty( $new_instance['country'] ) ) ? strip_tags( $new_instance['country'] ) : ''; 
  138. $instance['limit'] = ( ! empty( $new_instance['limit'] ) ) ? strip_tags( $new_instance['limit'] ) : ''; 
  139. $instance['cache'] = ( ! empty( $new_instance['cache'] ) ) ? strip_tags( $new_instance['cache'] ) : ''; 
  140. return $instance;