forked from lgharib/wp_cakemail_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
96 lines (82 loc) · 2.53 KB
/
functions.php
File metadata and controls
96 lines (82 loc) · 2.53 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
<?php
/*
Author: Savoir-Faire Linux
Contributor: Gharib Larbi
Function: cakemail_api_client
How to use:
1- Contact Cakemail (or one of their distributors ex: Courilleur) and ask for you api_key that is related to you email and password
2- Once you get these informations use the following curl command to get your related user_key:
curl -H 'apikey: YOUR_API_KEY_PROVIDED_BY_CAKEMAIL' -d 'email=YOUR_EMAIL&password=YOUR_PASSWORD' https://api.wbsrvc.com/User/Login
3- Add this code in the functions.php file in your Wordpress Theme and use [compteur_signatures lang="fr"] or [compteur_signatures] shortcode anywhere in your Wordpress pages or posts
*/
/*
Global configuration:
*/
$unique_api_key = 'YOUR_API_KEY_PROVIDED_BY_CAKEMAIL';
$unique_user_key = 'YOUR_USER_KEY_PROVIDED_BY_CAKEMAIL';
$unique_list_id = 'YOUR_CAKEMAIL_LIST_ID';
function cakemail_api_client_list_counter($list_id, $api_key, $user_key){
$url = 'https://api.wbsrvc.com/List/Show/';
$result = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array('apikey' => $api_key),
'body' => array(
'user_key' => $user_key,
'list_id' => $list_id,
'count' => 'true',
'status' => 'active',
),
'cookies' => array()
)
);
if ( is_wp_error( $result ) ) {
$error_message = $result->get_error_message();
return "Something went wrong: $error_message";
} else {
if (isset($result)) {
if (array_key_exists('body', $result)) {
$json_object = json_decode($result['body'], true);
if ($json_object['status'] == 'success') {
foreach($json_object['data'] as $key => $value) {
if($key=="count")
{
return $value;
}
else{
return "ERROR no Key count found: " . print_r($result);
}
}
} else {
return "Curl API Request failed: " . print_r($result);
}
}else{
return "No key body in result array";
}
}
}
}
function get_sign_count($atts)
{
$a = shortcode_atts( array(
'lang' => 'fr'
), $atts );
$lang = $a['lang'];
$count = cakemail_api_client_list_counter($unique_list_id, $unique_api_key, $unique_user_key);
if(is_numeric($count)){
if($lang == 'en'){
$formattedNumber = number_format($count);
}else{
$formattedNumber = number_format($count, 0, ',', ' ');
}
}
else{
$formattedNumber = $count;
}
return $formattedNumber;
}
add_shortcode( 'subscribers_counter', 'get_sign_count' );
?>