class-admin.php 3.97 KB
<?php

class WPF_Drip_Admin {

	private $slug;
	private $name;
	private $crm;

	/**
	 * Get things started
	 *
	 * @access  public
	 * @since   1.0
	*/

	public function __construct($slug, $name, $crm) {

		$this->slug = $slug;
		$this->name = $name;
		$this->crm = $crm;

		add_filter( 'wpf_configure_settings', array( $this, 'register_connection_settings' ), 15, 2 );
		add_action( 'show_field_drip_header_begin', array($this, 'show_field_drip_header_begin' ), 10, 2);
		add_action( 'show_field_drip_token_end', array($this, 'show_field_drip_token_end' ), 10, 2);

		// AJAX
		add_action( 'wp_ajax_wpf_test_connection_' . $this->slug, array($this, 'test_connection') );

		if(wp_fusion()->settings->get('crm') == $this->slug)
			$this->init();

	}

	/**
	 * Hooks to run when this CRM is selected as active
	 *
	 * @access  public
	 * @since   1.0
	*/

	public function init() {

		add_filter( 'wpf_initialize_options', array( $this, 'add_default_fields' ), 10 );

	}


	/**
	 * Loads Drip connection information on settings page
	 *
	 * @access  public
	 * @since   1.0
	*/

	public function register_connection_settings( $settings, $options ) {

		$new_settings = array();

		$new_settings['drip_header'] = array(
			'title'   => __('Drip Configuration', 'wp-fusion' ),
			'std'     => 0,
			'type'    => 'heading',
			'section' => 'setup'
		);

		$new_settings['drip_account'] = array(
			'title'   => __('Account ID', 'wp-fusion' ),
			'desc'    => __('Enter the Account ID for your Drip account (find it under Settings >> Site Setup >> 3rd Party Integrations).', 'wp-fusion' ),
			'std'     => '',
			'type'    => 'text',
			'section' => 'setup'
		);

		$new_settings['drip_token'] = array(
			'title'   => __('API Token', 'wp-fusion' ),
			'desc'    => __('Enter your Drip API token. You can find it in your Drip account <a href="https://www.getdrip.com/user/edit" target="_blank">here</a>.', 'wp-fusion' ),
			'type'    => 'api_validate',
			'section' => 'setup',
			'class'	  => 'api_key',
			'post_fields' => array('drip_account', 'drip_token')
		);

		$settings = wp_fusion()->settings->insert_setting_after('crm', $settings, $new_settings);

		return $settings;

	}


	/**
	 * Loads standard Drip field names and attempts to match them up with standard local ones
	 *
	 * @access  public
	 * @since   1.0
	*/

	public function add_default_fields( $options ) {

		if($options['connection_configured'] == true)
			$options['contact_fields']['user_email']['crm_field'] = 'email';

		return $options;

	}


	/**
	 * Puts a div around the Drip configuration section so it can be toggled
	 *
	 * @access  public
	 * @since   1.0
	*/

	public function show_field_drip_header_begin( $id, $field ) {

		echo '</table>';
		$crm = wp_fusion()->settings->get('crm');
		echo '<div id="' . $this->slug . '" class="crm-config ' . ($crm == false || $crm != $this->slug ? 'hidden' : 'crm-active') . '" data-name="' . $this->name .'" data-crm="' . $this->slug .'">';

	}

	/**
	 * Close out Active Campaign section
	 *
	 * @access  public
	 * @since   1.0
	*/


	public function show_field_drip_token_end($id, $field) {

		if($field['desc'] != '') {
			echo '<span class="description">'.$field['desc'].'</span>';
		}
		echo '</td>';
		echo '</tr>';

		echo '</table><div id="connection-output"></div>';
		echo '</div>'; // close #drip div
		echo '<table class="form-table">';
		
	}

	/**
	 * Verify connection credentials
	 *
	 * @access public
	 * @return bool
	 */

	public function test_connection() {

		$api_token = $_POST['drip_token'];
		$account_id = $_POST['drip_account'];

		$connection = $this->crm->connect($api_token, $account_id, true);

		if( is_wp_error( $connection ) ) {

			wp_send_json_error( $connection->get_error_message() );
			
		} else {

			$options = wp_fusion()->settings->get_all();
			$options['drip_token'] = $api_token;
			$options['drip_account'] = $account_id;
			$options['crm'] = $this->slug;
			$options['connection_configured'] = true;
			wp_fusion()->settings->set_all( $options );

			wp_send_json_success();

		}

		die();
		
	}


}