Plugin Directory

Changeset 170692 for php-code-widget


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

Version 2.0

Location:
php-code-widget
Files:
6 added
3 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");'));
  • php-code-widget/trunk/readme.txt

    r161013 r170692  
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=otto%40ottodestruct%2ecom
    44Tags: php, widget, execphp
    5 Requires at least: 2.5
    6 Tested up to: 2.8
    7 Stable tag: 1.2
     5Requires at least: 2.8
     6Tested up to: 2.9
     7Stable tag: 2.0
    88
    99Like the Text widget, but also allows working PHP code to be inserted.
     
    2626== Frequently Asked Questions ==
    2727
    28 = There's some kind of error on line 37! =
     28= There's some kind of error on line 43! =
    2929
    3030That error means that your PHP code is incorrect or otherwise broken.
     
    4242from.
    4343
    44 So, if it says that you have an error on line 27, I assure you, the problem
     44So, if it says that you have an error on line 43, I assure you, the problem
    4545is yours. Please don't email me about that error.
    4646
    4747== Screenshots ==
    4848
    49 1. The widgets screen showing two PHP code widgets in use.
     491. The widgets screen showing a PHP code widget in use.
     502. The output of the widget on the site.
     51
     52== Changelog ==
     53= 2.0 =
     54* Changed widget to use new Class methods for creating widget. This simplifies the widget and should eliminate any problems with it losing code or disappearing from sidebars and so forth.
     55* WARNING: Version 2.0 REQUIRES WordPress 2.8 and up. It will not work with older versions.
     56* WARNING: Upgrading this widget from 1.x will cause the widget to LOSE YOUR EXISTING SIDEBAR CODE. Copy and paste the existing code somewhere else before upgrading, then recreate the widgets afterwards.
Note: See TracChangeset for help on using the changeset viewer.