| 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 |
$exception_occur=0; |
| 64 |
$exception_msg=""; |
| 65 |
//////// code for error handling in exception mail//////// |
| 66 |
try { |
| 67 |
eval($content_to_eval); |
| 68 |
} catch (Throwable $e) { // For PHP 7 and later |
| 69 |
$exception_occur = 1; |
| 70 |
$exception_msg = $e->getMessage(); |
| 71 |
} |
| 72 |
catch (Exception $e) { // For PHP 5 |
| 73 |
$exception_occur = 1; |
| 74 |
$exception_msg = $e->getMessage(); |
| 75 |
} |
| 76 |
/////////////////////////////////////////////////////////// |
| 77 |
if($exception_occur==1) { |
| 78 |
// global $post; |
| 79 |
// $post_slug = $post->post_name; |
| 80 |
// if($post_slug!="xyz-ics-preview-page" && !is_customize_preview()) |
| 81 |
{ |
| 82 |
$status=1; |
| 83 |
$wpdb->update($wpdb->prefix.'xyz_ics_short_code', array('exception_status'=>$status), array('title'=>$snippet_name)); |
| 84 |
if(get_option('xyz_ips_exception_email')!="0" && get_option('xyz_ips_exception_email')!="" && get_option('xyz_ips_auto_exception')==1) |
| 85 |
{ |
| 86 |
$email=get_option('xyz_ips_exception_email'); |
| 87 |
$headers= "Content-type: text/html"; |
| 88 |
$subject="Exception Report"; |
| 89 |
$message="Hi,<br>An exception occurred while running one of the snippet in the widget.The snippet name is ".$snippet_name; |
| 90 |
$message.=".<br>Exception details are given below : <br>"; |
| 91 |
$message.=$exception_msg; |
| 92 |
wp_mail($email, $subject, $message,$headers); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
//eval($content_to_eval); |
| 97 |
echo $after_widget; |
| 98 |
} |
| 99 |
/** @see WP_Widget::update -- do not rename this */ |
| 100 |
function update($new_instance, $old_instance) { |
| 101 |
$instance = $old_instance; |
| 102 |
$instance['title'] = strip_tags($new_instance['title']); |
| 103 |
$instance['message'] = strip_tags($new_instance['message']); |
| 104 |
return $instance; |
| 105 |
} |
| 106 |
/** @see WP_Widget::form -- do not rename this */ |
| 107 |
function form($instance) { |
| 108 |
global $wpdb; |
| 109 |
$entries = $wpdb->get_results($wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."xyz_ips_short_code WHERE status=%d ORDER BY id DESC",1 )); |
| 110 |
if(isset($instance['title'])){ |
| 111 |
$title = esc_attr($instance['title']); |
| 112 |
}else{ |
| 113 |
$title = ''; |
| 114 |
} |
| 115 |
if(isset($instance['message'])){ |
| 116 |
$message = esc_attr($instance['message']); |
| 117 |
}else{ |
| 118 |
$message = ''; |
| 119 |
} |
| 120 |
?> |
| 121 |
<p> |
| 122 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 123 |
<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; ?>" /> |
| 124 |
</p> |
| 125 |
<p> |
| 126 |
<label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Choose Snippet :'); ?></label> |
| 127 |
<select name="<?php echo $this->get_field_name('message'); ?>"> |
| 128 |
<?php |
| 129 |
if(!empty($entries))//if( count($entries)>0 ) |
| 130 |
{ |
| 131 |
$count=1; |
| 132 |
$class = ''; |
| 133 |
foreach( $entries as $entry ) { |
| 134 |
?> |
| 135 |
<option value="<?php echo $entry->id;?>" <?php if($message==$entry->id)echo "selected"; ?>><?php echo $entry->title;?></option> |
| 136 |
<?php |
| 137 |
} |
| 138 |
} |
| 139 |
?> |
| 140 |
</select> |
| 141 |
</p> |
| 142 |
<?php |
| 143 |
} |
| 144 |
} // end class Xyz_Insert_Php_Widget |
| 145 |
|
| 146 |
function xyz_ips_add_snippet_widget(){ |
| 147 |
register_widget("Xyz_Insert_Php_Widget"); |
| 148 |
} |
| 149 |
add_action('widgets_init','xyz_ips_add_snippet_widget'); |
| 150 |
|
| 151 |
?> |
| 152 |
|