Skip to content

Commit

Permalink
v 0.6.0
Browse files Browse the repository at this point in the history
- Create a test lead.
- Separate page for settings.
  • Loading branch information
Darklg committed Jul 7, 2024
1 parent 84b61a1 commit d1bb7ee
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lang/wpucontactforms_zohocrm-fr_FR.l10n.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
return ['domain'=>NULL,'plural-forms'=>NULL,'messages'=>['Help'=>'Aide','Settings'=>'Réglages','No'=>'Non','Yes'=>'Oui','Check all'=>'Tout cocher','Uncheck all'=>'Tout décocher','Settings saved.'=>'Paramètres sauvegardés.','Upload New Media'=>'Envoi d\'un nouveau média','Language'=>'Langue','Save'=>'Sauvegarder','Connect WPU Contact Forms to ZohoCRM'=>'Connecter WPU Contact Forms à ZohoCRM','App Settings'=>'Paramètre de l\'App','Plugin Settings'=>'Paramètres du plugin','Token Settings'=>'Paramètres du token','Client ID'=>'Client ID','Client Secret'=>'Client Secret','Update Records'=>'Mettre à jour les enregistrements','Access token'=>'Access token','Token expiration'=>'Expiration du token','Last update'=>'Dernière mise à jour','Refresh token'=>'Refresh token','Accounts server'=>'Accounts server','API Domain'=>'API Domain','Token was successfully updated.'=>'Le token a été mis à jour avec succès.','Refresh your access token'=>'Actualiser votre access token','Last update: %s ago.'=>'Dernière mise à jour : il y a %s.','No application installed'=>'Aucune application installée','Please <a target="_blank" href="%s">create an new Server-based application here</a> and specify the following redirect_uri : <br /><strong contenteditable>%s</strong>'=>'Veuillez <a target="_blank" href="%s">créer une nouvelle application « server-based » ici</a> en spécifiant la redirect_uri suivante : <br /><strong contenteditable>%s</strong>','Get the client ID and client Secret and paste it below.'=>'Obtenez le client ID et le client Secret et collez-les ci-dessous.','Connect to your account'=>'Connectez-vous à votre compte','Connection seems to work'=>'La connexion semble fonctionner','Save Changes'=>'Sauvegarder','An error occured while refreshing the token.'=>'Une erreur s’est produite lors de l’actualisation du token.','The response did not contain a token.'=>'La réponse ne contenait pas de token.','There was an error'=>'Il y a eu une erreur'],'language'=>'fr_FR','x-generator'=>'Poedit 3.4.4'];
return ['domain'=>NULL,'plural-forms'=>NULL,'messages'=>['Help'=>'Aide','Settings'=>'Réglages','No'=>'Non','Yes'=>'Oui','Check all'=>'Tout cocher','Uncheck all'=>'Tout décocher','Settings saved.'=>'Paramètres sauvegardés.','Upload New Media'=>'Envoi d\'un nouveau média','Language'=>'Langue','Save'=>'Sauvegarder','Connect WPU Contact Forms to ZohoCRM'=>'Connecter WPU Contact Forms à ZohoCRM','App Settings'=>'Paramètre de l\'App','Plugin Settings'=>'Paramètres du plugin','Token Settings'=>'Paramètres du token','Client ID'=>'Client ID','Client Secret'=>'Client Secret','Update Records'=>'Mettre à jour les enregistrements','Access token'=>'Access token','Token expiration'=>'Expiration du token','Last update'=>'Dernière mise à jour','Refresh token'=>'Refresh token','Accounts server'=>'Accounts server','API Domain'=>'API Domain','Token was successfully updated.'=>'Le token a été mis à jour avec succès.','Refresh your access token'=>'Actualiser votre access token','Last update: %s ago.'=>'Dernière mise à jour : il y a %s.','No application installed'=>'Aucune application installée','Please <a target="_blank" href="%s">create an new Server-based application here</a> and specify the following redirect_uri : <br /><strong contenteditable>%s</strong>'=>'Veuillez <a target="_blank" href="%s">créer une nouvelle application « server-based » ici</a> en spécifiant la redirect_uri suivante : <br /><strong contenteditable>%s</strong>','Get the client ID and client Secret and paste it below.'=>'Obtenez le client ID et le client Secret et collez-les ci-dessous.','Connect to your account'=>'Connectez-vous à votre compte','Connection seems to work'=>'La connexion semble fonctionner','Test the API'=>'Tester l’API','Create or update a test lead.'=>'Créez ou mettez à jour un prospect de test.','API Test : <a href="%s" target="_blank">View the lead</a>'=>'Test API : <a href="%s" target="_blank">Voir le lead</a>','Save Changes'=>'Sauvegarder','An error occured while refreshing the token.'=>'Une erreur s’est produite lors de l’actualisation du token.','The response did not contain a token.'=>'La réponse ne contenait pas de token.','There was an error'=>'Il y a eu une erreur'],'language'=>'fr_FR','x-generator'=>'Poedit 3.4.4'];
Binary file modified lang/wpucontactforms_zohocrm-fr_FR.mo
Binary file not shown.
78 changes: 49 additions & 29 deletions lang/wpucontactforms_zohocrm-fr_FR.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: WPU Contact Forms ZohoCRM\n"
"POT-Creation-Date: 2024-07-04 21:53+0200\n"
"POT-Creation-Date: 2024-07-07 22:37+0200\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
Expand All @@ -22,17 +22,18 @@ msgstr "Aide"
#: .././inc/WPUBaseSettings/WPUBaseSettings.php:132
#: .././inc/WPUBaseSettings/WPUBaseSettings.php:615
#: .././inc/WPUBaseSettings/WPUBaseSettings.php:622
#: .././wpucontactforms_zohocrm.php:59
#: .././wpucontactforms_zohocrm.php:60 .././wpucontactforms_zohocrm.php:72
#: .././wpucontactforms_zohocrm.php:73
msgid "Settings"
msgstr "Réglages"

