MP_Addon_Mailchimp_Settings

Class MP_Addon_Mailchimp_Settings.


Description Description

See also See also


Top ↑

Source Source

File: calculator-mailchimp-add-on/inc/MP_Addon_Mailchimp_Settings.php

class MP_Addon_Mailchimp_Settings extends MP_Settings{


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


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



	/**
	 * Set Mailchimp export settings.
	 * @access public
	 * @since 1.0.0
	 * @return array {
	 *               Mailchimp export settings.
	 *
	 *  @type array 'allow_mailchimp' Allow sending lead data to Mailchimp
	 * }
	 */
	public function set_export_fields() {

		$this->export_settings = array(
			array(
				'id'       => 'allow_mailchimp',
				'title'    => __( 'Allow Mailchimp', 'CalculatorsMailchimpAddon' ),
				'callback' => array( $this, 'checkbox' ),
				'args'     => array(
					'option_name' => MortgagePlatform::$prefix . 'export',
					'label_for'   => 'allow_mailchimp',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'Allow Mailchimp', 'CalculatorsMailchimpAddon' ),
				),
			),
		);

		return $this->export_settings;
	}


	/**
	 * Set Mailchimp  settings.
	 * @access public
	 * @since 1.0.0
	 * @return array {
	 *               Mailchimp settings.
	 *
	 *  @type array 'mailchimp_api' Mailchimp API key
	 *  @type array 'mailchimp_lists' Mailchimp lists
	 * }
	 */
	public function set_fields() {

		$this->export_settings = array(
			array(
				'id'       => 'mailchimp_api',
				'title'    => __( 'Mailchimp API key', 'CalculatorsMailchimpAddon' ),
				'callback' => array( $this, 'input' ),
				'args'     => array(
					'option_name' => MortgagePlatform::$prefix . 'mailchimp',
					'label_for'   => 'mailchimp_api',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'Mailchimp API key', 'CalculatorsMailchimpAddon' ),
				),
			),
			array(
				'id'       => 'mailchimp_lists',
				'title'    => __( 'Mailchimp audience', 'CalculatorsMailchimpAddon' ),
				'callback' => array( $this, 'select' ),
				'args'     => array(
					'option_name' => MortgagePlatform::$prefix . 'mailchimp',
					'label_for'   => 'mailchimp_lists',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'Choose audience', 'CalculatorsMailchimpAddon' ),
					'options'     => self::lists()
				),
			),
		);

		return $this->export_settings;
	}


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

		$fields = $this->set_fields();

		$this->register_setting_fields(
			$fields,
			'mailchimp',
			__( 'Mailchimp Settings', 'CalculatorsMailchimpAddon' ),
			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'] ) && 'mailchimp' === $_GET['tab'] ) {
			$list .= '<li class="active"><a href="?page=' . esc_attr( MortgagePlatform::$domain ) . '&tab=mailchimp" >' . esc_html__( 'Mailchimp Settings', 'CalculatorsMailchimpAddon' ) . '</a></li>';
		} else {
			$list .= '<li ><a href="?page=' . esc_attr( MortgagePlatform::$domain ) . '&tab=mailchimp" >' . esc_html__( 'Mailchimp Settings', 'CalculatorsMailchimpAddon' ) . '</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 ( 'mailchimp' === $_GET['tab'] ) {
					?>
					<div class="mp-settings__content tab-pane mailchimp">
						<form action="options.php" method="POST">
							<?php
							settings_fields( MortgagePlatform::$prefix . 'settings_mailchimp' );
							do_settings_sections( MortgagePlatform::$prefix . 'mailchimp' );
							submit_button()
							?>
						</form>
					</div>
					<?php
				}
			}
		}

	}

	/**
	 * Mailchimp 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 );
	}


	/**
	 * Add Mailchimp metabox.
	 *
	 * @access public
	 * @since 1.0.0
	 */
	public function mailchimp_metabox() {
		add_meta_box(
			'mailchimp-metabox',
			__( 'Mailchimp', 'CalculatorsMailchimpAddon' ),
			array( $this, 'mailchimp_meta_callback' ),
			MortgagePlatform::$lead,
			'side',
			'core'
		);
	}


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

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

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

	}



	/**
	 * Mailchimp lists.
	 * @access public
	 * @static
	 * @since 1.0.0
	 * @return array mailchimp lists {
	 *  @type string 'id'=>'name'
	 * }
	 */
	public static function lists(){

		$mailchimp = new MP_Addon_Mailchimp();
		$lists = $mailchimp->get_lists();

		$array = array('default'=>'');
		foreach ($lists as $list){
			$array[$list->id] = $list->name;
		}

		return $array;
	}



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

		$options .= '<option disabled>'.__('Mailchimp','CalculatorsMailchimpAddon').'</option>';
		$options .= '<option value="mailchimp">'.__('Send to Mailchimp','CalculatorsMailchimpAddon').'</option>';
		$options .= '<option value="mailchimp_csv">'.__('Export CSV for Mailchimp','CalculatorsMailchimpAddon').'</option>';

		return $options;
	}


}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.