Advertisement
Guest User

Random text/random image widget

a guest
May 21st, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.24 KB | None | 0 0
  1.  
  2.    
  3.    
  4. /*
  5.  **************
  6.  * Random text widget
  7.  **************
  8. */
  9.  
  10. class Random_Text extends WP_Widget {
  11.  
  12.     function Random_Text() {
  13.         $widget_ops = array('classname' => 'custom_text', 'description' => __('Displays one of 5 text blocks at random on page refresh.'));
  14.         $control_ops = array('width' => 400, 'height' => 350);
  15.         $this->WP_Widget('random_text', __('Random Text'), $widget_ops, $control_ops);
  16.     }
  17.  
  18.     function widget( $args, $instance ) {
  19.         extract($args);
  20.         $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
  21.         $text1 = apply_filters( 'widget_text1', $instance['text1'], $instance );
  22.         $text2 = apply_filters( 'widget_text2', $instance['text2'], $instance );
  23.         $text3 = apply_filters( 'widget_text3', $instance['text3'], $instance );
  24.         $text4 = apply_filters( 'widget_text4', $instance['text4'], $instance );
  25.         $text5 = apply_filters( 'widget_text5', $instance['text5'], $instance );
  26.         $text1 = apply_filters( 'widget_text6', $instance['text6'], $instance );
  27.         $text2 = apply_filters( 'widget_text7', $instance['text7'], $instance );
  28.         $text3 = apply_filters( 'widget_text8', $instance['text8'], $instance );
  29.         $text4 = apply_filters( 'widget_text9', $instance['text9'], $instance );
  30.         $text5 = apply_filters( 'widget_text10', $instance['text10'], $instance );
  31.        
  32.         echo $before_widget; ?>
  33.        
  34.         <div class="widgetWrap">
  35.             <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
  36.            
  37.             <script type="text/javascript">//<![CDATA[
  38.                 var txtList = new Array()
  39.                     <?php if (!empty ($text1)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text1) : $text1; ?>";<?php } ?>
  40.                     <?php if (!empty ($text2)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text2) : $text2; ?>";<?php } ?>
  41.                     <?php if (!empty ($text3)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text3) : $text3; ?>";<?php } ?>
  42.                     <?php if (!empty ($text4)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text4) : $text4; ?>";<?php } ?>
  43.                     <?php if (!empty ($text5)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text5) : $text5; ?>";<?php } ?>
  44.                     <?php if (!empty ($text6)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text6) : $text6; ?>";<?php } ?>
  45.                     <?php if (!empty ($text7)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text7) : $text7; ?>";<?php } ?>
  46.                     <?php if (!empty ($text8)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text8) : $text8; ?>";<?php } ?>
  47.                     <?php if (!empty ($text9)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text9) : $text9; ?>";<?php } ?>
  48.                     <?php if (!empty ($text10)) { ?>txtList[txtList.length]="<?php echo $instance['filter'] ? wpautop($text10) : $text10; ?>";<?php } ?>
  49.                    
  50.                 j=parseInt(Math.random()*txtList.length);
  51.                 j=(isNaN(j))?0:j;
  52.                
  53.                 document.write(unescape(txtList[j]));
  54.             //]]></script>
  55.         </div>
  56.        
  57.         <?php
  58.         echo $after_widget;
  59.     }
  60.  
  61.     function update( $new_instance, $old_instance ) {
  62.         $instance = $old_instance;
  63.         $instance['title'] = strip_tags($new_instance['title']);
  64.        
  65.         //text1
  66.         if ( current_user_can('unfiltered_html') )
  67.             $instance['text1'] =  $new_instance['text1'];
  68.         else
  69.             $instance['text1'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text1']) ) ); // wp_filter_post_kses() expects slashed
  70.        
  71.         //text2
  72.         if ( current_user_can('unfiltered_html') )
  73.             $instance['text2'] =  $new_instance['text2'];
  74.         else
  75.             $instance['text2'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text2']) ) ); // wp_filter_post_kses() expects slashed
  76.        
  77.         //text3
  78.         if ( current_user_can('unfiltered_html') )
  79.             $instance['text3'] =  $new_instance['text3'];
  80.         else
  81.             $instance['text3'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text3']) ) ); // wp_filter_post_kses() expects slashed
  82.        
  83.         //text4
  84.         if ( current_user_can('unfiltered_html') )
  85.             $instance['text4'] =  $new_instance['text4'];
  86.         else
  87.             $instance['text4'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text4']) ) ); // wp_filter_post_kses() expects slashed
  88.        
  89.         //text5
  90.         if ( current_user_can('unfiltered_html') )
  91.             $instance['text5'] =  $new_instance['text5'];
  92.         else
  93.             $instance['text5'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text5']) ) ); // wp_filter_post_kses() expects slashed
  94.        
  95.         //text6
  96.         if ( current_user_can('unfiltered_html') )
  97.             $instance['text6'] =  $new_instance['text6'];
  98.         else
  99.             $instance['text6'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text6']) ) ); // wp_filter_post_kses() expects slashed
  100.        
  101.         //text7
  102.         if ( current_user_can('unfiltered_html') )
  103.             $instance['text7'] =  $new_instance['text7'];
  104.         else
  105.             $instance['text7'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text7']) ) ); // wp_filter_post_kses() expects slashed
  106.        
  107.         //text8
  108.         if ( current_user_can('unfiltered_html') )
  109.             $instance['text8'] =  $new_instance['text8'];
  110.         else
  111.             $instance['text8'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text8']) ) ); // wp_filter_post_kses() expects slashed
  112.        
  113.         //text9
  114.         if ( current_user_can('unfiltered_html') )
  115.             $instance['text9'] =  $new_instance['text9'];
  116.         else
  117.             $instance['text9'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text9']) ) ); // wp_filter_post_kses() expects slashed
  118.        
  119.         //text10
  120.         if ( current_user_can('unfiltered_html') )
  121.             $instance['text10'] =  $new_instance['text10'];
  122.         else
  123.             $instance['text10'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text10']) ) ); // wp_filter_post_kses() expects slashed
  124.        
  125.         $instance['filter'] = isset($new_instance['filter']);
  126.         return $instance;
  127.     }
  128.  
  129.     function form( $instance ) {
  130.         $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text1' => '', 'text2' => '', 'text3' => '', 'text4' => '', 'text5' => '',  ) );
  131.         $title = strip_tags($instance['title']);
  132.         $text1 = esc_textarea($instance['text1']);
  133.         $text2 = esc_textarea($instance['text2']);
  134.         $text3 = esc_textarea($instance['text3']);
  135.         $text4 = esc_textarea($instance['text4']);
  136.         $text5 = esc_textarea($instance['text5']);
  137.         $text6 = esc_textarea($instance['text6']);
  138.         $text7 = esc_textarea($instance['text7']);
  139.         $text8 = esc_textarea($instance['text8']);
  140.         $text9 = esc_textarea($instance['text9']);
  141.         $text10 = esc_textarea($instance['text10']);
  142. ?>
  143.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  144.         <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); ?>" /></p>
  145.  
  146.         <p><label for="<?php echo $this->get_field_id('text1'); ?>"><?php _e('Text 1:'); ?></label>
  147.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text1'); ?>" name="<?php echo $this->get_field_name('text1'); ?>"><?php echo $text1; ?></textarea></p>
  148.  
  149.         <p><label for="<?php echo $this->get_field_id('text2'); ?>"><?php _e('Text 2:'); ?></label>
  150.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text2'); ?>" name="<?php echo $this->get_field_name('text2'); ?>"><?php echo $text2; ?></textarea></p>
  151.  
  152.         <p><label for="<?php echo $this->get_field_id('text3'); ?>"><?php _e('Text 3:'); ?></label>
  153.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text3'); ?>" name="<?php echo $this->get_field_name('text3'); ?>"><?php echo $text3; ?></textarea></p>
  154.  
  155.         <p><label for="<?php echo $this->get_field_id('text4'); ?>"><?php _e('Text 4:'); ?></label>
  156.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text4'); ?>" name="<?php echo $this->get_field_name('text4'); ?>"><?php echo $text4; ?></textarea></p>
  157.  
  158.         <p><label for="<?php echo $this->get_field_id('text5'); ?>"><?php _e('Text 5:'); ?></label>
  159.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text5'); ?>" name="<?php echo $this->get_field_name('text5'); ?>"><?php echo $text5; ?></textarea></p>
  160.  
  161.         <p><label for="<?php echo $this->get_field_id('text6'); ?>"><?php _e('Text 6:'); ?></label>
  162.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text6'); ?>" name="<?php echo $this->get_field_name('text6'); ?>"><?php echo $text6; ?></textarea></p>
  163.  
  164.         <p><label for="<?php echo $this->get_field_id('text7'); ?>"><?php _e('Text 7:'); ?></label>
  165.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text7'); ?>" name="<?php echo $this->get_field_name('text7'); ?>"><?php echo $text7; ?></textarea></p>
  166.  
  167.         <p><label for="<?php echo $this->get_field_id('text8'); ?>"><?php _e('Text 8:'); ?></label>
  168.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text8'); ?>" name="<?php echo $this->get_field_name('text8'); ?>"><?php echo $text8; ?></textarea></p>
  169.  
  170.         <p><label for="<?php echo $this->get_field_id('text9'); ?>"><?php _e('Text 9:'); ?></label>
  171.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text9'); ?>" name="<?php echo $this->get_field_name('text9'); ?>"><?php echo $text9; ?></textarea></p>
  172.  
  173.         <p><label for="<?php echo $this->get_field_id('text10'); ?>"><?php _e('Text 10:'); ?></label>
  174.         <textarea class="widefat" rows="3" cols="20" id="<?php echo $this->get_field_id('text10'); ?>" name="<?php echo $this->get_field_name('text10'); ?>"><?php echo $text10; ?></textarea></p>
  175.  
  176.         <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
  177. <?php
  178.     }
  179. }
  180.  
  181. add_action('widgets_init', create_function('', 'return register_widget("Random_Text");'));
  182.    
  183. /*
  184.  **************
  185.  * Random image widget
  186.  **************
  187. */
  188.  
  189. class Random_Image extends WP_Widget {
  190.  
  191.     function Random_Image() {
  192.         $widget_ops = array('classname' => 'random_image', 'description' => __('Displays one of 5 images at random on page refresh.'));
  193.         $control_ops = array('width' => 400, 'height' => 350);
  194.         $this->WP_Widget('random_image', __('Random Image'), $widget_ops, $control_ops);
  195.     }
  196.  
  197.     function widget( $args, $instance ) {
  198.         extract($args);
  199.         $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
  200.         $img1 = apply_filters( 'widget_img1', $instance['img1'], $instance );
  201.         $img2 = apply_filters( 'widget_img2', $instance['img2'], $instance );
  202.         $img3 = apply_filters( 'widget_img3', $instance['img3'], $instance );
  203.         $img4 = apply_filters( 'widget_img4', $instance['img4'], $instance );
  204.         $img5 = apply_filters( 'widget_img5', $instance['img5'], $instance );
  205.        
  206.         echo $before_widget; ?>
  207.        
  208.         <div class="widgetWrap">
  209.             <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
  210.             <script type="text/javascript">//<![CDATA[
  211.                 var j,d="",l="",m="",p="",q="",z="",KW_ARI= new Array()
  212.                     <?php if ( !empty($img1) ) { ?>KW_ARI[KW_ARI.length]='<?php echo $img1 ?>';<?php } ?>
  213.                     <?php if ( !empty($img2) ) { ?>KW_ARI[KW_ARI.length]='<?php echo $img2 ?>';<?php } ?>
  214.                     <?php if ( !empty($img3) ) { ?>KW_ARI[KW_ARI.length]='<?php echo $img3 ?>';<?php } ?>
  215.                     <?php if ( !empty($img4) ) { ?>KW_ARI[KW_ARI.length]='<?php echo $img4 ?>';<?php } ?>
  216.                     <?php if ( !empty($img5) ) { ?>KW_ARI[KW_ARI.length]='<?php echo $img5 ?>';<?php } ?>
  217.                    
  218.                      j=parseInt(Math.random()*KW_ARI.length);
  219.                      j=(isNaN(j))?0:j;
  220.                      if (KW_ARI[j].indexOf('?')==-1) {
  221.                        document.write("<img src='"+KW_ARI[j]+"'>");
  222.                      }
  223.                      else {
  224.                        nvp=KW_ARI[j].substring(KW_ARI[j].indexOf('?')+2).split('&');
  225.                        for(var i=0;i<nvp.length;i++) {
  226.                          sub=nvp[i].split('=');
  227.                              switch(sub[0]) {
  228.                                case 'link':
  229.                              l="<a href='"+unescape(sub[1])+"'>";
  230.                              p="</a>";
  231.                                      break;
  232.                                case 'target':
  233.                              q=" target='"+unescape(sub[1])+"'";
  234.                                      break;
  235.                                default:
  236.                              m+=" "+sub[0]+"='"+unescape(sub[1])+"'";
  237.                                      break;
  238.                          }
  239.                        }
  240.                        z=(l!="")?((q!="")?l.substring(0,l.length-1)+q+">":l):"";
  241.                        z+="<img src='"+KW_ARI[j].substring(0,KW_ARI[j].indexOf('?'))+"'"+m+">"+p;
  242.                      document.write(z);
  243.                      }
  244.             //]]></script>
  245.         </div>
  246.        
  247.         <?php
  248.         echo $after_widget;
  249.     }
  250.  
  251.     function update( $new_instance, $old_instance ) {
  252.         $instance = $old_instance;
  253.         $instance['title'] = strip_tags($new_instance['title']);
  254.         // Images
  255.         $instance['img1'] = strip_tags($new_instance['img1']);
  256.         $instance['img2'] = strip_tags($new_instance['img2']);
  257.         $instance['img3'] = strip_tags($new_instance['img3']);
  258.         $instance['img4'] = strip_tags($new_instance['img4']);
  259.         $instance['img5'] = strip_tags($new_instance['img5']);
  260.        
  261.         return $instance;
  262.     }
  263.  
  264.     function form( $instance ) {
  265.         $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text1' => '', 'text2' => '', 'text3' => '', 'text4' => '', 'text5' => '',  ) );
  266.         $title = strip_tags($instance['title']);
  267.         $img1 = strip_tags($instance['img1']);
  268.         $img2 = strip_tags($instance['img2']);
  269.         $img3 = strip_tags($instance['img3']);
  270.         $img4 = strip_tags($instance['img4']);
  271.         $img5 = strip_tags($instance['img5']);
  272. ?>
  273.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  274.         <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); ?>" /></p>
  275.        
  276.         <p><label for="<?php echo $this->get_field_id('img1'); ?>"><?php _e('Image 1 URL:'); ?></label>
  277.         <input class="widefat" id="<?php echo $this->get_field_id('img1'); ?>" name="<?php echo $this->get_field_name('img1'); ?>" type="text" value="<?php echo esc_attr($img1); ?>" /></p>
  278.        
  279.         <p><label for="<?php echo $this->get_field_id('img2'); ?>"><?php _e('Image 2 URL:'); ?></label>
  280.         <input class="widefat" id="<?php echo $this->get_field_id('img2'); ?>" name="<?php echo $this->get_field_name('img2'); ?>" type="text" value="<?php echo esc_attr($img2); ?>" /></p>
  281.        
  282.         <p><label for="<?php echo $this->get_field_id('img3'); ?>"><?php _e('Image 3 URL:'); ?></label>
  283.         <input class="widefat" id="<?php echo $this->get_field_id('img3'); ?>" name="<?php echo $this->get_field_name('img3'); ?>" type="text" value="<?php echo esc_attr($img3); ?>" /></p>
  284.        
  285.         <p><label for="<?php echo $this->get_field_id('img4'); ?>"><?php _e('Image 4 URL:'); ?></label>
  286.         <input class="widefat" id="<?php echo $this->get_field_id('img4'); ?>" name="<?php echo $this->get_field_name('img4'); ?>" type="text" value="<?php echo esc_attr($img4); ?>" /></p>
  287.        
  288.         <p><label for="<?php echo $this->get_field_id('img5'); ?>"><?php _e('Image 5 URL:'); ?></label>
  289.         <input class="widefat" id="<?php echo $this->get_field_id('img5'); ?>" name="<?php echo $this->get_field_name('img5'); ?>" type="text" value="<?php echo esc_attr($img5); ?>" /></p>
  290. <?php
  291.     }
  292. }
  293.  
  294. add_action('widgets_init', create_function('', 'return register_widget("Random_Image");'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement