| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) |
| 3 |
exit; |
| 4 |
/** |
| 5 |
* XYZScripts Insert PHP Snippet Widget Class |
| 6 |
*/ |
| 7 |
////*****************************Sidebar Widget**********************************//// |
| 8 |
class Xyz_Insert_Php_Widget extends WP_Widget { |
| 9 |
/** constructor -- name this the same as the class above */ |
| 10 |
function __construct() { |
| 11 |
parent::__construct(false, $name = 'Insert PHP Snippet'); |
| 12 |
} |
| 13 |
/** @see WP_Widget::widget -- do not rename this */ |
| 14 |
function widget($args, $instance){ |
| 15 |
extract( $args ); |
| 16 |
global $wpdb; |
| 17 |
$title = apply_filters('widget_title', $instance['title']); |
| 18 |
$xyz_ips_id = $instance['message']; |
| 19 |
$entries = $wpdb->get_results($wpdb->prepare( "SELECT content FROM ".$wpdb->prefix."xyz_ips_short_code WHERE id=%d",$xyz_ips_id )); |
| 20 |
$entry = $entries[0]; |
| 21 |
echo $before_widget; |
| 22 |
if ( $title ) |
| 23 |
echo $before_title . $title . $after_title; |
| 24 |
$content_to_eval=$entry->content; |
| 25 |
/***** to handle old codes : start *****/ |
| 26 |
if(get_option('xyz_ips_auto_insert')==1){ |
| 27 |
$xyz_ips_content_start='<?php'; |
| 28 |
$new_line="\r\n"; |
| 29 |
$xyz_ips_content_end='?>'; |
| 30 |
if (stripos($content_to_eval, '<?php') !== false) |
| 31 |
$tag_start_position=stripos($content_to_eval,'<?php'); |
| 32 |
else |
| 33 |
$tag_start_position="-1"; |
| 34 |
if (stripos($content_to_eval, '?>') !== false) |
| 35 |
$tag_end_position=stripos($content_to_eval,'?>'); |
| 36 |
else |
| 37 |
$tag_end_position="-1"; |
| 38 |
if(stripos($content_to_eval, '<?php') === false && stripos($content_to_eval, '?>') === false) |
| 39 |
{ |
| 40 |
$content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval; |
| 41 |
} |
| 42 |
else if(stripos($content_to_eval, '<?php') !== false) |
| 43 |
{ |
| 44 |
if($tag_start_position>=0 && $tag_end_position>=0 && $tag_start_position>$tag_end_position) |
| 45 |
{ |
| 46 |
$content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval; |
| 47 |
} |
| 48 |
} |
| 49 |
else if(stripos($content_to_eval, '<?php') === false) |
| 50 |
{ |
| 51 |
if (stripos($content_to_eval, '?>') !== false) |
| 52 |
{ |
| 53 |
$content_to_eval=$xyz_ips_content_start.$new_line.$content_to_eval; |
| 54 |
} |
| 55 |
} |
| 56 |
$content_to_eval='?>'.$content_to_eval; |
| 57 |
} |
| 58 |
/***** to handle old codes : end *****/ |
| 59 |
else{ |
| 60 |
if(substr(trim($content_to_eval), 0,5)=='<?php') |
| 61 |
$content_to_eval='?>'.$content_to_eval; |
| 62 |
} |
| 63 |
|
| 64 |
eval($content_to_eval); |
| 65 |
echo $after_widget; |
| 66 |
} |
| 67 |
/** @see WP_Widget::update -- do not rename this */ |
| 68 |
function update($new_instance, $old_instance) { |
| 69 |
$instance = $old_instance; |
| 70 |
$instance['title'] = strip_tags($new_instance['title']); |
| 71 |
$instance['message'] = strip_tags($new_instance['message']); |
| 72 |
return $instance; |
| 73 |
} |
| 74 |
/** @see WP_Widget::form -- do not rename this */ |
| 75 |
function form($instance) { |
| 76 |
global $wpdb; |
| 77 |
$entries = $wpdb->get_results($wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."xyz_ips_short_code WHERE status=%d ORDER BY id DESC",1 )); |
| 78 |
if(isset($instance['title'])){ |
| 79 |
$title = esc_attr($instance['title']); |
| 80 |
}else{ |
| 81 |
$title = ''; |
| 82 |
} |
| 83 |
if(isset($instance['message'])){ |
| 84 |
$message = esc_attr($instance['message']); |
| 85 |
}else{ |
| 86 |
$message = ''; |
| 87 |
} |
| 88 |
?> |
| 89 |
<p> |
| 90 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 91 |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> |
| 92 |
</p> |
| 93 |
<p> |
| 94 |
<label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Choose Snippet :'); ?></label> |
| 95 |
<select name="<?php echo $this->get_field_name('message'); ?>"> |
| 96 |
<?php |
| 97 |
if( count($entries)>0 ) { |
| 98 |
$count=1; |
| 99 |
$class = ''; |
| 100 |
foreach( $entries as $entry ) { |
| 101 |
?> |
| 102 |
<option value="<?php echo $entry->id;?>" <?php if($message==$entry->id)echo "selected"; ?>><?php echo $entry->title;?></option> |
| 103 |
<?php |
| 104 |
} |
| 105 |
} |
| 106 |
?> |
| 107 |
</select> |
| 108 |
</p> |
| 109 |
<?php |
| 110 |
} |
| 111 |
} // end class Xyz_Insert_Php_Widget |
| 112 |
|
| 113 |
function xyz_ips_add_snippet_widget(){ |
| 114 |
register_widget("Xyz_Insert_Php_Widget"); |
| 115 |
} |
| 116 |
add_action('widgets_init','xyz_ips_add_snippet_widget'); |
| 117 |
|
| 118 |
?> |
| 119 |
|