themes = array (
"light" => array("title" => __('Under the Sun', 'contactme'), "css" => "front_light.css"),
"dark" => array("title" => __('Deep in the Universe', 'contactme'), "css" => "front_dark.css")
);
$this->options = array (
"version" => CM_VERSION,
"show_donationbox" => "",
"admin_email" => "alerts@".str_replace("www.", "", $_SERVER["SERVER_NAME"]),
"from_name" => get_bloginfo("name"),
"from_email" => "noreply@".str_replace("www.", "", $_SERVER["SERVER_NAME"]),
"send_confirmation" => "on",
"confirmation_email_subject" => __('Confirmation e-mail', 'contactme'),
"confirmation_email_body" => __('Dear {visitor_name}!', 'contactme').PHP_EOL.PHP_EOL.__('By this message we confirm receiving your request. We will contact you as soon as possible.', 'contactme').PHP_EOL.PHP_EOL.__('Thanks,', 'contactme').PHP_EOL.get_bloginfo("name"),
"theme" => "light",
"form_title" => __('Please fill in the following form to contact us', 'contactme'),
"thanksgiving_message" => __('
Form successfully submitted ', 'contactme').PHP_EOL.__('Thank you {visitor_name} , your message has been submitted to us.', 'contactme')
);
$this->get_settings();
if (is_admin()) {
wp_enqueue_style('contactme_admin', plugins_url('/css/admin.css?ver='.CM_VERSION, __FILE__));
if ($this->check_settings() !== true) add_action('admin_notices', array(&$this, 'admin_warning'));
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('init', array(&$this, 'admin_request_handler'));
add_action('wp_ajax_contactme_submit', array(&$this, "contactme_submit"));
add_action('wp_ajax_nopriv_contactme_submit', array(&$this, "contactme_submit"));
} else {
wp_enqueue_style('contactme_front', plugins_url('/css/'.$this->themes[$this->options['theme']]["css"].'?ver='.CM_VERSION, __FILE__));
add_shortcode('contactme', array(&$this, "shortcode_handler"));
}
}
function get_settings() {
$exists = get_option('contactme_version');
if ($exists) {
foreach ($this->options as $key => $value) {
$this->options[$key] = get_option('contactme_'.$key);
}
}
}
function update_settings() {
if (current_user_can('manage_options')) {
foreach ($this->options as $key => $value) {
update_option('contactme_'.$key, $value);
}
}
}
function populate_settings() {
foreach ($this->options as $key => $value) {
if (isset($_POST['contactme_'.$key])) {
$this->options[$key] = stripslashes($_POST['contactme_'.$key]);
}
}
}
function check_settings() {
$errors = array();
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $this->options['admin_email']) || strlen($this->options['admin_email']) == 0) $errors[] = __('E-mail for notifications must be valid e-mail address', 'contactme');
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $this->options['from_email']) || strlen($this->options['from_email']) == 0) $errors[] = __('Sender e-mail must be valid e-mail address', 'contactme');
if (strlen($this->options['from_name']) < 3) $errors[] = __('Sender name is too short', 'contactme');
if ($this->options['send_confirmation'] == 'on') {
if (strlen($this->options['confirmation_email_subject']) < 3) $errors[] = __('Confirmation e-mail subject must contain at least 3 characters', 'contactme');
else if (strlen($this->options['confirmation_email_subject']) > 64) $errors[] = __('Confirmation e-mail subject must contain maximum 64 characters', 'contactme');
if (strlen($this->options['confirmation_email_body']) < 3) $errors[] = __('Confirmation e-mail body must contain at least 3 characters', 'contactme');
}
if (strlen($this->options['form_title']) < 3) $errors[] = __('Form title is too short', 'contactme');
if (strlen($this->options['thanksgiving_message']) < 3) $errors[] = __('Thanksgiving message must contain at least 3 characters', 'contactme');
if (empty($errors)) return true;
return $errors;
}
function admin_menu() {
add_options_page(__('Contact Me', 'contactme'), __('Contact Me', 'contactme'), 'manage_options', 'contact-me', array(&$this, 'admin_settings'));
}
function admin_settings() {
global $wpdb;
$message = "";
$errors = $this->check_settings();
if (is_array($errors)) echo ''.__('The following error(s) exists:', 'contactme').' - '.implode(' - ', $errors).'
';
print ('
'.__('Contact Me - Settings', 'contactme').'
'.$message);
if ($this->options['show_donationbox'] != CM_VERSION) {
print ('
');
}
print ('
');
}
function admin_request_handler() {
global $wpdb;
if (!empty($_POST['ak_action'])) {
switch($_POST['ak_action']) {
case 'contactme_update_settings':
$this->populate_settings();
if (isset($_POST["contactme_send_confirmation"])) $this->options['send_confirmation'] = "on";
else $this->options['send_confirmation'] = "off";
$this->update_settings();
$errors = $this->check_settings();
if (!is_array($errors)) header('Location: '.get_bloginfo('wpurl').'/wp-admin/admin.php?page=contact-me&updated=true');
else header('Location: '.get_bloginfo('wpurl').'/wp-admin/admin.php?page=contact-me');
die();
break;
default:
break;
}
}
if (!empty($_GET['ak_action'])) {
switch($_GET['ak_action']) {
case 'contactme_hidedonationbox':
$this->options['show_donationbox'] = CM_VERSION;
$this->update_settings();
header('Location: '.get_bloginfo('wpurl').'/wp-admin/admin.php?page=contact-me');
die();
break;
default:
break;
}
}
}
function admin_warning() {
echo '
'.__('Contact Me plugin almost ready. You must do some settings for it to work.', 'contactme').'
';
}
function shortcode_handler($_atts) {
if ($this->check_settings() === true) {
$width = intval($_atts["width"]);
if ($width < 100 || $width > 1000) $width = "";
$title = $_atts["title"];
if (empty($title)) $title = htmlspecialchars($this->options['form_title'], ENT_QUOTES);
$suffix = "_".rand(1000, 9999);
$form = '
';
} else $form = "";
return $form;
}
function contactme_submit() {
$name = trim($_POST['contactme_name']);
$email = trim($_POST['contactme_email']);
$comments = trim($_POST['contactme_comments']);
if (get_magic_quotes_gpc()) {
$name = stripslashes($name);
$email = stripslashes($email);
$comments = stripslashes($comments);
}
$error = '';
if ($name == '') {
$error .= ''.__('Your name is required.', 'contactme').' ';
}
if ($email == '') {
$error .= ''.__('Your e-mail address is required.', 'contactme').' ';
} else if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $email)) {
$error .= ''.__('You have entered an invalid e-mail address.', 'contactme').' ';
}
if ($comments == '') {
$error .= ''.__('You must enter a message to send.', 'contactme').' ';
}
if ($error != '') {
echo '';
} else {
if ($this->options['send_confirmation'] == 'on') {
$keywords = array("{visitor_name}", "{visitor_email}", "{visitor_message}");
$vals = array($name, $email, $comments);
$msg = str_replace($keywords, $vals, $this->options['confirmation_email_body']);
$mail_headers = "Content-Type: text/plain; charset=utf-8".PHP_EOL;
$mail_headers .= "From: ".$this->options['from_name']." <".$this->options['from_email'].">".PHP_EOL;
$mail_headers .= "MIME-Version: 1.0".PHP_EOL;
wp_mail($email, $this->options['confirmation_email_subject'], $msg, $mail_headers);
}
$subject = __('You have been contacted by', 'contactme').' '.$name;
$msg = __('You have been contacted by', 'contactme').' '.$name.'. '.__('The message is as follows.', 'contactme').PHP_EOL.PHP_EOL;
$msg .= $comments.PHP_EOL.PHP_EOL;
$msg .= __('You can contact', 'contactme').' '.$name.' '.__('via email', 'contactme').' '.$email.'.'.PHP_EOL;
$msg .= __('Sent via', 'contactme').' '.$_SERVER['HTTP_REFERER'];
$msg = wordwrap($msg, 70);
$mail_headers .= "From: ".$name." <".$this->options['from_email'].">".PHP_EOL;
$mail_headers .= "Reply-To: ".$email.PHP_EOL;
$mail_headers .= "MIME-Version: 1.0".PHP_EOL;
$mail_headers .= "Content-type: text/plain; charset=utf-8".PHP_EOL;
if(wp_mail($this->options['admin_email'], $subject, $msg, $mail_headers)) {
$keywords = array("{visitor_name}", "{visitor_email}", "\n", "\r");
$vals = array($name, $email, " ", "");
$thanksgiving_message = str_replace($keywords, $vals, $this->options['thanksgiving_message']);
echo '
'.$thanksgiving_message.'
';
} else {
echo ''.__('Internal error occured!', 'contactme').'
';
}
}
exit;
}
}
$contactme = new contactme_class();
?>