<?php
add_action( 'admin_enqueue_scripts', 'cpabcal_feedback_insert_adminScripts', 1); 
add_action( 'wp_ajax_cpabcal_feedback', 'cpabcal_feedback' );
$cpabcal_plugfile =  'cpabc_appointments.php';
$cpabcal_plugslug = 'appointment-booking-calendar';
$cpabcal_supportlink = 'https://abc.dwbooster.com/contact-us';
$cpabcal_supportlink_full = $cpabcal_supportlink . '?priority-support=yes';

if (!get_option('installed_'.$cpabcal_plugslug, ''))
    update_option('installed_'.$cpabcal_plugslug, time() );

function cpabcal_feedback_insert_adminScripts($hook) {
    if( 'plugins.php' == $hook  )            
    {
        wp_enqueue_style('wp-jquery-ui-dialog');
        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui-dialog');       
        add_action( 'admin_footer', 'cpabcal_feedback_javascript' );            
    }
}
    


// This function is used only if explicitly accepted (opt-in) by the user
function cpabcal_feedback() {
    global $cpabcal_plugfile, $cpabcal_plugslug;
    if (
		isset( $_POST['_wpnonce'] ) &&
		wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'abc-feedback' )
	) {    
        $cpabcal_plugin_data = get_plugin_data( dirname(dirname(__FILE__)).'/'.$cpabcal_plugfile );
        $cpabcal_plugin_version = $cpabcal_plugin_data['Version'];
        $time = time() - get_option('installed_'.$cpabcal_plugslug, '');
        $data = array(
                     'answer' => sanitize_textarea_field(@$_POST["answer"]),
                     'otherplugin' => sanitize_textarea_field(@$_POST["opinfo"]),
                     'otherinfo' => sanitize_textarea_field(@$_POST["oinfo"]),
                     'plugin' => ($cpabcal_plugin_data['Name']),
                     'pluginv' => ($cpabcal_plugin_version),
                     'wordpress' => (get_bloginfo( 'version' )),
                     'itime' => ($time),
                     'phpversion' => (phpversion ())
                     );
        if (@$_POST["onymous"] == 'false') // send this data only if explicitly accepted
        {
            $current_user = wp_get_current_user();
            $data['email'] = ($current_user->user_email);
            $data['website'] = ($_SERVER['HTTP_HOST']);
            $data['url'] = (get_site_url());
            // for detecting theme and plugin conflicts:
            $data['theme'] = (wp_get_theme()->get('Name') . " " .wp_get_theme()->get('Version'));
            $plist = "";
            $activeplugins = get_option('active_plugins');
            $plugins =  get_plugins();
            foreach ($activeplugins as $plugin)
               $plist .= $plugins[$plugin]["Title"]." ".$plugins[$plugin]["Version"]."\n";
            $data['plugins'] = ($plist);
        }
        
        //extract data from the post
        //set POST variables
        $url = 'https://wordpress.dwbooster.com/licensesystem/debug-data.php';
        $fields = $data;    
        
        wp_remote_post( 
                         $url,
                         array ( 'body' => $fields )
                      );
        
	    wp_die(); // this is required to terminate immediately and return a proper response
    }
}


