-
Notifications
You must be signed in to change notification settings - Fork 0
/
hide-old-shortcodes.php
205 lines (190 loc) · 8.25 KB
/
hide-old-shortcodes.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
<?php
/*
Plugin Name: Hide Old Shortcodes
Description: This plugin hides old/non registered shortcodes on the front end from users. It also helps you locate them so you can remove them.
Version: 0.3.3
Author: Mike Hansen
Author URI: http://mikehansen.me?utm_campaign=plugin&utm_source=hos_wp_plugin
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
GitHub Plugin URI: https://github.com/MikeHansenMe/hide-old-shortcodes
GitHub Branch: stable
*/
function hos_return_all_shortcodes_regex() {
return
'\\[' // Opening bracket
. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
. "([\w-]+)" // 2: Shortcode name
. '(?![\\w-])' // Not followed by word character or hyphen
. '(' // 3: Unroll the loop: Inside the opening shortcode tag
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. '(?:'
. '\\/(?!\\])' // A forward slash not followed by a closing bracket
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. ')*?'
. ')'
. '(?:'
. '(\\/)' // 4: Self closing tag ...
. '\\]' // ... and closing bracket
. '|'
. '\\]' // Closing bracket
. '(?:'
. '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
. '[^\\[]*+' // Not an opening bracket
. '(?:'
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
. '[^\\[]*+' // Not an opening bracket
. ')*+'
. ')'
. '\\[\\/\\2\\]' // Closing shortcode tag
. ')?'
. ')'
. '(\\]?)'; // 6: Optional second closing bracket for escaping shortcodes: [[tag]]
}
function hos_replace_old_shortcodes( $content ) {
global $shortcode_tags;
global $post;
$post_type = get_post_type( $post ); //used later to help locate the usage
$pattern = hos_return_all_shortcodes_regex(); //returns our modified regex to return ALL shortcodes
preg_match_all( '/' . $pattern . '/s', $content, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
$raw_match = $match; //store the raw match to show usage
$match = $match[2]; //just the name of the shortcode
$ignored_shortcodes = get_option( 'hos_ignored_shortcodes', array() );
if( array_key_exists( 'hos_' . md5( $raw_match[0] . "-" . $post->ID ), $ignored_shortcodes ) AND $ignored_shortcodes[ 'hos_' . md5( $raw_match[0] . "-" . $post->ID ) ] == $raw_match[0] ) {
continue; //do not add the shortcode back to the log
}
if( ! array_key_exists( $match, $shortcode_tags ) ) { //check that it is not a active shortcode
$hidden_shortcodes = get_option( 'hos_hidden_shortcodes', array() );
if( ! array_key_exists( 'hos_' . md5( $raw_match[0] . "-" . $post->ID ), $hidden_shortcodes ) AND strpos( $raw_match[0],'[[') === false ) { //check if we already logged this, if so save time and skip
$hidden_shortcodes[ 'hos_' . md5( $raw_match[0] . "-" . $post->ID ) ] = array(
'shortcode' => $match,
'raw' => $raw_match[0],
'post_type' => $post_type,
'post_id' => $post->ID
);
update_option( 'hos_hidden_shortcodes', $hidden_shortcodes ); //save usage for log
}
$shortcode_tags[ $match ] = '__return_false'; //make the shortcode do nothing(hide)
}
}
return $content;
}
add_filter( 'the_content', 'hos_replace_old_shortcodes' );
function hos_add_page() {
add_management_page( 'Hide Old Shortcodes', 'Hide Old Shortcodes', 'edit_posts', 'hide-old-shortcodes', 'hos_page_content' );
}
add_action( 'admin_menu', 'hos_add_page' );
function hos_page_content() {
$message = array();
if( isset( $_GET['clear'] ) AND $_GET['clear'] == true ) {
update_option( 'hos_hidden_shortcodes', array() );
}
$hidden_shortcodes = get_option( 'hos_hidden_shortcodes', array() );
if( isset( $_GET['action'] ) AND isset( $hidden_shortcodes[ esc_attr( $_GET['key'] ) ] ) ) {
switch ( $_GET['action'] ) {
case 'escape':
$display_shortcode = $hidden_shortcodes[ esc_attr( $_GET['key'] ) ];
hos_escape_shortcode( $display_shortcode['post_id'], esc_attr( $_GET['key'] ), $display_shortcode['raw'] );
$message[] = array( 'type' => 'updated', 'message' => 'Shortcode ' . $display_shortcode['raw'] . ' will now show on the frontend.' );
unset( $hidden_shortcodes[ esc_attr( $_GET['key'] ) ] );
break;
case 'remove':
$remove_shortcode = $hidden_shortcodes[ esc_attr( $_GET['key'] ) ];
hos_remove_shortcode( $remove_shortcode['post_id'], esc_attr( $_GET['key'] ), $remove_shortcode['raw'] );
$message[] = array( 'type' => 'updated', 'message' => 'Shortcode "' . $remove_shortcode['raw'] . '" has been removed.' );
unset( $hidden_shortcodes[ esc_attr( $_GET['key'] ) ] );
break;
case 'ignore':
$ignore_shortcode = $hidden_shortcodes[ esc_attr( $_GET['key'] ) ];
hos_ignore_shortcode( $ignore_shortcode['post_id'], esc_attr( $_GET['key'] ), $ignore_shortcode['raw'] );
$message[] = array( 'type' => 'updated', 'message' => 'Shortcode "' . $ignore_shortcode['raw'] . '" will be ignored.' );
unset( $hidden_shortcodes[ esc_attr( $_GET['key'] ) ] );
break;
}
}
?>
<div class="wrap">
<h2>Hide Old Shortcodes</h2>
<h4>Here is a list of shortcodes that have been blocked from being viewed in the content.</h4>
<?php
if( count( $message ) > 0 ){
for ( $i=0; $i < count( $message ); $i++ ) {
echo "<div class='" . $message[ $i ]['type'] . "'><p>" . $message[ $i ]['message'] . "</p></div>";
}
}
?>
<table class="widefat">
<thead>
<tr>
<th>Shortcode</th>
<th>Raw Usage</th>
<th>Actions</th>
</tr>
</thead>
<?php
foreach ( $hidden_shortcodes as $hidden_shortcode_k => $hidden_shortcode_v ) {
echo "<tr>
<td>" . $hidden_shortcode_v['shortcode'] . "</td>
<td>" . $hidden_shortcode_v['raw'] . "</td>
<td>
<a href='post.php?post=" . $hidden_shortcode_v['post_id'] . "&action=edit'>Edit " . $hidden_shortcode_v['post_type'] . "</a> |
<a href='tools.php?page=hide-old-shortcodes&action=escape&key=" . $hidden_shortcode_k . "'>Escape/Allow</a> |
<a href='tools.php?page=hide-old-shortcodes&action=ignore&key=" . $hidden_shortcode_k . "'>Ignore</a> |
<a href='tools.php?page=hide-old-shortcodes&action=remove&key=" . $hidden_shortcode_k . "'>Remove</a>
</td>
</tr>";
}
?>
<tr>
<td><p><a href="tools.php?page=hide-old-shortcodes&clear=true">Clear Log</a></p></td>
<td></td>
<td></td>
</tr>
</table>
</div>
<?php
}
function hos_escape_shortcode( $id, $key, $raw ) {
$post = get_post( $id, ARRAY_A );
$post['post_content'] = str_replace( $raw, '[' . $raw . ']', $post['post_content'] );
wp_update_post( $post );
$shortcode_log = get_option( 'hos_hidden_shortcodes', array() );
unset( $shortcode_log[ $key ] );
update_option( 'hos_hidden_shortcodes', $shortcode_log );
}
function hos_remove_shortcode( $id, $key, $raw ) {
$post = get_post( $id, ARRAY_A );
$post['post_content'] = str_replace( $raw, '', $post['post_content'] );
wp_update_post( $post );
$shortcode_log = get_option( 'hos_hidden_shortcodes', array() );
unset( $shortcode_log[ $key ] );
update_option( 'hos_hidden_shortcodes', $shortcode_log );
}
function hos_ignore_shortcode( $id, $key, $raw ) {
$ignored_shortcodes = get_option( 'hos_ignored_shortcodes', array() );
$ignored_shortcodes[ $key ] = $raw;
update_option( 'hos_ignored_shortcodes', $ignored_shortcodes );
$shortcode_log = get_option( 'hos_hidden_shortcodes', array() );
unset( $shortcode_log[ $key ] );
update_option( 'hos_hidden_shortcodes', $shortcode_log );
}
function hos_load_updater() {
// Load base classes for github updater
if ( is_admin() ) {
/*
Check class_exist because this could be loaded in a different plugin
*/
if( ! class_exists( 'GitHub_Updater' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'updater/class-github-updater.php' );
}
if( ! class_exists( 'GitHub_Updater_GitHub_API' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'updater/class-github-api.php' );
}
if( ! class_exists( 'GitHub_Plugin_Updater' ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'updater/class-plugin-updater.php' );
}
new GitHub_Plugin_Updater;
}
}
add_action( 'admin_init', 'hos_load_updater' );