-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
131 lines (118 loc) · 3.74 KB
/
Copy pathplugin.php
File metadata and controls
131 lines (118 loc) · 3.74 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
<?php
/**
* Plugin Name: All the Shortcodes
* Description: Lists every shortcode registered on the site under Settings → View all shortcodes.
* Version: 2.0.0
* Author: Rowan
* Author URI: https://www.rowanprice.com
* Text Domain: view-all-shortcodes
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 6.0
* Requires PHP: 7.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class View_All_Shortcodes {
private const PAGE_SLUG = 'view-all-shortcodes';
/**
* The hook suffix returned when the settings page is registered.
*
* @var string
*/
private $page_hook = '';
/**
* Register WordPress hooks.
*/
public function init() {
add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
}
/**
* Add the plugin page to the Settings menu.
*/
public function register_settings_page() {
$this->page_hook = add_options_page(
__( 'View all shortcodes', 'view-all-shortcodes' ),
__( 'View all shortcodes', 'view-all-shortcodes' ),
'manage_options',
self::PAGE_SLUG,
array( $this, 'render_settings_page' )
);
}
/**
* Load assets only on this plugin's settings page.
*
* @param string $hook_suffix The current admin page hook suffix.
*/
public function enqueue_assets( $hook_suffix ) {
if ( $this->page_hook !== $hook_suffix ) {
return;
}
wp_enqueue_style(
'view-all-shortcodes-admin',
plugins_url( 'assets/css/admin.css', __FILE__ ),
array(),
'2.0.0'
);
wp_enqueue_script(
'view-all-shortcodes-admin',
plugins_url( 'assets/js/admin.js', __FILE__ ),
array(),
'2.0.0',
true
);
}
/**
* Render the settings page.
*/
public function render_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
global $shortcode_tags;
$shortcodes = is_array( $shortcode_tags ) ? array_keys( $shortcode_tags ) : array();
natcasesort( $shortcodes );
?>
<div class="wrap">
<h1><?php esc_html_e( 'Listing of shortcodes on this site', 'view-all-shortcodes' ); ?></h1>
<p><?php esc_html_e( 'Select a shortcode to copy it to your clipboard.', 'view-all-shortcodes' ); ?></p>
<p id="view-all-shortcodes-status" class="screen-reader-text" aria-live="polite"></p>
<?php if ( empty( $shortcodes ) ) : ?>
<div class="notice notice-info inline">
<p><?php esc_html_e( 'No shortcodes are currently registered on this site.', 'view-all-shortcodes' ); ?></p>
</div>
<?php else : ?>
<h2><?php esc_html_e( 'Shortcodes', 'view-all-shortcodes' ); ?></h2>
<ul class="view-all-shortcodes-list">
<?php foreach ( $shortcodes as $shortcode ) : ?>
<?php
$shortcode_text = sprintf( '[%s]', $shortcode );
$copy_label = sprintf(
/* translators: %s: shortcode text. */
__( 'Copy %s', 'view-all-shortcodes' ),
$shortcode_text
);
?>
<li>
<button
type="button"
class="button button-secondary view-all-shortcodes-copy"
data-shortcode="<?php echo esc_attr( $shortcode_text ); ?>"
data-success-message="<?php echo esc_attr( sprintf( __( '%s copied to clipboard.', 'view-all-shortcodes' ), $shortcode_text ) ); ?>"
data-error-message="<?php esc_attr_e( 'Your browser could not copy the shortcode. Please copy it manually.', 'view-all-shortcodes' ); ?>"
aria-label="<?php echo esc_attr( $copy_label ); ?>"
>
<code><?php echo esc_html( $shortcode_text ); ?></code>
</button>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php
}
}
$view_all_shortcodes = new View_All_Shortcodes();
$view_all_shortcodes->init();