autumn_bird

Executable PHP widget

Sep 22nd, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  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. License: GPL2
  10.  
  11. Copyright 2009 Samuel Wood (email : otto@ottodestruct.com)
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License version 2,
  15. as published by the Free Software Foundation.
  16.  
  17. You may NOT assume that you can use any other version of the GPL.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23.  
  24. The license for this software can likely be found here:
  25. http://www.gnu.org/licenses/gpl-2.0.html
  26.  
  27. */
  28.  
  29. class PHP_Code_Widget extends WP_Widget {
  30.  
  31. function PHP_Code_Widget() {
  32. $widget_ops = array('classname' => 'widget_execphp', 'description' => __('Arbitrary text, HTML, or PHP Code'));
  33. $control_ops = array('width' => 400, 'height' => 350);
  34. $this->WP_Widget('execphp', __('PHP Code'), $widget_ops, $control_ops);
  35. }
  36.  
  37. function widget( $args, $instance ) {
  38. extract($args);
  39. $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
  40. $text = apply_filters( 'widget_execphp', $instance['text'], $instance );
  41. echo $before_widget;
  42. if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
  43. ob_start();
  44. eval('?>'.$text);
  45. $text = ob_get_contents();
  46. ob_end_clean();
  47. ?>
  48. <div class="execphpwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
  49. <?php
  50. echo $after_widget;
  51. }
  52.  
  53. function update( $new_instance, $old_instance ) {
  54. $instance = $old_instance;
  55. $instance['title'] = strip_tags($new_instance['title']);
  56. if ( current_user_can('unfiltered_html') )
  57. $instance['text'] = $new_instance['text'];
  58. else
  59. $instance['text'] = stripslashes( wp_filter_post_kses( $new_instance['text'] ) );
  60. $instance['filter'] = isset($new_instance['filter']);
  61. return $instance;
  62. }
  63.  
  64. function form( $instance ) {
  65. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
  66. $title = strip_tags($instance['title']);
  67. $text = format_to_edit($instance['text']);
  68. ?>
  69. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  70. <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>
  71.  
  72. <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>
  73.  
  74. <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>
  75. <?php
  76. }
  77. }
  78.  
  79. add_action('widgets_init', create_function('', 'return register_widget("PHP_Code_Widget");'));
  80.  
  81. // donate link on manage plugin page
  82. add_filter('plugin_row_meta', 'execphp_donate_link', 10, 2);
  83. function execphp_donate_link($links, $file) {
  84. if ($file == plugin_basename(__FILE__)) {
  85. $donate_link = '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=otto%40ottodestruct%2ecom">Donate</a>';
  86. $links[] = $donate_link;
  87. }
  88. return $links;
  89. }
Add Comment
Please, Sign In to add comment