SettingsPage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 49
c 1
b 0
f 1
dl 0
loc 116
ccs 0
cts 48
cp 0
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 4 1
A get_setting_sections() 0 11 1
A register_settings() 0 25 3
A register_page() 0 18 2
A render_page() 0 19 1
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
		 *
76
		 * @param \EmailLog\Core\UI\Setting\SettingSection[] List of SettingSections.
77
		 */
78
		return apply_filters( 'el_setting_sections', array() );
79
	}
80
81
	/**
82
	 * Register page.
83
	 */
84
	public function register_page() {
85
86
		$sections = $this->get_setting_sections();
87
88
		if ( empty( $sections ) ) {
89
			return;
90
		}
91
92
		$this->page = add_submenu_page(
0 ignored issues
show
Documentation Bug introduced by
It seems like add_submenu_page(EmailLo...($this, 'render_page')) can also be of type false. However, the property $page is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
93
			LogListPage::PAGE_SLUG,
94
			__( 'Settings', 'email-log' ),
95
			__( 'Settings', 'email-log' ),
96
			'manage_options',
97
			self::PAGE_SLUG,
98
			array( $this, 'render_page' )
99
		);
100
101
		add_action( "load-{$this->page}", array( $this, 'render_help_tab' ) );
102
	}
103
104
	/**
105
	 * Render the page.
106
	 * //TODO: Convert these sections into tabs.
107
	 */
108
	public function render_page() {
109
		?>
110
		<div class="wrap">
111
			<h1><?php _e( 'Email Log Settings', 'email-log' ); ?></h1>
112
113
			<form method="post" action="options.php">
114
				<?php
115
				settings_errors();
116
				settings_fields( self::PAGE_SLUG );
117
				do_settings_sections( self::PAGE_SLUG );
118
119
				submit_button( __( 'Save', 'email-log' ) );
120
				?>
121
			</form>
122
123
		</div>
124
		<?php
125
126
		$this->render_page_footer();
127
	}
128
}
129