From f7ce432e4d187cb6121f16c249f09110e6f663c3 Mon Sep 17 00:00:00 2001 From: Nilesh Choudhary Date: Fri, 13 Sep 2019 15:39:36 +0530 Subject: [PATCH 1/6] [NEW] - Mautic new connection option with Username and Password added. --- assets/css/bsfm-cnt-admin.css | 3 +- assets/js/bsfm-cnt-admin-script.js | 39 +++ classes/class-bsf-cm-auto-update.php | 81 ++++++ contacts-in-mautic.php | 408 ++++++++++++++++++++------- 4 files changed, 421 insertions(+), 110 deletions(-) create mode 100644 assets/js/bsfm-cnt-admin-script.js create mode 100644 classes/class-bsf-cm-auto-update.php diff --git a/assets/css/bsfm-cnt-admin.css b/assets/css/bsfm-cnt-admin.css index b23f53f..1fba1d2 100644 --- a/assets/css/bsfm-cnt-admin.css +++ b/assets/css/bsfm-cnt-admin.css @@ -8,4 +8,5 @@ background-color: #b4b9be; padding: 3px 6px; color: #fff; -} \ No newline at end of file +} + diff --git a/assets/js/bsfm-cnt-admin-script.js b/assets/js/bsfm-cnt-admin-script.js new file mode 100644 index 0000000..982420a --- /dev/null +++ b/assets/js/bsfm-cnt-admin-script.js @@ -0,0 +1,39 @@ +(function( $ ) { + +var ContactInMautic = { + + /** + * Initializes the services logic. + * + * @return void + * @since x.x.x + */ + init: function() + { + $( document ).on( 'change', '.bsfm-mautic-type', this._changeMauticType ); + }, + + _changeMauticType: function() { + var val = $( this ).val(); + if( val != 'mautic_api' ) { + $( '.contacts-in-mautic-text-bsfm-username' ).show(); + $( '.contacts-in-mautic-text-bsfm-password' ).show(); + + $( '.contacts-in-mautic-text-bsfm-public-key' ).hide(); + $( '.contacts-in-mautic-text-bsfm-secret-key' ).hide(); + } else { + $( '.contacts-in-mautic-text-bsfm-username' ).hide(); + $( '.contacts-in-mautic-text-bsfm-password' ).hide(); + + $( '.contacts-in-mautic-text-bsfm-public-key' ).show(); + $( '.contacts-in-mautic-text-bsfm-secret-key' ).show(); + } + } + }; + + + $ ( function() { + ContactInMautic.init(); + }); + +})(jQuery); \ No newline at end of file diff --git a/classes/class-bsf-cm-auto-update.php b/classes/class-bsf-cm-auto-update.php new file mode 100644 index 0000000..e70ff66 --- /dev/null +++ b/classes/class-bsf-cm-auto-update.php @@ -0,0 +1,81 @@ +expires_in; - $credentials['access_token'] = $access_details->access_token; - $credentials['expires_in'] = $expiration; - $credentials['refresh_token'] = $access_details->refresh_token; - update_option( '_bsf_mautic_cnt_credentials', $credentials ); - } - } // refresh code token ends - $credentials = get_option( '_bsf_mautic_cnt_credentials' ); - $access_token = isset( $credentials['access_token'] ) ? $credentials['access_token'] : ''; - $response = ''; + $get_mautic_connect_type = get_option( 'bsf_mautic_connection_type' ); - if ( ! empty( $access_token ) ) { + // Get Connection type and check. - if( $atts['anonymous'] == 'on' ) { - $url = $credentials['baseUrl'] . '/api/contacts?access_token=' . $access_token; - } - else { - $url = $credentials['baseUrl'] . '/api/contacts?search=!is:anonymous&access_token=' . $access_token; - } - $response = wp_remote_get( $url ); - if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { - $response_body = $response['body']; - $contacts_details = json_decode( $response_body ); + if ( 'mautic_user_pass' == $get_mautic_connect_type ) { + + $credentials = get_option( '_bsf_mautic_cnt_user_pass_credentials' ); + $response = bsfm_connect_mautic_username_password( $credentials ); + $response_body = $response['body']; + $contacts_details = $response_body; + + } else { + $credentials = get_option( '_bsf_mautic_cnt_credentials' ); + // if token expired, get new access token + if ( isset( $credentials['expires_in'] ) && $credentials['expires_in'] < time() ) { + $grant_type = 'refresh_token'; + $response = bsf_mautic_get_access_token( $grant_type ); + + if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) { + $access_details = wp_remote_retrieve_body( $response ); + $expiration = time() + $access_details->expires_in; + $credentials['access_token'] = $access_details->access_token; + $credentials['expires_in'] = $expiration; + $credentials['refresh_token'] = $access_details->refresh_token; + update_option( '_bsf_mautic_cnt_credentials', $credentials ); + } + } // refresh code token ends + + $credentials = get_option( '_bsf_mautic_cnt_credentials' ); + $access_token = isset( $credentials['access_token'] ) ? $credentials['access_token'] : ''; + $response = ''; + + if ( ! empty( $access_token ) ) { + + if( $atts['anonymous'] == 'on' ) { + $url = $credentials['baseUrl'] . '/api/contacts?access_token=' . $access_token; + } + else { + $url = $credentials['baseUrl'] . '/api/contacts?search=!is:anonymous&access_token=' . $access_token; + } + $response = wp_remote_get( $url ); + if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { + $response_body = $response['body']; + $contacts_details = json_decode( $response_body ); + } } + } if ( isset( $contacts_details->total ) ) { @@ -130,15 +156,37 @@ function bsf_mautic_contact_setting_page() { + +

