web/wp-content/plugins/php-code-widget/execphp.php
branchwordpress
changeset 109 03b0d1493584
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Executable PHP widget
       
     4 Plugin URI: http://wordpress.org/extend/plugins/php-code-widget/
       
     5 Description: Like the Text widget, but it will take PHP code as well. Heavily derived from the Text widget code in WordPress.
       
     6 Author: Otto
       
     7 Version: 2.1
       
     8 Author URI: http://ottodestruct.com
       
     9 
       
    10     Copyright 2009  Samuel Wood  (email : otto@ottodestruct.com)
       
    11 
       
    12     This program is free software; you can redistribute it and/or modify
       
    13     it under the terms of the GNU General Public License version 2, 
       
    14     as published by the Free Software Foundation. 
       
    15     
       
    16     You may NOT assume that you can use any other version of the GPL.
       
    17 
       
    18     This program is distributed in the hope that it will be useful,
       
    19     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    21     GNU General Public License for more details.
       
    22     
       
    23     The license for this software can likely be found here: 
       
    24     http://www.gnu.org/licenses/gpl-2.0.html
       
    25     
       
    26 */
       
    27 
       
    28 class PHP_Code_Widget extends WP_Widget {
       
    29 
       
    30 	function PHP_Code_Widget() {
       
    31 		$widget_ops = array('classname' => 'widget_execphp', 'description' => __('Arbitrary text, HTML, or PHP Code'));
       
    32 		$control_ops = array('width' => 400, 'height' => 350);
       
    33 		$this->WP_Widget('execphp', __('PHP Code'), $widget_ops, $control_ops);
       
    34 	}
       
    35 
       
    36 	function widget( $args, $instance ) {
       
    37 		extract($args);
       
    38 		$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
       
    39 		$text = apply_filters( 'widget_execphp', $instance['text'], $instance );
       
    40 		echo $before_widget;
       
    41 		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } 
       
    42 			ob_start();
       
    43 			eval('?>'.$text);
       
    44 			$text = ob_get_contents();
       
    45 			ob_end_clean();
       
    46 			?>			
       
    47 			<div class="execphpwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
       
    48 		<?php
       
    49 		echo $after_widget;
       
    50 	}
       
    51 
       
    52 	function update( $new_instance, $old_instance ) {
       
    53 		$instance = $old_instance;
       
    54 		$instance['title'] = strip_tags($new_instance['title']);
       
    55 		if ( current_user_can('unfiltered_html') )
       
    56 			$instance['text'] =  $new_instance['text'];
       
    57 		else
       
    58 			$instance['text'] = stripslashes( wp_filter_post_kses( $new_instance['text'] ) );
       
    59 		$instance['filter'] = isset($new_instance['filter']);
       
    60 		return $instance;
       
    61 	}
       
    62 
       
    63 	function form( $instance ) {
       
    64 		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
       
    65 		$title = strip_tags($instance['title']);
       
    66 		$text = format_to_edit($instance['text']);
       
    67 ?>
       
    68 		<p><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); ?>" /></p>
       
    70 
       
    71 		<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
       
    72 
       
    73 		<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>
       
    74 <?php
       
    75 	}
       
    76 }
       
    77 
       
    78 add_action('widgets_init', create_function('', 'return register_widget("PHP_Code_Widget");'));