#: .././inc/WPUBaseSettings/WPUBaseSettings.php:162
#: .././wpucontactforms_zohocrm.php:109
#: .././wpucontactforms_zohocrm.php:124
msgid "No"
msgstr "Non"

#: .././inc/WPUBaseSettings/WPUBaseSettings.php:162
#: .././wpucontactforms_zohocrm.php:110
#: .././wpucontactforms_zohocrm.php:125
msgid "Yes"
msgstr "Oui"

Expand Down Expand Up @@ -60,76 +61,76 @@ msgstr "Langue"
msgid "Save"
msgstr "Sauvegarder"

#: .././wpucontactforms_zohocrm.php:49
#: .././wpucontactforms_zohocrm.php:50
msgid "Connect WPU Contact Forms to ZohoCRM"
msgstr "Connecter WPU Contact Forms à ZohoCRM"

#: .././wpucontactforms_zohocrm.php:85
#: .././wpucontactforms_zohocrm.php:100
msgid "App Settings"
msgstr "Paramètre de l'App"

#: .././wpucontactforms_zohocrm.php:88
#: .././wpucontactforms_zohocrm.php:103
msgid "Plugin Settings"
msgstr "Paramètres du plugin"

#: .././wpucontactforms_zohocrm.php:91
#: .././wpucontactforms_zohocrm.php:106
msgid "Token Settings"
msgstr "Paramètres du token"

#: .././wpucontactforms_zohocrm.php:97
#: .././wpucontactforms_zohocrm.php:112
msgid "Client ID"
msgstr "Client ID"

#: .././wpucontactforms_zohocrm.php:101
#: .././wpucontactforms_zohocrm.php:116
msgid "Client Secret"
msgstr "Client Secret"

#: .././wpucontactforms_zohocrm.php:105
#: .././wpucontactforms_zohocrm.php:120
msgid "Update Records"
msgstr "Mettre à jour les enregistrements"

#: .././wpucontactforms_zohocrm.php:114
#: .././wpucontactforms_zohocrm.php:129
msgid "Access token"
msgstr "Access token"

#: .././wpucontactforms_zohocrm.php:118
#: .././wpucontactforms_zohocrm.php:133
msgid "Token expiration"
msgstr "Expiration du token"

