YelpReviews_Settings

Class YelpReviews_Settings.


Source Source

File: mortgage-yelp-review/inc/YelpReviews_Settings.php

class YelpReviews_Settings {


	/**
	 * Yelp reviews settings.
	 *
	 * @access protected
	 * @since 1.0.0
	 *
	 * @var array
	 */
	protected $settings;

	/**
	 * Set settings fields of the Yelp reviews plugin.
	 * @access public
	 * @since 1.0.0
	 * @return array {
	 *
	 *               Setting fields.
	 *
	 *  @type array 'business_id' Business ID
	 *  @type array 'api_key' API Key
	 *  @type array 'count' Count of reviews
	 *  @type array 'style' Widget style
	 * }
	 */
	protected function set_fields(){

		$this->settings = array(
			array(
				'id'       => 'business_id',
				'title'    => __( 'Business ID', 'YelpReviewsWidget' ),
				'callback' => array( $this, 'input' ),
				'args'     => array(
					'option_name' => 'mortgage_yelp_widget',
					'label_for'   => 'business_id',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'The ID appears at the end of the URL. For example – www.yelp.com/biz/your-yelp-business-id.', 'YelpReviewsWidget' ),
				),
			),
			array(
				'id'       => 'api_key',
				'title'    => __( 'API Key', 'YelpReviewsWidget' ),
				'callback' => array( $this, 'input' ),
				'args'     => array(
					'option_name' => 'mortgage_yelp_widget',
					'label_for'   => 'api_key',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'API Key. https://www.yelp.com/developers/v3/manage_app', 'YelpReviewsWidget' ),
				),
			),
			array(
				'id'       => 'count',
				'title'    => __( 'Count', 'YelpReviewsWidget' ),
				'callback' => array( $this, 'number' ),
				'args'     => array(
					'option_name' => 'mortgage_yelp_widget',
					'label_for'   => 'count',
					'placeholder' => '',
					'default'     => '3',
					'description' => __( 'Count reviews', 'YelpReviewsWidget' ),
				),
			),
			array(
				'id'       => 'style',
				'title'    => __( 'Widget style', 'YelpReviewsWidget' ),
				'callback' => array( $this, 'select' ),
				'args'     => array(
					'option_name' => 'mortgage_yelp_widget',
					'label_for'   => 'style',
					'placeholder' => '',
					'default'     => '',
					'description' => __( 'Select widget style.', 'YelpReviewsWidget' ),
					'options'     => array(
						'list'   => __( 'List', 'YelpReviewsWidget' ),
						'grid' => __( 'Grid', 'YelpReviewsWidget' ),
						'slider' => __( 'Slider', 'YelpReviewsWidget' ),
					),
				),
			),
		);

		return $this->settings;

	}

	/**
	 * Registers a setting and its data.
	 *
	 * @access public
	 * @since 1.0.0
	 */
	public function register() {

		$general_fields = $this->set_fields();

		$this->register_setting_fields(
			$general_fields,
			'mortgage_yelp_widget',
			__( 'Settings', 'YelpReviewsWidget' ),
			array( $this, 'page_title' )
		);
	}

	/**
	 * Registers a setting and its data.
	 *
	 * @access protected
	 * @since 1.0.0
	 *
	 * @param array $fields setting fields
	 * @param string $name setting name
	 * @param string $title_link setting title
	 * @param callable $callback callback function
	 */
	protected function register_setting_fields( array $fields, string $name, string $title_link, callable $callback ) {
		register_setting(
			'mortgage_yelp_widget_settings',
			'mortgage_yelp_widget',
			''
		);
		add_settings_section(
			$name,
			$title_link,
			$callback,
			$name
		);

		$this->add_settings_field( $fields, $name );
	}


	/**
	 * Add setting fields.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @param array $setting_fields settings fields
	 * @param string $name settings name
	 */
	public function add_settings_field( array $setting_fields, string $name ) {

		foreach ( $setting_fields as $field ) {

			add_settings_field(
				$field['id'],
				$field['title'],
				$field['callback'],
				$name,
				$name,
				$field['args']
			);
		}
	}

	/**
	 * Callback function of page title.
	 *
	 * @access public
	 * @since 1.0.0
	 */
	public function page_title() {
        echo esc_html__('After set up, you can use the shortcode ', 'YelpReviewsWidget') ?><code>[le-yelp-reviews]</code><?php
        echo esc_html__(' or the widget to insert it into pages or sidebars.', 'YelpReviewsWidget');
	}




	/**
	 * Callback function to display setting input field.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @param array $args array of setting arguments
	 */
	public function input( array $args ) {

		$name        = $args['label_for'];
		$option_name = $args['option_name'];
		$input       = get_option( $option_name );
		$placeholder = $args['placeholder'];
		$default     = $args['default'];
		$description = $args['description'];
		$value       = ! empty( $input[ $name ] ) ? $input[ $name ] : $default;

		echo '<input style="width: 100%; padding: 5px 10px" type="text" class="regular-text" id="' . esc_attr( $name ) . '" name="' . esc_attr( $option_name ) . '[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '" >';
		echo '<p>' . esc_html( $description ) . '</p>';
	}

	/**
	 * Callback function to display setting number field.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @param array $args array of setting arguments
	 */
	public function number( array $args ) {

		$name        = $args['label_for'];
		$option_name = $args['option_name'];
		$input       = get_option( $option_name );
		$placeholder = $args['placeholder'];
		$default     = $args['default'];
		$description = $args['description'];
		$value       = ! empty( $input[ $name ] ) ? $input[ $name ] : $default;
		$api_key = isset(get_option('mortgage_yelp_widget')['api_key']) && !empty(get_option('mortgage_yelp_widget')['api_key']) ? true : false;
		$value = $api_key ? 3 : $value;
		$min = $api_key ? 3 : 0;
		$max = $api_key ? 3 : 10;

		echo '<input style="width: 100%; padding: 5px 10px" type="number" class="regular-text" min="'.$min.'" max="'.$max.'" step="1" id="' . esc_attr($name) . '" name="' . esc_attr($option_name) . '[' . esc_attr($name) . ']" value="' . esc_attr($value) . '" placeholder="' . esc_attr($placeholder) . '" >';
		echo '<p>' . esc_html( $description ) . '</p>';
	}


	/**
	 * Callback select function.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param array $args array of arguments
	 */
	public function select( array $args ) {
		$name        = $args['label_for'];
		$option_name = $args['option_name'];
		$input       = get_option( $option_name );
		$default     = $args['default'];
		$value       = isset( $input[ $name ] ) ? $input[ $name ] : $default;
		$description = $args['description'];

		echo '<select style="width: 50%;" name="' . esc_attr( $option_name ) . '[' . esc_attr( $name ) . ']" id="' . esc_attr( $name ) . '">';

		foreach ( $args['options'] as $key => $option ) {
			$selected = '';
			if ( '' !== $value ) {
				if ( $value === $key ) {
					$selected = ' selected="selected"';
				}
			}
			echo '<option' . esc_attr( $selected ) . ' value="' . esc_attr( $key ) . '">' . esc_attr( $option ) . '</option>';
		}
		echo '</select>';
		echo '<p>' . esc_html( $description ) . '</p>';
	}



}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.