-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-thinkery-admin.php
199 lines (174 loc) · 5.51 KB
/
class-thinkery-admin.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Thinkery Admin
*
* This contains the functions for the admin section.
*
* @package Thinkery
*/
/**
* This is the class for the Thinkery Plugin Admin section.
*
* @package Thinkery
* @author Alex Kirk
*/
class Thinkery_Admin {
/**
* Contains a reference to the Thinkery class.
*
* @var Thinkery
*/
private $thinkery;
/**
* Constructor
*
* @param Thinkery $thinkery A reference to the Thinkery object.
*/
public function __construct( Thinkery $thinkery ) {
$this->thinkery = $thinkery;
$this->register_hooks();
}
/**
* Register the WordPress hooks
*/
private function register_hooks() {
add_action( 'admin_menu', array( $this, 'register_admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_bar_menu', array( $this, 'admin_bar_thinkery_menu' ), 39 );
add_action( 'tool_box', array( $this, 'toolbox_bookmarklets' ) );
}
/**
* Registers the admin menus
*/
public function register_admin_menu() {
add_submenu_page( 'edit.php?post_type=' . Thinkery_Things::CPT, __( 'Save URL', 'thinkery' ), __( 'Save URL', 'thinkery' ), 'manage_options', 'thinkery-save-url', array( $this, 'render_save_url' ) );
add_action( 'load-thinkery_thing_page_thinkery-save-url', array( $this, 'process_admin_save_url' ) );
add_submenu_page( 'edit.php?post_type=' . Thinkery_Things::CPT, __( 'Settings' ), __( 'Settings' ), 'manage_options', 'thinkery-settings', array( $this, 'render_admin_settings' ) );
}
/**
* Load the admin scripts
*/
public function admin_enqueue_scripts() {
wp_enqueue_style( 'thinkery-admin', plugins_url( 'css/thinkery-admin.css', __FILE__ ) );
}
/**
* Check access for the Thinkery Admin settings page
*/
public function check_admin_settings() {
if ( ! current_user_can( 'administrator' ) ) {
wp_die( esc_html__( 'Sorry, you are not allowed to change the settings.', 'thinkery' ) );
}
}
/**
* Process the Thinkery Admin settings page
*/
public function process_admin_settings() {
$this->check_admin_settings();
if ( empty( $_POST ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'thinkery-settings' ) ) {
return;
}
if ( isset( $_GET['wp_http_referer'] ) ) {
wp_safe_redirect( $_GET['wp_http_referer'] );
} else {
wp_safe_redirect( add_query_arg( 'updated', '1', remove_query_arg( array( 'wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
}
exit;
}
/**
* Render the Thinkery Admin settings page
*/
public function render_admin_settings() {
$this->check_admin_settings();
$current_user = wp_get_current_user();
?>
<h1><?php esc_html_e( 'Thinkery Settings', 'thinkery' ); ?></h1>
<?php
if ( isset( $_GET['updated'] ) ) {
?>
<div id="message" class="updated notice is-dismissible"><p>
<?php
esc_html_e( 'Your settings were updated.', 'thinkery' );
?>
</p></div>
<?php
}
include apply_filters( 'thinkery_template_path', 'admin/settings.php' );
}
/**
* Save the saved_url
*/
function process_admin_save_url() {
$error = false;
if ( ! empty( $_POST ) && wp_verify_nonce( $_POST['_wpnonce'], 'save-url' ) ) {
return $this->thinkery->things->save_url( $_POST['url'] );
}
if ( isset( $_GET['error'] ) ) {
switch ( $_GET['error'] ) {
case 'invalid-url':
return new WP_Error( $_GET['error'], __( 'You entered an invalid URL.', 'thinkery' ) );
case 'invalid-content':
return new WP_Error( $_GET['error'], __( 'No content was extracted.', 'thinkery' ) );
case 'could-not-download':
return new WP_Error( $_GET['error'], __( 'Could not download the URL.', 'thinkery' ) );
default:
return new WP_Error( $_GET['error'], $_GET['error'] );
}
}
return false;
}
/**
* Save the saved_url through the UI
*/
function render_save_url() {
$error = $this->process_admin_save_url();
?>
<h1><?php esc_html_e( 'Save URL', 'thinkery' ); ?></h1>
<?php
if ( is_wp_error( $error ) ) {
?>
<div id="message" class="updated error is-dismissible"><p><?php echo esc_html( $error->get_error_message() ); ?></p></div>
<?php
}
if ( ! empty( $_GET['url'] ) ) {
$url = $_GET['url'];
}
include apply_filters( 'thinkery_template_path', 'admin/save-url.php' );
}
/**
* Add a Thinkery menu to the admin bar
*
* @param WP_Admin_Bar $wp_menu The admin bar to modify.
*/
public function admin_bar_thinkery_menu( WP_Admin_Bar $wp_menu ) {
$thinkery_url = site_url( '/thinkery/' );
$thinkery_title = 'Thinkery';
if ( current_user_can( 'administrator' ) ) {
$wp_menu->add_node(
array(
'id' => 'thinkery',
'parent' => '',
'title' => '<span class="ab-icon thinkery"></span> ' . esc_html( $thinkery_title ),
'href' => $thinkery_url,
)
);
}
}
/**
* Display the Bookmarklets at the Tools section of wp-admin
*/
public function toolbox_bookmarklets() {
?>
<div class="card">
<h2 class="title"><?php _e( 'Thinkery', 'thinkery' ); ?></h2>
<p><?php _e( "Drag this bookmarklet to your bookmarks bar and click it when you're on a site around the web that you want to save.", 'thinkery' ); ?></p>
<p>
<a href="javascript:void(location.href='<?php echo esc_attr( self_admin_url( 'admin-ajax.php?action=thinkery_save_url&url=' ) ); ?>'+encodeURIComponent(location.href))" style="display: inline-block; padding: .5em; border: 1px solid #999; border-radius: 4px; background-color: #ddd; text-decoration: none; margin-right: 3em"><?php echo esc_html_e( 'Save URL', 'thinkery' ); ?></a>
</p>
</div>
</div>
<?php
}
}