@@ -149,36 +197,90 @@ class="contacts-in-mautic-text"/>

- - - -
-

- + + + + +
+

+ +

- + + + + + +
-

- +

+
-

- this article for more information.', 'contacts-in-mautic' ); ?> -

-

- - -

- + + + + + + + + + + + + +
+

+ +
+ +
+

+ +
+

+ this article for more information.', 'contacts-in-mautic' ); ?> +

+ +

+ + +

+ + if( get_option( '_bsf_mautic_cnt_user_pass_credentials' ) || isset( $credentials['access_token'] ) ) { ?>

@@ -251,57 +353,84 @@ function bsf_cnt_authenticate_update() { $mautic_base_url = rtrim( $mautic_base_url ,"/"); $bsfm['bsfm-base-url'] = esc_url( $mautic_base_url ); } - if ( isset( $_POST['bsfm-public-key'] ) ) { - $bsfm['bsfm-public-key'] = sanitize_key( $_POST['bsfm-public-key'] ); - } - if ( isset( $_POST['bsfm-secret-key'] ) ) { - $bsfm['bsfm-secret-key'] = sanitize_key( $_POST['bsfm-secret-key'] ); - } - // Update the site-wide option since we're in the network admin. - if ( is_network_admin() ) { - update_site_option( '_bsf_mautic_cnt_config', $bsfm ); + + if( isset( $_POST['bsfm_mautic_type'] ) && ( 'mautic_user_pass' == $_POST['bsfm_mautic_type'] ) ) { + + if ( isset( $_POST['bsfm-username'] ) ) { + $bsfm['bsfm-username'] = sanitize_key( $_POST['bsfm-username'] ); + } + if ( isset( $_POST['bsfm-password'] ) ) { + $bsfm['bsfm-password'] = $_POST['bsfm-password']; + } + + $connect = bsfm_connect_mautic_username_password( $bsfm ); + + if ( '' != $connect['error'] || !empty( $connect['error'] ) ) { + update_option( 'mautic_user_pass_error_msg', $connect['error'] ); + } else { + update_option( 'mautic_user_pass_error_msg', $connect['error'] ); + } + + update_option( 'bsf_mautic_connection_type', 'mautic_user_pass' ); + update_option( '_bsf_mautic_cnt_user_pass_credentials', $bsfm ); + } else { - update_option( '_bsf_mautic_cnt_config', $bsfm ); - } - } - $mautic_api_url = $bsfm_public_key = $bsfm_secret_key = ''; - $post = $_POST; - $cpts_err = false; - $lists = null; - $ref_list_id = null; - $mautic_api_url = isset( $post['bsfm-base-url'] ) ? esc_attr( $post['bsfm-base-url'] ) : ''; - $bsfm_public_key = isset( $post['bsfm-public-key'] ) ? esc_attr( $post['bsfm-public-key'] ) : ''; - $bsfm_secret_key = isset( $post['bsfm-secret-key'] ) ? esc_attr( $post['bsfm-secret-key'] ) : ''; - $mautic_api_url = rtrim( $mautic_api_url ,"/"); - if ( $mautic_api_url == '' ) { - $status = 'error'; - _e( 'API URL is missing.', 'contacts-in-mautic' ); - $cpts_err = true; - } - if ( $bsfm_secret_key == '' ) { - $status = 'error'; - _e( 'Secret Key is missing.', 'contacts-in-mautic' ); - $cpts_err = true; + if ( isset( $_POST['bsfm-public-key'] ) ) { + $bsfm['bsfm-public-key'] = sanitize_key( $_POST['bsfm-public-key'] ); + } + if ( isset( $_POST['bsfm-secret-key'] ) ) { + $bsfm['bsfm-secret-key'] = sanitize_key( $_POST['bsfm-secret-key'] ); + } + // Update the site-wide option since we're in the network admin. + if ( is_network_admin() ) { + update_site_option( '_bsf_mautic_cnt_config', $bsfm ); + } else { + update_option( '_bsf_mautic_cnt_config', $bsfm ); + } + $mautic_api_url = $bsfm_public_key = $bsfm_secret_key = ''; + $post = $_POST; + $cpts_err = false; + $lists = null; + $ref_list_id = null; + $mautic_api_url = isset( $post['bsfm-base-url'] ) ? esc_attr( $post['bsfm-base-url'] ) : ''; + $bsfm_public_key = isset( $post['bsfm-public-key'] ) ? esc_attr( $post['bsfm-public-key'] ) : ''; + $bsfm_secret_key = isset( $post['bsfm-secret-key'] ) ? esc_attr( $post['bsfm-secret-key'] ) : ''; + $mautic_api_url = rtrim( $mautic_api_url ,"/"); + if ( $mautic_api_url == '' ) { + $status = 'error'; + _e( 'API URL is missing.', 'contacts-in-mautic' ); + $cpts_err = true; + } + if ( $bsfm_secret_key == '' ) { + $status = 'error'; + _e( 'Secret Key is missing.', 'contacts-in-mautic' ); + $cpts_err = true; + } + $settings = array( + 'baseUrl' => $mautic_api_url, + 'version' => 'OAuth2', + 'clientKey' => $bsfm_public_key, + 'clientSecret' => $bsfm_secret_key, + 'callback' => admin_url( 'options-general.php?page=mautic-count' ), + 'response_type' => 'code' + ); + + update_option( '_bsf_mautic_cnt_credentials', $settings ); + update_option( 'bsf_mautic_connection_type', 'mautic_api' ); + + $authurl = $settings['baseUrl'] . '/oauth/v2/authorize'; + //OAuth 2.0 + $authurl .= '?client_id=' . $settings['clientKey'] . '&redirect_uri=' . urlencode( $settings['callback'] ); + $state = md5( time() . mt_rand() ); + $authurl .= '&state=' . $state; + $authurl .= '&response_type=' . $settings['response_type']; + + wp_redirect( $authurl ); + exit(); + } } - $settings = array( - 'baseUrl' => $mautic_api_url, - 'version' => 'OAuth2', - 'clientKey' => $bsfm_public_key, - 'clientSecret' => $bsfm_secret_key, - 'callback' => admin_url( 'options-general.php?page=mautic-count' ), - 'response_type' => 'code' - ); - update_option( '_bsf_mautic_cnt_credentials', $settings ); - $authurl = $settings['baseUrl'] . '/oauth/v2/authorize'; - //OAuth 2.0 - $authurl .= '?client_id=' . $settings['clientKey'] . '&redirect_uri=' . urlencode( $settings['callback'] ); - $state = md5( time() . mt_rand() ); - $authurl .= '&state=' . $state; - $authurl .= '&response_type=' . $settings['response_type']; - wp_redirect( $authurl ); - exit(); } function bsf_get_mautic_data() { @@ -328,4 +457,65 @@ function bsf_get_mautic_data() { $credentials['refresh_token'] = $access_details->refresh_token; update_option( '_bsf_mautic_cnt_credentials', $credentials ); } -} \ No newline at end of file +} + + /** + * Connect BSF Mautic with Username and Password + * + * @param array $data form data + * @return array + * @since 1.0.3 + */ + function bsfm_connect_mautic_username_password( $data ) { + + $mautic_response = array( 'error' => '', 'body' => '' ); + + $mautic_base_url = $data['bsfm-base-url']; + $mautic_username = $data['bsfm-username']; + $mautic_password = wp_unslash( $data['bsfm-password'] ); + + $auth_key = base64_encode($mautic_username . ':' . $mautic_password); + + $params = array( + 'timeout' => 30, + 'httpversion' => '1.1', + 'headers' => array( + 'Authorization' => 'Basic ' . $auth_key + ) + ); + + $request = $mautic_base_url.'/api/contacts'; + $response = wp_remote_get( $request, $params ); + + if( is_wp_error( $response ) ) { + $mautic_response['error'] = __( 'There appears to be an error with the configuration.', 'convertpro-addon' ); + return $mautic_response; + } + + $body = json_decode( wp_remote_retrieve_body( $response ) ); + + if( isset( $body->errors ) ) { + + if( $body->errors[0]->code == 404 ) { + /* translators: %s Error Message */ + $mautic_response['error'] = sprintf( __( '404 error. This sometimes happens when you\'ve just enabled the API, and your cache needs to be rebuilt. See here for more info - %s', 'convertpro-addon' ), $body->errors[0]->message ); + + return $mautic_response; + + } elseif( $body->errors[0]->code == 403 ) { + /* translators: %s Error Message */ + $mautic_response['error'] = sprintf( __( '403 error. You need to enable the API from within Mautic\'s configuration settings to connect. - %s', 'convertpro-addon' ), $body->errors[0]->message ); + + return $mautic_response; + + } else { + /* translators: %s Error Message */ + $mautic_response['error'] = sprintf( __( '%s - %s', 'convertpro-addon' ), $body->errors[0]->code, $body->errors[0]->message ); + + return $mautic_response; + } + } + $mautic_response['body'] = $body; + + return $mautic_response; + } From 219703cfac9f248432a5cfadc3f7bc0e5a9d6358 Mon Sep 17 00:00:00 2001 From: Nilesh Choudhary Date: Mon, 16 Sep 2019 16:02:54 +0530 Subject: [PATCH 2/6] [FIX] - Anonymous attribute added. --- contacts-in-mautic.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/contacts-in-mautic.php b/contacts-in-mautic.php index 83e15db..2280b61 100644 --- a/contacts-in-mautic.php +++ b/contacts-in-mautic.php @@ -91,7 +91,13 @@ function bsf_mautic_cnt_scode( $bsf_atts ) { if ( 'mautic_user_pass' == $get_mautic_connect_type ) { $credentials = get_option( '_bsf_mautic_cnt_user_pass_credentials' ); - $response = bsfm_connect_mautic_username_password( $credentials ); + if( $atts['anonymous'] == 'on' ) { + $response = bsfm_connect_mautic_username_password( $credentials , 'on' ); + } + else { + $response = bsfm_connect_mautic_username_password( $credentials , 'off' ); + } + $response_body = $response['body']; $contacts_details = $response_body; @@ -363,7 +369,7 @@ function bsf_cnt_authenticate_update() { $bsfm['bsfm-password'] = $_POST['bsfm-password']; } - $connect = bsfm_connect_mautic_username_password( $bsfm ); + $connect = bsfm_connect_mautic_username_password( $bsfm, 'off' ); if ( '' != $connect['error'] || !empty( $connect['error'] ) ) { update_option( 'mautic_user_pass_error_msg', $connect['error'] ); @@ -466,7 +472,7 @@ function bsf_get_mautic_data() { * @return array * @since 1.0.3 */ - function bsfm_connect_mautic_username_password( $data ) { + function bsfm_connect_mautic_username_password( $data , $anonymous ) { $mautic_response = array( 'error' => '', 'body' => '' ); @@ -484,7 +490,11 @@ function bsfm_connect_mautic_username_password( $data ) { ) ); - $request = $mautic_base_url.'/api/contacts'; + if ( $anonymous == 'on' ) { + $request = $mautic_base_url.'/api/contacts'; + } else { + $request = $mautic_base_url.'/api/contacts?search=!is:anonymous'; + } $response = wp_remote_get( $request, $params ); if( is_wp_error( $response ) ) { From 1c5a6a3de6a138831befcab9cee8fac2386797b1 Mon Sep 17 00:00:00 2001 From: Nilesh Choudhary Date: Mon, 16 Sep 2019 19:34:43 +0530 Subject: [PATCH 3/6] [FIX] - Version number changed, strict condition checked and get_instance removed. --- classes/class-bsf-cm-auto-update.php | 24 +++--------------------- contacts-in-mautic.php | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/classes/class-bsf-cm-auto-update.php b/classes/class-bsf-cm-auto-update.php index e70ff66..2bdd90a 100644 --- a/classes/class-bsf-cm-auto-update.php +++ b/classes/class-bsf-cm-auto-update.php @@ -18,31 +18,13 @@ class Bsf_CM_Auto_Update { - /** - * Class instance. - * - * @access private - * @var $instance Class instance. - */ - private static $instance; - - /** - * Initiator - */ - public static function get_instance() { - if ( ! isset( self::$instance ) ) { - self::$instance = new self(); - } - return self::$instance; - } - /** * Constructor */ public function __construct() { // Plugin Updates. - add_action( 'init', __CLASS__ . '::init' ); + add_action( 'admin_init', __CLASS__ . '::init' ); } /** @@ -76,6 +58,6 @@ public static function init() { endif; /** - * calling 'get_instance()' method + * calling 'Bsf_CM_Auto_Update' Constructor */ -Bsf_CM_Auto_Update::get_instance(); +new Bsf_CM_Auto_Update(); \ No newline at end of file diff --git a/contacts-in-mautic.php b/contacts-in-mautic.php index 2280b61..3332f17 100644 --- a/contacts-in-mautic.php +++ b/contacts-in-mautic.php @@ -34,7 +34,7 @@ die; } // Set the BSF_CONTACT_MAUTIC_VERSION. -define('BSF_CONTACT_MAUTIC_VERSION', '1.0.3'); +define('BSF_CONTACT_MAUTIC_VERSION', '1.0.2'); // Include the auto update file. require_once plugin_dir_path( __FILE__ ) .'classes/class-bsf-cm-auto-update.php'; @@ -88,10 +88,10 @@ function bsf_mautic_cnt_scode( $bsf_atts ) { // Get Connection type and check. - if ( 'mautic_user_pass' == $get_mautic_connect_type ) { + if ( 'mautic_user_pass' === $get_mautic_connect_type ) { $credentials = get_option( '_bsf_mautic_cnt_user_pass_credentials' ); - if( $atts['anonymous'] == 'on' ) { + if( 'on' === $atts['anonymous'] ) { $response = bsfm_connect_mautic_username_password( $credentials , 'on' ); } else { @@ -124,7 +124,7 @@ function bsf_mautic_cnt_scode( $bsf_atts ) { if ( ! empty( $access_token ) ) { - if( $atts['anonymous'] == 'on' ) { + if( 'on' === $atts['anonymous'] ) { $url = $credentials['baseUrl'] . '/api/contacts?access_token=' . $access_token; } else { @@ -171,7 +171,7 @@ function bsf_mautic_contact_setting_page() { // Get Connection type and check. - if ( 'mautic_api' == $get_mautic_connect_type ) { + if ( 'mautic_api' === $get_mautic_connect_type ) { $bsfm = get_option( '_bsf_mautic_cnt_config' ); $bsfm_base_url = $bsfm_public_key = $bsfm_secret_key = ''; @@ -209,7 +209,7 @@ class="contacts-in-mautic-text"/> @@ -254,7 +254,7 @@ class="contacts-in-mautic-text"/> Date: Tue, 17 Sep 2019 15:35:12 +0530 Subject: [PATCH 4/6] Files indentation. --- classes/class-bsf-cm-auto-update.php | 66 ++++++------- contacts-in-mautic.php | 133 +++++++++++++-------------- 2 files changed, 94 insertions(+), 105 deletions(-) diff --git a/classes/class-bsf-cm-auto-update.php b/classes/class-bsf-cm-auto-update.php index 2bdd90a..2e45c72 100644 --- a/classes/class-bsf-cm-auto-update.php +++ b/classes/class-bsf-cm-auto-update.php @@ -8,54 +8,48 @@ * @since 1.0.3 */ -if ( ! class_exists( 'Bsf_CM_Auto_Update' ) ) : +/** + * Bsf_CM_Auto_Update initial setup + * + * @since 1.0.3 + */ +class Bsf_CM_Auto_Update { /** - * Bsf_CM_Auto_Update initial setup + * Constructor + */ + public function __construct() { + // Plugin Updates. + add_action( 'admin_init', __CLASS__ . '::init' ); + } + + /** + * Implement plugin auto update logic. * * @since 1.0.3 + * @return void */ - class Bsf_CM_Auto_Update { - + public static function init() { - /** - * Constructor - */ - public function __construct() { + // Get auto saved version number. + $saved_version = get_option( 'bsf_contact_mautic_version' ); - // Plugin Updates. - add_action( 'admin_init', __CLASS__ . '::init' ); + // If the version option not present then just create it. + if ( false === $saved_version ) { + add_option( 'bsf_contact_mautic_version', BSF_CONTACT_MAUTIC_VERSION ); } - /** - * Implement plugin auto update logic. - * - * @since 1.0.3 - * @return void - */ - public static function init() { - - // Get auto saved version number. - $saved_version = get_option( 'bsf_contact_mautic_version' ); - - // If the version option not present then just create it. - if ( false === $saved_version ) { - add_option( 'bsf_contact_mautic_version', BSF_CONTACT_MAUTIC_VERSION ); - } - - // Set the Mautic connection type option. - $check_option = get_option( 'bsf_mautic_connection_type' ); - if ( false === $check_option ){ - add_option( 'bsf_mautic_connection_type', 'mautic_api' ); - } - - // Update auto saved version number. - update_option( 'bsf_contact_mautic_version', BSF_CONTACT_MAUTIC_VERSION ); + // Set the Mautic connection type option. + $check_option = get_option( 'bsf_mautic_connection_type' ); + if ( false === $check_option ){ + add_option( 'bsf_mautic_connection_type', 'mautic_api' ); } -} + // Update auto saved version number. + update_option( 'bsf_contact_mautic_version', BSF_CONTACT_MAUTIC_VERSION ); + } -endif; +} /** * calling 'Bsf_CM_Auto_Update' Constructor diff --git a/contacts-in-mautic.php b/contacts-in-mautic.php index 3332f17..b874ca5 100644 --- a/contacts-in-mautic.php +++ b/contacts-in-mautic.php @@ -103,7 +103,7 @@ function bsf_mautic_cnt_scode( $bsf_atts ) { } else { $credentials = get_option( '_bsf_mautic_cnt_credentials' ); - // if token expired, get new access token + // if token expired, get new access token if ( isset( $credentials['expires_in'] ) && $credentials['expires_in'] < time() ) { $grant_type = 'refresh_token'; $response = bsf_mautic_get_access_token( $grant_type ); @@ -125,10 +125,9 @@ function bsf_mautic_cnt_scode( $bsf_atts ) { if ( ! empty( $access_token ) ) { if( 'on' === $atts['anonymous'] ) { - $url = $credentials['baseUrl'] . '/api/contacts?access_token=' . $access_token; - } - else { - $url = $credentials['baseUrl'] . '/api/contacts?search=!is:anonymous&access_token=' . $access_token; + $url = $credentials['baseUrl'] . '/api/contacts?access_token=' . $access_token; + } else { + $url = $credentials['baseUrl'] . '/api/contacts?search=!is:anonymous&access_token=' . $access_token; } $response = wp_remote_get( $url ); if ( ! is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) === 200 ) { @@ -136,7 +135,6 @@ function bsf_mautic_cnt_scode( $bsf_atts ) { $contacts_details = json_decode( $response_body ); } } - } if ( isset( $contacts_details->total ) ) { @@ -180,19 +178,18 @@ function bsf_mautic_contact_setting_page() { $bsfm_public_key = ( array_key_exists( 'bsfm-public-key', $bsfm ) ) ? $bsfm['bsfm-public-key'] : ''; $bsfm_secret_key = ( array_key_exists( 'bsfm-secret-key', $bsfm ) ) ? $bsfm['bsfm-secret-key'] : ''; } - }else{ + } else { $bsfm = get_option( '_bsf_mautic_cnt_user_pass_credentials' ); $bsfm_base_url = $bsfm_username = $bsfm_password = ''; if ( is_array( $bsfm ) ) { - $bsfm_base_url = ( array_key_exists( 'bsfm-base-url', $bsfm ) ) ? $bsfm['bsfm-base-url'] : ''; - $bsfm_username = ( array_key_exists( 'bsfm-username', $bsfm ) ) ? $bsfm['bsfm-username'] : ''; - $bsfm_password = ( array_key_exists( 'bsfm-password', $bsfm ) ) ? $bsfm['bsfm-password'] : ''; + $bsfm_base_url = ( array_key_exists( 'bsfm-base-url', $bsfm ) ) ? $bsfm['bsfm-base-url'] : ''; + $bsfm_username = ( array_key_exists( 'bsfm-username', $bsfm ) ) ? $bsfm['bsfm-username'] : ''; + $bsfm_password = ( array_key_exists( 'bsfm-password', $bsfm ) ) ? $bsfm['bsfm-password'] : ''; } } ?> -

@@ -311,7 +308,6 @@ function bsf_mautic_get_access_token( $grant_type ) { $credentials = get_option( '_bsf_mautic_cnt_credentials' ); if ( ! isset( $credentials['baseUrl'] ) ) { - return; } @@ -349,7 +345,6 @@ function bsf_mautic_get_access_token( $grant_type ) { function bsf_cnt_authenticate_update() { if ( ! isset( $_POST['bsf-mautic-cnt-nonce'] ) || isset( $_POST['bsfm-cnt-disconnect-mautic'] ) ) { - return; } @@ -388,7 +383,7 @@ function bsf_cnt_authenticate_update() { if ( isset( $_POST['bsfm-secret-key'] ) ) { $bsfm['bsfm-secret-key'] = sanitize_key( $_POST['bsfm-secret-key'] ); } - // Update the site-wide option since we're in the network admin. + // Update the site-wide option since we're in the network admin. if ( is_network_admin() ) { update_site_option( '_bsf_mautic_cnt_config', $bsfm ); } else { @@ -402,7 +397,7 @@ function bsf_cnt_authenticate_update() { $mautic_api_url = isset( $post['bsfm-base-url'] ) ? esc_attr( $post['bsfm-base-url'] ) : ''; $bsfm_public_key = isset( $post['bsfm-public-key'] ) ? esc_attr( $post['bsfm-public-key'] ) : ''; $bsfm_secret_key = isset( $post['bsfm-secret-key'] ) ? esc_attr( $post['bsfm-secret-key'] ) : ''; - $mautic_api_url = rtrim( $mautic_api_url ,"/"); + $mautic_api_url = rtrim( $mautic_api_url ,"/"); if ( $mautic_api_url == '' ) { $status = 'error'; _e( 'API URL is missing.', 'contacts-in-mautic' ); @@ -426,7 +421,7 @@ function bsf_cnt_authenticate_update() { update_option( 'bsf_mautic_connection_type', 'mautic_api' ); $authurl = $settings['baseUrl'] . '/oauth/v2/authorize'; - //OAuth 2.0 + //OAuth 2.0 $authurl .= '?client_id=' . $settings['clientKey'] . '&redirect_uri=' . urlencode( $settings['callback'] ); $state = md5( time() . mt_rand() ); $authurl .= '&state=' . $state; @@ -465,67 +460,67 @@ function bsf_get_mautic_data() { } } - /** - * Connect BSF Mautic with Username and Password - * - * @param array $data form data - * @return array - * @since 1.0.3 - */ - function bsfm_connect_mautic_username_password( $data , $anonymous ) { - - $mautic_response = array( 'error' => '', 'body' => '' ); - - $mautic_base_url = $data['bsfm-base-url']; - $mautic_username = $data['bsfm-username']; - $mautic_password = wp_unslash( $data['bsfm-password'] ); - - $auth_key = base64_encode($mautic_username . ':' . $mautic_password); - - $params = array( - 'timeout' => 30, - 'httpversion' => '1.1', - 'headers' => array( - 'Authorization' => 'Basic ' . $auth_key - ) - ); - - if ( 'on' === $anonymous ) { - $request = $mautic_base_url.'/api/contacts'; - } else { - $request = $mautic_base_url.'/api/contacts?search=!is:anonymous'; - } - $response = wp_remote_get( $request, $params ); +/** + * Connect BSF Mautic with Username and Password + * + * @param array $data form data + * @return array + * @since 1.0.3 + */ +function bsfm_connect_mautic_username_password( $data , $anonymous ) { - if( is_wp_error( $response ) ) { - $mautic_response['error'] = __( 'There appears to be an error with the configuration.', 'convertpro-addon' ); - return $mautic_response; - } + $mautic_response = array( 'error' => '', 'body' => '' ); - $body = json_decode( wp_remote_retrieve_body( $response ) ); + $mautic_base_url = $data['bsfm-base-url']; + $mautic_username = $data['bsfm-username']; + $mautic_password = wp_unslash( $data['bsfm-password'] ); - if( isset( $body->errors ) ) { + $auth_key = base64_encode($mautic_username . ':' . $mautic_password); - if( $body->errors[0]->code == 404 ) { - /* translators: %s Error Message */ - $mautic_response['error'] = sprintf( __( '404 error. This sometimes happens when you\'ve just enabled the API, and your cache needs to be rebuilt. See here for more info - %s', 'convertpro-addon' ), $body->errors[0]->message ); + $params = array( + 'timeout' => 30, + 'httpversion' => '1.1', + 'headers' => array( + 'Authorization' => 'Basic ' . $auth_key + ) + ); - return $mautic_response; + if ( 'on' === $anonymous ) { + $request = $mautic_base_url.'/api/contacts'; + } else { + $request = $mautic_base_url.'/api/contacts?search=!is:anonymous'; + } + $response = wp_remote_get( $request, $params ); - } elseif( $body->errors[0]->code == 403 ) { - /* translators: %s Error Message */ - $mautic_response['error'] = sprintf( __( '403 error. You need to enable the API from within Mautic\'s configuration settings to connect. - %s', 'convertpro-addon' ), $body->errors[0]->message ); + if( is_wp_error( $response ) ) { + $mautic_response['error'] = __( 'There appears to be an error with the configuration.', 'convertpro-addon' ); + return $mautic_response; + } - return $mautic_response; + $body = json_decode( wp_remote_retrieve_body( $response ) ); - } else { - /* translators: %s Error Message */ - $mautic_response['error'] = sprintf( __( '%s - %s', 'convertpro-addon' ), $body->errors[0]->code, $body->errors[0]->message ); + if( isset( $body->errors ) ) { - return $mautic_response; - } - } - $mautic_response['body'] = $body; + if( $body->errors[0]->code == 404 ) { + /* translators: %s Error Message */ + $mautic_response['error'] = sprintf( __( '404 error. This sometimes happens when you\'ve just enabled the API, and your cache needs to be rebuilt. See here for more info - %s', 'convertpro-addon' ), $body->errors[0]->message ); - return $mautic_response; + return $mautic_response; + + } elseif( $body->errors[0]->code == 403 ) { + /* translators: %s Error Message */ + $mautic_response['error'] = sprintf( __( '403 error. You need to enable the API from within Mautic\'s configuration settings to connect. - %s', 'convertpro-addon' ), $body->errors[0]->message ); + + return $mautic_response; + + } else { + /* translators: %s Error Message */ + $mautic_response['error'] = sprintf( __( '%s - %s', 'convertpro-addon' ), $body->errors[0]->code, $body->errors[0]->message ); + + return $mautic_response; + } } + $mautic_response['body'] = $body; + + return $mautic_response; +} \ No newline at end of file From 2fa98c87882e8846d3a1f5f0b6289f07314b47c8 Mon Sep 17 00:00:00 2001 From: Nikhil Chavan Date: Wed, 18 Sep 2019 13:51:53 +0530 Subject: [PATCH 5/6] Version bump to v1.0.3 --- README.md | 11 +++++++---- assets/js/bsfm-cnt-admin-script.js | 2 +- contacts-in-mautic.php | 4 ++-- readme.txt | 10 ++++++---- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2437a4f..9e736a5 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ **Contributors:** [brainstormforce](https://profiles.wordpress.org/brainstormforce) **Tags:** mautic, contacts, api, count, email **Requires at least:** 4.1 -**Stable tag:** 1.0.2 +**Stable tag:** 1.0.3 **Tested up to:** 5.2 Display your Mautic Contacts count on your website @@ -28,14 +28,17 @@ A very useful plugin to display your mautic contacts count on your website. This ## Changelog ## -# Version 1.0.2 +### Version 1.0.3 ### +- Improvement: Add Username/Password API authentication method which is more reliable. + +### Version 1.0.2 ### - Fix: Prevent fatal error when plugin incorrect response is received. -# Version 1.0.1 +### Version 1.0.1 ### - Improvement: Change the Cron to update contacts count to be executed every week rather than every day, making fewer requests to Mautic - Fix: Add correct comma separate formatting for the contacts count. -# Version 1.0.0 +### Version 1.0.0 ### - Initial Release. ## Installation ## diff --git a/assets/js/bsfm-cnt-admin-script.js b/assets/js/bsfm-cnt-admin-script.js index 982420a..21cedef 100644 --- a/assets/js/bsfm-cnt-admin-script.js +++ b/assets/js/bsfm-cnt-admin-script.js @@ -6,7 +6,7 @@ var ContactInMautic = { * Initializes the services logic. * * @return void - * @since x.x.x + * @since 1.0.3 */ init: function() { diff --git a/contacts-in-mautic.php b/contacts-in-mautic.php index b874ca5..c1adda4 100644 --- a/contacts-in-mautic.php +++ b/contacts-in-mautic.php @@ -3,7 +3,7 @@ * Plugin Name: Contacts in Mautic * Plugin URI: http://brainstormforce.com * Description: Get All Mautic Contacts Count using simple shortcode. - * Version: 1.0.2 + * Version: 1.0.3 * Author: Brainstormforce * Author URI: http://brainstormforce.com * License: GNU General Public License v2.0 @@ -34,7 +34,7 @@ die; } // Set the BSF_CONTACT_MAUTIC_VERSION. -define('BSF_CONTACT_MAUTIC_VERSION', '1.0.2'); +define('BSF_CONTACT_MAUTIC_VERSION', '1.0.3'); // Include the auto update file. require_once plugin_dir_path( __FILE__ ) .'classes/class-bsf-cm-auto-update.php'; diff --git a/readme.txt b/readme.txt index 4851bb2..1df9154 100644 --- a/readme.txt +++ b/readme.txt @@ -2,7 +2,7 @@ Contributors: brainstormforce Tags: mautic, contacts, api, count, email Requires at least: 4.1 -Stable tag: 1.0.2 +Stable tag: 1.0.3 Tested up to: 5.2 Display your Mautic Contacts count on your website @@ -28,15 +28,17 @@ A very useful plugin to display your mautic contacts count on your website. This == Changelog == -# Version 1.0.2 += Version 1.0.3 = +- Improvement: Add Username/Password API authentication method which is more reliable. += Version 1.0.2 = - Fix: Prevent fatal error when plugin incorrect response is received. -# Version 1.0.1 += Version 1.0.1 = - Improvement: Change the Cron to update contacts count to be executed every week rather than every day, making fewer requests to Mautic - Fix: Add correct comma separate formatting for the contacts count. -# Version 1.0.0 += Version 1.0.0 = - Initial Release. == Installation == From 2ba68cce302a3303252e83cc14b76f5792d0caf8 Mon Sep 17 00:00:00 2001 From: Nilesh Choudhary Date: Thu, 19 Sep 2019 15:36:06 +0530 Subject: [PATCH 6/6] If condition for version check added. --- classes/class-bsf-cm-auto-update.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/classes/class-bsf-cm-auto-update.php b/classes/class-bsf-cm-auto-update.php index 2e45c72..5732528 100644 --- a/classes/class-bsf-cm-auto-update.php +++ b/classes/class-bsf-cm-auto-update.php @@ -39,6 +39,11 @@ public static function init() { add_option( 'bsf_contact_mautic_version', BSF_CONTACT_MAUTIC_VERSION ); } + // If equals then return. + if ( version_compare( $saved_version, BSF_CONTACT_MAUTIC_VERSION, '=' ) ) { + return; + } + // Set the Mautic connection type option. $check_option = get_option( 'bsf_mautic_connection_type' ); if ( false === $check_option ){