| 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( |
| 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 |
|
| 102 |
/** |
| 103 |
* Render the page. |
| 104 |
* //TODO: Convert these sections into tabs. |
| 105 |
*/ |
| 106 |
public function render_page() { |
| 107 |
?> |
| 108 |
<div class="wrap"> |
| 109 |
<h1><img class="el-logo" src="<?php echo esc_url(EMAIL_LOG_URL . 'assets/img/logo-64x64.png'); ?>" /><?php esc_html_e( 'Email Log', 'email-log' ); ?></h1> |
| 110 |
<div class="email-log-body-wrapper"> |
| 111 |
<form method="post" action="options.php"> |
| 112 |
<?php |
| 113 |
settings_errors(); |
| 114 |
settings_fields( self::PAGE_SLUG ); |
| 115 |
do_settings_sections( self::PAGE_SLUG ); |
| 116 |
|
| 117 |
submit_button( __( 'Save', 'email-log' ) ); |
| 118 |
?> |
| 119 |
</form> |
| 120 |
</div> |
| 121 |
<div class="email-log-sidebar-wrapper"> |
| 122 |
<?php \EmailLog\Core\EmailLog::wp_kses_wf($this->sidebar()); ?> |
| 123 |
</div> |
| 124 |
|
| 125 |
</div> |
| 126 |
<?php |
| 127 |
|
| 128 |
$this->render_page_footer(); |
| 129 |
} |
| 130 |
} |
| 131 |
|