morganestes
4/27/2016 - 5:19 PM

Integrates Olark with WordPress and Easy Digital Downloads by displaying user information.

Integrates Olark with WordPress and Easy Digital Downloads by displaying user information.

<?php
/*
 * Plugin Name: Olark In WP
 * Plugin URI: https://www.nosegraze.com
 * Description: Integrates Olark with WordPress and Easy Digital Downloads by displaying user information.
 * Version: 1.0
 * Author: Nose Graze
 * Author URI: https://www.nosegraze.com
 * License: GPL2
 * 
 * This is a sample plugin for WordPress. You'll need to modify this code a little, put it inside a folder,
 * zip that up, and then install that zip file like a plugin (Plugins > Add New > Upload).
 * 
 * @package olark-in-wp
 * @copyright Copyright (c) 2016, Nose Graze Ltd.
 * @license GPL2+
*/

/**
 * Olark Code
 * 
 * Adds Olark code to the footer of your WordPress site, including some custom
 * API calls to add additional user and customer information. Make sure you paste
 * in your normal Olark chat code where designated.
 *
 * @return void
 */
function olark_in_wp() {
	?>
	<script data-cfasync="false" type='text/javascript'>/*<![CDATA[*/
		<!-- paste your normal Olark script code here (but without the <script> tags since those are already included now -->

		<?php
		/*
		 * Updates their status to tell us which EDD product they're viewing.
		 */
		if ( is_single( 'download' ) ) {
			?>
			olark('api.chat.updateVisitorStatus', {
				snippet: "Viewing Product: <?php echo get_the_title(); ?>"
			});
			<?php
		}

		/*
		 * Add extra information if the current user is logged in.
		 */
		if ( is_user_logged_in() ) {
			$current_user = wp_get_current_user();
			?>
			// Update the visitor's full name with their WordPress display name.
			olark('api.visitor.updateFullName', {
				fullName: "<?php echo htmlspecialchars(
					$current_user->display_name, ENT_QUOTES ); ?>"
			});
	
			// Update the visitor's email address with their WordPress account email.
			olark('api.visitor.updateEmailAddress', {
				emailAddress: "<?php echo htmlspecialchars(
					$current_user->user_email, ENT_QUOTES ); ?>"
			});
			<?php
	
			/*
			 * Add a link to the customer's Easy Digital Downloads customer page.
			 */
			if ( class_exists( 'EDD_Customer' ) ) {
				$oCustomer = new EDD_Customer( $current_user->user_email );
				$nID = $oCustomer->id;
		
				if ( $nID != 0 ) {
					$sCustomerURL = admin_url( 'edit.php?post_type=download&page=edd-customers&view=overview&id=' . urlencode( $nID ) );
					?>
					// Sends the operator a link to the customer's profile page.
					olark('api.chat.onBeginConversation', function () {
						// Notifies the operator - the visitor does not see this
						olark('api.chat.sendNotificationToOperator', {
							body: "Customer Page: <?php echo esc_url( $sCustomerURL ); ?>"
						});
					});
					<?php
				}
			}
		}
		?>
		/*]]>*/</script>
	<?php

}

add_action( 'wp_footer', 'olark_in_wp' );