WP init
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
if(!function_exists('ekit_mail_chimp_rest')){
|
||||
|
||||
function ekit_mail_chimp_rest(WP_REST_Request $request ){
|
||||
$return = ['success' => [], 'error' => [] ];
|
||||
|
||||
$token = $request['token'];
|
||||
$list = $request['list'];
|
||||
$email = $request['email'];
|
||||
$firstname = $request['firstname'];
|
||||
$lastname = $request['lastname'];
|
||||
$phone = $request['phone'];
|
||||
|
||||
$url = 'https://us20.api.mailchimp.com/3.0/lists/'.$list.'/members/';
|
||||
|
||||
$margeFiled = [];
|
||||
if(strlen($firstname) > 1):
|
||||
$margeFiled['FNAME'] = $firstname;
|
||||
endif;
|
||||
if(strlen($lastname) > 1):
|
||||
$margeFiled['LNAME'] = $lastname;
|
||||
endif;
|
||||
if(strlen($phone) > 1):
|
||||
$margeFiled['PHONE'] = $phone;
|
||||
endif;
|
||||
|
||||
$postData = [];
|
||||
$postData['email_address'] = $email;
|
||||
$postData['status'] = 'subscribed';
|
||||
if(sizeof($margeFiled) > 0):
|
||||
$postData['merge_fields'] = $margeFiled;
|
||||
endif;
|
||||
$postData['status_if_new'] = 'subscribed';
|
||||
|
||||
$response = wp_remote_post( $url, [
|
||||
'method' => 'POST',
|
||||
'data_format' => 'body',
|
||||
'timeout' => 45,
|
||||
'headers' => [
|
||||
|
||||
'Authorization' => 'apikey '.$token,
|
||||
'Content-Type' => 'application/json; charset=utf-8'
|
||||
],
|
||||
'body' => wp_json_encode($postData )
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$error_message = $response->get_error_message();
|
||||
$return['error'] = "Something went wrong: $error_message";
|
||||
} else {
|
||||
$return['success'] = $response;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* wp rest api add action
|
||||
*/
|
||||
add_action( 'rest_api_init', function () {
|
||||
register_rest_route( 'elementskit-lite', '/mailchimp/',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'ekit_mail_chimp_rest',
|
||||
'permission_callback' => '__return_true',
|
||||
)
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace ElementsKit_Lite;
|
||||
|
||||
use \Elementor\ElementsKit_Widget_Mail_Chimp_Handler;
|
||||
|
||||
class ElementsKit_Widget_Mail_Chimp_Api extends Core\Handler_Api {
|
||||
|
||||
public function config(){
|
||||
$this->prefix = 'widget/mailchimp';
|
||||
}
|
||||
|
||||
public function get_sendmail(){
|
||||
|
||||
$return = ['success' => [], 'error' => [] ];
|
||||
$dataApi = ElementsKit_Widget_Mail_Chimp_Handler::get_data();
|
||||
|
||||
$token = isset($dataApi['token']) ? $dataApi['token'] : '';
|
||||
$listed = $this->request['listed'];
|
||||
|
||||
$email = $this->request['email'];
|
||||
$firstname = $this->request['firstname'];
|
||||
$lastname = $this->request['lastname'];
|
||||
$phone = $this->request['phone'];
|
||||
|
||||
$data = [
|
||||
'email_address' => (($email != '') ? $email : ''),
|
||||
'status_if_new' => 'subscribed',
|
||||
'merge_fields' => [
|
||||
'FNAME' => (($firstname != '') ? $firstname : ''),
|
||||
'LNAME' => (($lastname != '') ? $lastname : ''),
|
||||
'PHONE' => (($phone != '') ? $phone : ''),
|
||||
],
|
||||
];
|
||||
|
||||
if(!empty($this->request['double_opt_in']) && $this->request['double_opt_in'] === 'yes') {
|
||||
$data['status'] = 'pending';
|
||||
} else {
|
||||
$data['status'] = 'subscribed';
|
||||
}
|
||||
|
||||
$server = explode('-', $token);
|
||||
if( !is_array($server) || empty($token) || !isset($server[1]) ){
|
||||
$return['error'] = esc_html__( 'Please set API Key into Dashboard User Data. ', 'elementskit-lite' );
|
||||
return $return;
|
||||
}
|
||||
|
||||
$url = 'https://'.$server[1].'.api.mailchimp.com/3.0/lists/'.$listed.'/members/';
|
||||
|
||||
$response = wp_remote_post( $url, [
|
||||
'method' => 'POST',
|
||||
'data_format' => 'body',
|
||||
'timeout' => 45,
|
||||
'headers' => [
|
||||
|
||||
'Authorization' => 'apikey '.$token,
|
||||
'Content-Type' => 'application/json; charset=utf-8'
|
||||
],
|
||||
'body' => wp_json_encode($data )
|
||||
]
|
||||
);
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$error_message = $response->get_error_message();
|
||||
$return['error'] = "Something went wrong: $error_message";
|
||||
} else {
|
||||
$return['success'] = $response;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
//https://us20.api.mailchimp.com/3.0/lists?apikey=24550c8cb06076781d51a80274a52878-us20
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace Elementor;
|
||||
|
||||
use ElementsKit_Lite\ElementsKit_Widget_Mail_Chimp_Api;
|
||||
use ElementsKit_Lite\Libs\Framework\Attr;
|
||||
|
||||
class ElementsKit_Widget_Mail_Chimp_Handler extends \ElementsKit_Lite\Core\Handler_Widget{
|
||||
|
||||
static function get_name() {
|
||||
return 'elementskit-mail-chimp';
|
||||
}
|
||||
|
||||
static function get_title() {
|
||||
return esc_html__( 'MailChimp', 'elementskit-lite' );
|
||||
}
|
||||
|
||||
static function get_icon() {
|
||||
return 'eicon-mailchimp ekit-widget-icon ';
|
||||
}
|
||||
|
||||
static function get_categories() {
|
||||
return [ 'elementskit' ];
|
||||
}
|
||||
static function get_keywords() {
|
||||
return ['ekit', 'email', 'mail chimp', 'mail', 'subscription'];
|
||||
}
|
||||
|
||||
static function get_dir() {
|
||||
return \ElementsKit_Lite::widget_dir() . 'mail-chimp/';
|
||||
}
|
||||
|
||||
static function get_url() {
|
||||
return \ElementsKit_Lite::widget_url() . 'mail-chimp/';
|
||||
}
|
||||
|
||||
public function wp_init() {
|
||||
|
||||
require_once $this->get_dir() . 'mail-chimp-api.php';
|
||||
|
||||
new ElementsKit_Widget_Mail_Chimp_Api();
|
||||
}
|
||||
|
||||
|
||||
static function get_data(){
|
||||
$data = Attr::instance()->utils->get_option('user_data', []);
|
||||
|
||||
$token = (isset($data['mail_chimp']) && !empty($data['mail_chimp']['token']) ) ? $data['mail_chimp']['token'] : '';
|
||||
|
||||
$list = (isset($data['mail_chimp']) && !empty($data['mail_chimp']['list']) ) ? $data['mail_chimp']['list'] : '';
|
||||
|
||||
return [
|
||||
'token' => $token,
|
||||
'list' => $list,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user