MP_Addon_Hubspot_Settings

Class MP_Addon_Hubspot_Settings


Description Description

See also See also


Top ↑

Source Source

File: calculator-hubspot-add-on/inc/MP_Addon_Hubspot_Settings.php

class MP_Addon_Hubspot_Settings extends MP_Settings {

	/**
	 * Hubspot export settings.
	 * @var array settings
	 * @since  1.0.0
	 * @access private
	 */
	private $export_settings;

	/**
	 * Hubspot settings.
	 *
	 * @var array settings
	 *
	 * @since 1.0.0
	 * @access private
	 */
	private $settings;


	/**
	 * Set Hubspot export settings.
	 * @access public
	 * @since 1.0.0
	 * @return array {
	 *               Hubspot settings.
	 *
	 *  @type array 'allow_hubspot' Enable sending to Hubspot when a new lead added
	 * }
	 */
	public function set_export_fields(){
		$this->export_settings = array(
			array(
				'id'       => 'allow_hubspot',
				'title'    => __( 'Allow Hubspot', 'CalculatorsHubspotAddon' ),
				'callback' => array( $this, 'checkbox' ),
				'args'     => array(
					'option_name' => MortgagePlatform::$prefix . 'export',
					'label_for'   => 'allow_hubspot',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'Allow Hubspot', 'CalculatorsHubspotAddon' ),
				),
			),
		);

		return $this->export_settings;
	}


	/**
	 * Set Hubspot settings.
	 * @access public
	 * @since 1.0.0
	 * @return array {
	 *               Hubspot settings.
	 *
	 *  @type array 'hubspot_api_key' Hubspot API key
	 * }
	 */
	public function set_fields() {

		$this->settings = array(
			array(
				'id'       => 'hubspot_api_key',
				'title'    => __( 'Hubspot API Key', 'CalculatorsHubspotAddon' ),
				'callback' => array( $this, 'input' ),
				'args'     => array(
					'option_name' => MortgagePlatform::$prefix . 'hubspot',
					'label_for'   => 'hubspot_api_key',
					'placeholder' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
					'default'     => '',
					'description' => __( 'Enter API Key', 'CalculatorsHubspotAddon' ),
				),
			),
		);

		return  $this->settings;

	}


    /**
    * Hubspot settings.
    *
    * @since 1.0.0
    * @access public
    *
    * @param array $array export settings
    *
    * @return mixed export data settings
    */
	public function filter_export_data_settings( $array ) {

		$export_button = array();

		$settings = $this->set_export_fields();
        if(is_array($array)){
            foreach ($array as $key => $value){
                if(isset($value['id']) && 'lead_json_manually' === $value['id']){
                    $export_button[] = $value;
                    unset($array[$key]);
                }
            }
        } else {
            $array = array();
        }

        return array_merge( $array, $settings, $export_button );
	}



	/**
	 * Register Zoho Settings.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function register() {

		$fields = $this->set_fields();

		$this->register_setting_fields(
			$fields,
			'hubspot',
			__( 'Hubspot Settings', 'CalculatorsHubspotAddon' ),
			array( $this, 'page_title' )
		);

		add_filter( MortgagePlatform::$prefix . 'menu', array( $this, 'filter_menu' ), 50 );
		add_action( MortgagePlatform::$prefix . 'menu_settings', array( $this, 'filter_settings' ) );

	}


	/**
	 * Filter Settings Menu.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $list menu list
	 *
	 * @return string
	 */
	public function filter_menu( $list ) {

		if ( isset( $_GET['tab'] ) && 'hubspot' === $_GET['tab'] ) {
			$list .= '<li class="active"><a href="?page=' . esc_attr( MortgagePlatform::$domain ) . '&tab=hubspot" >' . esc_html__( 'Hubspot Settings', 'CalculatorsHubspotAddon' ) . '</a></li>';
		} else {
			$list .= '<li ><a href="?page=' . esc_attr( MortgagePlatform::$domain ) . '&tab=hubspot" >' . esc_html__( 'Hubspot Settings', 'CalculatorsHubspotAddon' ) . '</a></li>';
		}

		return $list;

	}


	/**
	 * Add Setting Fields.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @global $pagenow
	 */
	public function filter_settings() {
		global $pagenow;
		if ( isset( $_GET['tab'] ) ) {
			if ( 'admin.php' === $pagenow && MortgagePlatform::$domain === $_GET['page'] ) {

				if ( 'hubspot' === $_GET['tab'] ) {
					?>
					<div class="mp-settings__content tab-pane hubspot">
						<form action="options.php" method="POST">
							<?php
							settings_fields( MortgagePlatform::$prefix . 'settings_hubspot' );
							do_settings_sections( MortgagePlatform::$prefix . 'hubspot' );
							submit_button()
							?>
						</form>
					</div>
					<?php
				}
			}
		}

	}



	/**
	 * Add Hubspot metabox.
	 *
	 * @access public
	 * @since 1.0.0
	 */
	public function hubspot_metabox() {
		add_meta_box(
			'hubspot-metabox',
			__( 'Hubspot', 'CalculatorsHubspotAddon' ),
			array( $this, 'hubspot_meta_callback' ),
			MortgagePlatform::$lead,
			'side',
			'core'
		);
	}


	/**
	 * Callback for Hubspot.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @param object $post post data
	 * @param array $meta  meta data
	 */
	public function hubspot_meta_callback( $post, $meta) {

		wp_nonce_field( MortgagePlatform::$prefix . 'hubspot', MortgagePlatform::$prefix . 'hubspot' );

		echo '<button id="' . esc_attr( $meta['id'] ) . '-send-button" class="button button-large">'.__('Send to Hubspot','CalculatorsHubspotAddon').'</button>';
		echo '<span class="spinner"></span>';

	}



	/**
	 * Add send options.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @param string $options send options
	 *
	 * @return string
	 */
	public function export_to_hubspot($options) {

		$options .= '<option disabled>'.__('Hubspot','CalculatorsHubspotAddon').'</option>';
		$options .= '<option value="hubspot">'.__('Send to Hubspot','CalculatorsHubspotAddon').'</option>';
		$options .= '<option value="hubspot_csv">'.__('Export CSV for Hubspot','CalculatorsHubspotAddon').'</option>';

		return $options;
	}

}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.