class-settings.php
3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Block Lab Settings.
*
* @package Block_Lab
* @copyright Copyright(c) 2020, Block Lab
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Block_Lab\Admin;
use Block_Lab\Component_Abstract;
/**
* Class Settings
*/
class Settings extends Component_Abstract {
/**
* Page slug.
*
* @var string
*/
public $slug = 'block-lab-settings';
/**
* Register any hooks that this component needs.
*/
public function register_hooks() {
add_action( 'admin_menu', [ $this, 'add_submenu_pages' ] );
add_action( 'admin_init', [ $this, 'register_settings' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'admin_notices', [ $this, 'show_notices' ] );
}
/**
* Enqueue scripts and styles used by the Settings screen.
*
* @return void
*/
public function enqueue_scripts() {
$page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
// Enqueue scripts and styles on the edit screen of the Block post type.
if ( $this->slug === $page ) {
wp_enqueue_style(
$this->slug,
$this->plugin->get_url( 'css/admin.settings.css' ),
[],
$this->plugin->get_version()
);
}
}
/**
* Add submenu pages to the Block Lab menu.
*/
public function add_submenu_pages() {
add_submenu_page(
'edit.php?post_type=' . block_lab()->get_post_type_slug(),
__( 'Block Lab Settings', 'block-lab' ),
__( 'Settings', 'block-lab' ),
'manage_options',
$this->slug,
[ $this, 'render_page' ]
);
}
/**
* Register Block Lab settings.
*/
public function register_settings() {
register_setting( 'block-lab-license-key', 'block_lab_license_key' );
}
/**
* Render the Settings page.
*/
public function render_page() {
?>
<div class="wrap block-lab-settings">
<?php
$this->render_page_header();
include block_lab()->get_path() . 'php/views/license.php';
?>
</div>
<?php
}
/**
* Render the Settings page header.
*/
public function render_page_header() {
?>
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
<h2 class="nav-tab-wrapper">
<a href="<?php echo esc_url( add_query_arg( 'tab', 'license' ) ); ?>" title="<?php esc_attr_e( 'License', 'block-lab' ); ?>" class="nav-tab nav-tab-active dashicons-before dashicons-nametag">
<?php esc_html_e( 'License', 'block-lab' ); ?>
</a>
<a href="https://getblocklab.com/docs/" target="_blank" class="nav-tab dashicons-before dashicons-info">
<?php esc_html_e( 'Documentation', 'block-lab' ); ?>
</a>
<a href="https://wordpress.org/support/plugin/block-lab/" target="_blank" class="nav-tab dashicons-before dashicons-sos">
<?php esc_html_e( 'Help', 'block-lab' ); ?>
</a>
</h2>
<?php
}
/**
* Prepare notices to be displayed after saving the settings.
*
* @param string $notice The notice text to display.
*/
public function prepare_notice( $notice ) {
$notices = get_option( 'block_lab_notices', [] );
$notices[] = $notice;
update_option( 'block_lab_notices', $notices );
}
/**
* Show any admin notices after saving the settings.
*/
public function show_notices() {
$notices = get_option( 'block_lab_notices', [] );
if ( empty( $notices ) || ! is_array( $notices ) ) {
return;
}
foreach ( $notices as $notice ) {
echo wp_kses_post( $notice );
}
delete_option( 'block_lab_notices' );
}
}