Completed
Pull Request — master (#16)
by Rasmus
04:25
created

Imgix_Options_page   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A instance() 0 7 2
A imgix_options_page() 0 64 1
A imgix_add_options_link() 0 3 1
A imgix_register_settings() 0 3 1
A get_option() 0 3 2
1
<?php
2
3
class Imgix_Options_page {
4
5
	/**
6
	 * The instance of the class.
7
	 *
8
	 * @var Imgix_Options_page
9
	 */
10
	protected static $instance;
11
12
	/**
13
	 * Plugin options
14
	 *
15
	 * @var array
16
	 */
17
	protected $options = [];
18
19
20
	public function __construct() {
21
		$this->options = get_option( 'imgix_settings', [] );
22
		add_action( 'admin_init', [ $this, 'imgix_register_settings' ] );
23
		add_action( 'admin_menu', [ $this, 'imgix_add_options_link' ] );
24
	}
25
26
	/**
27
	 * Plugin loader instance.
28
	 *
29
	 * @return Imgix_Options_page
30
	 */
31
	public static function instance() {
32
		if ( ! isset( self::$instance ) ) {
33
			self::$instance = new self;
34
		}
35
36
		return self::$instance;
37
	}
38
39
	/**
40
	 * Renders options page
41
	 */
42
	public function imgix_options_page() {
43
		?>
44
		<div class="wrap">
45
46
			<h1>
47
				<img src="<?php echo plugins_url( 'assets/images/imgix-logo.png', __DIR__ ); ?>" alt="imgix Logo">
48
			</h1>
49
50
			<p><strong>Need help getting started?</strong> It's easy! Check out our
51
				<a href="https://github.com/imgix-wordpress/imgix-wordpress#getting-started" target="_blank">instructions.</a>
52
			</p>
53
54
			<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
55
				<?php settings_fields( 'imgix_settings_group' ); ?>
56
				<table class="form-table">
57
					<tbody>
58
						<tr>
59
							<th>
60
								<label class="description" for="imgix_settings[cdn_link]"><?php esc_html_e( 'imgix Source', 'imgix' ); ?>
61
							</th>
62
							<td>
63
								<input id="imgix_settings[cdn_link]" type="url" name="imgix_settings[cdn_link]" placeholder="https://yourcompany.imgix.net" value="<?php echo $this->get_option( 'cdn_link' ); ?>" class="regular-text code"/>
64
							</td>
65
						</tr>
66
						<tr>
67
							<th>
68
								<label class="description" for="imgix_settings[auto_format]"><?php esc_html_e( 'Auto Format Images', 'imgix' ); ?></label>
69
							</th>
70
							<td>
71
								<input id="imgix_settings[auto_format]" type="checkbox" name="imgix_settings[auto_format]" value="1" <?php checked( $this->get_option( 'auto_format' ) ) ?> />
72
							</td>
73
						</tr>
74
						<tr>
75
							<th>
76
								<label class="description" for="imgix_settings[auto_enhance]"><?php esc_html_e( 'Auto Enhance Images', 'imgix' ); ?></label>
77
							</th>
78
							<td>
79
								<input id="imgix_settings[auto_enhance]" type="checkbox" name="imgix_settings[auto_enhance]" value="1" <?php checked( $this->get_option( 'auto_enhance' ) ) ?> />
80
							</td>
81
						</tr>
82
						<tr>
83
							<th>
84
								<label class="description" for="imgix_settings[add_dpi2_srcset]"><?php esc_html_e( 'Automatically add retina images using srcset', 'imgix' ); ?></label>
85
							</th>
86
							<td>
87
								<input id="imgix_settings[add_dpi2_srcset]" type="checkbox" name="imgix_settings[add_dpi2_srcset]" value="1" <?php checked( $this->get_option( 'add_dpi2_srcset' ) ) ?> />
88
							</td>
89
						</tr>
90
					</tbody>
91
				</table>
92
93
				<p class="submit">
94
					<input type="submit" class="button-primary" value="<?php esc_html_e( 'Save Options', 'imgix' ); ?>"/>
95
				</p>
96
			</form>
97
98
			<p class="description">
99
				This plugin is powered by
100
				<a href="http://www.imgix.com" target="_blank">imgix</a>. You can find and contribute to the code on
101
				<a href="https://github.com/imgix-wordpress/images-via-imgix" target="_blank">GitHub</a>.
102
			</p>
103
		</div>
104
		<?php
105
	}
106
107
	/**
108
	 *  Adds link to options page in Admin > Settings menu.
109
	 */
110
	public function imgix_add_options_link() {
111
		add_options_page( 'imgix', 'imgix', 'manage_options', 'imgix-options', [ $this, 'imgix_options_page' ] );
112
	}
113
114
	/**
115
	 *  Creates our settings in the options table.
116
	 */
117
	public function imgix_register_settings() {
118
		register_setting( 'imgix_settings_group', 'imgix_settings' );
119
	}
120
121
	/**
122
	 * Get option and handle if option is not set
123
	 *
124
	 * @param string $key
125
	 *
126
	 * @return mixed
127
	 */
128
	protected function get_option( $key ) {
129
		return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
130
	}
131
}
132
133
Imgix_Options_page::instance();
134