data=$this->get_data_object();
global $pagenow;
if(in_array($pagenow, array("admin-ajax.php"))){
add_action('wp_ajax_update_feed_'.$this->id, array($this, 'update_feed'));
add_action('wp_ajax_update_feed_sort_'.$this->id, array($this, 'update_feed_sort'));
add_action('wp_ajax_get_field_map_'.$this->id, array($this, 'get_field_map_ajax'));
add_action('wp_ajax_get_field_map_object_'.$this->id, array($this, 'get_field_map_object_ajax'));
add_action('wp_ajax_get_objects_'.$this->id, array($this, 'get_objects_ajax'));
add_action('wp_ajax_log_detail_'.$this->id, array($this, 'log_detail'));
add_action('wp_ajax_refresh_data_'.$this->id, array($this, 'refresh_data'));
add_action('wp_ajax_send_to_crm_'.$this->id, array($this, 'send_to_crm'));
}
//crmperks forms
add_action( 'cfx_add_meta_box', array($this,'add_meta_box_crmperks_form'),10,2 );
add_action('cfx_form_entry_updated', array($this, 'update_entry_crm_perks_forms'),10,3);
add_action('cfx_form_post_note_added', array($this, 'create_note_crm_perks_forms'),10,3);
add_action('cfx_form_pre_note_deleted', array($this, 'delete_note_crm_perks_forms'),10,2);
add_action('cfx_form_pre_trash_leads', array($this, 'trash_leads_crm_perks_forms'),10,2);
add_action('cfx_form_pre_restore_leads', array($this, 'restore_leads_crm_perks_forms'),10,2);
if($this->is_crm_page()){
$base_url=$this->get_base_url();
wp_register_script( 'vxc-tooltip',$base_url. 'js/jquery.tipTip.js', array( 'jquery' ), $this->version, true );
wp_register_style('vxc-tooltip', $base_url. 'css/tooltip.css');
wp_register_style('vx-fonts', $base_url. 'css/font-awesome.min.css');
wp_register_style('vx-datepicker', $base_url. 'css/jquery-ui.min.css');
wp_register_script( 'vxg-select2',$base_url. 'js/select2.min.js', array( 'jquery' ), $this->version, true );
wp_register_style('vxg-select2', $base_url. 'css/select2.min.css',array(),array('ver'=>'1.0'));
wp_register_script( 'vx-sorter',$base_url. 'js/jquery.tablesorter.min.js', array( 'jquery' ), $this->version, true );
}
//creates the subnav left menu
add_filter("admin_menu", array($this, 'create_menu'), 60);
add_filter( 'vx_cf_meta_boxes_right', array($this,'add_meta_box'),10,3 );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2);
add_action('vxcf_entry_submit_btn', array($this, 'entry_checkbox'));
add_action('vx_cf7_post_note_added', array($this, 'create_note_e'),10,3);
add_action('vx_cf7_pre_note_deleted', array($this, 'delete_note_e'),10,2);
add_action('vx_cf7_pre_trash_leads', array($this, 'trash_leads_e'));
add_action('vx_cf7_pre_restore_leads', array($this, 'restore_leads_e'));
add_action('vx_cf7_entry_updated', array($this, 'update_entry_e'),10,3);
//
add_action('vx_contact_post_note_added', array($this, 'create_note_c'),10,3);
add_action('vx_contact_pre_note_deleted', array($this, 'delete_note_c'),10,2);
add_action('vx_contact_pre_trash_leads', array($this, 'trash_leads_c'));
add_action('vx_contact_pre_restore_leads', array($this, 'restore_leads_c'));
add_action('vx_contact_entry_updated', array($this, 'update_entry_c'),10,3);
add_filter('vx_callcenter_entries_action',array($this,'bulk_send_crm_callcenter'),10,4);
add_filter('vx_callcenter_bulk_actions',array($this,'add_bulk_send_crm_callcenter'));
$this->setup_plugin();
}
public function update_entry_crm_perks_forms($entry_id,$lead,$form){
$option=get_option($this->type.'_settings',array());
if( !empty($option['update']) ){
$event= 'update';
$lead['__vx_id']=$entry_id;
$form['id']='vf_'.$form['id'];
$form['cfx_type']='vf';
$push=$this->push($lead,$form,$event);
if(!empty($push['msg'])){
$this->screen_msg($push['msg'],$push['class']);
}
}
}
public function create_note_crm_perks_forms($id, $entry, $note){
$option=get_option($this->type.'_settings',array());
if(!empty($option['notes']) ){
if(!empty($entry['form_id'])){
$form=array('id'=>'vf_'.$entry['form_id']);
$entry['__vx_id']=$entry['id'];
$title=substr($note,0,100);
self::$note=array('id'=>$id,'body'=>$note,'title'=>$title);
$push=$this->push($entry,$form,'add_note');
}
}
}
public function delete_note_crm_perks_forms($note_id,$entry){
$option=get_option($this->type.'_settings',array());
if(!empty($option['notes'])){
if(!empty($entry['form_id'])){
$form=array('id'=>'vf_'.$entry['form_id']);
self::$note=array('id'=>$note_id);
$entry['__vx_id']=$entry['id'];
$push=$this->push($entry,$form,'delete_note');
}
}
}
public function trash_leads_crm_perks_forms($leads,$form_id){
$option=get_option($this->type.'_settings',array());
if(!empty($option['delete'])){
if(is_array($leads)){
$updated=$error='';
foreach($leads as $lead){
if(!empty($form_id)){
$entry=array('__vx_id'=>$lead);
$push=$this->push($entry,array('id'=>$form_id),'delete');
if(!empty($push['msg'])){
if($push['class'] == 'updated'){
$updated.=$push['msg'].' ';
}else{
$error.=$push['msg'].' ';
}
}
}
}
if(!empty($updated)){
$this->add_msg($updated,'updated');
}
if(!empty($error)){
$this->add_msg($error,'error');
}
}
}
}
public function restore_leads_crm_perks_forms($leads,$form_id){
$option=get_option($this->type.'_settings',array());
if(!empty($option['restore'])){
if(is_array($leads)){
$updated=$error='';
foreach($leads as $lead){
if( !empty($lead['id'])){
$lead['__vx_id']=$lead['id'];
$push=$this->push($lead,array('id'=>$form_id),'restore');
if(!empty($push['msg'])){
if($push['class'] == 'updated'){
$updated.=$push['msg'].' ';
}else{
$error.=$push['msg'].' ';
}
}
}
}
if(!empty($updated)){
$this->add_msg($updated,'updated');
}
if(!empty($error)){
$this->add_msg($error,'error');
}
}
}
// var_dump($leads); die();
}
public function add_bulk_send_crm_callcenter($list){
$list['send_hubspot']=__('Send to HubSpot','contact-form-hubspot-crm');
return $list;
}
public function bulk_send_crm_callcenter($msg,$action,$ids,$type){
if($action == 'send_hubspot'){
$entry=array(); $notice=''; $class='updated';
if(class_exists('vx_dialpad')){
$pad=new vx_dialpad();
$form=$pad->get_form($type);
if(is_array($ids)){
foreach($ids as $id){
$entry=$pad->get_entry($type,$id);
if(!empty($entry['id'])){
$entry['__vx_id']=$entry['id'];
$temp=$this->push($entry,$form,'',false);
if(!empty($temp['msg'])){
if(!empty($notice)){
$notice.=' ';
}
$notice.=$temp['msg'];
if($temp['class'] !='updated'){
$class=$temp['class'];
}
}
} }
}
}
if(empty($notice)){
$notice='Nothing Sent to HubSpot';
}
$msg=array('msg'=>$notice,'class'=>$class);
}
return $msg;
}
public function update_entry_e($entry,$entry_id,$lead){
$this->update_entry($entry,$entry_id,$lead);
}
public function update_entry_c($entry,$entry_id,$lead){
$this->update_entry($entry,$entry_id,$lead,'addon');
}
/**
* Send entry to crm on update
*
* @param mixed $form
* @param mixed $lead_id
*/
public function update_entry($entry,$entry_id,$lead,$type=''){
$option=get_option($this->type.'_settings',array());
//checkbox checked || auto send to crm on update
if( !empty($_POST[$this->id.'_send']) || ( !empty($option['update']) ) ){
// only update , if already sent to crm
//will in case of auto update option or send to crm checkbox
$event= 'update';
$entry['__vx_id']=$entry_id;
$entry['__vx_type']=$type;
$form=$this->get_form($lead['form_id']);
$push=$this->push($entry,$form,$event);
if(!empty($push['msg'])){
$this->screen_msg($push['msg'],$push['class']);
}
}
}
public function trash_leads_e($leads){
$this->trash_leads($leads);
}
public function trash_leads_c($leads){
$this->trash_leads($leads,'addon');
}
/**
* Delete entry from crm on deletion
*
* @param mixed $lead_id
*/
public function trash_leads($leads,$type=''){
$option=get_option($this->type.'_settings',array());
if(!empty($option['delete'])){
if(is_array($leads)){
$updated=$error='';
foreach($leads as $lead){
$entry=$this->get_cf_entry($lead,$type);
if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
$entry['__vx_id']=$lead;
$push=$this->push($entry,$form,'delete');
if(!empty($push['msg'])){
if($push['class'] == 'updated'){
$updated.=$push['msg'].' ';
}else{
$error.=$push['msg'].' ';
}
}
}
}
if(!empty($updated)){
$this->add_msg($updated,'updated');
}
if(!empty($error)){
$this->add_msg($error,'error');
}
}
}
// var_dump($leads); die();
}
public function restore_leads_e($leads){
$this->restore_leads($leads);
}
public function restore_leads_c($leads){
$this->restore_leads($leads,'addon');
}
/**
* Send entry to crm on restore
*
* @param mixed $lead_id
*/
public function restore_leads($leads,$type=''){
$option=get_option($this->type.'_settings',array());
if(!empty($option['restore'])){
if(is_array($leads)){
$updated=$error='';
foreach($leads as $lead){
$entry=$this->get_cf_entry($lead,$type);
if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
if($type == 'addon'){
$detail=$entry;
}else{
$detail=$this->get_cf_entry_detail($lead);
}
$detail['__vx_id']=$lead;
$push=$this->push($detail,$form,'restore');
if(!empty($push['msg'])){
if($push['class'] == 'updated'){
$updated.=$push['msg'].' ';
}else{
$error.=$push['msg'].' ';
}
}
}
}
if(!empty($updated)){
$this->add_msg($updated,'updated');
}
if(!empty($error)){
$this->add_msg($error,'error');
}
}
}
// var_dump($leads); die();
}
public function delete_note_e($note_id,$lead_id){
$this->delete_note($note_id,$lead_id);
}
public function delete_note_c($note_id,$lead_id){
$this->delete_note($note_id,$lead_id,'addon');
}
/**
* delete note from crm when deleted from GF entry
*
* @param mixed $note_id
* @param mixed $lead_id
*/
public function delete_note($note_id,$lead_id,$type=''){
$option=get_option($this->type.'_settings',array());
if(!empty($option['notes'])){
$entry=$this->get_cf_entry($lead_id,$type);
if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
self::$note=array('id'=>$note_id);
$entry['__vx_id']=$entry['id'];
$push=$this->push($entry,$form,'delete_note');
}
}
}
public function create_note_e($id, $lead_id, $note){
$this->create_note($id, $lead_id, $note);
}
public function create_note_c($id, $lead_id, $note){
$this->create_note($id, $lead_id, $note,'addon');
}
/**
* send entry note to crm
*
* @param mixed $id
* @param mixed $lead_id
* @param mixed $user_id
* @param mixed $user_name
* @param mixed $note
* @param mixed $note_type
*/
public function create_note($id, $lead_id, $note,$type=''){
$option=get_option($this->type.'_settings',array());
if(!empty($option['notes'])){
$entry=$this->get_cf_entry($lead_id,$type);
if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
if($type == 'addon'){
$entry_detail=$entry;
}else{
$entry_detail=$this->get_cf_entry_detail($lead_id);
}
$entry_detail['__vx_id']=$lead_id;
$title=substr($note,0,100);
self::$note=array('id'=>$id,'body'=>$note,'title'=>$title);
$push=$this->push($entry_detail,$form,'add_note');
}
}
}
public function entry_checkbox($lead){
?>
id.'_edit_settings');
if($debug){
echo "
".self::$debug_html."
";
self::$debug_html='';
}
if(!empty($_POST[$this->id.'_send_btn']) && !empty($_REQUEST['id'])){
$tab=$this->post('tab');
$id=$this->post('id');
$form=array('title'=>'Contact Form');
$lead=array();
if($tab == 'contacts'){
global $vxcf_crm;
if(method_exists($vxcf_crm,'get_entry')){
$lead=$vxcf_crm->get_entry($id);
$form['id']='vx_contacts';
}
}else{
$info=$this->get_cf_entry($id);
$lead=$this->get_cf_entry_detail($id);
if(!empty($info['form_id'])){
$form['id']=$info['form_id'];
}
}
$lead['__vx_id']=$id;
$push=$this->push($lead,$form);
if(!empty($push['msg'])){
$this->screen_msg($push['msg'],$push['class']);
}
}
//send to crm in order page message
$msgs=get_option($this->id.'_msgs');
if(is_array($msgs)){
foreach($msgs as $msg){
$this->screen_msg($msg['msg'],$msg['class']);
}
update_option($this->id.'_msgs','');
}
}
/**
* Add settings and support link
*
* @param mixed $links
* @param mixed $file
*/
public function plugin_action_links( $links, $file ) {
$slug=$this->get_slug();
if ( $file == $slug ) {
$settings_link=$this->link_to_settings();
array_unshift( $links, '' . esc_html__('Settings', 'contact-form-hubspot-crm') . '' );
}
return $links;
}
/**
* Creates left nav menu under Forms
*
* @param mixed $menus
*/
public function create_menu(){
// Adding submenu if user has access
$page_title =__('HubSpot for Contact Form','contact-form-hubspot-crm');
$menu_title =__('HubSpot','contact-form-hubspot-crm');
$capability = $this->id."_read_feeds";
$menu_id='vxcf_leads';
if(empty($GLOBALS['admin_page_hooks'][$menu_id])){
add_menu_page($page_title,$menu_title,$capability,$this->id,array( $this,'mapping_page'));
}else{
add_submenu_page('vxcf_leads',$page_title,$menu_title,$capability,$this->id,array( $this,'mapping_page'));
}
}
/**
* plugin admin features
*
*/
public function setup_plugin(){
global $wpdb;
if(isset($_REQUEST[$this->id.'_tab_action']) && $_REQUEST[$this->id.'_tab_action']=="get_code"){
$part=array('code'=>'');
if(isset($_REQUEST['code'])){
$part['code']=$this->post('code');
}
if(isset($_REQUEST['error'])){
$part['error']=$this->post('error');
$part['error_description']=$this->post('error_description');
}
$redir= urldecode($_REQUEST['state'])."&".http_build_query($part);
wp_safe_redirect($redir);
die();
}
if(isset($_REQUEST[$this->id.'_tab_action']) && $_REQUEST[$this->id.'_tab_action']=="del_account"){
check_admin_referer('vx_nonce','vx_nonce');
if( current_user_can($this->id."_edit_settings")){
$id=$this->post('id');
$data=$this->get_data_object();
$res=$data->del_account($id);
$class='';
if($res){
$msg=__('Account Deleted Successfully','contact-form-hubspot-crm');
$class='updated';
}else{
$msg=__('Error While Removing Account','contact-form-hubspot-crm');
$class='error';
}
$this->add_msg($msg,$class);
}
$redir=$this->link_to_settings('accounts');
wp_safe_redirect($redir.'&'.$this->id.'_msg=1');
die();
}
if(isset($_REQUEST[$this->id.'_tab_action']) && $_REQUEST[$this->id.'_tab_action']=="get_token"){
check_admin_referer('vx_nonce','vx_nonce');
if(!current_user_can($this->id."_edit_settings")){
$msg=__('You do not have permissions to add token','contact-form-hubspot-crm');
$this->display_msg('admin',$msg);
return;
}
$id=$this->post('id');
$info=$this->get_info($id);
$api=$this->get_api($info);
$info['data']=$api->handle_code($id);
//get objects after saving acces token
$token=$this->post('access_token',$info['data']);
if(!empty($token)){
$this->get_objects($info,true);
}
$link=$this->link_to_settings('accounts');
wp_safe_redirect($link.'&id='.$id);
die();
}
if($this->post('vx_tab_action_'.$this->id)=="export_log"){
check_admin_referer('vx_nonce','vx_nonce');
if(!current_user_can($this->id."_export_logs")){
$msg=__('You do not have permissions to export logs','contact-form-hubspot-crm');
$this->display_msg('admin',$msg);
return;
}
header('Content-disposition: attachment; filename='.date("Y-m-d",current_time('timestamp')).'.csv');
header('Content-Type: application/excel');
$data=$this->get_data_object();
$sql_end=$data->get_log_query();
$forms=array();
$sql="select * $sql_end limit 3000";
$results = $wpdb->get_results($sql , ARRAY_A );
$fields=array(); $field_titles=array("#",__('Status','contact-form-hubspot-crm'),__('HubSpot ID','contact-form-hubspot-crm') ,__('Entry ID','contact-form-hubspot-crm'),__('Description','contact-form-hubspot-crm'),__('Time','contact-form-hubspot-crm'));
$fp = fopen('php://output', 'w');
fputcsv($fp, $field_titles);
$sno=0;
foreach($results as $row){
$sno++;
$row=$this->verify_log($row);
fputcsv($fp, array($sno,$row['title'],$row['_crm_id'],$row['entry_id'],$row['desc'],$row['time']));
}
fclose($fp);
die();
}
if($this->post('vx_tab_action_'.$this->id)=="clear_logs" ){
check_admin_referer('vx_nonce','vx_nonce');
if(!current_user_can($this->id."_edit_settings")){
$msg=__('You do not have permissions to clear logs','contact-form-hubspot-crm');
$this->display_msg('admin',$msg);
return;
}
$data=$this->get_data_object();
$clear=$data->clear_logs();
$msg=__('Error While Clearing HubSpot Logs','contact-form-hubspot-crm');
$level="error";
if(!empty($clear)){
$msg=__('HubSpot Logs Cleared Successfully','contact-form-hubspot-crm');
$level="updated";
}
$this->add_msg($msg,$level);
$link=$this->link_to_settings('logs').$this->id.'_msg=1';
wp_safe_redirect($link);
die();
}
//
self::$tooltips = array(
'vx_feed_name' => esc_html__('Enter feed name of your choice.', 'contact-form-hubspot-crm'),
'vx_sel_object' => esc_html__('Select the Object to Create when a Form is Submitted.', 'contact-form-hubspot-crm'),
'vx_sel_account' =>__('Select the HubSpot account you would like to export entries to.', 'contact-form-hubspot-crm'),
'vx_sel_form' => esc_html__('Select the Contact Form you would like to integrate with HubSpot. Contacts generated by this form will be automatically added to your HubSpot account.', 'contact-form-hubspot-crm'),
'vx_map_fields' => esc_html__('Associate your HubSpot fields to the appropriate Contact Form fields.', 'contact-form-hubspot-crm'),
'vx_optin_condition' =>__('When the opt-in condition is enabled, form submissions will only be exported to HubSpot when the condition is met. When disabled all form submissions will be exported.', 'contact-form-hubspot-crm'),
'vx_manual_export' => esc_html__('If you do not want all entries sent to HubSpot, but only specific, approved entries, check this box. To manually send an entry to HubSpot, go to Entries, choose the entry you would like to send to HubSpot, and then click the "Send to HubSpot" button.', 'contact-form-hubspot-crm'),
'vx_entry_notes' => esc_html__('Enable this option if you want to synchronize Contact Form entry notes to HubSpot Object notes. For example , when you add a note to a Contact Form entry, it will be added to the HubSpot Object selected in the feed.', 'contact-form-hubspot-crm'),
'vx_primary_key' => esc_html__('Which field should be used to update existing objects?', 'contact-form-hubspot-crm'),
'vx_oauth' => esc_html__('OAuth 2.0 is a industry-standard protocol for authorization', 'contact-form-hubspot-crm'),
'vx_api' => esc_html__('Get HubSpot API key from Account Menu -> Integrations -> Get your API Key', 'contact-form-hubspot-crm'),
'vx_custom_app'=>__('This option is for advanced users who want to override default HubSpot App.','contact-form-hubspot-crm'),
'vx_disable_logs'=>__('When an order is sent to HubSpot we store that order information in the database and show it in the HubSpot Log. Check this box if you do not want to save the exported order information in the logs.','contact-form-hubspot-crm'),
'vx_lists'=>__('Get lists from Hub Spot.','contact-form-hubspot-crm'),
'vx_sel_list'=>__('Which List should be assigned to this object.','contact-form-hubspot-crm'),
'vx_list_check'=>__('If enabled, Contact will be added to selected List','contact-form-hubspot-crm'),
'vx_flows'=>__('Get Work Flows from Hub Spot.','contact-form-hubspot-crm'),
'vx_sel_flow'=>__('Which Work Flow should be assigned to this object.','contact-form-hubspot-crm'),
'vx_flow_check'=>__('If enabled, Contact will be added to selected Work Flow','contact-form-hubspot-crm'),
'vx_assign_company'=>__('Enable this option if you want to assign a Company this object.','contact-form-hubspot-crm'),
'vx_sel_company'=>__('Select Contact feed. Company created by this feed will be assigned to this object.','contact-form-hubspot-crm'),
'vx_assign_contact'=>__('Enable this option , if you want to assign a Contact to this object','contact-form-hubspot-crm'),
'vx_sel_contact'=>__('Select Contact feed. Contact created by this feed will be assigned to this object','contact-form-hubspot-crm'),
'vx_camp_check'=>__('If enabled, Lead/Contact will be added to selected Campaign','contact-form-hubspot-crm'),
'vx_owner_check'=>__('Enable this option if you want to assign another object owner.','contact-form-hubspot-crm'),
'vx_owners'=>__('Get Users list from HubSpot','contact-form-hubspot-crm'),
'vx_order_notes'=>__('Enable this option if you want to synchronize WooCommerce Order notes to HubSpot Object notes. For example, when you add a note to a WooCommerce Order, it will be added to the HubSpot Object selected in the feed.','contact-form-hubspot-crm'),
'vx_sel_owner'=>__('Select a user as a owner of this object','contact-form-hubspot-crm'),
'vx_entry_note'=>__('Check this option if you want to send more data as CRM entry note.', 'contact-form-hubspot-crm'),
'vx_note_fields'=>__('Select fields which you want to send as a note', 'contact-form-hubspot-crm'),
'vx_disable_note'=>__('Enable this option if you want to add note only for new CRM entry', 'contact-form-hubspot-crm'),
'vx_entry_note'=>__('Check this option if you want to send more data as CRM entry note.', 'contact-form-hubspot-crm'),
'vx_note_fields'=>__('Select fields which you want to send as a note', 'contact-form-hubspot-crm'),
'vx_disable_note'=>__('Enable this option if you want to add note only for new CRM entry', 'contact-form-hubspot-crm')
);
}
public function add_meta_box_crmperks_form($lead,$form){
$lead_id=isset($lead['id']) ? $lead['id'] : "";
$form_id=isset($lead['form_id']) ? 'vf_'.$lead['form_id'] : "";
if(! $this->has_feed($form_id)) { return ''; }
$data=$this->get_data_object();
$log_entry=$data->get_log_by_lead($lead['id'],$form_id);
$log_url=$this->link_to_settings('logs').'&entry_id='.$lead['id'];
?>