PHP Code Widget - Version 2.0

Version Description

  • 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.
  • WARNING: Version 2.0 REQUIRES WordPress 2.8 and up. It will not work with older versions.
  • WARNING: Upgrading this widget from 1.x might cause the widget to LOSE YOUR EXISTING SIDEBAR CODE. Copy and paste the existing code somewhere else before upgrading (just in case), then recreate the widgets afterwards.
Download this release

Release Info

Developer Otto42
Plugin Icon PHP Code Widget
Version 2.0
Comparing to
See all releases

Code changes from version 1.1 to 2.0

Files changed (4) hide show
  1. execphp.php +51 -107
  2. readme.txt +17 -6
  3. screenshot-1.png +0 -0
  4. screenshot-2.png +0 -0
execphp.php CHANGED
@@ -1,134 +1,78 @@
1
  <?php
2
  /*
3
  Plugin Name: Executable PHP widget
 
4
  Description: Like the Text widget, but it will take PHP code as well. Heavily derived from the Text widget code in WordPress.
5
  Author: Otto
6
- Version: 1.1
7
  Author URI: http://ottodestruct.com
8
- Plugin URI: http://wordpress.org/extend/plugins/php-code-widget/
9
- */
10
- /*
11
  This program is free software; you can redistribute it and/or modify
12
  it under the terms of the GNU General Public License version 2,
13
- as published by the Free Software Foundation.
 
 
14
 
15
  This program is distributed in the hope that it will be useful,
16
  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
  GNU General Public License for more details.
 
 
 
 
19
  */
20
 
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 );
27
-
28
- $options = get_option('widget_execphp');
29
- if ( !isset($options[$number]) )
30
- return;
31
-
32
- $title = $options[$number]['title'];
33
- $text = apply_filters( 'widget_execphp', $options[$number]['text'] );
34
- ?>
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; ?>
39
- <?php
40
- }
41
-
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
- 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
- ?>
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.0
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");'));
 
 
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: Otto
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=otto%40ottodestruct%2ecom
4
  Tags: php, widget, execphp
5
- Requires at least: 2.5
6
- Tested up to: 2.7
7
- Stable tag: 1.1
8
 
9
  Like the Text widget, but also allows working PHP code to be inserted.
10
 
@@ -17,6 +17,10 @@ This makes it easier to migrate to a widget-based theme.
17
  All PHP code must be enclosed in the standard <?php and ?> tags for it to be
18
  recognized.
19
 
 
 
 
 
20
  == Installation ==
21
 
22
  1. Upload `execphp.php` to the `/wp-content/plugins/` directory
@@ -25,7 +29,7 @@ recognized.
25
 
26
  == Frequently Asked Questions ==
27
 
28
- = There's some kind of error on line 27! =
29
 
30
  That error means that your PHP code is incorrect or otherwise broken.
31
 
@@ -41,9 +45,16 @@ put into one of the widgets. The reason that it shows the error being in the
41
  execphp.php file is because that is where your code is actually being run
42
  from.
43
 
44
- So, if it says that you have an error on line 27, I assure you, the problem
45
  is yours. Please don't email me about that error.
46
 
47
  == Screenshots ==
48
 
49
- 1. The widgets screen showing two PHP code widgets in use.
 
 
 
 
 
 
 
2
  Contributors: Otto
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=otto%40ottodestruct%2ecom
4
  Tags: php, widget, execphp
5
+ Requires at least: 2.8
6
+ Tested up to: 2.9
7
+ Stable tag: 2.0
8
 
9
  Like the Text widget, but also allows working PHP code to be inserted.
10
 
17
  All PHP code must be enclosed in the standard <?php and ?> tags for it to be
18
  recognized.
19
 
20
+ WARNING: Upgrading to 2.0 from 1.2 may cause loss of your existing widgets.
21
+ Copy the code you have in them elsewhere first, then recreate your widgets
22
+ afterwards.
23
+
24
  == Installation ==
25
 
26
  1. Upload `execphp.php` to the `/wp-content/plugins/` directory
29
 
30
  == Frequently Asked Questions ==
31
 
32
+ = There's some kind of error on line 43! =
33
 
34
  That error means that your PHP code is incorrect or otherwise broken.
35
 
45
  execphp.php file is because that is where your code is actually being run
46
  from.
47
 
48
+ So, if it says that you have an error on line 43, I assure you, the problem
49
  is yours. Please don't email me about that error.
50
 
51
  == Screenshots ==
52
 
53
+ 1. The widgets screen showing a PHP code widget in use.
54
+ 2. The output of the widget on the site.
55
+
56
+ == Changelog ==
57
+ = 2.0 =
58
+ * 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.
59
+ * WARNING: Version 2.0 REQUIRES WordPress 2.8 and up. It will not work with older versions.
60
+ * WARNING: Upgrading this widget from 1.x might cause the widget to LOSE YOUR EXISTING SIDEBAR CODE. Copy and paste the existing code somewhere else before upgrading (just in case), then recreate the widgets afterwards.
screenshot-1.png CHANGED
Binary file
screenshot-2.png ADDED
Binary file