Plugin Directory


Ignore:
Timestamp:
11/05/2009 05:14:02 PM (16 years ago)
Author:
Otto42
Message:

Version 2.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • php-code-widget/trunk/execphp.php

    r126299 r170692  
    22/*
    33Plugin Name: Executable PHP widget
     4Plugin URI: http://wordpress.org/extend/plugins/php-code-widget/
    45Description: Like the Text widget, but it will take PHP code as well. Heavily derived from the Text widget code in WordPress.
    56Author: Otto
    6 Version: 1.2
     7Version: 2.0
    78Author URI: http://ottodestruct.com
    8 Plugin URI: http://wordpress.org/extend/plugins/php-code-widget/
    9 */
    10 /*
     9
     10    Copyright 2009  Samuel Wood  (email : otto@ottodestruct.com)
     11
    1112    This program is free software; you can redistribute it and/or modify
    1213    it under the terms of the GNU General Public License version 2,
    13     as published by the Free Software Foundation.
     14    as published by the Free Software Foundation.
     15   
     16    You may NOT assume that you can use any other version of the GPL.
    1417
    1518    This program is distributed in the hope that it will be useful,
     
    1720    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1821    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   
    1926*/
    2027
    21 function widget_execphp($args, $widget_args = 1) {
    22     extract( $args, EXTR_SKIP );
    23     if ( is_numeric($widget_args) )
    24         $widget_args = array( 'number' => $widget_args );
    25     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    26     extract( $widget_args, EXTR_SKIP );
     28class PHP_Code_Widget extends WP_Widget {
    2729
    28     $options = get_option('widget_execphp');
    29     if ( !isset($options[$number]) )
    30         return;
     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    }
    3135
    32     $title = $options[$number]['title'];
    33     $text = apply_filters( 'widget_execphp', $options[$number]['text'] );
     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 wpautop($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']);
    3467?>
    35         <?php echo $before_widget; ?>
    36             <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
    37             <div class="execphpwidget"><?php eval('?>'.$text); ?></div>
    38         <?php echo $after_widget; ?>
     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>
    3974<?php
     75    }
    4076}
    4177
    42 function widget_execphp_control($widget_args) {
    43     global $wp_registered_widgets;
    44     static $updated = false;
    45 
    46     if ( is_numeric($widget_args) )
    47         $widget_args = array( 'number' => $widget_args );
    48     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    49     extract( $widget_args, EXTR_SKIP );
    50 
    51     $options = get_option('widget_execphp');
    52     if ( !is_array($options) )
    53         $options = array();
    54 
    55     if ( !$updated && !empty($_POST['sidebar']) ) {
    56         $sidebar = (string) $_POST['sidebar'];
    57 
    58         $sidebars_widgets = wp_get_sidebars_widgets();
    59         if ( isset($sidebars_widgets[$sidebar]) )
    60             $this_sidebar =& $sidebars_widgets[$sidebar];
    61         else
    62             $this_sidebar = array();
    63 
    64         foreach ( $this_sidebar as $_widget_id ) {
    65             if ( 'widget_execphp' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
    66                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
    67                 if ( !in_array( "execphp-$widget_number", $_POST['widget-id'] ) ) unset($options[$widget_number]);
    68             }
    69         }
    70 
    71         foreach ( (array) $_POST['widget-execphp'] as $widget_number => $widget_text ) {
    72             $title = strip_tags(stripslashes($widget_text['title']));
    73             if ( current_user_can('unfiltered_html') )
    74                 $text = stripslashes( $widget_text['text'] );
    75             else
    76                 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
    77             $options[$widget_number] = compact( 'title', 'text' );
    78         }
    79 
    80         update_option('widget_execphp', $options);
    81         $updated = true;
    82     }
    83 
    84     if ( -1 == $number ) {
    85         $title = '';
    86         $text = '';
    87         $number = '%i%';
    88     } else {
    89         $title = attribute_escape($options[$number]['title']);
    90         $text = format_to_edit($options[$number]['text']);
    91     }
    92 ?>
    93         <p>
    94             <input class="widefat" id="execphp-title-<?php echo $number; ?>" name="widget-execphp[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
    95             <p>PHP Code (MUST be enclosed in &lt;?php and ?&gt; tags!):</p>
    96             <textarea class="widefat" rows="16" cols="20" id="execphp-text-<?php echo $number; ?>" name="widget-execphp[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
    97             <input type="hidden" id="execphp-submit-<?php echo $number; ?>" name="execphp-submit-<?php echo $number; ?>" value="1" />
    98         </p>
    99 <?php
    100 }
    101 
    102 function widget_execphp_register() {
    103 
    104     // Check for the required API functions
    105     if ( !function_exists('wp_register_sidebar_widget') || !function_exists('wp_register_widget_control') )
    106         return;
    107 
    108     if ( !$options = get_option('widget_execphp') )
    109         $options = array();
    110     $widget_ops = array('classname' => 'widget_execphp', 'description' => __('Arbitrary text, HTML, or PHP code'));
    111     $control_ops = array('width' => 460, 'height' => 350, 'id_base' => 'execphp');
    112     $name = __('PHP Code');
    113 
    114     $id = false;
    115     foreach ( array_keys($options) as $o ) {
    116         // Old widgets can have null values for some reason
    117         if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
    118             continue;
    119         $id = "execphp-$o"; // Never never never translate an id
    120         wp_register_sidebar_widget($id, $name, 'widget_execphp', $widget_ops, array( 'number' => $o ));
    121         wp_register_widget_control($id, $name, 'widget_execphp_control', $control_ops, array( 'number' => $o ));
    122     }
    123    
    124     // If there are none, we register the widget's existance with a generic template
    125     if ( !$id ) {
    126         wp_register_sidebar_widget( 'execphp-1', $name, 'widget_execphp', $widget_ops, array( 'number' => -1 ) );
    127         wp_register_widget_control( 'execphp-1', $name, 'widget_execphp_control', $control_ops, array( 'number' => -1 ) );
    128     }
    129    
    130 }
    131 
    132 add_action( 'widgets_init', 'widget_execphp_register' );
    133 
    134 ?>
     78add_action('widgets_init', create_function('', 'return register_widget("PHP_Code_Widget");'));
Note: See TracChangeset for help on using the changeset viewer.