class-options-page.php 1.78 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1
2
3
4
5
6
7
8
9
<?php
/**
 * Plugin options page
 *
 * @package Camo_Image_Proxy
 */

namespace Camo_Image_Proxy;

Erick Hitter's avatar
Erick Hitter committed
10
11
12
/**
 * Class Options_Page
 */
Erick Hitter's avatar
Erick Hitter committed
13
14
class Options_Page {
	use Singleton;
Erick Hitter's avatar
Erick Hitter committed
15

Erick Hitter's avatar
Erick Hitter committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	/**
	 * Settings screen section
	 *
	 * @var string
	 */
	private $section = 'camp-image-proxy';

	/**
	 * Field labels
	 *
	 * @var array
	 */
	private $labels = [];

	/**
	 * Option name
	 *
	 * @var string
	 */
	private $name;

Erick Hitter's avatar
Erick Hitter committed
37
38
39
40
	/**
	 * Hooks
	 */
	public function setup() {
Erick Hitter's avatar
Erick Hitter committed
41
42
43
44
45
		$this->name = Options::instance()->name;

		$this->labels['host'] = __( 'Host', 'camo-image-proxy' );
		$this->labels['key']  = __( 'Shared Key', 'camo-image-proxy' );

Erick Hitter's avatar
Erick Hitter committed
46
47
48
49
50
51
52
		add_action( 'admin_init', [ $this, 'action_admin_init' ] );
	}

	/**
	 * Add fields to Media settings page
	 */
	public function action_admin_init() {
Erick Hitter's avatar
Erick Hitter committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
		register_setting( 'media', $this->name, [ Options::instance(), 'sanitize_all' ] );
		add_settings_section( $this->section, __( 'Camo Image Proxy', 'camo-image-proxy' ), '__return_false', 'media' );

		foreach ( $this->labels as $key => $label ) {
			$args = [
				'option' => $key,
				'label'  => $label,
			];
			add_settings_field( $key, $label, [ $this, 'screen' ], 'media', $this->section, $args );
		}
	}

	/**
	 * Render options field
	 *
	 * @param array $args Field arguments.
	 */
	public function screen( $args ) {
		$value      = Options::instance()->get( $args['option'] );
		$input_type = 'host' === $args['option'] ? 'url' : 'text';
		$name       = sprintf( '%1$s[%2$s]', $this->name, $args['option'] );
		$html_id    = sprintf( '%1$s-%2$s', str_replace( '_', '-', $this->name ), $args['option'] );
Erick Hitter's avatar
Erick Hitter committed
75

Erick Hitter's avatar
Erick Hitter committed
76
77
78
		?>
		<input
			type="<?php echo esc_attr( $input_type ); ?>"
Erick Hitter's avatar
PHPCS    
Erick Hitter committed
79
			name="<?php echo esc_attr( $name ); ?>"
Erick Hitter's avatar
Erick Hitter committed
80
81
82
83
84
			class="regular-text"
			id="<?php echo esc_attr( $html_id ); ?>"
			value="<?php echo esc_attr( $value ); ?>"
		/>
		<?php
Erick Hitter's avatar
Erick Hitter committed
85
	}
Erick Hitter's avatar
Erick Hitter committed
86
}