function cpabcal_feedback_javascript() { 
    global $cpabcal_plugslug, $cpabcal_supportlink, $cpabcal_supportlink_full; 
      ?>
	<script type="text/javascript">

$ = jQuery.noConflict()
$(window).on('load',function () {
        document.querySelector('[data-slug="<?php echo esc_js($cpabcal_plugslug); ?>"] .deactivate a').addEventListener('click', function(event){
            event.preventDefault()
            var urlRedirect = document.querySelector('[data-slug="<?php echo esc_js($cpabcal_plugslug); ?>"] .deactivate a').getAttribute('href');
                      
            $('<div title="QUICK FEEDBACK"><div style="padding:10px;">'+
               '<style type="text/css">.abcreasonblock { margin-top:8px; }</style>'+
      		   '<h3><strong>If you have a moment, please let us know why you are deactivating:</strong></h3>'+
               '<form id="cpfeedbackform">'+ 
               
               '<div class="abcreasonblock"><input type="radio" name="abcm_reason" onclick="cpabcal_update_reason(this);" value="no-work"> The plugin didn\'t work<br />'+
                '<div id="abcm_nowork" style="margin-left:25px;display:none;padding:10px;border:1px dotted gray;color:#660000"><strong>We can help!</strong> We offer <strong>free support</strong> for this plugin with <strong>timely answers</strong> and <strong>solution for almost 100% of cases</strong>. <a href="<?php echo esc_js($cpabcal_supportlink_full); ?>"><strong>Send a message to support</strong></a> and get a solution.</div></div>'+
                
               
               '<div class="abcreasonblock"><input type="radio" name="abcm_reason" onclick="cpabcal_update_reason(this);" value="no-paypal"> Don\'t want to use PayPal<br />'+
                '<div id="abcm_nopaypal" style="margin-left:25px;display:none;padding:10px;border:1px dotted gray;color:#660000"><strong>No problem, we have a solution!</strong> There is a similar free plugin without PayPal, the <a href="plugin-install.php?tab=plugin-information&plugin=appointment-hour-booking&TB_iframe=true" class="thickbox open-plugin-details-modal" onclick="javascript:cpabcal_apphourbk(this);"><strong>Appointment Hour Booking</strong></a>. Questions? <a href="<?php echo esc_js($cpabcal_supportlink_full); ?>"><strong>Send us a message</strong></a> and get a solution.</div></div>'+
                                
                
               '<div class="abcreasonblock"><input type="radio" name="abcm_reason" onclick="cpabcal_update_reason(this);" value="-"> I don\'t like to share this information with you<br /></div>'+
               
               '<div class="abcreasonblock"><input type="radio" name="abcm_reason" onclick="cpabcal_update_reason(this);" value="temporary-deactivation"> It\'s a temporary deactivation. I\'m just debugging an issue.<br /></div>'+
               
               '<div class="abcreasonblock"><input type="radio" name="abcm_reason" onclick="cpabcal_update_reason(this);" value="better-plugin"> I found a better plugin<br />'+
               '<div id="abcm_otherplugin" style="margin-left:25px;display:none;"><input type="text" name="abcm_otherpinfo" placeholder="What\'s the plugin name?" style="width:100%"></div></div>'+
               
               '<div class="abcreasonblock"><input type="radio" name="abcm_reason" onclick="cpabcal_update_reason(this);" value="other"> Other<br />'+
               '<div id="abcm_other" style="margin-left:25px;display:none;font-weight:bold;">Kindly tell us the reason so we can improve.<br /><input type="text" name="abcm_otherinfo" style="width:100%"></div></div>'+
            
               '<div id="abcnofeedback" style="display:none;margin-top:30px;text-align:right"><input type="checkbox" name="cpabcanonymous" value="yes"> Anonymous feedback</div>'+
               
               '</form>'+               
      		   '</div></div>'
      		  ).dialog({
                  width:'600',
      			dialogClass: 'wp-dialog',
                  modal: true,
                  close: function(event, ui)
                  {
                      $(this).dialog("close");
                      $(this).remove();
                  },
                  closeOnEscape: true,
                  buttons: [
                      {
                        id: 'abcdeactivatebtn',
                        text: "Skip & Deactivate", 
                        click: function() {                               
                                   var answer = $("input[name='abcm_reason']:checked").val();
                                   if (answer == undefined || answer == '' || answer == '-')
                                       window.location.href = urlRedirect; 
                                   else
                                   {
                                       var opinfo = $("input[name='abcm_otherpinfo']").val();
                                       var oinfo = $("input[name='abcm_otherinfo']").val();
                                       var isAnonymous = $("input[name='cpabcanonymous']:checked").length > 0;
                                       var data = {
		                                   	'action': 'cpabcal_feedback',
                                            '_wpnonce' : '<?php echo esc_js(wp_create_nonce( 'abc-feedback' )); ?>',
		                                   	'answer': $("input[name='abcm_reason']:checked").val(),
                                            'opinfo': $("input[name='abcm_otherpinfo']").val(),
                                            'oinfo': $("input[name='abcm_otherinfo']").val(),
                                            'onymous': $("input[name='cpabcanonymous']:checked").length > 0
		                                   };                                       
                                       $.post(ajaxurl, data, function(response) {
			                               window.location.href = urlRedirect;      						           
		                               });    
                                   }                                   
      				           }                            
                      },
                      {
                        text: "We can help: Support Service", 
                        click: function() {
                                   window.open('<?php echo esc_js($cpabcal_supportlink_full); ?>');
      						       $(this).dialog("close");
      				           }                            
                      },
                      {
                        text: "Cancel", 
                        "class": 'button button-primary button-close',
                        click: function() {
      						       $(this).dialog("close");
      				           }                            
                      }
                  ]
              });
        })
});

function cpabcal_apphourbk(item)
{
    $("#abcm_nopaypal").closest('.ui-dialog-content').dialog('close');
    return true;
}

function cpabcal_update_reason(field)
{
    document.getElementById("abcdeactivatebtn").value = 'Submit & Deactivate';
    document.getElementById("abcdeactivatebtn").innerHTML = '<span class="ui-button-text">Submit &amp; Deactivate</span>';
    document.getElementById("abcnofeedback").style.display = '';
    document.getElementById("abcm_other").style.display = 'none';
    document.getElementById("abcm_otherplugin").style.display = 'none';
    document.getElementById("abcm_nowork").style.display = 'none';
    document.getElementById("abcm_nopaypal").style.display = 'none';
    if (field.value == '-')
    {
        document.getElementById("abcdeactivatebtn").value = 'Skip & Deactivate';
        document.getElementById("abcdeactivatebtn").innerHTML = '<span class="ui-button-text">Skip &amp; Deactivate</span>'; 
        document.getElementById("abcnofeedback").style.display = 'none';        
    }    
    else if (field.value == 'other')
        document.getElementById("abcm_other").style.display = '';
    else if (field.value == 'better-plugin')
        document.getElementById("abcm_otherplugin").style.display = '';
    else if (field.value == 'no-work')
        document.getElementById("abcm_nowork").style.display = '';    
    else if (field.value == 'no-paypal')
        document.getElementById("abcm_nopaypal").style.display = ''; 
}

	</script><?php
}

?>