PluginProbe
Connector for Gravity Forms and Google Sheets / 1.2.8
Connector for Gravity Forms and Google Sheets v1.2.8
1.3.0 trunk 1.0.2 1.0.3 1.0.4 1.0.7 1.1.1 1.2.0 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9
wp-gravity-forms-spreadsheets / wp-gravity-forms-spreadsheets.php

wp-gravity-forms-spreadsheets.php in Connector for Gravity Forms and Google Sheets 1.2.8, at wp-gravity-forms-spreadsheets.php

1,850 lines 64.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Connector for Gravity Forms and Google Sheets
4 * Description: Integrates Gravity Forms with Google Sheets allowing form submissions to be automatically sent to your Google Sheets.
5 * Version: 1.2.8
6 * Requires at least: 3.8
7 * Author URI: https://www.crmperks.com
8 * Plugin URI: https://www.crmperks.com/plugins/gravity-forms-plugins/gravity-forms-google-sheets-plugin/
9 * Author: CRM Perks.
10 * Text Domain: gravity-forms-googlesheets-crm
11 * Domain Path: /languages/
12 */
13 // Exit if accessed directly
14 if( !defined( 'ABSPATH' ) ) exit;
15
16
17 if( !class_exists( 'vxg_googlesheets' ) ):
18
19 class vxg_googlesheets {
20
21
22 public $url = 'https://www.crmperks.com';
23
24 public $crm_name = 'googlesheets';
25 public $id = 'vxg_googlesheets';
26 public $domain = 'vxg-gsheets';
27 public $version = "1.2.8";
28 public $update_id = '30021';
29 public $min_gravityforms_version = '1.3.9';
30 public $type = 'vxg_googlesheets_pro';
31 public $fields = null;
32 public $data = null;
33
34 private $filter_condition;
35 private $plugin_dir= '';
36 private $temp= '';
37 private $crm_arr= false;
38 private $entry;
39 private $form;
40 public $notice_js= false;
41 public static $title='Gravity Forms Google Sheets Plugin';
42 public static $path = '';
43 public static $slug = '';
44 public static $debug_html = '';
45 public static $save_key='';
46 public static $lic_msg = '';
47 public static $db_version='';
48 public static $vx_plugins;
49 public static $note;
50 public static $feeds_res;
51 public static $gf_status='';
52 public static $plugin='';
53 public static $gf_status_msg='';
54 public static $is_pr=true;
55 public static $api_timeout;
56
57 public function instance(){
58 self::$path=$this->get_base_path();
59 add_action( 'plugins_loaded', array( $this, 'setup_main' ) );
60 register_deactivation_hook(__FILE__,array($this,'deactivate'));
61 register_activation_hook(__FILE__,(array($this,'activate')));
62
63
64 }
65
66 /**
67 * Plugin starting point. Will load appropriate files
68 *
69 */
70 public function init(){
71
72 self::$gf_status= $this->gravity_forms_status();
73 if(self::$gf_status !== 1){
74 add_action( 'admin_notices', array( $this, 'install_gf_notice' ) );
75 $slug=$this->get_slug();
76 add_action( 'after_plugin_row_'.$slug, array( $this, 'install_gf_notice_plugin_row' ) );
77 return;
78 }
79
80 $pro_file=self::$path . 'wp/crmperks-notices.php';
81 if(file_exists($pro_file)){
82 include_once($pro_file);
83 self::$is_pr=false;
84 }else{
85 $this->plugin_api(true);
86 self::$is_pr=true;
87 $pro_file=self::$path . 'pro/add-ons.php';
88 if(file_exists($pro_file)){
89 include_once($pro_file);
90 } }
91
92 require_once(self::$path . "includes/crmperks-gf.php");
93 require_once(self::$path . "includes/plugin-pages.php");
94
95 }
96
97
98 /**
99 * install plugin
100 *
101 */
102 public function setup_main(){
103 // include_once(self::$path. "includes/edit-form.php");
104 //handling post submission. gform_after_submission runs after gform_replace_merge_tags
105 add_action('gform_entry_created', array($this, 'gf_entry_created_before'), 99, 2);
106 //added via GF API
107 add_action("gform_post_add_entry", array($this, 'gf_entry_created_before'), 40, 2);
108 //trash , restore entry
109 add_action('gform_update_status', array($this, 'entry_status'),10,3);
110 //update entry
111 add_action('gform_after_update_entry', array($this, 'update_entry'),10,2);
112 //updated via GF API
113 add_action('gform_post_update_entry', array($this, 'update_entry_api'),10,2);
114
115
116
117 if(self::$is_pr){
118 // add_action("gform_post_payment_status", array($this, 'gf_entry_paid'), 10, 2); //$feed,$entry
119 add_action("gform_post_payment_completed", array($this, 'gf_entry_paid_normal'), 10, 2); //$entry,$pay_info
120
121 add_action('gform_after_submission', array($this, 'gf_entry_created_after'), 99, 2);
122 add_action("gform_post_add_subscription_payment", array($this, 'gf_entry_paid_subscription'), 10, 2); //$entry,$pay_info
123 }
124
125 add_filter("gform_confirmation", array($this, 'confirmation_error'));
126
127 if(is_admin()){
128 add_action('init', array($this,'init'));
129 //loading translations
130 load_plugin_textdomain('gravity-forms-googlesheets-crm', FALSE, $this->plugin_dir_name(). '/languages/' );
131
132 self::$db_version=get_option($this->type."_version");
133 if(self::$db_version != $this->version && current_user_can( 'manage_options' )){
134 $this->install_plugin();
135 update_option($this->type."_version", $this->version);
136 }
137 }
138 }
139
140 public function plugin_api($start_instance=false){
141 if(empty(self::$path)){ self::$path=$this->get_base_path(); }
142 $file=self::$path . "pro/plugin-api.php";
143 if(!class_exists('vxcf_plugin_api') && file_exists($file)){
144 require_once($file);
145 }
146 if(class_exists('vxcf_plugin_api')){
147 $slug=$this->get_slug();
148 $settings_link=$this->link_to_settings();
149 $is_plugin_page=$this->is_crm_page();
150 self::$plugin=new vxcf_plugin_api($this->id,$this->version,$this->type,$this->domain,$this->update_id,self::$title,$slug,self::$path,$settings_link,$is_plugin_page);
151 if($start_instance){
152 self::$plugin->instance();
153 }
154 } }
155 public function install_plugin(){
156 $data=$this->get_data_object();
157 $data->update_table();
158 //add post permissions
159 require_once(self::$path . "includes/install.php");
160 $install=new vxg_install_googlesheets();
161 $install->create_roles();
162 }
163 public function install_gf_notice(){
164 $message=self::$gf_status_msg;
165 if(!empty($message)){
166 $this->display_msg('admin',$message,'gravity');
167 $this->notice_js=true;
168
169 }
170 }
171 /**
172 * Install Gravity Forms Notice (plugin row)
173 *
174 */
175 public function install_gf_notice_plugin_row(){
176 $message=self::$gf_status_msg;
177 if(!empty($message)){
178 $this->display_msg('',$message,'gravity');
179 }
180 }
181 /**
182 * display admin notice
183 *
184 * @param mixed $type
185 * @param mixed $message
186 * @param mixed $id
187 */
188 public function display_msg($type,$message,$id=""){
189 //exp
190 global $wp_version;
191 $ver=floatval($wp_version);
192 if($type == "admin"){
193 if($ver<4.2){
194 ?>
195 <div class="error vx_notice notice" data-id="<?php echo esc_html($id) ?>"><p style="display: table"><span style="display: table-cell; width: 98%"><span class="dashicons dashicons-megaphone"></span> <b><?php esc_html_e('Gravity Forms Google Sheets Plugin','gravity-forms-googlesheets-crm') ?>. </b><?php echo wp_kses_post($message);?> </span>
196 <span style="display: table-cell; padding-left: 10px; vertical-align: middle;"><a href="#" class="notice-dismiss" title="<?php esc_html_e('Dismiss Notice','gravity-forms-googlesheets-crm') ?>">dismiss</a></span> </p></div>
197 <?php
198 }else{
199 ?>
200 <div class="error vx_notice notice is-dismissible" data-id="<?php echo esc_html($id) ?>"><p><span class="dashicons dashicons-megaphone"></span> <b><?php esc_html_e('Gravity Forms Google Sheets Plugin','gravity-forms-googlesheets-crm') ?>. </b> <?php echo wp_kses_post($message);?> </p>
201 </div>
202 <?php
203 }
204 }else{
205 ?>
206 <tr class="plugin-update-tr"><td colspan="5" class="plugin-update">
207 <style type="text/css"> .vx_msg a{color: #fff; text-decoration: underline;} .vx_msg a:hover{color: #eee} </style>
208 <div style="background-color: rgba(224, 224, 224, 0.5); padding: 9px; margin: 0px 10px 10px 28px "><div style="background-color: #d54d21; padding: 5px 10px; color: #fff" class="vx_msg"> <span class="dashicons dashicons-info"></span> <?php echo wp_kses_post($message) ?>
209 </div></div></td></tr>
210 <?php
211 }
212 }
213 /**
214 * admin_screen_message function.
215 *
216 * @param mixed $message
217 * @param mixed $level
218 */
219 public function screen_msg( $message, $level = 'updated') {
220 echo '<div class="'. esc_attr( $level ) .' notice is-dismissible"><p>';
221 echo wp_kses_post($message);
222 echo '</p></div>';
223 }
224
225
226
227 /**
228 * Gravity forms status
229 *
230 */
231 public function gravity_forms_status() {
232
233 $installed = 0;
234 if(!class_exists('RGForms')) {
235 if(file_exists(WP_PLUGIN_DIR.'/gravityforms/gravityforms.php')) {
236 $installed=2;
237 }
238 }else{
239 $installed=1;
240 if(!$this->is_gravityforms_supported()){
241 $installed=3;
242 }
243 }
244 if($installed !=1){
245 if($installed === 0){ // not found
246 $message = sprintf(__("%sGravity Forms%s is required. %sPurchase it today!%s", 'gravity-forms-googlesheets-crm'), "<a href='http://www.gravityforms.com/'>", "</a>", "<a href='http://www.gravityforms.com/'>", "</a>");
247 }else if($installed === 2){ // not active
248 $message = sprintf(__('Gravity Forms is installed but not active. %sActivate Gravity Forms%s to use the Gravity Forms Google Sheets Plugin','gravity-forms-googlesheets-crm'), '<strong><a href="'.wp_nonce_url(admin_url('plugins.php?action=activate&plugin=gravityforms/gravityforms.php'), 'activate-plugin_gravityforms/gravityforms.php').'">', '</a></strong>');
249 } else if($installed === 3){ // not supported
250 $message = sprintf(__("A higher version of %sGravity Forms%s is required. %sPurchase it today!%s", 'gravity-forms-googlesheets-crm'), "<a href='http://www.gravityforms.com/'>", "</a>", "<a href='http://www.gravityforms.com/'>", "</a>");
251 }
252 self::$gf_status_msg=$message;
253 }
254 return $installed;
255 }
256 /**
257 * display error to admin only in form front
258 *
259 * @param mixed $confirmation
260 * @param mixed $form
261 * @param mixed $lead
262 * @param mixed $ajax
263 */
264 public function confirmation_error($confirmation, $form = '', $lead = '', $ajax ='' ) {
265 if(current_user_can('administrator') && !empty($_REQUEST['VXGGoogle SheetsError'])) { ///
266 $confirmation .= sprintf(__('%sThe entry was not added to Google Sheets because: %s. %sYou are only being shown this because you are an administrator. Other users will not see this message.%s', 'gravity-forms-googlesheets-crm'), '<div class="error" style="text-align:center; color:#790000; font-size:14px; line-height:1.5em; margin-bottom:16px;background-color:#FFDFDF; margin-bottom:6px!important; padding:6px 6px 4px 6px!important; border:1px dotted #C89797">','<strong>'.esc_html($_REQUEST['VXGGoogle SheetsError']). '</strong>', '<br /><em>', '</em></div>');
267 }
268 return $confirmation;
269 }
270
271 /**
272 * Returns true if the current page is an Feed pages. Returns false if not
273 *
274 * @param mixed $page
275 */
276 public function is_crm_page($page=""){
277 if(empty($page)) {
278 $page = $this->post("page");
279 }
280 if(isset($_GET['subview'])){
281 $page = $this->post("subview");
282 }
283 return $page == $this->id;
284 }
285 /**
286 * Called when entry is manually updated in the Single Entry view of Gravity Forms.
287 *
288 * @param mixed $form
289 * @param mixed $entry_id
290 */
291 public function manual_export( $form, $entry_id = NULL ) {
292
293 global $plugin_page;
294
295 // Is this the Gravity Forms entries page?
296 if(false === ($this->is_gravity_page('gf_entries') && rgget("view") == 'entry' && (rgget('lid') || !rgblank(rgget('pos'))))) {
297 return;
298 }
299 $entry=array();
300 // Both admin_init and gforms_after_update_entry will have this set
301 if( empty( $_POST['gforms_save_entry'] ) || empty( $_POST['action'] ) ) { return; }
302
303 // Different checks since admin_init runs in both cases but we need to wait for entry update
304 $current_hook = current_filter();
305
306 if( $current_hook == 'admin_init' && empty( $_POST[$this->id.'_send'] ) ) { return; }
307 if( $current_hook == 'gform_after_update_entry' && empty( $_POST[$this->id.'_update'] ) ) { return; }
308
309 // Verify authenticity of request
310 check_admin_referer('gforms_save_entry', 'gforms_save_entry');
311
312 // For admin_init hook, get the entry ID from the URL
313 if(empty($entry_id)) {
314 $entry_id = rgget('lid');
315 $form_id = rgget('id');
316
317 // fetch alternative entry id: look for gf list details when using pagination
318 if(empty($entry_id)) {
319 $entry_id=$this->get_entry_id($form_id);
320 }
321 $form = RGFormsModel::get_form_meta($form_id);
322 }
323 $entry=$this->get_gf_entry($entry_id);
324 if(!current_user_can($this->id."_send_to_crm")){
325 return;
326 }
327 // Export the entry
328 $push=$this->push($entry, $form,"",true);
329 if(!empty($push['msg'])){
330 $this->screen_msg($push['msg'],$push['class']);
331 }
332 // Don't send twice.
333 unset($_POST[$this->id.'_update']);
334 unset($_POST[$this->id.'_send']);
335 }
336 /**
337 * get entry id
338 *
339 * @param mixed $form_id
340 */
341 public function get_entry_id($form_id){
342 $entry_id='';
343 $position = rgget('pos');
344 $paging = array('offset' => $position, 'page_size' => 1);
345
346 $entries = GFAPI::get_entries($form_id, array(), null, $paging);
347
348 if(!empty($entries)) {
349 // pluck first entry to use id from, should always only be one
350 $entry = array_shift($entries);
351 $entry_id = $entry['id'];
352 }
353
354 return $entry_id;
355 }
356
357
358 /**
359 * settings link
360 *
361 * @param mixed $escaped
362 */
363 public function link_to_settings( $escaped = true ) {
364
365 $url = admin_url('admin.php?page=gf_settings&subview='.$this->id);
366
367 return $url;
368 }
369
370 /**
371 * Get CRM info
372 *
373 */
374 public function get_info($id){
375 $data=$this->get_data_object();
376 $info=$data->get_account($id);
377 $info_arr=$data=array(); $meta=array();
378 if(is_array($info)){
379 if(!empty($info['data'])){
380
381 $info['data']=trim($info['data']);
382 if(strpos($info['data'],'{') !== 0){
383 $info['data']=$this->de_crypt($info['data']);
384 }
385 $info_arr=json_decode($info['data'],true);
386
387 if(!is_array($info_arr)){
388 $info_arr=array();
389 }
390 }
391
392 //$info_arr['time']=$info['time'];
393 $info_arr['id']=$info['id'];
394 $info['data']=$info_arr;
395 if(!empty($info['meta'])){
396 $meta=json_decode($info['meta'],true);
397 }
398 $info['meta']=is_array($meta) ? $meta : array();
399
400 if(!empty($info['time'])){
401 $info['time']=strtotime($info['time']);
402 }
403 }
404
405 return $info;
406 }
407 /**
408 * update account
409 *
410 * @param mixed $data
411 * @param mixed $id
412 */
413 public function update_info($data,$id) {
414
415 if(empty($id)){
416 return;
417 }
418
419 $time = current_time( 'mysql' ,1);
420
421 $sql=array('updated'=>$time);
422 if(is_array($data)){
423
424
425 if(isset($data['meta'])){
426 $sql['meta']= json_encode($data['meta']);
427 }
428 if( isset($data['data']) && is_array($data['data'])){
429 $_data=$this->get_data_object();
430 $acount=$_data->get_account($id);
431 if(empty($acount['time'])){
432 $sql['time']= $time;
433 }
434 $sql['status']='2';
435 if(isset($data['data']['class'])){
436 $sql['status']= $data['data']['class'] == 'updated' ? '1' : '2';
437 }
438 if(isset($data['data']['meta'])){
439 unset($data['data']['meta']);
440 }
441 if(isset($data['data']['status'])){
442 unset($data['data']['status']);
443 }
444 if(isset($data['data']['name'])){
445 $sql['name']=$data['data']['name'];
446 // unset($data['data']['name']);
447 }else if(isset($_GET['id'])){
448 $sql['name']="Account #".$this->post('id');
449 }
450
451 $enc_str=json_encode($data['data']);
452 // $enc_str=$this->en_crypt($enc_str);
453 $sql['data']=$enc_str;
454 }
455 }
456
457
458 $data=$this->get_data_object();
459 $result = $data->update_account($sql,$id);
460
461 return $result;
462 }
463
464 /**
465 * gravity forms field values, modify check boxes etc
466 *
467 * @param mixed $entry
468 * @param mixed $form
469 * @param mixed $gf_field_id
470 * @param mixed $crm_field_id
471 * @param mixed $custom
472 */
473 public function verify_field_val($entry,$form,$gf_field_id,$crm_field_id="",$custom=""){
474
475 $value=false;
476 /* if(empty($field)){
477 return $value;
478 }*/
479
480 if(isset($entry[$gf_field_id])){
481 $value=$entry[$gf_field_id];
482 if(in_array($gf_field_id,array('date_created','payment_date'))){
483 $date_format=trim(get_option( 'date_format' ).' '.get_option( 'time_format' ));
484 if(empty($date_format)){
485 $date_format='Y/m/d H:i:s';
486 }
487 $value=GFCommon::format_date( $value, false, $date_format , false );
488
489 }
490 if(is_numeric($gf_field_id)){
491 $field = RGFormsModel::get_field($form, $gf_field_id);
492 //$value=GFCommon::get_lead_field_display( $field, $value,'',false,'text' );
493 //$value=$field->get_value_entry_detail($value,'',false,'text');
494 if(isset($field->type) && in_array($field->type,array('list')) ){
495 //$value=maybe_unserialize($value);
496 }
497 if(isset($field->type) && in_array($field->type,array('option','product')) ){
498 $found=strpos($value,'|');
499 if($found){
500 if(!empty($custom)){ //it is empty with process_tags function
501 $value=substr($value,$found+1);
502 }else{
503 $value=substr($value,0,$found);
504 }
505 }
506 }
507 if( (isset($field->storageType) && $field->storageType == 'json') || $field->type == 'fileupload' ){
508 $value=$field->get_value_entry_detail($value,'',false,'text');//convert direct url to gravity download hash
509 /* $value_temp=json_decode($value,1); if(!empty($value_temp)){ $value=$value_temp; }
510 if(is_array($value)){
511 $value=implode("\n",$value);
512 }*/
513 }
514 if( in_array($field->type, array('survey','quiz')) && !empty($field->choices)){
515 $val_temp=explode(':',$value);
516 if(count($val_temp) > 1){ $value=$val_temp[1]; }
517 foreach($field->choices as $v){
518 if($v['value'] == $value){
519 $value=!empty($v['score']) ? $v['score'] : $v['text'];
520 $value=!empty($v['gquizWeight']) ? $v['gquizWeight'] : $v['text'];
521 break;
522 }
523 }
524 }
525 if($field->type == 'consent'){
526 $value= !empty($value) ? 'Yes' : 'No';
527 }
528
529 if($field->type == 'date' && !empty($value) ){
530 $formats=array('mdy'=>'m/d/Y','dmy'=>'d/m/Y','dmy_dash'=>'d-m-Y','dmy_dot'=>'d.m.Y','ymd_slash'=>'Y/m/d','ymd_dash'=>'Y-m-d','ymd_dot'=>'Y.m.d');
531 if( !empty($field->dateFormat) && isset($formats[$field->dateFormat])){
532 $value=date($formats[$field->dateFormat],strtotime($value));
533 }
534 }else if($field->type == "post_image"){
535 $value=rtrim($value,'|:|');
536 }else if($field->type == 'list' && is_array($value)){
537 $v_temp=array();
538 foreach($value as $v){
539 if(is_array($v)){
540 $v=trim(implode(', ',array_values($v)));
541 }
542 $v_temp[]=$v;
543 }
544 $value=trim(implode(" - \n",$v_temp));
545 }else if($field->type == 'number'){
546 $value=$field->get_value_entry_detail($value);
547 }
548 }
549 $val=true;
550 }else{ //check if full address
551 if($gf_field_id=='entry_url'){
552 $value=add_query_arg(array('page'=>'gf_entries','view'=>'entry','lid'=>$entry['id'],'id'=>$entry['form_id']), admin_url('admin.php'));
553 $val=true;
554 }else if($gf_field_id=="form_title"){
555 $value=$form['title'];
556 $val=true;
557 }else{
558 $field = RGFormsModel::get_field($form, $gf_field_id);
559
560 if(!empty($field)){
561 if($field->type == "address"){
562 $address_type="";
563 if($crm_field_id!=""){
564 $address_type=isset($custom[$crm_field_id]['type']) && $custom[$crm_field_id]['type'] == "address" ? "json" : "";
565 }
566 $value=$this->get_address($entry,$gf_field_id,$address_type);
567 $val=true;
568 }else if(is_numeric($gf_field_id)){
569 // This is for checkboxes
570 $elements = array();
571 foreach($entry as $key => $val_e) {
572 if(is_numeric($key) && floor($key) == floor($gf_field_id) && !empty($val_e)) {
573 $elements[] = htmlspecialchars($val_e);
574 }}
575 if(count($elements)>0){
576 $value=$elements; $val=true;
577 } }
578 }else if(isset($_REQUEST[$gf_field_id])){
579 $value=$this->post($gf_field_id);
580 }
581 }
582 }
583 if($value && is_array($value)){
584 // $value=implode(", ",$value);
585 }
586
587 return $value;
588 }
589 /**
590 * filter enteries
591 *
592 * @param mixed $feed
593 * @param mixed $entry
594 * @param mixed $form
595 */
596 public function check_filter($feed,$entry,$form){
597 $filters=$this->post('filters',$feed);
598 $final=$this->filter_condition=null;
599 if(is_array($filters)){
600 $time=current_time('timestamp');
601 foreach($filters as $filter_s){
602 $check=null; $and=null; $and_c=array();
603 if(is_array($filter_s)){
604 foreach($filter_s as $filter){
605 $field=$filter['field'];
606 $fval=$filter['value'];
607 $val=$this->verify_field_val($entry,$form,$field);
608 if(is_array($val)){ $val=implode(' ',$val); }
609
610 switch($filter['op']){
611 case"is": $check=$fval == $val; break;
612 case"is_not": $check=$fval != $val; break;
613 case"contains": $check=preg_match('/'.$fval.'/i', $val) > 0; break;
614 case"not_contains": $check=preg_match('/'.$fval.'/i', $val) === 0; break;
615 case"is_in": $check=strpos($fval,$val) !==false; break;
616 case"not_in": $check=strpos($fval,$val) ===false; break;
617 case"starts": $check=strpos($val,$fval) === 0; break;
618 case"not_starts": $check=strpos($val,$fval) !== 0; break;
619 case"ends": $check=(strrpos($val,$fval)+strlen($fval)) == strlen($val); break;
620 case"not_ends": $check=(strrpos($val,$fval)+strlen($fval)) != strlen($val); break;
621 case"less": $check=(float)$val<(float)$fval; break;
622 case"greater": $check=(float)$val>(float)$fval; break;
623 case"less_date": $check=strtotime($val,$time) < strtotime($fval,$time); break;
624 case"greater_date": $check=strtotime($val,$time) > strtotime($fval,$time); break;
625 case"equal_date": $check=strtotime($val,$time) == strtotime($fval,$time); break;
626 case"empty": $check=$val == ""; break;
627 case"not_empty": $check=$val != ""; break;
628 }
629 $and_c[]=array("check"=>$check,"field_val"=>$fval,"input"=>$val,"field"=>$field,"op"=>$filter['op']);
630 if($check !== null){
631 if($and !== null){
632 $and=$and && $check;
633 }else{
634 $and=$check;
635 }
636 }
637 } //end and loop filter
638 }
639 if($and !== null){
640 if($final !== null){
641 $final=$final || $and;
642 }else{
643 $final=$and;
644 }
645 }
646 $this->filter_condition[]=$and_c;
647 } // end or loop
648 }
649 // var_dump($final); die();
650 return $final === null ? true : $final;
651 }
652
653 /**
654 * get address components
655 *
656 * @param mixed $entry
657 * @param mixed $field_id
658 * @param mixed $type
659 */
660 private function get_address($entry, $field_id,$type=""){
661 $street_value = str_replace(" ", " ", trim($entry[$field_id . ".1"]));
662 $street2_value = str_replace(" ", " ", trim($entry[$field_id . ".2"]));
663 $city_value = str_replace(" ", " ", trim($entry[$field_id . ".3"]));
664 $state_value = str_replace(" ", " ", trim($entry[$field_id . ".4"]));
665 $zip_value = trim($entry[$field_id . ".5"]);
666 if(method_exists('GF_Field_Address','get_country_code')){
667 $field_c=new GF_Field_Address();
668 $country_value=$field_c->get_country_code(trim($entry[$field_id . ".6"]));
669 }else{
670 $country_value = GFCommon::get_country_code(trim($entry[$field_id . ".6"]));
671 }
672 $country =trim($entry[$field_id . ".6"]);
673 $address = $street_value;
674 $address .= !empty($address) && !empty($street2_value) ? " $street2_value" : $street2_value;
675 if($type =="json"){
676 $arr=array("street"=>$address,"city"=>$city_value,"state"=>$state_value,"zip"=>$zip_value,"country"=>$country);
677 return json_encode($arr);
678 }
679 $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? " $city_value" : $city_value;
680 $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? " $state_value" : $state_value;
681 $address .= !empty($address) && !empty($zip_value) ? " $zip_value" : $zip_value;
682 $address .= !empty($address) && !empty($country_value) ? " $country_value" : $country_value;
683
684 return $address;
685 }
686 /**
687 * if gravity forms page
688 *
689 * @param mixed $page
690 */
691 public function is_gravity_page($page = array()){
692 if(!class_exists('RGForms')) { return false; }
693 $current_page = trim(strtolower(RGForms::get("page")));
694 if(empty($page)) {
695 $gf_pages = array("gf_edit_forms","gf_new_form","gf_entries","gf_settings","gf_export","gf_help");
696 } else {
697 $gf_pages = is_array($page) ? $page : array($page);
698 }
699
700 return in_array($current_page, $gf_pages);
701 }
702 /**
703 * Add checkbox to entry info - option to send entry to crm
704 *
705 * @param mixed $form_id
706 * @param mixed $lead
707 */
708 public function entry_info_send_checkbox( $form_id, $lead ) {
709
710 // If this entry's form isn't connected to crm, don't show the checkbox
711 if(!$this->show_send_to_crm_button() ) { return; }
712
713 // If this is not the Edit screen, get outta here.
714 if(empty($_POST["screen_mode"]) || $_POST["screen_mode"] === 'view') { return; }
715
716 if(!current_user_can($this->id."_send_to_crm")){return; }
717
718 if( apply_filters( $this->id.'_show_manual_export_button', true ) ) {
719 printf('<input type="checkbox" name="'.$this->id.'_update" id="'.$this->id.'_update" value="1" /><label for="'.$this->id.'_update" title="%s">%s</label><br /><br />', esc_html__('Create or update this entry in Google Sheets. The fields will be mapped according to the form feed settings.', 'gravity-forms-googlesheets-crm'), esc_html__('Send to Google Sheets', 'gravity-forms-googlesheets-crm'));
720 } else {
721 echo '<input type="hidden" name="'.$this->id.'_update" id="'.$this->id.'_update" value="1" />';
722 }
723 }
724 /**
725 * Add button to entry info - option to send entry to crm
726 *
727 * @param mixed $button
728 */
729 public function entry_info_send_button( $button = '' ) {
730 // If this entry's form isn't connected to crm, don't show the button
731 if(!$this->show_send_to_crm_button()) { return $button; }
732 if(!current_user_can($this->id."_send_to_crm")){return; }
733 // Is this the view or the edit screen?
734 $mode = empty($_POST["screen_mode"]) ? "view" : $this->post("screen_mode");
735 if($mode === 'view') {
736 $margin="";
737 if(defined("vx_btn")){
738 $margin="margin-top: 5px;";
739 }else{define('vx_btn','true');}
740 $button.= '<input type="submit" class="button button-large button-secondary alignright" name="'.$this->id.'_send" style="margin-left:5px; '.$margin.'" title="'.__('Create or update this entry in Google Sheets. The fields will be mapped according to the form feed settings.','gravity-forms-googlesheets-crm').'" value="'.__('Send to Google Sheets', 'gravity-forms-googlesheets-crm').'" onclick="jQuery(\'#action\').val(\'send_to_crm\')" />';
741 //logs button
742
743 $entry_id=$this->post('lid');
744 $form_id = rgget('id');
745 if(empty($entry_id)){
746 $entry_id=$this->get_entry_id($form_id);
747 }
748 $log_url=admin_url( 'admin.php?page=gf_edit_forms&view=settings&subview='.$this->id.'&tab=log&id='.$form_id.'&entry_id='.$entry_id);
749 $button.= '<a class="button button-large button-secondary alignright" style="margin-left:5px; margin-top:5px; " title="'.__('Go to Google Sheets Logs','gravity-forms-googlesheets-crm').'" href="'.$log_url.'">'.__('Google Sheets Logs','gravity-forms-googlesheets-crm').'</a>';
750
751 }
752 return $button;
753 }
754 /**
755 * Whether to show the Entry "Send to CRM" button or not
756 *
757 * If the entry's form has been mapped to CRM feed, show the Send to CRM button. Otherwise, don't.
758 *
759 * @return boolean True: Show the button; False: don't show the button.
760 */
761 public function show_send_to_crm_button() {
762
763 $form_id = rgget('id');
764
765 return $this->has_feed($form_id);
766 }
767 /**
768 * Does the current form have a feed assigned to it?
769 * @param INT $form_id Form ID
770 * @return boolean
771 */
772 function has_feed($form_id) {
773 $data=$this->get_data_object();
774 $feeds = $data->get_feed_by_form( $form_id , true);
775
776 return !empty($feeds);
777 }
778
779 /**
780 * Add note to GF Entry
781 * @param int $id Entry ID
782 * @param string $note Note text
783 */
784 private function add_note($id, $note) {
785
786 RGFormsModel::add_note($id, 0, esc_html__('Gravity Forms Google Sheets Plugin','gravity-forms-googlesheets-crm'), $note);
787 }
788
789 /**
790 * if gravity forms installed and supported
791 *
792 */
793 private function is_gravityforms_supported(){
794 if(class_exists("GFCommon")){
795 $is_correct_version = version_compare(GFCommon::$version, $this->min_gravityforms_version, ">=");
796 return $is_correct_version;
797 }
798 else{
799 return false;
800 }
801 }
802 /**
803 * uninstall plugin
804 *
805 */
806 public function uninstall(){
807 //droping all tables
808 require_once(self::$path . "includes/install.php");
809 $install=new vxg_install_googlesheets();
810 do_action('uninstall_vx_plugin_'.$install->id);
811 $install->remove_data();
812 }
813 /**
814 * email validation
815 *
816 * @param mixed $email
817 */
818 public function is_valid_email($email){
819 if(function_exists('filter_var')){
820 if(filter_var($email, FILTER_VALIDATE_EMAIL)){
821 return true;
822 }
823 }else{
824 if(strpos($email,"@")>1){
825 return true;
826 }
827 }
828 return false;
829 }
830 /**
831 * deactivate
832 *
833 * @param mixed $action
834 */
835 public function deactivate($action="deactivate"){
836 do_action('plugin_status_'.$this->type,$action);
837 }
838 /**
839 * activate plugin
840 *
841 */
842 public function activate(){
843 $this->plugin_api(true);
844 do_action('plugin_status_'.$this->type,'activate');
845 }
846 /**
847 * Send CURL Request
848 *
849 * @param mixed $body
850 * @param mixed $path
851 * @param mixed $method
852 */
853 public function request($path="",$method='POST',$body="",$head=array()) {
854
855 $args = array(
856 'body' => $body,
857 'headers'=> $head,
858 'method' => strtoupper($method), // GET, POST, PUT, DELETE, etc.
859 'sslverify' => false,
860 'timeout' => 20,
861 );
862
863 $response = wp_remote_request($path, $args);
864
865 if(is_wp_error($response)) {
866 $this->error_msg= $response->get_error_message();
867 return false;
868 } else if(isset($response['response']['code']) && $response['response']['code'] != 200 && $response['response']['code'] != 404) {
869 $this->error_msg = strip_tags($response['body']);
870 return false;
871 } else if(!$response) {
872 return false;
873 }
874 $result=wp_remote_retrieve_body($response);
875 return $result;
876 }
877
878 /**
879 * Adds feed tooltips to the list of tooltips
880 *
881 * @param mixed $tooltips
882 */
883 public function tooltips($tooltips){
884 $crm_tooltips = array(
885 'vx_feed_name' => '<h6>' . esc_html__('Feed Name', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Enter feed name of your choice.', 'gravity-forms-googlesheets-crm'),
886 'vx_sel_object' => '<h6>' .__('Google Sheets Object', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Select the Object to Create when a Form is Submitted.', 'gravity-forms-googlesheets-crm'),
887 'vx_sel_account' => '<h6>' .__('Google Sheets Account', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Select the Google Sheets account you would like to export entries to.', 'gravity-forms-googlesheets-crm'),
888 'vx_sel_form' => '<h6>' . esc_html__('Gravity Form', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Select the Gravity Form you would like to integrate with Google Sheets. Contacts generated by this form will be automatically added to your Google Sheets account.', 'gravity-forms-googlesheets-crm'),
889
890 'vx_map_fields' => '<h6>' . esc_html__('Map Standard Fields', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Associate your Google Sheets fields to the appropriate Gravity Form fields.', 'gravity-forms-googlesheets-crm'),
891
892 'vx_optin_condition' => '<h6>' . esc_html__('Opt-In Condition', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('When the opt-in condition is enabled, form submissions will only be exported to Google Sheets when the condition is met. When disabled all form submissions will be exported.', 'gravity-forms-googlesheets-crm'),
893
894 'vx_manual_export' => '<h6>' . esc_html__('Manual Export', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('If you do not want all entries sent to Google Sheets, but only specific, approved entries, check this box. To manually send an entry to Google Sheets, go to Entries, choose the entry you would like to send to Google Sheets, and then click the "Send to Google Sheets" button.', 'gravity-forms-googlesheets-crm'),
895
896 'vx_entry_notes' => '<h6>' . esc_html__('Entry Notes', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Enable this option if you want to synchronize Gravity Forms entry notes to Google Sheets Object notes. For example , when you add a note to a Gravity Forms entry, it will be added to the Google Sheets Object selected in the feed.', 'gravity-forms-googlesheets-crm'),
897
898 'vx_primary_key' => '<h6>' . esc_html__('Primary Key', 'gravity-forms-googlesheets-crm') . '</h6>' . esc_html__('Which field should be used to update existing objects?', 'gravity-forms-googlesheets-crm'),
899
900
901 'vx_web' => '<h6>' . esc_html__('Web to Lead', 'gravity-forms-googlesheets-crm') . '</h6>' . sprintf(__('Web-to-Lead is available for all Google Sheets Editions. If you are not sure if your Google Sheets Edition supports the API, you should use Web-to-Lead. Editions that do not support the Google Sheets API: %s 1: Personal Edition %s 2: Group Edition %s 3: Professional Edition %s Note: You can purchase API access for a Professional Edition', 'gravity-forms-googlesheets-crm'),'<br/>','<br/>','<br/>','<br/>'),
902
903 'vx_api' => '<h6>' . esc_html__('Google Sheets API', 'gravity-forms-googlesheets-crm') . '</h6>' .sprintf(__('The API features are more powerful than Web-to-Lead. You can create different object types, as well as other advanced features. If you have any of the following Google Sheets Editions, you can use the included API Add-on: %s 1: Enterprise Edition %s 2: Unlimited Edition %s 3: Developer Edition %s 4: Professional Edition - Requires API Upgrade', 'gravity-forms-googlesheets-crm'),'<br/>','<br/>','<br/>','<br/>','<br/>'),
904
905
906 'vx_custom_app'=>'<h6>' . esc_html__('Custom App', 'gravity-forms-googlesheets-crm') . '</h6>' .__('This option is for advanced users who want to override default Google Sheets App.','gravity-forms-googlesheets-crm'),
907
908
909 'vx_camps'=>'<h6>' . esc_html__('Google Sheets Campaigns', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Get Campaigns and Status list from googlesheets.','gravity-forms-googlesheets-crm'),
910
911 'vx_sel_price_book'=>__('Which Pricebook should be searched for product','gravity-forms-googlesheets-crm'),
912
913 'vx_sel_camp'=>'<h6>' . esc_html__('Select Campaign', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Which Campaign should be assigned to this object.','gravity-forms-googlesheets-crm'),
914
915 'vx_sel_status'=>'<h6>' . esc_html__('Campaign Status', 'gravity-forms-googlesheets-crm') . '</h6>' .__('What should be Member Status.','gravity-forms-googlesheets-crm'),
916
917 'vx_pro_desc'=>'<h6>' . esc_html__('Price Book', 'gravity-forms-googlesheets-crm') . '</h6>' .__('A new product will be created in selected Pricebook. You can add a description for new products created by this plugin.','gravity-forms-googlesheets-crm'),
918
919 'vx_assign_account'=>'<h6>' . esc_html__('Google Sheets Account', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Enable this option if you want to assign an account this object.','gravity-forms-googlesheets-crm'),
920
921 'vx_sel_account'=>'<h6>' . esc_html__('Select Account', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Object created by this feed will be assigned to the selected Account.','gravity-forms-googlesheets-crm'),
922
923 'vx_assign_contract'=>'<h6>' . esc_html__('Assign Contract', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Enable this option , if you want to assign a Contract to this object','gravity-forms-googlesheets-crm'),
924
925 'vx_sel_contract'=>'<h6>' . esc_html__('Select Contact', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Select Contract feed. Contract created by this feed will be assigned to this object','gravity-forms-googlesheets-crm'),
926
927 'vx_camp_check'=>'<h6>' . esc_html__('Add to Campaign', 'gravity-forms-googlesheets-crm') . '</h6>' .__('If enabled, Lead/Contact will be added to selected Campaign','gravity-forms-googlesheets-crm'),
928 'vx_owner_check'=>'<h6>' . esc_html__('Assign Owner', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Enable this option if you want to assign another object owner.','gravity-forms-googlesheets-crm'),
929
930 'vx_owners'=>'<h6>' . esc_html__('Google Sheets Users', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Get Users list from Google Sheets','gravity-forms-googlesheets-crm'),
931
932 'vx_order_notes'=>'<h6>' . esc_html__('Entry Notes', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Enable this option if you want to synchronize Entry notes to Google Sheets Object notes. For example, when you add a note to a Entry, it will be added to the Google Sheets Object selected in the feed.','gravity-forms-googlesheets-crm'),
933
934 'vx_sel_owner'=>'<h6>' . esc_html__('Select Owner', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Select a user as a owner of this object','gravity-forms-googlesheets-crm'),
935
936 'vx_entry_note'=>'<h6>' . esc_html__('Entry Note', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Check this option if you want to send more data as CRM entry note.', 'gravity-forms-googlesheets-crm'),
937 'vx_note_fields'=>'<h6>' . esc_html__('Note Fields', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Select fields which you want to send as a note', 'gravity-forms-googlesheets-crm'),
938 'vx_disable_note'=>'<h6>' . esc_html__('Note Fields', 'gravity-forms-googlesheets-crm') . '</h6>' .__('Enable this option if you want to add note only for new CRM entry', 'gravity-forms-googlesheets-crm')
939
940 );
941 return array_merge($tooltips,$crm_tooltips);
942 }
943
944 /**
945 * Formates User Informations and submitted form to string
946 * This string is sent to email and googlesheets
947 * @param array $info User informations
948 * @param bool $is_html If HTML needed or not
949 * @return string formated string
950 */
951 public function format_user_info($info,$is_html=false){
952 $str=""; $file="";
953 if($is_html){
954 if(file_exists(self::$path."templates/email.php")){
955 ob_start();
956 include_once(self::$path."templates/email.php");
957 $file= ob_get_contents(); // data is now in here
958 ob_end_clean();
959 }
960 if(trim($file) == "")
961 $is_html=false;
962 }
963 if(isset($info['info']) && is_array($info['info'])){
964 if($is_html){
965 if(isset($info['info_title'])){
966 $str.='<tr><td style="font-family: Helvetica, Arial, sans-serif;background-color: #C35050; height: 36px; color: #fff; font-size: 24px; padding: 0px 10px">'.$info['info_title'].'</td></tr>'."\n";
967 }
968 if(is_array($info['info']) && count($info['info'])>0){
969 $str.='<tr><td style="padding: 10px;"><table border="0" cellpadding="0" cellspacing="0" width="100%;"><tbody>';
970 foreach($info['info'] as $f_k=>$f_val){
971 $str.='<tr><td style="padding-top: 10px;color: #303030;font-family: Helvetica;font-size: 13px;line-height: 150%;text-align: right; font-weight: bold; width: 28%; padding-right: 10px;">'.$f_k.'</td><td style="padding-top: 10px;color: #303030;font-family: Helvetica;font-size: 13px;line-height: 150%;text-align: left; word-break:break-all;">'.$f_val.'</td></tr>'."\n";
972 }
973 $str.="</table></td></tr>";
974 }
975 }else{
976 if(isset($info['title']))
977 $str.="\n".$info['title']."\n";
978 foreach($info['info'] as $f_k=>$f_val){
979 $str.=$f_k." : ".$f_val."\n";
980 }
981 }
982 }
983 if($is_html){
984 $str=str_replace(array("{title}","{msg}","{sf_contents}"),array($info['title'],$info['msg'],$str),$file);
985 }
986 return $str;
987 }
988
989
990 /**
991 * if plugin user is valid
992 *
993 * @param mixed $update
994 */
995
996 public function is_valid_user($update){
997 return is_array($update) && isset($update['user']['user']) && $update['user']['user']!=""&& isset($update['user']['expires']);
998 }
999 /**
1000 * Get variable from array
1001 *
1002 * @param mixed $key
1003 * @param mixed $arr
1004 */
1005 public function post($key, $arr="") {
1006 if(is_array($arr)){
1007 return isset($arr[$key]) ? $arr[$key] : "";
1008 }
1009 //clean when getting extrenals
1010 return isset($_REQUEST[$key]) ? $this->clean($_REQUEST[$key],$key) : "";
1011 }
1012 public function clean($var,$key=''){
1013 if ( is_array( $var ) ) {
1014 $a=array();
1015 foreach($var as $k=>$v){
1016 $a[$k]=$this->clean($v,$k);
1017 }
1018 return $a;
1019 }else {
1020 $var=wp_unslash($var);
1021 if(in_array($key,array('note_val','value'))){
1022 $var=sanitize_textarea_field($var);
1023 }else{
1024 $var=sanitize_text_field($var);
1025 }
1026 return $var;
1027 }
1028 }
1029
1030 /**
1031 * Get WP Encryption key
1032 * @return string Encryption key
1033 */
1034 public static function get_key(){
1035 $k='Wezj%+l-x.4fNzx%hJ]FORKT5Ay1w,iczS=DZrp~H+ve2@1YnS;;g?_VTTWX~-|t';
1036 if(defined('AUTH_KEY')){
1037 $k=AUTH_KEY;
1038 }
1039 return substr($k,0,30);
1040 }
1041 /**
1042 * check if other version of this plugin exists
1043 *
1044 */
1045 public function other_plugin_version(){
1046
1047 $status=0;
1048 if(class_exists('vxg_googlesheets_wp')){
1049 $status=1;
1050 }else if( file_exists(WP_PLUGIN_DIR.'/gravity-forms-googlesheets-crm/gravity-forms-googlesheets-crm.php')) {
1051 $status=2;
1052 }
1053 return $status;
1054 }
1055 /**
1056 * Get time Offset
1057 *
1058 */
1059 public function time_offset(){
1060 $offset = (int) get_option('gmt_offset');
1061 return $offset*3600;
1062 }
1063 /**
1064 * Decrypts Values
1065 * @param array $info Google Sheets encrypted API info
1066 * @return array API settings
1067 */
1068 public static function de_crypt($info){
1069 $info=trim($info);
1070 if($info == "")
1071 return '';
1072 $str=base64_decode($info);
1073 $key=self::get_key();
1074 $decrypted_string='';
1075 if(function_exists("openssl_encrypt") && strpos($str,':')!==false ) {
1076 $method='AES-256-CBC';
1077 $arr = explode(':', $str);
1078 if(isset($arr[1]) && $arr[1]!=""){
1079 $decrypted_string=openssl_decrypt($arr[0],$method,$key,false, base64_decode($arr[1]));
1080 }
1081 }else{
1082 $decrypted_string=$str;
1083 }
1084 return $decrypted_string;
1085 }
1086 /**
1087 * Encrypts Values
1088 * @param string $str
1089 * @return string Encrypted Value
1090 */
1091 public static function en_crypt($str){
1092 $str=trim($str);
1093 if($str == "")
1094 return '';
1095 $key=self::get_key();
1096 if(function_exists("openssl_encrypt")) {
1097 $method='AES-256-CBC';
1098 $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
1099 $enc_str=openssl_encrypt($str,$method, $key,false,$iv);
1100 $enc_str.=":".base64_encode($iv);
1101 }else{
1102 $enc_str=$str;
1103 }
1104 $enc_str=base64_encode($enc_str);
1105 return $enc_str;
1106 }
1107
1108 /**
1109 * Get variable from array
1110 *
1111 * @param mixed $key
1112 * @param mixed $key2
1113 * @param mixed $arr
1114 */
1115 public function post2($key,$key2, $arr="") {
1116 if(is_array($arr) && isset($arr[$key]) && is_array($arr[$key])){
1117 return isset($arr[$key][$key2]) ? $arr[$key][$key2] : "";
1118 }
1119 return isset($_REQUEST[$key][$key2]) && is_array($_REQUEST[$key]) ? $this->clean($_REQUEST[$key][$key2]) : "";
1120 }
1121 /**
1122 * Get variable from array
1123 *
1124 * @param mixed $key
1125 * @param mixed $key2
1126 * @param mixed $arr
1127 */
1128 public function post3($key,$key2,$key3, $arr="") {
1129 if(is_array($arr)){
1130 return isset($arr[$key][$key2][$key3]) ? $arr[$key][$key2][$key3] : "";
1131 }
1132 return isset($_REQUEST[$key][$key2][$key3]) ? $this->clean($_REQUEST[$key][$key2][$key3]) : "";
1133 }
1134 /**
1135 * get base url
1136 *
1137 */
1138 public function get_base_url(){
1139 return plugin_dir_url(__FILE__);
1140 }
1141 /**
1142 * get plugin direcotry name
1143 *
1144 */
1145 public function plugin_dir_name(){
1146 if(!empty($this->plugin_dir)){
1147 return $this->plugin_dir;
1148 }
1149 if(empty(self::$path)){
1150 self::$path=$this->get_base_path();
1151 }
1152 $this->plugin_dir=basename(self::$path);
1153 return $this->plugin_dir;
1154 }
1155 /**
1156 * get plugin slug
1157 *
1158 */
1159 public function get_slug(){
1160 return plugin_basename(__FILE__);
1161 }
1162 public function do_actions(){
1163 if(!is_object(self::$plugin) ){ $this->plugin_api(); }
1164 if(is_object(self::$plugin) && method_exists(self::$plugin,'valid_addons')){
1165 return self::$plugin->valid_addons();
1166 }
1167
1168 return false;
1169 }
1170 /**
1171 * Returns the physical path of the plugin's root folder
1172 *
1173 */
1174 public function get_base_path(){
1175 return plugin_dir_path(__FILE__);
1176 }
1177 /**
1178 * Writes an error message to the Gravity Forms log.
1179 *
1180 */
1181 public function log_msg($message,$type=""){
1182 if (class_exists("GFLogging")) {
1183 GFLogging::include_logger();
1184 $slug=$this->plugin_dir_name();
1185 $log_type=KLogger::DEBUG;
1186 if($type == "error"){
1187 $log_type=KLogger::ERROR;
1188 }
1189 GFLogging::log_message($slug, $message,$log_type);
1190 }
1191 }
1192 /**
1193 * get api object
1194 *
1195 * @param mixed $settings
1196 * @return vxg_api_zoho
1197 */
1198 public function get_api($crm=""){
1199 $api = false;
1200 if(!class_exists("vxg_googlesheets_api"))
1201 require_once($this->get_base_path()."api/api.php");
1202
1203 $api= new vxg_googlesheets_api($crm);
1204
1205 return $api;
1206 }
1207 /**
1208 * get gravity forms entry
1209 *
1210 */
1211 public function get_gf_entry($entry_id){
1212 $entry=array();
1213 // Fetch entry (use new GF API from version 1.8)
1214 if( class_exists( 'GFAPI' ) && !empty( $entry_id ) ) {
1215 $entry = GFAPI::get_entry( $entry_id );
1216 } elseif( class_exists( 'RGFormsModel' ) && !empty( $entry_id ) ) {
1217 $entry = RGFormsModel::get_lead( $entry_id );
1218 }
1219 return $entry;
1220 }
1221 /**
1222 * get data object
1223 *
1224 */
1225 public function get_data_object(){
1226 require_once(self::$path . "includes/data.php");
1227 if(!is_object($this->data))
1228 $this->data=new vxg_googlesheets_data();
1229 return $this->data;
1230 }
1231 public function gf_entry_created_before($entry, $form){
1232 $this->gf_entry_created($entry, $form);
1233 }
1234 public function gf_entry_created_after($entry, $form){
1235 $this->gf_entry_created($entry, $form,'after_submit');
1236 }
1237 /**
1238 * gravity forms entry created
1239 *
1240 * @param mixed $entry
1241 * @param mixed $form
1242 */
1243 public function gf_entry_created( $entry, $form, $event='submit'){
1244
1245 if(is_array($entry) && isset($entry['status']) && $entry['status'] == 'active' && empty($entry['partial_entry_percent'])){
1246
1247 $entry_id=$this->post('id',$entry);
1248 if($this->do_actions()){
1249 do_action('vx_addons_save_entry',$entry_id,$entry,'gf',$form);
1250 }
1251 $this->push($entry,$form,$event,false);
1252 }
1253 }
1254
1255 public function gf_entry_paid_normal($entry,$pay_info){
1256 $this->gf_entry_paid($entry,$pay_info);
1257 }
1258 public function gf_entry_paid_subscription($entry,$pay_info){
1259 $this->gf_entry_paid($entry,$pay_info,'subscription_paid');
1260 }
1261 public function gf_entry_paid($entry,$pay_info,$event='paid'){
1262 // if($entry['payment_status'] == 'Paid'){
1263 $entry_id=$this->post('id',$entry);
1264 $form=array('id'=>$entry['form_id'],'title'=>'form id '.$entry['form_id']);
1265 if(!empty($feed['meta']['feedName'])){
1266 $form['title']=$feed['meta']['feedName'];
1267 }
1268 if($this->do_actions()){
1269 do_action('vx_addons_save_entry',$entry_id,$entry,'gf',$form);
1270 }
1271 $this->push($entry,$form,'paid',false);
1272 // }
1273 }
1274 /**
1275 * Send entry to crm on update
1276 *
1277 * @param mixed $form
1278 * @param mixed $lead_id
1279 */
1280 public function update_entry($form,$lead_id){
1281
1282 $meta=get_option($this->type.'_settings',array());
1283
1284 if(!empty($meta['update']) || isset($_POST[$this->id.'_update'])){
1285 $entry=$this->get_gf_entry($lead_id);
1286
1287 $push=$this->push($entry,$form,'update');
1288 if(!empty($push['msg'])){
1289 $this->screen_msg($push['msg'],$push['class']);
1290 }
1291 }
1292
1293 }
1294 public function update_entry_api($entry){
1295 $meta=get_option($this->type.'_settings',array());
1296 if(!empty($meta['update']) || isset($_POST[$this->id.'_update'])){
1297 if(!empty($entry['form_id'])){
1298 $form = GFAPI::get_form( $entry['form_id'] );
1299 $push=$this->push($entry,$form,'update');
1300 if(!empty($push['msg'])){
1301 $this->screen_msg($push['msg'],$push['class']);
1302 }
1303 }
1304 }
1305 }
1306 public function entry_status($id,$status,$old){
1307 $meta=get_option($this->type.'_settings',array());
1308 $option = '';
1309 if($status == 'active'){
1310 $option= 'restore';
1311 }else if($status == 'trash'){
1312 $option='delete';
1313 }
1314
1315 if( !empty($option) && !empty($meta[$option])){
1316 // $option= $option == 'restore' ? '' : $option;
1317 $entry=$this->get_gf_entry($id);
1318 $form=array();
1319 if(!empty($entry['form_id'])){
1320 $form = RGFormsModel::get_form_meta($entry['form_id']);
1321 }
1322 $this->push($entry,$form,$option);
1323 }
1324
1325 }
1326 /**
1327 * push form data to crm
1328 *
1329 * @param mixed $entry
1330 * @param mixed $form
1331 * @param mixed $is_admin
1332 */
1333 public function push($entry, $form,$event="",$is_admin=false,$log=""){
1334
1335
1336 $data_db=$this->get_data_object();
1337 $log_id=''; $feeds_meta=array();
1338 if(!empty($log)){
1339 if(isset($log['id'])){
1340 $log_id=$log['id'];
1341 }
1342 $log_feed=$data_db->get_feed($log['feed_id']);
1343 if(!empty($log_feed)){
1344 $feeds_meta=array($log_feed);
1345 }
1346 }else{
1347 //get feeds of a form
1348 $feeds=$data_db->get_feed_by_form($entry['form_id'],true);
1349
1350 if(is_array($feeds) && count($feeds)>0){
1351 $k=1000; $e=2000; $i=1;
1352 foreach($feeds as $feed){
1353 $data=isset($feed['data']) ? json_decode($feed['data'],true) : array();
1354 $meta=isset($feed['meta']) ? json_decode($feed['meta'],true) : array();
1355 $feed['meta']=$meta;
1356 $feed['data']=$data;
1357 $object=$this->post('object',$feed);
1358 if(!empty($data['contract_check']) || !empty($data['account_check'])|| !empty($data['contact_check'])){
1359 if($object == 'Order'){
1360 $feeds_meta[$e++]=$feed;
1361 }else{
1362 $feeds_meta[$k++]=$feed;
1363 }
1364 }else{
1365 $feeds_meta[$i++]=$feed;
1366 }
1367 }
1368 ksort($feeds_meta);
1369 //
1370 }
1371 }
1372 $form_id=0;
1373 if(isset($form['id'])){
1374 $form_id=$form['id'];
1375 }
1376
1377 $entry_id=$this->post('id',$entry);
1378 if(isset($entry['__vx_id'])){
1379 $entry_id=$entry['__vx_id'];
1380 }else{
1381 $entry=apply_filters('vx_crm_post_fields',$entry,$entry_id,'gf',$form);
1382 }
1383 //var_dump($entry); die();
1384 $n=0;
1385 $screen_msg_class="updated"; $notice="";
1386 if(is_array($feeds_meta) && count($feeds_meta)>0){
1387 while($feed=current($feeds_meta)){
1388 next($feeds_meta);
1389 $n++;
1390 $temp=array();
1391 $force_send=false;
1392 $post_comment=true;
1393 $screen_msg="";
1394 $parent_id=0;
1395 if(isset($entry['__vx_parent_id'])){
1396 $parent_id=$entry['__vx_parent_id'];
1397 }
1398 $object=$this->post('object',$feed);
1399 $data=$feed['data'];
1400 $meta=$feed['meta'];
1401 $account=$this->post('account',$feed);
1402
1403 $info=$this->get_info($account);
1404 $info_data=array();
1405 if(isset($info['data'])){
1406 $info_data=$info['data'];
1407 }
1408 $api_type=$this->post('api',$info_data);
1409
1410 if(!empty($object) && in_array($event,array('restore','update','delete','add_note','delete_note'))){
1411
1412 $search_object=$object;
1413 if(in_array($event,array('add_note','delete_note')) && !empty($log)){
1414
1415 if($event == 'add_note'){
1416 $note=json_decode($log['data'],true);
1417 if(!empty($note['Title']['value'])){
1418 self::$note=array('id'=>$log['parent_id']);
1419 self::$note['title']=$note['Title']['value'];
1420 self::$note['body']=$note['Body']['value'];
1421 }
1422 }
1423 }
1424 if($event == 'delete_note' && !empty(self::$note)){
1425 $parent_id=self::$note['id'];
1426 }
1427
1428 if(in_array($event,array('delete_note','add_note'))){
1429 //check feed
1430 $order_notes=$this->post('entry_notes',$data); //if notes sync not enabled in feed return
1431
1432 if( empty($order_notes)){
1433 continue;
1434 }
1435
1436 //change main object to Note
1437 $feed['related_object']=$object;
1438 $object=$feed['object']=!empty($meta['note_list']) ? 'ContentNote' : 'Note';
1439 }
1440 if($event == 'delete_note'){
1441 //when deleting note search note object
1442 $search_object='Note';
1443 }
1444 $_data=$this->get_data_object();
1445 $feed_log=$_data->get_feed_log($feed['id'],$entry_id,$search_object,$parent_id);
1446
1447 if($event == 'restore' && $feed_log['status'] != 5) { // only allow successfully deleted records
1448 continue;
1449 }
1450 if( in_array($event,array('update','delete') ) && !in_array($feed_log['status'],array(1,2) ) ){ // only allow successfully sent records
1451 continue;
1452 }
1453
1454 if(empty($feed_log['crm_id']) || empty($feed_log['object']) || $feed_log['object'] != $search_object){
1455
1456 continue;
1457 }
1458 //if($event !='restore'){
1459 $feed['crm_id']=$feed_log['crm_id'];
1460 // unset($feed['primary_key']);
1461 //}
1462 $feed['event']=$event;
1463 // add note and save related extra info
1464 if( $event == 'add_note' && !empty(self::$note)){
1465 $temp=array('Title'=>array('value'=>self::$note['title']),'Body'=>array('value'=>self::$note['body']),'ParentId'=>array('value'=> $feed['crm_id']));
1466
1467 $parent_id=self::$note['id'];
1468 $object_link=$feed_log['crm_id'];
1469 $feed['note_object_link']='<a href="'.$feed_log['link'].'" target="_blank">'.$feed_log['crm_id'].'</a>';
1470 }
1471 // delete not and save extra info
1472 if( $event == 'delete_note'){
1473
1474 $feed_log_arr= json_decode($feed_log['extra'],true);
1475 if(isset($feed_log_arr['note_object_link'])){
1476 $feed['note_object_link']=$feed_log_arr['note_object_link'];
1477 }
1478 $temp=array('ParentId'=>array('value'=> $feed['crm_id']));
1479 }
1480 //delete object
1481 if( $event == 'delete'){
1482 $temp=array('Id'=>array('value'=> $feed['crm_id']));
1483 }
1484 //
1485 if(!in_array($event , array('update','restore') )){
1486 //do not apply filters when adding note , deleting note , entry etc
1487 $force_send=true;
1488 }
1489 if($event == 'restore'){ // send as new entry
1490 // unset($feed['crm_id']);
1491 }
1492 //do not post comment in al other cases
1493 $post_comment=false;
1494
1495 }
1496 // var_dump(self::$note,$object,$feed['note_object'],$feed['object'],$feed['crm_id'],$feed['event'],$temp,$force_send);
1497 //continue;
1498 $feed_event=$this->post('manual_export',$data);
1499 if(!$is_admin){
1500 if($event == 'submit' && $feed_event != ''){ //if manual export is yes
1501 continue;
1502 }
1503 if($event == 'after_submit' && $feed_event != '4'){
1504 continue;
1505 }
1506 if($event == 'subscription_paid' && $feed_event != '3'){
1507 continue;
1508 }
1509 if($event == 'paid' && $feed_event != '2'){ // only process paid event, if set in feed
1510 continue;
1511 }
1512 }
1513 if(!$force_send && isset($data['map']) && is_array($data['map']) && count($data['map'])>0){
1514
1515
1516 $custom= isset($meta['fields']['fields']) && is_array($meta['fields']['fields']) ? $meta['fields']['fields'] : array();
1517 $skip_feed=false;
1518 foreach($data['map'] as $k=>$v){
1519
1520 $value=false;
1521 if(!empty($v)){ //if value not empty
1522 $field_type=$this->post('type',$v);
1523 if( !empty($field_type) ){ //custom value
1524 $value=trim($this->post('value',$v));
1525 $value=$this->process_tags($entry,$form,$value);
1526
1527
1528 }else{ //general field
1529 $field=$this->post('field',$v);
1530 if($field !=""){
1531 if(strpos($field,'_vx_feed-') !== false){
1532 $temp_feed_id=substr($field,9);
1533 if(isset(self::$feeds_res[$temp_feed_id])){
1534 $value= !empty(self::$feeds_res[$temp_feed_id]['id']) ? self::$feeds_res[$temp_feed_id]['id'] : '';
1535 }else if($n < count($feeds_meta) ){ // it is not last element
1536 $feeds_meta[]=$feed; $skip_feed=true;
1537 break;
1538 }
1539 }else{
1540 $value=$this->verify_field_val($entry,$form,$field,$k,$custom);
1541 $gf_field= RGFormsModel::get_field($form, $field);
1542 if(!empty($gf_field) && in_array($gf_field->type,array('checkbox')) && !empty($gf_field->choices) && count($gf_field->choices)< 2 ){
1543 if(!empty($value)){ $value='Yes'; }else{ $value='No'; } //single checkbox or acceptance field , googlesheets web2lead accepts only 1 for checkbox field
1544 }
1545 }
1546 }
1547
1548 }
1549
1550 if($value!== false){
1551 if(isset($custom[$k]['name'])){
1552 $temp[$k]=array('value'=>$value,'label'=>$custom[$k]['label']);
1553 }else if(isset($custom[$k]['name_c'])){
1554 $temp[$custom[$k]['name_c']]=array('value'=>$value,'label'=>$custom[$k]['name_c']);
1555 }
1556 }
1557 }
1558 }
1559 //var_dump($temp,$entry,$form); die();
1560 if($skip_feed){ continue; }
1561
1562 if($object == 'Order'){
1563 $temp['Status']=array('value'=>'Draft','label'=>'Draft');
1564 }
1565 //change owner id
1566 if(isset($data['owner']) && !empty($data['user'])){
1567 $temp['OwnerId']=array('value'=>apply_filters('vx_assigned_user_id',$data['user'],$this->id,$feed['id'],$entry,$form),'label'=>'Owner ID');
1568 }
1569
1570 //add account or contract
1571
1572 if(!empty($data['contract_check']) && !empty($data['object_contract'])){
1573 $contract_feed=$data['object_contract'];
1574 if( isset(self::$feeds_res[$contract_feed]) ){
1575
1576 $contract_res=self::$feeds_res[$contract_feed];
1577 /////
1578 if(!empty($contract_res['id'])){
1579 $temp['ContractId']=array('value'=> $contract_res['id'],'label'=>'Contract ID');
1580 }else{ //if empty continue
1581 continue;
1582 }
1583 }
1584 }
1585 if(!empty($data['account_check']) && !empty($data['object_account'])){
1586 $account_feed=$data['object_account'];
1587 if( isset(self::$feeds_res[$account_feed]) ){
1588
1589 $account_res=self::$feeds_res[$account_feed];
1590 /////
1591 if(!empty($account_res['id'])){
1592 $temp['AccountId']=array('value'=> $account_res['id'],'label'=>'Account ID');
1593 }else{ //if empty continue
1594 continue;
1595 }
1596 }
1597
1598 }
1599 if(!empty($data['contact_check']) && !empty($data['contact_feed'])){
1600 $contact_feed=$data['contact_feed'];
1601 if( isset(self::$feeds_res[$contact_feed]) ){
1602
1603 $contact_res=self::$feeds_res[$contact_feed];
1604 /////
1605 if(!empty($contact_res['id'])){
1606 $temp['contact_id']=$contact_res['id'];
1607 }else{ //if empty continue
1608 // continue;
1609 }
1610 }
1611 }
1612 //add note
1613 if(!empty($data['note_check']) ){
1614 $entry_note=''; $entry_note_title='';
1615 if(!empty($data['note_fields']) && is_array($data['note_fields'])){
1616 $data['note_val']='{'.implode("}\n{",$data['note_fields'])."}\n";
1617 }
1618 if(!empty($data['note_val'])){
1619 $entry_note=$this->process_tags($entry,$form,$data['note_val']);
1620 if(empty($entry_note_title)){
1621 $entry_note_title=substr($entry_note,0,20);
1622 }
1623 if(!empty($entry_note)){
1624 $feed['__vx_entry_note']=array('Title'=>$entry_note_title,'Body'=>$entry_note);
1625 }
1626 }
1627 }
1628 }
1629 $no_filter=true;
1630 $temp=apply_filters($this->id.'_post_data', $temp ,$entry);
1631 if(isset($_REQUEST['bulk_action']) && $_REQUEST['bulk_action'] =="send_to_crm_bulk_force" && !empty($log_id)){
1632 $force_send=true;
1633 }
1634 if(!$force_send && $this->post('optin_enabled',$data) == "1"){ //apply filters if not sending by force and optin is enabled
1635 $no_filter=$this->check_filter($data,$entry,$form);
1636 $res=array("status"=>"4","extra"=>array("filter"=>$this->filter_condition),"data"=>$temp);
1637 }
1638
1639 //echo json_encode($temp).'---------'.json_encode($entry); die();
1640
1641
1642 if($no_filter){ //get $res if no filter , other wise use filtered $res
1643 $api=$this->get_api($info);
1644 $feed_arr=$feed;
1645 if(is_array($meta) && is_array($data)){
1646 $feed_arr=array_merge($meta,$data,$feed);
1647 }
1648 $res=$api->push_object($feed['object'],$temp,$feed_arr);
1649 if($object == 'Contact' && !empty($res['id'])){
1650 $entry['sf_contact_id']=$res['id'];
1651 }
1652 }
1653
1654 $feed_id=$this->post('id',$feed);
1655 self::$feeds_res[$feed_id]=$res;
1656 $status=$res['status']; $error="";
1657 $id=$this->post('id',$res);
1658 $added=false;
1659 if($api_type != 'web' && !empty($id)){
1660 $added=true;
1661 }else if($api_type == 'web' && !empty($status)){
1662 $added=true;
1663 }
1664 if( $added){
1665 $id=$res['id'];
1666 $action=$this->post('action',$res);
1667 if($action == "Added"){
1668 if(empty($res['link'])){
1669 $msg=sprintf(__('Successfully Added to Google Sheets (%s) with ID # %s .', 'gravity-forms-googlesheets-crm'),$feed['object'],$res['id']);
1670 }else{
1671 $msg=sprintf(__('Successfully Added to Google Sheets (%s) with ID # %s . View entry at %s', 'gravity-forms-googlesheets-crm'),$feed['object'],$res['id'],$res['link']);
1672 }
1673 $screen_msg=__( 'Entry added in Google Sheets', 'gravity-forms-googlesheets-crm');
1674 }else{
1675 if(empty($res['link'])){
1676 $msg=sprintf(__('Successfully Updated to Google Sheets (%s) with ID # %s . View entry at %s', 'gravity-forms-googlesheets-crm'),$feed['object'],$res['id'],$res['link']);
1677 }else{
1678 $msg=sprintf(__('Successfully Updated to Google Sheets (%s) with ID # %s .', 'gravity-forms-googlesheets-crm'),$feed['object'],$res['id']);
1679 }
1680 $screen_msg=__( 'Entry updated in Google Sheets', 'gravity-forms-googlesheets-crm');
1681 }
1682
1683
1684 }else if($this->post('status',$res) == 4){
1685 $screen_msg=$msg=__( 'Entry filtered', 'gravity-forms-googlesheets-crm');
1686 }else{
1687 $status=0; $screen_msg_class="error";
1688 $screen_msg=__('Errors when adding to Google Sheets. Entry not sent! Check the Entry Notes below for more details.' , 'gravity-forms-googlesheets-crm' );
1689 if($log_id!=""){
1690 //message for bulk actions in logs
1691 $screen_msg=__('Errors when adding to Google Sheets. Entry not sent' , 'gravity-forms-googlesheets-crm' );
1692 }
1693 $msg=sprintf(__('Error while creating %s', 'gravity-forms-googlesheets-crm'),$feed['object']);
1694 if($this->post('error',$res)!=""){
1695 $error= is_array($res['error']) ? json_encode($res['error']) : $res['error'];
1696 $msg.=" ($error)";
1697
1698 $_REQUEST['VXGGoogle SheetsError']=$msg; //front end form error for admin only
1699 }
1700 if(!$is_admin){
1701 $info_data['msg']=$msg;
1702 $this->send_error_email($info_data,$entry,$form);
1703 }
1704 }
1705 //insert log
1706 $arr=array("object"=>$feed["object"],"form_id"=>$form_id,"status"=>$status,"entry_id"=>$entry_id,"crm_id"=>$id,"meta"=>$error,"time"=>date('Y-m-d H:i:s'),"data"=>$temp,"response"=>$this->post('response',$res),"extra"=>$this->post('extra',$res),"feed_id"=>$this->post('id',$feed),"link"=>$this->post('link',$res),'parent_id'=>$parent_id,'event'=>$event);
1707
1708
1709 $settings=get_option($this->type.'_settings',array());
1710 if($this->post('disable_log',$settings) !="yes"){
1711 $insert_id=$data_db->insert_log($arr,$log_id);
1712 }
1713 if(!empty($insert_id)){ //
1714 $log_url=admin_url( 'admin.php?page=gf_edit_forms&view=settings&subview='.$this->id.'&tab=log&log_id='.$insert_id.'&id='.$form_id);
1715 $log_link=' <a href="'.$log_url.'" class="vx_log_link" data-id="'.$insert_id.'">'.__('View Detail','gravity-forms-googlesheets-crm')."</a>";
1716 $screen_msg.=$log_link;
1717 }
1718 if($post_comment){
1719 //insert entry comment
1720
1721 // $this->add_note($entry["id"], $msg);
1722 }
1723
1724 if($notice!=""){
1725 $notice.='<br/>';
1726 }
1727 $notice.='<b>'.$object.': </b>'.$screen_msg;
1728
1729 }
1730 }
1731
1732 return array("msg"=>$notice,"class"=>$screen_msg_class);
1733 }
1734 public function process_tags($entry,$form,$value,$crm_field_id='',$custom=''){
1735 $value = preg_replace( '/\{(?=\d+(?::|\}))/', '{:', $value ); // Prepend ":" to merge tags in the form {field_id:modifier} #46626
1736 $value = \GFCommon::replace_variables( $value, $form, $entry, false, false, true, 'text' );
1737 //starts with { and ends } , any char in brackets except {
1738 preg_match_all('/\{[^\{]+\}/',$value,$matches);
1739 if(!empty($matches[0])){
1740 $vals=array();
1741 foreach($matches[0] as $m){
1742 $m=trim($m,'{}');
1743 $val_cust=$this->verify_field_val($entry,$form,$m,$crm_field_id,$custom);
1744 if(is_array($val_cust)){ $val_cust=trim(implode(' ',$val_cust)); }
1745 $vals['{'.$m.'}']=$val_cust;
1746 }
1747
1748 $value=str_replace(array_keys($vals),array_values($vals),$value);
1749 }
1750 return $value;
1751 }
1752 /**
1753 * Send error email
1754 *
1755 * @param mixed $info
1756 * @param mixed $entry
1757 * @param mixed $form
1758 */
1759 public function send_error_email($info,$entry,$form){
1760 if( trim($this->post('error_email',$info))!=""){
1761 $subject="Error While Posting to Google Sheets";
1762 $entry_link=add_query_arg(array('page' => 'gf_entries','view'=>'entry', 'id' => $entry['form_id'],'lid'=>$entry['id']), admin_url('admin.php'));
1763 $page_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
1764
1765 $detail=array(
1766 "Time"=>date('d/M/y H:i:s',current_time('timestamp')),
1767 "Page URL"=>'<a href="'.$page_url.'" style="word-break:break-all;">'.$page_url.'</a>',
1768 "Entry ID"=>'<a href="'.$entry_link.'" target="_blank" style="word-break:break-all;">'.$entry_link.'</a>'
1769 );
1770 if(isset($form['title'])){
1771 $detail["Form Name"]=$form['title'];
1772 $detail["Form Id"]=$form['id'];
1773 }
1774 $email_info=array("msg"=>$info['msg'],"title"=>__('Google Sheets','gravity-forms-googlesheets-crm')." Error","info_title"=>"More Detail","info"=>$detail);
1775 $email_body=$this->format_user_info($email_info,true);
1776
1777 $error_emails=explode(",",$info['error_email']);
1778 $headers = array('Content-Type: text/html; charset=UTF-8');
1779 foreach($error_emails as $email)
1780 wp_mail(trim($email),$subject, $email_body,$headers);
1781 }
1782 }
1783 /**
1784 * Get Objects from local options or from googlesheets
1785 *
1786 * @param mixed $check_option
1787 * @return array
1788 */
1789 public function get_objects($info="",$refresh=false){
1790
1791 $data=$this->post('data',$info);
1792 $api_type=$this->post('api',$data);
1793
1794 if(empty($info)){
1795 $option=get_option($this->id.'_meta',array());
1796
1797 return !empty($option['objects']) ? $option['objects'] : '';
1798 }
1799 $objects=array();
1800 $meta=$this->post('meta',$info);
1801
1802 if(! isset($meta['objects'])){
1803 $refresh=true;
1804 }else{
1805 $objects=$meta['objects'];
1806 }
1807 //get objects from googlesheets
1808 if($refresh){
1809 $api=$this->get_api($info);
1810 $objects=$api->get_crm_objects();
1811
1812 if(is_array($objects)){
1813 $option=get_option($this->id.'_meta',array());
1814
1815 $option_objects=$objects;
1816 if(!empty($option['objects']) && is_array($option['objects'])){
1817 $option_objects=array_merge($option['objects'],$option_objects);
1818 }
1819
1820 $option['objects']=$option_objects;
1821 update_option($this->id.'_meta',$option); //save objects for logs search option
1822 $meta["objects"]=$objects;
1823 $this->update_info(array("meta"=>$meta),$info['id']);
1824 }
1825 }
1826 return $objects;
1827 }
1828
1829 /**
1830 * check if user conected to crm
1831 *
1832 * @param mixed $settings
1833 */
1834 public function api_is_valid($info="") {
1835 if(!empty($info['data']['api']) && $info['data']['api'] == 'web'){
1836 return true;
1837 }
1838
1839 if(isset($info['data']) && is_array($info['data']) && isset($info['data']['access_token']) && !empty($info['data']['access_token'])){
1840 return true;
1841 }else{
1842 return false;}
1843 }
1844 }
1845
1846 endif;
1847 $vxg_googlesheets=new vxg_googlesheets();
1848 $vxg_googlesheets->instance();
1849 $vx_gf['vxg_googlesheets']='vxg_googlesheets';
1850