Completed
Push — master ( 419d71...4ab42b )
by Sudar
11:47
created

SettingsPage::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php namespace EmailLog\Core\UI\Page;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * Settings Page.
7
 * This page is displayed only if any add-on has a setting enabled.
8
 *
9
 * @since 2.0.0
10
 */
11
class SettingsPage extends BasePage {
12
13
	/**
14
	 * Page slug.
15
	 */
16
	const PAGE_SLUG = 'email-log-settings';
17
18
	/**
19
	 * Specify additional hooks.
20
	 *
21
	 * @inheritdoc
22
	 */
23
	public function load() {
24
		parent::load();
25
26
		add_action( 'admin_init', array( $this, 'register_settings' ) );
27
	}
28
29
	/**
30
	 * Register settings and add setting sections and fields.
31
	 */
32
	public function register_settings() {
33
		$sections = $this->get_setting_sections();
34
35
		foreach ( $sections as $section ) {
36
			register_setting(
37
				self::PAGE_SLUG,
38
				$section->option_name,
39
				array( 'sanitize_callback' => $section->sanitize_callback )
40
			);
41
42
			add_settings_section(
43
				$section->id,
44
				$section->title,
45
				$section->callback,
46
				self::PAGE_SLUG
47
			);
48
49
			foreach ( $section->fields as $field ) {
50
				add_settings_field(
51
					$section->id . '[' . $field->id . ']',
52
					$field->title,
53
					$field->callback,
54
					self::PAGE_SLUG,
55
					$section->id,
56
					$field->args
57
				);
58
			}
59
		}
60
	}
61
62
	/**
63
	 * Get a list of setting sections defined.
64
	 * An add-on can define a setting section.
65
	 *
66
	 * @return \EmailLog\Core\UI\Setting\SettingSection[] List of defined setting sections.
67
	 */
68
	protected function get_setting_sections() {
69
		/**
70
		 * Specify the list of setting sections in the settings page.
71
		 * An add-on can add its own setting section by adding an instance of
72
		 * SectionSection to the array.
73
		 *
74
		 * @since 2.0.0
75
		 * @param \EmailLog\Core\UI\Setting\SettingSection[] List of SettingSections.
76
		 */
77
		return apply_filters( 'el_setting_sections', array() );
78
	}
79
80
	/**
81
	 * Register page.
82
	 */
83
	public function register_page() {
84
85
		$sections = $this->get_setting_sections();
86
87
		if ( empty( $sections ) ) {
88
			return;
89
		}
90
91
		$this->page = add_submenu_page(
92
			LogListPage::PAGE_SLUG,
93
			__( 'Settings', 'email-log' ),
94
			__( 'Settings', 'email-log' ),
95
			'manage_options',
96
			self::PAGE_SLUG,
97
			array( $this, 'render_page' )
98
		);
99
100
		add_action( "load-{$this->page}", array( $this, 'render_help_tab' ) );
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
101
	}
102
103
	/**
104
	 * Render the page.
105
	 * //TODO: Convert these sections into tabs.
106
	 */
107
	public function render_page() {
108
		?>
109
		<div class="wrap">
110
			<h1><?php _e( 'Email Log Settings', 'email-log' ); ?></h1>
111
112
			<form method="post" action="options.php">
113
				<?php
114
				settings_errors();
115
				settings_fields( self::PAGE_SLUG );
116
				do_settings_sections( self::PAGE_SLUG );
117
118
				submit_button( __( 'Save', 'email-log' ) );
119
				?>
120
			</form>
121
122
		</div>
123
		<?php
124
	}
125
}
126