#: .././wpucontactforms_zohocrm.php:122
#: .././wpucontactforms_zohocrm.php:137
msgid "Last update"
msgstr "Dernière mise à jour"

#: .././wpucontactforms_zohocrm.php:126
#: .././wpucontactforms_zohocrm.php:141
msgid "Refresh token"
msgstr "Refresh token"

#: .././wpucontactforms_zohocrm.php:130
#: .././wpucontactforms_zohocrm.php:145
msgid "Accounts server"
msgstr "Accounts server"

#: .././wpucontactforms_zohocrm.php:134
#: .././wpucontactforms_zohocrm.php:149
msgid "API Domain"
msgstr "API Domain"

#: .././wpucontactforms_zohocrm.php:198 .././wpucontactforms_zohocrm.php:391
#: .././wpucontactforms_zohocrm.php:213 .././wpucontactforms_zohocrm.php:452
msgid "Token was successfully updated."
msgstr "Le token a été mis à jour avec succès."

#: .././wpucontactforms_zohocrm.php:203
#: .././wpucontactforms_zohocrm.php:218
msgid "Refresh your access token"
msgstr "Actualiser votre access token"

#: .././wpucontactforms_zohocrm.php:205
#: .././wpucontactforms_zohocrm.php:220
#, php-format
msgid "Last update: %s ago."
msgstr "Dernière mise à jour : il y a %s."

#: .././wpucontactforms_zohocrm.php:209
#: .././wpucontactforms_zohocrm.php:224
msgid "No application installed"
msgstr "Aucune application installée"

#: .././wpucontactforms_zohocrm.php:210
#: .././wpucontactforms_zohocrm.php:225
#, php-format
msgid ""
"Please <a target=\"_blank\" href=\"%s\">create an new Server-based "
Expand All @@ -140,30 +141,49 @@ msgstr ""
"« server-based » ici</a> en spécifiant la redirect_uri suivante : <br /"
"><strong contenteditable>%s</strong>"

#: .././wpucontactforms_zohocrm.php:211
#: .././wpucontactforms_zohocrm.php:226
msgid "Get the client ID and client Secret and paste it below."
msgstr "Obtenez le client ID et le client Secret et collez-les ci-dessous."

#: .././wpucontactforms_zohocrm.php:217
#: .././wpucontactforms_zohocrm.php:230
msgid "Connect to your account"
msgstr "Connectez-vous à votre compte"

#: .././wpucontactforms_zohocrm.php:220
#: .././wpucontactforms_zohocrm.php:232
msgid "Connection seems to work"
msgstr "La connexion semble fonctionner"

#: .././wpucontactforms_zohocrm.php:229
#: .././wpucontactforms_zohocrm.php:238 .././wpucontactforms_zohocrm.php:240
msgid "Test the API"
msgstr "Tester l’API"

#: .././wpucontactforms_zohocrm.php:239
msgid "Create or update a test lead."
msgstr "Créez ou mettez à jour un prospect de test."

#: .././wpucontactforms_zohocrm.php:272
#, php-format
msgid "API Test : <a href=\"%s\" target=\"_blank\">View the lead</a>"
msgstr "Test API : <a href=\"%s\" target=\"_blank\">Voir le lead</a>"

#: .././wpucontactforms_zohocrm.php:284
msgid "Save Changes"
msgstr "Sauvegarder"

#: .././wpucontactforms_zohocrm.php:280
#: .././wpucontactforms_zohocrm.php:337
msgid "An error occured while refreshing the token."
msgstr "Une erreur s’est produite lors de l’actualisation du token."

#: .././wpucontactforms_zohocrm.php:284
#: .././wpucontactforms_zohocrm.php:342
msgid "The response did not contain a token."
msgstr "La réponse ne contenait pas de token."

#: .././wpucontactforms_zohocrm.php:394
#: .././wpucontactforms_zohocrm.php:455
msgid "There was an error"
msgstr "Il y a eu une erreur"

#~ msgid "Test"
#~ msgstr "Test"

