-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadmin.php
369 lines (342 loc) · 12.8 KB
/
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* Handles hooks and enqueueing for admin page and Gutenberg plugin scripts
*
* @package Civil_Publisher
*/
namespace Civil_Publisher;
/**
* Enqueue Gutenberg editor plugin script.
*/
function enqueue_post_panel() {
if ( ! current_user_can_sign_posts() || ! in_array( get_post_type(), get_civil_post_types(), true ) ) {
return;
}
wp_enqueue_script(
'civil-publisher-post-panel',
plugins_url( 'build/post-panel.build.js', __FILE__ ),
array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-edit-post', 'wp-data' ),
ASSETS_VERSION,
true
);
common_scripts( 'civil-publisher-post-panel' );
}
if ( is_manager_enabled() ) {
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_post_panel' );
}
/**
* Add Civil admin menu and sub-menu items.
*/
function add_menus() {
add_menu_page(
__( 'Civil', 'civil' ),
__( 'Civil', 'civil' ),
'edit_posts',
TOP_LEVEL_MENU,
null,
'data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjIwIiB2aWV3Qm94PSIwIDAgMjAgMjAiIHdpZHRoPSIyMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGRlZnM+PHBhdGggaWQ9ImEiIGQ9Im02LjEwMjIxNjMyIDIuMjI4NTcxNDNoNy43OTU1NjczOGMxLjQyNjQzIDAgMS45NDM2ODc5LjE0ODUyMDk4IDIuNDY1MTY5NC40Mjc0MTIzMi41MjE0ODE1LjI3ODg5MTM1LjkzMDc0MzIuNjg4MTUzMDcgMS4yMDk2MzQ2IDEuMjA5NjM0NTUuMjc4ODkxMy41MjE0ODE0OC40Mjc0MTIzIDEuMDM4NzM5NDYuNDI3NDEyMyAyLjQ2NTE2OTQ1djcuNzk1NTY3MzVjMCAxLjQyNjQzLS4xNDg1MjEgMS45NDM2ODgtLjQyNzQxMjMgMi40NjUxNjk1LS4yNzg4OTE0LjUyMTQ4MTQtLjY4ODE1MzEuOTMwNzQzMi0xLjIwOTYzNDYgMS4yMDk2MzQ1LS41MjE0ODE1LjI3ODg5MTQtMS4wMzg3Mzk0LjQyNzQxMjMtMi40NjUxNjk0LjQyNzQxMjNoLTcuNzk1NTY3MzhjLTEuNDI2NDI5OTggMC0xLjk0MzY4Nzk2LS4xNDg1MjA5LTIuNDY1MTY5NDUtLjQyNzQxMjMtLjUyMTQ4MTQ4LS4yNzg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yLjA5MTkwMzYtMS44NDAwNTc5NS0zLjc0MTU3MDItMS44NDAwNTc5NS0yLjY4MjI5NDkzIDAtNC43NTkwNTYwMyAyLjA5MDQzNTg2LTQuNzU5MDU2MDMgNC44NDU3MTQyNnoiLz48bWFzayBpZD0iYiIgZmlsbD0iI2ZmZiI+PHVzZSBmaWxsPSJub25lIiB4bGluazpocmVmPSIjYSIvPjwvbWFzaz48L2RlZnM+PGcgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj48dXNlIGZpbGw9IiNkOGQ4ZDgiIHhsaW5rOmhyZWY9IiNhIi8+PGcgbWFzaz0idXJsKCNiKSI+PHBhdGggZD0ibTAgMGgyMXYyMWgtMjF6IiBmaWxsPSIjNDQ0IiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtLjUzMiAtLjM2NykiLz48L2c+PC9nPjwvc3ZnPg=='
);
add_submenu_page(
TOP_LEVEL_MENU,
__( 'Story Boosts', 'civil' ),
__( 'Story Boosts', 'civil' ),
'edit_posts',
STORY_BOOSTS_SETTINGS,
__NAMESPACE__ . '\story_boosts_settings_content'
);
if ( apply_filters( 'civil_enable_credibility_indicators', true ) ) {
add_submenu_page(
TOP_LEVEL_MENU,
__( 'Credibility Indicators', 'civil' ),
__( 'Credibility Indicators', 'civil' ),
'edit_posts',
CREDIBILITY_INDICATORS,
__NAMESPACE__ . '\credibililty_indicators_content'
);
}
if ( is_manager_enabled() ) {
add_submenu_page(
TOP_LEVEL_MENU,
__( 'Newsroom Manager', 'civil' ),
__( 'Newsroom Manager', 'civil' ),
'manage_options',
MANAGEMENT_PAGE,
__NAMESPACE__ . '\newsroom_manager_content'
);
}
// Remove unneeded "Civil" submenu.
remove_submenu_page( TOP_LEVEL_MENU, TOP_LEVEL_MENU );
}
add_action( 'admin_menu', __NAMESPACE__ . '\add_menus' );
/**
* Add FAQ link to plugin menu.
*/
function add_faq_link() {
// phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited -- can't find any other way to get external link submenu except redirect headers or crazy url filter shit
global $submenu;
$submenu[ TOP_LEVEL_MENU ][] = array( 'Manager Help 🡭', 'edit_posts', FAQ_HOME );
// phpcs:enable
}
if ( is_manager_enabled() ) {
add_action( 'admin_menu', __NAMESPACE__ . '\add_faq_link' );
}
/**
* Civil Newsroom Manager page content.
*/
function newsroom_manager_content() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'civil' ) );
}
require_once dirname( __FILE__ ) . '/newsroom-manager.php';
}
/**
* Newsroom content viewer.
*/
function content_viewer_content() {
if ( ! current_user_can( 'edit_posts' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'civil' ) );
}
require_once dirname( __FILE__ ) . '/content-viewer.php';
}
/**
* FAQ and Help page content.
*/
function help_menu_content() {
if ( ! current_user_can( 'edit_posts' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'civil' ) );
}
require_once dirname( __FILE__ ) . '/faq-help.php';
}
/**
* Story Boosts Settings content.
*/
function story_boosts_settings_content() {
require_once dirname( __FILE__ ) . '/story-boosts-settings.php';
}
/**
* Credibiity Indicators content.
*/
function credibililty_indicators_content() {
require_once dirname( __FILE__ ) . '/credibililty-indicators.php';
}
/**
* Enqueue Civil Newsroom Manager script.
*/
function contract_management_script() {
wp_enqueue_script(
'civil-publisher-newsroom-management',
plugins_url( 'build/newsroom-management.build.js', __FILE__ ),
// Need these deps in order to expose React and wp.apiRequest.
array( 'wp-edit-post', 'wp-data' ),
ASSETS_VERSION,
true
);
common_scripts( 'civil-publisher-newsroom-management' );
}
add_action( 'admin_print_scripts-civil_page_' . MANAGEMENT_PAGE, __NAMESPACE__ . '\contract_management_script' );
/**
* Enqueue Content Viewer.
*/
function content_viewer_script() {
wp_enqueue_script(
'civil-publisher-content-viewer',
plugins_url( 'build/content-viewer.build.js', __FILE__ ),
// Need these deps in order to expose React and wp.apiRequest.
array( 'wp-edit-post', 'wp-data' ),
ASSETS_VERSION,
true
);
common_scripts( 'civil-publisher-content-viewer' );
}
add_action( 'admin_print_scripts-civil_page_' . CONTENT_VIEWER, __NAMESPACE__ . '\content_viewer_script' );
/**
* Alert user that Gutenberg plugin is needed if they don't have it activated. Only show on Manager page though so we don't drive them crazy if they mostly use the classic editor.
*/
function gutenberg_nag() {
$page_id = get_current_screen()->id ?? '';
if ( 'civil_page_' . MANAGEMENT_PAGE !== $page_id
|| is_gutenberg_enabled()
) {
return;
}
if ( is_gutenberg_disabled_by_classic_editor() ) {
$title = __( 'Gutenberg plugin disabled', 'civil' );
$body = sprintf(
wp_kses(
/* translators: 1: Gutenberg plugin demo URL, 2: Classic Editor settings URL */
__( 'The Civil Publisher requires WordPress\'s official <a target="_blank" href="%1$s">Gutenberg editor</a> to be active. You currently have Gutenberg disabled via the Classic Editor plugin. Please <a href="%2$s">visit Classic Editor settings</a> and set the default editor to the "Block Editor" in order to continue.', 'civil' ),
array(
'a' => array(
'href' => array(),
'target' => array(),
),
)
),
esc_url( 'https://wordpress.org/gutenberg/' ),
esc_url( admin_url( '/options-writing.php#classic-editor-options' ) )
);
} else {
$title = __( 'Gutenberg plugin missing', 'civil' );
$body = sprintf(
wp_kses(
/* translators: 1: Gutenberg plugin demo URL, 2: Install Gutenberg plugin URL */
__( 'The Civil Publisher requires WordPress\'s official <a target="_blank" href="%1$s">Gutenberg editor</a> to be installed and activated. Please either <a target="_blank" href="%2$s">install the Gutenberg plugin</a> or upgrade WordPress to the latest version, which includes Gutenberg by default.', 'civil' ),
array(
'a' => array(
'href' => array(),
'target' => array(),
),
)
),
esc_url( 'https://wordpress.org/gutenberg/' ),
esc_url( admin_url( '/plugin-install.php?tab=plugin-information&plugin=gutenberg' ) )
);
}
civil_notice_open();
?>
<h3><?php echo esc_html( $title ); ?></h3>
<p><?php echo esc_html( $body ); ?></p>
<?php
civil_notice_close();
}
if ( is_manager_enabled() ) {
add_action( 'admin_notices', __NAMESPACE__ . '\gutenberg_nag' );
}
/**
* If necessary, alert user that they need to set up newsroom to use plugin.
*/
function newsroom_setup_nag() {
// Don't show on newsroom manager page.
$page_id = get_current_screen()->id ?? '';
if ( 'civil_page_' . MANAGEMENT_PAGE === $page_id ) {
return;
}
if ( current_user_can( 'manage_options' ) && empty( get_option( NEWSROOM_ADDRESS_OPTION_KEY ) ) ) {
$management_page_url = menu_page_url( MANAGEMENT_PAGE, false );
civil_notice_open();
?>
<h3><?php esc_html_e( 'Civil Publisher Installed!', 'civil' ); ?></h3>
<p>
<?php
echo sprintf(
wp_kses(
/* translators: 1: Management page URL */
__( 'Please take a few minutes to <a href="%1$s">set up your Civil newsroom</a> to start signing and publishing your posts.', 'civil' ),
array( 'a' => array( 'href' => array() ) )
),
esc_url( $management_page_url )
);
?>
</p>
<p class="civil-buttons-wrap">
<a href="<?php echo esc_url( $management_page_url ); ?>" class="button button-primary"><?php esc_html_e( 'Set Up Newsroom', 'civil' ); ?></a>
<a href="<?php echo esc_url( FAQ_HOME ); ?>" class="button"><?php esc_html_e( 'FAQ and Help', 'civil' ); ?></a>
</p>
<?php
civil_notice_close();
}
}
if ( is_manager_enabled() ) {
add_action( 'admin_notices', __NAMESPACE__ . '\newsroom_setup_nag' );
}
/**
* If necessary, alert user that they need to fill in their ETH wallet address.
*/
function wallet_address_nag() {
// Don't show on newsroom manager page, or if newsroom setup nag is showing.
$page_id = get_current_screen()->id ?? '';
if ( 'civil_page_' . MANAGEMENT_PAGE === $page_id
|| ( current_user_can( 'manage_options' ) && empty( get_option( NEWSROOM_ADDRESS_OPTION_KEY ) ) )
) {
return;
}
if ( current_user_can_sign_posts() && empty( get_user_meta( get_current_user_id(), USER_ETH_ADDRESS_META_KEY ) ) ) {
$edit_profile_url = get_edit_user_link() . '#civil_publisher_eth_wallet_address';
civil_notice_open();
?>
<h3><?php esc_html_e( 'Civil Publisher', 'civil' ); ?></h3>
<p>
<?php
echo sprintf(
wp_kses(
/* translators: 1: Edit profile URL */
__( 'You need to <a href="%1$s">add your wallet address to your profile</a> before you can use your newsroom contract features.', 'civil' ),
array( 'a' => array( 'href' => array() ) )
),
esc_url( $edit_profile_url )
);
?>
</p>
<p class="civil-buttons-wrap">
<a href="<?php echo esc_url( $edit_profile_url ); ?>" class="button button-primary"><?php esc_html_e( 'Add Wallet Address', 'civil' ); ?></a>
<a href="<?php echo esc_url( FAQ_HOME ); ?>" class="button"><?php esc_html_e( 'FAQ and Help', 'civil' ); ?></a>
</p>
<?php
civil_notice_close();
}
}
if ( is_manager_enabled() ) {
add_action( 'admin_notices', __NAMESPACE__ . '\wallet_address_nag' );
}
/**
* Output opening HTML for Civil admin notice.
*/
function civil_notice_open() {
?>
<style>
.civil-notice {
display: flex;
align-items: center;
height: 125px;
}
.civil-logo-wrap {
display: table;
height: calc(100% - 32px);
margin-right: 15px;
padding: 15px;
background: black;
}
.civil-logo-wrap-inner {
display: table-cell;
vertical-align: middle;
}
.civil-notice h3 {
margin: 8px 0;
}
p.civil-buttons-wrap {
margin-top: 10px;
}
p.civil-buttons-wrap .button:last-child {
margin-left: 8px;
}
</style>
<div class="notice notice-error civil-notice is-dismissible">
<div class="civil-logo-wrap">
<div class="civil-logo-wrap-inner">
<img src="<?php echo esc_url( plugins_url( 'images/civil-logo.svg', __FILE__ ) ); ?>" />
</div>
</div>
<div class="civil-notice-body">
<?php
}
/**
* Output closing HTML for Civil admin notice.
*/
function civil_notice_close() {
?>
</div>
</div>
<?php
}
/**
* Dumb script to ensure external help menu link opens in a new tab.
*/
function help_menu_new_tab() {
?>
<script>
var submenu = document.querySelector(".toplevel_page_<?php echo esc_attr( TOP_LEVEL_MENU ); ?> .wp-submenu a[href*='help.civil.co']");
submenu && submenu.setAttribute("target", "_blank");
</script>
<?php
}
add_action( 'admin_print_footer_scripts', __NAMESPACE__ . '\help_menu_new_tab' );