-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
96 lines (78 loc) · 2.2 KB
/
ajax.php
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
<?php
/*
******* Post View Count **********
*/
add_action( 'wp_ajax_nopriv_post_view_count', 'post_view_count' );
add_action( 'wp_ajax_post_view_count', 'post_view_count' );
function post_view_count()
{
$id = $_GET['post_id'];
$currentView = (int) get_post_meta( $id, 'post_view_count', true );
$newView = $currentView + 1;
update_post_meta( $id, 'post_view_count', $newView );
echo $newView;
die();
}
/*
********* AH Search ***********
*/
add_action( 'wp_ajax_nopriv_ah_search', 'ah_search' );
add_action( 'wp_ajax_ah_search', 'ah_search' );
function ah_search()
{
$query = isset( $_GET['s'] ) ? htmlspecialchars( $_GET['s'] ) : '';
$r = array();
$i = 0;
$result = new WP_Query( 's=' . $query );
if ( $result->have_posts() ):
while( $result->have_posts() ) : $result->the_post();
$r[$i]['title'] = get_the_title();
$r[$i]['url'] = get_the_permalink();
$r[$i]['type'] = get_post_type();
$i++;
endwhile;
endif;
wp_send_json( $r );
}
/*
* Contact Form
*/
/* AJAX check */
add_action( 'wp_ajax_nopriv_ah_contact_form', 'ah_contact_form' );
add_action( 'wp_ajax_ah_contact_form', 'ah_contact_form' );
function ah_contact_form()
{
$mailInfo = array();
$notice = '';
if ( $_REQUEST['name'] === '' ) {
$notice = 'Please enter your name. <br />';
}
if ( $_REQUEST['email'] === '' ) {
$notice .= 'Please enter your email. <br />';
}
if ( $_REQUEST['subject'] === '' ) {
$notice .= 'Please Enter your subject. <br />';
}
if ( $_REQUEST['message'] === '' ) {
$notice .= 'Please Enter your message';
}
if ( $notice === '' ) {
$from = $_POST['email'];
$fromName = $_POST['name'];
$subject = isset( $_POST['subject'] ) ? $_POST['subject'] : 'Champ sujet vide';
$message = $_POST['message'];
$headers[] = 'From: ' . $fromName . '<' . $from . '>';
$headers[] = 'Reply-To: ' . $fromName . '<' . $from . '>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
if ( wp_mail( get_bloginfo('admin_email'), $subject, $message, $headers ) ) {
echo 1;
// echo 'You Sent <pre>' . print_r($_REQUEST, true) . '</pre>';
} else {
// echo 'Email not sent';
echo 0;
}
} else {
echo '<p style="color:red">From server <br />' . $notice . '</p>';
}
die();
}