#~ msgid "Test connection"
#~ msgstr "Tester la connexion"
97 changes: 79 additions & 18 deletions wpucontactforms_zohocrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Plugin URI: https://github.com/WordPressUtilities/wpucontactforms_zohocrm
Update URI: https://github.com/WordPressUtilities/wpucontactforms_zohocrm
Description: Connect WPU Contact Forms to ZohoCRM
Version: 0.521
Version: 0.6.0
Author: darklg
Author URI: https://darklg.me/
Text Domain: wpucontactforms_zohocrm
Expand All @@ -25,13 +25,14 @@ class WPUContactFormsZohoCRM {
public $settings;
public $basecron;
public $settings_update;
private $plugin_version = '0.5.2';
private $plugin_version = '0.6.0';
private $plugin_settings = array(
'id' => 'wpucontactforms_zohocrm',
'name' => 'WPU Contact Forms - ZohoCRM'
);
private $messages = false;
private $api_version = 'v2';
private $target_object = 'Leads';
private $settings_obj;

public function __construct() {
Expand All @@ -51,16 +52,30 @@ public function plugins_loaded() {
# CUSTOM PAGE
$admin_pages = array(
'main' => array(
'has_form' => false,
'has_form' => true,
'icon_url' => 'dashicons-admin-generic',
'menu_name' => $this->plugin_settings['name'],
'name' => $this->plugin_settings['name'],
'settings_link' => true,
'settings_name' => __('Settings', 'wpucontactforms_zohocrm'),
'function_content' => array(&$this,
'page_content__main'
),
'function_action' => array(&$this,
'page_action__main'
)

),
'settings' => array(
'has_form' => false,
'parent' => 'main',
'menu_name' => __('Settings', 'wpucontactforms_zohocrm'),
'name' => __('Settings', 'wpucontactforms_zohocrm'),
'function_content' => array(&$this,
'page_content__settings'
)
)

);
$pages_options = array(
'id' => $this->plugin_settings['id'],
Expand Down Expand Up @@ -111,14 +126,17 @@ public function plugins_loaded() {
)
),
'access_token' => array(
'readonly' => 'readonly',
'label' => __('Access token', 'wpucontactforms_zohocrm'),
'section' => 'token'
),
'expires_in' => array(
'readonly' => 'readonly',
'label' => __('Token expiration', 'wpucontactforms_zohocrm'),
'section' => 'token'
),
'last_update' => array(
'readonly' => 'readonly',
'label' => __('Last update', 'wpucontactforms_zohocrm'),
'section' => 'token'
),
Expand Down Expand Up @@ -209,20 +227,60 @@ public function page_content__main() {
echo '<div class="notice notice-error"><p>' . __('No application installed', 'wpucontactforms_zohocrm') . '</p></div>';
echo '<p>' . sprintf(__('Please <a target="_blank" href="%s">create an new Server-based application here</a> and specify the following redirect_uri : <br /><strong contenteditable>%s</strong>', 'wpucontactforms_zohocrm'), $api_create_url, $this->redirect_uri) . '</p>';
echo '<p>' . __('Get the client ID and client Secret and paste it below.', 'wpucontactforms_zohocrm') . '</p>';
echo '<hr />';
} elseif ($token_validity == 'expired-token') {
echo $refresh_token_link;
echo '<hr />';
} elseif ($token_validity != 'valid') {
echo '<p><a class="button-primary" href="' . $connect_url . '">' . __('Connect to your account', 'wpucontactforms_zohocrm') . '</a></p>';
echo '<hr />';
} else {
echo '<div class="notice updated"><p>' . __('Connection seems to work', 'wpucontactforms_zohocrm') . '</p></div>';
echo $refresh_token_link;
echo '<hr />';
}
echo '<hr />';

if ($token_validity == 'valid') {
echo '<h3>' . __('Test the API', 'wpucontactforms_zohocrm') . '</h3>';
echo '<p>' . __('Create or update a test lead.', 'wpucontactforms_zohocrm') . '</p>';
submit_button(__('Test the API', 'wpucontactforms_zohocrm'), 'primary', 'test_connection', false);
}
}

}

public function page_action__main() {
if (isset($_POST['test_connection'])) {
$urlparts = wp_parse_url(home_url());
$domain = isset($urlparts['host']) && $urlparts['host'] ? $urlparts['host'] : 'example.com';
$test_lead = $this->create_or_update_lead(array(
'First_Name' => 'Test',
'Last_Name' => 'Test',
'Email' => 'test_lead@' . $domain,
'Lead_Source' => 'Test - ' . $domain
), true);

$req = $this->build_request('/org', 'GET');
$org_details = json_decode(wp_remote_retrieve_body($req), true);
$base_org_url = 'https://crm.zoho.eu';
if (is_array($org_details) && isset($org_details['org'], $org_details['org'][0])) {
$account_server = $this->settings_obj->get_setting('accounts_server');
if ($account_server == 'https://accounts.zoho.com') {
$base_org_url = 'https://crm.zoho.com';
}
$base_org_url .= '/crm/' . $org_details['org'][0]['domain_name'] . '/tab/' . $this->target_object . '/';
}

if (is_array($test_lead) && isset($test_lead['data'], $test_lead['data'][0])) {
$base_org_url .= $test_lead['data'][0]['details']['id'];
}

$this->set_message('test_connection', sprintf(__('API Test : <a href="%s" target="_blank">View the lead</a>', 'wpucontactforms_zohocrm'), $base_org_url), 'updated');

}
}

/* Settings
-------------------------- */

public function page_content__settings() {
echo '<form action="' . admin_url('options.php') . '" method="post">';
settings_fields($this->settings_details['option_id']);
do_settings_sections($this->settings_details['plugin_id']);
Expand Down Expand Up @@ -292,7 +350,7 @@ public function refresh_token() {
$this->update_tokens_from_response($output_body);
}

public function create_or_update_lead($data = array()) {
public function create_or_update_lead($data = array(), $return_data = false) {
if (!is_array($data)) {
$data = array();
}
Expand All @@ -317,28 +375,28 @@ public function create_or_update_lead($data = array()) {

/* If an email exists : try to update record */
if (isset($data['Email']) && is_email($data['Email']) && $update_records) {
$req_search_lead = $this->build_request('/Leads/search?criteria=(Email:equals:' . urlencode($data['Email']) . ')', 'GET');
$req_search_lead = $this->build_request('/' . $this->target_object . '/search?criteria=(Email:equals:' . urlencode($data['Email']) . ')', 'GET');

if (!is_wp_error($req_search_lead)) {
$lead_details = json_decode(wp_remote_retrieve_body($req_search_lead), true);
if (is_array($lead_details) && isset($lead_details['data'], $lead_details['data'][0])) {
$data['id'] = $lead_details['data'][0]['id'];
return $this->update_lead($data);
return $this->update_lead($data, $return_data);
}
}
}

return $this->create_lead($data);
return $this->create_lead($data, $return_data);
}

private function update_lead($data) {
$req = $this->build_request('/Leads', 'PUT', $data);
return $this->api_is_successful_req($req);
private function update_lead($data, $return_data = false) {
$req = $this->build_request('/' . $this->target_object, 'PUT', $data);
return $this->api_is_successful_req($req, $return_data);
}

private function create_lead($data) {
$req = $this->build_request('/Leads', 'POST', $data);
return $this->api_is_successful_req($req);
private function create_lead($data, $return_data = false) {
$req = $this->build_request('/' . $this->target_object, 'POST', $data);
return $this->api_is_successful_req($req, $return_data);
}

public function build_request($endpoint, $method, $data = array()) {
Expand All @@ -365,13 +423,16 @@ public function build_request($endpoint, $method, $data = array()) {
return $req;
}

public function api_is_successful_req($req) {
public function api_is_successful_req($req, $return_data = false) {
if (is_wp_error($req)) {
error_log('Error : ' . $req->get_error_message());
return false;
}
$req_body = wp_remote_retrieve_body($req);
$req_details = json_decode($req_body, true);
if ($return_data) {
return $req_details;
}
return (is_array($req_details) && isset($req_details['data'], $req_details['data'][0]));
}

Expand Down

0 comments on commit d1bb7ee

Please sign in to comment.