-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress-plugin-frontend.php
More file actions
143 lines (114 loc) · 4.52 KB
/
wordpress-plugin-frontend.php
File metadata and controls
143 lines (114 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
Plugin Name: WordPress Frontend
Plugin URI: https://github.com/anuchit33/wordpress-plugin-frontend
Description: wordpress-plugin-frontend
Author: Anuchit Yai-in
Version: 0.0.1
*/
class WordPressPluginFrontend {
function __construct() {
# Activation / Deactivation Hooks
register_activation_hook(__FILE__, array($this, 'wp_activation'));
register_deactivation_hook(__FILE__, array($this, 'wp_deactivation'));
# Shortcode
add_shortcode('contact-us', array($this, 'wp_shortcode_display'));
# add action get
add_action('wp_ajax_get_gold_price', array($this, 'wp_api_get_gold_price'));
add_action('wp_ajax_nopriv_get_gold_price', array($this, 'wp_api_get_gold_price'));
}
function wp_activation(){
global $wpdb;
/**1. create page**/
# create page Contact-US
$page_id = wp_insert_post(array(
'post_title' => 'Contact-US',
'post_type' => 'page',
'post_status' => 'publish',
'comment_status' => 'closed',
'post_content' => "[contact-us]"
));
# save page_id
update_option( 'wp-frontend', $page_id );
/**2. create table contact_message**/
global $wpdb;
$table_name = $wpdb->prefix.'contact_message';
$charset_collate = $wpdb->get_charset_collate();
$sql_contact_message = "CREATE TABLE `$table_name` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`subject` varchar(55) NOT NULL,
`email` varchar(55) NOT NULL,
`message` text NOT NULL,
`created_datetime` datetime NOT NULL,
UNIQUE KEY id (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$wpdb->query($sql_contact_message);
/**3. create table contact_email**/
global $wpdb;
$table_name = $wpdb->prefix.'contact_email';
$charset_collate = $wpdb->get_charset_collate();
$sql_contact_email = "CREATE TABLE `$table_name` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(55) NOT NULL,
`email` varchar(55) NOT NULL,
UNIQUE KEY id (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$wpdb->query($sql_contact_email);
}
function wp_deactivation(){
global $wpdb;
# 1.delete page
$page_id = get_option('wp-frontend');
wp_delete_post($page_id);
# 2.delete table contact_message
$table_name = $wpdb->prefix . 'contact_message';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
# 3.delete table contact_email
$table_name = $wpdb->prefix . 'contact_email';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
}
// function wp_shortcode_display($atts){
// return 'Hello WordPress Plugin!';
// }
function wp_shortcode_display($atts) {
# handle action POST
$this->action_post();
ob_start();
require_once( dirname(__FILE__) . '/templates/frontend/page-contact.php');
$content = ob_get_contents();
ob_end_clean();
return $content;
}
function action_post(){
if (isset($_POST['subject'])){
if (! isset( $_POST['csrf'] ) || ! wp_verify_nonce( $_POST['csrf'], 'post-contact' )){
echo 'Sorry, your nonce did not verify.';
exit;
}
global $wpdb;
$data = array(
'subject' => sanitize_text_field($_POST['subject']),
'email' => sanitize_text_field($_POST['email']),
'message' => sanitize_text_field($_POST['message']),
'created_datetime' => date('Y-m-d H:i:s'),
);
$tablename = $wpdb->prefix . 'contact_message';
$wpdb->insert($tablename, $data);
echo 'Saved your post successfully! :)';
// send email
$tablename = $wpdb->prefix . 'contact_email';
$results = $wpdb->get_results("SELECT * FROM " . $tablename . " ", OBJECT);
$to = array();
foreach ($results as $v) {
$to[] = $v->email;
}
$subject = 'Contact Us';
$message = "Subject: " . $data["subject"] . " \n Email: " . $data["email"] . " \n Message: \n " . $data["message"] . "";
$headers = array('From: '.$data['email'].' <'.$data['email'].'>');
wp_mail($to, $subject, $message, $headers);
}
}
}
new WordPressPluginFrontend();